oss.c 12.8 KB
Newer Older
1 2 3
/*****************************************************************************
 * oss.c : OSS input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2002-2009 VLC authors and VideoLAN
5 6 7 8 9 10 11
 * $Id$
 *
 * Authors: Benjamin Pracht <bigben at videolan dot org>
 *          Richard Hosking <richard at hovis dot net>
 *          Antoine Cellerier <dionoea at videolan d.t org>
 *          Dennis Lou <dlou99 at yahoo dot com>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (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
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 26 27 28 29 30 31 32 33 34 35 36 37 38
 *****************************************************************************/

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

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
#include <vlc_demux.h>
39
#include <vlc_fs.h>
40

41
#include <errno.h>
42 43 44 45 46
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>

Joris van Rooij's avatar
Joris van Rooij committed
47 48 49 50 51 52
#ifdef HAVE_SYS_SOUNDCARD_H
#  include <sys/soundcard.h>
#endif
#ifdef HAVE_SOUNDCARD_H
#  include <soundcard.h>
#endif
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

#include <poll.h>

/*****************************************************************************
 * Module descriptior
 *****************************************************************************/

static int  DemuxOpen ( vlc_object_t * );
static void DemuxClose( vlc_object_t * );


#define STEREO_TEXT N_( "Stereo" )
#define STEREO_LONGTEXT N_( \
    "Capture the audio stream in stereo." )
#define SAMPLERATE_TEXT N_( "Samplerate" )
#define SAMPLERATE_LONGTEXT N_( \
    "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )

#define OSS_DEFAULT "/dev/dsp"

#define CFG_PREFIX "oss-"

vlc_module_begin ()
    set_shortname( N_("OSS") )
    set_description( N_("OSS input") )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )

    add_shortcut( "oss" )
    set_capability( "access_demux", 10 )
    set_callbacks( DemuxOpen, DemuxClose )

85
    add_bool( CFG_PREFIX "stereo", true, STEREO_TEXT, STEREO_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
86
                true )
87
    add_integer( CFG_PREFIX "samplerate", 48000, SAMPLERATE_TEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
88
                SAMPLERATE_LONGTEXT, true )
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
vlc_module_end ()

/*****************************************************************************
 * Access: local prototypes
 *****************************************************************************/

static int DemuxControl( demux_t *, int, va_list );

static int Demux( demux_t * );

static block_t* GrabAudio( demux_t *p_demux );

static int OpenAudioDev( demux_t * );
static int OpenAudioDevOss( demux_t * );
static bool ProbeAudioDevOss( demux_t *, const char *psz_device );

struct buffer_t
{
    void *  start;
    size_t  length;
};

struct demux_sys_t
{
    const char *psz_device;  /* OSS device from MRL */

Antoine Cellerier's avatar
Antoine Cellerier committed
115
    int  i_fd;
116 117 118 119

    /* Audio */
    unsigned int i_sample_rate;
    bool b_stereo;
Antoine Cellerier's avatar
Antoine Cellerier committed
120 121 122
    size_t i_max_frame_size;
    block_t *p_block;
    es_out_id_t *p_es;
123 124

    int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
125 126 127 128 129 130 131 132
};

