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.

209 lines
6.8KB

  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. /*
  60. #ifdef MAC_OS_X_VERSION_10_5
  61. AudioDeviceIOProcID fMesureCallbackID;
  62. #endif
  63. */
  64. static OSStatus Render(void *inRefCon,
  65. AudioUnitRenderActionFlags *ioActionFlags,
  66. const AudioTimeStamp *inTimeStamp,
  67. UInt32 inBusNumber,
  68. UInt32 inNumberFrames,
  69. AudioBufferList *ioData);
  70. static OSStatus MeasureCallback(AudioDeviceID inDevice,
  71. const AudioTimeStamp* inNow,
  72. const AudioBufferList* inInputData,
  73. const AudioTimeStamp* inInputTime,
  74. AudioBufferList* outOutputData,
  75. const AudioTimeStamp* inOutputTime,
  76. void* inClientData);
  77. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  78. UInt32 inChannel,
  79. Boolean isInput,
  80. AudioDevicePropertyID inPropertyID,
  81. void* inClientData);
  82. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  83. UInt32 inChannel,
  84. Boolean isInput,
  85. AudioDevicePropertyID inPropertyID,
  86. void* inClientData);
  87. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  88. OSStatus GetDefaultDevice(AudioDeviceID* id);
  89. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  90. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  91. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  92. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  93. // Setup
  94. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  95. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  96. OSStatus DestroyAggregateDevice();
  97. bool IsAggregateDevice(AudioDeviceID device);
  98. int SetupDevices(const char* capture_driver_uid,
  99. const char* playback_driver_uid,
  100. char* capture_driver_name,
  101. char* playback_driver_name,
  102. jack_nframes_t samplerate);
  103. int SetupChannels(bool capturing,
  104. bool playing,
  105. int& inchannels,
  106. int& outchannels,
  107. int& in_nChannels,
  108. int& out_nChannels,
  109. bool strict);
  110. int SetupBuffers(int inchannels);
  111. void DisposeBuffers();
  112. int SetupBufferSize(jack_nframes_t buffer_size);
  113. int SetupSampleRate(jack_nframes_t samplerate);
  114. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  115. int OpenAUHAL(bool capturing,
  116. bool playing,
  117. int inchannels,
  118. int outchannels,
  119. int in_nChannels,
  120. int out_nChannels,
  121. jack_nframes_t nframes,
  122. jack_nframes_t samplerate);
  123. void CloseAUHAL();
  124. int AddListeners();
  125. void RemoveListeners();
  126. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  127. bool TakeHog();
  128. public:
  129. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  130. virtual ~JackCoreAudioDriver();
  131. int Open(jack_nframes_t buffer_size,
  132. jack_nframes_t samplerate,
  133. bool capturing,
  134. bool playing,
  135. int chan_in,
  136. int chan_out,
  137. bool monitor,
  138. const char* capture_driver_name,
  139. const char* playback_driver_name,
  140. jack_nframes_t capture_latency,
  141. jack_nframes_t playback_latency,
  142. int async_output_latency,
  143. int computation_grain,
  144. bool hogged);
  145. int Close();
  146. int Attach();
  147. int Start();
  148. int Stop();
  149. int Read();
  150. int Write();
  151. // BufferSize can be changed
  152. bool IsFixedBufferSize()
  153. {
  154. return false;
  155. }
  156. int SetBufferSize(jack_nframes_t buffer_size);
  157. };
  158. } // end of namespace
  159. #endif