oss.c 22.7 KB
Newer Older
1
/*****************************************************************************
2
 * oss.c : OSS /dev/dsp module for vlc
3
 *****************************************************************************
4
 * Copyright (C) 2000-2002 the VideoLAN team
5
 * $Id$
6 7
 *
 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8
 *          Sam Hocevar <sam@zoy.org>
9
 *          Christophe Massiot <massiot@via.ecp.fr>
10 11 12 13 14
 *
 * 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.
15
 *
16 17 18 19 20 21 22
 * 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
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 25 26 27 28 29
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

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

34 35 36 37
#include <fcntl.h>                                       /* open(), O_WRONLY */
#include <sys/ioctl.h>                                            /* ioctl() */
#include <unistd.h>                                      /* write(), close() */

38
#include <vlc_common.h>
39
#include <vlc_plugin.h>
40
#include <vlc_fs.h>
41

zorglub's avatar
zorglub committed
42
#include <vlc_aout.h>
43 44 45 46 47 48 49 50 51

/* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
 * SNDCTL_DSP_GETOSPACE */
#ifdef HAVE_SOUNDCARD_H
#   include <soundcard.h>
#elif defined( HAVE_SYS_SOUNDCARD_H )
#   include <sys/soundcard.h>
#endif

52 53
/* Patches for ignorant OSS versions */
#ifndef AFMT_AC3
54
#   define AFMT_AC3     0x00000400        /* Dolby Digital AC3 */
55 56 57 58 59 60 61 62 63 64
#endif

#ifndef AFMT_S16_NE
#   ifdef WORDS_BIGENDIAN
#       define AFMT_S16_NE AFMT_S16_BE
#   else
#       define AFMT_S16_NE AFMT_S16_LE
#   endif
#endif

65
/*****************************************************************************
66
 * aout_sys_t: OSS audio output method descriptor
67 68
 *****************************************************************************
 * This structure is part of the audio output thread descriptor.
69
 * It describes the DSP specific properties of an audio device.
70 71 72
 *****************************************************************************/
struct aout_sys_t
{
gbazin's avatar
 
gbazin committed
73
    int i_fd;
gbazin's avatar
 
gbazin committed
74 75
    int i_fragstotal;
    mtime_t max_buffer_duration;
Laurent Aimar's avatar
Laurent Aimar committed
76
    vlc_thread_t thread;
77 78
};

79
/* This must be a power of 2. */
80
#define FRAME_SIZE 1024
81
#define FRAME_COUNT 32
82

83 84 85 86 87 88
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
static void Close        ( vlc_object_t * );

89
static void Play         ( audio_output_t * );
Laurent Aimar's avatar
Laurent Aimar committed
90
static void* OSSThread   ( void * );
91

92
static mtime_t BufferDuration( audio_output_t * p_aout );
gbazin's avatar
 
gbazin committed
93

94 95 96
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
97 98
vlc_module_begin ()
    set_shortname( "OSS" )
99
    set_description( N_("Open Sound System") )
gbazin's avatar
 
gbazin committed
100

101 102
    set_category( CAT_AUDIO )
    set_subcategory( SUBCAT_AUDIO_AOUT )
103
    add_loadfile( "oss-audio-device", "/dev/dsp",
104
                  N_("OSS DSP device"), NULL, false )
105
        add_deprecated_alias( "dspdev" )   /* deprecated since 0.9.3 */
gbazin's avatar
 
gbazin committed
106

107 108 109 110
    set_capability( "audio output", 100 )
    add_shortcut( "oss" )
    set_callbacks( Open, Close )
vlc_module_end ()
111

112 113 114
/*****************************************************************************
 * Probe: probe the audio device for available formats and channels
 *****************************************************************************/
