Jack2 1.9.10
|
00001 /* 00002 Copyright (C) 2004-2008 Grame 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00017 00018 */ 00019 00020 #ifndef __JackCoreAudioDriver__ 00021 #define __JackCoreAudioDriver__ 00022 00023 #include <AudioToolbox/AudioConverter.h> 00024 #include <CoreAudio/CoreAudio.h> 00025 #include <AudioUnit/AudioUnit.h> 00026 #include "JackAudioDriver.h" 00027 #include "JackTime.h" 00028 00029 #include <vector> 00030 00031 using namespace std; 00032 00033 namespace Jack 00034 { 00035 00036 #define kVersion 102 00037 00038 typedef UInt8 CAAudioHardwareDeviceSectionID; 00039 #define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01) 00040 #define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00) 00041 #define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00) 00042 #define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF) 00043 00044 #define WAIT_COUNTER 60 00045 #define WAIT_NOTIFICATION_COUNTER 30 00046 00053 class JackAC3Encoder; 00054 00055 class JackCoreAudioDriver : public JackAudioDriver 00056 { 00057 00058 private: 00059 00060 JackAC3Encoder* fAC3Encoder; 00061 00062 AudioUnit fAUHAL; 00063 00064 AudioBufferList* fJackInputData; 00065 AudioBufferList* fDriverOutputData; 00066 00067 AudioDeviceID fDeviceID; // Used "duplex" device 00068 AudioObjectID fPluginID; // Used for aggregate device 00069 00070 AudioUnitRenderActionFlags* fActionFags; 00071 const AudioTimeStamp* fCurrentTime; 00072 00073 bool fState; 00074 bool fHogged; 00075 00076 char fCaptureUID[256]; 00077 char fPlaybackUID[256]; 00078 00079 float fIOUsage; 00080 float fComputationGrain; 00081 bool fClockDriftCompensate; 00082 bool fDigitalPlayback; 00083 00084 static OSStatus Render(void *inRefCon, 00085 AudioUnitRenderActionFlags *ioActionFlags, 00086 const AudioTimeStamp *inTimeStamp, 00087 UInt32 inBusNumber, 00088 UInt32 inNumberFrames, 00089 AudioBufferList *ioData); 00090 00091 static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData); 00092 00093 static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice, 00094 UInt32 inChannel, 00095 Boolean isInput, 00096 AudioDevicePropertyID inPropertyID, 00097 void* inClientData); 00098 00099 static OSStatus SRNotificationCallback(AudioDeviceID inDevice, 00100 UInt32 inChannel, 00101 Boolean isInput, 00102 AudioDevicePropertyID inPropertyID, 00103 void* inClientData); 00104 00105 static OSStatus BSNotificationCallback(AudioDeviceID inDevice, 00106 UInt32 inChannel, 00107 Boolean isInput, 00108 AudioDevicePropertyID inPropertyID, 00109 void* inClientData); 00110 00111 OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id); 00112 OSStatus GetDefaultDevice(AudioDeviceID* id); 00113 OSStatus GetDefaultInputDevice(AudioDeviceID* id); 00114 OSStatus GetDefaultOutputDevice(AudioDeviceID* id); 00115 OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name); 00116 OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput); 00117 OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies); 00118 00119 // Setup 00120 OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice); 00121 OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice); 00122 OSStatus DestroyAggregateDevice(); 00123 bool IsAggregateDevice(AudioDeviceID device); 00124 00125 int SetupDevices(const char* capture_driver_uid, 00126 const char* playback_driver_uid, 00127 char* capture_driver_name, 00128 char* playback_driver_name, 00129 jack_nframes_t samplerate, 00130 bool ac3_encoding); 00131 00132 int SetupChannels(bool capturing, 00133 bool playing, 00134 int& inchannels, 00135 int& outchannels, 00136 int& in_nChannels, 00137 int& out_nChannels, 00138 bool strict); 00139 00140 int SetupBuffers(int inchannels); 00141 void DisposeBuffers(); 00142 00143 int SetupBufferSize(jack_nframes_t buffer_size); 00144 int SetupSampleRate(jack_nframes_t samplerate); 00145 int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate); 00146 00147 int OpenAUHAL(bool capturing, 00148 bool playing, 00149 int inchannels, 00150 int outchannels, 00151 int in_nChannels, 00152 int out_nChannels, 00153 const vector<int>& chan_in_list, 00154 const vector<int>& chan_out_list, 00155 jack_nframes_t nframes, 00156 jack_nframes_t samplerate); 00157 void CloseAUHAL(); 00158 00159 int AddListeners(); 00160 void RemoveListeners(); 00161 00162 bool TakeHogAux(AudioDeviceID deviceID, bool isInput); 00163 bool TakeHog(); 00164 00165 void UpdateLatencies(); 00166 00167 bool IsDigitalDevice(AudioDeviceID device); 00168 OSStatus Render(AudioUnitRenderActionFlags* ioActionFlags, const AudioTimeStamp* inTimeStamp, AudioBufferList* ioData); 00169 00170 public: 00171 00172 JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table); 00173 virtual ~JackCoreAudioDriver(); 00174 00175 int Open(jack_nframes_t buffer_size, 00176 jack_nframes_t samplerate, 00177 bool capturing, 00178 bool playing, 00179 int inchannels, 00180 int outchannels, 00181 const char* chan_in_list, 00182 const char* chan_out_list, 00183 bool monitor, 00184 const char* capture_driver_name, 00185 const char* playback_driver_name, 00186 jack_nframes_t capture_latency, 00187 jack_nframes_t playback_latency, 00188 int async_output_latency, 00189 int computation_grain, 00190 bool hogged, 00191 bool clock_drift, 00192 bool ac3_encoding, 00193 int ac3_bitrate, 00194 bool ac3_lfe); 00195 int Close(); 00196 00197 int Attach(); 00198 00199 int Start(); 00200 int Stop(); 00201 00202 int Read(); 00203 int Write(); 00204 00205 // BufferSize can be changed 00206 bool IsFixedBufferSize() 00207 { 00208 return false; 00209 } 00210 00211 int SetBufferSize(jack_nframes_t buffer_size); 00212 }; 00213 00214 } // end of namespace 00215 00216 #endif