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.

203 lines
6.4KB

  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. /*!
  34. \brief The CoreAudio driver.
  35. \todo hardware monitoring
  36. */
  37. class JackCoreAudioDriver : public JackAudioDriver
  38. {
  39. private:
  40. AudioUnit fAUHAL;
  41. AudioBufferList* fJackInputData;
  42. AudioBufferList* fDriverOutputData;
  43. AudioDeviceID fDeviceID; // Used "duplex" device
  44. AudioObjectID fPluginID; // Used for aggregate device
  45. AudioUnitRenderActionFlags* fActionFags;
  46. AudioTimeStamp* fCurrentTime;
  47. bool fState;
  48. bool fHogged;
  49. // Initial state
  50. bool fCapturing;
  51. bool fPlaying;
  52. int fInChannels;
  53. int fOutChannels;
  54. char fCaptureUID[256];
  55. char fPlaybackUID[256];
  56. bool fMonitor;
  57. float fIOUsage;
  58. float fComputationGrain;
  59. bool fClockDriftCompensate;
  60. /*
  61. #ifdef MAC_OS_X_VERSION_10_5
  62. AudioDeviceIOProcID fMesureCallbackID;
  63. #endif
  64. */
  65. static OSStatus Render(void *inRefCon,
  66. AudioUnitRenderActionFlags *ioActionFlags,
  67. const AudioTimeStamp *inTimeStamp,
  68. UInt32 inBusNumber,
  69. UInt32 inNumberFrames,
  70. AudioBufferList *ioData);
  71. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  72. UInt32 inChannel,
  73. Boolean isInput,
  74. AudioDevicePropertyID inPropertyID,
  75. void* inClientData);
  76. static OSStatus SRNotificationCallback(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. public:
  123. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  124. virtual ~JackCoreAudioDriver();
  125. int Open(jack_nframes_t buffer_size,
  126. jack_nframes_t samplerate,
  127. bool capturing,
  128. bool playing,
  129. int chan_in,
  130. int chan_out,
  131. bool monitor,
  132. const char* capture_driver_name,
  133. const char* playback_driver_name,
  134. jack_nframes_t capture_latency,
  135. jack_nframes_t playback_latency,
  136. int async_output_latency,
  137. int computation_grain,
  138. bool hogged,
  139. bool clock_drift);
  140. int Close();
  141. int Attach();
  142. int Start();
  143. int Stop();
  144. int Read();
  145. int Write();
  146. // BufferSize can be changed
  147. bool IsFixedBufferSize()
  148. {
  149. return false;
  150. }
  151. int SetBufferSize(jack_nframes_t buffer_size);
  152. };
  153. } // end of namespace
  154. #endif