get rid off unneeded debug
[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_BASE_PTS,
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
63 static GstStaticPadTemplate srctemplate =
64     GST_STATIC_PAD_TEMPLATE ("src",
65         GST_PAD_SRC,
66         GST_PAD_ALWAYS,
67         GST_STATIC_CAPS ("audio/mpeg, "
68         "mpegversion = 4,"
69         "stream-format = (string) adts")
70     );
71
72 #define gst_dreamaudiosource_parent_class parent_class
73 G_DEFINE_TYPE (GstDreamAudioSource, gst_dreamaudiosource, GST_TYPE_PUSH_SRC);
74
75 static GstCaps *gst_dreamaudiosource_getcaps (GstBaseSrc * psrc, GstCaps * filter);
76 static gboolean gst_dreamaudiosource_start (GstBaseSrc * bsrc);
77 static gboolean gst_dreamaudiosource_stop (GstBaseSrc * bsrc);
78 static gboolean gst_dreamaudiosource_unlock (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_base_pts (GstDreamAudioSource *self);
87
88 static void
89 gst_dreamaudiosource_class_init (GstDreamAudioSourceClass * klass)
90 {
91         GObjectClass *gobject_class;
92         GstElementClass *gstelement_class;
93         GstBaseSrcClass *gstbasesrc_class;
94         GstPushSrcClass *gstpush_src_class;
95
96         gobject_class = (GObjectClass *) klass;
97         gstelement_class = (GstElementClass *) klass;
98         gstbasesrc_class = (GstBaseSrcClass *) klass;
99         gstpush_src_class = (GstPushSrcClass *) klass;
100
101         gobject_class->set_property = gst_dreamaudiosource_set_property;
102         gobject_class->get_property = gst_dreamaudiosource_get_property;
103         gobject_class->dispose = gst_dreamaudiosource_dispose;
104
105         gst_element_class_add_pad_template (gstelement_class,
106                                             gst_static_pad_template_get (&srctemplate));
107
108         gst_element_class_set_static_metadata (gstelement_class,
109             "Dream Audio source", "Source/Audio",
110             "Provide an audio elementary stream from Dreambox encoder device",
111             "Andreas Frisch <fraxinas@opendreambox.org>");
112
113         gstelement_class->change_state = gst_dreamaudiosource_change_state;
114
115         gstbasesrc_class->get_caps = gst_dreamaudiosource_getcaps;
116         gstbasesrc_class->start = gst_dreamaudiosource_start;
117         gstbasesrc_class->stop = gst_dreamaudiosource_stop;
118         gstbasesrc_class->unlock = gst_dreamaudiosource_unlock;
119
120         gstpush_src_class->create = gst_dreamaudiosource_create;
121
122         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
123           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
124             "Bitrate in kbit/sec", 16, 320, DEFAULT_BITRATE,
125             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
126
127         g_object_class_install_property (gobject_class, ARG_INPUT_MODE,
128           g_param_spec_enum ("input-mode", "Input Mode",
129             "Select the input source of the audio stream",
130             GST_TYPE_DREAMAUDIOSOURCE_INPUT_MODE, DEFAULT_INPUT_MODE,
131             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
132
133         gst_dreamaudiosource_signals[SIGNAL_GET_BASE_PTS] =
134                 g_signal_new ("get-base-pts",
135                 G_TYPE_FROM_CLASS (klass),
136                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
137                 G_STRUCT_OFFSET (GstDreamAudioSourceClass, get_base_pts),
138                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
139
140         klass->get_base_pts = gst_dreamaudiosource_get_base_pts;
141
142 }
143
144 static gint64
145 gst_dreamaudiosource_get_base_pts (GstDreamAudioSource *self)
146 {
147         GST_DEBUG_OBJECT (self, "gst_dreamaudiosource_get_base_pts %" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
148         return self->base_pts;
149 }
150
151 static void gst_dreamaudiosource_set_bitrate (GstDreamAudioSource * self, uint32_t bitrate)
152 {
153         if (!self->encoder || !self->encoder->fd)
154                 return;
155         uint32_t abr = bitrate*1000;
156         int ret = ioctl(self->encoder->fd, AENC_SET_BITRATE, &abr);
157         if (ret != 0)
158         {
159                 GST_WARNING_OBJECT (self, "can't set audio bitrate to %i bytes/s!", abr);
160                 return;
161         }
162         GST_INFO_OBJECT (self, "set audio bitrate to %i kBytes/s", bitrate);
163         self->audio_info.bitrate = abr;
164 }
165
166 void gst_dreamaudiosource_set_input_mode (GstDreamAudioSource *self, GstDreamAudioSourceInputMode mode)
167 {
168         g_return_if_fail (GST_IS_DREAMAUDIOSOURCE (self));
169         GEnumValue *val = g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (GST_TYPE_DREAMAUDIOSOURCE_INPUT_MODE)), mode);
170         if (!val)
171         {
172                 GST_ERROR_OBJECT (self, "no such input_mode %i!", mode);
173                 goto out;
174         }
175         const gchar *value_nick = val->value_nick;
176         GST_DEBUG_OBJECT (self, "setting input_mode to %s (%i)...", value_nick, mode);
177         g_mutex_lock (&self->mutex);
178         if (!self->encoder || !self->encoder->fd)
179         {
180                 GST_ERROR_OBJECT (self, "can't set input mode because encoder device not opened!");
181                 goto out;
182         }
183         int int_mode = mode;
184         int ret = ioctl(self->encoder->fd, AENC_SET_SOURCE, &int_mode);
185         if (ret != 0)
186         {
187                 GST_WARNING_OBJECT (self, "can't set input mode to %s (%i) error: %s", value_nick, mode, strerror(errno));
188                 goto out;
189         }
190         GST_INFO_OBJECT (self, "successfully set input mode to %s (%i)", value_nick, mode);
191         self->input_mode = mode;
192 out:
193         g_mutex_unlock (&self->mutex);
194         return;
195 }
196
197 GstDreamAudioSourceInputMode gst_dreamaudiosource_get_input_mode (GstDreamAudioSource *self)
198 {
199         GstDreamAudioSourceInputMode result;
200         g_return_val_if_fail (GST_IS_DREAMAUDIOSOURCE (self), -1);
201         GST_OBJECT_LOCK (self);
202         result =self->input_mode;
203         GST_OBJECT_UNLOCK (self);
204         return result;
205 }
206
207 gboolean
208 gst_dreamaudiosource_plugin_init (GstPlugin *plugin)
209 {
210         GST_DEBUG_CATEGORY_INIT (dreamaudiosource_debug, "dreamaudiosource", 0, "dreamaudiosource");
211         return gst_element_register (plugin, "dreamaudiosource", GST_RANK_PRIMARY, GST_TYPE_DREAMAUDIOSOURCE);
212 }
213
214 static void
215 gst_dreamaudiosource_init (GstDreamAudioSource * self)
216 {
217         self->encoder = NULL;
218         self->buffers_list = NULL;
219         self->descriptors_available = 0;
220         self->input_mode = DEFAULT_INPUT_MODE;
221
222         g_mutex_init (&self->mutex);
223         READ_SOCKET (self) = -1;
224         WRITE_SOCKET (self) = -1;
225
226         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
227         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
228
229         self->encoder = malloc(sizeof(EncoderInfo));
230
231         if(!self->encoder) {
232                 GST_ERROR_OBJECT(self,"out of space");
233                 return;
234         }
235
236         char fn_buf[32];
237         sprintf(fn_buf, "/dev/aenc%d", 0);
238         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
239         if(self->encoder->fd <= 0) {
240                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
241                 free(self->encoder);
242                 self->encoder = NULL;
243                 return;
244         }
245
246         int flags = fcntl(self->encoder->fd, F_GETFL, 0);
247         fcntl(self->encoder->fd, F_SETFL, flags | O_NONBLOCK);
248
249         self->encoder->buffer = malloc(ABUFSIZE);
250         if(!self->encoder->buffer) {
251                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
252                 return;
253         }
254
255         self->encoder->cdb = (unsigned char *)mmap(0, AMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
256         if(!self->encoder->cdb || self->encoder->cdb== MAP_FAILED) {
257                 GST_ERROR_OBJECT(self,"cannot mmap cdb: %s (%d)", strerror(errno));
258                 return;
259         }
260
261 #ifdef dump
262         self->dumpfd = open("/media/hdd/movie/dreamaudiosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
263         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
264 #endif
265 }
266
267 static void
268 gst_dreamaudiosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
269 {
270         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
271
272         switch (prop_id) {
273                 case ARG_BITRATE:
274                         gst_dreamaudiosource_set_bitrate(self, g_value_get_int (value));
275                         break;
276                 case ARG_INPUT_MODE:
277                              gst_dreamaudiosource_set_input_mode (self, g_value_get_enum (value));
278                         break;
279                 default:
280                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
281                         break;
282         }
283 }
284
285 static void
286 gst_dreamaudiosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
287 {
288         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (object);
289
290         switch (prop_id) {
291                 case ARG_BITRATE:
292                         g_value_set_int (value, self->audio_info.bitrate/1000);
293                         break;
294                 case ARG_INPUT_MODE:
295                         g_value_set_enum (value, gst_dreamaudiosource_get_input_mode (self));
296                         break;
297                 default:
298                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
299                         break;
300         }
301 }
302
303 static GstCaps *
304 gst_dreamaudiosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
305 {
306         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
307         GstPadTemplate *pad_template;
308         GstCaps *caps;
309
310         pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
311         g_return_val_if_fail (pad_template != NULL, NULL);
312
313         if (self->encoder == NULL) {
314                 GST_DEBUG_OBJECT (self, "encoder not opened -> use template caps");
315                 caps = gst_pad_template_get_caps (pad_template);
316         }
317         else
318         {
319                 caps = gst_caps_make_writable(gst_pad_template_get_caps (pad_template));
320         }
321
322         GST_DEBUG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
323         return caps;
324 }
325
326 static gboolean gst_dreamaudiosource_unlock (GstBaseSrc * bsrc)
327 {
328         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
329         GST_DEBUG_OBJECT (self, "stop creating buffers");
330         SEND_COMMAND (self, CONTROL_STOP);
331         return TRUE;
332 }
333
334 static void
335 gst_dreamaudiosource_free_buffer (struct _bufferdebug * bufferdebug)
336 {
337         GST_OBJECT_LOCK (bufferdebug->self);
338         GList *list = g_list_first (bufferdebug->self->buffers_list);
339         int count = 0;
340         while (list) {
341                 GST_TRACE_OBJECT (bufferdebug->self, "buffers_list[%i] = %" GST_PTR_FORMAT "", count, list->data);
342                 count++;
343                 list = g_list_next (list);
344         }
345         bufferdebug->self->buffers_list = g_list_remove(g_list_first (bufferdebug->self->buffers_list), bufferdebug->buffer);
346         GST_TRACE_OBJECT (bufferdebug->self, "removing %" GST_PTR_FORMAT " @ %" GST_TIME_FORMAT " from list -> new length=%i", bufferdebug->buffer, GST_TIME_ARGS (bufferdebug->buffer_pts), g_list_length(bufferdebug->self->buffers_list));
347         GST_OBJECT_UNLOCK (bufferdebug->self);
348 }
349
350 static GstFlowReturn
351 gst_dreamaudiosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
352 {
353         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (psrc);
354         EncoderInfo *enc = self->encoder;
355
356         static int dumpoffset;
357
358         GST_LOG_OBJECT (self, "new buffer requested");
359
360         if (!enc) {
361                 GST_WARNING_OBJECT (self, "encoder device not opened!");
362                 return GST_FLOW_ERROR;
363         }
364
365         while (1)
366         {
367                 *outbuf = NULL;
368
369                 if (self->descriptors_available == 0)
370                 {
371                         self->descriptors_count = 0;
372                         struct pollfd rfd[2];
373
374                         rfd[0].fd = enc->fd;
375                         rfd[0].events = POLLIN;
376                         rfd[1].fd = READ_SOCKET (self);
377                         rfd[1].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
378
379                         int ret = poll(rfd, 2, 200);
380
381                         if (G_UNLIKELY (ret == -1))
382                         {
383                                 GST_ERROR_OBJECT (self, "SELECT ERROR!");
384                                 return GST_FLOW_ERROR;
385                         }
386                         else if ( ret == 0 )
387                         {
388                                 GST_LOG_OBJECT (self, "SELECT TIMEOUT");
389                                 //!!! TODO generate dummy payload
390                                 *outbuf = gst_buffer_new();
391                         }
392                         else if ( G_UNLIKELY (rfd[1].revents) )
393                         {
394                                 char command;
395                                 READ_COMMAND (self, command, ret);
396                                 if (command == CONTROL_STOP)
397                                 {
398                                         GST_LOG_OBJECT (self, "CONTROL_STOP!");
399                                         return GST_FLOW_FLUSHING;
400                                 }
401                         }
402                         else if ( G_LIKELY(rfd[0].revents & POLLIN) )
403                         {
404                                 int rlen = read(enc->fd, enc->buffer, ABUFSIZE);
405                                 if (rlen <= 0 || rlen % ABDSIZE ) {
406                                         if ( errno == 512 )
407                                                 return GST_FLOW_FLUSHING;
408                                         GST_WARNING_OBJECT (self, "read error %s (%i)", strerror(errno), errno);
409                                         return GST_FLOW_ERROR;
410                                 }
411                                 self->descriptors_available = rlen / ABDSIZE;
412                                 GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
413                         }
414                 }
415
416                 while (self->descriptors_count < self->descriptors_available) {
417                         off_t offset = self->descriptors_count * ABDSIZE;
418                         AudioBufferDescriptor *desc = (AudioBufferDescriptor*)(&enc->buffer[offset]);
419
420                         uint32_t f = desc->stCommon.uiFlags;
421
422                         GST_LOG_OBJECT (self, "descriptors_count=%d, descriptors_available=%u\tuiOffset=%u, uiLength=%"G_GSIZE_FORMAT"", self->descriptors_count, self->descriptors_available, desc->stCommon.uiOffset, desc->stCommon.uiLength);
423
424                         if (f & CDB_FLAG_METADATA) {
425                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
426                                 self->descriptors_count = self->descriptors_available;
427                                 continue;
428                         }
429
430                         struct _bufferdebug * bdg = NULL;
431                         GDestroyNotify buffer_free_func = NULL;
432                         if ( gst_debug_category_get_threshold (dreamaudiosource_debug) >= GST_LEVEL_TRACE)
433                         {
434                                 bdg = malloc(sizeof(struct _bufferdebug));
435                                 buffer_free_func = (GDestroyNotify) gst_dreamaudiosource_free_buffer;
436                         }
437
438                         *outbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, AMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, bdg, buffer_free_func);
439
440                         if (bdg)
441                         {
442                                 bdg->self = self;
443                                 bdg->buffer = *outbuf;
444                                 self->buffers_list = g_list_append(self->buffers_list, *outbuf);
445                         }
446
447                         if (f & CDB_FLAG_PTS_VALID)
448                         {
449                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
450                                 {
451                                         if (self->dreamvideosrc)
452                                         {
453                                                 g_mutex_lock (&self->mutex);
454                                                 guint64 videosource_base_pts;
455                                                 g_signal_emit_by_name(self->dreamvideosrc, "get-base-pts", &videosource_base_pts);
456                                                 if (videosource_base_pts != GST_CLOCK_TIME_NONE)
457                                                 {
458                                                         GST_DEBUG_OBJECT (self, "use DREAMVIDEOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (videosource_base_pts) );
459                                                         self->base_pts = videosource_base_pts;
460                                                 }
461                                                 g_mutex_unlock (&self->mutex);
462                                         }
463                                         else
464                                         {
465                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
466                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
467                                         }
468                                 }
469                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
470                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
471                                 {
472                                         buffer_time -= self->base_pts;
473                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
474                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
475                                 }
476                                 if (bdg)
477                                         bdg->buffer_pts = buffer_time;
478                         }
479
480 #ifdef dump
481                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
482                         dumpoffset += wret;
483                         GST_LOG_OBJECT (self, "read %"G_GSIZE_FORMAT" dumped %i total 0x%08X", desc->stCommon.uiLength, wret, dumpoffset );
484 #endif
485                         self->descriptors_count++;
486
487                         break;
488                 }
489
490                 if (self->descriptors_count == self->descriptors_available) {
491                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
492                         /* release consumed descs */
493                         if (write(enc->fd, &self->descriptors_count, sizeof(self->descriptors_count)) != sizeof(self->descriptors_count)) {
494                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
495                                 return GST_FLOW_ERROR;
496                         }
497                         self->descriptors_available = 0;
498                 }
499
500                 if (*outbuf)
501                 {
502                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
503                         return GST_FLOW_OK;
504                 }
505         }
506
507         return GST_FLOW_ERROR;
508 }
509
510 static GstStateChangeReturn gst_dreamaudiosource_change_state (GstElement * element, GstStateChange transition)
511 {
512         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (element);
513         GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
514         int ret;
515         GST_OBJECT_LOCK (self);
516         switch (transition) {
517                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
518                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
519                         self->base_pts = GST_CLOCK_TIME_NONE;
520                         ret = ioctl(self->encoder->fd, AENC_START);
521                         if ( ret != 0 )
522                                 goto fail;
523                         self->descriptors_available = 0;
524                         CLEAR_COMMAND (self);
525                         GST_INFO_OBJECT (self, "started encoder!");
526                         break;
527                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
528                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED self->descriptors_count=%i self->descriptors_available=%i", self->descriptors_count, self->descriptors_available);
529                         while (self->descriptors_count < self->descriptors_available)
530                                 GST_LOG_OBJECT (self, "flushing self->descriptors_count=%i", self->descriptors_count++);
531                         if (self->descriptors_count)
532                                 write(self->encoder->fd, &self->descriptors_count, sizeof(self->descriptors_count));
533                         ret = ioctl(self->encoder->fd, AENC_STOP);
534                         if ( ret != 0 )
535                                 goto fail;
536                         GST_INFO_OBJECT (self, "stopped encoder!");
537                         break;
538                 default:
539                         break;
540         }
541         GST_OBJECT_UNLOCK (self);
542         if (GST_ELEMENT_CLASS (parent_class)->change_state)
543                 sret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
544         return sret;
545 fail:
546         GST_ERROR_OBJECT(self,"can't perform encoder ioctl!");
547         GST_OBJECT_UNLOCK (self);
548         return GST_STATE_CHANGE_FAILURE;
549 }
550
551 static gboolean
552 gst_dreamaudiosource_start (GstBaseSrc * bsrc)
553 {
554         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
555         self->dreamvideosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamvideosource0");
556
557         int control_sock[2];
558         if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
559         {
560                 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE, (NULL), GST_ERROR_SYSTEM);
561                 return FALSE;
562         }
563         READ_SOCKET (self) = control_sock[0];
564         WRITE_SOCKET (self) = control_sock[1];
565         fcntl (READ_SOCKET (self), F_SETFL, O_NONBLOCK);
566         fcntl (WRITE_SOCKET (self), F_SETFL, O_NONBLOCK);
567
568         GST_DEBUG_OBJECT (self, "started. reference to dreamvideosource=%" GST_PTR_FORMAT "", self->dreamvideosrc);
569         return TRUE;
570 }
571
572 static gboolean
573 gst_dreamaudiosource_stop (GstBaseSrc * bsrc)
574 {
575         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (bsrc);
576         if (self->dreamvideosrc)
577                 gst_object_unref(self->dreamvideosrc);
578         close (READ_SOCKET (self));
579         close (WRITE_SOCKET (self));
580         READ_SOCKET (self) = -1;
581         WRITE_SOCKET (self) = -1;
582         GST_DEBUG_OBJECT (self, "stop");
583         return TRUE;
584 }
585
586 static void
587 gst_dreamaudiosource_dispose (GObject * gobject)
588 {
589         GstDreamAudioSource *self = GST_DREAMAUDIOSOURCE (gobject);
590         if (self->encoder) {
591                 if (self->encoder->buffer)
592                         free(self->encoder->buffer);
593                 if (self->encoder->cdb)
594                         munmap(self->encoder->cdb, AMMAPSIZE);
595                 if (self->encoder->fd)
596                         close(self->encoder->fd);
597                 free(self->encoder);
598         }
599 #ifdef dump
600         close(self->dumpfd);
601 #endif
602         g_list_free(self->buffers_list);
603         g_mutex_clear (&self->mutex);
604         GST_DEBUG_OBJECT (self, "disposed");
605         G_OBJECT_CLASS (parent_class)->dispose (gobject);
606 }