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

  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. const 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. static OSStatus Render(void *inRefCon,
  56. AudioUnitRenderActionFlags *ioActionFlags,
  57. const AudioTimeStamp *inTimeStamp,
  58. UInt32 inBusNumber,
  59. UInt32 inNumberFrames,
  60. AudioBufferList *ioData);
  61. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  62. UInt32 inChannel,
  63. Boolean isInput,
  64. AudioDevicePropertyID inPropertyID,
  65. void* inClientData);
  66. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  67. UInt32 inChannel,
  68. Boolean isInput,
  69. AudioDevicePropertyID inPropertyID,
  70. void* inClientData);
  71. static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
  72. UInt32 inChannel,
  73. Boolean isInput,
  74. AudioDevicePropertyID inPropertyID,
  75. void* inClientData);
  76. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  77. OSStatus GetDefaultDevice(AudioDeviceID* id);
  78. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  79. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  80. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  81. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  82. OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
  83. // Setup
  84. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  85. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  86. OSStatus DestroyAggregateDevice();
  87. bool IsAggregateDevice(AudioDeviceID device);
  88. int SetupDevices(const char* capture_driver_uid,
  89. const char* playback_driver_uid,
  90. char* capture_driver_name,
  91. char* playback_driver_name,
  92. jack_nframes_t samplerate);
  93. int SetupChannels(bool capturing,
  94. bool playing,
  95. int& inchannels,
  96. int& outchannels,
  97. int& in_nChannels,
  98. int& out_nChannels,
  99. bool strict);
  100. int SetupBuffers(int inchannels);
  101. void DisposeBuffers();
  102. int SetupBufferSize(jack_nframes_t buffer_size);
  103. int SetupSampleRate(jack_nframes_t samplerate);
  104. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  105. int OpenAUHAL(bool capturing,
  106. bool playing,
  107. int inchannels,
  108. int outchannels,
  109. int in_nChannels,
  110. int out_nChannels,
  111. const vector<int>& chan_in_list,
  112. const vector<int>& chan_out_list,
  113. jack_nframes_t nframes,
  114. jack_nframes_t samplerate);
  115. void CloseAUHAL();
  116. int AddListeners();
  117. void RemoveListeners();
  118. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  119. bool TakeHog();
  120. void UpdateLatencies();
  121. public:
  122. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  123. virtual ~JackCoreAudioDriver();
  124. int Open(jack_nframes_t buffer_size,
  125. jack_nframes_t samplerate,
  126. bool capturing,
  127. bool playing,
  128. int inchannels,
  129. int outchannels,
  130. const char* chan_in_list,
  131. const char* chan_out_list,
  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