whitespace cleanup
[gst-plugin-dreamsource.git] / src / gstdreamaudiosource.c
1 /*
2  * GStreamer dreamaudiosource
3  * Copyright 2014-2015 Andreas Frisch <fraxinas@opendreambox.org>
4  *
5  * This program is licensed under the Creative Commons
6  * Attribution-NonCommercial-ShareAlike 3.0 Unported
7  * License. To view a copy of this license, visit
8  * http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
9  * Creative Commons,559 Nathan Abbott Way,Stanford,California 94305,USA.
10  *
11  * Alternatively, this program may be distributed and executed on
12  * hardware which is licensed by Dream Multimedia GmbH.
13  *
14  * This program is NOT free software. It is open source, you are allowed
15  * to modify it (if you keep the license), but it may not be commercially
16  * distributed other than under the conditions noted above.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <gst/gst.h>
24 #include "gstdreamaudiosource.h"
25
26 GST_DEBUG_CATEGORY_STATIC (dreamaudiosource_debug);
27 #define GST_CAT_DEFAULT dreamaudiosource_debug
28
29 enum
30 {
31         SIGNAL_GET_BASE_PTS,
32         LAST_SIGNAL
33 };
34 enum
35 {
36         ARG_0,
37         ARG_BITRATE,
38 };
39
40 static guint gst_dreamaudiosource_signals[LAST_SIGNAL] = { 0 };
41
42 #define DEFAULT_BITRATE 128
43
44 static GstStaticPadTemplate srctemplate =
45     GST_STATIC_PAD_TEMPLATE ("src",
46         GST_PAD_SRC,
47         GST_PAD_ALWAYS,
48         GST_STATIC_CAPS ("audio/mpeg, "
49         "mpegversion = 4,"
50         "stream-format = (string) adts")
51     );
52
53 #define gst_dreamaudiosource_parent_class parent_class
54 G_DEFINE_TYPE (GstDreamAudioSource, gst_dreamaudiosource, GST_TYPE_PUSH_SRC);
55
56 static GstCaps *gst_dreamaudiosource_getcaps (GstBaseSrc * psrc, GstCaps * filter);
57 static gboolean gst_dreamaudiosource_start (GstBaseSrc * bsrc);
58 static gboolean gst_dreamaudiosource_stop (GstBaseSrc * bsrc);
59 static void gst_dreamaudiosource_finalize (GObject * gobject);
60 static GstFlowReturn gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf);
61
62 static void gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
63 static void gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
64
65 static GstStateChangeReturn gst_dreamaudiosource_change_state (GstElement * element, GstStateChange transition);
66 static gint64 gst_dreamaudiosource_get_base_pts (GstDreamAudioSource *self);
67
68 static void
69 gst_dreamaudiosource_class_init (GstDreamAudioSourceClass * klass)
70 {
71         GObjectClass *gobject_class;
72         GstElementClass *gstelement_class;
73         GstBaseSrcClass *gstbasesrc_class;
74         GstPushSrcClass *gstpush_src_class;
75
76         gobject_class = (GObjectClass *) klass;
77         gstelement_class = (GstElementClass *) klass;
78         gstbasesrc_class = (GstBaseSrcClass *) klass;
79         gstpush_src_class = (GstPushSrcClass *) klass;
80
81         gobject_class->set_property = gst_dreamaudiosource_set_property;
82         gobject_class->get_property = gst_dreamaudiosource_get_property;
83         gobject_class->finalize = gst_dreamaudiosource_finalize;
84
85         gst_element_class_add_pad_template (gstelement_class,
86                                             gst_static_pad_template_get (&srctemplate));
87
88         gst_element_class_set_static_metadata (gstelement_class,
89             "Dream Audio source", "Source/Audio",
90             "Provide an audio elementary stream from Dreambox encoder device",
91             "Andreas Frisch <fraxinas@opendreambox.org>");
92
93         gstelement_class->change_state = gst_dreamaudiosource_change_state;
94
95         gstbasesrc_class->get_caps = gst_dreamaudiosource_getcaps;
96         gstbasesrc_class->start = gst_dreamaudiosource_start;
97         gstbasesrc_class->stop = gst_dreamaudiosource_stop;
98
99         gstpush_src_class->create = gst_dreamaudiosource_create;
100
101         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
102           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
103             "Bitrate in kbit/sec", 16, 320, DEFAULT_BITRATE,
104             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
105
106         gst_dreamaudiosource_signals[SIGNAL_GET_BASE_PTS] =
107                 g_signal_new ("get-base-pts",
108                 G_TYPE_FROM_CLASS (klass),
109                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
110                 G_STRUCT_OFFSET (GstDreamAudioSourceClass, get_base_pts),
111                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
112
113         klass->get_base_pts = gst_dreamaudiosource_get_base_pts;
114
115 }
116
117 static gint64
118 gst_dreamaudiosource_get_base_pts (GstDreamAudioSource *self)
119 {
120         GST_DEBUG_OBJECT (self, "gst_dreamaudiosource_get_base_pts " GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
121         return self->base_pts;
122 }
123
124 static void gst_dreamaudiosource_set_bitrate (GstDreamAudioSource * self, uint32_t bitrate)
125 {
126         if (!self->encoder || !self->encoder->fd)
127                 return;
128         uint32_t abr = bitrate*1000;
129         int ret = ioctl(self->encoder->fd, AENC_SET_BITRATE, &abr);
130         if (ret != 0)
131         {
132                 GST_WARNING_OBJECT (self, "can't set audio bitrate to %i bytes/s!", abr);
133                 return;
134         }
135         GST_INFO_OBJECT (self, "set audio bitrate to %i kBytes/s", bitrate);
136         self->audio_info.bitrate = abr;
137 }
138
139 gboolean
140 gst_dreamaudiosource_plugin_init (GstPlugin *plugin)
141 {
142         GST_DEBUG_CATEGORY_INIT (dreamaudiosource_debug, "dreamaudiosource", 0, "dreamaudiosource");
143         return gst_element_register (plugin, "dreamaudiosource", GST_RANK_PRIMARY, GST_TYPE_DREAMAUDIOSOURCE);
144 }
145
146 static void
147 gst_dreamaudiosource_init (GstDreamAudioSource * self)
148 {
149         self->encoder = NULL;
150         self->descriptors_available = 0;
151         g_mutex_init (&self->mutex);
152
153         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
154         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
155
156         self->encoder = malloc(sizeof(EncoderInfo));
157
158         if(!self->encoder) {
159                 GST_ERROR_OBJECT(self,"out of space");
160                 return;
161         }
162
163         char fn_buf[32];
164         sprintf(fn_buf, "/dev/aenc%d", 0);
165         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
166         if(self->encoder->fd <= 0) {
167                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
168                 free(self->encoder);
169                 self->encoder = NULL;
170                 return;
171         }
172
173         self->encoder->buffer = malloc(ABUFSIZE);
174         if(!self->encoder->buffer) {
175                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
176                 return;
177         }
178
179         self->encoder->cdb = (unsigned char *)mmap(0, AMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
180         if(!self->encoder->cdb || self->encoder->cdb== MAP_FAILED) {
181                 GST_ERROR_OBJECT(self,"cannot mmap cdb: %s (%d)", strerror(errno));
182                 return;
183         }
184
185 #ifdef dump
186         self->dumpfd = open("/media/hdd/movie/dreamaudiosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
187         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
188 #endif
189
190         gst_dreamaudiosource_set_bitrate(self, DEFAULT_BITRATE);
191 }
192
193 static void
194 gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
195 {
196         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
197
198         switch (prop_id) {
199                 case ARG_BITRATE:
200                         gst_dreamaudiosource_set_bitrate(self, g_value_get_int (value));
201                         break;
202                 default:
203                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
204                         break;
205         }
206 }
207
208 static void
209 gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
210 {
211         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
212
213         switch (prop_id) {
214                 case ARG_BITRATE:
215                         g_value_set_int (value, self->audio_info.bitrate/1000);
216                         break;
217                 default:
218                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219                         break;
220         }
221 }
222
223 static GstCaps *
224 gst_dreamaudiosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
225 {
226         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
227         GstPadTemplate *pad_template;
228         GstCaps *caps;
229
230         pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
231         g_return_val_if_fail (pad_template != NULL, NULL);
232
233         if (self->encoder == NULL) {
234                 GST_DEBUG_OBJECT (self, "encoder not opened -> use template caps");
235                 caps = gst_pad_template_get_caps (pad_template);
236         }
237         else
238         {
239                 caps = gst_caps_make_writable(gst_pad_template_get_caps (pad_template));
240         }
241
242         GST_DEBUG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
243         return caps;
244 }
245
246 static void
247 gst_dreamaudiosource_free_buffer (GstDreamAudioSource * self)
248 {
249         GST_LOG_OBJECT (self, "gst_dreamaudiosource_free_buffer");
250 }
251
252 static GstFlowReturn
253 gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
254 {
255         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (psrc);
256         EncoderInfo *enc = self->encoder;
257         static int dumpoffset;
258
259         GST_LOG_OBJECT (self, "new buffer requested");
260
261         if (!enc) {
262                 GST_WARNING_OBJECT (self, "encoder device not opened!");
263                 return GST_FLOW_ERROR;
264         }
265
266         while (1)
267         {
268                 *outbuf = NULL;
269
270                 if (self->descriptors_available == 0)
271                 {
272                         self->descriptors_count = 0;
273                         int rlen = read(enc->fd, enc->buffer, ABUFSIZE);
274                         if (rlen <= 0 || rlen % ABDSIZE ) {
275                                 if ( errno == 512 )
276                                         return GST_FLOW_FLUSHING;
277                                 GST_WARNING_OBJECT (self, "read error %s", strerror(errno));
278                                 return GST_FLOW_ERROR;
279                         }
280                         self->descriptors_available = rlen / ABDSIZE;
281                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
282                 }
283
284                 while (self->descriptors_count < self->descriptors_available) {
285                         off_t offset = self->descriptors_count * ABDSIZE;
286                         AudioBufferDescriptor *desc = (AudioBufferDescriptor*)(&enc->buffer[offset]);
287
288                         uint32_t f = desc->stCommon.uiFlags;
289
290                         GST_LOG_OBJECT (self, "descriptors_count=%d, descriptors_available=%u\tuiOffset=%u, uiLength=%"G_GSIZE_FORMAT"", self->descriptors_count, self->descriptors_available, desc->stCommon.uiOffset, desc->stCommon.uiLength);
291
292                         if (f & CDB_FLAG_METADATA) {
293                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
294                                 self->descriptors_count = self->descriptors_available;
295                                 continue;
296                         }
297
298                         *outbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, AMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, self, (GDestroyNotify)gst_dreamaudiosource_free_buffer);
299
300                         if (f & CDB_FLAG_PTS_VALID)
301                         {
302                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
303                                 {
304                                         if (self->dreamvideosrc)
305                                         {
306                                                 g_mutex_lock (&self->mutex);
307                                                 guint64 videosource_base_pts;
308                                                 g_signal_emit_by_name(self->dreamvideosrc, "get-base-pts", &videosource_base_pts);
309                                                 if (videosource_base_pts != GST_CLOCK_TIME_NONE)
310                                                 {
311                                                         GST_DEBUG_OBJECT (self, "use DREAMVIDEOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (videosource_base_pts) );
312                                                         self->base_pts = videosource_base_pts;
313                                                 }
314                                                 g_mutex_unlock (&self->mutex);
315                                         }
316                                         else
317                                         {
318                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
319                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
320                                         }
321                                 }
322                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
323                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
324                                 {
325                                         buffer_time -= self->base_pts;
326                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
327                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
328                                 }
329                         }
330
331 #ifdef dump
332                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
333                         dumpoffset += wret;
334                         GST_LOG_OBJECT (self, "read %"G_GSIZE_FORMAT" dumped %i total 0x%08X", desc->stCommon.uiLength, wret, dumpoffset );
335 #endif
336                         self->descriptors_count++;
337
338                         break;
339                 }
340
341                 if (self->descriptors_count == self->descriptors_available) {
342                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
343                         /* release consumed descs */
344                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
345                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
346                                 return GST_FLOW_ERROR;
347                         }
348                         self->descriptors_available = 0;
349                 }
350
351                 if (*outbuf)
352                 {
353                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
354                         return GST_FLOW_OK;
355                 }
356         }
357
358         return GST_FLOW_ERROR;
359 }
360
361 static GstStateChangeReturn gst_dreamaudiosource_change_state (GstElement * element, GstStateChange transition)
362 {
363         g_return_val_if_fail (element, GST_STATE_CHANGE_FAILURE);
364         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (element);
365         int ret;
366
367         switch (transition) {
368                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
369                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
370                         self->base_pts = GST_CLOCK_TIME_NONE;
371                         ret = ioctl(self->encoder->fd, AENC_START);
372                         if ( ret != 0 )
373                         {
374                                 GST_ERROR_OBJECT(self,"can't start encoder ioctl!");
375                                 return GST_STATE_CHANGE_FAILURE;
376                         }
377                         GST_INFO_OBJECT (self, "started encoder!");
378                         break;
379                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
380                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED");
381                         ret = ioctl(self->encoder->fd, AENC_STOP);
382                         if ( ret != 0 )
383                         {
384                                 GST_ERROR_OBJECT(self,"can't stop encoder ioctl!");
385                                 return GST_STATE_CHANGE_FAILURE;
386                         }
387                         GST_INFO_OBJECT (self, "stopped encoder!");
388                         break;
389                 default:
390                         break;
391         }
392
393         if (GST_ELEMENT_CLASS (parent_class)->change_state)
394                 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
395
396         return GST_STATE_CHANGE_SUCCESS;
397 }
398
399 static gboolean
400 gst_dreamaudiosource_start (GstBaseSrc * bsrc)
401 {
402         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
403         self->dreamvideosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamvideosource0");
404         self->base_pts = GST_CLOCK_TIME_NONE;
405         GST_DEBUG_OBJECT (self, "started. reference to dreamvideosource=%" GST_PTR_FORMAT"", self->dreamvideosrc);
406         return TRUE;
407 }
408
409 static gboolean
410 gst_dreamaudiosource_stop (GstBaseSrc * bsrc)
411 {
412         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
413         GST_DEBUG_OBJECT (self, "closed");
414         return TRUE;
415 }
416
417 static void
418 gst_dreamaudiosource_finalize (GObject * gobject)
419 {
420         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (gobject);
421         if (self->encoder) {
422                 if (self->encoder->buffer)
423                         free(self->encoder->buffer);
424                 if (self->encoder->cdb)
425                         munmap(self->encoder->cdb, AMMAPSIZE);
426                 free(self->encoder);
427         }
428 #ifdef dump
429         close(self->dumpfd);
430 #endif
431         g_mutex_clear (&self->mutex);
432         GST_DEBUG_OBJECT (self, "finalized");
433         G_OBJECT_CLASS (parent_class)->finalize (gobject);
434 }