1 /*****************************************************************************
2 * oss.c : OSS input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
9 * Antoine Cellerier <dionoea at videolan d.t org>
10 * Dennis Lou <dlou99 at yahoo dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_access.h>
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
44 #include <sys/ioctl.h>
47 #ifdef HAVE_SYS_SOUNDCARD_H
48 # include <sys/soundcard.h>
50 #ifdef HAVE_SOUNDCARD_H
51 # include <soundcard.h>
56 /*****************************************************************************
58 *****************************************************************************/
60 static int DemuxOpen ( vlc_object_t * );
61 static void DemuxClose( vlc_object_t * );
64 #define STEREO_TEXT N_( "Stereo" )
65 #define STEREO_LONGTEXT N_( \
66 "Capture the audio stream in stereo." )
67 #define SAMPLERATE_TEXT N_( "Samplerate" )
68 #define SAMPLERATE_LONGTEXT N_( \
69 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
71 #define CACHING_TEXT N_("Caching value in ms")
72 #define CACHING_LONGTEXT N_( \
73 "Caching value for OSS captures. This " \
74 "value should be set in milliseconds." )
76 #define OSS_DEFAULT "/dev/dsp"
78 #define CFG_PREFIX "oss-"
81 set_shortname( N_("OSS") )
82 set_description( N_("OSS input") )
83 set_category( CAT_INPUT )
84 set_subcategory( SUBCAT_INPUT_ACCESS )
87 set_capability( "access_demux", 10 )
88 set_callbacks( DemuxOpen, DemuxClose )
90 add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
92 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
93 SAMPLERATE_LONGTEXT, true )
94 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
95 CACHING_TEXT, CACHING_LONGTEXT, true )
98 /*****************************************************************************
99 * Access: local prototypes
100 *****************************************************************************/
102 static int DemuxControl( demux_t *, int, va_list );
104 static int Demux( demux_t * );
106 static block_t* GrabAudio( demux_t *p_demux );
108 static int OpenAudioDev( demux_t * );
109 static int OpenAudioDevOss( demux_t * );
110 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
120 const char *psz_device; /* OSS device from MRL */
126 unsigned int i_sample_rate;
128 size_t i_max_frame_size;
132 int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
135 static int FindMainDevice( demux_t *p_demux )
137 msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
138 if( ProbeAudioDevOss( p_demux, p_demux->p_sys->psz_device ) )
140 msg_Dbg( p_demux, "'%s' is an audio device", p_demux->p_sys->psz_device );
141 p_demux->p_sys->i_fd = OpenAudioDev( p_demux );
144 if( p_demux->p_sys->i_fd < 0 )
149 /*****************************************************************************
150 * DemuxOpen: opens oss device, access_demux callback
151 *****************************************************************************
153 * url: <oss device>::::
155 *****************************************************************************/
156 static int DemuxOpen( vlc_object_t *p_this )
158 demux_t *p_demux = (demux_t*)p_this;
161 /* Only when selected */
162 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
165 p_demux->pf_control = DemuxControl;
166 p_demux->pf_demux = Demux;
167 p_demux->info.i_update = 0;
168 p_demux->info.i_title = 0;
169 p_demux->info.i_seekpoint = 0;
171 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
172 if( p_sys == NULL ) return VLC_ENOMEM;
174 p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
175 p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
176 p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
179 p_sys->p_block = NULL;
180 p_sys->i_next_demux_date = -1;
182 if( p_demux->psz_path && *p_demux->psz_path )
183 p_sys->psz_device = p_demux->psz_path;
185 p_sys->psz_device = OSS_DEFAULT;
187 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
189 DemuxClose( p_this );
196 /*****************************************************************************
197 * Close: close device, free resources
198 *****************************************************************************/
199 static void DemuxClose( vlc_object_t *p_this )
201 demux_t *p_demux = (demux_t *)p_this;
202 demux_sys_t *p_sys = p_demux->p_sys;
204 if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
206 if( p_sys->p_block ) block_Release( p_sys->p_block );
210 /*****************************************************************************
212 *****************************************************************************/
213 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
215 demux_sys_t *p_sys = p_demux->p_sys;
221 /* Special for access_demux */
222 case DEMUX_CAN_PAUSE:
224 case DEMUX_CAN_CONTROL_PACE:
225 pb = (bool*)va_arg( args, bool * );
229 case DEMUX_GET_PTS_DELAY:
230 pi64 = (int64_t*)va_arg( args, int64_t * );
231 *pi64 = (int64_t)p_sys->i_cache * 1000;
235 pi64 = (int64_t*)va_arg( args, int64_t * );
239 case DEMUX_SET_NEXT_DEMUX_TIME:
240 p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
243 /* TODO implement others */
251 /*****************************************************************************
252 * Demux: Processes the audio frame
253 *****************************************************************************/
254 static int Demux( demux_t *p_demux )
256 demux_sys_t *p_sys = p_demux->p_sys;
260 fd.events = POLLIN|POLLPRI;
263 block_t *p_block = NULL;
269 es_out_Send( p_demux->out, p_sys->p_es, p_block );
274 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. */
276 if( fd.revents & (POLLIN|POLLPRI) )
278 p_block = GrabAudio( p_demux );
280 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
283 } while( p_block && p_sys->i_next_demux_date > 0 &&
284 p_block->i_pts < p_sys->i_next_demux_date );
287 es_out_Send( p_demux->out, p_sys->p_es, p_block );
292 /*****************************************************************************
293 * GrabAudio: Grab an audio frame
294 *****************************************************************************/
295 static block_t* GrabAudio( demux_t *p_demux )
297 demux_sys_t *p_sys = p_demux->p_sys;
298 struct audio_buf_info buf_info;
299 int i_read = 0, i_correct;
302 if( p_sys->p_block ) p_block = p_sys->p_block;
303 else p_block = block_New( p_demux, p_sys->i_max_frame_size );
307 msg_Warn( p_demux, "cannot get block" );
311 p_sys->p_block = p_block;
313 i_read = read( p_sys->i_fd, p_block->p_buffer,
314 p_sys->i_max_frame_size );
316 if( i_read <= 0 ) return 0;
318 p_block->i_buffer = i_read;
321 /* Correct the date because of kernel buffering */
323 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
325 i_correct += buf_info.bytes;
329 p_block->i_pts = p_block->i_dts =
330 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
331 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
336 /*****************************************************************************
337 * OpenAudioDev: open and set up the audio device and probe for capabilities
338 *****************************************************************************/
339 static int OpenAudioDevOss( demux_t *p_demux )
344 i_fd = utf8_open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
348 msg_Err( p_demux, "cannot open OSS audio device (%m)" );
352 i_format = AFMT_S16_LE;
353 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
354 || i_format != AFMT_S16_LE )
357 "cannot set audio format (16b little endian) (%m)" );
361 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
362 &p_demux->p_sys->b_stereo ) < 0 )
364 msg_Err( p_demux, "cannot set audio channels count (%m)" );
368 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
369 &p_demux->p_sys->i_sample_rate ) < 0 )
371 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
375 p_demux->p_sys->i_max_frame_size = 6 * 1024;
381 if( i_fd >= 0 ) close( i_fd );
385 static int OpenAudioDev( demux_t *p_demux )
387 demux_sys_t *p_sys = p_demux->p_sys;
388 int i_fd = OpenAudioDevOss( p_demux );
393 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
394 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
395 p_sys->i_sample_rate );
398 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
400 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
401 fmt.audio.i_rate = p_sys->i_sample_rate;
402 fmt.audio.i_bitspersample = 16;
403 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
404 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
406 msg_Dbg( p_demux, "new audio es %d channels %dHz",
407 fmt.audio.i_channels, fmt.audio.i_rate );
409 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
414 /*****************************************************************************
415 * ProbeAudioDev: probe audio for capabilities
416 *****************************************************************************/
417 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
420 int i_fd = utf8_open( psz_device, O_RDONLY | O_NONBLOCK );
424 msg_Err( p_demux, "cannot open device %s for OSS audio (%m)", psz_device );
428 /* this will fail if the device is video */
429 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
431 msg_Err( p_demux, "cannot get audio caps (%m)" );
435 if( i_fd >= 0 ) close( i_fd );
440 if( i_fd >= 0 ) close( i_fd );