115
static void Probe( audio_output_t * p_aout )
116
{
117
    struct aout_sys_t * p_sys = p_aout->sys;
gbazin's avatar
 
gbazin committed
118
    vlc_value_t val, text;
119 120
    int i_format, i_nb_channels;

gbazin's avatar
 
gbazin committed
121
    var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
122
    text.psz_string = _("Audio Device");
gbazin's avatar
 
gbazin committed
123
    var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
124

gbazin's avatar
 
gbazin committed
125
    /* Test for multi-channel. */
126
#ifdef SNDCTL_DSP_GETCHANNELMASK
127
    if ( aout_FormatNbChannels( &p_aout->format ) > 2 )
128 129 130 131
    {
        /* Check that the device supports this. */

        int i_chanmask;
gbazin's avatar
 
gbazin committed
132 133 134 135 136 137 138 139 140 141 142

        /* Reset all. */
        i_format = AFMT_S16_NE;
        if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
            ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
        {
            msg_Err( p_aout, "cannot reset OSS audio device" );
            var_Destroy( p_aout, "audio-device" );
            return;
        }

143 144 145 146 147
        if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
                    &i_chanmask ) == 0 )
        {
            if ( !(i_chanmask & DSP_BIND_FRONT) )
            {
148
                msg_Err( p_aout, "no front channels! (%x)",
149 150 151 152 153
                         i_chanmask );
                return;
            }

            if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
154
                  && (p_aout->format.i_physical_channels ==
155 156 157 158
                       (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
                         | AOUT_CHAN_LFE)) )
            {
gbazin's avatar
 
gbazin committed
159
                val.i_int = AOUT_VAR_5_1;
160
                text.psz_string = (char*) "5.1";
gbazin's avatar
 
gbazin committed
161 162
                var_Change( p_aout, "audio-device",
                            VLC_VAR_ADDCHOICE, &val, &text );
163 164 165
            }

            if ( (i_chanmask & DSP_BIND_SURR)
166
                  && (p_aout->format.i_physical_channels &
167 168 169
                       (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
                         | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
            {
gbazin's avatar
 
gbazin committed
170
                val.i_int = AOUT_VAR_2F2R;
171
                text.psz_string = _("2 Front 2 Rear");
gbazin's avatar
 
gbazin committed
172 173
                var_Change( p_aout, "audio-device",
                            VLC_VAR_ADDCHOICE, &val, &text );
174 175 176 177 178
            }
        }
    }
#endif

gbazin's avatar
 
gbazin committed
179 180 181 182 183 184 185 186 187 188
    /* Reset all. */
    i_format = AFMT_S16_NE;
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
        ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
    {
        msg_Err( p_aout, "cannot reset OSS audio device" );
        var_Destroy( p_aout, "audio-device" );
        return;
    }

189 190 191 192 193
    /* Test for stereo. */
    i_nb_channels = 2;
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
         && i_nb_channels == 2 )
    {
gbazin's avatar
 
gbazin committed
194
        val.i_int = AOUT_VAR_STEREO;
195
        text.psz_string = _("Stereo");
gbazin's avatar
 
gbazin committed
196
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
197 198 199 200 201 202 203 204 205 206 207 208 209 210
    }

    /* Reset all. */
    i_format = AFMT_S16_NE;
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
        ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
    {
        msg_Err( p_aout, "cannot reset OSS audio device" );
        var_Destroy( p_aout, "audio-device" );
        return;
    }

    /* Test for mono. */
    i_nb_channels = 1;
211
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
212 213
         && i_nb_channels == 1 )
    {
gbazin's avatar
 
gbazin committed
214
        val.i_int = AOUT_VAR_MONO;
215
        text.psz_string = _("Mono");
gbazin's avatar
 
gbazin committed
216
        var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
217
        if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER )
218 219 220
        {
            var_Set( p_aout, "audio-device", val );
        }
221
    }
222

gbazin's avatar
 
gbazin committed
223 224 225 226 227 228 229 230
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
    {
        msg_Err( p_aout, "cannot reset OSS audio device" );
        var_Destroy( p_aout, "audio-device" );
        return;
    }

    /* Test for spdif. */
231
    if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
gbazin's avatar
 
gbazin committed
232 233 234 235 236 237
    {
        i_format = AFMT_AC3;

        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
             && i_format == AFMT_AC3 )
        {
gbazin's avatar
 
gbazin committed
238
            val.i_int = AOUT_VAR_SPDIF;
239
            text.psz_string = _("A/52 over S/PDIF");
gbazin's avatar
 
gbazin committed
240 241
            var_Change( p_aout, "audio-device",
                        VLC_VAR_ADDCHOICE, &val, &text );
242
            if( var_InheritBool( p_aout, "spdif" ) )
gbazin's avatar
 
gbazin committed
243 244
                var_Set( p_aout, "audio-device", val );
        }
245
        else if( var_InheritBool( p_aout, "spdif" ) )
246
        {
247
            msg_Warn( p_aout, "S/PDIF not supported by card" );
248
        }
