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.

178 lines
6.0KB

  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 SRNotificationCallback(AudioDeviceID inDevice,
  60. UInt32 inChannel,
  61. Boolean isInput,
  62. AudioDevicePropertyID inPropertyID,
  63. void* inClientData);
  64. static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
  65. UInt32 inChannel,
  66. Boolean isInput,
  67. AudioDevicePropertyID inPropertyID,
  68. void* inClientData);
  69. OSStatus GetDefaultDevice(AudioDeviceID* id);
  70. OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
  71. OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
  72. OSStatus GetDefaultInputDevice(AudioDeviceID* id);
  73. OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
  74. OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
  75. AudioDeviceID GetDeviceIDFromName(const char* name);
  76. // Setup
  77. OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  78. OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
  79. OSStatus DestroyAggregateDevice();
  80. bool IsAggregateDevice(AudioDeviceID device);
  81. int SetupDevices(const char* capture_driver_uid,
  82. const char* playback_driver_uid,
  83. char* capture_driver_name,
  84. char* playback_driver_name,
  85. jack_nframes_t samplerate);
  86. int SetupChannels(bool capturing,
  87. bool playing,
  88. int& inchannels,
  89. int& outchannels,
  90. int& in_nChannels,
  91. int& out_nChannels,
  92. bool strict);
  93. int OpenAUHAL(bool capturing,
  94. bool playing,
  95. int inchannels,
  96. int outchannels,
  97. int in_nChannels,
  98. int out_nChannels,
  99. jack_nframes_t buffer_size,
  100. jack_nframes_t samplerate);
  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 SetupBuffers(int inchannels);
  105. void DisposeBuffers();
  106. void CloseAUHAL();
  107. int AddListeners();
  108. void RemoveListeners();
  109. int GetLatency(int port_index, bool input);
  110. OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
  111. public:
  112. JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params);
  113. ~JackCoreAudioAdapter()
  114. {}
  115. virtual int Open();
  116. virtual int Close();
  117. virtual int SetSampleRate(jack_nframes_t sample_rate);
  118. virtual int SetBufferSize(jack_nframes_t buffer_size);
  119. virtual int GetInputLatency(int port_index);
  120. virtual int GetOutputLatency(int port_index);
  121. };
  122. } // end of namepace
  123. #ifdef __cplusplus
  124. extern "C"
  125. {
  126. #endif
  127. #include "JackCompilerDeps.h"
  128. #include "driver_interface.h"
  129. SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor();
  130. #ifdef __cplusplus
  131. }
  132. #endif
  133. #endif