Jack2  1.9.10
JackCoreAudioAdapter.h
00001 /*
00002 Copyright (C) 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 __JackCoreAudioAdapter__
00021 #define __JackCoreAudioAdapter__
00022 
00023 #include "JackAudioAdapterInterface.h"
00024 #include "jack.h"
00025 #include "jslist.h"
00026 #include <AudioToolbox/AudioConverter.h>
00027 #include <CoreAudio/CoreAudio.h>
00028 #include <AudioUnit/AudioUnit.h>
00029 
00030 #include <vector>
00031 
00032 using namespace std;
00033 
00034 namespace Jack
00035 {
00036 
00037 typedef UInt8   CAAudioHardwareDeviceSectionID;
00038 #define kAudioDeviceSectionInput        ((CAAudioHardwareDeviceSectionID)0x01)
00039 #define kAudioDeviceSectionOutput       ((CAAudioHardwareDeviceSectionID)0x00)
00040 #define kAudioDeviceSectionGlobal       ((CAAudioHardwareDeviceSectionID)0x00)
00041 #define kAudioDeviceSectionWildcard     ((CAAudioHardwareDeviceSectionID)0xFF)
00042 
00043 #define WAIT_COUNTER 60
00044 
00049 class JackCoreAudioAdapter : public JackAudioAdapterInterface
00050 {
00051 
00052     private:
00053 
00054         AudioUnit fAUHAL;
00055         AudioBufferList* fInputData;
00056 
00057         char fCaptureUID[256];
00058         char fPlaybackUID[256];
00059 
00060         bool fCapturing;
00061         bool fPlaying;
00062 
00063         AudioDeviceID fDeviceID;    // Used "duplex" device
00064         AudioObjectID fPluginID;    // Used for aggregate device
00065 
00066         vector<int> fInputLatencies;
00067         vector<int> fOutputLatencies;
00068 
00069         bool fState;
00070 
00071         AudioUnitRenderActionFlags* fActionFags;
00072         AudioTimeStamp* fCurrentTime;
00073         bool fClockDriftCompensate;
00074 
00075         static  OSStatus Render(void *inRefCon,
00076                                 AudioUnitRenderActionFlags *ioActionFlags,
00077                                 const AudioTimeStamp *inTimeStamp,
00078                                 UInt32 inBusNumber,
00079                                 UInt32 inNumberFrames,
00080                                 AudioBufferList *ioData);
00081 
00082         static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
00083 
00084         static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
00085                                                 UInt32 inChannel,
00086                                                 Boolean isInput,
00087                                                 AudioDevicePropertyID inPropertyID,
00088                                                 void* inClientData);
00089         static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
00090                                                     UInt32 inChannel,
00091                                                     Boolean     isInput,
00092                                                     AudioDevicePropertyID inPropertyID,
00093                                                     void* inClientData);
00094 
00095         OSStatus GetDefaultDevice(AudioDeviceID* id);
00096         OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
00097         OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
00098         OSStatus GetDefaultInputDevice(AudioDeviceID* id);
00099         OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
00100         OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
00101         AudioDeviceID GetDeviceIDFromName(const char* name);
00102 
00103         // Setup
00104         OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
00105         OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
00106         OSStatus DestroyAggregateDevice();
00107         bool IsAggregateDevice(AudioDeviceID device);
00108 
00109         int SetupDevices(const char* capture_driver_uid,
00110                          const char* playback_driver_uid,
00111                          char* capture_driver_name,
00112                          char* playback_driver_name,
00113                          jack_nframes_t samplerate);
00114 
00115         int SetupChannels(bool capturing,
00116                           bool playing,
00117                           int& inchannels,
00118                           int& outchannels,
00119                           int& in_nChannels,
00120                           int& out_nChannels,
00121                           bool strict);
00122 
00123         int OpenAUHAL(bool capturing,
00124                     bool playing,
00125                     int inchannels,
00126                     int outchannels,
00127                     int in_nChannels,
00128                     int out_nChannels,
00129                     jack_nframes_t buffer_size,
00130                     jack_nframes_t samplerate);
00131 
00132         int SetupBufferSize(jack_nframes_t buffer_size);
00133         int SetupSampleRate(jack_nframes_t samplerate);
00134         int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
00135 
00136         int SetupBuffers(int inchannels);
00137         void DisposeBuffers();
00138         void CloseAUHAL();
00139 
00140         int AddListeners();
00141         void RemoveListeners();
00142 
00143         int GetLatency(int port_index, bool input);
00144         OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
00145         
00146         OSStatus Render(AudioUnitRenderActionFlags *ioActionFlags,
00147                         const AudioTimeStamp *inTimeStamp,
00148                         UInt32 inNumberFrames,
00149                         AudioBufferList *ioData);
00150 
00151     public:
00152 
00153         JackCoreAudioAdapter(jack_nframes_t buffer_size, jack_nframes_t sample_rate, const JSList* params);
00154         ~JackCoreAudioAdapter()
00155         {}
00156 
00157         virtual int Open();
00158         virtual int Close();
00159 
00160         virtual int SetSampleRate(jack_nframes_t sample_rate);
00161         virtual int SetBufferSize(jack_nframes_t buffer_size);
00162 
00163         virtual int GetInputLatency(int port_index);
00164         virtual int GetOutputLatency(int port_index);
00165 };
00166 
00167 
00168 } // end of namepace
00169 
00170 #ifdef __cplusplus
00171 extern "C"
00172 {
00173 #endif
00174 
00175 #include "JackCompilerDeps.h"
00176 #include "driver_interface.h"
00177 
00178 SERVER_EXPORT jack_driver_desc_t* jack_get_descriptor();
00179 
00180 #ifdef __cplusplus
00181 }
00182 #endif
00183 
00184 #endif