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.

185 lines
6.3KB

  1. /*
  2. Copyright (C) 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 __JackCoreAudioAdapter__
  16. #define __JackCoreAudioAdapter__
  17. #include "JackAudioAdapterInterface.h"
  18. #include "jack.h"
  19. #include "jslist.h"
  20. #include <AudioToolbox/AudioConverter.h>
  21. #include <CoreAudio/CoreAudio.h>
  22. #include <AudioUnit/AudioUnit.h>
  23. #include <vector>
  24. using namespace std;
  25. namespace Jack
  26. {
  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 Audio adapter using CoreAudio API.
  35. */
  36. class JackCoreAudioAdapter : public JackAudioAdapterInterface
  37. {
  38. private:
  39. AudioUnit fAUHAL;
  40. AudioBufferList* fInputData;
  41. char fCaptureUID[256];
  42. char fPlaybackUID[256];
  43. bool fCapturing;
  44. bool fPlaying;
  45. AudioDeviceID fDeviceID; // Used "duplex" device
  46. AudioObjectID fPluginID; // Used for aggregate device
  47. vector<int> fInputLatencies;
  48. vector<int> fOutputLatencies;
  49. bool fState;
  50. AudioUnitRenderActionFlags* fActionFags;
  51. AudioTimeStamp* fCurrentTime;
  52. bool fClockDriftCompensate;
  53. static OSStatus Render(void *inRefCon,
  54. AudioUnitRenderActionFlags *ioActionFlags,
  55. const AudioTimeStamp *inTimeStamp,
  56. UInt32 inBusNumber,
  57. UInt32 inNumberFrames,
  58. AudioBufferList *ioData);
  59. static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
  60. static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
  61. UInt32 inChannel,
  62. Boolean isInput,
  63. AudioDevicePropertyID inPropertyID,
  64. void* inClientData);
  65. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  66. UInt32 inChannel,
  67. Boolean isInput,
  68. AudioDevicePropertyID inPropertyID,
  69. void* inClientData);
  70. OSStatus GetDefaultDevice(AudioDeviceID* id);
  71. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  72. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  73. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  74. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  75. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  76. AudioDeviceID GetDeviceIDFromName(const char* name);
  77. // Setup
  78. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  79. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  80. OSStatus DestroyAggregateDevice();
  81. bool IsAggregateDevice(AudioDeviceID device);
  82. int SetupDevices(const char* capture_driver_uid,
  83. const char* playback_driver_uid,
  84. char* capture_driver_name,
  85. char* playback_driver_name,
  86. jack_nframes_t samplerate);
  87. int SetupChannels(bool capturing,
  88. bool playing,
  89. int& inchannels,
  90. int& outchannels,
  91. int& in_nChannels,
  92. int& out_nChannels,
  93. bool strict);
  94. int OpenAUHAL(bool capturing,
  95. bool playing,
  96. int inchannels,
  97. int outchannels,
  98. int in_nChannels,
  99. int out_nChannels,
  100. jack_nframes_t buffer_size,
  101. jack_nframes_t samplerate);
  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 SetupBuffers(int inchannels);
  106. void DisposeBuffers();
  107. void CloseAUHAL();
  108. int AddListeners();
  109. void RemoveListeners();
  110. int GetLatency(int port_index, bool input);
  111. OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
  112. OSStatus Render(AudioUnitRenderActionFlags *ioActionFlags,
  113. const AudioTimeStamp *inTimeStamp,
  114. UInt32 inNumberFrames,
  115. AudioBufferList *ioData);
  116. public:
  117. JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params);
  118. ~JackCoreAudioAdapter()
  119. {}
  120. virtual int Open();
  121. virtual int Close();
  122. virtual int SetSampleRate(jack_nframes_t sample_rate);
  123. virtual int SetBufferSize(jack_nframes_t buffer_size);
  124. virtual int GetInputLatency(int port_index);
  125. virtual int GetOutputLatency(int port_index);
  126. };
  127. } // end of namespace
  128. #ifdef __cplusplus
  129. extern "C"
  130. {
  131. #endif
  132. #include "JackCompilerDeps.h"
  133. #include "driver_interface.h"
  134. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor();
  135. #ifdef __cplusplus
  136. }
  137. #endif
  138. #endif