]> git.sesse.net Git - vlc/blob - modules/gui/macosx/aout.m
* Mac OS X audio device discovery and selection, patch courtesy of
[vlc] / modules / gui / macosx / aout.m
1 /*****************************************************************************
2  * aout.m: CoreAudio output plugin
3  *****************************************************************************
4  * Copyright (C) 2002 VideoLAN
5  * $Id: aout.m,v 1.12 2002/10/02 22:56:53 massiot Exp $
6  *
7  * Authors: Colin Delacroix <colin@zoy.org>
8  *          Jon Lech Johansen <jon-vl@nanocrew.net>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include <vlc/vlc.h>
33 #include <vlc/aout.h>
34 #include "aout_internal.h"
35 #include "asystm.h"
36
37 #include <Carbon/Carbon.h>
38 #include <CoreAudio/AudioHardware.h>
39 #include <CoreAudio/HostTime.h>
40 #include <AudioToolbox/AudioConverter.h>
41
42 #define A52_FRAME_NB 1536
43
44 /*****************************************************************************
45  * aout_sys_t: private audio output method descriptor
46  *****************************************************************************
47  * This structure is part of the audio output thread descriptor.
48  * It describes the CoreAudio specific properties of an output thread.
49  *****************************************************************************/
50 struct aout_sys_t
51 {
52     AudioDeviceID       device;         // the audio device
53
54     AudioStreamBasicDescription stream_format;
55
56     UInt32              i_buffer_size;  // audio device buffer size
57     mtime_t             clock_diff;
58 };
59
60 /*****************************************************************************
61  * Local prototypes.
62  *****************************************************************************/
63 static void     Play            ( aout_instance_t *p_aout );
64
65 static OSStatus IOCallback      ( AudioDeviceID inDevice,
66                                   const AudioTimeStamp *inNow, 
67                                   const void *inInputData, 
68                                   const AudioTimeStamp *inInputTime,
69                                   AudioBufferList *outOutputData, 
70                                   const AudioTimeStamp *inOutputTime, 
71                                   void *threadGlobals );
72
73 /*****************************************************************************
74  * Open: open a CoreAudio HAL device
75  *****************************************************************************/
76 extern MacOSXAudioSystem *gTheMacOSXAudioSystem; // Remove this global, access audio system froma aout some other way
77
78 int E_(OpenAudio)( vlc_object_t * p_this )
79 {
80     OSStatus err;
81     UInt32 i_param_size;
82     aout_instance_t * p_aout = (aout_instance_t *)p_this;
83     struct aout_sys_t * p_sys;
84     msg_Dbg(p_aout, "************************* ENTER OpenAudio ****************************");
85     
86     /* Allocate instance */
87     p_sys = p_aout->output.p_sys = malloc( sizeof( struct aout_sys_t ) );
88     memset( p_sys, 0, sizeof( struct aout_sys_t ) );
89     if( p_aout->output.p_sys == NULL )
90     {
91         msg_Err( p_aout, "out of memory" );
92         return( 1 );
93     }
94
95     /* Get the default output device */
96     // We now ask the GUI for the selected device
97     p_sys->device=[gTheMacOSXAudioSystem getSelectedDeviceSetToRate:p_aout->output.output.i_rate];
98     if(p_sys->device==0)
99     {
100         msg_Err( p_aout, "couldn't get output device");
101         return( -1 );
102     }
103     msg_Dbg(p_aout, "device returned: %ld", p_sys->device);
104
105     p_aout->output.pf_play = Play;
106     aout_VolumeSoftInit( p_aout );
107
108     /* Get a description of the data format used by the device */
109     i_param_size = sizeof(AudioStreamBasicDescription); 
110     err = AudioDeviceGetProperty(p_sys->device, 0, false, kAudioDevicePropertyStreamFormat, &i_param_size, &p_sys->stream_format );
111     if( err != noErr )
112     {
113         msg_Err( p_aout, "failed to get stream format: %4.4s", &err );
114         return -1 ;
115     }
116
117     msg_Dbg( p_aout, "mSampleRate %ld, mFormatID %4.4s, mFormatFlags %ld, mBytesPerPacket %ld, mFramesPerPacket %ld, mBytesPerFrame %ld, mChannelsPerFrame %ld, mBitsPerChannel %ld",
118            (UInt32)p_sys->stream_format.mSampleRate, &p_sys->stream_format.mFormatID,
119            p_sys->stream_format.mFormatFlags, p_sys->stream_format.mBytesPerPacket,
120            p_sys->stream_format.mFramesPerPacket, p_sys->stream_format.mBytesPerFrame,
121            p_sys->stream_format.mChannelsPerFrame, p_sys->stream_format.mBitsPerChannel );
122
123     msg_Dbg( p_aout, "vlc format %4.4s, mac output format '%4.4s'",
124              (char *)&p_aout->output.output.i_format, &p_sys->stream_format.mFormatID );
125
126     switch(p_sys->stream_format.mFormatID)
127     {
128     case 0:
129     case kAudioFormatLinearPCM:
130         p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
131         if ( p_sys->stream_format.mChannelsPerFrame < 6 )
132             p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
133         else
134             p_aout->output.output.i_channels = AOUT_CHAN_3F2R | AOUT_CHAN_LFE;
135         break;
136
137     case kAudioFormat60958AC3:
138     case 'IAC3':
139         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
140         //not necessary, use the input's format by default --Meuuh
141         //p_aout->output.output.i_channels = AOUT_CHAN_DOLBY | AOUT_CHAN_LFE;
142         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE; //p_sys->stream_format.mBytesPerFrame;
143         p_aout->output.output.i_frame_length = A52_FRAME_NB; //p_sys->stream_format.mFramesPerPacket;
144         break;
145
146     default:
147         msg_Err( p_aout, "Unknown hardware format '%4.4s'. Go ask Heiko.", &p_sys->stream_format.mFormatID );
148         return -1;
149     }
150
151     /* Set sample rate and channels per frame */
152     p_aout->output.output.i_rate = p_sys->stream_format.mSampleRate;
153
154     /* Get the buffer size that the device uses for IO */
155     i_param_size = sizeof( p_sys->i_buffer_size );
156 #if 1   // i have a feeling we should use the buffer size imposed by the AC3 device (usually about 6144)
157     err = AudioDeviceGetProperty( p_sys->device, 1, false, 
158                                   kAudioDevicePropertyBufferSize, 
159                                   &i_param_size, &p_sys->i_buffer_size );
160     if(err) {
161         msg_Err(p_aout, "failed to get buffer size - err %4.4s, device %ld", &err, p_sys->device);
162         return -1;
163     }
164     else msg_Dbg( p_aout, "native buffer Size: %d", p_sys->i_buffer_size );
165 #else
166     p_sys->i_buffer_size = p_aout->output.output.i_bytes_per_frame;
167     err = AudioDeviceSetProperty( p_sys->device, 0, 1, false,
168                                   kAudioDevicePropertyBufferSize,
169                                   i_param_size, &p_sys->i_buffer_size );
170     if( err != noErr )
171     {
172         msg_Err( p_aout, "failed to set device buffer size: %4.4s", err );
173         return( -1 );
174     }
175     else msg_Dbg(p_aout, "bufferSize set to %d", p_sys->i_buffer_size);
176 #endif
177
178     p_aout->output.i_nb_samples = p_sys->i_buffer_size / p_sys->stream_format.mBytesPerFrame;
179
180     /* Add callback */
181     err = AudioDeviceAddIOProc( p_sys->device,
182                                 (AudioDeviceIOProc)IOCallback,
183                                 (void *)p_aout );
184
185     /* Open the output with callback IOCallback */
186     err = AudioDeviceStart( p_sys->device,
187                             (AudioDeviceIOProc)IOCallback );
188     if( err != noErr )
189     {
190         msg_Err( p_aout, "AudioDeviceStart failed: %d", err );
191         return -1;
192     }
193
194     /* Let's pray for the following operation to be atomic... */
195     p_sys->clock_diff = - (mtime_t)AudioConvertHostTimeToNanos(
196                                  AudioGetCurrentHostTime()) / 1000;
197     p_sys->clock_diff += mdate();
198
199     return 0;
200 }
201
202 /*****************************************************************************
203  * Close: close the CoreAudio HAL device
204  *****************************************************************************/
205 void E_(CloseAudio)( aout_instance_t * p_aout )
206 {
207     struct aout_sys_t * p_sys = p_aout->output.p_sys;
208     OSStatus err; 
209
210     /* Stop playing sound through the device */
211     err = AudioDeviceStop( p_sys->device,
212                            (AudioDeviceIOProc)IOCallback ); 
213     if( err != noErr )
214     {
215         msg_Err( p_aout, "AudioDeviceStop failed: %d", err );
216     }
217
218     free( p_sys );
219 }
220
221 /*****************************************************************************
222  * Play: nothing to do
223  *****************************************************************************/
224 static void Play( aout_instance_t * p_aout )
225 {
226 }
227
228 /*****************************************************************************
229  * IOCallback : callback for audio output
230  *****************************************************************************/
231 static OSStatus IOCallback( AudioDeviceID inDevice,
232                             const AudioTimeStamp *inNow, 
233                             const void *inInputData,
234                             const AudioTimeStamp *inInputTime, 
235                             AudioBufferList *outOutputData,
236                             const AudioTimeStamp *inOutputTime, 
237                             void *threadGlobals )
238 {
239     aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
240     struct aout_sys_t * p_sys = p_aout->output.p_sys;
241     mtime_t         current_date;
242     AudioTimeStamp  host_time;
243     aout_buffer_t * p_buffer;
244
245     host_time.mFlags = kAudioTimeStampHostTimeValid;
246     AudioDeviceTranslateTime( inDevice, inOutputTime, &host_time );
247     current_date = p_sys->clock_diff
248                  + AudioConvertHostTimeToNanos(host_time.mHostTime) / 1000;
249
250 //    msg_Dbg(p_aout, "Now fetching audio data");
251     p_buffer = aout_OutputNextBuffer( p_aout, current_date, (p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i')) );
252
253     /* move data into output data buffer */
254     if ( p_buffer != NULL )
255     {
256         BlockMoveData( p_buffer->p_buffer,
257                        outOutputData->mBuffers[ 0 ].mData, 
258                        p_sys->i_buffer_size );
259
260 //      msg_Dbg(p_aout, "This buffer has %d bytes, i take %d", p_buffer->i_nb_bytes, p_sys->i_buffer_size);
261     
262         aout_BufferFree( p_buffer );
263     }
264     else
265     {
266         memset(outOutputData->mBuffers[ 0 ].mData, 0, p_sys->i_buffer_size);
267     }
268
269     return noErr;     
270 }
271