Commit 793ac1e6 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

ALSA: rewrite capture plugin

 * use a thread instead of short interval polling,
 * do not assume latency is shorter than a period,
 * negotiate sample format instead automatically,
 * map all known ALSA formats to VLC codecs,
 * negotiate channels and sample rate,
 * partial support for 3-6 channels (missing preferences),
 * negotiate buffer and period times according to caching,
 * tolerate off-by-one period size due to rounding,
 * use the "default" device by default rather than "hw",
 * remove old-style hardware-only suggestions in debug messages.
parent 0ef62578
/***************************************************************************** /*****************************************************************************
* alsa.c : Alsa input module for vlc * alsa.c: ALSA capture module for VLC
***************************************************************************** *****************************************************************************
* Copyright (C) 2002-2011 the VideoLAN team * Copyright (C) 2012 Rémi Denis-Courmont
* $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>
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU Lesser General Public License for more details.
* *
* You should have received a copy of the GNU General Public License * You should have received a copy of the Lesser GNU General Public License
* along with this program; if not, write to the Free Software * along with this program; if not, write to the Free Software Foundation,
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA. * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
/*
* ALSA support based on parts of
* http://www.equalarea.com/paul/alsa-audio.html
* and hints taken from alsa-utils (aplay/arecord)
* http://www.alsa-project.org
*/
/*****************************************************************************
* Preamble
*****************************************************************************/ *****************************************************************************/
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include <config.h>
#endif #endif
#include <assert.h>
#include <sys/types.h>
#include <poll.h>
#include <alsa/asoundlib.h>
#include <vlc_common.h> #include <vlc_common.h>
#include <vlc_plugin.h>
#include <vlc_access.h>
#include <vlc_demux.h> #include <vlc_demux.h>
#include <vlc_input.h>
#include <vlc_fourcc.h>
#include <vlc_aout.h> #include <vlc_aout.h>
#include <vlc_plugin.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/soundcard.h>
#define ALSA_PCM_NEW_HW_PARAMS_API
#define ALSA_PCM_NEW_SW_PARAMS_API
#include <alsa/asoundlib.h>
#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 FORMAT_TEXT N_( "Capture format (default s16l)" )
#define FORMAT_LONGTEXT N_( \
"Capture format of audio stream." )
#define SAMPLERATE_TEXT N_( "Samplerate" )
#define SAMPLERATE_LONGTEXT N_( \
"Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
#define HELP_TEXT N_( \ #define HELP_TEXT N_( \
"Use alsa:// to open the default audio input. If multiple audio " \ "Pass alsa:// to open the default ALSA capture device, " \
"inputs are available, they will be listed in the vlc debug output. " \ "or alsa://SOURCE to open a specific device named SOURCE.")
"To select hw:0,1 , use alsa://hw:0,1 ." ) #define STEREO_TEXT N_("Stereo")
#define RATE_TEXT N_("Sample rate")
#define ALSA_DEFAULT "hw"
#define CFG_PREFIX "alsa-" static int Open (vlc_object_t *);
static void Close (vlc_object_t *);
static const char *const ppsz_fourcc[] = {
"u8", "s8", "gsm", "u16l", "s16l", "u16b", "s16b", static const int rate_values[] = { 192000, 176400,
"u24l", "s24l", "u24b", "s24b", "u32l", "s32l", 96000, 88200, 48000, 44100,
"u32b", "s32b", "f32l", "f32b", "f64l", "f64b" 32000, 22050, 24000, 16000,
11025, 8000, 4000
}; };
static const char *const ppsz_fourcc_text[] = { static const const char *rate_names[] = { N_("192000 Hz"), N_("176400 Hz"),
N_("PCM U8"), N_("PCM S8"), N_("GSM Audio"), N_("96000 Hz"), N_("88200 Hz"), N_("48000 Hz"), N_("44100 Hz"),
N_("PCM U16 LE"), N_("PCM S16 LE"), N_("32000 Hz"), N_("22050 Hz"), N_("24000 Hz"), N_("16000 Hz"),
N_("PCM U16 BE"), N_("PCM S16 BE"), N_("11025 Hz"), N_("8000 Hz"), N_("4000 Hz")
N_("PCM U24 LE"), N_("PCM S24 LE"),
N_("PCM U24 BE"), N_("PCM S24 BE"),
N_("PCM U32 LE"), N_("PCM S32 LE"),
N_("PCM U32 BE"), N_("PCM S32 BE"),
N_("PCM F32 LE"), N_("PCM F32 BE"),
N_("PCM F64 LE"), N_("PCM F64 BE")
}; };
vlc_module_begin() vlc_module_begin ()
set_shortname( N_("ALSA") ) set_shortname (N_("ALSA"))
set_description( N_("ALSA audio capture input") ) set_description (N_("ALSA audio capture"))
set_category( CAT_INPUT ) set_capability ("access_demux", 0)
set_subcategory( SUBCAT_INPUT_ACCESS ) set_category (CAT_INPUT)
set_help( HELP_TEXT ) set_subcategory (SUBCAT_INPUT_ACCESS)
set_help (HELP_TEXT)
add_shortcut( "alsa" )
set_capability( "access_demux", 10 ) add_obsolete_string ("alsa-format") /* since 2.1.0 */
set_callbacks( DemuxOpen, DemuxClose ) add_bool ("alsa-stereo", true, STEREO_TEXT, STEREO_TEXT, true)
add_integer ("alsa-samplerate", 48000, RATE_TEXT, RATE_TEXT, true)
add_bool( CFG_PREFIX "stereo", true, STEREO_TEXT, STEREO_LONGTEXT, change_integer_list (rate_values, rate_names)
true )
add_string( CFG_PREFIX "format", "s16l", FORMAT_TEXT, add_shortcut ("alsa")
FORMAT_LONGTEXT, true ) set_callbacks (Open, Close)
change_string_list( ppsz_fourcc, ppsz_fourcc_text, 0 ) vlc_module_end ()
add_integer( CFG_PREFIX "samplerate", 48000, SAMPLERATE_TEXT,
SAMPLERATE_LONGTEXT, true ) /** Helper for ALSA -> VLC debugging output */
vlc_module_end() /** XXX: duplicated from ALSA output */
static void Dump (vlc_object_t *obj, const char *msg,
/***************************************************************************** int (*cb)(void *, snd_output_t *), void *p)
* 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 *, const char * );
static bool ProbeAudioDevAlsa( demux_t *, const char * );
static char *ListAvailableDevices( demux_t *, bool b_probe );
struct demux_sys_t
{ {
/* Audio */ snd_output_t *output;
unsigned int i_sample_rate; char *str;
bool b_stereo;
vlc_fourcc_t i_format;
size_t i_max_frame_size;
block_t *p_block;
es_out_id_t *p_es;
/* ALSA Audio */
snd_pcm_t *p_alsa_pcm;
size_t i_alsa_frame_size;
int i_alsa_chunk_size;
int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
};
static int FindMainDevice( demux_t *p_demux, const char *psz_device ) if (unlikely(snd_output_buffer_open (&output)))
{ return;
if( psz_device )
{ int val = cb (p, output);
msg_Dbg( p_demux, "opening device '%s'", psz_device ); if (val)
if( ProbeAudioDevAlsa( p_demux, psz_device ) )
{
msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
OpenAudioDev( p_demux, psz_device );
}
}
else if( ProbeAudioDevAlsa( p_demux, ALSA_DEFAULT ) )
{
msg_Dbg( p_demux, "'%s' is an audio device", ALSA_DEFAULT );
OpenAudioDev( p_demux, ALSA_DEFAULT );
}
else if( ( psz_device = ListAvailableDevices( p_demux, true ) ) )
{ {
msg_Dbg( p_demux, "'%s' is an audio device", psz_device ); msg_Warn (obj, "cannot get info: %s", snd_strerror (val));
OpenAudioDev( p_demux, psz_device ); return;
free( (char *)psz_device );
} }
if( p_demux->p_sys->p_alsa_pcm == NULL ) size_t len = snd_output_buffer_string (output, &str);
return VLC_EGENERIC; if (len > 0 && str[len - 1])
return VLC_SUCCESS; len--; /* strip trailing newline */
msg_Dbg (obj, "%s%.*s", msg, (int)len, str);
snd_output_close (output);
} }
#define Dump(o, m, cb, p) \
Dump(VLC_OBJECT(o), m, (int (*)(void *, snd_output_t *))(cb), p)
static char *ListAvailableDevices( demux_t *p_demux, bool b_probe ) static void DumpDevice (vlc_object_t *obj, snd_pcm_t *pcm)
{ {
snd_ctl_card_info_t *p_info = NULL; snd_pcm_info_t *info;
snd_ctl_card_info_alloca( &p_info );
snd_pcm_info_t *p_pcminfo = NULL;
snd_pcm_info_alloca( &p_pcminfo );
if( !b_probe ) Dump (obj, " ", snd_pcm_dump, pcm);
msg_Dbg( p_demux, "Available alsa capture devices:" ); snd_pcm_info_alloca (&info);
int i_card = -1; if (snd_pcm_info (pcm, info) == 0)
while( !snd_card_next( &i_card ) && i_card >= 0 )
{ {
char psz_devname[10]; msg_Dbg (obj, " device name : %s", snd_pcm_info_get_name (info));
snprintf( psz_devname, 10, "hw:%d", i_card ); msg_Dbg (obj, " device ID : %s", snd_pcm_info_get_id (info));
msg_Dbg (obj, " subdevice name: %s",
snd_ctl_t *p_ctl = NULL; snd_pcm_info_get_subdevice_name (info));
if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
snd_ctl_card_info( p_ctl, p_info );
if( !b_probe )
msg_Dbg( p_demux, " %s (%s)",
snd_ctl_card_info_get_id( p_info ),
snd_ctl_card_info_get_name( p_info ) );
int i_dev = -1;
while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
{
snd_pcm_info_set_device( p_pcminfo, i_dev );
snd_pcm_info_set_subdevice( p_pcminfo, 0 );
snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
if( !b_probe )
msg_Dbg( p_demux, " hw:%d,%d : %s (%s)", i_card, i_dev,
snd_pcm_info_get_id( p_pcminfo ),
snd_pcm_info_get_name( p_pcminfo ) );
else
{
char *psz_device;
if( asprintf( &psz_device, "hw:%d,%d", i_card, i_dev ) > 0 )
{
if( ProbeAudioDevAlsa( p_demux, psz_device ) )
{
snd_ctl_close( p_ctl );
return psz_device;
}
else
free( psz_device );
}
}
}
snd_ctl_close( p_ctl );
} }
return NULL;
} }
/***************************************************************************** static void DumpDeviceStatus (vlc_object_t *obj, snd_pcm_t *pcm)
* DemuxOpen: opens alsa device, access_demux callback
*****************************************************************************
*
* url: <alsa device>::::
*
*****************************************************************************/
static int DemuxOpen( vlc_object_t *p_this )
{ {
demux_t *p_demux = (demux_t*)p_this; snd_pcm_status_t *status;
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;
p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
if( p_sys == NULL ) return VLC_ENOMEM;
p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
p_sys->p_es = NULL;
p_sys->p_block = NULL;
p_sys->i_next_demux_date = -1;
char *psz_format = var_InheritString( p_demux, CFG_PREFIX "format" );
p_sys->i_format = vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_format );
free( psz_format );
const char *psz_device = NULL;
if( p_demux->psz_location && *p_demux->psz_location )
psz_device = p_demux->psz_location;
else
ListAvailableDevices( p_demux, false );
if( FindMainDevice( p_demux, psz_device ) != VLC_SUCCESS )
{
if( p_demux->psz_location && *p_demux->psz_location )
ListAvailableDevices( p_demux, false );
DemuxClose( p_this );
return VLC_EGENERIC;
}
return VLC_SUCCESS; snd_pcm_status_alloca (&status);
snd_pcm_status (pcm, status);
Dump (obj, "current status:\n", snd_pcm_status_dump, status);
} }
#define DumpDeviceStatus(o, p) DumpDeviceStatus(VLC_OBJECT(o), p)
/*****************************************************************************
* 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;
if( p_sys->p_alsa_pcm ) struct demux_sys_t
{
snd_pcm_close( p_sys->p_alsa_pcm );
}
if( p_sys->p_block ) block_Release( p_sys->p_block );
free( p_sys );
}
/*****************************************************************************
* DemuxControl:
*****************************************************************************/
static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
{ {
demux_sys_t *p_sys = p_demux->p_sys; snd_pcm_t *pcm;
es_out_id_t *es;
switch( i_query ) vlc_thread_t thread;
{
/* Special for access_demux */
case DEMUX_CAN_PAUSE:
case DEMUX_CAN_SEEK:
case DEMUX_SET_PAUSE_STATE:
case DEMUX_CAN_CONTROL_PACE:
*va_arg( args, bool * ) = false;
return VLC_SUCCESS;
case DEMUX_GET_PTS_DELAY:
*va_arg( args, int64_t * ) =
INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
return VLC_SUCCESS;
case DEMUX_GET_TIME:
*va_arg( args, int64_t * ) = mdate();
return VLC_SUCCESS;
case DEMUX_SET_NEXT_DEMUX_TIME:
p_sys->i_next_demux_date = va_arg( args, int64_t );
return VLC_SUCCESS;
/* TODO implement others */ mtime_t caching;
default: snd_pcm_uframes_t period_size;
return VLC_EGENERIC; unsigned rate;
} };
return VLC_EGENERIC;
}
/***************************************************************************** static void Poll (snd_pcm_t *pcm, int canc)
* Demux: Processes the audio frame
*****************************************************************************/
static int Demux( demux_t *p_demux )
{ {
demux_sys_t *p_sys = p_demux->p_sys; int n = snd_pcm_poll_descriptors_count (pcm);
struct pollfd ufd[n];
block_t *p_block = NULL; unsigned short revents;
snd_pcm_poll_descriptors (pcm, ufd, n);
do do
{ {
p_block = GrabAudio( p_demux ); vlc_restorecancel (canc);
if( p_block ) poll (ufd, n, -1);
{ canc = vlc_savecancel ();
es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts ); snd_pcm_poll_descriptors_revents (pcm, ufd, n, &revents);
es_out_Send( p_demux->out, p_sys->p_es, p_block ); }
p_block = NULL; while (!revents);
}
} 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 );
return 1;
} }
/***************************************************************************** static void *Thread (void *data)
* GrabAudio: Grab an audio frame
*****************************************************************************/
static block_t* GrabAudio( demux_t *p_demux )
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_t *demux = data;
int i_read, i_correct; demux_sys_t *sys = demux->p_sys;
block_t *p_block; snd_pcm_t *pcm = sys->pcm;
size_t bytes;
if( p_sys->p_block ) p_block = p_sys->p_block; int canc, val;
else p_block = block_New( p_demux, p_sys->i_max_frame_size );
canc = vlc_savecancel ();
if( !p_block ) bytes = snd_pcm_frames_to_bytes (pcm, sys->period_size);
val = snd_pcm_start (pcm);
if (val)
{ {
msg_Warn( p_demux, "cannot get block" ); msg_Err (demux, "cannot prepare device: %s", snd_strerror (val));
return NULL; return NULL;
} }
p_sys->p_block = p_block; for (;;)
/* ALSA */
i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer,
p_sys->i_alsa_chunk_size );
if( i_read == -EAGAIN )
{ {
snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */ block_t *block = block_Alloc (bytes);
return NULL; if (unlikely(block == NULL))
} break;
if( i_read < 0 ) /* Wait for data */
i_read = snd_pcm_recover( p_sys->p_alsa_pcm, i_read, 0 ); Poll (pcm, canc);
if( i_read <= 0 ) /* Read data */
{ snd_pcm_sframes_t frames, delay;
switch( i_read ) mtime_t pts;
frames = snd_pcm_readi (pcm, block->p_buffer, sys->period_size);
pts = mdate ();
if (frames < 0)
{ {
case 0: /* state recovered or no data */ if (frames == -EAGAIN)
return NULL; continue;
case -EAGAIN:
snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */ val = snd_pcm_recover (pcm, frames, 1);
return NULL; if (val == 0)
default: {
msg_Err( p_demux, "Failed to read alsa frame (%s)", msg_Warn (demux, "cannot read samples: %s",
snd_strerror( i_read ) ); snd_strerror (frames));
return NULL; continue;
}
msg_Err (demux, "cannot recover record stream: %s",
snd_strerror (val));
DumpDeviceStatus (demux, pcm);
break;
} }
}
/* convert from frames to bytes */ /* Compute time stamp */
i_read *= p_sys->i_alsa_frame_size; if (snd_pcm_delay (pcm, &delay))
delay = 0;
delay += frames;
pts -= (CLOCK_FREQ * delay) / sys->rate;
p_block->i_buffer = i_read; block->i_buffer = snd_pcm_frames_to_bytes (pcm, frames);
p_sys->p_block = 0; block->i_nb_samples = frames;
block->i_pts = pts;
block->i_length = (CLOCK_FREQ * frames) / sys->rate;
/* Correct the date because of kernel buffering */ es_out_Control (demux->out, ES_OUT_SET_PCR, block->i_pts);
i_correct = i_read; es_out_Send (demux->out, sys->es, block);
/* ALSA */
int i_err;
snd_pcm_sframes_t delay = 0;
if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
{
size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
/* Test for overrun */
if( i_correction_delta > p_sys->i_max_frame_size )
{
msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
i_correction_delta, p_sys->i_max_frame_size );
i_correction_delta = p_sys->i_max_frame_size;
snd_pcm_prepare( p_sys->p_alsa_pcm );
}
i_correct += i_correction_delta;
}
else
{
/* delay failed so reset */
msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
snd_pcm_prepare( p_sys->p_alsa_pcm );
} }
return NULL;
/* 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;
} }
static snd_pcm_format_t GetAlsaPCMFormat( demux_t *p_demux, const vlc_fourcc_t i_format ) static int Control (demux_t *demux, int query, va_list ap)
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_sys_t *sys = demux->p_sys;
switch( i_format ) switch (query)
{ {
case VLC_CODEC_U8: return SND_PCM_FORMAT_U8; case DEMUX_GET_TIME:
case VLC_CODEC_S8: return SND_PCM_FORMAT_S8; *va_arg (ap, int64_t *) = mdate();
break;
case VLC_CODEC_GSM: return SND_PCM_FORMAT_GSM;
case VLC_CODEC_U16L: return SND_PCM_FORMAT_U16_LE;
case VLC_CODEC_S16L: return SND_PCM_FORMAT_S16_LE;
case VLC_CODEC_U16B: return SND_PCM_FORMAT_U16_BE;
case VLC_CODEC_S16B: return SND_PCM_FORMAT_S16_BE;
case VLC_CODEC_U24L: return SND_PCM_FORMAT_U24_3LE; case DEMUX_GET_PTS_DELAY:
case VLC_CODEC_S24L: return SND_PCM_FORMAT_S24_3LE; *va_arg (ap, int64_t *) = sys->caching;
case VLC_CODEC_U24B: return SND_PCM_FORMAT_U24_3BE; break;
case VLC_CODEC_S24B: return SND_PCM_FORMAT_S24_3BE;
case VLC_CODEC_U32L: return SND_PCM_FORMAT_U32_LE; //case DEMUX_SET_NEXT_DEMUX_TIME: still needed?
case VLC_CODEC_U32B: return SND_PCM_FORMAT_U32_BE;
case VLC_CODEC_S32L: return SND_PCM_FORMAT_S32_LE;
case VLC_CODEC_S32B: return SND_PCM_FORMAT_S32_BE;
case VLC_CODEC_F32L: return SND_PCM_FORMAT_FLOAT_LE;
case VLC_CODEC_F32B: return SND_PCM_FORMAT_FLOAT_BE;
case VLC_CODEC_F64L: return SND_PCM_FORMAT_FLOAT64_LE; case DEMUX_HAS_UNSUPPORTED_META:
case VLC_CODEC_F64B: return SND_PCM_FORMAT_FLOAT64_BE; case DEMUX_CAN_RECORD:
case DEMUX_CAN_PAUSE:
case DEMUX_CAN_CONTROL_PACE:
case DEMUX_CAN_CONTROL_RATE:
case DEMUX_CAN_SEEK:
*va_arg (ap, bool *) = false;
break;;
default: default:
msg_Err( p_demux, "ALSA: unsupported sample format '%s' falling back to 's16l'", return VLC_EGENERIC;
(const char *)&i_format );
p_sys->i_format = VLC_CODEC_S16L;
} }
return SND_PCM_FORMAT_S16_LE; return VLC_SUCCESS;
} }
/***************************************************************************** static const vlc_fourcc_t formats[] = {
* OpenAudioDev: open and set up the audio device and probe for capabilities [SND_PCM_FORMAT_S8] = VLC_CODEC_S8,
*****************************************************************************/ [SND_PCM_FORMAT_U8] = VLC_CODEC_U8,
static int OpenAudioDevAlsa( demux_t *p_demux, const char *psz_device ) [SND_PCM_FORMAT_S16_LE] = VLC_CODEC_S16L,
[SND_PCM_FORMAT_S16_BE] = VLC_CODEC_S16B,
[SND_PCM_FORMAT_U16_LE] = VLC_CODEC_U16L,
[SND_PCM_FORMAT_U16_BE] = VLC_CODEC_U16B,
//[SND_PCM_FORMAT_S24_LE] = VLC_CODEC_?,
//[SND_PCM_FORMAT_S24_BE] = VLC_CODEC_?,
[SND_PCM_FORMAT_U24_LE] = VLC_CODEC_U32L, // TODO: replay gain
[SND_PCM_FORMAT_U24_BE] = VLC_CODEC_U32B, // ^
[SND_PCM_FORMAT_S32_LE] = VLC_CODEC_S32L,
[SND_PCM_FORMAT_S32_BE] = VLC_CODEC_S32B,
[SND_PCM_FORMAT_U32_LE] = VLC_CODEC_U32L,
[SND_PCM_FORMAT_U32_BE] = VLC_CODEC_U32B,
[SND_PCM_FORMAT_FLOAT_LE] = VLC_CODEC_F32L,
[SND_PCM_FORMAT_FLOAT_BE] = VLC_CODEC_F32B,
[SND_PCM_FORMAT_FLOAT64_LE] = VLC_CODEC_F32L,
[SND_PCM_FORMAT_FLOAT64_BE] = VLC_CODEC_F32B,
//[SND_PCM_FORMAT_IEC958_SUBFRAME_LE] = VLC_CODEC_SPDIFL,
//[SND_PCM_FORMAT_IEC958_SUBFRAME_BE] = VLC_CODEC_SPDIFB,
[SND_PCM_FORMAT_MU_LAW] = VLC_CODEC_MULAW,
[SND_PCM_FORMAT_A_LAW] = VLC_CODEC_ALAW,
//[SND_PCM_FORMAT_IMA_ADPCM] = VLC_CODEC_ADPCM_?, // XXX: which one?
[SND_PCM_FORMAT_MPEG] = VLC_CODEC_MPGA,
[SND_PCM_FORMAT_GSM] = VLC_CODEC_GSM,
//[SND_PCM_FORMAT_SPECIAL] = VLC_CODEC_?
[SND_PCM_FORMAT_S24_3LE] = VLC_CODEC_S24L,
[SND_PCM_FORMAT_S24_3BE] = VLC_CODEC_S24B,
[SND_PCM_FORMAT_U24_3LE] = VLC_CODEC_U24L,
[SND_PCM_FORMAT_U24_3BE] = VLC_CODEC_U24B,
[SND_PCM_FORMAT_S20_3LE] = VLC_CODEC_S24L, // TODO: replay gain
[SND_PCM_FORMAT_S20_3BE] = VLC_CODEC_S24B, // ^
[SND_PCM_FORMAT_U20_3LE] = VLC_CODEC_U24L, // ^
[SND_PCM_FORMAT_U20_3BE] = VLC_CODEC_U24B, // ^
[SND_PCM_FORMAT_S18_3LE] = VLC_CODEC_S24L, // ^
[SND_PCM_FORMAT_S18_3BE] = VLC_CODEC_S24B, // ^
[SND_PCM_FORMAT_U18_3LE] = VLC_CODEC_U24L, // ^
[SND_PCM_FORMAT_U18_3BE] = VLC_CODEC_U24B, // ^
};
#ifdef WORDS_BIGENDIAN
# define C(f) f##BE, f##LE
#else
# define C(f) f##LE, f##BE
#endif
/* Formats in order of decreasing preference */
static const uint8_t choices[] = {
C(SND_PCM_FORMAT_FLOAT_),
C(SND_PCM_FORMAT_S32_),
C(SND_PCM_FORMAT_U32_),
C(SND_PCM_FORMAT_S16_),
C(SND_PCM_FORMAT_U16_),
C(SND_PCM_FORMAT_FLOAT64_),
C(SND_PCM_FORMAT_S24_3),
C(SND_PCM_FORMAT_U24_3),
SND_PCM_FORMAT_MPEG,
SND_PCM_FORMAT_GSM,
SND_PCM_FORMAT_MU_LAW,
SND_PCM_FORMAT_A_LAW,
SND_PCM_FORMAT_S8,
SND_PCM_FORMAT_U8,
};
static uint16_t channel_maps[] = {
AOUT_CHAN_CENTER, AOUT_CHANS_2_0, AOUT_CHANS_3_0 /* ? */,
AOUT_CHANS_4_0, AOUT_CHANS_5_0 /* ? */, AOUT_CHANS_5_1,
/* TODO: support 7-8 channels - need channels reodering */
};
static int Open (vlc_object_t *obj)
{ {
demux_sys_t *p_sys = p_demux->p_sys; demux_t *demux = (demux_t *)obj;
p_sys->p_alsa_pcm = NULL; demux_sys_t *sys = malloc (sizeof (*sys));
snd_pcm_hw_params_t *p_hw_params = NULL;
snd_pcm_format_t i_alsa_pcm_format; static_assert (sizeof (formats) / sizeof (formats[0]) ==
snd_pcm_uframes_t buffer_size; SND_PCM_FORMAT_LAST + 1, "unknown formats");
snd_pcm_uframes_t chunk_size;
if (unlikely(sys == NULL))
/* ALSA */ return VLC_ENOMEM;
int i_err;
/* Open the device */
if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device, const char *device = demux->psz_location;
SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0) if (device == NULL || !device[0])
device = "default";
const int mode = SND_PCM_NONBLOCK
/*| SND_PCM_NO_AUTO_RESAMPLE*/
| SND_PCM_NO_AUTO_CHANNELS
/*| SND_PCM_NO_AUTO_FORMAT*/;
snd_pcm_t *pcm;
int val = snd_pcm_open (&pcm, device, SND_PCM_STREAM_CAPTURE, mode);
if (val != 0)
{ {
msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)", msg_Err (demux, "cannot open ALSA device \"%s\": %s", device,
psz_device, snd_strerror( i_err ) ); snd_strerror (val));
goto adev_fail; free (sys);
return VLC_EGENERIC;
} }
sys->pcm = pcm;
msg_Dbg (demux, "using ALSA device: %s", device);
DumpDevice (VLC_OBJECT(demux), pcm);
if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0) /* Negotiate capture parameters */
{ snd_pcm_hw_params_t *hw;
msg_Err( p_demux, "Cannot set ALSA nonblock (%s)", es_format_t fmt;
snd_strerror( i_err ) ); unsigned param;
goto adev_fail; int dir;
}
/* Begin setting hardware parameters */ snd_pcm_hw_params_alloca (&hw);
snd_pcm_hw_params_any (pcm, hw);
Dump (demux, "initial hardware setup:\n", snd_pcm_hw_params_dump, hw);
if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 ) val = snd_pcm_hw_params_set_rate_resample (pcm, hw, 0);
if (val)
{ {
msg_Err( p_demux, msg_Err (demux, "cannot disable resampling: %s", snd_strerror (val));
"ALSA: cannot allocate hardware parameter structure (%s)", goto error;
snd_strerror( i_err ) );
goto adev_fail;
} }
if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 ) val = snd_pcm_hw_params_set_access (pcm, hw,
SND_PCM_ACCESS_RW_INTERLEAVED);
if (val)
{ {
msg_Err( p_demux, msg_Err (demux, "cannot set access mode: %s", snd_strerror (val));
"ALSA: cannot initialize hardware parameter structure (%s)", goto error;
snd_strerror( i_err ) );
goto adev_fail;
} }
/* Set Interleaved access */ snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;
if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, for (size_t i = 0; i < sizeof (choices) / sizeof (choices[0]); i++)
SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 ) if (snd_pcm_hw_params_test_format (pcm, hw, choices[i]) == 0)
{
val = snd_pcm_hw_params_set_format (pcm, hw, choices[i]);
if (val)
{
msg_Err (demux, "cannot set sample format: %s",
snd_strerror (val));
goto error;
}
format = choices[i];
break;
}
if (format == SND_PCM_FORMAT_UNKNOWN)
{ {
msg_Err( p_demux, "ALSA: cannot set access type (%s)", msg_Err (demux, "no supported sample format");
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
/* Set capture format, default is signed 16 bit little endian */ assert ((size_t)format < (sizeof (formats) / sizeof (formats[0])));
i_alsa_pcm_format = GetAlsaPCMFormat( p_demux, p_sys->i_format ); es_format_Init (&fmt, AUDIO_ES, formats[format]);
if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, fmt.audio.i_format = fmt.i_codec;
i_alsa_pcm_format ) ) < 0 )
param = 1 + var_InheritBool (demux, "alsa-stereo");
val = snd_pcm_hw_params_set_channels_max (pcm, hw, &param);
if (val)
{ {
msg_Err( p_demux, "ALSA: cannot set sample format (%s)", msg_Err (demux, "cannot restrict channels count: %s",
snd_strerror( i_err ) ); snd_strerror (val));
goto adev_fail; goto error;
} }
val = snd_pcm_hw_params_set_channels_last (pcm, hw, &param);
/* Set sample rate */ if (val)
i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params,
&p_sys->i_sample_rate, NULL );
if( i_err < 0 )
{ {
msg_Err( p_demux, "ALSA: cannot set sample rate (%s)", msg_Err (demux, "cannot set channels count: %s", snd_strerror (val));
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
assert (param > 0);
/* Set channels */ assert (param < (sizeof (channel_maps) / sizeof (channel_maps[0])));
unsigned int channels = p_sys->b_stereo ? 2 : 1; fmt.audio.i_channels = param;
if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, fmt.audio.i_original_channels =
channels ) ) < 0 ) fmt.audio.i_physical_channels = channel_maps[param - 1];
param = var_InheritInteger (demux, "alsa-samplerate");
val = snd_pcm_hw_params_set_rate_max (pcm, hw, &param, NULL);
if (val)
{ {
channels = ( channels==1 ) ? 2 : 1; msg_Err (demux, "cannot restrict rate to %u Hz or less: %s", 192000,
msg_Warn( p_demux, "ALSA: cannot set channel count (%s). " snd_strerror (val));
"Trying with channels=%d", goto error;
snd_strerror( i_err ),
channels );
if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params,
channels ) ) < 0 )
{
msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
snd_strerror( i_err ) );
goto adev_fail;
}
p_sys->b_stereo = ( channels == 2 );
} }
val = snd_pcm_hw_params_set_rate_last (pcm, hw, &param, &dir);
/* Set metrics for buffer calculations later */ if (val)
unsigned int buffer_time;
if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
{ {
msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)", msg_Err (demux, "cannot set sample rate: %s", snd_strerror (val));
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
if( buffer_time > 500000 ) buffer_time = 500000; if (dir)
msg_Warn (demux, "sample rate is not integral");
/* Set period time */ fmt.audio.i_rate = param;
unsigned int period_time = buffer_time / 4; sys->rate = param;
i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params,
&period_time, 0 ); sys->caching = INT64_C(1000) * var_InheritInteger (demux, "live-caching");
if( i_err < 0 ) param = sys->caching;
val = snd_pcm_hw_params_set_buffer_time_near (pcm, hw, &param, NULL);
if (val)
{ {
msg_Err( p_demux, "ALSA: cannot set period time (%s)", msg_Err (demux, "cannot set buffer duration: %s", snd_strerror (val));
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
/* Set buffer time */ param /= 4;
i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, val = snd_pcm_hw_params_set_period_time_near (pcm, hw, &param, NULL);
&buffer_time, 0 ); if (val)
if( i_err < 0 )
{ {
msg_Err( p_demux, "ALSA: cannot set buffer time (%s)", msg_Err (demux, "cannot set period: %s", snd_strerror (val));
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
/* Apply new hardware parameters */ val = snd_pcm_hw_params_get_period_size (hw, &sys->period_size, &dir);
if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 ) if (val)
{ {
msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)", msg_Err (demux, "cannot get period size: %s", snd_strerror (val));
snd_strerror( i_err ) ); goto error;
goto adev_fail;
} }
if (dir > 0)
sys->period_size++;
/* Get various buffer metrics */ /* Commit hardware parameters */
snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 ); val = snd_pcm_hw_params (pcm, hw);
snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size ); if (val)
if( chunk_size == buffer_size )
{ {
msg_Err( p_demux, msg_Err (demux, "cannot commit hardware parameters: %s",
"ALSA: period cannot equal buffer size (%lu == %lu)", snd_strerror (val));
chunk_size, buffer_size); goto error;
goto adev_fail;
} }
Dump (demux, "final HW setup:\n", snd_pcm_hw_params_dump, hw);
int bits_per_sample = snd_pcm_format_physical_width(i_alsa_pcm_format); /* Kick recording */
int bits_per_frame = bits_per_sample * channels; aout_FormatPrepare (&fmt.audio);
sys->es = es_out_Add (demux->out, &fmt);
p_sys->i_alsa_chunk_size = chunk_size;
p_sys->i_alsa_frame_size = bits_per_frame / 8;
p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
snd_pcm_hw_params_free( p_hw_params ); if (vlc_clone (&sys->thread, Thread, demux, VLC_THREAD_PRIORITY_INPUT))
p_hw_params = NULL;
/* Prep device */
if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
{ {
msg_Err( p_demux, es_out_Del (demux->out, sys->es);
"ALSA: cannot prepare audio interface for use (%s)", goto error;
snd_strerror( i_err ) );
goto adev_fail;
} }
snd_pcm_start( p_sys->p_alsa_pcm ); demux->p_sys = sys;
demux->pf_demux = NULL;
demux->pf_control = Control;
return VLC_SUCCESS; return VLC_SUCCESS;
error:
adev_fail: snd_pcm_close (pcm);
free (sys);
if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
p_sys->p_alsa_pcm = NULL;
return VLC_EGENERIC; return VLC_EGENERIC;
} }
static int OpenAudioDev( demux_t *p_demux, const char *psz_device ) static void Close (vlc_object_t *obj)
{
demux_sys_t *p_sys = p_demux->p_sys;
if( OpenAudioDevAlsa( p_demux, psz_device ) != VLC_SUCCESS )
return VLC_EGENERIC;
msg_Dbg( p_demux, "opened adev=`%s' %s %dHz codec '%s'",
psz_device, p_sys->b_stereo ? "stereo" : "mono",
p_sys->i_sample_rate,
vlc_fourcc_GetDescription( AUDIO_ES, p_sys->i_format ) );
es_format_t fmt;
es_format_Init( &fmt, AUDIO_ES, p_sys->i_format );
fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
fmt.audio.i_rate = p_sys->i_sample_rate;
fmt.audio.i_bitspersample = aout_BitsPerSample( p_sys->i_format );
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 );
p_sys->p_es = es_out_Add( p_demux->out, &fmt );
return VLC_SUCCESS;
}
/*****************************************************************************
* ProbeAudioDevAlsa: probe audio for capabilities
*****************************************************************************/
static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
{ {
int i_err; demux_t *demux = (demux_t *)obj;
snd_pcm_t *p_alsa_pcm; demux_sys_t *sys = demux->p_sys;
if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
{
msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
return false;
}
snd_pcm_close( p_alsa_pcm ); vlc_cancel (sys->thread);
vlc_join (sys->thread, NULL);
return true; snd_pcm_close (sys->pcm);
free (sys);
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment