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.

217 lines
7.5KB

  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 JackAC3Encoder;
  39. class JackCoreAudioDriver : public JackAudioDriver
  40. {
  41. private:
  42. JackAC3Encoder* fAC3Encoder;
  43. AudioUnit fAUHAL;
  44. AudioBufferList* fJackInputData;
  45. AudioBufferList* fDriverOutputData;
  46. AudioDeviceID fDeviceID; // Used "duplex" device
  47. AudioObjectID fPluginID; // Used for aggregate device
  48. AudioUnitRenderActionFlags* fActionFags;
  49. const AudioTimeStamp* fCurrentTime;
  50. bool fState;
  51. bool fHogged;
  52. char fCaptureUID[256];
  53. char fPlaybackUID[256];
  54. float fIOUsage;
  55. float fComputationGrain;
  56. bool fClockDriftCompensate;
  57. bool fDigitalPlayback;
  58. static OSStatus Render(void *inRefCon,
  59. AudioUnitRenderActionFlags *ioActionFlags,
  60. const AudioTimeStamp *inTimeStamp,
  61. UInt32 inBusNumber,
  62. UInt32 inNumberFrames,
  63. AudioBufferList *ioData);
  64. static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
  65. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  66. UInt32 inChannel,
  67. Boolean isInput,
  68. AudioDevicePropertyID inPropertyID,
  69. void* inClientData);
  70. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  71. UInt32 inChannel,
  72. Boolean isInput,
  73. AudioDevicePropertyID inPropertyID,
  74. void* inClientData);
  75. static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
  76. UInt32 inChannel,
  77. Boolean isInput,
  78. AudioDevicePropertyID inPropertyID,
  79. void* inClientData);
  80. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  81. OSStatus GetDefaultDevice(AudioDeviceID* id);
  82. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  83. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  84. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  85. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  86. OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
  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. bool ac3_encoding);
  98. int SetupChannels(bool capturing,
  99. bool playing,
  100. int& inchannels,
  101. int& outchannels,
  102. int& in_nChannels,
  103. int& out_nChannels,
  104. bool strict);
  105. int SetupBuffers(int inchannels);
  106. void DisposeBuffers();
  107. int SetupBufferSize(jack_nframes_t buffer_size);
  108. int SetupSampleRate(jack_nframes_t samplerate);
  109. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  110. int OpenAUHAL(bool capturing,
  111. bool playing,
  112. int inchannels,
  113. int outchannels,
  114. int in_nChannels,
  115. int out_nChannels,
  116. const vector<int>& chan_in_list,
  117. const vector<int>& chan_out_list,
  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. void UpdateLatencies();
  126. bool IsDigitalDevice(AudioDeviceID device);
  127. OSStatus Render(AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, AudioBufferList* ioData);
  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 inchannels,
  136. int outchannels,
  137. const char* chan_in_list,
  138. const char* chan_out_list,
  139. bool monitor,
  140. const char* capture_driver_name,
  141. const char* playback_driver_name,
  142. jack_nframes_t capture_latency,
  143. jack_nframes_t playback_latency,
  144. int async_output_latency,
  145. int computation_grain,
  146. bool hogged,
  147. bool clock_drift,
  148. bool ac3_encoding,
  149. int ac3_bitrate,
  150. bool ac3_lfe);
  151. int Close();
  152. int Attach();
  153. int Start();
  154. int Stop();
  155. int Read();
  156. int Write();
  157. // BufferSize can be changed
  158. bool IsFixedBufferSize()
  159. {
  160. return false;
  161. }
  162. int SetBufferSize(jack_nframes_t buffer_size);
  163. };
  164. } // end of namespace
  165. #endif