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.

202 lines
6.9KB

  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. 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. // Setup
  83. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  84. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  85. OSStatus DestroyAggregateDevice();
  86. bool IsAggregateDevice(AudioDeviceID device);
  87. int SetupDevices(const char* capture_driver_uid,
  88. const char* playback_driver_uid,
  89. char* capture_driver_name,
  90. char* playback_driver_name,
  91. jack_nframes_t samplerate);
  92. int SetupChannels(bool capturing,
  93. bool playing,
  94. int& inchannels,
  95. int& outchannels,
  96. int& in_nChannels,
  97. int& out_nChannels,
  98. bool strict);
  99. int SetupBuffers(int inchannels);
  100. void DisposeBuffers();
  101. int SetupBufferSize(jack_nframes_t buffer_size);
  102. int SetupSampleRate(jack_nframes_t samplerate);
  103. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  104. int OpenAUHAL(bool capturing,
  105. bool playing,
  106. int inchannels,
  107. int outchannels,
  108. int in_nChannels,
  109. int out_nChannels,
  110. const vector<int>& chan_in_list,
  111. const vector<int>& chan_out_list,
  112. jack_nframes_t nframes,
  113. jack_nframes_t samplerate);
  114. void CloseAUHAL();
  115. int AddListeners();
  116. void RemoveListeners();
  117. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  118. bool TakeHog();
  119. void UpdateLatencies();
  120. public:
  121. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  122. virtual ~JackCoreAudioDriver();
  123. int Open(jack_nframes_t buffer_size,
  124. jack_nframes_t samplerate,
  125. bool capturing,
  126. bool playing,
  127. int inchannels,
  128. int outchannels,
  129. const char* chan_in_list,
  130. const char* chan_out_list,
  131. bool monitor,
  132. const char* capture_driver_name,
  133. const char* playback_driver_name,
  134. jack_nframes_t capture_latency,
  135. jack_nframes_t playback_latency,
  136. int async_output_latency,
  137. int computation_grain,
  138. bool hogged,
  139. bool clock_drift);
  140. int Close();
  141. int Attach();
  142. int Start();
  143. int Stop();
  144. int Read();
  145. int Write();
  146. // BufferSize can be changed
  147. bool IsFixedBufferSize()
  148. {
  149. return false;
  150. }
  151. int SetBufferSize(jack_nframes_t buffer_size);
  152. };
  153. } // end of namespace
  154. #endif