aout.m 9.51 KB
Newer Older
1 2 3
/*****************************************************************************
 * aout.m: CoreAudio output plugin
 *****************************************************************************
4
 * Copyright (C) 2002 VideoLAN
5
 * $Id: aout.m,v 1.7 2002/08/25 09:40:00 sam Exp $
6 7 8
 *
 * Authors: Colin Delacroix <colin@zoy.org>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
9
 *          Christophe Massiot <massiot@via.ecp.fr>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <string.h>
30
#include <stdlib.h>
31 32 33

#include <vlc/vlc.h>
#include <vlc/aout.h>
34
#include "aout_internal.h"
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

#include <Carbon/Carbon.h>
#include <CoreAudio/AudioHardware.h>
#include <CoreAudio/HostTime.h>
#include <AudioToolbox/AudioConverter.h>

/*****************************************************************************
 * aout_sys_t: private audio output method descriptor
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
 * It describes the CoreAudio specific properties of an output thread.
 *****************************************************************************/
struct aout_sys_t
{
    AudioDeviceID       device;         // the audio device
50 51 52 53 54

    AudioStreamBasicDescription stream_format;

    UInt32              i_buffer_size;  // audio device buffer size
    mtime_t             clock_diff;
55 56 57 58 59
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
60
static int      SetFormat       ( aout_instance_t *p_aout );
61
static void     Play            ( aout_instance_t *p_aout );
62

63
static OSStatus IOCallback      ( AudioDeviceID inDevice,
64 65 66 67 68 69 70 71
                                  const AudioTimeStamp *inNow, 
                                  const void *inInputData, 
                                  const AudioTimeStamp *inInputTime,
                                  AudioBufferList *outOutputData, 
                                  const AudioTimeStamp *inOutputTime, 
                                  void *threadGlobals );

/*****************************************************************************
72
 * Open: open a CoreAudio HAL device
73
 *****************************************************************************/
74
int E_(OpenAudio)( vlc_object_t * p_this )
75 76
{
    OSStatus err;
77 78 79 80 81 82 83 84
    UInt32 i_param_size;
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
    struct aout_sys_t * p_sys;

    /* Allocate instance */
    p_sys = p_aout->output.p_sys = malloc( sizeof( struct aout_sys_t ) );
    memset( p_sys, 0, sizeof( struct aout_sys_t ) );
    if( p_aout->output.p_sys == NULL )
85 86 87 88 89
    {
        msg_Err( p_aout, "out of memory" );
        return( 1 );
    }

90 91 92
    /* Get the default output device */
    /* FIXME : be more clever in choosing from several devices */
    i_param_size = sizeof( p_sys->device );
93
    err = AudioHardwareGetProperty( kAudioHardwarePropertyDefaultOutputDevice,
94 95
                                    &i_param_size, 
                                    (void *)&p_sys->device );
96 97 98 99 100 101
    if( err != noErr ) 
    {
        msg_Err( p_aout, "failed to get the device: %d", err );
        return( -1 );
    }

102 103
    p_aout->output.pf_setformat = SetFormat;
    p_aout->output.pf_play = Play;
104

105
    return 0;
106 107 108
}

/*****************************************************************************
109
 * SetFormat: find the closest available format from p_format
110
 *****************************************************************************/
111
static int SetFormat( aout_instance_t * p_aout )
112
{
113 114 115 116 117 118 119 120 121 122
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    OSErr err;

    /* Get a description of the data format used by the device */
    UInt32 i_param_size = sizeof( p_sys->stream_format ); 
    err = AudioDeviceGetProperty( p_sys->device, 0, false, 
                                  kAudioDevicePropertyStreamFormat, 
                                  &i_param_size,
                                  &p_sys->stream_format );
    if( err != noErr )
123
    {
124 125
        msg_Err( p_aout, "failed to get stream format: %d", err );
        return -1 ;
126 127
    }

128
    if( p_sys->stream_format.mFormatID != kAudioFormatLinearPCM )
129
    {
130 131
        msg_Err( p_aout, "kAudioFormatLinearPCM required" );
        return -1 ;
132 133
    }

134 135
    /* We only deal with floats */
    if ( p_aout->output.output.i_format != AOUT_FMT_FLOAT32 )
136
    {
137 138 139
        msg_Err( p_aout, "cannot set format 0x%x",
                 p_aout->output.output.i_format );
        return -1;
140
    }
141 142

    /* Set sample rate and channels per frame */
143 144
    p_aout->output.output.i_rate = p_sys->stream_format.mSampleRate;
    p_aout->output.output.i_channels = p_sys->stream_format.mChannelsPerFrame;
145 146 147 148 149 150 151 152 153 154

    /* Get the buffer size that the device uses for IO */
    i_param_size = sizeof( p_sys->i_buffer_size );
#if 0
    err = AudioDeviceGetProperty( p_sys->device, 0, false, 
                                  kAudioDevicePropertyBufferSize, 
                                  &i_param_size, &p_sys->i_buffer_size );
msg_Dbg( p_aout, "toto : %d", p_sys->i_buffer_size );
#else
    p_sys->i_buffer_size = sizeof(float) * p_aout->output.output.i_channels
155
                            * 4096;
156 157 158 159 160
    err = AudioDeviceSetProperty( p_sys->device, 0, 0, false,
                                  kAudioDevicePropertyBufferSize,
                                  i_param_size, &p_sys->i_buffer_size );
#endif
    if( err != noErr )
161
    {
162
        msg_Err( p_aout, "failed to set device buffer size: %d", err );
163 164 165
        return( -1 );
    }

166 167
    p_aout->output.i_nb_samples = p_sys->i_buffer_size / sizeof(float)
                                   / p_aout->output.output.i_channels;
168

169 170 171 172
    /* Add callback */
    err = AudioDeviceAddIOProc( p_sys->device,
                                (AudioDeviceIOProc)IOCallback,
                                (void *)p_aout );
173

174 175 176 177
    /* Open the output with callback IOCallback */
    err = AudioDeviceStart( p_sys->device,
                            (AudioDeviceIOProc)IOCallback );
    if( err != noErr )
178
    {
179 180
        msg_Err( p_aout, "AudioDeviceStart failed: %d", err );
        return -1;
181 182
    }

183
    /* Let's pray for the following operation to be atomic... */
184 185 186
    p_sys->clock_diff = - (mtime_t)AudioConvertHostTimeToNanos(
                                 AudioGetCurrentHostTime()) / 1000;
    p_sys->clock_diff += mdate();
187

188
    return 0;
189 190 191
}

/*****************************************************************************
192
 * Close: close the CoreAudio HAL device
193
 *****************************************************************************/
194
void E_(CloseAudio)( aout_instance_t * p_aout )
195
{
196 197
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    OSStatus err; 
198

199 200 201
    /* Stop playing sound through the device */
    err = AudioDeviceStop( p_sys->device,
                           (AudioDeviceIOProc)IOCallback ); 
202 203
    if( err != noErr )
    {
204
        msg_Err( p_aout, "AudioDeviceStop failed: %d", err );
205 206
    }

207
    free( p_sys );
208 209 210
}

/*****************************************************************************
211
 * Play: queue a buffer for playing by IOCallback
212
 *****************************************************************************/
213
static void Play( aout_instance_t * p_aout )
214 215 216 217
{
}

/*****************************************************************************
218
 * IOCallback : callback for audio output
219
 *****************************************************************************/
220 221 222 223 224 225 226
static OSStatus IOCallback( AudioDeviceID inDevice,
                            const AudioTimeStamp *inNow, 
                            const void *inInputData,
                            const AudioTimeStamp *inInputTime, 
                            AudioBufferList *outOutputData,
                            const AudioTimeStamp *inOutputTime, 
                            void *threadGlobals )
227
{
228 229 230 231 232
    aout_instance_t * p_aout = (aout_instance_t *)threadGlobals;
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    mtime_t         current_date;
    AudioTimeStamp  host_time;
    aout_buffer_t * p_buffer;
233

234 235 236 237
    host_time.mFlags = kAudioTimeStampHostTimeValid;
    AudioDeviceTranslateTime( inDevice, inOutputTime, &host_time );
    current_date = p_sys->clock_diff
                 + AudioConvertHostTimeToNanos(host_time.mHostTime) / 1000;
238

239
    p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_FALSE );
240

241 242
    /* move data into output data buffer */
    if ( p_buffer != NULL )
243
    {
244 245 246 247
        BlockMoveData( p_buffer->p_buffer,
                       outOutputData->mBuffers[ 0 ].mData, 
                       p_sys->i_buffer_size );
        aout_BufferFree( p_buffer );
248
    }
249
    else
250
    {
251
        memset(outOutputData->mBuffers[ 0 ].mData, 0, p_sys->i_buffer_size);
252 253
    }

254
    return noErr;     
255
}
256