1 /* GStreamer
2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000,2005 Wim Taymans <wim@fluendo.com>
4 *
5 * gstosssrc.c:
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
16 *
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 /**
24 * SECTION:element-osssrc
25 * @title: osssrc
26 *
27 * This element lets you record sound using the Open Sound System (OSS).
28 *
29 * ## Example pipelines
30 * |[
31 * gst-launch-1.0 -v osssrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=mymusic.ogg
32 * ]| will record sound from your sound card using OSS and encode it to an
33 * Ogg/Vorbis file (this will only work if your mixer settings are right
34 * and the right inputs enabled etc.)
35 *
36 */
37
38 #ifdef HAVE_CONFIG_H
39 #include "config.h"
40 #endif
41
42 #include <sys/ioctl.h>
43 #include <fcntl.h>
44 #include <errno.h>
45 #include <unistd.h>
46 #include <string.h>
47
48 #ifdef HAVE_OSS_INCLUDE_IN_SYS
49 # include <sys/soundcard.h>
50 #else
51 # ifdef HAVE_OSS_INCLUDE_IN_ROOT
52 # include <soundcard.h>
53 # else
54 # ifdef HAVE_OSS_INCLUDE_IN_MACHINE
55 # include <machine/soundcard.h>
56 # else
57 # error "What to include?"
58 # endif /* HAVE_OSS_INCLUDE_IN_MACHINE */
59 # endif /* HAVE_OSS_INCLUDE_IN_ROOT */
60 #endif /* HAVE_OSS_INCLUDE_IN_SYS */
61
62 #include "common.h"
63 #include "gstossaudioelements.h"
64 #include "gstosssrc.h"
65
66 #include <gst/gst-i18n-plugin.h>
67
68 GST_DEBUG_CATEGORY_EXTERN (oss_debug);
69 #define GST_CAT_DEFAULT oss_debug
70
71 #define DEFAULT_DEVICE "/dev/dsp"
72 #define DEFAULT_DEVICE_NAME ""
73
74 enum
75 {
76 PROP_0,
77 PROP_DEVICE,
78 PROP_DEVICE_NAME,
79 };
80
81 #define gst_oss_src_parent_class parent_class
82 G_DEFINE_TYPE (GstOssSrc, gst_oss_src, GST_TYPE_AUDIO_SRC);
83 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (osssrc, "osssrc", GST_RANK_SECONDARY,
84 GST_TYPE_OSS_SRC, oss_element_init (plugin));
85
86 static void gst_oss_src_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
88 static void gst_oss_src_set_property (GObject * object, guint prop_id,
89 const GValue * value, GParamSpec * pspec);
90
91 static void gst_oss_src_dispose (GObject * object);
92 static void gst_oss_src_finalize (GstOssSrc * osssrc);
93
94 static GstCaps *gst_oss_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
95
96 static gboolean gst_oss_src_open (GstAudioSrc * asrc);
97 static gboolean gst_oss_src_close (GstAudioSrc * asrc);
98 static gboolean gst_oss_src_prepare (GstAudioSrc * asrc,
99 GstAudioRingBufferSpec * spec);
100 static gboolean gst_oss_src_unprepare (GstAudioSrc * asrc);
101 static guint gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length,
102 GstClockTime * timestamp);
103 static guint gst_oss_src_delay (GstAudioSrc * asrc);
104 static void gst_oss_src_reset (GstAudioSrc * asrc);
105
106 #define FORMATS "{" GST_AUDIO_NE(S16)","GST_AUDIO_NE(U16)", S8, U8 }"
107
108 static GstStaticPadTemplate osssrc_src_factory = GST_STATIC_PAD_TEMPLATE ("src",
109 GST_PAD_SRC,
110 GST_PAD_ALWAYS,
111 GST_STATIC_CAPS ("audio/x-raw, "
112 "format = (string) " FORMATS ", "
113 "layout = (string) interleaved, "
114 "rate = (int) [ 1, MAX ], "
115 "channels = (int) 1; "
116 "audio/x-raw, "
117 "format = (string) " FORMATS ", "
118 "layout = (string) interleaved, "
119 "rate = (int) [ 1, MAX ], "
120 "channels = (int) 2, " "channel-mask = (bitmask) 0x3")
121 );
122
123 static void
gst_oss_src_dispose(GObject * object)124 gst_oss_src_dispose (GObject * object)
125 {
126 G_OBJECT_CLASS (parent_class)->dispose (object);
127 }
128
129 static void
gst_oss_src_class_init(GstOssSrcClass * klass)130 gst_oss_src_class_init (GstOssSrcClass * klass)
131 {
132 GObjectClass *gobject_class;
133 GstElementClass *gstelement_class;
134 GstBaseSrcClass *gstbasesrc_class;
135 GstAudioSrcClass *gstaudiosrc_class;
136
137 gobject_class = (GObjectClass *) klass;
138 gstelement_class = (GstElementClass *) klass;
139 gstbasesrc_class = (GstBaseSrcClass *) klass;
140 gstaudiosrc_class = (GstAudioSrcClass *) klass;
141
142 gobject_class->dispose = gst_oss_src_dispose;
143 gobject_class->finalize = (GObjectFinalizeFunc) gst_oss_src_finalize;
144 gobject_class->get_property = gst_oss_src_get_property;
145 gobject_class->set_property = gst_oss_src_set_property;
146
147 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_oss_src_getcaps);
148
149 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_oss_src_open);
150 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_oss_src_prepare);
151 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_oss_src_unprepare);
152 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_oss_src_close);
153 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_oss_src_read);
154 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_oss_src_delay);
155 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_oss_src_reset);
156
157 g_object_class_install_property (gobject_class, PROP_DEVICE,
158 g_param_spec_string ("device", "Device",
159 "OSS device (usually /dev/dspN)", DEFAULT_DEVICE,
160 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
161
162 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
163 g_param_spec_string ("device-name", "Device name",
164 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
165 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
166
167
168 gst_element_class_set_static_metadata (gstelement_class, "Audio Source (OSS)",
169 "Source/Audio",
170 "Capture from a sound card via OSS",
171 "Erik Walthinsen <omega@cse.ogi.edu>, " "Wim Taymans <wim@fluendo.com>");
172
173 gst_element_class_add_static_pad_template (gstelement_class,
174 &osssrc_src_factory);
175 }
176
177 static void
gst_oss_src_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)178 gst_oss_src_set_property (GObject * object, guint prop_id,
179 const GValue * value, GParamSpec * pspec)
180 {
181 GstOssSrc *src;
182
183 src = GST_OSS_SRC (object);
184
185 switch (prop_id) {
186 case PROP_DEVICE:
187 g_free (src->device);
188 src->device = g_value_dup_string (value);
189 break;
190 default:
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
192 break;
193 }
194 }
195
196 static void
gst_oss_src_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)197 gst_oss_src_get_property (GObject * object, guint prop_id,
198 GValue * value, GParamSpec * pspec)
199 {
200 GstOssSrc *src;
201
202 src = GST_OSS_SRC (object);
203
204 switch (prop_id) {
205 case PROP_DEVICE:
206 g_value_set_string (value, src->device);
207 break;
208 case PROP_DEVICE_NAME:
209 g_value_set_string (value, src->device_name);
210 break;
211 default:
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 break;
214 }
215 }
216
217 static void
gst_oss_src_init(GstOssSrc * osssrc)218 gst_oss_src_init (GstOssSrc * osssrc)
219 {
220 const gchar *device;
221
222 GST_DEBUG ("initializing osssrc");
223
224 device = g_getenv ("AUDIODEV");
225 if (device == NULL)
226 device = DEFAULT_DEVICE;
227
228 osssrc->fd = -1;
229 osssrc->device = g_strdup (device);
230 osssrc->device_name = g_strdup (DEFAULT_DEVICE_NAME);
231 osssrc->probed_caps = NULL;
232 }
233
234 static void
gst_oss_src_finalize(GstOssSrc * osssrc)235 gst_oss_src_finalize (GstOssSrc * osssrc)
236 {
237 g_free (osssrc->device);
238 g_free (osssrc->device_name);
239
240 G_OBJECT_CLASS (parent_class)->finalize ((GObject *) (osssrc));
241 }
242
243 static GstCaps *
gst_oss_src_getcaps(GstBaseSrc * bsrc,GstCaps * filter)244 gst_oss_src_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
245 {
246 GstOssSrc *osssrc;
247 GstCaps *caps;
248
249 osssrc = GST_OSS_SRC (bsrc);
250
251 if (osssrc->fd == -1) {
252 GST_DEBUG_OBJECT (osssrc, "device not open, using template caps");
253 return NULL; /* base class will get template caps for us */
254 }
255
256 if (osssrc->probed_caps) {
257 GST_LOG_OBJECT (osssrc, "Returning cached caps");
258 return gst_caps_ref (osssrc->probed_caps);
259 }
260
261 caps = gst_oss_helper_probe_caps (osssrc->fd);
262
263 if (caps) {
264 osssrc->probed_caps = gst_caps_ref (caps);
265 }
266
267 GST_INFO_OBJECT (osssrc, "returning caps %" GST_PTR_FORMAT, caps);
268
269 if (filter && caps) {
270 GstCaps *intersection;
271
272 intersection =
273 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
274 gst_caps_unref (caps);
275 return intersection;
276 } else {
277 return caps;
278 }
279 }
280
281 static gint
ilog2(gint x)282 ilog2 (gint x)
283 {
284 /* well... hacker's delight explains... */
285 x = x | (x >> 1);
286 x = x | (x >> 2);
287 x = x | (x >> 4);
288 x = x | (x >> 8);
289 x = x | (x >> 16);
290 x = x - ((x >> 1) & 0x55555555);
291 x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
292 x = (x + (x >> 4)) & 0x0f0f0f0f;
293 x = x + (x >> 8);
294 x = x + (x >> 16);
295 return (x & 0x0000003f) - 1;
296 }
297
298 static gint
gst_oss_src_get_format(GstAudioRingBufferFormatType fmt,GstAudioFormat rfmt)299 gst_oss_src_get_format (GstAudioRingBufferFormatType fmt, GstAudioFormat rfmt)
300 {
301 gint result;
302
303 switch (fmt) {
304 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW:
305 result = AFMT_MU_LAW;
306 break;
307 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW:
308 result = AFMT_A_LAW;
309 break;
310 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_IMA_ADPCM:
311 result = AFMT_IMA_ADPCM;
312 break;
313 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MPEG:
314 result = AFMT_MPEG;
315 break;
316 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW:
317 {
318 switch (rfmt) {
319 case GST_AUDIO_FORMAT_U8:
320 result = AFMT_U8;
321 break;
322 case GST_AUDIO_FORMAT_S16LE:
323 result = AFMT_S16_LE;
324 break;
325 case GST_AUDIO_FORMAT_S16BE:
326 result = AFMT_S16_BE;
327 break;
328 case GST_AUDIO_FORMAT_S8:
329 result = AFMT_S8;
330 break;
331 case GST_AUDIO_FORMAT_U16LE:
332 result = AFMT_U16_LE;
333 break;
334 case GST_AUDIO_FORMAT_U16BE:
335 result = AFMT_U16_BE;
336 break;
337 default:
338 result = 0;
339 break;
340 }
341 break;
342 }
343 default:
344 result = 0;
345 break;
346 }
347 return result;
348 }
349
350 static gboolean
gst_oss_src_open(GstAudioSrc * asrc)351 gst_oss_src_open (GstAudioSrc * asrc)
352 {
353 GstOssSrc *oss;
354 int mode;
355
356 oss = GST_OSS_SRC (asrc);
357
358 mode = O_RDONLY;
359 mode |= O_NONBLOCK;
360
361 oss->fd = open (oss->device, mode, 0);
362 if (oss->fd == -1) {
363 switch (errno) {
364 case EACCES:
365 goto no_permission;
366 default:
367 goto open_failed;
368 }
369 }
370
371 g_free (oss->device_name);
372 oss->device_name = gst_oss_helper_get_card_name ("/dev/mixer");
373
374 return TRUE;
375
376 no_permission:
377 {
378 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
379 (_("Could not open audio device for recording. "
380 "You don't have permission to open the device.")),
381 GST_ERROR_SYSTEM);
382 return FALSE;
383 }
384 open_failed:
385 {
386 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
387 (_("Could not open audio device for recording.")),
388 ("Unable to open device %s for recording: %s",
389 oss->device, g_strerror (errno)));
390 return FALSE;
391 }
392 }
393
394 static gboolean
gst_oss_src_close(GstAudioSrc * asrc)395 gst_oss_src_close (GstAudioSrc * asrc)
396 {
397 GstOssSrc *oss;
398
399 oss = GST_OSS_SRC (asrc);
400
401 close (oss->fd);
402
403 gst_caps_replace (&oss->probed_caps, NULL);
404
405 return TRUE;
406 }
407
408 static gboolean
gst_oss_src_prepare(GstAudioSrc * asrc,GstAudioRingBufferSpec * spec)409 gst_oss_src_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
410 {
411 GstOssSrc *oss;
412 struct audio_buf_info info;
413 int mode;
414 int fmt, tmp;
415 guint width, rate, channels;
416
417 oss = GST_OSS_SRC (asrc);
418
419 mode = fcntl (oss->fd, F_GETFL);
420 mode &= ~O_NONBLOCK;
421 if (fcntl (oss->fd, F_SETFL, mode) == -1)
422 goto non_block;
423
424 fmt = gst_oss_src_get_format (spec->type,
425 GST_AUDIO_INFO_FORMAT (&spec->info));
426 if (fmt == 0)
427 goto wrong_format;
428
429 width = GST_AUDIO_INFO_WIDTH (&spec->info);
430 rate = GST_AUDIO_INFO_RATE (&spec->info);
431 channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
432
433 if (width != 16 && width != 8)
434 goto dodgy_width;
435
436 tmp = ilog2 (spec->segsize);
437 tmp = ((spec->segtotal & 0x7fff) << 16) | tmp;
438 GST_DEBUG_OBJECT (oss, "set segsize: %d, segtotal: %d, value: %08x",
439 spec->segsize, spec->segtotal, tmp);
440
441 SET_PARAM (oss, SNDCTL_DSP_SETFRAGMENT, tmp, "SETFRAGMENT");
442
443 SET_PARAM (oss, SNDCTL_DSP_RESET, 0, "RESET");
444
445 SET_PARAM (oss, SNDCTL_DSP_SETFMT, fmt, "SETFMT");
446 if (channels == 2)
447 SET_PARAM (oss, SNDCTL_DSP_STEREO, 1, "STEREO");
448 SET_PARAM (oss, SNDCTL_DSP_CHANNELS, channels, "CHANNELS");
449 SET_PARAM (oss, SNDCTL_DSP_SPEED, rate, "SPEED");
450
451 GET_PARAM (oss, SNDCTL_DSP_GETISPACE, &info, "GETISPACE");
452
453 spec->segsize = info.fragsize;
454 spec->segtotal = info.fragstotal;
455
456 oss->bytes_per_sample = GST_AUDIO_INFO_BPF (&spec->info);
457
458 GST_DEBUG_OBJECT (oss, "got segsize: %d, segtotal: %d, value: %08x",
459 spec->segsize, spec->segtotal, tmp);
460
461 return TRUE;
462
463 non_block:
464 {
465 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
466 ("Unable to set device %s in non blocking mode: %s",
467 oss->device, g_strerror (errno)), (NULL));
468 return FALSE;
469 }
470 wrong_format:
471 {
472 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
473 ("Unable to get format (%d, %d)", spec->type,
474 GST_AUDIO_INFO_FORMAT (&spec->info)), (NULL));
475 return FALSE;
476 }
477 dodgy_width:
478 {
479 GST_ELEMENT_ERROR (oss, RESOURCE, OPEN_READ,
480 ("Unexpected width %d", width), (NULL));
481 return FALSE;
482 }
483 }
484
485 static gboolean
gst_oss_src_unprepare(GstAudioSrc * asrc)486 gst_oss_src_unprepare (GstAudioSrc * asrc)
487 {
488 /* could do a SNDCTL_DSP_RESET, but the OSS manual recommends a close/open */
489
490 if (!gst_oss_src_close (asrc))
491 goto couldnt_close;
492
493 if (!gst_oss_src_open (asrc))
494 goto couldnt_reopen;
495
496 return TRUE;
497
498 couldnt_close:
499 {
500 GST_DEBUG_OBJECT (asrc, "Could not close the audio device");
501 return FALSE;
502 }
503 couldnt_reopen:
504 {
505 GST_DEBUG_OBJECT (asrc, "Could not reopen the audio device");
506 return FALSE;
507 }
508 }
509
510 static guint
gst_oss_src_read(GstAudioSrc * asrc,gpointer data,guint length,GstClockTime * timestamp)511 gst_oss_src_read (GstAudioSrc * asrc, gpointer data, guint length,
512 GstClockTime * timestamp)
513 {
514 return read (GST_OSS_SRC (asrc)->fd, data, length);
515 }
516
517 static guint
gst_oss_src_delay(GstAudioSrc * asrc)518 gst_oss_src_delay (GstAudioSrc * asrc)
519 {
520 GstOssSrc *oss;
521 gint delay = 0;
522 gint ret;
523
524 oss = GST_OSS_SRC (asrc);
525
526 #ifdef SNDCTL_DSP_GETODELAY
527 ret = ioctl (oss->fd, SNDCTL_DSP_GETODELAY, &delay);
528 #else
529 ret = -1;
530 #endif
531 if (ret < 0) {
532 audio_buf_info info;
533
534 ret = ioctl (oss->fd, SNDCTL_DSP_GETOSPACE, &info);
535
536 delay = (ret < 0 ? 0 : (info.fragstotal * info.fragsize) - info.bytes);
537 }
538 return delay / oss->bytes_per_sample;
539 }
540
541 static void
gst_oss_src_reset(GstAudioSrc * asrc)542 gst_oss_src_reset (GstAudioSrc * asrc)
543 {
544 /* There's nothing we can do here really: OSS can't handle access to the
545 * same device/fd from multiple threads and might deadlock or blow up in
546 * other ways if we try an ioctl SNDCTL_DSP_RESET or similar */
547 }
548