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