aout.c 11.1 KB
Newer Older
1
/*****************************************************************************
2
 * aout.c : QNX audio output
3
 *****************************************************************************
4
 * Copyright (C) 2000, 2001 the VideoLAN team
5 6 7
 *
 * Authors: Henri Fallon <henri@videolan.org>
 *          Jon Lech Johansen <jon-vl@nanocrew.net>
8 9
 *          Pascal Levesque <pascal.levesque@mindready.com>
 *
10 11 12 13
 * 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.
14
 *
15 16 17 18 19 20 21
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <errno.h>                                                 /* ENOMEM */

30 31 32 33
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

34
#include <vlc_common.h>
35 36 37 38 39

#ifdef HAVE_ALLOCA_H
#   include <alloca.h>
#endif

40
#include <vlc_aout.h>
41 42 43 44 45 46 47 48

#include <sys/asoundlib.h>

struct aout_sys_t
{
    snd_pcm_t  * p_pcm_handle;
    int          i_card;
    int          i_device;
49

50
    uint8_t *    p_silent_buffer;
51 52
};

53 54
#define DEFAULT_FRAME_SIZE 2048

55 56 57
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
58 59
int            OpenAudio    ( vlc_object_t *p_this );
void           CloseAudio   ( vlc_object_t *p_this );
60 61
static int     GetBufInfo       ( aout_instance_t * );
static void    Play             ( aout_instance_t * );
62
static void*   QNXaoutThread    ( vlc_object_t * );
63 64 65 66 67 68

/*****************************************************************************
 * Open : creates a handle and opens an alsa device
 *****************************************************************************
 * This function opens an alsa device, through the alsa API
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
69
int OpenAudio( vlc_object_t *p_this )
70
{
71
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
72
    int i_ret;
Christophe Massiot's avatar
Christophe Massiot committed
73
    int i_bytes_per_sample;
74
    int i_nb_channels;
Christophe Massiot's avatar
Christophe Massiot committed
75 76 77
    snd_pcm_channel_info_t pi;
    snd_pcm_channel_params_t pp;
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
78 79

    /* allocate structure */
80 81 82
    p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
    if( p_aout->output.p_sys == NULL )
        return -1;
83 84

    /* open audio device */
85 86 87
    if( ( i_ret = snd_pcm_open_preferred( &p_aout->output.p_sys->p_pcm_handle,
                                          &p_aout->output.p_sys->i_card,
                                          &p_aout->output.p_sys->i_device,
88 89 90 91
                                          SND_PCM_OPEN_PLAYBACK ) ) < 0 )
    {
        msg_Err( p_aout, "unable to open audio device (%s)",
                         snd_strerror( i_ret ) );
92 93
        free( p_aout->output.p_sys );
        return -1;
94 95 96
    }

    /* disable mmap */
