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.

213 lines
7.3KB

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