ease debugging overhead
[gst-plugin-dreamsource.git] / src / gstdreamvideosource.c
1 /*
2  * GStreamer dreamvideosource
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 "gstdreamvideosource.h"
25
26 GST_DEBUG_CATEGORY_STATIC (dreamvideosource_debug);
27 #define GST_CAT_DEFAULT dreamvideosource_debug
28
29 enum
30 {
31         SIGNAL_GET_BASE_PTS,
32         LAST_SIGNAL
33 };
34
35 enum
36 {
37   ARG_0,
38   ARG_CAPS,
39   ARG_BITRATE,
40 };
41
42 static guint gst_dreamvideosource_signals[LAST_SIGNAL] = { 0 };
43
44
45 #define DEFAULT_BITRATE 2048
46 #define DEFAULT_FRAMERATE 25
47 #define DEFAULT_WIDTH 1280
48 #define DEFAULT_HEIGHT 720
49
50 static GstStaticPadTemplate srctemplate =
51     GST_STATIC_PAD_TEMPLATE ("src",
52         GST_PAD_SRC,
53         GST_PAD_ALWAYS,
54         GST_STATIC_CAPS ("video/x-h264, "
55         "width = { 720, 1280, 1920 }, "
56         "height = { 576, 720, 1080 }, "
57         "framerate = { 25/1, 30/1, 50/1, 60/1 }, "
58         "pixel-aspect-ratio = { 5/4, 16/9 }, "
59         "stream-format = (string) byte-stream, "
60         "profile = (string) main")
61     );
62
63 #define gst_dreamvideosource_parent_class parent_class
64 G_DEFINE_TYPE (GstDreamVideoSource, gst_dreamvideosource, GST_TYPE_PUSH_SRC);
65
66 static GstCaps *gst_dreamvideosource_getcaps (GstBaseSrc * psrc, GstCaps * filter);
67 static gboolean gst_dreamvideosource_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
68 static GstCaps *gst_dreamvideosource_fixate (GstBaseSrc * bsrc, GstCaps * caps);
69
70 static gboolean gst_dreamvideosource_start (GstBaseSrc * bsrc);
71 static gboolean gst_dreamvideosource_stop (GstBaseSrc * bsrc);
72 static void gst_dreamvideosource_dispose (GObject * gobject);
73 static GstFlowReturn gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf);
74
75 static void gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
76 static void gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
77
78 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition);
79 static gint64 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self);
80
81 static void
82 gst_dreamvideosource_class_init (GstDreamVideoSourceClass * klass)
83 {
84         GObjectClass *gobject_class;
85         GstElementClass *gstelement_class;
86         GstBaseSrcClass *gstbsrc_class;
87         GstPushSrcClass *gstpush_src_class;
88
89         gobject_class = (GObjectClass *) klass;
90         gstelement_class = (GstElementClass *) klass;
91         gstbsrc_class = (GstBaseSrcClass *) klass;
92         gstpush_src_class = (GstPushSrcClass *) klass;
93
94         gobject_class->set_property = gst_dreamvideosource_set_property;
95         gobject_class->get_property = gst_dreamvideosource_get_property;
96         gobject_class->dispose = gst_dreamvideosource_dispose;
97
98         gst_element_class_add_pad_template (gstelement_class,
99                                             gst_static_pad_template_get (&srctemplate));
100
101         gst_element_class_set_static_metadata (gstelement_class,
102             "Dream Video source", "Source/Video",
103             "Provide an h.264 video elementary stream from Dreambox encoder device",
104             "Andreas Frisch <fraxinas@opendreambox.org>");
105
106         gstelement_class->change_state = gst_dreamvideosource_change_state;
107
108         gstbsrc_class->get_caps = gst_dreamvideosource_getcaps;
109         gstbsrc_class->set_caps = gst_dreamvideosource_setcaps;
110         gstbsrc_class->fixate = gst_dreamvideosource_fixate;
111         gstbsrc_class->start = gst_dreamvideosource_start;
112         gstbsrc_class->stop = gst_dreamvideosource_stop;
113
114         gstpush_src_class->create = gst_dreamvideosource_create;
115
116         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
117           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
118             "Bitrate in kbit/sec", 16, 200000, DEFAULT_BITRATE,
119             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
120
121         g_object_class_install_property (gobject_class, ARG_CAPS,
122           g_param_spec_boxed ("caps", "Caps",
123             "The caps for the source stream", GST_TYPE_CAPS,
124             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
125
126         gst_dreamvideosource_signals[SIGNAL_GET_BASE_PTS] =
127                 g_signal_new ("get-base-pts",
128                 G_TYPE_FROM_CLASS (klass),
129                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
130                 G_STRUCT_OFFSET (GstDreamVideoSourceClass, get_base_pts),
131                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
132
133         klass->get_base_pts = gst_dreamvideosource_get_base_pts;
134 }
135
136 static gint64
137 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self)
138 {
139         GST_DEBUG_OBJECT (self, "gst_dreamvideosource_get_base_pts %" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
140         return self->base_pts;
141 }
142
143 static void gst_dreamvideosource_set_bitrate (GstDreamVideoSource * self, uint32_t bitrate)
144 {
145         if (!self->encoder || !self->encoder->fd)
146                 return;
147         g_mutex_lock (&self->mutex);
148         uint32_t vbr = bitrate*1000;
149         int ret = ioctl(self->encoder->fd, VENC_SET_BITRATE, &vbr);
150         if (ret != 0)
151         {
152                 GST_WARNING_OBJECT (self, "can't set video bitrate to %i bytes/s!", vbr);
153                 g_mutex_unlock (&self->mutex);
154                 return;
155         }
156         GST_INFO_OBJECT (self, "set video bitrate to %i kBytes/s", bitrate);
157         self->video_info.bitrate = vbr;
158         g_mutex_unlock (&self->mutex);
159 }
160
161 static gboolean gst_dreamvideosource_set_format (GstDreamVideoSource * self, VideoFormatInfo * info)
162 {
163         if (!self->encoder || !self->encoder->fd)
164         {
165                 GST_ERROR_OBJECT (self, "can't set format because encoder device not opened!");
166                 return FALSE;
167         }
168
169         GST_OBJECT_LOCK (self);
170
171         if (info->fps_n > 0)
172         {
173                 int venc_fps = 0;
174                 switch (info->fps_n) {
175                         case 25:
176                                 venc_fps = rate_25;
177                                 break;
178                         case 30:
179                                 venc_fps = rate_30;
180                                 break;
181                         case 50:
182                                 venc_fps = rate_50;
183                                 break;
184                         case 60:
185                                 venc_fps = rate_60;
186                                 break;
187                         default:
188                                 GST_ERROR_OBJECT (self, "invalid framerate %d/%d", info->fps_n, info->fps_d);
189                                 goto fail;
190                 }
191                 if (!ioctl(self->encoder->fd, VENC_SET_FRAMERATE, &venc_fps))
192                         GST_INFO_OBJECT (self, "set framerate to %d/%d -> ioctrl(%d, VENC_SET_FRAMERATE, &%d)", info->fps_n, info->fps_d, self->encoder->fd, venc_fps);
193                 else
194                 {
195                         GST_WARNING_OBJECT (self, "can't set framerate to %d/%d -> ioctrl(%d, VENC_SET_FRAMERATE, &%d)", info->fps_n, info->fps_d, self->encoder->fd, venc_fps);
196                         goto fail;
197                 }
198         }
199
200         if (info->width && info->height)
201         {
202                 int venc_size = 0;
203                 if ( info->width == 720 && info->height == 576 )
204                         venc_size = fmt_720x576;
205                 else if ( info->width == 1280 && info->height == 720)
206                         venc_size = fmt_1280x720;
207                 else if ( info->width == 1920 && info->height == 1080)
208                         venc_size = fmt_1920x1080;
209                 else
210                 {
211                         GST_ERROR_OBJECT (self, "invalid resolution %dx%d", info->width, info->height);
212                         goto fail;
213                 }
214                 if (!ioctl(self->encoder->fd, VENC_SET_RESOLUTION, &venc_size))
215                         GST_INFO_OBJECT (self, "set resolution to %dx%d -> ioctrl(%d, VENC_SET_RESOLUTION, &%d)", info->width, info->height, self->encoder->fd, venc_size);
216                 else
217                 {
218                         GST_WARNING_OBJECT (self, "can't set resolution to %dx%d -> ioctrl(%d, VENC_SET_RESOLUTION, &%d)", info->width, info->height, self->encoder->fd, venc_size);
219                         goto fail;
220                 }
221         }
222
223         self->video_info = *info;
224         GST_OBJECT_UNLOCK (self);
225         return TRUE;
226
227 fail:
228         GST_OBJECT_UNLOCK (self);
229         return FALSE;
230 }
231
232
233 gboolean
234 gst_dreamvideosource_plugin_init (GstPlugin *plugin)
235 {
236         GST_DEBUG_CATEGORY_INIT (dreamvideosource_debug, "dreamvideosource", 0, "dreamvideosource");
237         return gst_element_register (plugin, "dreamvideosource", GST_RANK_PRIMARY, GST_TYPE_DREAMVIDEOSOURCE);
238 }
239
240 static void
241 gst_dreamvideosource_init (GstDreamVideoSource * self)
242 {
243         GstPadTemplate *pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
244         self->current_caps = gst_pad_template_get_caps (pad_template);
245
246         self->encoder = NULL;
247         self->descriptors_available = 0;
248
249         self->buffers_in_use = 0;
250
251         g_mutex_init (&self->mutex);
252
253         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
254         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
255
256         self->encoder = malloc(sizeof(EncoderInfo));
257
258         if(!self->encoder) {
259                 GST_ERROR_OBJECT(self,"out of space");
260                 return;
261         }
262
263         char fn_buf[32];
264         sprintf(fn_buf, "/dev/venc%d", 0);
265         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
266         if(self->encoder->fd <= 0) {
267                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
268                 free(self->encoder);
269                 self->encoder = NULL;
270                 return;
271         }
272
273         self->encoder->buffer = malloc(VBUFSIZE);
274         if(!self->encoder->buffer) {
275                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
276                 return;
277         }
278
279         self->encoder->cdb = (unsigned char *)mmap(0, VMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
280
281         if(!self->encoder->cdb) {
282                 GST_ERROR_OBJECT(self,"cannot mmap cdb");
283                 return;
284         }
285
286 #ifdef dump
287         self->dumpfd = open("/media/hdd/movie/dreamvideosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
288         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
289 #endif
290
291         gst_dreamvideosource_set_bitrate(self, DEFAULT_BITRATE);
292 }
293
294 static void
295 gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
296 {
297         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
298
299         switch (prop_id) {
300                 case ARG_CAPS:
301                 {
302                         GstCaps *caps = gst_caps_copy (gst_value_get_caps (value));
303                         gst_dreamvideosource_setcaps(GST_BASE_SRC(object), caps);
304                         gst_caps_unref (caps);
305                         break;
306                 }
307                 case ARG_BITRATE:
308                         gst_dreamvideosource_set_bitrate(self, g_value_get_int (value));
309                         break;
310                 default:
311                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
312                         break;
313         }
314 }
315
316 static void
317 gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
318 {
319         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
320
321         switch (prop_id) {
322                 case ARG_CAPS:
323                         g_value_take_boxed (value, gst_dreamvideosource_getcaps (GST_BASE_SRC(object), GST_CAPS_ANY));
324                         break;
325                 case ARG_BITRATE:
326                         g_value_set_int (value, self->video_info.bitrate/1000);
327                         break;
328                 default:
329                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
330                         break;
331         }
332 }
333
334 static GstCaps *
335 gst_dreamvideosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
336 {
337         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
338         GstCaps *caps = gst_caps_copy(self->current_caps);
339
340         GST_LOG_OBJECT (self, "gst_dreamvideosource_getcaps %" GST_PTR_FORMAT " filter %" GST_PTR_FORMAT, caps, filter);
341
342         if (filter) {
343                 GstCaps *intersection;
344                 intersection = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
345                 gst_caps_unref (caps);
346                 caps = intersection;
347         }
348
349         GST_DEBUG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
350         return caps;
351 }
352
353 static gboolean
354 gst_dreamvideosource_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
355 {
356         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
357         GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
358         GstCaps *current_caps;
359         const GstStructure *structure;
360         VideoFormatInfo info;
361         gboolean ret;
362         int width, height;
363         const GValue *framerate, *par;
364         structure = gst_caps_get_structure (caps, 0);
365
366         current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
367         if (current_caps && gst_caps_is_equal (current_caps, caps)) {
368                 GST_DEBUG_OBJECT (self, "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
369                 ret = TRUE;
370         } else {
371                 GstState state;
372                 gst_element_get_state (GST_ELEMENT(self), &state, NULL, 1*GST_MSECOND);
373                 if (state == GST_STATE_PLAYING)
374                 {
375                         GST_WARNING_OBJECT (self, "can't change caps while in PLAYING state %" GST_PTR_FORMAT, caps);
376                         return FALSE;
377                 }
378                 else if (gst_structure_has_name (structure, "video/x-h264"))
379                 {
380                         memset (&info, 0, sizeof(VideoFormatInfo));
381                         ret = gst_structure_get_int (structure, "width", &info.width);
382                         ret &= gst_structure_get_int (structure, "height", &info.height);
383                         framerate = gst_structure_get_value (structure, "framerate");
384                         if (GST_VALUE_HOLDS_FRACTION(framerate)) {
385                                 info.fps_n = gst_value_get_fraction_numerator (framerate);
386                                 info.fps_d = gst_value_get_fraction_denominator (framerate);
387                         }
388                         GST_INFO_OBJECT (self, "set caps %" GST_PTR_FORMAT, caps);
389                         gst_caps_replace (&self->current_caps, caps);
390
391                         if (gst_dreamvideosource_set_format(self, &info) && gst_caps_is_fixed(caps))
392                                 ret = gst_pad_push_event (bsrc->srcpad, gst_event_new_caps (caps));
393                 }
394                 else {
395                         GST_WARNING_OBJECT (self, "unsupported caps: %" GST_PTR_FORMAT, caps);
396                         ret = FALSE;
397                 }
398         }
399         if (current_caps)
400                 gst_caps_unref (current_caps);
401         return ret;
402 }
403
404 static GstCaps *
405 gst_dreamvideosource_fixate (GstBaseSrc * bsrc, GstCaps * caps)
406 {
407         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
408         GstStructure *structure;
409
410         caps = gst_caps_make_writable (caps);
411         structure = gst_caps_get_structure (caps, 0);
412
413         gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
414         gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
415         gst_structure_fixate_field_nearest_fraction (structure, "framerate", DEFAULT_FRAMERATE, 1);
416         gst_structure_fixate_field_nearest_fraction (structure, "pixel-aspect-ratio", DEFAULT_WIDTH, DEFAULT_HEIGHT);
417
418         caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
419         GST_DEBUG_OBJECT (bsrc, "fixate caps: %" GST_PTR_FORMAT, caps);
420         return caps;
421 }
422
423 static void
424 gst_dreamvideosource_free_buffer (GstDreamVideoSource * self)
425 {
426         self->buffers_in_use--;
427         GST_TRACE_OBJECT (self, "gst_dreamvideosource_free_buffer buffer still in use: %i", self->buffers_in_use);
428 }
429
430 static GstFlowReturn
431 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
432 {
433         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
434         EncoderInfo *enc = self->encoder;
435
436         GST_LOG_OBJECT (self, "new buffer requested");
437
438         if (!enc) {
439                 GST_WARNING_OBJECT (self, "encoder device not opened!");
440                 return GST_FLOW_ERROR;
441         }
442
443         while (1)
444         {
445                 *outbuf = NULL;
446
447                 if (self->descriptors_available == 0)
448                 {
449                         self->descriptors_count = 0;
450                         int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
451                         if (rlen <= 0 || rlen % VBDSIZE ) {
452                                 if ( errno == 512 )
453                                         return GST_FLOW_FLUSHING;
454                                 GST_WARNING_OBJECT (self, "read error %s", strerror(errno));
455                                 return GST_FLOW_ERROR;
456                         }
457                         self->descriptors_available = rlen / VBDSIZE;
458                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
459                 }
460
461                 while (self->descriptors_count < self->descriptors_available) {
462                         off_t offset = self->descriptors_count * VBDSIZE;
463                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
464
465                         uint32_t f = desc->stCommon.uiFlags;
466
467                         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);
468
469                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
470                         {
471                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
472                                 self->descriptors_count = self->descriptors_available;
473                                 continue;
474                         }
475
476                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
477                         {
478                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
479                                 {
480                                         if (self->dreamaudiosrc)
481                                         {
482                                                 g_mutex_lock (&self->mutex);
483                                                 guint64 audiosource_base_pts;
484                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
485                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
486                                                 {
487                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
488                                                         self->base_pts = audiosource_base_pts;
489                                                 }
490                                                 g_mutex_unlock (&self->mutex);
491                                         }
492                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
493                                         {
494                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
495                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
496                                         }
497                                 }
498                         }
499
500                         *outbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, VMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, self, (GDestroyNotify)gst_dreamvideosource_free_buffer);
501
502                         if (f & CDB_FLAG_PTS_VALID)
503                         {
504                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
505                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
506                                 {
507                                         buffer_time -= self->base_pts;
508                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
509                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
510                                 }
511                         }
512 #ifdef dump
513                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
514                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
515 #endif
516
517                         self->descriptors_count++;
518
519                         break;
520                 }
521
522                 if (self->descriptors_count == self->descriptors_available) {
523                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
524                         /* release consumed descs */
525                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
526                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
527                                 return GST_FLOW_ERROR;
528                         }
529                         self->descriptors_available = 0;
530                 }
531
532                 if (*outbuf)
533                 {
534                         self->buffers_in_use++;
535                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
536                         return GST_FLOW_OK;
537                 }
538
539         }
540         return GST_FLOW_ERROR;
541 }
542
543 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition)
544 {
545         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
546         int ret;
547
548         switch (transition) {
549                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
550                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
551                         self->base_pts = GST_CLOCK_TIME_NONE;
552                         ret = ioctl(self->encoder->fd, VENC_START);
553                         if ( ret != 0 )
554                         {
555                                 GST_ERROR_OBJECT(self,"can't start encoder ioctl!");
556                                 return GST_STATE_CHANGE_FAILURE;
557                         }
558                         self->descriptors_available = 0;
559                         GST_INFO_OBJECT (self, "started encoder!");
560                         break;
561                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
562                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED self->descriptors_count=%i self->descriptors_available=%i", self->descriptors_count, self->descriptors_available);
563                         GST_OBJECT_LOCK (self);
564                         while (self->descriptors_count < self->descriptors_available)
565                                 GST_INFO_OBJECT (self, "flushing self->descriptors_count=%i", self->descriptors_count++);
566                         if (self->descriptors_count)
567                                 write(self->encoder->fd, &self->descriptors_count, 4);
568                         ret = ioctl(self->encoder->fd, VENC_STOP);
569                         GST_OBJECT_UNLOCK (self);
570                         if ( ret != 0 )
571                         {
572                                 GST_ERROR_OBJECT(self,"can't stop encoder ioctl!");
573                                 return GST_STATE_CHANGE_FAILURE;
574                         }
575                         GST_INFO_OBJECT (self, "stopped encoder!");
576                         break;
577                 default:
578                         break;
579         }
580         if (GST_ELEMENT_CLASS (parent_class)->change_state)
581                 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
582         return GST_STATE_CHANGE_SUCCESS;
583 }
584
585 static gboolean
586 gst_dreamvideosource_start (GstBaseSrc * bsrc)
587 {
588         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
589         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
590         GST_DEBUG_OBJECT (self, "started. reference to dreamaudiosource=%" GST_PTR_FORMAT"", self->dreamaudiosrc);
591         return TRUE;
592 }
593
594 static gboolean
595 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
596 {
597         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
598         if (self->dreamaudiosrc)
599                 gst_object_unref(self->dreamaudiosrc);
600         GST_DEBUG_OBJECT (self, "stop");
601         return TRUE;
602 }
603
604 static void
605 gst_dreamvideosource_dispose (GObject * gobject)
606 {
607         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
608         if (self->encoder) {
609                 if (self->encoder->buffer)
610                         free(self->encoder->buffer);
611                 if (self->encoder->cdb)
612                         munmap(self->encoder->cdb, VMMAPSIZE);
613                 if (self->encoder->fd)
614                         close(self->encoder->fd);
615                 free(self->encoder);
616         }
617 #ifdef dump
618         close(self->dumpfd);
619 #endif
620         if (self->current_caps)
621                 gst_caps_unref(self->current_caps);
622         g_mutex_clear (&self->mutex);
623         GST_DEBUG_OBJECT (self, "disposed");
624         G_OBJECT_CLASS (parent_class)->dispose (gobject);
625 }