aout.m 10.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * aout.m: CoreAudio output plugin
 *****************************************************************************
4
 * Copyright (C) 2002 VideoLAN
5
 * $Id: aout.m,v 1.12 2002/10/02 22:56:53 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
#include "asystm.h"
36 37 38 39 40 41

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

42 43
#define A52_FRAME_NB 1536

44 45 46 47 48 49 50 51 52
/*****************************************************************************
 * 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
53 54 55 56 57

    AudioStreamBasicDescription stream_format;

    UInt32              i_buffer_size;  // audio device buffer size
    mtime_t             clock_diff;
58 59 60 61 62
};

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
63
static void     Play            ( aout_instance_t *p_aout );
64

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

/*****************************************************************************
74
 * Open: open a CoreAudio HAL device
75
 *****************************************************************************/
76 77
extern MacOSXAudioSystem *gTheMacOSXAudioSystem; // Remove this global, access audio system froma aout some other way

78
int E_(OpenAudio)( vlc_object_t * p_this )
79 80
{
    OSStatus err;
81 82 83
    UInt32 i_param_size;
    aout_instance_t * p_aout = (aout_instance_t *)p_this;
    struct aout_sys_t * p_sys;
84 85
    msg_Dbg(p_aout, "************************* ENTER OpenAudio ****************************");
    
86 87 88 89
    /* 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 )
90 91 92 93 94
    {
        msg_Err( p_aout, "out of memory" );
        return( 1 );
    }

95
    /* Get the default output device */
96 97 98
    // We now ask the GUI for the selected device
    p_sys->device=[gTheMacOSXAudioSystem getSelectedDeviceSetToRate:p_aout->output.output.i_rate];
    if(p_sys->device==0)
99
    {
100
        msg_Err( p_aout, "couldn't get output device");
101 102
        return( -1 );
    }
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 110
    i_param_size = sizeof(AudioStreamBasicDescription); 
    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 118 119 120 121 122 123 124 125 126
    msg_Dbg( p_aout, "mSampleRate %ld, mFormatID %4.4s, mFormatFlags %ld, mBytesPerPacket %ld, mFramesPerPacket %ld, mBytesPerFrame %ld, mChannelsPerFrame %ld, mBitsPerChannel %ld",
           (UInt32)p_sys->stream_format.mSampleRate, &p_sys->stream_format.mFormatID,
           p_sys->stream_format.mFormatFlags, p_sys->stream_format.mBytesPerPacket,
           p_sys->stream_format.mFramesPerPacket, p_sys->stream_format.mBytesPerFrame,
           p_sys->stream_format.mChannelsPerFrame, p_sys->stream_format.mBitsPerChannel );

    msg_Dbg( p_aout, "vlc format %4.4s, mac output format '%4.4s'",
             (char *)&p_aout->output.output.i_format, &p_sys->stream_format.mFormatID );

    switch(p_sys->stream_format.mFormatID)
127
    {
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
    case 0:
    case kAudioFormatLinearPCM:
        p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
        if ( p_sys->stream_format.mChannelsPerFrame < 6 )
            p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
        else
            p_aout->output.output.i_channels = AOUT_CHAN_3F2R | AOUT_CHAN_LFE;
        break;

    case kAudioFormat60958AC3:
    case 'IAC3':
        p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
        //not necessary, use the input's format by default --Meuuh
        //p_aout->output.output.i_channels = AOUT_CHAN_DOLBY | AOUT_CHAN_LFE;
        p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE; //p_sys->stream_format.mBytesPerFrame;
        p_aout->output.output.i_frame_length = A52_FRAME_NB; //p_sys->stream_format.mFramesPerPacket;
        break;

    default:
        msg_Err( p_aout, "Unknown hardware format '%4.4s'. Go ask Heiko.", &p_sys->stream_format.mFormatID );
        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 155

    /* Get the buffer size that the device uses for IO */
    i_param_size = sizeof( p_sys->i_buffer_size );
156 157
#if 1	// i have a feeling we should use the buffer size imposed by the AC3 device (usually about 6144)
    err = AudioDeviceGetProperty( p_sys->device, 1, false, 
158 159
                                  kAudioDevicePropertyBufferSize, 
                                  &i_param_size, &p_sys->i_buffer_size );
160 161 162 163 164
    if(err) {
	msg_Err(p_aout, "failed to get buffer size - err %4.4s, device %ld", &err, p_sys->device);
	return -1;
    }
    else msg_Dbg( p_aout, "native buffer Size: %d", p_sys->i_buffer_size );
165
#else
166 167
    p_sys->i_buffer_size = p_aout->output.output.i_bytes_per_frame;
    err = AudioDeviceSetProperty( p_sys->device, 0, 1, false,
168 169 170
                                  kAudioDevicePropertyBufferSize,
                                  i_param_size, &p_sys->i_buffer_size );
    if( err != noErr )
171
    {
172
        msg_Err( p_aout, "failed to set device buffer size: %4.4s", err );
173 174
        return( -1 );
    }
175 176
    else msg_Dbg(p_aout, "bufferSize set to %d", p_sys->i_buffer_size);
#endif
177

178
    p_aout->output.i_nb_samples = p_sys->i_buffer_size / p_sys->stream_format.mBytesPerFrame;
179

180 181 182 183
    /* Add callback */
    err = AudioDeviceAddIOProc( p_sys->device,
                                (AudioDeviceIOProc)IOCallback,
                                (void *)p_aout );
184

185 186 187 188
    /* Open the output with callback IOCallback */
    err = AudioDeviceStart( p_sys->device,
                            (AudioDeviceIOProc)IOCallback );
    if( err != noErr )
189
    {
190 191
        msg_Err( p_aout, "AudioDeviceStart failed: %d", err );
        return -1;
192 193
    }

194
    /* Let's pray for the following operation to be atomic... */
195 196 197
    p_sys->clock_diff = - (mtime_t)AudioConvertHostTimeToNanos(
                                 AudioGetCurrentHostTime()) / 1000;
    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 208
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
    OSStatus err; 
209

210 211 212
    /* Stop playing sound through the device */
    err = AudioDeviceStop( p_sys->device,
                           (AudioDeviceIOProc)IOCallback ); 
213 214
    if( err != noErr )
    {
215
        msg_Err( p_aout, "AudioDeviceStop failed: %d", err );
216 217
    }

218
    free( p_sys );
219 220 221
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
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 232 233 234 235 236 237
static OSStatus IOCallback( AudioDeviceID inDevice,
                            const AudioTimeStamp *inNow, 
                            const void *inInputData,
                            const AudioTimeStamp *inInputTime, 
                            AudioBufferList *outOutputData,
                            const AudioTimeStamp *inOutputTime, 
                            void *threadGlobals )
238
{
239 240 241 242 243
    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;
244

245 246 247 248
    host_time.mFlags = kAudioTimeStampHostTimeValid;
    AudioDeviceTranslateTime( inDevice, inOutputTime, &host_time );
    current_date = p_sys->clock_diff
                 + AudioConvertHostTimeToNanos(host_time.mHostTime) / 1000;
249

250 251
//    msg_Dbg(p_aout, "Now fetching audio data");
    p_buffer = aout_OutputNextBuffer( p_aout, current_date, (p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i')) );
252

253 254
    /* move data into output data buffer */
    if ( p_buffer != NULL )
255
    {
256
	BlockMoveData( p_buffer->p_buffer,
257 258
                       outOutputData->mBuffers[ 0 ].mData, 
                       p_sys->i_buffer_size );
259 260 261 262

//	msg_Dbg(p_aout, "This buffer has %d bytes, i take %d", p_buffer->i_nb_bytes, p_sys->i_buffer_size);
    
	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