aout.m 9.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * aout.m: CoreAudio output plugin
 *****************************************************************************
4
 * Copyright (C) 2002 VideoLAN
5
 * $Id: aout.m,v 1.4 2002/08/14 00:43:52 massiot 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 61 62
static int      SetFormat       ( aout_instance_t *p_aout );
static void     Play            ( aout_instance_t *p_aout,
                                  aout_buffer_t *p_buffer );
63

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

/*****************************************************************************
73
 * Open: open a CoreAudio HAL device
74
 *****************************************************************************/
75
int E_(OpenAudio)( vlc_object_t * p_this )
76 77
{
    OSStatus err;
78 79 80 81 82 83 84 85
    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 )
86 87 88 89 90
    {
        msg_Err( p_aout, "out of memory" );
        return( 1 );
    }

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

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

106
    return 0;
107 108 109
}

/*****************************************************************************
110
 * SetFormat: find the closest available format from p_format
111
 *****************************************************************************/
112
static int SetFormat( aout_instance_t * p_aout )
113
{
114 115 116 117 118 119 120 121 122 123
    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 )
124
    {
125 126
        msg_Err( p_aout, "failed to get stream format: %d", err );
        return -1 ;
127 128
    }

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

135 136
    /* We only deal with floats */
    if ( p_aout->output.output.i_format != AOUT_FMT_FLOAT32 )
137
    {
138 139 140
        msg_Err( p_aout, "cannot set format 0x%x",
                 p_aout->output.output.i_format );
        return -1;
141
    }
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    p_sys->stream_format.mFormatFlags |=
        kLinearPCMFormatFlagIsFloat;

    /* Set sample rate and channels per frame */
    p_sys->stream_format.mSampleRate
                 = p_aout->output.output.i_rate; 
    p_sys->stream_format.mChannelsPerFrame
                 = p_aout->output.output.i_channels;

    /* 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
160
                            * 4096;
161 162 163 164 165
    err = AudioDeviceSetProperty( p_sys->device, 0, 0, false,
                                  kAudioDevicePropertyBufferSize,
                                  i_param_size, &p_sys->i_buffer_size );
#endif
    if( err != noErr )
166
    {
167
        msg_Err( p_aout, "failed to set device buffer size: %d", err );
168 169 170
        return( -1 );
    }

171 172
    p_aout->output.i_nb_samples = p_sys->i_buffer_size / sizeof(float)
                                   / p_aout->output.output.i_channels;
173

174 175 176 177
    /* Add callback */
    err = AudioDeviceAddIOProc( p_sys->device,
                                (AudioDeviceIOProc)IOCallback,
                                (void *)p_aout );
178

179 180 181 182
    /* Open the output with callback IOCallback */
    err = AudioDeviceStart( p_sys->device,
                            (AudioDeviceIOProc)IOCallback );
    if( err != noErr )
183
    {
184 185
        msg_Err( p_aout, "AudioDeviceStart failed: %d", err );
        return -1;
186 187
    }

188 189 190
    /* Let's pray for the following operation to be atomic... */
    p_sys->clock_diff = mdate()
         - AudioConvertHostTimeToNanos(AudioGetCurrentHostTime()) / 1000;
191

192
    return 0;
193 194 195
}

/*****************************************************************************
196
 * Close: close the CoreAudio HAL device
197
 *****************************************************************************/
198
void E_(CloseAudio)( aout_instance_t * p_aout )
199
{
200 201
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    OSStatus err; 
202

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

211
    free( p_sys );
212 213 214
}

/*****************************************************************************
215
 * Play: queue a buffer for playing by IOCallback
216
 *****************************************************************************/
217
static void Play( aout_instance_t * p_aout, aout_buffer_t * p_buffer )
218
{
219
    aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
220 221 222
}

/*****************************************************************************
223
 * IOCallback : callback for audio output
224
 *****************************************************************************/
225 226 227 228 229 230 231
static OSStatus IOCallback( AudioDeviceID inDevice,
                            const AudioTimeStamp *inNow, 
                            const void *inInputData,
                            const AudioTimeStamp *inInputTime, 
                            AudioBufferList *outOutputData,
                            const AudioTimeStamp *inOutputTime, 
                            void *threadGlobals )
232
{
233 234 235 236 237
    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;
238

239 240 241 242
    host_time.mFlags = kAudioTimeStampHostTimeValid;
    AudioDeviceTranslateTime( inDevice, inOutputTime, &host_time );
    current_date = p_sys->clock_diff
                 + AudioConvertHostTimeToNanos(host_time.mHostTime) / 1000;
243

244
    p_buffer = aout_OutputNextBuffer( p_aout, current_date, 0 );
245

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

259
    return noErr;     
260
}
261