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.

195 lines
6.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. /*!
  34. \brief The CoreAudio driver.
  35. \todo hardware monitoring
  36. */
  37. class JackCoreAudioDriver : public JackAudioDriver
  38. {
  39. private:
  40. AudioUnit fAUHAL;
  41. AudioBufferList* fJackInputData;
  42. AudioBufferList* fDriverOutputData;
  43. AudioDeviceID fDeviceID; // Used "duplex" device
  44. AudioObjectID fPluginID; // Used for aggregate device
  45. AudioUnitRenderActionFlags* fActionFags;
  46. AudioTimeStamp* fCurrentTime;
  47. bool fState;
  48. bool fHogged;
  49. char fCaptureUID[256];
  50. char fPlaybackUID[256];
  51. float fIOUsage;
  52. float fComputationGrain;
  53. bool fClockDriftCompensate;
  54. /*
  55. #ifdef MAC_OS_X_VERSION_10_5
  56. AudioDeviceIOProcID fMesureCallbackID;
  57. #endif
  58. */
  59. static OSStatus Render(void *inRefCon,
  60. AudioUnitRenderActionFlags *ioActionFlags,
  61. const AudioTimeStamp *inTimeStamp,
  62. UInt32 inBusNumber,
  63. UInt32 inNumberFrames,
  64. AudioBufferList *ioData);
  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. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  76. OSStatus GetDefaultDevice(AudioDeviceID* id);
  77. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  78. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  79. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  80. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  81. // Setup
  82. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  83. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  84. OSStatus DestroyAggregateDevice();
  85. bool IsAggregateDevice(AudioDeviceID device);
  86. int SetupDevices(const char* capture_driver_uid,
  87. const char* playback_driver_uid,
  88. char* capture_driver_name,
  89. char* playback_driver_name,
  90. jack_nframes_t samplerate);
  91. int SetupChannels(bool capturing,
  92. bool playing,
  93. int& inchannels,
  94. int& outchannels,
  95. int& in_nChannels,
  96. int& out_nChannels,
  97. bool strict);
  98. int SetupBuffers(int inchannels);
  99. void DisposeBuffers();
  100. int SetupBufferSize(jack_nframes_t buffer_size);
  101. int SetupSampleRate(jack_nframes_t samplerate);
  102. int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
  103. int OpenAUHAL(bool capturing,
  104. bool playing,
  105. int inchannels,
  106. int outchannels,
  107. int in_nChannels,
  108. int out_nChannels,
  109. jack_nframes_t nframes,
  110. jack_nframes_t samplerate);
  111. void CloseAUHAL();
  112. int AddListeners();
  113. void RemoveListeners();
  114. bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
  115. bool TakeHog();
  116. public:
  117. JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
  118. virtual ~JackCoreAudioDriver();
  119. int Open(jack_nframes_t buffer_size,
  120. jack_nframes_t samplerate,
  121. bool capturing,
  122. bool playing,
  123. int chan_in,
  124. int chan_out,
  125. bool monitor,
  126. const char* capture_driver_name,
  127. const char* playback_driver_name,
  128. jack_nframes_t capture_latency,
  129. jack_nframes_t playback_latency,
  130. int async_output_latency,
  131. int computation_grain,
  132. bool hogged,
  133. bool clock_drift);
  134. int Close();
  135. int Attach();
  136. int Start();
  137. int Stop();
  138. int Read();
  139. int Write();
  140. // BufferSize can be changed
  141. bool IsFixedBufferSize()
  142. {
  143. return false;
  144. }
  145. int SetBufferSize(jack_nframes_t buffer_size);
  146. };
  147. } // end of namespace
  148. #endif