aout.m 9.04 KB
Newer Older
1 2 3
/*****************************************************************************
 * aout.m: CoreAudio output plugin
 *****************************************************************************
4
 * Copyright (C) 2002 VideoLAN
5
 * $Id: aout.m,v 1.11 2002/09/30 21:32:33 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
static void     Play            ( aout_instance_t *p_aout );
61

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

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

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

101
    p_aout->output.pf_play = Play;
102
    aout_VolumeSoftInit( p_aout );
103

104
    /* Get a description of the data format used by the device */
Christophe Massiot's avatar
Christophe Massiot committed
105
    i_param_size = sizeof( p_sys->stream_format ); 
106 107 108 109 110
    err = AudioDeviceGetProperty( p_sys->device, 0, false, 
                                  kAudioDevicePropertyStreamFormat, 
                                  &i_param_size,
                                  &p_sys->stream_format );
    if( err != noErr )
111
    {
112 113
        msg_Err( p_aout, "failed to get stream format: %d", err );
        return -1 ;
114 115
    }

116
    if( p_sys->stream_format.mFormatID != kAudioFormatLinearPCM )
117
    {
118 119
        msg_Err( p_aout, "kAudioFormatLinearPCM required" );
        return -1 ;
120 121
    }

122
    /* We only deal with floats. FIXME : this is where we should do S/PDIF. */
123
    p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
124 125

    /* Set sample rate and channels per frame */
126
    p_aout->output.output.i_rate = p_sys->stream_format.mSampleRate;
127 128
    /* FIXME : this is where we should ask for downmixing. */
    p_aout->output.output.i_channels = 2; //p_sys->stream_format.mChannelsPerFrame;
129 130 131 132 133 134 135 136 137 138

    /* 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
139
                            * 4096;
140 141 142 143 144
    err = AudioDeviceSetProperty( p_sys->device, 0, 0, false,
                                  kAudioDevicePropertyBufferSize,
                                  i_param_size, &p_sys->i_buffer_size );
#endif
    if( err != noErr )
145
    {
146
        msg_Err( p_aout, "failed to set device buffer size: %d", err );
147 148 149
        return( -1 );
    }

150 151
    p_aout->output.i_nb_samples = p_sys->i_buffer_size / sizeof(float)
                                   / p_aout->output.output.i_channels;
152

153 154 155 156
    /* Add callback */
    err = AudioDeviceAddIOProc( p_sys->device,
                                (AudioDeviceIOProc)IOCallback,
                                (void *)p_aout );
157

158 159 160 161
    /* Open the output with callback IOCallback */
    err = AudioDeviceStart( p_sys->device,
                            (AudioDeviceIOProc)IOCallback );
    if( err != noErr )
162
    {
163 164
        msg_Err( p_aout, "AudioDeviceStart failed: %d", err );
        return -1;
165 166
    }

167
    /* Let's pray for the following operation to be atomic... */
168 169 170
    p_sys->clock_diff = - (mtime_t)AudioConvertHostTimeToNanos(
                                 AudioGetCurrentHostTime()) / 1000;
    p_sys->clock_diff += mdate();
171

172
    return 0;
173 174 175
}

/*****************************************************************************
176
 * Close: close the CoreAudio HAL device
177
 *****************************************************************************/
178
void E_(CloseAudio)( aout_instance_t * p_aout )
179
{
180 181
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    OSStatus err; 
182

183 184 185
    /* Stop playing sound through the device */
    err = AudioDeviceStop( p_sys->device,
                           (AudioDeviceIOProc)IOCallback ); 
186 187
    if( err != noErr )
    {
188
        msg_Err( p_aout, "AudioDeviceStop failed: %d", err );
189 190
    }

191
    free( p_sys );
192 193 194
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
195
 * Play: nothing to do
196
 *****************************************************************************/
197
static void Play( aout_instance_t * p_aout )
198 199 200 201
{
}

/*****************************************************************************
202
 * IOCallback : callback for audio output
203
 *****************************************************************************/
204 205 206 207 208 209 210
static OSStatus IOCallback( AudioDeviceID inDevice,
                            const AudioTimeStamp *inNow, 
                            const void *inInputData,
                            const AudioTimeStamp *inInputTime, 
                            AudioBufferList *outOutputData,
                            const AudioTimeStamp *inOutputTime, 
                            void *threadGlobals )
211
{
212 213 214 215 216
    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;
217

218 219 220 221
    host_time.mFlags = kAudioTimeStampHostTimeValid;
    AudioDeviceTranslateTime( inDevice, inOutputTime, &host_time );
    current_date = p_sys->clock_diff
                 + AudioConvertHostTimeToNanos(host_time.mHostTime) / 1000;
222

223
    p_buffer = aout_OutputNextBuffer( p_aout, current_date, VLC_FALSE );
224

225 226
    /* move data into output data buffer */
    if ( p_buffer != NULL )
227
    {
228 229 230 231
        BlockMoveData( p_buffer->p_buffer,
                       outOutputData->mBuffers[ 0 ].mData, 
                       p_sys->i_buffer_size );
        aout_BufferFree( p_buffer );
232
    }
233
    else
234
    {
235
        memset(outOutputData->mBuffers[ 0 ].mData, 0, p_sys->i_buffer_size);
236 237
    }

238
    return noErr;     
239
}
240