Jack2  1.9.8
JackCoreAudioDriver.h
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 JackCoreAudioDriver : public JackAudioDriver
00054 {
00055 
00056     private:
00057 
00058         AudioUnit fAUHAL;
00059 
00060         AudioBufferList* fJackInputData;
00061         AudioBufferList* fDriverOutputData;
00062 
00063         AudioDeviceID fDeviceID;    // Used "duplex" device
00064         AudioObjectID fPluginID;    // Used for aggregate device
00065     
00066         AudioUnitRenderActionFlags* fActionFags;
00067         const AudioTimeStamp* fCurrentTime;
00068 
00069         bool fState;
00070         bool fHogged;
00071 
00072         char fCaptureUID[256];
00073         char fPlaybackUID[256];
00074 
00075         float fIOUsage;
00076         float fComputationGrain;
00077         bool fClockDriftCompensate;
00078    
00079     
00080         static OSStatus Render(void *inRefCon,
00081                                AudioUnitRenderActionFlags *ioActionFlags,
00082                                const AudioTimeStamp *inTimeStamp,
00083                                UInt32 inBusNumber,
00084                                UInt32 inNumberFrames,
00085                                AudioBufferList *ioData);
00086     
00087         static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
00088    
00089         static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
00090                                                     UInt32 inChannel,
00091                                                     Boolean     isInput,
00092                                                     AudioDevicePropertyID inPropertyID,
00093                                                     void* inClientData);
00094 
00095         static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
00096                                                UInt32 inChannel,
00097                                                Boolean  isInput,
00098                                                AudioDevicePropertyID inPropertyID,
00099                                                void* inClientData);
00100 
00101         static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
00102                                                 UInt32 inChannel,
00103                                                 Boolean isInput,
00104                                                 AudioDevicePropertyID inPropertyID,
00105                                                 void* inClientData);
00106 
00107         OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
00108         OSStatus GetDefaultDevice(AudioDeviceID* id);
00109         OSStatus GetDefaultInputDevice(AudioDeviceID* id);
00110         OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
00111         OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
00112         OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
00113         OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
00114 
00115         // Setup
00116         OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
00117         OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
00118         OSStatus DestroyAggregateDevice();
00119         bool IsAggregateDevice(AudioDeviceID device);
00120 
00121         int SetupDevices(const char* capture_driver_uid,
00122                          const char* playback_driver_uid,
00123                          char* capture_driver_name,
00124                          char* playback_driver_name,
00125                          jack_nframes_t samplerate);
00126 
00127         int SetupChannels(bool capturing,
00128                           bool playing,
00129                           int& inchannels,
00130                           int& outchannels,
00131                           int& in_nChannels,
00132                           int& out_nChannels,
00133                           bool strict);
00134 
00135         int SetupBuffers(int inchannels);
00136         void DisposeBuffers();
00137 
00138         int SetupBufferSize(jack_nframes_t buffer_size);
00139         int SetupSampleRate(jack_nframes_t samplerate);
00140         int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
00141 
00142         int OpenAUHAL(bool capturing,
00143                       bool playing,
00144                       int inchannels,
00145                       int outchannels,
00146                       int in_nChannels,
00147                       int out_nChannels,
00148                       const vector<int>& chan_in_list,
00149                       const vector<int>& chan_out_list,
00150                       jack_nframes_t nframes,
00151                       jack_nframes_t samplerate);
00152         void CloseAUHAL();
00153 
00154         int AddListeners();
00155         void RemoveListeners();
00156 
00157         bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
00158         bool TakeHog();
00159 
00160         void UpdateLatencies();
00161 
00162     public:
00163 
00164         JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
00165         virtual ~JackCoreAudioDriver();
00166 
00167         int Open(jack_nframes_t buffer_size,
00168                  jack_nframes_t samplerate,
00169                  bool capturing,
00170                  bool playing,
00171                  int inchannels,
00172                  int outchannels,
00173                  const char* chan_in_list,
00174                  const char* chan_out_list,
00175                  bool monitor,
00176                  const char* capture_driver_name,
00177                  const char* playback_driver_name,
00178                  jack_nframes_t capture_latency,
00179                  jack_nframes_t playback_latency,
00180                  int async_output_latency,
00181                  int computation_grain,
00182                  bool hogged,
00183                  bool clock_drift);
00184         int Close();
00185 
00186         int Attach();
00187 
00188         int Start();
00189         int Stop();
00190 
00191         int Read();
00192         int Write();
00193 
00194         // BufferSize can be changed
00195         bool IsFixedBufferSize()
00196         {
00197             return false;
00198         }
00199 
00200         int SetBufferSize(jack_nframes_t buffer_size);
00201 };
00202 
00203 } // end of namespace
00204 
00205 #endif