1 /*****************************************************************************
2 * oss.c : OSS input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 VLC authors and VideoLAN
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 it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * 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>
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 OSS_DEFAULT "/dev/dsp"
73 #define CFG_PREFIX "oss-"
76 set_shortname( N_("OSS") )
77 set_description( N_("OSS input") )
78 set_category( CAT_INPUT
)
79 set_subcategory( SUBCAT_INPUT_ACCESS
)
82 set_capability( "access", 0 )
83 set_callbacks( DemuxOpen
, DemuxClose
)
85 add_bool( CFG_PREFIX
"stereo", true, STEREO_TEXT
, STEREO_LONGTEXT
,
87 add_integer( CFG_PREFIX
"samplerate", 48000, SAMPLERATE_TEXT
,
88 SAMPLERATE_LONGTEXT
, true )
91 /*****************************************************************************
92 * Access: local prototypes
93 *****************************************************************************/
95 static int DemuxControl( demux_t
*, int, va_list );
97 static int Demux( demux_t
* );
99 static block_t
* GrabAudio( demux_t
*p_demux
);
101 static int OpenAudioDev( demux_t
* );
102 static int OpenAudioDevOss( demux_t
* );
103 static bool ProbeAudioDevOss( demux_t
*, const char *psz_device
);
113 const char *psz_device
; /* OSS device from MRL */
118 unsigned int i_sample_rate
;
120 size_t i_max_frame_size
;
124 mtime_t i_next_demux_date
; /* Used to handle oss:// as input-slave properly */
127 static int FindMainDevice( demux_t
*p_demux
)
129 demux_sys_t
*p_sys
= p_demux
->p_sys
;
131 msg_Dbg( p_demux
, "opening device '%s'", p_sys
->psz_device
);
132 if( ProbeAudioDevOss( p_demux
, p_sys
->psz_device
) )
134 msg_Dbg( p_demux
, "'%s' is an audio device", p_sys
->psz_device
);
135 p_sys
->i_fd
= OpenAudioDev( p_demux
);
138 if( p_sys
->i_fd
< 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 if (p_demux
->out
== NULL
)
159 p_demux
->pf_control
= DemuxControl
;
160 p_demux
->pf_demux
= Demux
;
162 p_demux
->p_sys
= p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( demux_sys_t
) );
163 if( p_sys
== NULL
) return VLC_ENOMEM
;
165 p_sys
->i_sample_rate
= var_InheritInteger( p_demux
, CFG_PREFIX
"samplerate" );
166 p_sys
->b_stereo
= var_InheritBool( p_demux
, CFG_PREFIX
"stereo" );
169 p_sys
->p_block
= NULL
;
170 p_sys
->i_next_demux_date
= -1;
172 if( p_demux
->psz_location
&& *p_demux
->psz_location
)
173 p_sys
->psz_device
= p_demux
->psz_location
;
175 p_sys
->psz_device
= OSS_DEFAULT
;
177 if( FindMainDevice( p_demux
) != VLC_SUCCESS
)
179 DemuxClose( p_this
);
186 /*****************************************************************************
187 * Close: close device, free resources
188 *****************************************************************************/
189 static void DemuxClose( vlc_object_t
*p_this
)
191 demux_t
*p_demux
= (demux_t
*)p_this
;
192 demux_sys_t
*p_sys
= p_demux
->p_sys
;
194 if( p_sys
->i_fd
>= 0 )
195 vlc_close( p_sys
->i_fd
);
197 if( p_sys
->p_block
) block_Release( p_sys
->p_block
);
200 /*****************************************************************************
202 *****************************************************************************/
203 static int DemuxControl( demux_t
*p_demux
, int i_query
, va_list args
)
205 demux_sys_t
*p_sys
= p_demux
->p_sys
;
211 /* Special for access_demux */
212 case DEMUX_CAN_PAUSE
:
214 case DEMUX_CAN_CONTROL_PACE
:
215 pb
= va_arg( args
, bool * );
219 case DEMUX_GET_PTS_DELAY
:
220 pi64
= va_arg( args
, int64_t * );
221 *pi64
= INT64_C(1000)
222 * var_InheritInteger( p_demux
, "live-caching" );
226 pi64
= va_arg( args
, int64_t * );
230 case DEMUX_SET_NEXT_DEMUX_TIME
:
231 p_sys
->i_next_demux_date
= va_arg( args
, mtime_t
);
234 /* TODO implement others */
242 /*****************************************************************************
243 * Demux: Processes the audio frame
244 *****************************************************************************/
245 static int Demux( demux_t
*p_demux
)
247 demux_sys_t
*p_sys
= p_demux
->p_sys
;
251 fd
.events
= POLLIN
|POLLPRI
;
254 block_t
*p_block
= NULL
;
260 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
265 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. */
269 if( fd
.revents
& (POLLIN
|POLLPRI
) )
271 p_block
= GrabAudio( p_demux
);
273 es_out_SetPCR( p_demux
->out
, p_block
->i_pts
);
276 } while( p_block
&& p_sys
->i_next_demux_date
> 0 &&
277 p_block
->i_pts
< p_sys
->i_next_demux_date
);
280 es_out_Send( p_demux
->out
, p_sys
->p_es
, p_block
);
285 /*****************************************************************************
286 * GrabAudio: Grab an audio frame
287 *****************************************************************************/
288 static block_t
* GrabAudio( demux_t
*p_demux
)
290 demux_sys_t
*p_sys
= p_demux
->p_sys
;
291 struct audio_buf_info buf_info
;
292 int i_read
= 0, i_correct
;
295 if( p_sys
->p_block
) p_block
= p_sys
->p_block
;
296 else p_block
= block_Alloc( p_sys
->i_max_frame_size
);
300 msg_Warn( p_demux
, "cannot get block" );
304 p_sys
->p_block
= p_block
;
306 i_read
= read( p_sys
->i_fd
, p_block
->p_buffer
,
307 p_sys
->i_max_frame_size
);
309 if( i_read
<= 0 ) return NULL
;
311 p_block
->i_buffer
= i_read
;
312 p_sys
->p_block
= NULL
;
314 /* Correct the date because of kernel buffering */
316 if( ioctl( p_sys
->i_fd
, SNDCTL_DSP_GETISPACE
, &buf_info
) == 0 )
318 i_correct
+= buf_info
.bytes
;
322 p_block
->i_pts
= p_block
->i_dts
=
323 mdate() - CLOCK_FREQ
* (mtime_t
)i_correct
/
324 2 / ( p_sys
->b_stereo
? 2 : 1) / p_sys
->i_sample_rate
;
329 /*****************************************************************************
330 * OpenAudioDev: open and set up the audio device and probe for capabilities
331 *****************************************************************************/
332 static int OpenAudioDevOss( demux_t
*p_demux
)
334 demux_sys_t
*p_sys
= (demux_sys_t
*)p_demux
->p_sys
;
338 i_fd
= vlc_open( p_sys
->psz_device
, O_RDONLY
| O_NONBLOCK
);
342 msg_Err( p_demux
, "cannot open OSS audio device (%s)",
343 vlc_strerror_c(errno
) );
347 i_format
= AFMT_S16_LE
;
348 if( ioctl( i_fd
, SNDCTL_DSP_SETFMT
, &i_format
) < 0
349 || i_format
!= AFMT_S16_LE
)
352 "cannot set audio format (16b little endian) (%s)",
353 vlc_strerror_c(errno
) );
357 if( ioctl( i_fd
, SNDCTL_DSP_STEREO
, &p_sys
->b_stereo
) < 0 )
359 msg_Err( p_demux
, "cannot set audio channels count (%s)",
360 vlc_strerror_c(errno
) );
364 if( ioctl( i_fd
, SNDCTL_DSP_SPEED
, &p_sys
->i_sample_rate
) < 0 )
366 msg_Err( p_demux
, "cannot set audio sample rate (%s)",
367 vlc_strerror_c(errno
) );
371 p_sys
->i_max_frame_size
= 6 * 1024;
382 static int OpenAudioDev( demux_t
*p_demux
)
384 demux_sys_t
*p_sys
= p_demux
->p_sys
;
385 int i_fd
= OpenAudioDevOss( p_demux
);
390 msg_Dbg( p_demux
, "opened adev=`%s' %s %dHz",
391 p_sys
->psz_device
, p_sys
->b_stereo
? "stereo" : "mono",
392 p_sys
->i_sample_rate
);
395 es_format_Init( &fmt
, AUDIO_ES
, VLC_CODEC_S16L
);
397 fmt
.audio
.i_channels
= p_sys
->b_stereo
? 2 : 1;
398 fmt
.audio
.i_rate
= p_sys
->i_sample_rate
;
399 fmt
.audio
.i_bitspersample
= 16;
400 fmt
.audio
.i_blockalign
= fmt
.audio
.i_channels
* fmt
.audio
.i_bitspersample
/ 8;
401 fmt
.i_bitrate
= fmt
.audio
.i_channels
* fmt
.audio
.i_rate
* fmt
.audio
.i_bitspersample
;
403 msg_Dbg( p_demux
, "new audio es %d channels %dHz",
404 fmt
.audio
.i_channels
, fmt
.audio
.i_rate
);
406 p_sys
->p_es
= es_out_Add( p_demux
->out
, &fmt
);
411 /*****************************************************************************
412 * ProbeAudioDev: probe audio for capabilities
413 *****************************************************************************/
414 static bool ProbeAudioDevOss( demux_t
*p_demux
, const char *psz_device
)
417 int i_fd
= vlc_open( psz_device
, O_RDONLY
| O_NONBLOCK
);
421 msg_Err( p_demux
, "cannot open device %s for OSS audio (%s)",
422 psz_device
, vlc_strerror_c(errno
) );
426 /* this will fail if the device is video */
427 if( ioctl( i_fd
, SNDCTL_DSP_GETCAPS
, &i_caps
) < 0 )
429 msg_Err( p_demux
, "cannot get audio caps (%s)",
430 vlc_strerror_c(errno
) );