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.

206 lines
7.1KB

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