97
    if( ( i_ret = snd_pcm_plugin_set_disable( p_aout->output.p_sys->p_pcm_handle,
98 99 100
                                              PLUGIN_DISABLE_MMAP ) ) < 0 )
    {
        msg_Err( p_aout, "unable to disable mmap (%s)", snd_strerror(i_ret) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
101
        CloseAudio( p_this );
102 103
        free( p_aout->output.p_sys );
        return -1;
104 105
    }

106 107
    p_aout->output.p_sys->p_silent_buffer = malloc( DEFAULT_FRAME_SIZE * 4 );
    p_aout->output.pf_play = Play;
108
    aout_VolumeSoftInit( p_aout );
109 110 111 112 113

    memset( &pi, 0, sizeof(pi) );
    memset( &pp, 0, sizeof(pp) );

    pi.channel = SND_PCM_CHANNEL_PLAYBACK;
114
    if( ( i_ret = snd_pcm_plugin_info( p_aout->output.p_sys->p_pcm_handle,
115 116 117 118
                                       &pi ) ) < 0 )
    {
        msg_Err( p_aout, "unable to get plugin info (%s)",
                         snd_strerror( i_ret ) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
119
        CloseAudio( p_this );
120
        free( p_aout->output.p_sys );
121
        return -1;
122 123 124 125 126 127 128
    }

    pp.mode       = SND_PCM_MODE_BLOCK;
    pp.channel    = SND_PCM_CHANNEL_PLAYBACK;
    pp.start_mode = SND_PCM_START_FULL;
    pp.stop_mode  = SND_PCM_STOP_STOP;

129
    pp.buf.block.frags_max   = 3;
130
    pp.buf.block.frags_min   = 1;
131

132
    pp.format.interleave     = 1;
133
    pp.format.rate           = p_aout->output.output.i_rate;
134

135 136
    i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
    if ( i_nb_channels > 2 )
137
    {
138 139 140
        /* I don't know if QNX supports more than two channels. */
        i_nb_channels = 2;
        p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
141
    }
142 143 144 145 146 147
    pp.format.voices         = i_nb_channels;

    p_aout->output.output.i_format = AOUT_FMT_S16_NE;
    p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
    pp.format.format = SND_PCM_SFMT_S16;
    i_bytes_per_sample = 2;
148

149 150 151
    pp.buf.block.frag_size = p_aout->output.i_nb_samples *
                            p_aout->output.output.i_channels *
                            i_bytes_per_sample;
152 153

    /* set parameters */
154
    if( ( i_ret = snd_pcm_plugin_params( p_aout->output.p_sys->p_pcm_handle,
155 156 157
                                         &pp ) ) < 0 )
    {
        msg_Err( p_aout, "unable to set parameters (%s)", snd_strerror(i_ret) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
158
        CloseAudio( p_this );
159
        free( p_aout->output.p_sys );
160
        return -1;
161 162 163
    }

    /* prepare channel */
164
    if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
165 166 167 168
                                          SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
    {
        msg_Err( p_aout, "unable to prepare channel (%s)",
                         snd_strerror( i_ret ) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
169
        CloseAudio( p_this );
170
        free( p_aout->output.p_sys );
171
        return -1;
172 173
    }

Christophe Massiot's avatar
Christophe Massiot committed
174 175
    /* Create audio thread and wait for its readiness. */
    if( vlc_thread_create( p_aout, "aout", QNXaoutThread,
176
                           VLC_THREAD_PRIORITY_OUTPUT, false ) )
Christophe Massiot's avatar
Christophe Massiot committed
177
    {
178
        msg_Err( p_aout, "cannot create QNX audio thread (%m)" );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
179
        CloseAudio( p_this );
Christophe Massiot's avatar
Christophe Massiot committed
180 181 182
        free( p_aout->output.p_sys );
        return -1;
    }
183

184 185 186 187 188 189 190 191 192 193 194
    return( 0 );
}

/*****************************************************************************
 * GetBufInfo: buffer status query
 *****************************************************************************
 * This function returns the number of used byte in the queue.
 * It also deals with errors : indeed if the device comes to run out
 * of data to play, it switches to the "underrun" status. It has to
 * be flushed and re-prepared
 *****************************************************************************/
195
static int GetBufInfo( aout_instance_t *p_aout )
196 197 198 199 200 201
{
    int i_ret;
    snd_pcm_channel_status_t status;

    /* get current pcm status */
    memset( &status, 0, sizeof(status) );
202
    if( ( i_ret = snd_pcm_plugin_status( p_aout->output.p_sys->p_pcm_handle,
203 204 205 206 207 208 209 210 211 212 213 214
                                         &status ) ) < 0 )
    {
        msg_Err( p_aout, "unable to get device status (%s)",
                         snd_strerror( i_ret ) );
        return( -1 );
    }

    /* check for underrun */
    switch( status.status )
    {
        case SND_PCM_STATUS_READY:
        case SND_PCM_STATUS_UNDERRUN:
215
            if( ( i_ret = snd_pcm_plugin_prepare( p_aout->output.p_sys->p_pcm_handle,
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                                          SND_PCM_CHANNEL_PLAYBACK ) ) < 0 )
            {
                msg_Err( p_aout, "unable to prepare channel (%s)",
                                 snd_strerror( i_ret ) );
            }
            break;
    }

    return( status.count );
}

/*****************************************************************************
 * Play : plays a sample
 *****************************************************************************
 * Plays a sample using the snd_pcm_write function from the alsa API
 *****************************************************************************/
232
static void Play( aout_instance_t *p_aout )
233 234 235 236 237 238
{
}

/*****************************************************************************
 * CloseAudio: close the audio device
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
239
void CloseAudio ( vlc_object_t *p_this )
240
{
241
    aout_instance_t *p_aout = (aout_instance_t *)p_this;
242 243
    int i_ret;

244
    vlc_object_kill( p_aout );
245 246 247
    vlc_thread_join( p_aout );

    if( ( i_ret = snd_pcm_close( p_aout->output.p_sys->p_pcm_handle ) ) < 0 )
248 249 250 251 252
    {
        msg_Err( p_aout, "unable to close audio device (%s)",
                         snd_strerror( i_ret ) );
    }

253 254
    free( p_aout->output.p_sys->p_silent_buffer );
    free( p_aout->output.p_sys );
255
}
256 257 258 259 260


/*****************************************************************************
 * QNXaoutThread: asynchronous thread used to DMA the data to the device
 *****************************************************************************/
261
static void* QNXaoutThread( vlc_object_t *p_this )
262
{
263
    aout_instance_t * p_aout = (aout_instance_t*)p_this;
264
    struct aout_sys_t * p_sys = p_aout->output.p_sys;
265
    int canc = vlc_savecancel ();
266

267
    while ( vlc_object_alive (p_aout) )
268 269 270
    {
        aout_buffer_t * p_buffer;
        int i_tmp, i_size;
271
        uint8_t * p_bytes;
272

273
        if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
274 275 276 277 278 279 280 281 282 283 284 285 286
        {
            mtime_t next_date = 0;

            /* Get the presentation date of the next write() operation. It
             * is equal to the current date + duration of buffered samples.
             * Order is important here, since GetBufInfo is believed to take
             * more time than mdate(). */
            next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
                      / p_aout->output.output.i_bytes_per_frame
                      / p_aout->output.output.i_rate
                      * p_aout->output.output.i_frame_length;
            next_date += mdate();

287
            p_buffer = aout_OutputNextBuffer( p_aout, next_date, false );
288 289 290
        }
        else
        {
291
            p_buffer = aout_OutputNextBuffer( p_aout, 0, true );
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        }

        if ( p_buffer != NULL )
        {
            p_bytes = p_buffer->p_buffer;
            i_size = p_buffer->i_nb_bytes;
        }
        else
        {
            i_size = DEFAULT_FRAME_SIZE / p_aout->output.output.i_frame_length
                      * p_aout->output.output.i_bytes_per_frame;
            p_bytes = p_aout->output.p_sys->p_silent_buffer;
            memset( p_bytes, 0, i_size );
        }

        i_tmp = snd_pcm_plugin_write( p_aout->output.p_sys->p_pcm_handle,
                                        (void *) p_bytes,
                                        (size_t) i_size );

        if( i_tmp < 0 )
        {
313
            msg_Err( p_aout, "write failed (%m)" );
314 315 316 317 318 319 320 321
        }

        if ( p_buffer != NULL )
        {
            aout_BufferFree( p_buffer );
        }
    }

322
    vlc_restorecancel (canc);
323
    return NULL;
324 325
}