249
    }
250 251 252

    var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
                     NULL );
253 254
}

255
/*****************************************************************************
256
 * Open: open the audio device (the digital sound processor)
257
 *****************************************************************************
258
 * This function opens the DSP as a usual non-blocking write-only file, and
259 260 261 262
 * modifies the p_aout->p_sys->i_fd with the file's descriptor.
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
263
    audio_output_t * p_aout = (audio_output_t *)p_this;
264 265
    struct aout_sys_t * p_sys;
    char * psz_device;
266
    vlc_value_t val;
267 268

    /* Allocate structure */
269
    p_aout->sys = p_sys = malloc( sizeof( aout_sys_t ) );
270
    if( p_sys == NULL )
271
        return VLC_ENOMEM;
272

273
    /* Get device name */
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
274
    if( (psz_device = var_InheritString( p_aout, "oss-audio-device" )) == NULL )
275
    {
276
        msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
277
        free( p_sys );
278
        return VLC_EGENERIC;
279 280
    }

281 282 283 284 285
    /* Open the sound device in non-blocking mode, because ALSA's OSS
     * emulation and some broken OSS drivers would make a blocking call
     * wait forever until the device is available. Since this breaks the
     * OSS spec, we immediately put it back to blocking mode if the
     * operation was successful. */
286
    p_sys->i_fd = vlc_open( psz_device, O_WRONLY | O_NDELAY );
gbazin's avatar
 
gbazin committed
287
    if( p_sys->i_fd < 0 )
288
    {
289
        msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
ivoire's avatar
ivoire committed
290
        free( psz_device );
291
        free( p_sys );
292
        return VLC_EGENERIC;
293
    }
294 295 296 297 298

    /* if the opening was ok, put the device back in blocking mode */
    fcntl( p_sys->i_fd, F_SETFL,
            fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );

Christophe Massiot's avatar
Christophe Massiot committed
299
    free( psz_device );
300

301 302
    p_aout->pf_play = Play;
    p_aout->pf_pause = NULL;
303
    p_aout->pf_flush = NULL;
304

gbazin's avatar
 
gbazin committed
305
    if ( var_Type( p_aout, "audio-device" ) == 0 )
306 307 308 309 310 311 312
    {
        Probe( p_aout );
    }

    if ( var_Get( p_aout, "audio-device", &val ) < 0 )
    {
        /* Probe() has failed. */
ivoire's avatar
ivoire committed
313
        close( p_sys->i_fd );
314 315 316 317
        free( p_sys );
        return VLC_EGENERIC;
    }

gbazin's avatar
 
gbazin committed
318
    if ( val.i_int == AOUT_VAR_SPDIF )
319
    {
320
        p_aout->format.i_format = VLC_CODEC_SPDIFL;
321
    }
gbazin's avatar
 
gbazin committed
322
    else if ( val.i_int == AOUT_VAR_5_1 )
323
    {
324 325
        p_aout->format.i_format = VLC_CODEC_S16N;
        p_aout->format.i_physical_channels
326
            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
327
               | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
328 329
               | AOUT_CHAN_LFE;
    }
gbazin's avatar
 
gbazin committed
330
    else if ( val.i_int == AOUT_VAR_2F2R )
331
    {
332 333
        p_aout->format.i_format = VLC_CODEC_S16N;
        p_aout->format.i_physical_channels
334
            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
335
               | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
336
    }
gbazin's avatar
 
gbazin committed
337
    else if ( val.i_int == AOUT_VAR_STEREO )
338
    {
339 340
        p_aout->format.i_format = VLC_CODEC_S16N;
        p_aout->format.i_physical_channels
341 342
            = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
    }
gbazin's avatar
 
gbazin committed
343
    else if ( val.i_int == AOUT_VAR_MONO )
344
    {
345 346
        p_aout->format.i_format = VLC_CODEC_S16N;
        p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
347
    }
348 349 350
    else
    {
        /* This should not happen ! */
351 352
        msg_Err( p_aout, "internal: can't find audio-device (%"PRId64")",
                 val.i_int );
ivoire's avatar
ivoire committed
353
        close( p_sys->i_fd );
354 355 356
        free( p_sys );
        return VLC_EGENERIC;
    }
357

358
    var_TriggerCallback( p_aout, "intf-change" );
359

360
    /* Reset the DSP device */
