Jack2  1.9.8
JackCoreAudioDriver.h
1 /*
2 Copyright (C) 2004-2008 Grame
3 
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 
18 */
19 
20 #ifndef __JackCoreAudioDriver__
21 #define __JackCoreAudioDriver__
22 
23 #include <AudioToolbox/AudioConverter.h>
24 #include <CoreAudio/CoreAudio.h>
25 #include <AudioUnit/AudioUnit.h>
26 #include "JackAudioDriver.h"
27 #include "JackTime.h"
28 
29 #include <vector>
30 
31 using namespace std;
32 
33 namespace Jack
34 {
35 
36 #define kVersion 102
37 
38 typedef UInt8 CAAudioHardwareDeviceSectionID;
39 #define kAudioDeviceSectionInput ((CAAudioHardwareDeviceSectionID)0x01)
40 #define kAudioDeviceSectionOutput ((CAAudioHardwareDeviceSectionID)0x00)
41 #define kAudioDeviceSectionGlobal ((CAAudioHardwareDeviceSectionID)0x00)
42 #define kAudioDeviceSectionWildcard ((CAAudioHardwareDeviceSectionID)0xFF)
43 
44 #define WAIT_COUNTER 60
45 #define WAIT_NOTIFICATION_COUNTER 30
46 
54 {
55 
56  private:
57 
58  AudioUnit fAUHAL;
59 
60  AudioBufferList* fJackInputData;
61  AudioBufferList* fDriverOutputData;
62 
63  AudioDeviceID fDeviceID; // Used "duplex" device
64  AudioObjectID fPluginID; // Used for aggregate device
65 
66  AudioUnitRenderActionFlags* fActionFags;
67  const AudioTimeStamp* fCurrentTime;
68 
69  bool fState;
70  bool fHogged;
71 
72  char fCaptureUID[256];
73  char fPlaybackUID[256];
74 
75  float fIOUsage;
76  float fComputationGrain;
77  bool fClockDriftCompensate;
78 
79 
80  static OSStatus Render(void *inRefCon,
81  AudioUnitRenderActionFlags *ioActionFlags,
82  const AudioTimeStamp *inTimeStamp,
83  UInt32 inBusNumber,
84  UInt32 inNumberFrames,
85  AudioBufferList *ioData);
86 
87  static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
88 
89  static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
90  UInt32 inChannel,
91  Boolean isInput,
92  AudioDevicePropertyID inPropertyID,
93  void* inClientData);
94 
95  static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
96  UInt32 inChannel,
97  Boolean isInput,
98  AudioDevicePropertyID inPropertyID,
99  void* inClientData);
100 
101  static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
102  UInt32 inChannel,
103  Boolean isInput,
104  AudioDevicePropertyID inPropertyID,
105  void* inClientData);
106 
107  OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
108  OSStatus GetDefaultDevice(AudioDeviceID* id);
109  OSStatus GetDefaultInputDevice(AudioDeviceID* id);
110  OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
111  OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
112  OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
113  OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
114 
115  // Setup
116  OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
117  OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
118  OSStatus DestroyAggregateDevice();
119  bool IsAggregateDevice(AudioDeviceID device);
120 
121  int SetupDevices(const char* capture_driver_uid,
122  const char* playback_driver_uid,
123  char* capture_driver_name,
124  char* playback_driver_name,
125  jack_nframes_t samplerate);
126 
127  int SetupChannels(bool capturing,
128  bool playing,
129  int& inchannels,
130  int& outchannels,
131  int& in_nChannels,
132  int& out_nChannels,
133  bool strict);
134 
135  int SetupBuffers(int inchannels);
136  void DisposeBuffers();
137 
138  int SetupBufferSize(jack_nframes_t buffer_size);
139  int SetupSampleRate(jack_nframes_t samplerate);
140  int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
141 
142  int OpenAUHAL(bool capturing,
143  bool playing,
144  int inchannels,
145  int outchannels,
146  int in_nChannels,
147  int out_nChannels,
148  const vector<int>& chan_in_list,
149  const vector<int>& chan_out_list,
150  jack_nframes_t nframes,
151  jack_nframes_t samplerate);
152  void CloseAUHAL();
153 
154  int AddListeners();
155  void RemoveListeners();
156 
157  bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
158  bool TakeHog();
159 
160  void UpdateLatencies();
161 
162  public:
163 
164  JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
165  virtual ~JackCoreAudioDriver();
166 
167  int Open(jack_nframes_t buffer_size,
168  jack_nframes_t samplerate,
169  bool capturing,
170  bool playing,
171  int inchannels,
172  int outchannels,
173  const char* chan_in_list,
174  const char* chan_out_list,
175  bool monitor,
176  const char* capture_driver_name,
177  const char* playback_driver_name,
178  jack_nframes_t capture_latency,
179  jack_nframes_t playback_latency,
180  int async_output_latency,
181  int computation_grain,
182  bool hogged,
183  bool clock_drift);
184  int Close();
185 
186  int Attach();
187 
188  int Start();
189  int Stop();
190 
191  int Read();
192  int Write();
193 
194  // BufferSize can be changed
195  bool IsFixedBufferSize()
196  {
197  return false;
198  }
199 
200  int SetBufferSize(jack_nframes_t buffer_size);
201 };
202 
203 } // end of namespace
204 
205 #endif