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