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