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