361
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
362
    {
363
        msg_Err( p_aout, "cannot reset OSS audio device" );
364 365
        close( p_sys->i_fd );
        free( p_sys );
366 367
        return VLC_EGENERIC;
    }
368

369
    /* Set the output format */
370
    if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
371
    {
372
        int i_format = AFMT_AC3;
373

374 375 376
        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
             || i_format != AFMT_AC3 )
        {
377 378 379 380
            msg_Err( p_aout, "cannot reset OSS audio device" );
            close( p_sys->i_fd );
            free( p_sys );
            return VLC_EGENERIC;
381 382
        }

383 384 385 386
        p_aout->format.i_format = VLC_CODEC_SPDIFL;
        p_aout->i_nb_samples = A52_FRAME_NB;
        p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
        p_aout->format.i_frame_length = A52_FRAME_NB;
387 388

        aout_VolumeNoneInit( p_aout );
389
    }
390

391
    if ( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
392
    {
393 394 395 396
        unsigned int i_format = AFMT_S16_NE;
        unsigned int i_frame_size, i_fragments;
        unsigned int i_rate;
        unsigned int i_nb_channels;
gbazin's avatar
 
gbazin committed
397
        audio_buf_info audio_buf;
398 399 400 401 402 403 404 405 406 407 408 409

        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
        {
            msg_Err( p_aout, "cannot set audio output format" );
            close( p_sys->i_fd );
            free( p_sys );
            return VLC_EGENERIC;
        }

        switch ( i_format )
        {
        case AFMT_U8:
410
            p_aout->format.i_format = VLC_CODEC_U8;
411 412
            break;
        case AFMT_S8:
413
            p_aout->format.i_format = VLC_CODEC_S8;
414 415
            break;
        case AFMT_U16_LE:
416
            p_aout->format.i_format = VLC_CODEC_U16L;
417 418
            break;
        case AFMT_S16_LE:
419
            p_aout->format.i_format = VLC_CODEC_S16L;
420 421
            break;
        case AFMT_U16_BE:
422
            p_aout->format.i_format = VLC_CODEC_U16B;
423 424
            break;
        case AFMT_S16_BE:
425
            p_aout->format.i_format = VLC_CODEC_S16B;
426 427 428 429 430 431 432 433 434
            break;
        default:
            msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
                     i_format );
            close( p_sys->i_fd );
            free( p_sys );
            return VLC_EGENERIC;
        }

435
        i_nb_channels = aout_FormatNbChannels( &p_aout->format );
436

437
        /* Set the number of channels */
438
        if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
439
            i_nb_channels != aout_FormatNbChannels( &p_aout->format ) )
440
        {
441
            msg_Err( p_aout, "cannot set number of audio channels (%s)",
442
                     aout_FormatPrintChannels( &p_aout->format) );
443 444
            close( p_sys->i_fd );
            free( p_sys );
445
            return VLC_EGENERIC;
446 447 448
        }

        /* Set the output rate */
449
        i_rate = p_aout->format.i_rate;
450 451 452
        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
        {
            msg_Err( p_aout, "cannot set audio output rate (%i)",
453
                             p_aout->format.i_rate );
454 455
            close( p_sys->i_fd );
            free( p_sys );
456
            return VLC_EGENERIC;
457 458
        }

459
        if( i_rate != p_aout->format.i_rate )
460
        {
461
            p_aout->format.i_rate = i_rate;
462
        }
gbazin's avatar
 
gbazin committed
463 464

        /* Set the fragment size */
465
        aout_FormatPrepare( &p_aout->format );
gbazin's avatar
 
gbazin committed
466 467 468

        /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
         *                              1 << yyyy   is fragsize */