static int FindMainDevice( demux_t *p_demux )
{
    msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
    if( ProbeAudioDevOss( p_demux, p_demux->p_sys->psz_device ) )
    {
        msg_Dbg( p_demux, "'%s' is an audio device", p_demux->p_sys->psz_device );
Antoine Cellerier's avatar
Antoine Cellerier committed
133
        p_demux->p_sys->i_fd = OpenAudioDev( p_demux );
134 135
    }

Antoine Cellerier's avatar
Antoine Cellerier committed
136
    if( p_demux->p_sys->i_fd < 0 )
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
        return VLC_EGENERIC;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * DemuxOpen: opens oss device, access_demux callback
 *****************************************************************************
 *
 * url: <oss device>::::
 *
 *****************************************************************************/
static int DemuxOpen( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;

    /* Only when selected */
    if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;

    /* Set up p_demux */
    p_demux->pf_control = DemuxControl;
    p_demux->pf_demux = Demux;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

163
    p_demux->p_sys = p_sys = vlc_calloc( p_this, 1, sizeof( demux_sys_t ) );
164 165
    if( p_sys == NULL ) return VLC_ENOMEM;

166 167
    p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
    p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
Antoine Cellerier's avatar
Antoine Cellerier committed
168 169 170
    p_sys->i_fd = -1;
    p_sys->p_es = NULL;
    p_sys->p_block = NULL;
171
    p_sys->i_next_demux_date = -1;
172

173 174
    if( p_demux->psz_location && *p_demux->psz_location )
        p_sys->psz_device = p_demux->psz_location;
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    else
        p_sys->psz_device = OSS_DEFAULT;

    if( FindMainDevice( p_demux ) != VLC_SUCCESS )
    {
        DemuxClose( p_this );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}

/*****************************************************************************
 * Close: close device, free resources
 *****************************************************************************/
static void DemuxClose( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys   = p_demux->p_sys;

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
195 196
    if( p_sys->i_fd >= 0 )
        vlc_close( p_sys->i_fd );
197

Antoine Cellerier's avatar
Antoine Cellerier committed
198
    if( p_sys->p_block ) block_Release( p_sys->p_block );
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
}

/*****************************************************************************
 * DemuxControl:
 *****************************************************************************/
static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    bool *pb;
    int64_t    *pi64;

    switch( i_query )
    {
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_SEEK:
        case DEMUX_CAN_CONTROL_PACE:
216
            pb = va_arg( args, bool * );
217 218 219 220
            *pb = false;
            return VLC_SUCCESS;

        case DEMUX_GET_PTS_DELAY:
221
            pi64 = va_arg( args, int64_t * );
222 223
            *pi64 = INT64_C(1000)
                  * var_InheritInteger( p_demux, "live-caching" );
224 225 226
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
227
            pi64 = va_arg( args, int64_t * );
228 229 230
            *pi64 = mdate();
            return VLC_SUCCESS;

231
        case DEMUX_SET_NEXT_DEMUX_TIME:
232
            p_sys->i_next_demux_date = va_arg( args, int64_t );
233 234
            return VLC_SUCCESS;

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
        /* TODO implement others */
        default:
            return VLC_EGENERIC;
    }

    return VLC_EGENERIC;
}

/*****************************************************************************
 * Demux: Processes the audio frame
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    struct pollfd fd;
Antoine Cellerier's avatar
Antoine Cellerier committed
251
    fd.fd = p_sys->i_fd;
252 253 254
    fd.events = POLLIN|POLLPRI;
    fd.revents = 0;

255 256 257
    block_t *p_block = NULL;

    do
258
    {
259
        if( p_block )
260
        {
261 262 263 264 265
            es_out_Send( p_demux->out, p_sys->p_es, p_block );
            p_block = NULL;
        }

        /* Wait for data */
266
        if( poll( &fd, 1, 10 ) ) /* Timeout after 0.01 seconds. Bigger delays are an issue when used with/as an input-slave since all the inputs run in the same thread. */
267
        {
268 269
            if( errno == EINTR )
                continue;
270
            if( fd.revents & (POLLIN|POLLPRI) )
271
            {
272 273
                p_block = GrabAudio( p_demux );
                if( p_block )
274
                    es_out_SetPCR( p_demux->out, p_block->i_pts );
275 276
            }
        }
277 278 279 280 281
    } while( p_block && p_sys->i_next_demux_date > 0 &&
             p_block->i_pts < p_sys->i_next_demux_date );

    if( p_block )
        es_out_Send( p_demux->out, p_sys->p_es, p_block );
282 283 284 285 286 287 288 289 290 291 292 293 294 295

    return 1;
}

/*****************************************************************************
 * GrabAudio: Grab an audio frame
 *****************************************************************************/
