JackCoreAudioDriver.h 7.34 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __JackCoreAudioDriver__
#define __JackCoreAudioDriver__

#include <AudioToolbox/AudioConverter.h>
#include <CoreAudio/CoreAudio.h>
#include <AudioUnit/AudioUnit.h>
#include "JackAudioDriver.h"
#include "JackTime.h"

29
30
31
32
#include <vector>

using namespace std;

sletz's avatar
sletz committed
33
34
35
36
37
namespace Jack
{

#define kVersion 102

38
39
40
41
42
typedef	UInt8	CAAudioHardwareDeviceSectionID;
#define	kAudioDeviceSectionInput	((CAAudioHardwareDeviceSectionID)0x01)
#define	kAudioDeviceSectionOutput	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionGlobal	((CAAudioHardwareDeviceSectionID)0x00)
#define	kAudioDeviceSectionWildcard	((CAAudioHardwareDeviceSectionID)0xFF)
43

44
#define WAIT_COUNTER 60
45
#define WAIT_NOTIFICATION_COUNTER 30
46

sletz's avatar
sletz committed
47
48
/*!
\brief The CoreAudio driver.
sletz's avatar
sletz committed
49

sletz's avatar
sletz committed
50
51
52
\todo hardware monitoring
*/

Stephane Letz's avatar
Stephane Letz committed
53
54
class JackAC3Encoder;

55
class JackCoreAudioDriver : public JackAudioDriver
sletz's avatar
sletz committed
56
57
58
{

    private:
Stephane Letz's avatar
Stephane Letz committed
59
60
    
        JackAC3Encoder* fAC3Encoder;
sletz's avatar
sletz committed
61
62

        AudioUnit fAUHAL;
sletz's avatar
sletz committed
63
64
65
66

        AudioBufferList* fJackInputData;
        AudioBufferList* fDriverOutputData;

67
68
        AudioDeviceID fDeviceID;    // Used "duplex" device
        AudioObjectID fPluginID;    // Used for aggregate device
69
    
sletz's avatar
sletz committed
70
        AudioUnitRenderActionFlags* fActionFags;
Stephane Letz's avatar
Stephane Letz committed
71
        const AudioTimeStamp* fCurrentTime;
sletz's avatar
sletz committed
72
73

        bool fState;
74
        bool fHogged;
sletz's avatar
sletz committed
75
76
77
78

        char fCaptureUID[256];
        char fPlaybackUID[256];

79
        float fIOUsage;
80
        float fComputationGrain;
81
        bool fClockDriftCompensate;
82
83
   
    
sletz's avatar
sletz committed
84
        static OSStatus Render(void *inRefCon,
sletz's avatar
sletz committed
85
86
87
88
89
                               AudioUnitRenderActionFlags *ioActionFlags,
                               const AudioTimeStamp *inTimeStamp,
                               UInt32 inBusNumber,
                               UInt32 inNumberFrames,
                               AudioBufferList *ioData);
90
91
92
    
        static OSStatus AudioHardwareNotificationCallback(AudioHardwarePropertyID inPropertyID,void* inClientData);
   
sletz's avatar
sletz committed
93
        static OSStatus DeviceNotificationCallback(AudioDeviceID inDevice,
sletz's avatar
sletz committed
94
95
96
97
                                                    UInt32 inChannel,
                                                    Boolean	isInput,
                                                    AudioDevicePropertyID inPropertyID,
                                                    void* inClientData);
sletz's avatar
sletz committed
98
99
100
101
102
103

        static OSStatus SRNotificationCallback(AudioDeviceID inDevice,
                                               UInt32 inChannel,
                                               Boolean	isInput,
                                               AudioDevicePropertyID inPropertyID,
                                               void* inClientData);
sletz's avatar
sletz committed
104

105
106
107
108
109
        static OSStatus BSNotificationCallback(AudioDeviceID inDevice,
                                                UInt32 inChannel,
                                                Boolean	isInput,
                                                AudioDevicePropertyID inPropertyID,
                                                void* inClientData);
sletz's avatar
sletz committed
110

sletz's avatar
sletz committed
111
112
113
114
115
        OSStatus GetDeviceIDFromUID(const char* UID, AudioDeviceID* id);
        OSStatus GetDefaultDevice(AudioDeviceID* id);
        OSStatus GetDefaultInputDevice(AudioDeviceID* id);
        OSStatus GetDefaultOutputDevice(AudioDeviceID* id);
        OSStatus GetDeviceNameFromID(AudioDeviceID id, char* name);
116
        OSStatus GetTotalChannels(AudioDeviceID device, int& channelCount, bool isInput);
Stephane Letz's avatar
Stephane Letz committed
117
        OSStatus GetStreamLatencies(AudioDeviceID device, bool isInput, vector<int>& latencies);
118

sletz's avatar
sletz committed
119
        // Setup
120
        OSStatus CreateAggregateDevice(AudioDeviceID captureDeviceID, AudioDeviceID playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
121
        OSStatus CreateAggregateDeviceAux(vector<AudioDeviceID> captureDeviceID, vector<AudioDeviceID> playbackDeviceID, jack_nframes_t samplerate, AudioDeviceID* outAggregateDevice);
122
        OSStatus DestroyAggregateDevice();
123
        bool IsAggregateDevice(AudioDeviceID device);
124

sletz's avatar
sletz committed
125
126
127
        int SetupDevices(const char* capture_driver_uid,
                         const char* playback_driver_uid,
                         char* capture_driver_name,
128
129
                         char* playback_driver_name,
                         jack_nframes_t samplerate);
sletz's avatar
sletz committed
130
131
132
133
134
135
136
137
138

        int SetupChannels(bool capturing,
                          bool playing,
                          int& inchannels,
                          int& outchannels,
                          int& in_nChannels,
                          int& out_nChannels,
                          bool strict);

139
        int SetupBuffers(int inchannels);
sletz's avatar
sletz committed
140
141
        void DisposeBuffers();

142
143
144
        int SetupBufferSize(jack_nframes_t buffer_size);
        int SetupSampleRate(jack_nframes_t samplerate);
        int SetupSampleRateAux(AudioDeviceID inDevice, jack_nframes_t samplerate);
sletz's avatar
sletz committed
145
146
147
148
149
150
151

        int OpenAUHAL(bool capturing,
                      bool playing,
                      int inchannels,
                      int outchannels,
                      int in_nChannels,
                      int out_nChannels,
152
153
                      const vector<int>& chan_in_list,
                      const vector<int>& chan_out_list,
sletz's avatar
sletz committed
154
                      jack_nframes_t nframes,
155
                      jack_nframes_t samplerate);
sletz's avatar
sletz committed
156
157
158
159
        void CloseAUHAL();

        int AddListeners();
        void RemoveListeners();
160

161
162
        bool TakeHogAux(AudioDeviceID deviceID, bool isInput);
        bool TakeHog();
sletz's avatar
sletz committed
163

164
        void UpdateLatencies();
Stephane Letz's avatar
Stephane Letz committed
165
166
        
        bool IsDigitalDevice(AudioDeviceID device);
167

sletz's avatar
sletz committed
168
169
    public:

sletz's avatar
sletz committed
170
        JackCoreAudioDriver(const char* name, const char* alias, JackLockedEngine* engine, JackSynchro* table);
sletz's avatar
sletz committed
171
172
        virtual ~JackCoreAudioDriver();

sletz's avatar
sletz committed
173
174
        int Open(jack_nframes_t buffer_size,
                 jack_nframes_t samplerate,
sletz's avatar
sletz committed
175
176
                 bool capturing,
                 bool playing,
177
178
179
180
                 int inchannels,
                 int outchannels,
                 const char* chan_in_list,
                 const char* chan_out_list,
sletz's avatar
sletz committed
181
182
183
184
                 bool monitor,
                 const char* capture_driver_name,
                 const char* playback_driver_name,
                 jack_nframes_t capture_latency,
185
                 jack_nframes_t playback_latency,
186
                 int async_output_latency,
187
                 int computation_grain,
188
                 bool hogged,
Stephane Letz's avatar
Stephane Letz committed
189
190
191
192
                 bool clock_drift,
                 bool ac3_encoding,
                 int ac3_bitrate, 
                 bool ac3_lfe);
sletz's avatar
sletz committed
193
        int Close();
sletz's avatar
sletz committed
194
195
196
197
198
199
200
201
202

        int Attach();

        int Start();
        int Stop();

        int Read();
        int Write();

203
204
205
206
207
208
        // BufferSize can be changed
        bool IsFixedBufferSize()
        {
            return false;
        }

209
        int SetBufferSize(jack_nframes_t buffer_size);
sletz's avatar
sletz committed
210
211
212
213
214
};

} // end of namespace

#endif