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 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_access.h>
37 #include <vlc_demux.h>
43 #include <sys/ioctl.h>
46 #ifdef HAVE_SYS_SOUNDCARD_H
47 # include <sys/soundcard.h>
49 #ifdef HAVE_SOUNDCARD_H
50 # include <soundcard.h>
55 /*****************************************************************************
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-"
75 set_shortname( N_("OSS") )
76 set_description( N_("OSS input") )
77 set_category( CAT_INPUT
)
78 set_subcategory( SUBCAT_INPUT_ACCESS
)
81 set_capability( "access", 0 )
82 set_callbacks( DemuxOpen
, DemuxClose
)
84 add_bool( CFG_PREFIX
"stereo", true, STEREO_TEXT
, STEREO_LONGTEXT
,
86 add_integer( CFG_PREFIX
"samplerate", 48000, SAMPLERATE_TEXT
,
87 SAMPLERATE_LONGTEXT
, true )
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
);
112 const char *psz_device
; /* OSS device from MRL */
117 unsigned int i_sample_rate
;
119 size_t i_max_frame_size
;
123 vlc_tick_t i_next_demux_date
; /* Used to handle oss:// as input-slave properly */
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 )
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
;
154 if (p_demux
->out
== NULL
)
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" );
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
;
174 p_sys
->psz_device
= OSS_DEFAULT
;
176 if( FindMainDevice( p_demux
) != VLC_SUCCESS
)
178 DemuxClose( p_this
);
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 /*****************************************************************************
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
;
209 /* Special for access_demux */
210 case DEMUX_CAN_PAUSE
:
212 case DEMUX_CAN_CONTROL_PACE
:
213 pb
= va_arg( args
, bool * );
217 case DEMUX_GET_PTS_DELAY
:
218 *va_arg( args
, vlc_tick_t
* ) =
219 VLC_TICK_FROM_MS(var_InheritInteger( p_demux
, "live-caching" ));
223 *va_arg( args
, vlc_tick_t
* ) = vlc_tick_now();
226 case DEMUX_SET_NEXT_DEMUX_TIME
:
227 p_sys
->i_next_demux_date
= va_arg( args
, vlc_tick_t
);
230 /* TODO implement others */
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
;
247 fd
.events
= POLLIN
|POLLPRI
;
250 block_t
*p_block
= NULL
;
256 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
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. */
265 if( fd
.revents
& (POLLIN
|POLLPRI
) )
267 p_block
= GrabAudio( p_demux
);
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
);
276 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
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
;
291 if( p_sys
->p_block
) p_block
= p_sys
->p_block
;
292 else p_block
= block_Alloc( p_sys
->i_max_frame_size
);
296 msg_Warn( p_demux
, "cannot get block" );
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 */
312 if( ioctl( p_sys
->i_fd
, SNDCTL_DSP_GETISPACE
, &buf_info
) == 0 )
314 i_correct
+= buf_info
.bytes
;
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
);
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
;
334 i_fd
= vlc_open( p_sys
->psz_device
, O_RDONLY
| O_NONBLOCK
);
338 msg_Err( p_demux
, "cannot open OSS audio device (%s)",
339 vlc_strerror_c(errno
) );
343 i_format
= AFMT_S16_LE
;
344 if( ioctl( i_fd
, SNDCTL_DSP_SETFMT
, &i_format
) < 0
345 || i_format
!= AFMT_S16_LE
)
348 "cannot set audio format (16b little endian) (%s)",
349 vlc_strerror_c(errno
) );
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
) );
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
) );
367 p_sys
->i_max_frame_size
= 6 * 1024;
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
);
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
);
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
);
407 /*****************************************************************************
408 * ProbeAudioDev: probe audio for capabilities
409 *****************************************************************************/
410 static bool ProbeAudioDevOss( demux_t
*p_demux
, const char *psz_device
)
413 int i_fd
= vlc_open( psz_device
, O_RDONLY
| O_NONBLOCK
);
417 msg_Err( p_demux
, "cannot open device %s for OSS audio (%s)",
418 psz_device
, vlc_strerror_c(errno
) );
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
) );