-
Notifications
You must be signed in to change notification settings - Fork 54
/
AppController_AudioDigital.m
327 lines (289 loc) · 11.5 KB
/
AppController_AudioDigital.m
1
2
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
//
// Movist
//
// Copyright 2006 ~ 2008 Yong-Hoe Kim. All rights reserved.
// Cheol Ju <[email protected]>
//
// This file is part of Movist.
//
// Movist 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 3 of the License, or
// (at your option) any later version.
//
// Movist 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, see <http://www.gnu.org/licenses/>.
//
/* Check digital output
* From http://lists-archives.org/mplayer-dev-eng/20119-ac3-dts-passthrough-for-mac-os-x.html
*/
#import "AppController.h"
#import "UserDefaults.h"
#import "MMovie_QuickTime.h"
#import <CoreAudio/CoreAudio.h>
static BOOL audioDeviceSupportsDigital(AudioStreamID* streamID);
static void registerAudioDeviceListener(AudioObjectPropertyListenerProc proc, void* data);
static OSStatus audioDeviceListener(AudioObjectID device, UInt32 inNumberOfAddresses,
const AudioObjectPropertyAddress inAddresses[], void* inClientData);
static AudioStreamID _audioStreamID;
@implementation AppController (AudioDigital)
- (BOOL)isCurrentlyDigitalAudioOut
{
if (!_movie || !_audioDeviceSupportsDigital ||
![_defaults boolForKey:MAutodetectDigitalAudioOutKey]) {
return FALSE;
}
return ([_movie hasAC3Codec] && [_movie supportsAC3DigitalOut]) ||
([_movie hasDTSCodec] && [_movie supportsDTSDigitalOut]);
}
- (void)initDigitalAudioOut
{
_audioDeviceSupportsDigital = audioDeviceSupportsDigital(&_audioStreamID);
[self updateDigitalAudioOut:nil]; // nil: not to set volume
registerAudioDeviceListener(audioDeviceListener, self);
}
- (BOOL)updateDigitalAudioOut:(id)sender
{
[_audioDeviceTextField setStringValue:
(_audioDeviceSupportsDigital) ? NSLocalizedString(@"Digital", nil) :
NSLocalizedString(@"Analog", nil)];
BOOL currentlyDigitalOut = [self isCurrentlyDigitalAudioOut];
[_audioOutTextField setStringValue:
(_movie == nil) ? NSLocalizedString(@"None", nil) :
(currentlyDigitalOut) ? NSLocalizedString(@"Digital", nil) :
NSLocalizedString(@"Analog", nil)];
if (currentlyDigitalOut) {
if (_movie && [_movie isMemberOfClass:[MMovie_QuickTime class]]) {
// get current format
OSStatus err;
UInt32 paramSize = sizeof(AudioStreamBasicDescription);
AudioStreamBasicDescription format;
AudioObjectPropertyAddress propertyAddress = {
kAudioStreamPropertyPhysicalFormat,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectGetPropertyData(_audioStreamID, &propertyAddress, 0, NULL, ¶mSize, &format);
if (err != noErr) {
TRACE(@"could not get the stream format: [%4.4s]\n", (char*)&err);
return FALSE;
}
// change sample-rate
format.mSampleRate = 48000.000000; // 48.000 kHz by default
for(MTrack* track in [_movie audioTracks]) {
// use sample-rate of first enabled audio track
if ([track isEnabled] && [track audioSampleRate] != 0) {
format.mSampleRate = [track audioSampleRate];
break;
}
}
// set as current format
err = AudioObjectSetPropertyData(_audioStreamID, &propertyAddress, 0, NULL, sizeof(AudioStreamBasicDescription), &format);
if (err != noErr) {
TRACE(@"could not set the stream format: [%4.4s]\n", (char *)&err);
return FALSE;
}
}
if (sender) {
[self setVolume:DIGITAL_VOLUME]; // always
}
}
else {
if (sender && ![_defaults boolForKey:MUpdateSystemVolumeKey]) {
[self setVolume:[_defaults floatForKey:MVolumeKey]]; // restore analog volume
}
}
return TRUE;
}
- (void)audioDeviceChanged
{
BOOL digital = audioDeviceSupportsDigital(&_audioStreamID);
if (digital == _audioDeviceSupportsDigital) {
return;
}
_audioDeviceSupportsDigital = digital;
[self performSelectorOnMainThread:@selector(updateDigitalAudioOut:)
withObject:self waitUntilDone:FALSE];
if (_movie) { // reopen current movie to use new audio device
[self performSelectorOnMainThread:@selector(reopenMovieWithMovieClass:)
withObject:[_movie class] waitUntilDone:FALSE];
}
}
@end
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id, AudioStreamID* streamID);
static BOOL audioDeviceSupportsDigital(AudioStreamID* streamID)
{
/* Find the ID of the default Device. */
UInt32 paramSize = sizeof(AudioDeviceID);
AudioDeviceID audioDev = 0;
OSStatus err;
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, ¶mSize, &audioDev);
if (err != noErr) {
TRACE(@"could not get default audio device: [%4.4s]\n", (char *)&err);
return FALSE;
}
/* Retrieve the length of the device name. */
paramSize = 0;
propertyAddress.mSelector = kAudioDevicePropertyDeviceName;
err = AudioObjectGetPropertyDataSize(audioDev, &propertyAddress, 0, NULL, ¶mSize);
if (err != noErr) {
TRACE(@"could not get default audio device name length: [%4.4s]\n", (char *)&err);
return FALSE;
}
/* Retrieve the name of the device. */
char* psz_name = (char *)malloc(paramSize);
err = AudioObjectGetPropertyData(audioDev, &propertyAddress, 0, NULL, ¶mSize, psz_name);
if (err != noErr) {
TRACE(@"could not get default audio device name: [%4.4s]\n", (char *)&err);
free(psz_name);
return FALSE;
}
//TRACE(@"got default audio output device ID: %#lx Name: %s\n", audioDev, psz_name);
BOOL isDigital = AudioDeviceSupportsDigital(audioDev, streamID);
//TRACE(@"support digital s/pdif output:%d\n", isDigital);
free( psz_name);
return isDigital;
}
static void registerAudioDeviceListener(AudioObjectPropertyListenerProc proc, void* data)
{
/* Find the ID of the default Device. */
UInt32 paramSize = sizeof(AudioDeviceID);
AudioDeviceID audioDev = 0;
AudioObjectPropertyAddress propertyAddress = {
kAudioHardwarePropertyDefaultOutputDevice,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, ¶mSize, &audioDev);
if (err != noErr) {
TRACE(@"could not get default audio device: [%4.4s]\n", (char *)&err);
return;
}
/* add callback func */
propertyAddress.mSelector = kAudioDevicePropertyDeviceHasChanged;
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
err = AudioObjectAddPropertyListener(audioDev, &propertyAddress, proc, data);
if (err != noErr) {
TRACE(@"AudioDeviceAddPropertyListener failed: [%4.4s]\n", (char *)&err);
}
}
static OSStatus audioDeviceListener(AudioObjectID device, UInt32 inNumberOfAddresses,
const AudioObjectPropertyAddress inAddresses[], void* inClientData)
{
int i;
for (i = 0; i < inNumberOfAddresses; ++i)
{
if(inAddresses[i].mSelector == kAudioDevicePropertyDeviceHasChanged)
{
//TRACE(@"got notify kAudioDevicePropertyDeviceHasChanged.\n");
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[(AppController*)inClientData audioDeviceChanged];
[pool release];
break;
}
}
return noErr;
}
////////////////////////////////////////////////////////////////////////////////
#pragma mark -
static int AudioStreamSupportsDigital(AudioStreamID i_stream_id)
{
OSStatus err = noErr;
UInt32 paramSize;
AudioStreamBasicDescription *p_format_list = NULL;
int i, i_formats, b_return = FALSE;
AudioObjectPropertyAddress propertyAddress = {
kAudioStreamPropertyPhysicalFormats,
kAudioObjectPropertyScopeGlobal,
kAudioObjectPropertyElementMaster
};
/* Retrieve all the stream formats supported by each output stream. */
err = AudioObjectGetPropertyDataSize(i_stream_id, &propertyAddress, 0, NULL, ¶mSize);
if (err != noErr) {
TRACE(@"could not get number of streamformats: [%4.4s]\n", (char *)&err);
return FALSE;
}
i_formats = paramSize / sizeof(AudioStreamBasicDescription);
p_format_list = (AudioStreamBasicDescription *)malloc(paramSize);
if (p_format_list == NULL) {
TRACE(@"could not malloc the memory\n" );
return FALSE;
}
err = AudioObjectGetPropertyData(i_stream_id, &propertyAddress, 0, NULL, ¶mSize, p_format_list);
if (err != noErr) {
TRACE(@"could not get the list of streamformats: [%4.4s]\n", (char *)&err);
free(p_format_list);
return FALSE;
}
for (i = 0; i < i_formats; ++i) {
//TRACE(@"supported format:", &p_format_list[i]);
if (p_format_list[i].mFormatID == kAudioFormatAC3 ||
p_format_list[i].mFormatID == kAudioFormat60958AC3 ||
p_format_list[i].mFormatID == 'IAC3') {
//TRACE(@"[%d]sampleRate = %lf", i, p_format_list[i].mSampleRate);
b_return = TRUE;
}
}
free(p_format_list);
return b_return;
}
static int AudioDeviceSupportsDigital(AudioDeviceID i_dev_id, AudioStreamID* streamID)
{
OSStatus err = noErr;
UInt32 paramSize = 0;
AudioStreamID* p_streams = NULL;
int i = 0, i_streams = 0;
AudioObjectPropertyAddress propertyAddress = {
kAudioDevicePropertyStreams,
kAudioDevicePropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
/* Retrieve all the output streams. */
err = AudioObjectGetPropertyDataSize(i_dev_id, &propertyAddress, 0, NULL, ¶mSize);
if (err != noErr) {
TRACE(@"could not get number of streams: [%4.4s]\n", (char*)&err);
return FALSE;
}
i_streams = paramSize / sizeof(AudioStreamID);
p_streams = (AudioStreamID *)malloc(paramSize);
if (p_streams == NULL) {
TRACE(@"out of memory\n");
return FALSE;
}
err = AudioObjectGetPropertyData(i_dev_id, &propertyAddress, 0, NULL, ¶mSize, p_streams);
if (err != noErr) {
TRACE(@"could not get number of streams: [%4.4s]\n", (char*)&err);
free(p_streams);
return FALSE;
}
for (i = 0; i < i_streams; ++i) {
if (AudioStreamSupportsDigital(p_streams[i])) {
*streamID = p_streams[i];
/* Install the callback. */
/*
err = AudioStreamAddPropertyListener(p_streams[i], 0,
kAudioStreamPropertyPhysicalFormat,
StreamListener, 0);
if (err != noErr) {
TRACE(@"AudioStreamAddPropertyListener failed: [%s]\n", (char *)&err);
return FALSE;
}
*/
break;
}
}
free(p_streams);
return i < i_streams;
}