input: add input_SetProgramId
[vlc.git] / modules / access / oss.c
blob40327232e200fcc9f953d46ad20634a9a10b0449
1 /*****************************************************************************
2 * oss.c : OSS input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
6 * Authors: Benjamin Pracht <bigben at videolan dot org>
7 * Richard Hosking <richard at hovis dot net>
8 * Antoine Cellerier <dionoea at videolan d.t org>
9 * Dennis Lou <dlou99 at yahoo dot com>
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
26 /*****************************************************************************
27 * Preamble
28 *****************************************************************************/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_access.h>
37 #include <vlc_demux.h>
38 #include <vlc_fs.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <sys/ioctl.h>
44 #include <sys/mman.h>
46 #ifdef HAVE_SYS_SOUNDCARD_H
47 # include <sys/soundcard.h>
48 #endif
49 #ifdef HAVE_SOUNDCARD_H
50 # include <soundcard.h>
51 #endif
53 #include <poll.h>
55 /*****************************************************************************
56 * Module descriptior
57 *****************************************************************************/
59 static int DemuxOpen ( vlc_object_t * );
60 static void DemuxClose( vlc_object_t * );
63 #define STEREO_TEXT N_( "Stereo" )
64 #define STEREO_LONGTEXT N_( \
65 "Capture the audio stream in stereo." )
66 #define SAMPLERATE_TEXT N_( "Samplerate" )
67 #define SAMPLERATE_LONGTEXT N_( \
68 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
70 #define OSS_DEFAULT "/dev/dsp"
72 #define CFG_PREFIX "oss-"
74 vlc_module_begin ()
75 set_shortname( N_("OSS") )
76 set_description( N_("OSS input") )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_ACCESS )
80 add_shortcut( "oss" )
81 set_capability( "access", 0 )
82 set_callbacks( DemuxOpen, DemuxClose )
84 add_bool( CFG_PREFIX "stereo", true, STEREO_TEXT, STEREO_LONGTEXT,
85 true )
86 add_integer( CFG_PREFIX "samplerate", 48000, SAMPLERATE_TEXT,
87 SAMPLERATE_LONGTEXT, true )
88 vlc_module_end ()
90 /*****************************************************************************
91 * Access: local prototypes
92 *****************************************************************************/
94 static int DemuxControl( demux_t *, int, va_list );
96 static int Demux( demux_t * );
98 static block_t* GrabAudio( demux_t *p_demux );
100 static int OpenAudioDev( demux_t * );
101 static int OpenAudioDevOss( demux_t * );
102 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
104 struct buffer_t
106 void * start;
107 size_t length;
110 typedef struct
112 const char *psz_device; /* OSS device from MRL */
114 int i_fd;
116 /* Audio */
117 unsigned int i_sample_rate;
118 bool b_stereo;
119 size_t i_max_frame_size;
120 block_t *p_block;
121 es_out_id_t *p_es;
123 vlc_tick_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
124 } demux_sys_t;
126 static int FindMainDevice( demux_t *p_demux )
128 demux_sys_t *p_sys = p_demux->p_sys;
130 msg_Dbg( p_demux, "opening device '%s'", p_sys->psz_device );
131 if( ProbeAudioDevOss( p_demux, p_sys->psz_device ) )
133 msg_Dbg( p_demux, "'%s' is an audio device", p_sys->psz_device );
134 p_sys->i_fd = OpenAudioDev( p_demux );
137 if( p_sys->i_fd < 0 )
138 return VLC_EGENERIC;
139 return VLC_SUCCESS;
142 /*****************************************************************************
143 * DemuxOpen: opens oss device, access_demux callback
144 *****************************************************************************
146 * url: <oss device>::::
148 *****************************************************************************/
149 static int DemuxOpen( vlc_object_t *p_this )
151 demux_t *p_demux = (demux_t*)p_this;
152 demux_sys_t *p_sys;
154 if (p_demux->out == NULL)
155 return VLC_EGENERIC;
157 /* Set up p_demux */
158 p_demux->pf_control = DemuxControl;
159 p_demux->pf_demux = Demux;
161 p_demux->p_sys = p_sys = vlc_obj_calloc( p_this, 1, sizeof( demux_sys_t ) );
162 if( p_sys == NULL ) return VLC_ENOMEM;
164 p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
165 p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
166 p_sys->i_fd = -1;
167 p_sys->p_es = NULL;
168 p_sys->p_block = NULL;
169 p_sys->i_next_demux_date = -1;
171 if( p_demux->psz_location && *p_demux->psz_location )
172 p_sys->psz_device = p_demux->psz_location;
173 else
174 p_sys->psz_device = OSS_DEFAULT;
176 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
178 DemuxClose( p_this );
179 return VLC_EGENERIC;
182 return VLC_SUCCESS;
185 /*****************************************************************************
186 * Close: close device, free resources
187 *****************************************************************************/
188 static void DemuxClose( vlc_object_t *p_this )
190 demux_t *p_demux = (demux_t *)p_this;
191 demux_sys_t *p_sys = p_demux->p_sys;
193 if( p_sys->i_fd >= 0 )
194 vlc_close( p_sys->i_fd );
196 if( p_sys->p_block ) block_Release( p_sys->p_block );
199 /*****************************************************************************
200 * DemuxControl:
201 *****************************************************************************/
202 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
204 demux_sys_t *p_sys = p_demux->p_sys;
205 bool *pb;
207 switch( i_query )
209 /* Special for access_demux */
210 case DEMUX_CAN_PAUSE:
211 case DEMUX_CAN_SEEK:
212 case DEMUX_CAN_CONTROL_PACE:
213 pb = va_arg( args, bool * );
214 *pb = false;
215 return VLC_SUCCESS;
217 case DEMUX_GET_PTS_DELAY:
218 *va_arg( args, vlc_tick_t * ) =
219 VLC_TICK_FROM_MS(var_InheritInteger( p_demux, "live-caching" ));
220 return VLC_SUCCESS;
222 case DEMUX_GET_TIME:
223 *va_arg( args, vlc_tick_t * ) = vlc_tick_now();
224 return VLC_SUCCESS;
226 case DEMUX_SET_NEXT_DEMUX_TIME:
227 p_sys->i_next_demux_date = va_arg( args, vlc_tick_t );
228 return VLC_SUCCESS;
230 /* TODO implement others */
231 default:
232 return VLC_EGENERIC;
235 return VLC_EGENERIC;
238 /*****************************************************************************
239 * Demux: Processes the audio frame
240 *****************************************************************************/
241 static int Demux( demux_t *p_demux )
243 demux_sys_t *p_sys = p_demux->p_sys;
245 struct pollfd fd;
246 fd.fd = p_sys->i_fd;
247 fd.events = POLLIN|POLLPRI;
248 fd.revents = 0;
250 block_t *p_block = NULL;
254 if( p_block )
256 es_out_Send( p_demux->out, p_sys->p_es, p_block );
257 p_block = NULL;
260 /* Wait for data */
261 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. */
263 if( errno == EINTR )
264 continue;
265 if( fd.revents & (POLLIN|POLLPRI) )
267 p_block = GrabAudio( p_demux );
268 if( p_block )
269 es_out_SetPCR( p_demux->out, p_block->i_pts );
272 } while( p_block && p_sys->i_next_demux_date > 0 &&
273 p_block->i_pts < p_sys->i_next_demux_date );
275 if( p_block )
276 es_out_Send( p_demux->out, p_sys->p_es, p_block );
278 return 1;
281 /*****************************************************************************
282 * GrabAudio: Grab an audio frame
283 *****************************************************************************/
284 static block_t* GrabAudio( demux_t *p_demux )
286 demux_sys_t *p_sys = p_demux->p_sys;
287 struct audio_buf_info buf_info;
288 int i_read = 0, i_correct;
289 block_t *p_block;
291 if( p_sys->p_block ) p_block = p_sys->p_block;
292 else p_block = block_Alloc( p_sys->i_max_frame_size );
294 if( !p_block )
296 msg_Warn( p_demux, "cannot get block" );
297 return NULL;
300 p_sys->p_block = p_block;
302 i_read = read( p_sys->i_fd, p_block->p_buffer,
303 p_sys->i_max_frame_size );
305 if( i_read <= 0 ) return NULL;
307 p_block->i_buffer = i_read;
308 p_sys->p_block = NULL;
310 /* Correct the date because of kernel buffering */
311 i_correct = i_read;
312 if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
314 i_correct += buf_info.bytes;
317 /* Timestamp */
318 p_block->i_pts = p_block->i_dts =
319 vlc_tick_now() - vlc_tick_from_samples(i_correct,
320 2 * ( p_sys->b_stereo ? 2 : 1) * p_sys->i_sample_rate);
322 return p_block;
325 /*****************************************************************************
326 * OpenAudioDev: open and set up the audio device and probe for capabilities
327 *****************************************************************************/
328 static int OpenAudioDevOss( demux_t *p_demux )
330 demux_sys_t *p_sys = (demux_sys_t *)p_demux->p_sys;
331 int i_fd;
332 int i_format;
334 i_fd = vlc_open( p_sys->psz_device, O_RDONLY | O_NONBLOCK );
336 if( i_fd < 0 )
338 msg_Err( p_demux, "cannot open OSS audio device (%s)",
339 vlc_strerror_c(errno) );
340 goto adev_fail;
343 i_format = AFMT_S16_LE;
344 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
345 || i_format != AFMT_S16_LE )
347 msg_Err( p_demux,
348 "cannot set audio format (16b little endian) (%s)",
349 vlc_strerror_c(errno) );
350 goto adev_fail;
353 if( ioctl( i_fd, SNDCTL_DSP_STEREO, &p_sys->b_stereo ) < 0 )
355 msg_Err( p_demux, "cannot set audio channels count (%s)",
356 vlc_strerror_c(errno) );
357 goto adev_fail;
360 if( ioctl( i_fd, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate ) < 0 )
362 msg_Err( p_demux, "cannot set audio sample rate (%s)",
363 vlc_strerror_c(errno) );
364 goto adev_fail;
367 p_sys->i_max_frame_size = 6 * 1024;
369 return i_fd;
371 adev_fail:
373 if( i_fd >= 0 )
374 vlc_close( i_fd );
375 return -1;
378 static int OpenAudioDev( demux_t *p_demux )
380 demux_sys_t *p_sys = p_demux->p_sys;
381 int i_fd = OpenAudioDevOss( p_demux );
383 if( i_fd < 0 )
384 return i_fd;
386 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
387 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
388 p_sys->i_sample_rate );
390 es_format_t fmt;
391 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
393 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
394 fmt.audio.i_rate = p_sys->i_sample_rate;
395 fmt.audio.i_bitspersample = 16;
396 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
397 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
399 msg_Dbg( p_demux, "new audio es %d channels %dHz",
400 fmt.audio.i_channels, fmt.audio.i_rate );
402 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
404 return i_fd;
407 /*****************************************************************************
408 * ProbeAudioDev: probe audio for capabilities
409 *****************************************************************************/
410 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
412 int i_caps;
413 int i_fd = vlc_open( psz_device, O_RDONLY | O_NONBLOCK );
415 if( i_fd < 0 )
417 msg_Err( p_demux, "cannot open device %s for OSS audio (%s)",
418 psz_device, vlc_strerror_c(errno) );
419 goto open_failed;
422 /* this will fail if the device is video */
423 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
425 msg_Err( p_demux, "cannot get audio caps (%s)",
426 vlc_strerror_c(errno) );
427 goto open_failed;
430 if( i_fd >= 0 )
431 vlc_close( i_fd );
433 return true;
435 open_failed:
436 if( i_fd >= 0 )
437 vlc_close( i_fd );
438 return false;