static block_t* GrabAudio( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    struct audio_buf_info buf_info;
    int i_read = 0, i_correct;
    block_t *p_block;

Antoine Cellerier's avatar
Antoine Cellerier committed
296
    if( p_sys->p_block ) p_block = p_sys->p_block;
297
    else p_block = block_Alloc( p_sys->i_max_frame_size );
298 299 300 301

    if( !p_block )
    {
        msg_Warn( p_demux, "cannot get block" );
302
        return NULL;
303 304
    }

Antoine Cellerier's avatar
Antoine Cellerier committed
305
    p_sys->p_block = p_block;
306

Antoine Cellerier's avatar
Antoine Cellerier committed
307 308
    i_read = read( p_sys->i_fd, p_block->p_buffer,
                p_sys->i_max_frame_size );
309

310
    if( i_read <= 0 ) return NULL;
311 312

    p_block->i_buffer = i_read;
313
    p_sys->p_block = NULL;
314 315 316

    /* Correct the date because of kernel buffering */
    i_correct = i_read;
Antoine Cellerier's avatar
Antoine Cellerier committed
317
    if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
    {
        i_correct += buf_info.bytes;
    }

    /* Timestamp */
    p_block->i_pts = p_block->i_dts =
        mdate() - INT64_C(1000000) * (mtime_t)i_correct /
        2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;

    return p_block;
}

/*****************************************************************************
 * OpenAudioDev: open and set up the audio device and probe for capabilities
 *****************************************************************************/
static int OpenAudioDevOss( demux_t *p_demux )
{
    int i_fd;
    int i_format;

338
    i_fd = vlc_open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
339 340 341

    if( i_fd < 0 )
    {
342 343
        msg_Err( p_demux, "cannot open OSS audio device (%s)",
                 vlc_strerror_c(errno) );
344 345 346 347 348 349 350 351
        goto adev_fail;
    }

    i_format = AFMT_S16_LE;
    if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
        || i_format != AFMT_S16_LE )
    {
        msg_Err( p_demux,
352 353
                 "cannot set audio format (16b little endian) (%s)",
                 vlc_strerror_c(errno) );
354 355 356 357 358 359
        goto adev_fail;
    }

    if( ioctl( i_fd, SNDCTL_DSP_STEREO,
               &p_demux->p_sys->b_stereo ) < 0 )
    {
360 361
        msg_Err( p_demux, "cannot set audio channels count (%s)",
                 vlc_strerror_c(errno) );
362 363 364 365 366 367
        goto adev_fail;
    }

    if( ioctl( i_fd, SNDCTL_DSP_SPEED,
               &p_demux->p_sys->i_sample_rate ) < 0 )
    {
368 369
        msg_Err( p_demux, "cannot set audio sample rate (%s)",
                 vlc_strerror_c(errno) );
370 371 372
        goto adev_fail;
    }

Antoine Cellerier's avatar
Antoine Cellerier committed
373
    p_demux->p_sys->i_max_frame_size = 6 * 1024;
374 375 376 377 378

    return i_fd;

 adev_fail:

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
379 380
    if( i_fd >= 0 )
        vlc_close( i_fd );
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
    return -1;
}

static int OpenAudioDev( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    int i_fd = OpenAudioDevOss( p_demux );

    if( i_fd < 0 )
        return i_fd;

    msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
             p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
             p_sys->i_sample_rate );

    es_format_t fmt;
397
    es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
398 399 400 401 402 403 404 405 406 407

    fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
    fmt.audio.i_rate = p_sys->i_sample_rate;
    fmt.audio.i_bitspersample = 16;
    fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
    fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;

    msg_Dbg( p_demux, "new audio es %d channels %dHz",
             fmt.audio.i_channels, fmt.audio.i_rate );

Antoine Cellerier's avatar
Antoine Cellerier committed
408
    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
409 410 411 412 413 414 415 416 417 418

    return i_fd;
}

/*****************************************************************************
 * ProbeAudioDev: probe audio for capabilities
 *****************************************************************************/
static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
{
    int i_caps;
419
    int i_fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
420 421 422

    if( i_fd < 0 )
    {
423 424
        msg_Err( p_demux, "cannot open device %s for OSS audio (%s)",
                 psz_device, vlc_strerror_c(errno) );
425 426 427 428 429 430
        goto open_failed;
    }

    /* this will fail if the device is video */
    if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
    {
431 432
        msg_Err( p_demux, "cannot get audio caps (%s)",
                 vlc_strerror_c(errno) );
433 434 435
        goto open_failed;
    }

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
436 437
    if( i_fd >= 0 )
        vlc_close( i_fd );
438 439 440 441

    return true;

open_failed:
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
442
    if( i_fd >= 0 )
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
443
        vlc_close( i_fd );
444 445
    return false;
}