]> git.sesse.net Git - vlc/blob - modules/access/oss.c
Merge branch 1.0-bugfix
[vlc] / modules / access / oss.c
1 /*****************************************************************************
2  * oss.c : OSS input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
34
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>
40
41 #include <ctype.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <sys/ioctl.h>
45 #include <sys/mman.h>
46
47 #ifdef HAVE_SYS_SOUNDCARD_H
48 #  include <sys/soundcard.h>
49 #endif
50 #ifdef HAVE_SOUNDCARD_H
51 #  include <soundcard.h>
52 #endif
53
54 #include <poll.h>
55
56 /*****************************************************************************
57  * Module descriptior
58  *****************************************************************************/
59
60 static int  DemuxOpen ( vlc_object_t * );
61 static void DemuxClose( vlc_object_t * );
62
63
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)" )
70
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." )
75
76 #define OSS_DEFAULT "/dev/dsp"
77
78 #define CFG_PREFIX "oss-"
79
80 vlc_module_begin ()
81     set_shortname( N_("OSS") )
82     set_description( N_("OSS input") )
83     set_category( CAT_INPUT )
84     set_subcategory( SUBCAT_INPUT_ACCESS )
85
86     add_shortcut( "oss" )
87     set_capability( "access_demux", 10 )
88     set_callbacks( DemuxOpen, DemuxClose )
89
90     add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
91                 true )
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 )
96 vlc_module_end ()
97
98 /*****************************************************************************
99  * Access: local prototypes
100  *****************************************************************************/
101
102 static int DemuxControl( demux_t *, int, va_list );
103
104 static int Demux( demux_t * );
105
106 static block_t* GrabAudio( demux_t *p_demux );
107
108 static int OpenAudioDev( demux_t * );
109 static int OpenAudioDevOss( demux_t * );
110 static bool ProbeAudioDevOss( demux_t *, const char *psz_device );
111
112 struct buffer_t
113 {
114     void *  start;
115     size_t  length;
116 };
117
118 struct demux_sys_t
119 {
120     const char *psz_device;  /* OSS device from MRL */
121
122     int  i_fd;
123
124     /* Audio */
125     int i_cache;
126     unsigned int i_sample_rate;
127     bool b_stereo;
128     size_t i_max_frame_size;
129     block_t *p_block;
130     es_out_id_t *p_es;
131
132     int64_t i_next_demux_date; /* Used to handle oss:// as input-slave properly */
133 };
134
135 static int FindMainDevice( demux_t *p_demux )
136 {
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 ) )
139     {
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 );
142     }
143
144     if( p_demux->p_sys->i_fd < 0 )
145         return VLC_EGENERIC;
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * DemuxOpen: opens oss device, access_demux callback
151  *****************************************************************************
152  *
153  * url: <oss device>::::
154  *
155  *****************************************************************************/
156 static int DemuxOpen( vlc_object_t *p_this )
157 {
158     demux_t     *p_demux = (demux_t*)p_this;
159     demux_sys_t *p_sys;
160
161     /* Only when selected */
162     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
163
164     /* Set up p_demux */
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;
170
171     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
172     if( p_sys == NULL ) return VLC_ENOMEM;
173
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" );
177     p_sys->i_fd = -1;
178     p_sys->p_es = NULL;
179     p_sys->p_block = NULL;
180     p_sys->i_next_demux_date = -1;
181
182     if( p_demux->psz_path && *p_demux->psz_path )
183         p_sys->psz_device = p_demux->psz_path;
184     else
185         p_sys->psz_device = OSS_DEFAULT;
186
187     if( FindMainDevice( p_demux ) != VLC_SUCCESS )
188     {
189         DemuxClose( p_this );
190         return VLC_EGENERIC;
191     }
192
193     return VLC_SUCCESS;
194 }
195
196 /*****************************************************************************
197  * Close: close device, free resources
198  *****************************************************************************/
199 static void DemuxClose( vlc_object_t *p_this )
200 {
201     demux_t     *p_demux = (demux_t *)p_this;
202     demux_sys_t *p_sys   = p_demux->p_sys;
203
204     if( p_sys->i_fd >= 0 ) close( p_sys->i_fd );
205
206     if( p_sys->p_block ) block_Release( p_sys->p_block );
207     free( p_sys );
208 }
209
210 /*****************************************************************************
211  * DemuxControl:
212  *****************************************************************************/
213 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
214 {
215     demux_sys_t *p_sys = p_demux->p_sys;
216     bool *pb;
217     int64_t    *pi64;
218
219     switch( i_query )
220     {
221         /* Special for access_demux */
222         case DEMUX_CAN_PAUSE:
223         case DEMUX_CAN_SEEK:
224         case DEMUX_SET_PAUSE_STATE:
225         case DEMUX_CAN_CONTROL_PACE:
226             pb = (bool*)va_arg( args, bool * );
227             *pb = false;
228             return VLC_SUCCESS;
229
230         case DEMUX_GET_PTS_DELAY:
231             pi64 = (int64_t*)va_arg( args, int64_t * );
232             *pi64 = (int64_t)p_sys->i_cache * 1000;
233             return VLC_SUCCESS;
234
235         case DEMUX_GET_TIME:
236             pi64 = (int64_t*)va_arg( args, int64_t * );
237             *pi64 = mdate();
238             return VLC_SUCCESS;
239
240         case DEMUX_SET_NEXT_DEMUX_TIME:
241             p_sys->i_next_demux_date = (int64_t)va_arg( args, int64_t );
242             return VLC_SUCCESS;
243
244         /* TODO implement others */
245         default:
246             return VLC_EGENERIC;
247     }
248
249     return VLC_EGENERIC;
250 }
251
252 /*****************************************************************************
253  * Demux: Processes the audio frame
254  *****************************************************************************/
255 static int Demux( demux_t *p_demux )
256 {
257     demux_sys_t *p_sys = p_demux->p_sys;
258
259     struct pollfd fd;
260     fd.fd = p_sys->i_fd;
261     fd.events = POLLIN|POLLPRI;
262     fd.revents = 0;
263
264     block_t *p_block = NULL;
265
266     do
267     {
268         if( p_block )
269         {
270             es_out_Send( p_demux->out, p_sys->p_es, p_block );
271             p_block = NULL;
272         }
273
274         /* Wait for data */
275         if( poll( &fd, 1, 500 ) ) /* Timeout after 0.5 seconds since I don't know if pf_demux can be blocking. */
276         {
277             if( fd.revents & (POLLIN|POLLPRI) )
278             {
279                 p_block = GrabAudio( p_demux );
280                 if( p_block )
281                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
282             }
283         }
284     } while( p_block && p_sys->i_next_demux_date > 0 &&
285              p_block->i_pts < p_sys->i_next_demux_date );
286
287     if( p_block )
288         es_out_Send( p_demux->out, p_sys->p_es, p_block );
289
290     return 1;
291 }
292
293 /*****************************************************************************
294  * GrabAudio: Grab an audio frame
295  *****************************************************************************/
296 static block_t* GrabAudio( demux_t *p_demux )
297 {
298     demux_sys_t *p_sys = p_demux->p_sys;
299     struct audio_buf_info buf_info;
300     int i_read = 0, i_correct;
301     block_t *p_block;
302
303     if( p_sys->p_block ) p_block = p_sys->p_block;
304     else p_block = block_New( p_demux, p_sys->i_max_frame_size );
305
306     if( !p_block )
307     {
308         msg_Warn( p_demux, "cannot get block" );
309         return 0;
310     }
311
312     p_sys->p_block = p_block;
313
314     i_read = read( p_sys->i_fd, p_block->p_buffer,
315                 p_sys->i_max_frame_size );
316
317     if( i_read <= 0 ) return 0;
318
319     p_block->i_buffer = i_read;
320     p_sys->p_block = 0;
321
322     /* Correct the date because of kernel buffering */
323     i_correct = i_read;
324     if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
325     {
326         i_correct += buf_info.bytes;
327     }
328
329     /* Timestamp */
330     p_block->i_pts = p_block->i_dts =
331         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
332         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
333
334     return p_block;
335 }
336
337 /*****************************************************************************
338  * OpenAudioDev: open and set up the audio device and probe for capabilities
339  *****************************************************************************/
340 static int OpenAudioDevOss( demux_t *p_demux )
341 {
342     int i_fd;
343     int i_format;
344
345     i_fd = open( p_demux->p_sys->psz_device, O_RDONLY | O_NONBLOCK );
346
347     if( i_fd < 0 )
348     {
349         msg_Err( p_demux, "cannot open OSS audio device (%m)" );
350         goto adev_fail;
351     }
352
353     i_format = AFMT_S16_LE;
354     if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
355         || i_format != AFMT_S16_LE )
356     {
357         msg_Err( p_demux,
358                  "cannot set audio format (16b little endian) (%m)" );
359         goto adev_fail;
360     }
361
362     if( ioctl( i_fd, SNDCTL_DSP_STEREO,
363                &p_demux->p_sys->b_stereo ) < 0 )
364     {
365         msg_Err( p_demux, "cannot set audio channels count (%m)" );
366         goto adev_fail;
367     }
368
369     if( ioctl( i_fd, SNDCTL_DSP_SPEED,
370                &p_demux->p_sys->i_sample_rate ) < 0 )
371     {
372         msg_Err( p_demux, "cannot set audio sample rate (%m)" );
373         goto adev_fail;
374     }
375
376     p_demux->p_sys->i_max_frame_size = 6 * 1024;
377
378     return i_fd;
379
380  adev_fail:
381
382     if( i_fd >= 0 ) close( i_fd );
383     return -1;
384 }
385
386 static int OpenAudioDev( demux_t *p_demux )
387 {
388     demux_sys_t *p_sys = p_demux->p_sys;
389     int i_fd = OpenAudioDevOss( p_demux );
390
391     if( i_fd < 0 )
392         return i_fd;
393
394     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
395              p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
396              p_sys->i_sample_rate );
397
398     es_format_t fmt;
399     es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
400
401     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
402     fmt.audio.i_rate = p_sys->i_sample_rate;
403     fmt.audio.i_bitspersample = 16;
404     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
405     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
406
407     msg_Dbg( p_demux, "new audio es %d channels %dHz",
408              fmt.audio.i_channels, fmt.audio.i_rate );
409
410     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
411
412     return i_fd;
413 }
414
415 /*****************************************************************************
416  * ProbeAudioDev: probe audio for capabilities
417  *****************************************************************************/
418 static bool ProbeAudioDevOss( demux_t *p_demux, const char *psz_device )
419 {
420     int i_caps;
421     int i_fd = open( psz_device, O_RDONLY | O_NONBLOCK );
422
423     if( i_fd < 0 )
424     {
425         msg_Err( p_demux, "cannot open device %s for OSS audio (%m)", psz_device );
426         goto open_failed;
427     }
428
429     /* this will fail if the device is video */
430     if( ioctl( i_fd, SNDCTL_DSP_GETCAPS, &i_caps ) < 0 )
431     {
432         msg_Err( p_demux, "cannot get audio caps (%m)" );
433         goto open_failed;
434     }
435
436     if( i_fd >= 0 ) close( i_fd );
437
438     return true;
439
440 open_failed:
441     if( i_fd >= 0 ) close( i_fd );
442     return false;
443 }