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_input.h>
45 #include <sys/ioctl.h>
48 #include <sys/soundcard.h>
52 /*****************************************************************************
54 *****************************************************************************/
56 static int DemuxOpen ( vlc_object_t * );
57 static void DemuxClose( vlc_object_t * );
60 #define STEREO_TEXT N_( "Stereo" )
61 #define STEREO_LONGTEXT N_( \
62 "Capture the audio stream in stereo." )
63 #define SAMPLERATE_TEXT N_( "Samplerate" )
64 #define SAMPLERATE_LONGTEXT N_( \
65 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
67 #define CACHING_TEXT N_("Caching value in ms")
68 #define CACHING_LONGTEXT N_( \
69 "Caching value for OSS captures. This " \
70 "value should be set in milliseconds." )
72 #define OSS_DEFAULT "/dev/dsp"
74 #define CFG_PREFIX "oss-"
77 set_shortname( N_("OSS") )
78 set_description( N_("OSS input") )
79 set_category( CAT_INPUT )
80 set_subcategory( SUBCAT_INPUT_ACCESS )
83 set_capability( "access_demux", 10 )
84 set_callbacks( DemuxOpen, DemuxClose )
86 add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
88 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
89 SAMPLERATE_LONGTEXT, true );
90 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
91 CACHING_TEXT, CACHING_LONGTEXT, true );
94 /*****************************************************************************
95 * Access: local prototypes
96 *****************************************************************************/
98 static int DemuxControl( demux_t *, int, va_list );
100 static int Demux( demux_t * );
102 static block_t* GrabAudio( demux_t *p_demux );
104 static int OpenAudioDev( demux_t * );
105 static int OpenAudioDevOss( demux_t * );
106 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
116 const char *psz_device; /* OSS device from MRL */
122 unsigned int i_sample_rate;
124 size_t i_audio_max_frame_size;
125 block_t *p_block_audio;
126 es_out_id_t *p_es_audio;
129 static int FindMainDevice( demux_t *p_demux )
131 msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
132 if( ProbeAudioDevOss( p_demux, p_demux->p_sys->psz_device ) )
134 msg_Dbg( p_demux, "'%s' is an audio device", p_demux->p_sys->psz_device );
135 p_demux->p_sys->i_fd_audio = OpenAudioDev( p_demux );
138 if( p_demux->p_sys->i_fd_audio < 0 )
143 /*****************************************************************************
144 * DemuxOpen: opens oss device, access_demux callback
145 *****************************************************************************
147 * url: <oss device>::::
149 *****************************************************************************/
150 static int DemuxOpen( vlc_object_t *p_this )
152 demux_t *p_demux = (demux_t*)p_this;
155 /* Only when selected */
156 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
159 p_demux->pf_control = DemuxControl;
160 p_demux->pf_demux = Demux;
161 p_demux->info.i_update = 0;
162 p_demux->info.i_title = 0;
163 p_demux->info.i_seekpoint = 0;
165 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
166 if( p_sys == NULL ) return VLC_ENOMEM;
168 p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
169 p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
170 p_sys->i_pts = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
171 p_sys->psz_device = NULL;
172 p_sys->i_fd_audio = -1;
173 p_sys->p_es_audio = 0;
174 p_sys->p_block_audio = 0;
176 if( p_demux->psz_path && *p_demux->psz_path )
177 p_sys->psz_device = p_demux->psz_path;
179 p_sys->psz_device = OSS_DEFAULT;
180 msg_Err( p_this, "Device is %s", p_sys->psz_device );
182 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
184 DemuxClose( p_this );
191 /*****************************************************************************
192 * Close: close device, free resources
193 *****************************************************************************/
194 static void DemuxClose( vlc_object_t *p_this )
196 demux_t *p_demux = (demux_t *)p_this;
197 demux_sys_t *p_sys = p_demux->p_sys;
199 if( p_sys->i_fd_audio >= 0 ) close( p_sys->i_fd_audio );
201 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
205 /*****************************************************************************
207 *****************************************************************************/
208 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
210 demux_sys_t *p_sys = p_demux->p_sys;
216 /* Special for access_demux */
217 case DEMUX_CAN_PAUSE:
219 case DEMUX_SET_PAUSE_STATE:
220 case DEMUX_CAN_CONTROL_PACE:
221 pb = (bool*)va_arg( args, bool * );
225 case DEMUX_GET_PTS_DELAY:
226 pi64 = (int64_t*)va_arg( args, int64_t * );
227 *pi64 = (int64_t)p_sys->i_pts * 1000;
231 pi64 = (int64_t*)va_arg( args, int64_t * );
235 /* TODO implement others */
243 /*****************************************************************************
244 * Demux: Processes the audio frame
245 *****************************************************************************/
246 static int Demux( demux_t *p_demux )
248 demux_sys_t *p_sys = p_demux->p_sys;
251 fd.fd = p_sys->i_fd_audio;
252 fd.events = POLLIN|POLLPRI;
256 if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
258 if( fd.revents & (POLLIN|POLLPRI) )
260 block_t *p_block = GrabAudio( p_demux );
263 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
264 es_out_Send( p_demux->out, p_sys->p_es_audio, p_block );
272 /*****************************************************************************
273 * GrabAudio: Grab an audio frame
274 *****************************************************************************/
275 static block_t* GrabAudio( demux_t *p_demux )
277 demux_sys_t *p_sys = p_demux->p_sys;
278 struct audio_buf_info buf_info;
279 int i_read = 0, i_correct;
282 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
283 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
287 msg_Warn( p_demux, "cannot get block" );
291 p_sys->p_block_audio = p_block;
293 i_read = read( p_sys->i_fd_audio, p_block->p_buffer,
294 p_sys->i_audio_max_frame_size );
296 if( i_read <= 0 ) return 0;
298 p_block->i_buffer = i_read;
299 p_sys->p_block_audio = 0;
301 /* Correct the date because of kernel buffering */
303 if( ioctl( p_sys->i_fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
305 i_correct += buf_info.bytes;
309 p_block->i_pts = p_block->i_dts =
310 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
311 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
316 /*****************************************************************************
317 * OpenAudioDev: open and set up the audio device and probe for capabilities
318 *****************************************************************************/
319 static int OpenAudioDevOss( demux_t *p_demux )
324 i_fd = open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
328 msg_Err( p_demux, "cannot open OSS audio device (%m)" );
332 i_format = AFMT_S16_LE;
333 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
334 || i_format != AFMT_S16_LE )
337 "cannot set audio format (16b little endian) (%m)" );
341 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
342 &p_demux->p_sys->b_stereo ) < 0 )
344 msg_Err( p_demux, "cannot set audio channels count (%m)" );
348 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
349 &p_demux->p_sys->i_sample_rate ) < 0 )
351 msg_Err( p_demux, "cannot set audio sample rate (%m)" );
355 p_demux->p_sys->i_audio_max_frame_size = 6 * 1024;
361 if( i_fd >= 0 ) close( i_fd );
365 static int OpenAudioDev( demux_t *p_demux )
367 demux_sys_t *p_sys = p_demux->p_sys;
368 int i_fd = OpenAudioDevOss( p_demux );
373 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
374 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
375 p_sys->i_sample_rate );
378 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
380 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
381 fmt.audio.i_rate = p_sys->i_sample_rate;
382 fmt.audio.i_bitspersample = 16;
383 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
384 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
386 msg_Dbg( p_demux, "new audio es %d channels %dHz",
387 fmt.audio.i_channels, fmt.audio.i_rate );
389 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
394 /*****************************************************************************
395 * ProbeAudioDev: probe audio for capabilities
396 *****************************************************************************/
397 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
400 int i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
404 msg_Err( p_demux, "cannot open device %s for OSS audio (%m)", psz_device );
408 /* this will fail if the device is video */
409 if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
411 msg_Err( p_demux, "cannot get audio caps (%m)" );
415 if( i_fd >= 0 ) close( i_fd );
420 if( i_fd >= 0 ) close( i_fd );