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