469
        i_frame_size = ((uint64_t)p_aout->format.i_bytes_per_frame * p_aout->format.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
470 471
        i_fragments = 4;
        while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
gbazin's avatar
 
gbazin committed
472 473 474 475 476 477 478 479 480 481 482
        {
            ++i_fragments;
        }
        i_fragments |= FRAME_COUNT << 16;
        if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
        {
            msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
        }

        if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
        {
483
            msg_Err( p_aout, "cannot get fragment size" );
gbazin's avatar
 
gbazin committed
484 485 486 487
            close( p_sys->i_fd );
            free( p_sys );
            return VLC_EGENERIC;
        }
gbazin's avatar
 
gbazin committed
488
        else
gbazin's avatar
 
gbazin committed
489 490
        {
            /* Number of fragments actually allocated */
491
            p_aout->sys->i_fragstotal = audio_buf.fragstotal;
gbazin's avatar
 
gbazin committed
492 493

            /* Maximum duration the soundcard's buffer can hold */
494
            p_aout->sys->max_buffer_duration =
gbazin's avatar
 
gbazin committed
495
                (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
496 497 498
                / p_aout->format.i_bytes_per_frame
                / p_aout->format.i_rate
                * p_aout->format.i_frame_length;
gbazin's avatar
 
gbazin committed
499

500 501
            p_aout->i_nb_samples = audio_buf.fragsize /
                p_aout->format.i_bytes_per_frame;
gbazin's avatar
 
gbazin committed
502 503 504
        }

        aout_VolumeSoftInit( p_aout );
505 506
    }

Christophe Massiot's avatar
Christophe Massiot committed
507
    /* Create OSS thread and wait for its readiness. */
Laurent Aimar's avatar
Laurent Aimar committed
508 509
    if( vlc_clone( &p_sys->thread, OSSThread, p_aout,
                   VLC_THREAD_PRIORITY_OUTPUT ) )
Christophe Massiot's avatar
Christophe Massiot committed
510
    {
511
        msg_Err( p_aout, "cannot create OSS thread (%m)" );
Christophe Massiot's avatar
Christophe Massiot committed
512 513
        close( p_sys->i_fd );
        free( p_sys );
514
        return VLC_ENOMEM;
Christophe Massiot's avatar
Christophe Massiot committed
515
    }
516

517
    return VLC_SUCCESS;
518 519 520
}

/*****************************************************************************
Christophe Massiot's avatar
Christophe Massiot committed
521
 * Play: nothing to do
522
 *****************************************************************************/
523
static void Play( audio_output_t *p_aout )
524
{
525
    VLC_UNUSED(p_aout);
526 527 528
}

/*****************************************************************************
529
 * Close: close the DSP audio device
530 531 532
 *****************************************************************************/
static void Close( vlc_object_t * p_this )
{
533
    audio_output_t *p_aout = (audio_output_t *)p_this;
534
    struct aout_sys_t * p_sys = p_aout->sys;
535

Laurent Aimar's avatar
Laurent Aimar committed
536 537
    vlc_cancel( p_sys->thread );
    vlc_join( p_sys->thread, NULL );
538
    p_aout->b_die = false;
539

540
    ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
541
    close( p_sys->i_fd );
542

543
    free( p_sys );
544 545 546
}

/*****************************************************************************
547
 * BufferDuration: buffer status query
548
 *****************************************************************************
549
 * This function returns the duration in microseconds of the current buffer.
550
 *****************************************************************************/
551
static mtime_t BufferDuration( audio_output_t * p_aout )
552
{
553
    struct aout_sys_t * p_sys = p_aout->sys;
554
    audio_buf_info audio_buf;
555
    int i_bytes;
556

557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
#ifdef SNDCTL_DSP_GETODELAY
    if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETODELAY, &i_bytes ) < 0 )
#endif
    {
        /* Fall back to GETOSPACE and approximate latency. */

        /* Fill the audio_buf_info structure:
         * - fragstotal: total number of fragments allocated
         * - fragsize: size of a fragment in bytes
         * - bytes: available space in bytes (includes partially used fragments)
         * Note! 'bytes' could be more than fragments*fragsize */
        ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );

        /* calculate number of available fragments (not partially used ones) */
        i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
    }
573 574 575

    /* Return the fragment duration */
    return (mtime_t)i_bytes * 1000000
576 577 578
            / p_aout->format.i_bytes_per_frame
            / p_aout->format.i_rate
            * p_aout->format.i_frame_length;
579 580
}

Laurent Aimar's avatar
Laurent Aimar committed
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
typedef struct
{
    aout_buffer_t *p_buffer;
    void          *p_bytes;
} oss_thread_ctx_t;

static void OSSThreadCleanup( void *data )
{
    oss_thread_ctx_t *p_ctx = data;
    if( p_ctx->p_buffer )
        aout_BufferFree( p_ctx->p_buffer );
    else
        free( p_ctx->p_bytes );
}

596
/*****************************************************************************
597
 * OSSThread: asynchronous thread used to DMA the data to the device
598
 *****************************************************************************/
