use background encoding as default input-mode, add missing caps initialization
[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 Property 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 GType gst_dreamaudiosource_input_mode_get_type (void)
30 {
31         static volatile gsize input_mode_type = 0;
32         static const GEnumValue input_mode[] = {
33                 {GST_DREAMAUDIOSOURCE_INPUT_MODE_LIVE, "GST_DREAMAUDIOSOURCE_INPUT_MODE_LIVE", "live"},
34                 {GST_DREAMAUDIOSOURCE_INPUT_MODE_HDMI_IN, "GST_DREAMAUDIOSOURCE_INPUT_MODE_HDMI_IN", "hdmi_in"},
35                 {GST_DREAMAUDIOSOURCE_INPUT_MODE_BACKGROUND, "GST_DREAMAUDIOSOURCE_INPUT_MODE_BACKGROUND", "background"},
36                 {0, NULL, NULL},
37         };
38
39         if (g_once_init_enter (&input_mode_type)) {
40                 GType tmp = g_enum_register_static ("GstDreamAudioSourceInputMode", input_mode);
41                 g_once_init_leave (&input_mode_type, tmp);
42         }
43         return (GType) input_mode_type;
44 }
45
46 enum
47 {
48         SIGNAL_GET_DTS_OFFSET,
49         LAST_SIGNAL
50 };
51 enum
52 {
53         ARG_0,
54         ARG_BITRATE,
55         ARG_INPUT_MODE
56 };
57
58 static guint gst_dreamaudiosource_signals[LAST_SIGNAL] = { 0 };
59
60 #define DEFAULT_BITRATE     128
61 #define DEFAULT_SAMPLERATE  48000
62 #define DEFAULT_INPUT_MODE  GST_DREAMAUDIOSOURCE_INPUT_MODE_BACKGROUND
63 #define DEFAULT_BUFFER_SIZE 80
64
65 static GstStaticPadTemplate srctemplate =
66     GST_STATIC_PAD_TEMPLATE ("src",
67         GST_PAD_SRC,
68         GST_PAD_ALWAYS,
69         GST_STATIC_CAPS ("audio/mpeg, "
70         "mpegversion = 4,"
71         "stream-format = (string) adts,"
72         "rate = 48000")
73     );
74
75 #define gst_dreamaudiosource_parent_class parent_class
76 G_DEFINE_TYPE (GstDreamAudioSource, gst_dreamaudiosource, GST_TYPE_PUSH_SRC);
77
78 static GstCaps *gst_dreamaudiosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
79 static gboolean gst_dreamaudiosource_unlock (GstBaseSrc * bsrc);
80 static gboolean gst_dreamaudiosource_unlock_stop (GstBaseSrc * bsrc);
81 static gboolean gst_dreamaudiosource_query (GstBaseSrc * bsrc, GstQuery * query);
82
83 static void gst_dreamaudiosource_dispose (GObject * gobject);
84 static GstFlowReturn gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf);
85
86 static void gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
87 static void gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
88
89 static GstStateChangeReturn gst_dreamaudiosource_change_state (GstElement * element, GstStateChange transition);
90 static gint64 gst_dreamaudiosource_get_dts_offset (GstDreamAudioSource *self);
91
92 static gboolean gst_dreamaudiosource_encoder_init (GstDreamAudioSource * self);
93 static void gst_dreamaudiosource_encoder_release (GstDreamAudioSource * self);
94
95 static void gst_dreamaudiosource_read_thread_func (GstDreamAudioSource * self);
96
97 #ifdef PROVIDE_CLOCK
98 static GstClock *gst_dreamaudiosource_provide_clock (GstElement * elem);
99 // static GstClockTime gst_dreamaudiosource_get_encoder_time_ (GstClock * clock, GstBaseSrc * bsrc);
100 #endif
101
102 static void
103 gst_dreamaudiosource_class_init (GstDreamAudioSourceClass * klass)
104 {
105         GObjectClass *gobject_class;
106         GstElementClass *gstelement_class;
107         GstBaseSrcClass *gstbasesrc_class;
108         GstPushSrcClass *gstpush_src_class;
109
110         gobject_class = (GObjectClass *) klass;
111         gstelement_class = (GstElementClass *) klass;
112         gstbasesrc_class = (GstBaseSrcClass *) klass;
113         gstpush_src_class = (GstPushSrcClass *) klass;
114
115         gobject_class->set_property = gst_dreamaudiosource_set_property;
116         gobject_class->get_property = gst_dreamaudiosource_get_property;
117         gobject_class->dispose = gst_dreamaudiosource_dispose;
118
119         gst_element_class_add_pad_template (gstelement_class,
120                                             gst_static_pad_template_get (&srctemplate));
121
122         gst_element_class_set_static_metadata (gstelement_class,
123             "Dream Audio source", "Source/Audio",
124             "Provide an audio elementary stream from Dreambox encoder device",
125             "Andreas Frisch <fraxinas@opendreambox.org>");
126
127         gstelement_class->change_state = gst_dreamaudiosource_change_state;
128
129         gstbasesrc_class->get_caps = gst_dreamaudiosource_getcaps;
130         gstbasesrc_class->unlock = gst_dreamaudiosource_unlock;
131         gstbasesrc_class->unlock_stop = gst_dreamaudiosource_unlock_stop;
132         gstbasesrc_class->query = gst_dreamaudiosource_query;
133
134         gstpush_src_class->create = gst_dreamaudiosource_create;
135
136 #ifdef PROVIDE_CLOCK
137         gstelement_class->provide_clock = GST_DEBUG_FUNCPTR (gst_dreamaudiosource_provide_clock);
138 //      g_type_class_ref (GST_TYPE_SYSTEM_CLOCK);
139 #endif
140
141         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
142           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
143             "Bitrate in kbit/sec", 16, 320, DEFAULT_BITRATE,
144             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
145
146         g_object_class_install_property (gobject_class, ARG_INPUT_MODE,
147           g_param_spec_enum ("input-mode", "Input Mode",
148             "Select the input source of the audio stream",
149             GST_TYPE_DREAMAUDIOSOURCE_INPUT_MODE, DEFAULT_INPUT_MODE,
150             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
151
152         gst_dreamaudiosource_signals[SIGNAL_GET_DTS_OFFSET] =
153                 g_signal_new ("get-dts-offset",
154                 G_TYPE_FROM_CLASS (klass),
155                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
156                 G_STRUCT_OFFSET (GstDreamAudioSourceClass, get_dts_offset),
157                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
158
159         klass->get_dts_offset = gst_dreamaudiosource_get_dts_offset;
160 }
161
162 static gint64
163 gst_dreamaudiosource_get_dts_offset (GstDreamAudioSource *self)
164 {
165         GST_DEBUG_OBJECT (self, "gst_dreamaudiosource_get_dts_offset %" GST_TIME_FORMAT"", GST_TIME_ARGS (self->dts_offset) );
166         return self->dts_offset;
167 }
168
169 static void gst_dreamaudiosource_set_bitrate (GstDreamAudioSource * self, uint32_t bitrate)
170 {
171         g_mutex_lock (&self->mutex);
172         uint32_t abr = bitrate*1000;
173         if (!self->encoder || !self->encoder->fd)
174         {
175                 self->audio_info.bitrate = bitrate;
176                 g_mutex_unlock (&self->mutex);
177                 return;
178         }
179
180         int ret = ioctl(self->encoder->fd, AENC_SET_BITRATE, &abr);
181         if (ret != 0)
182         {
183                 GST_WARNING_OBJECT (self, "can't set audio bitrate to %i bytes/s!", abr);
184                 g_mutex_unlock (&self->mutex);
185                 return;
186         }
187         GST_INFO_OBJECT (self, "set audio bitrate to %i kBytes/s", bitrate);
188         self->audio_info.bitrate = bitrate;
189         g_mutex_unlock (&self->mutex);
190 }
191
192 void gst_dreamaudiosource_set_input_mode (GstDreamAudioSource *self, GstDreamAudioSourceInputMode mode)
193 {
194         g_return_if_fail (GST_IS_DREAMAUDIOSOURCE (self));
195         GEnumValue *val = g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (GST_TYPE_DREAMAUDIOSOURCE_INPUT_MODE)), mode);
196         if (!val)
197         {
198                 GST_ERROR_OBJECT (self, "no such input_mode %i!", mode);
199                 goto out;
200         }
201         const gchar *value_nick = val->value_nick;
202
203         g_mutex_lock (&self->mutex);
204         if (!self->encoder || !self->encoder->fd)
205         {
206                 self->input_mode = mode;
207                 goto out;
208         }
209         int int_mode = mode;
210         int ret = ioctl(self->encoder->fd, AENC_SET_SOURCE, &int_mode);
211         if (ret != 0)
212         {
213                 GST_WARNING_OBJECT (self, "can't set input mode to %s (%i) error: %s", value_nick, mode, strerror(errno));
214                 goto out;
215         }
216         GST_INFO_OBJECT (self, "successfully set input mode to %s (%i)", value_nick, mode);
217         self->input_mode = mode;
218 out:
219         g_mutex_unlock (&self->mutex);
220         return;
221 }
222
223 GstDreamAudioSourceInputMode gst_dreamaudiosource_get_input_mode (GstDreamAudioSource *self)
224 {
225         GstDreamAudioSourceInputMode result;
226         g_return_val_if_fail (GST_IS_DREAMAUDIOSOURCE (self), -1);
227         GST_OBJECT_LOCK (self);
228         result =self->input_mode;
229         GST_OBJECT_UNLOCK (self);
230         return result;
231 }
232
233 gboolean
234 gst_dreamaudiosource_plugin_init (GstPlugin *plugin)
235 {
236         GST_DEBUG_CATEGORY_INIT (dreamaudiosource_debug, "dreamaudiosource", 0, "dreamaudiosource");
237         return gst_element_register (plugin, "dreamaudiosource", GST_RANK_PRIMARY, GST_TYPE_DREAMAUDIOSOURCE);
238 }
239
240 static void
241 gst_dreamaudiosource_init (GstDreamAudioSource * self)
242 {
243         self->encoder = NULL;
244         self->descriptors_available = 0;
245         self->input_mode = DEFAULT_INPUT_MODE;
246
247         self->buffer_size = DEFAULT_BUFFER_SIZE;
248         g_queue_init (&self->current_frames);
249         self->readthread = NULL;
250
251         g_mutex_init (&self->mutex);
252         g_cond_init (&self->cond);
253         READ_SOCKET (self) = -1;
254         WRITE_SOCKET (self) = -1;
255
256         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
257         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
258
259         self->encoder = NULL;
260         self->encoder_clock = NULL;
261
262 #ifdef dump
263         self->dumpfd = open("/media/hdd/movie/dreamaudiosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
264         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
265 #endif
266 }
267
268 static gboolean gst_dreamaudiosource_encoder_init (GstDreamAudioSource * self)
269 {
270         GST_LOG_OBJECT (self, "initializating encoder...");
271         self->encoder = malloc(sizeof(EncoderInfo));
272
273         if (!self->encoder) {
274                 GST_ERROR_OBJECT (self,"out of space");
275                 return FALSE;
276         }
277
278         char fn_buf[32];
279         sprintf(fn_buf, "/dev/aenc%d", 0);
280         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
281         if (self->encoder->fd <= 0) {
282                 GST_ERROR_OBJECT (self,"cannot open device %s (%s)", fn_buf, strerror(errno));
283                 free(self->encoder);
284                 self->encoder = NULL;
285                 return FALSE;
286         }
287
288         self->encoder->buffer = malloc(ABUFSIZE);
289         if (!self->encoder->buffer) {
290                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
291                 return FALSE;
292         }
293
294         self->encoder->cdb = (unsigned char *)mmap (0, AMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
295
296         if (!self->encoder->cdb || self->encoder->cdb == MAP_FAILED) {
297                 GST_ERROR_OBJECT(self, "cannot alloc buffer: %s (%i)", strerror(errno), errno);
298                 self->encoder->cdb = NULL;
299                 return FALSE;
300         }
301
302         int control_sock[2];
303         if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
304         {
305                 GST_ERROR_OBJECT(self, "cannot create control sockets: %s (%i)", strerror(errno), errno);
306                 return FALSE;
307         }
308         READ_SOCKET (self) = control_sock[0];
309         WRITE_SOCKET (self) = control_sock[1];
310         fcntl (READ_SOCKET (self), F_SETFL, O_NONBLOCK);
311         fcntl (WRITE_SOCKET (self), F_SETFL, O_NONBLOCK);
312
313         self->memtrack_list = NULL;
314         self->encoder->used_range_min = UINT32_MAX;
315         self->encoder->used_range_max = 0;
316
317         self->audio_info.samplerate = DEFAULT_SAMPLERATE;
318         gst_dreamaudiosource_set_bitrate (self, self->audio_info.bitrate);
319         gst_dreamaudiosource_set_input_mode (self, self->input_mode);
320
321 #ifdef PROVIDE_CLOCK
322         self->encoder_clock = gst_dreamsource_clock_new ("GstDreamAudioSourceClock", self->encoder->fd);
323         GST_DEBUG_OBJECT (self, "self->encoder_clock = %" GST_PTR_FORMAT, self->encoder_clock);
324         GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
325 #endif
326
327         GST_LOG_OBJECT (self, "encoder %s successfully initialized", fn_buf);
328         return TRUE;
329 }
330
331 static void gst_dreamaudiosource_encoder_release (GstDreamAudioSource * self)
332 {
333         GST_LOG_OBJECT (self, "releasing encoder...");
334         if (self->encoder) {
335                 if (self->encoder->buffer)
336                         free(self->encoder->buffer);
337                 if (self->encoder->cdb)
338                         munmap(self->encoder->cdb, AMMAPSIZE);
339                 if (self->encoder->fd)
340                         close(self->encoder->fd);
341                 free(self->encoder);
342         }
343         self->encoder = NULL;
344         close (READ_SOCKET (self));
345         close (WRITE_SOCKET (self));
346         READ_SOCKET (self) = -1;
347         WRITE_SOCKET (self) = -1;
348         if (self->encoder_clock) {
349                 gst_object_unref (self->encoder_clock);
350                 self->encoder_clock = NULL;
351         }
352 }
353
354 static void
355 gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
356 {
357         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
358
359         switch (prop_id) {
360                 case ARG_BITRATE:
361                         gst_dreamaudiosource_set_bitrate(self, g_value_get_int (value));
362                         break;
363                 case ARG_INPUT_MODE:
364                              gst_dreamaudiosource_set_input_mode (self, g_value_get_enum (value));
365                         break;
366                 default:
367                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
368                         break;
369         }
370 }
371
372 static void
373 gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
374 {
375         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
376
377         switch (prop_id) {
378                 case ARG_BITRATE:
379                         g_value_set_int (value, self->audio_info.bitrate/1000);
380                         break;
381                 case ARG_INPUT_MODE:
382                         g_value_set_enum (value, gst_dreamaudiosource_get_input_mode (self));
383                         break;
384                 default:
385                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
386                         break;
387         }
388 }
389
390 static GstCaps *
391 gst_dreamaudiosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
392 {
393         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
394         GstPadTemplate *pad_template;
395         GstCaps *caps;
396
397         pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
398         g_return_val_if_fail (pad_template != NULL, NULL);
399
400         if (self->encoder == NULL) {
401                 GST_DEBUG_OBJECT (self, "encoder not opened -> use template caps");
402                 caps = gst_pad_template_get_caps (pad_template);
403         }
404         else
405         {
406                 caps = gst_caps_make_writable(gst_pad_template_get_caps (pad_template));
407         }
408
409         GST_DEBUG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
410         return caps;
411 }
412
413 static gboolean gst_dreamaudiosource_query (GstBaseSrc * bsrc, GstQuery * query)
414 {
415         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
416         gboolean ret = TRUE;
417         switch (GST_QUERY_TYPE (query)) {
418                 case GST_QUERY_LATENCY:{
419                         if (self->audio_info.samplerate) {
420                                 GstClockTime min, max;
421
422                                 g_mutex_lock (&self->mutex);
423                                 min = gst_util_uint64_scale_ceil (GST_SECOND, 1000, self->audio_info.samplerate);
424                                 g_mutex_unlock (&self->mutex);
425
426                                 max = self->buffer_size * min;
427
428                                 gst_query_set_latency (query, TRUE, min, max);
429                                 GST_DEBUG_OBJECT (bsrc, "set LATENCY QUERY %" GST_PTR_FORMAT, query);
430                                 ret = TRUE;
431                         } else {
432                                 ret = FALSE;
433                         }
434                         break;
435                 }
436                 default:
437                         ret = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
438                         break;
439         }
440         return ret;
441 }
442
443 static gboolean gst_dreamaudiosource_unlock (GstBaseSrc * bsrc)
444 {
445         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
446         GST_DEBUG_OBJECT (self, "stop creating buffers");
447         g_mutex_lock (&self->mutex);
448         self->flushing = TRUE;
449         GST_DEBUG_OBJECT (self, "set flushing TRUE");
450         g_cond_signal (&self->cond);
451         g_mutex_unlock (&self->mutex);
452         return TRUE;
453 }
454
455 static gboolean gst_dreamaudiosource_unlock_stop (GstBaseSrc * bsrc)
456 {
457         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
458         GST_DEBUG_OBJECT (self, "start creating buffers...");
459         g_mutex_lock (&self->mutex);
460         self->flushing = FALSE;
461         g_queue_foreach (&self->current_frames, (GFunc) gst_buffer_unref, NULL);
462         g_queue_clear (&self->current_frames);
463         g_mutex_unlock (&self->mutex);
464         return TRUE;
465 }
466
467 static void gst_dreamaudiosource_free_buffer (struct _buffer_memorytracker * memtrack)
468 {
469         GstDreamAudioSource * self = memtrack->self;
470         GST_OBJECT_LOCK(self);
471         GST_TRACE_OBJECT (self, "freeing %" GST_PTR_FORMAT " uiOffset=%i uiLength=%i used_range_min=%i", memtrack->buffer, memtrack->uiOffset, memtrack->uiLength, self->encoder->used_range_min);
472         GList *list = g_list_first (self->memtrack_list);
473         guint abs_minimum = UINT32_MAX;
474         guint abs_maximum = 0;
475         struct _buffer_memorytracker * mt;
476         int count = 0;
477         self->memtrack_list = g_list_remove(list, memtrack);
478         free(memtrack);
479         if (self->encoder)
480         {
481                 list = g_list_first (self->memtrack_list);
482                 while (list) {
483                         mt = list->data;
484                         if (abs_minimum > 0 && mt->uiOffset < abs_minimum)
485                                 abs_minimum = mt->uiOffset;
486                         if (mt->uiOffset+mt->uiLength > abs_maximum)
487                                 abs_maximum = mt->uiOffset+mt->uiLength;
488                         count++;
489                         list = g_list_next (list);
490                 }
491                 GST_TRACE_OBJECT (self, "new abs_minimum=%i, abs_maximum=%i", abs_minimum, abs_maximum);
492                 self->encoder->used_range_min = abs_minimum;
493                 self->encoder->used_range_max = abs_maximum;
494         }
495         GST_OBJECT_UNLOCK(self);
496 }
497
498 static void gst_dreamaudiosource_read_thread_func (GstDreamAudioSource * self)
499 {
500         EncoderInfo *enc = self->encoder;
501         GstDreamSourceReadthreadState state = READTHREADSTATE_NONE;
502         GstBuffer *readbuf = NULL;
503
504         if (!enc) {
505                 GST_WARNING_OBJECT (self, "encoder device not opened!");
506                 return;
507         }
508
509         GST_DEBUG_OBJECT (self, "enter read thread");
510
511         GstMessage *message;
512         GValue val = { 0 };
513
514         message = gst_message_new_stream_status (GST_OBJECT_CAST (self), GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (GST_OBJECT_PARENT(self)));
515         g_value_init (&val, GST_TYPE_G_THREAD);
516         g_value_set_boxed (&val, self->readthread);
517         gst_message_set_stream_status_object (message, &val);
518         g_value_unset (&val);
519         GST_DEBUG_OBJECT (self, "posting ENTER stream status");
520         gst_element_post_message (GST_ELEMENT_CAST (self), message);
521         GstClockTime clock_time, base_time;
522         gboolean discont = TRUE;
523
524         while (TRUE) {
525                 {
526                         if (state == READTHREADSTATE_STOP)
527                                 goto stop_running;
528
529                         struct pollfd rfd[2];
530                         int timeout, nfds;
531
532                         rfd[0].fd = READ_SOCKET (self);
533                         rfd[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
534                         rfd[1].revents = 0;
535                         rfd[1].events = POLLIN;
536                         nfds = 1;
537                         timeout = 0;
538
539                         if (state <= READTRREADSTATE_PAUSED)
540                                 timeout = 200;
541                         else if (state == READTRREADSTATE_RUNNING && self->descriptors_available == 0)
542                         {
543                                 rfd[1].fd = enc->fd;
544                                 self->descriptors_count = 0;
545                                 timeout = 200;
546                                 nfds = 2;
547                         }
548
549                         int ret = poll(rfd, nfds, timeout);
550
551                         if (G_UNLIKELY (ret == -1))
552                         {
553                                 GST_ERROR_OBJECT (self, "SELECT ERROR!");
554                                 goto stop_running;
555                         }
556                         else if ( ret == 0 && self->descriptors_available == 0 )
557                         {
558                                 g_mutex_lock (&self->mutex);
559                                 gst_clock_get_internal_time(self->encoder_clock);
560                                 if (self->flushing)
561                                 {
562                                         GST_DEBUG_OBJECT (self, "FLUSHING!");
563                                         g_cond_signal (&self->cond);
564                                         g_mutex_unlock (&self->mutex);
565                                         continue;
566                                 }
567                                 g_mutex_unlock (&self->mutex);
568                                 GST_DEBUG_OBJECT (self, "SELECT TIMEOUT");
569                                 //!!! TODO generate valid dummy payload
570                                 discont = TRUE;
571                                 if (self->dts_offset != GST_CLOCK_TIME_NONE)
572                                         readbuf = gst_buffer_new();
573                         }
574                         else if ( rfd[0].revents )
575                         {
576                                 char command;
577                                 READ_COMMAND (self, command, ret);
578                                 switch (command) {
579                                         case CONTROL_STOP:
580                                                 GST_DEBUG_OBJECT (self, "CONTROL_STOP!");
581                                                 state = READTHREADSTATE_STOP;
582                                                 break;
583                                         case CONTROL_PAUSE:
584                                                 GST_DEBUG_OBJECT (self, "CONTROL_PAUSE!");
585                                                 state = READTRREADSTATE_PAUSED;
586                                                 break;
587                                         case CONTROL_RUN:
588                                                 GST_DEBUG_OBJECT (self, "CONTROL_RUN");
589                                                 state = READTRREADSTATE_RUNNING;
590                                                 break;
591                                         default:
592                                                 GST_ERROR_OBJECT (self, "illegal control socket command %c received!", command);
593                                 }
594                                 continue;
595                         }
596                         else if ( G_LIKELY(rfd[1].revents & POLLIN) )
597                         {
598                                 clock_time = gst_clock_get_internal_time (self->encoder_clock);
599                                 base_time = gst_element_get_base_time(GST_ELEMENT(self));
600                                 int rlen = read(enc->fd, enc->buffer, ABUFSIZE);
601                                 if (rlen <= 0 || rlen % ABDSIZE ) {
602                                         if ( errno == 512 )
603                                                 goto stop_running;
604                                         GST_WARNING_OBJECT (self, "read error %s (%i)", strerror(errno), errno);
605                                         goto stop_running;
606                                 }
607                                 self->descriptors_available = rlen / ABDSIZE;
608                                 GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
609                         }
610                 }
611
612                 while (self->descriptors_count < self->descriptors_available)
613                 {
614                         GstClockTime encoder_pts = GST_CLOCK_TIME_NONE;
615                         GstClockTime result_pts = GST_CLOCK_TIME_NONE;
616
617                         off_t offset = self->descriptors_count * ABDSIZE;
618                         AudioBufferDescriptor *desc = (AudioBufferDescriptor*)(&enc->buffer[offset]);
619
620                         uint32_t f = desc->stCommon.uiFlags;
621
622                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
623                         {
624                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
625                                 self->descriptors_count = self->descriptors_available;
626                                 continue;
627                         }
628
629                         GST_LOG_OBJECT (self, "descriptors_count=%d, descriptors_available=%d\tuiOffset=%d, uiLength=%d", self->descriptors_count, self->descriptors_available, desc->stCommon.uiOffset, desc->stCommon.uiLength);
630
631                         if (self->encoder->used_range_min == UINT32_MAX)
632                                 self->encoder->used_range_min = desc->stCommon.uiOffset;
633
634                         if (self->encoder->used_range_max == 0)
635                                 self->encoder->used_range_max = desc->stCommon.uiOffset+desc->stCommon.uiLength;
636
637 //                      if (desc->stCommon.uiOffset < self->encoder->used_range_max && desc->stCommon.uiOffset+desc->stCommon.uiLength > self->encoder->used_range_min)
638 //                      {
639 //                              GST_WARNING_OBJECT (self, "encoder overwrites buffer memory that is still in use! uiOffset=%i uiLength=%i used_range_min=%i used_range_max=%i", desc->stCommon.uiOffset, desc->stCommon.uiLength, self->encoder->used_range_min, self->encoder->used_range_max);
640 //                              self->descriptors_count++;
641 //                              readbuf = gst_buffer_new();
642 //                              continue;
643 //                      }
644
645                         // uiDTS since kernel driver booted
646                         if (f & CDB_FLAG_PTS_VALID)
647                         {
648                                 encoder_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
649                                 GST_LOG_OBJECT (self, "f & CDB_FLAG_PTS_VALID && encoder's uiPTS=%" GST_TIME_FORMAT"", GST_TIME_ARGS(encoder_pts));
650
651                                 g_mutex_lock (&self->mutex);
652                                 if (G_UNLIKELY (self->dts_offset == GST_CLOCK_TIME_NONE))
653                                 {
654                                         if (self->dreamvideosrc)
655                                         {
656                                                 guint64 videosource_dts_offset;
657                                                 g_signal_emit_by_name(self->dreamvideosrc, "get-dts-offset", &videosource_dts_offset);
658                                                 if (videosource_dts_offset != GST_CLOCK_TIME_NONE)
659                                                 {
660                                                         GST_DEBUG_OBJECT (self, "use DREAMVIDEOSOURCE's dts_offset=%" GST_TIME_FORMAT "", GST_TIME_ARGS (videosource_dts_offset) );
661                                                         self->dts_offset = videosource_dts_offset;
662                                                 }
663                                         }
664                                         if (self->dts_offset == GST_CLOCK_TIME_NONE)
665                                         {
666                                                 self->dts_offset = encoder_pts;
667                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as dts_offset=%" GST_TIME_FORMAT" (%lld)", GST_TIME_ARGS (self->dts_offset), desc->stCommon.uiPTS);
668                                         }
669                                 }
670                                 g_mutex_unlock (&self->mutex);
671                         }
672
673                         if (G_UNLIKELY (self->dts_offset == GST_CLOCK_TIME_NONE))
674                         {
675                                 GST_DEBUG_OBJECT (self, "dts_offset is still unknown, skipping frame...");
676                                 self->descriptors_count++;
677                                 break;
678                         }
679
680                         if (encoder_pts != GST_CLOCK_TIME_NONE)
681                         {
682                                 GstClockTime pts_clock_time = encoder_pts - self->dts_offset;
683                                 GstClockTime internal, external;
684                                 GstClockTime rate_n, rate_d;
685                                 GstClockTimeDiff diff;
686
687                                 gst_clock_get_calibration (self->encoder_clock, &internal, &external, &rate_n, &rate_d);
688
689                                 if (internal > pts_clock_time) {
690                                         diff = internal - pts_clock_time;
691                                         diff = gst_util_uint64_scale (diff, rate_n, rate_d);
692                                         pts_clock_time = external - diff;
693                                 } else {
694                                         diff = pts_clock_time - internal;
695                                         diff = gst_util_uint64_scale (diff, rate_n, rate_d);
696                                         pts_clock_time = external + diff;
697                                 }
698
699                                 if ( pts_clock_time >= base_time )
700                                         result_pts = pts_clock_time - base_time;
701                                 else
702                                         GST_DEBUG_OBJECT (self, "pts_clock_time < base_time, skipping frame...");
703
704 #define extra_timestamp_debug
705 #ifdef extra_timestamp_debug
706                                 GstClockTime my_int_time = gst_clock_get_internal_time(self->encoder_clock);
707                                 GstClockTime pipeline_int_time = GST_CLOCK_TIME_NONE;
708                                 GstClock *elemclk = gst_element_get_clock (GST_ELEMENT (self));
709                                 if (elemclk)
710                                 {
711                                         pipeline_int_time = gst_clock_get_internal_time(elemclk);
712                                         gst_object_unref (elemclk);
713                                 }
714
715                                 GST_LOG_OBJECT (self, "post-calibration\n"
716                                 "%" GST_TIME_FORMAT "=base_time       %" GST_TIME_FORMAT "=clock_time\n"
717                                 "%" GST_TIME_FORMAT "=encoder_pts     %" GST_TIME_FORMAT "=pts_clock_time     %" GST_TIME_FORMAT "=result_pts\n"
718                                 "%" GST_TIME_FORMAT "=internal        %" GST_TIME_FORMAT "=external           %" GST_TIME_FORMAT "=diff\n"
719                                 "%" GST_TIME_FORMAT "=rate_n          %" GST_TIME_FORMAT "=rate_d\n"
720                                 "%" GST_TIME_FORMAT "=my_int_time     %" GST_TIME_FORMAT "=pipeline_int_time\n"
721                                 ,
722                                 GST_TIME_ARGS (base_time), GST_TIME_ARGS (clock_time),
723                                 GST_TIME_ARGS (encoder_pts), GST_TIME_ARGS (pts_clock_time), GST_TIME_ARGS (result_pts),
724                                 GST_TIME_ARGS (internal), GST_TIME_ARGS (external), GST_TIME_ARGS (diff),
725                                 GST_TIME_ARGS (rate_n), GST_TIME_ARGS (rate_d),
726                                 GST_TIME_ARGS (my_int_time), GST_TIME_ARGS (pipeline_int_time)
727                                 );
728 #endif
729                         }
730
731                         struct _buffer_memorytracker * memtrack = malloc(sizeof(struct _buffer_memorytracker));
732                         if (G_UNLIKELY (!memtrack))
733                         {
734                                 GST_ERROR_OBJECT (self, "can't allocate buffer_memorytracker");
735                                 goto stop_running;
736                         }
737
738                         if (readbuf)
739                         {
740                                 GST_INFO_OBJECT (self, "LAST BUFFER WAS INCOMPLETE... appending");
741                                 GstBuffer *append_buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, AMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, memtrack, (GDestroyNotify) gst_dreamaudiosource_free_buffer);
742                                 readbuf = gst_buffer_append (readbuf, append_buffer);
743                         }
744                         else
745                         {
746                                 GST_OBJECT_LOCK (self);
747                                 readbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, AMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, memtrack, (GDestroyNotify) gst_dreamaudiosource_free_buffer);
748                                 if (desc->stCommon.uiLength == 0)
749                                 {
750                                         GST_WARNING_OBJECT (self, "ZERO SIZE BUFFER");
751                                 }
752                                 memtrack->self = self;
753                                 memtrack->buffer = readbuf;
754                                 memtrack->uiOffset = desc->stCommon.uiOffset;
755                                 memtrack->uiLength = desc->stCommon.uiLength;
756                                 self->memtrack_list = g_list_append(self->memtrack_list, memtrack);
757                                 GST_OBJECT_UNLOCK (self);
758                         }
759                         if (result_pts != GST_CLOCK_TIME_NONE)
760                         {
761                                 GST_BUFFER_PTS(readbuf) = result_pts;
762                                 GST_BUFFER_DTS(readbuf) = result_pts;
763                         }
764 #ifdef dump
765                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
766                         GST_LOG_OBJECT (self, "read=%i dumped=%i gst_buffer_get_size=%" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (readbuf) );
767 #endif
768                         self->descriptors_count++;
769                         break;
770                 }
771
772                 if (self->descriptors_count == self->descriptors_available)
773                 {
774                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
775                         /* release consumed descs */
776                         if (write(enc->fd, &self->descriptors_count, sizeof(self->descriptors_count)) != sizeof(self->descriptors_count)) {
777                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
778                                 goto stop_running;
779                         }
780                         self->descriptors_available = 0;
781                 }
782
783                 if (readbuf)
784                 {
785                         g_mutex_lock (&self->mutex);
786                         if (gst_buffer_get_size (readbuf) && !self->flushing)
787                         {
788                                 while (g_queue_get_length (&self->current_frames) >= self->buffer_size)
789                                 {
790                                         GstBuffer * oldbuf = g_queue_pop_head (&self->current_frames);
791                                         GST_WARNING_OBJECT (self, "dropping %" GST_PTR_FORMAT " because of queue overflow! buffers count=%i", oldbuf, g_queue_get_length (&self->current_frames));
792                                         gst_buffer_unref(oldbuf);
793                                         GST_BUFFER_FLAG_SET ((GstBuffer *) g_queue_peek_head (&self->current_frames), GST_BUFFER_FLAG_DISCONT);
794                                 }
795                                 if (discont)
796                                 {
797                                         GST_BUFFER_FLAG_SET (readbuf, GST_BUFFER_FLAG_DISCONT);
798                                         discont = FALSE;
799                                 }
800                                 g_queue_push_tail (&self->current_frames, readbuf);
801                                 GST_INFO_OBJECT (self, "read %" GST_PTR_FORMAT " to queue", readbuf );
802                         }
803                         else
804                         {
805                                 GST_INFO_OBJECT (self, "dropping %" GST_PTR_FORMAT " because %s", readbuf, self->flushing?"FLUSHING":"size = 0");
806                                 gst_buffer_unref(readbuf);
807                         }
808                         g_cond_signal (&self->cond);
809                         g_mutex_unlock (&self->mutex);
810                         readbuf = NULL;
811                 }
812         }
813
814         g_assert_not_reached ();
815         return;
816
817         stop_running:
818         {
819                 g_mutex_unlock (&self->mutex);
820                 g_cond_signal (&self->cond);
821                 GST_DEBUG ("stop running, exit thread");
822                 message = gst_message_new_stream_status (GST_OBJECT_CAST (self), GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (GST_OBJECT_PARENT(self)));
823                 g_value_init (&val, GST_TYPE_G_THREAD);
824                 g_value_set_boxed (&val, self->readthread);
825                 gst_message_set_stream_status_object (message, &val);
826                 g_value_unset (&val);
827                 GST_DEBUG_OBJECT (self, "posting LEAVE stream status");
828                 gst_element_post_message (GST_ELEMENT_CAST (self), message);
829                 return;
830         }
831 }
832
833 static GstFlowReturn
834 gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
835 {
836         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (psrc);
837
838         GST_LOG_OBJECT (self, "new buffer requested. queue has %i buffers", g_queue_get_length (&self->current_frames));
839
840         g_mutex_lock (&self->mutex);
841         while (g_queue_is_empty (&self->current_frames) && !self->flushing)
842         {
843                 GST_INFO_OBJECT (self, "waiting for buffer from encoder");
844                 g_cond_wait (&self->cond, &self->mutex);
845         }
846
847         *outbuf = g_queue_pop_head (&self->current_frames);
848         g_mutex_unlock (&self->mutex);
849
850         if (*outbuf)
851         {
852                 GST_INFO_OBJECT (self, "pushing %" GST_PTR_FORMAT ". queue has %i buffers", *outbuf, g_queue_get_length (&self->current_frames));
853                 return GST_FLOW_OK;
854         }
855         GST_INFO_OBJECT (self, "FLUSHING");
856         return GST_FLOW_FLUSHING;
857 }
858
859 static GstStateChangeReturn gst_dreamaudiosource_change_state (GstElement * element, GstStateChange transition)
860 {
861         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (element);
862         GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
863         int ret;
864
865         switch (transition) {
866                 case GST_STATE_CHANGE_NULL_TO_READY:
867                 {
868                         if (!gst_dreamaudiosource_encoder_init (self))
869                         {
870                                 GError *err = g_error_new (GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_READ, "Can't initialize encoder device");
871                                 GstMessage *msg = gst_message_new_error (GST_OBJECT (self), err, NULL);
872                                 gst_element_post_message (element, msg);
873                                 g_error_free (err);
874                                 return GST_STATE_CHANGE_FAILURE;
875                         }
876                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_NULL_TO_READY");
877                         break;
878                 }
879                 case GST_STATE_CHANGE_READY_TO_PAUSED:
880                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_READY_TO_PAUSED");
881                         self->dreamvideosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamvideosource0");
882
883                         self->dts_offset = GST_CLOCK_TIME_NONE;
884 #ifdef PROVIDE_CLOCK
885                         gst_element_post_message (element, gst_message_new_clock_provide (GST_OBJECT_CAST (element), self->encoder_clock, TRUE));
886 #endif
887                         self->flushing = TRUE;
888                         self->readthread = g_thread_try_new ("dreamaudiosrc-read", (GThreadFunc) gst_dreamaudiosource_read_thread_func, self, NULL);
889                         GST_DEBUG_OBJECT (self, "started readthread @%p", self->readthread );
890                         break;
891                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
892                         g_mutex_lock (&self->mutex);
893                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
894                         GstClock *pipeline_clock = gst_element_get_clock (GST_ELEMENT (self));
895                         if (pipeline_clock)
896                         {
897                                 if (pipeline_clock != self->encoder_clock)
898                                 {
899                                         gst_clock_set_master (self->encoder_clock, pipeline_clock);
900                                         GST_DEBUG_OBJECT (self, "slaved %" GST_PTR_FORMAT "to pipeline_clock %" GST_PTR_FORMAT "", self->encoder_clock, pipeline_clock);
901                                 }
902                                 else
903                                         GST_DEBUG_OBJECT (self, "encoder_clock is master clock");
904                         }
905                                 else
906                                         GST_WARNING_OBJECT (self, "no pipeline clock!");
907                         ret = ioctl(self->encoder->fd, AENC_START);
908                         if ( ret != 0 )
909                                 goto fail;
910                         self->descriptors_available = 0;
911                         CLEAR_COMMAND (self);
912                         g_mutex_unlock (&self->mutex);
913                         break;
914                 default:
915                         break;
916         }
917
918         if (GST_ELEMENT_CLASS (parent_class)->change_state)
919                 sret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
920
921         switch (transition) {
922                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
923                         g_mutex_lock (&self->mutex);
924                         SEND_COMMAND (self, CONTROL_RUN);
925                         GST_INFO_OBJECT (self, "started encoder!");
926                         g_mutex_unlock (&self->mutex);
927                         break;
928                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
929                         g_mutex_lock (&self->mutex);
930                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED self->descriptors_count=%i self->descriptors_available=%i", self->descriptors_count, self->descriptors_available);
931                         SEND_COMMAND (self, CONTROL_PAUSE);
932                         if (self->descriptors_count < self->descriptors_available)
933                                 self->descriptors_count = self->descriptors_available;
934                         if (self->descriptors_count)
935                                 write(self->encoder->fd, &self->descriptors_count, sizeof(self->descriptors_count));
936                         ret = ioctl(self->encoder->fd, AENC_STOP);
937                         if ( ret != 0 )
938                                 goto fail;
939 #ifdef PROVIDE_CLOCK
940                         gst_clock_set_master (self->encoder_clock, NULL);
941 #endif
942                         GST_INFO_OBJECT (self, "stopped encoder!");
943                         g_mutex_unlock (&self->mutex);
944                         break;
945                 case GST_STATE_CHANGE_PAUSED_TO_READY:
946                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PAUSED_TO_READY");
947 #ifdef PROVIDE_CLOCK
948                         gst_element_post_message (element, gst_message_new_clock_lost (GST_OBJECT_CAST (element), self->encoder_clock));
949                         gst_clock_set_calibration (self->encoder_clock, 0, 0, 1, 1);
950 #endif
951                         GST_DEBUG_OBJECT (self, "stopping readthread @%p...", self->readthread);
952                         SEND_COMMAND (self, CONTROL_STOP);
953                         g_thread_join (self->readthread);
954                         if (self->dreamvideosrc)
955                                 gst_object_unref(self->dreamvideosrc);
956                         self->dreamvideosrc = NULL;
957                         break;
958                 case GST_STATE_CHANGE_READY_TO_NULL:
959                         gst_dreamaudiosource_encoder_release (self);
960                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_READY_TO_NULL");
961                         break;
962                 default:
963                         break;
964         }
965
966         return sret;
967 fail:
968         GST_ERROR_OBJECT(self,"can't perform encoder ioctl! error: %s (%i)", strerror(errno), errno);
969         g_mutex_unlock (&self->mutex);
970         return GST_STATE_CHANGE_FAILURE;
971 }
972
973 static void
974 gst_dreamaudiosource_dispose (GObject * gobject)
975 {
976         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (gobject);
977 #ifdef dump
978         close(self->dumpfd);
979 #endif
980         g_list_free(self->memtrack_list);
981         g_mutex_clear (&self->mutex);
982         g_cond_clear (&self->cond);
983         GST_DEBUG_OBJECT (self, "disposed");
984         G_OBJECT_CLASS (parent_class)->dispose (gobject);
985 }
986
987 #ifdef PROVIDE_CLOCK
988 static GstClock *gst_dreamaudiosource_provide_clock (GstElement * element)
989 {
990         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (element);
991
992         if (!self->encoder || self->encoder->fd < 0)
993         {
994                 GST_DEBUG_OBJECT (self, "encoder device not started, can't provide clock!");
995                 return NULL;
996         }
997
998         return GST_CLOCK_CAST (gst_object_ref (self->encoder_clock));
999 }
1000
1001 #endif
1002