jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.6KB

  1. /*
  2. Copyright (C) 2004-2008 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #ifndef __JackCoreAudioDriver__
  16. #define __JackCoreAudioDriver__
  17. #include <AudioToolbox/AudioConverter.h>
  18. #include <CoreAudio/CoreAudio.h>
  19. #include <AudioUnit/AudioUnit.h>
  20. #include "JackAudioDriver.h"
  21. #include "JackTime.h"
  22. namespace Jack
  23. {
  24. #define kVersion 102
  25. typedef UInt8 CAAudioHardwareDeviceSectionID;
  26. #define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01)
  27. #define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00)
  28. #define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00)
  29. #define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF)
  30. #define WAIT_COUNTER 60
  31. /*!
  32. \brief The CoreAudio driver.
  33. \todo hardware monitoring
  34. */
  35. class JackCoreAudioDriver : public JackAudioDriver
  36. {
  37. private:
  38. AudioUnit fAUHAL;
  39. AudioBufferList* fJackInputData;
  40. AudioBufferList* fDriverOutputData;
  41. AudioDeviceID fDeviceID; // Used "duplex" device
  42. AudioObjectID fPluginID; // Used for aggregate device
  43. AudioUnitRenderActionFlags* fActionFags;
  44. AudioTimeStamp* fCurrentTime;
  45. bool fState;
  46. bool fHogged;
  47. // Initial state
  48. bool fCapturing;
  49. bool fPlaying;
  50. int fInChannels;
  51. int fOutChannels;
  52. char fCaptureUID[256];
  53. char fPlaybackUID[256];
  54. bool fMonitor;
  55. float fIOUsage;
  56. float fComputationGrain;
  57. /*
  58. #ifdef MAC_OS_X_VERSION_10_5
  59. AudioDeviceIOProcID fMesureCallbackID;
  60. #endif
  61. */
  62. static OSStatus Render(void *inRefCon,
  63. AudioUnitRenderActionFlags *ioActionFlags,
  64. const AudioTimeStamp *inTimeStamp,
  65. UInt32 inBusNumber,
  66. UInt32 inNumberFrames,
  67. AudioBufferList *ioData);
  68. static OSStatus MeasureCallback(AudioDeviceID inDevice,
  69. const AudioTimeStamp* inNow,
  70. const AudioBufferList* inInputData,
  71. const AudioTimeStamp* inInputTime,
  72. AudioBufferList* outOutputData,
  73. const AudioTimeStamp* inOutputTime,
  74. void* inClientData);
  75. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  76. UInt32 inChannel,
  77. Boolean isInput,
  78. AudioDevicePropertyID inPropertyID,
  79. void* inClientData);
  80. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  81. UInt32 inChannel,
  82. Boolean isInput,
  83. AudioDevicePropertyID inPropertyID,
  84. void* inClientData);
  85. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  86. OSStatus GetDefaultDevice(AudioDeviceID* id);
  87. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  88. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  89. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  90. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  91. // Setup
  92. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  93. OSStatus DestroyAggregateDevice();
  94. bool IsAggregateDevice(AudioDeviceID device);
  95. int SetupDevices(const char* capture_driver_uid,
  96. const char* playback_driver_uid,
  97. char* capture_driver_name,
  98. char* playback_driver_name,
  99. jack_nframes_t samplerate);
  100. int SetupChannels(bool capturing,
  101. bool playing,
  102. int& inchannels,
  103. int& outchannels,
  104. int& in_nChannels,
  105. int& out_nChannels,
  106. bool strict);
  107. int SetupBuffers(int inchannels);
  108. void DisposeBuffers();
  109. int SetupBufferSize(jack_nframes_t buffer_size);
  110. int SetupSampleRate(jack_nframes_t samplerate);
  111. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  112. int OpenAUHAL(bool capturing,
  113. bool playing,
  114. int inchannels,
  115. int outchannels,
  116. int in_nChannels,
  117. int out_nChannels,
  118. jack_nframes_t nframes,
  119. jack_nframes_t samplerate);
  120. void CloseAUHAL();
  121. int AddListeners();
  122. void RemoveListeners();
  123. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  124. bool TakeHog();
  125. public:
  126. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  127. virtual ~JackCoreAudioDriver();
  128. int Open(jack_nframes_t buffer_size,
  129. jack_nframes_t samplerate,
  130. bool capturing,
  131. bool playing,
  132. int chan_in,
  133. int chan_out,
  134. bool monitor,
  135. const char* capture_driver_name,
  136. const char* playback_driver_name,
  137. jack_nframes_t capture_latency,
  138. jack_nframes_t playback_latency,
  139. int async_output_latency,
  140. int computation_grain,
  141. bool hogged);
  142. int Close();
  143. int Attach();
  144. int Start();
  145. int Stop();
  146. int Read();
  147. int Write();
  148. // BufferSize can be changed
  149. bool IsFixedBufferSize()
  150. {
  151. return false;
  152. }
  153. int SetBufferSize(jack_nframes_t buffer_size);
  154. };
  155. } // end of namespace
  156. #endif