Laurent Aimar's avatar
Laurent Aimar committed
599
static void* OSSThread( void *obj )
600
{
601
    audio_output_t * p_aout = (audio_output_t*)obj;
602
    struct aout_sys_t * p_sys = p_aout->sys;
603
    mtime_t next_date = 0;
604

Laurent Aimar's avatar
Laurent Aimar committed
605
    for( ;; )
606
    {
607
        aout_buffer_t * p_buffer = NULL;
608

Laurent Aimar's avatar
Laurent Aimar committed
609
        int canc = vlc_savecancel ();
610
        if ( p_aout->format.i_format != VLC_CODEC_SPDIFL )
Christophe Massiot's avatar
Christophe Massiot committed
611
        {
612
            mtime_t buffered = BufferDuration( p_aout );
613

gbazin's avatar
 
gbazin committed
614 615
            /* Next buffer will be played at mdate() + buffered */
            p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
616
                                              false );
gbazin's avatar
 
gbazin committed
617 618

            if( p_buffer == NULL &&
619 620
                buffered > ( p_aout->sys->max_buffer_duration
                             / p_aout->sys->i_fragstotal ) )
621
            {
Laurent Aimar's avatar
Laurent Aimar committed
622
                vlc_restorecancel (canc);
gbazin's avatar
 
gbazin committed
623 624 625
                /* If we have at least a fragment full, then we can wait a
                 * little and retry to get a new audio buffer instead of
                 * playing a blank sample */
626 627
                msleep( ( p_aout->sys->max_buffer_duration
                          / p_aout->sys->i_fragstotal / 2 ) );
gbazin's avatar
 
gbazin committed
628
                continue;
629
            }
630 631 632
        }
        else
        {
Laurent Aimar's avatar
Laurent Aimar committed
633 634
            vlc_restorecancel (canc);

635 636 637 638 639 640 641 642 643
            /* emu10k1 driver does not report Buffer Duration correctly in
             * passthrough mode so we have to cheat */
            if( !next_date )
            {
                next_date = mdate();
            }
            else
            {
                mtime_t delay = next_date - mdate();
644
                if( delay > AOUT_MAX_PTS_ADVANCE )
645 646 647 648
                {
                    msleep( delay / 2 );
                }
            }
649

Laurent Aimar's avatar
Laurent Aimar committed
650
            for( ;; )
651
            {
Laurent Aimar's avatar
Laurent Aimar committed
652 653 654 655 656 657
                canc = vlc_savecancel ();
                p_buffer = aout_OutputNextBuffer( p_aout, next_date, true );
                if ( p_buffer )
                    break;
                vlc_restorecancel (canc);

658
                msleep( VLC_HARD_MIN_SLEEP );
659 660
                next_date = mdate();
            }
661
        }
662

Laurent Aimar's avatar
Laurent Aimar committed
663 664
        uint8_t * p_bytes;
        int i_size;
665 666 667
        if ( p_buffer != NULL )
        {
            p_bytes = p_buffer->p_buffer;
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
668
            i_size = p_buffer->i_buffer;
669 670
            /* This is theoretical ... we'll see next iteration whether
             * we're drifting */
671
            next_date += p_buffer->i_length;
672 673 674
        }
        else
        {
675 676
            i_size = FRAME_SIZE / p_aout->format.i_frame_length
                      * p_aout->format.i_bytes_per_frame;
677
            p_bytes = malloc( i_size );
Christophe Massiot's avatar
Christophe Massiot committed
678
            memset( p_bytes, 0, i_size );
679
            next_date = 0;
680 681
        }

Laurent Aimar's avatar
Laurent Aimar committed
682 683 684 685 686 687 688 689 690
        oss_thread_ctx_t ctx = {
            .p_buffer = p_buffer,
            .p_bytes  = p_bytes,
        };

        vlc_cleanup_push( OSSThreadCleanup, &ctx );
        vlc_restorecancel( canc );

        int i_tmp = write( p_sys->i_fd, p_bytes, i_size );
691 692 693

        if( i_tmp < 0 )
        {
Rafaël Carré's avatar
Rafaël Carré committed
694
            msg_Err( p_aout, "write failed (%m)" );
695
        }
Laurent Aimar's avatar
Laurent Aimar committed
696
        vlc_cleanup_run();
697
    }
698

699
    return NULL;
700
}