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.7KB

  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. #include <vector>
  23. using namespace std;
  24. namespace Jack
  25. {
  26. #define kVersion 102
  27. typedef UInt8 CAAudioHardwareDeviceSectionID;
  28. #define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01)
  29. #define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00)
  30. #define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00)
  31. #define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF)
  32. #define WAIT_COUNTER 60
  33. #define WAIT_NOTIFICATION_COUNTER 30
  34. /*!
  35. \brief The CoreAudio driver.
  36. \todo hardware monitoring
  37. */
  38. class JackCoreAudioDriver : public JackAudioDriver
  39. {
  40. private:
  41. AudioUnit fAUHAL;
  42. AudioBufferList* fJackInputData;
  43. AudioBufferList* fDriverOutputData;
  44. AudioDeviceID fDeviceID; // Used "duplex" device
  45. AudioObjectID fPluginID; // Used for aggregate device
  46. AudioUnitRenderActionFlags* fActionFags;
  47. AudioTimeStamp* fCurrentTime;
  48. bool fState;
  49. bool fHogged;
  50. char fCaptureUID[256];
  51. char fPlaybackUID[256];
  52. float fIOUsage;
  53. float fComputationGrain;
  54. bool fClockDriftCompensate;
  55. /*
  56. #ifdef MAC_OS_X_VERSION_10_5
  57. AudioDeviceIOProcID fMesureCallbackID;
  58. #endif
  59. */
  60. static OSStatus Render(void *inRefCon,
  61. AudioUnitRenderActionFlags *ioActionFlags,
  62. const AudioTimeStamp *inTimeStamp,
  63. UInt32 inBusNumber,
  64. UInt32 inNumberFrames,
  65. AudioBufferList *ioData);
  66. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  67. UInt32 inChannel,
  68. Boolean isInput,
  69. AudioDevicePropertyID inPropertyID,
  70. void* inClientData);
  71. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  72. UInt32 inChannel,
  73. Boolean isInput,
  74. AudioDevicePropertyID inPropertyID,
  75. void* inClientData);
  76. static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
  77. UInt32 inChannel,
  78. Boolean isInput,
  79. AudioDevicePropertyID inPropertyID,
  80. void* inClientData);
  81. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  82. OSStatus GetDefaultDevice(AudioDeviceID* id);
  83. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  84. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  85. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  86. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  87. // Setup
  88. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  89. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  90. OSStatus DestroyAggregateDevice();
  91. bool IsAggregateDevice(AudioDeviceID device);
  92. int SetupDevices(const char* capture_driver_uid,
  93. const char* playback_driver_uid,
  94. char* capture_driver_name,
  95. char* playback_driver_name,
  96. jack_nframes_t samplerate);
  97. int SetupChannels(bool capturing,
  98. bool playing,
  99. int& inchannels,
  100. int& outchannels,
  101. int& in_nChannels,
  102. int& out_nChannels,
  103. bool strict);
  104. int SetupBuffers(int inchannels);
  105. void DisposeBuffers();
  106. int SetupBufferSize(jack_nframes_t buffer_size);
  107. int SetupSampleRate(jack_nframes_t samplerate);
  108. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  109. int OpenAUHAL(bool capturing,
  110. bool playing,
  111. int inchannels,
  112. int outchannels,
  113. int in_nChannels,
  114. int out_nChannels,
  115. jack_nframes_t nframes,
  116. jack_nframes_t samplerate);
  117. void CloseAUHAL();
  118. int AddListeners();
  119. void RemoveListeners();
  120. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  121. bool TakeHog();
  122. void UpdateLatencies();
  123. public:
  124. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  125. virtual ~JackCoreAudioDriver();
  126. int Open(jack_nframes_t buffer_size,
  127. jack_nframes_t samplerate,
  128. bool capturing,
  129. bool playing,
  130. int chan_in,
  131. int chan_out,
  132. bool monitor,
  133. const char* capture_driver_name,
  134. const char* playback_driver_name,
  135. jack_nframes_t capture_latency,
  136. jack_nframes_t playback_latency,
  137. int async_output_latency,
  138. int computation_grain,
  139. bool hogged,
  140. bool clock_drift);
  141. int Close();
  142. int Attach();
  143. int Start();
  144. int Stop();
  145. int Read();
  146. int Write();
  147. // BufferSize can be changed
  148. bool IsFixedBufferSize()
  149. {
  150. return false;
  151. }
  152. int SetBufferSize(jack_nframes_t buffer_size);
  153. };
  154. } // end of namespace
  155. #endif