implement bitrate property setters
[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 static gint64 gst_dreamaudiosource_get_base_pts (GstDreamAudioSource *self);
65
66 static void
67 gst_dreamaudiosource_class_init (GstDreamAudioSourceClass * klass)
68 {
69         GObjectClass *gobject_class;
70         GstElementClass *gstelement_class;
71         GstBaseSrcClass *gstbasesrc_class;
72         GstPushSrcClass *gstpush_src_class;
73
74         gobject_class = (GObjectClass *) klass;
75         gstelement_class = (GstElementClass *) klass;
76         gstbasesrc_class = (GstBaseSrcClass *) klass;
77         gstpush_src_class = (GstPushSrcClass *) klass;
78
79         gobject_class->set_property = gst_dreamaudiosource_set_property;
80         gobject_class->get_property = gst_dreamaudiosource_get_property;
81         gobject_class->finalize = gst_dreamaudiosource_finalize;
82
83         gst_element_class_add_pad_template (gstelement_class,
84                                             gst_static_pad_template_get (&srctemplate));
85
86         gst_element_class_set_static_metadata (gstelement_class,
87             "Dream Audio source", "Source/Audio",
88             "Provide an audio elementary stream from Dreambox encoder device",
89             "Andreas Frisch <fraxinas@opendreambox.org>");
90
91         gstbasesrc_class->get_caps = gst_dreamaudiosource_getcaps;
92         gstbasesrc_class->start = gst_dreamaudiosource_start;
93         gstbasesrc_class->stop = gst_dreamaudiosource_stop;
94
95         gstpush_src_class->create = gst_dreamaudiosource_create;
96
97         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
98           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
99             "Bitrate in kbit/sec", 16, 320, DEFAULT_BITRATE,
100             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
101         
102         gst_dreamaudiosource_signals[SIGNAL_GET_BASE_PTS] =
103                 g_signal_new ("get-base-pts",
104                 G_TYPE_FROM_CLASS (klass),
105                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
106                 G_STRUCT_OFFSET (GstDreamAudioSourceClass, get_base_pts),
107                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
108
109         klass->get_base_pts = gst_dreamaudiosource_get_base_pts;
110
111 }
112
113 static gint64
114 gst_dreamaudiosource_get_base_pts (GstDreamAudioSource *self)
115 {
116         GST_DEBUG_OBJECT (self, "gst_dreamaudiosource_get_base_pts " GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
117         return self->base_pts;
118 }
119
120 gboolean
121 gst_dreamaudiosource_plugin_init (GstPlugin *plugin)
122 {
123         GST_DEBUG_CATEGORY_INIT (dreamaudiosource_debug, "dreamaudiosource", 0, "dreamaudiosource");
124         return gst_element_register (plugin, "dreamaudiosource", GST_RANK_PRIMARY, GST_TYPE_DREAMAUDIOSOURCE);
125 }
126
127 static void
128 gst_dreamaudiosource_init (GstDreamAudioSource * self)
129 {
130         self->encoder = NULL;
131         self->descriptors_available = 0;
132         self->base_pts = GST_CLOCK_TIME_NONE;
133         g_mutex_init (&self->mutex);
134         
135         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
136         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
137 }
138
139 static void gst_dreamaudiosource_set_bitrate (GstDreamAudioSource * self, uint32_t bitrate)
140 {
141         if (!self->encoder || !self->encoder->fd)
142                 return;
143         uint32_t abr = bitrate*1000;
144         int ret = ioctl(self->encoder->fd, AENC_SET_BITRATE, &abr);
145         if (ret != 0)
146         {
147                 GST_WARNING_OBJECT (self, "can't set audio bitrate to %i bytes/s!", abr);
148                 return;
149         }
150         GST_INFO_OBJECT (self, "set audio bitrate to %i kBytes/s", bitrate);
151         self->audio_info.bitrate = abr;
152 }
153
154 static void
155 gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
156 {
157         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
158         
159         switch (prop_id) {
160                 case ARG_BITRATE:
161                         gst_dreamaudiosource_set_bitrate(self, g_value_get_int (value));
162                         break;
163                 default:
164                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
165                         break;
166         }
167 }
168
169 static void
170 gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
171 {
172         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
173         
174         switch (prop_id) {
175                 case ARG_BITRATE:
176                         g_value_set_int (value, self->audio_info.bitrate/1000);
177                         break;
178                 default:
179                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
180                         break;
181         }
182 }
183
184 static GstCaps *
185 gst_dreamaudiosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
186 {
187         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
188         GstPadTemplate *pad_template;
189         GstCaps *caps;
190
191         pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
192         g_return_val_if_fail (pad_template != NULL, NULL);
193
194         if (self->encoder == NULL) {
195                 GST_DEBUG_OBJECT (self, "encoder not opened -> use template caps");
196                 caps = gst_pad_template_get_caps (pad_template);
197         }
198         else
199         {
200                 caps = gst_caps_make_writable(gst_pad_template_get_caps (pad_template));
201         }
202
203         GST_INFO_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
204         return caps;
205 }
206
207 static void
208 gst_dreamaudiosource_free_buffer (GstDreamAudioSource * self)
209 {
210         GST_LOG_OBJECT (self, "gst_dreamaudiosource_free_buffer");
211 }
212
213 static GstFlowReturn
214 gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
215 {
216         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (psrc);
217         EncoderInfo *enc = self->encoder;
218         static int dumpoffset;
219
220         GST_LOG_OBJECT (self, "new buffer requested");
221
222         if (!enc) {
223                 GST_WARNING_OBJECT (self, "encoder device not opened!");
224                 return GST_FLOW_ERROR;
225         }
226
227         
228         while (1)
229         {
230                 *outbuf = NULL;
231                 
232                 if (self->descriptors_available == 0)
233                 {
234                         self->descriptors_count = 0;
235                         int rlen = read(enc->fd, enc->buffer, ABUFSIZE);
236                         if (rlen <= 0 || rlen % ABDSIZE ) {
237                                 GST_WARNING_OBJECT (self, "read error %d (errno %i)", rlen, errno);
238                                 return GST_FLOW_ERROR;
239                         }
240                         self->descriptors_available = rlen / ABDSIZE;
241                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
242                 }
243
244                 while (self->descriptors_count < self->descriptors_available) {
245                         off_t offset = self->descriptors_count * ABDSIZE;
246                         AudioBufferDescriptor *desc = (AudioBufferDescriptor*)(&enc->buffer[offset]);
247
248                         uint32_t f = desc->stCommon.uiFlags;
249
250                         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);
251
252                         if (f & CDB_FLAG_METADATA) { 
253                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
254                                 self->descriptors_count = self->descriptors_available;
255                                 continue;
256                         }
257                         
258                         *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);
259                         
260                         if (f & CDB_FLAG_PTS_VALID)
261                         {
262                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
263                                 {
264                                         if (self->dreamvideosrc)
265                                         {
266                                                 g_mutex_lock (&self->mutex);
267                                                 guint64 videosource_base_pts;
268                                                 g_signal_emit_by_name(self->dreamvideosrc, "get-base-pts", &videosource_base_pts);
269                                                 if (videosource_base_pts != GST_CLOCK_TIME_NONE)
270                                                 {
271                                                         GST_DEBUG_OBJECT (self, "use DREAMVIDEOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (videosource_base_pts) );
272                                                         self->base_pts = videosource_base_pts;
273                                                 }
274                                                 g_mutex_unlock (&self->mutex);
275                                         }
276                                         else
277                                         {
278                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
279                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
280                                         }
281                                 }
282                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
283                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
284                                 {
285                                         buffer_time -= self->base_pts;
286                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
287                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
288                                 }
289                                 
290                         }
291                         
292 #ifdef dump
293                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
294                         dumpoffset += wret;
295                         GST_LOG_OBJECT (self, "read %"G_GSIZE_FORMAT" dumped %i total 0x%08X", desc->stCommon.uiLength, wret, dumpoffset );
296 #endif
297                         self->descriptors_count++;
298
299                         break;
300                 }
301
302                 if (self->descriptors_count == self->descriptors_available) {
303                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
304                         /* release consumed descs */
305                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
306                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
307                                 return GST_FLOW_ERROR;
308                         }
309                         self->descriptors_available = 0;
310                 }
311
312                 if (*outbuf)
313                 {
314                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
315                         return GST_FLOW_OK;
316                 }
317         }
318
319         return GST_FLOW_ERROR;
320 }
321
322 static gboolean
323 gst_dreamaudiosource_start (GstBaseSrc * bsrc)
324 {
325         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
326
327
328         char fn_buf[32];
329
330         self->encoder = malloc(sizeof(EncoderInfo));
331
332         if(!self->encoder) {
333                 GST_ERROR_OBJECT(self,"out of space");
334                 return FALSE;
335         }
336
337         sprintf(fn_buf, "/dev/aenc%d", 0);
338         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
339         if(self->encoder->fd <= 0) {
340                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
341                 free(self->encoder);
342                 self->encoder = NULL;
343                 return FALSE;
344         }
345
346         self->encoder->buffer = malloc(ABUFSIZE);
347         if(!self->encoder->buffer) {
348                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
349                 return FALSE;
350         }
351
352         self->encoder->cdb = (unsigned char *)mmap(0, AMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
353
354         if(!self->encoder->cdb || self->encoder->cdb== MAP_FAILED) {
355                 GST_ERROR_OBJECT(self,"cannot mmap cdb: %s (%d)", strerror(errno));
356                 return FALSE;
357         }
358 #ifdef dump
359         self->dumpfd = open("/media/hdd/movie/dreamaudiosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
360         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
361 #endif
362         
363         GST_DEBUG_OBJECT (self, "cdb buffer mapped to %08x", self->encoder->cdb);
364         
365         gst_dreamaudiosource_set_bitrate(self, DEFAULT_BITRATE);
366         
367         int rlen = 0;
368         do {
369                 rlen = read(self->encoder->fd, self->encoder->buffer, ABUFSIZE);
370                 GST_DEBUG_OBJECT (self, "flushed %d bytes", rlen);
371         } while (rlen > 0);
372         
373         int ret = ioctl(self->encoder->fd, AENC_START);
374         if ( ret != 0 )
375         {
376                 GST_ERROR_OBJECT(self,"can't start encoder ioctl!");
377                 return FALSE;
378         }
379         GST_INFO_OBJECT (self, "started encoder!");
380         
381         self->dreamvideosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamvideosource0");
382         
383         return TRUE;
384 }
385
386 static gboolean
387 gst_dreamaudiosource_stop (GstBaseSrc * bsrc)
388 {
389         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
390         if (self->encoder) {
391                 if (self->encoder->fd > 0)
392                         ioctl(self->encoder->fd, AENC_STOP);
393                 close(self->encoder->fd);
394         }
395 #ifdef dump
396         close(self->dumpfd);
397 #endif
398         GST_DEBUG_OBJECT (self, "closed");
399         return TRUE;
400 }
401
402 static void
403 gst_dreamaudiosource_finalize (GObject * gobject)
404 {
405         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (gobject);
406         if (self->encoder) {
407                 if (self->encoder->buffer)
408                         free(self->encoder->buffer);
409                 if (self->encoder->cdb) 
410                         munmap(self->encoder->cdb, AMMAPSIZE);
411                 free(self->encoder);
412         }
413         g_mutex_clear (&self->mutex);
414         GST_DEBUG_OBJECT (self, "finalized");
415         G_OBJECT_CLASS (parent_class)->finalize (gobject);
416 }