allow independent setting of resolution and framerate, remove par from video_info...
[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 Multimedia GmbH.
13  *
14  * This program is NOT free software. It is open source, you are allowed
15  * to modify it (if you keep the license), but it may not be commercially
16  * distributed other than under the conditions noted above.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <gst/gst.h>
24 #include "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_finalize (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->finalize = gst_dreamvideosource_finalize;
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         GST_INFO_OBJECT (self, "requested to set resolution to %dx%d, framerate to %d/%d", info->width, info->height, info->fps_n, info->fps_d);
171
172         if (info->fps_n > 0)
173         {
174                 int venc_fps = 0;
175                 switch (info->fps_n) {
176                         case 25:
177                                 venc_fps = rate_25;
178                                 break;
179                         case 30:
180                                 venc_fps = rate_30;
181                                 break;
182                         case 50:
183                                 venc_fps = rate_50;
184                                 break;
185                         case 60:
186                                 venc_fps = rate_60;
187                                 break;
188                         default:
189                                 GST_ERROR_OBJECT (self, "invalid framerate %d/%d", info->fps_n, info->fps_d);
190                                 goto fail;
191                 }
192                 if (!ioctl(self->encoder->fd, VENC_SET_FRAMERATE, &venc_fps))
193                         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);
194                 else
195                 {
196                         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);
197                         goto fail;
198                 }
199         }
200
201         if (info->width && info->height)
202         {
203                 int venc_size = 0;
204                 if ( info->width == 720 && info->height == 576 )
205                         venc_size = fmt_720x576;
206                 else if ( info->width == 1280 && info->height == 720)
207                         venc_size = fmt_1280x720;
208                 else if ( info->width == 1920 && info->height == 1080)
209                         venc_size = fmt_1920x1080;
210                 else
211                 {
212                         GST_ERROR_OBJECT (self, "invalid resolution %dx%d", info->width, info->height);
213                         goto fail;
214                 }
215                 if (!ioctl(self->encoder->fd, VENC_SET_RESOLUTION, &venc_size))
216                         GST_INFO_OBJECT (self, "set resolution to %dx%d -> ioctrl(%d, VENC_SET_RESOLUTION, &%d)", info->width, info->height, self->encoder->fd, venc_size);
217                 else
218                 {
219                         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);
220                         goto fail;
221                 }
222         }
223         
224         self->video_info = *info;
225         GST_OBJECT_UNLOCK (self);
226         return TRUE;
227         
228 fail:
229 //      g_mutex_unlock (&self->mutex);
230         GST_OBJECT_UNLOCK (self);
231         return FALSE;
232 }
233
234
235 gboolean
236 gst_dreamvideosource_plugin_init (GstPlugin *plugin)
237 {
238         GST_DEBUG_CATEGORY_INIT (dreamvideosource_debug, "dreamvideosource", 0, "dreamvideosource");
239         return gst_element_register (plugin, "dreamvideosource", GST_RANK_PRIMARY, GST_TYPE_DREAMVIDEOSOURCE);
240 }
241
242 static void
243 gst_dreamvideosource_init (GstDreamVideoSource * self)
244 {
245         GstPadTemplate *pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
246         self->current_caps = gst_pad_template_get_caps (pad_template);
247
248         self->encoder = NULL;
249         self->descriptors_available = 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_INFO_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         GST_LOG_OBJECT (self, "gst_dreamvideosource_free_buffer");
427 }
428
429 static GstFlowReturn
430 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
431 {
432         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
433         EncoderInfo *enc = self->encoder;
434         
435         GST_LOG_OBJECT (self, "new buffer requested");
436
437         if (!enc) {
438                 GST_WARNING_OBJECT (self, "encoder device not opened!");
439                 return GST_FLOW_ERROR;
440         }
441         
442         while (1)
443         {
444                 *outbuf = NULL;
445                 
446                 if (self->descriptors_available == 0)
447                 {
448                         self->descriptors_count = 0;
449                         int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
450                         if (rlen <= 0 || rlen % VBDSIZE ) {
451                                 if ( errno == 512 )
452                                         return GST_FLOW_FLUSHING;
453                                 GST_WARNING_OBJECT (self, "read error %s", strerror(errno));
454                                 return GST_FLOW_ERROR;
455                         }
456                         self->descriptors_available = rlen / VBDSIZE;
457                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
458                 }
459
460                 while (self->descriptors_count < self->descriptors_available) {
461                         off_t offset = self->descriptors_count * VBDSIZE;
462                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
463
464                         uint32_t f = desc->stCommon.uiFlags;
465
466                         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);
467
468                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
469                         { 
470                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
471                                 self->descriptors_count = self->descriptors_available;
472                                 continue;
473                         }
474                         
475                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
476                         {
477                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
478                                 {
479                                         if (self->dreamaudiosrc)
480                                         {
481                                                 g_mutex_lock (&self->mutex);
482                                                 guint64 audiosource_base_pts;
483                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
484                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
485                                                 {
486                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
487                                                         self->base_pts = audiosource_base_pts;
488                                                 }
489                                                 g_mutex_unlock (&self->mutex);
490                                         }
491                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
492                                         {
493                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
494                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
495                                         }
496                                 }
497                         }
498                         
499                         *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);
500                         
501                         if (f & CDB_FLAG_PTS_VALID)
502                         {
503                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
504                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
505                                 {
506                                         buffer_time -= self->base_pts;
507                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
508                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
509                                 }
510                         }
511 #ifdef dump
512                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
513                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
514 #endif
515
516                         self->descriptors_count++;
517
518                         break;
519                 }
520
521                 if (self->descriptors_count == self->descriptors_available) {
522                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
523                         /* release consumed descs */
524                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
525                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
526                                 return GST_FLOW_ERROR;
527                         }
528                         self->descriptors_available = 0;
529                 }
530                 
531                 if (*outbuf)
532                 {
533                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
534                         return GST_FLOW_OK;
535                 }
536
537         }
538         return GST_FLOW_ERROR;
539 }
540
541 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition)
542 {
543         g_return_val_if_fail (GST_DREAMVIDEOSOURCE (element), FALSE);
544         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
545         int ret;
546
547         switch (transition) {
548                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
549                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
550                         self->base_pts = GST_CLOCK_TIME_NONE;
551                         ret = ioctl(self->encoder->fd, VENC_START);
552                         if ( ret != 0 )
553                         {
554                                 GST_ERROR_OBJECT(self,"can't start encoder ioctl!");
555                                 return GST_STATE_CHANGE_FAILURE;
556                         }
557                         GST_INFO_OBJECT (self, "started encoder!");
558                         break;
559                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
560                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED");
561                         ret = ioctl(self->encoder->fd, VENC_STOP);
562                         if ( ret != 0 )
563                         {
564                                 GST_ERROR_OBJECT(self,"can't stop encoder ioctl!");
565                                 return GST_STATE_CHANGE_FAILURE;
566                         }
567                         GST_INFO_OBJECT (self, "stopped encoder!");
568                         break;
569                 default:
570                         break;
571         }
572
573         if (GST_ELEMENT_CLASS (parent_class)->change_state)
574                 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
575
576         return GST_STATE_CHANGE_SUCCESS;
577 }
578
579 static gboolean
580 gst_dreamvideosource_start (GstBaseSrc * bsrc)
581 {
582         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
583         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
584         GST_DEBUG_OBJECT (self, "started. reference to dreamaudiosource=%" GST_PTR_FORMAT"", self->dreamaudiosrc);
585         return TRUE;
586 }
587
588 static gboolean
589 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
590 {
591         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
592         GST_DEBUG_OBJECT (self, "stop");
593         return TRUE;
594 }
595
596 static void
597 gst_dreamvideosource_finalize (GObject * gobject)
598 {
599         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
600         if (self->encoder) {
601                 if (self->encoder->buffer)
602                         free(self->encoder->buffer);
603                 if (self->encoder->cdb) 
604                         munmap(self->encoder->cdb, VMMAPSIZE);
605                 free(self->encoder);
606         }
607 #ifdef dump
608         close(self->dumpfd);
609 #endif
610         if (self->current_caps)
611                 gst_caps_unref(self->current_caps);
612         g_mutex_clear (&self->mutex);
613         GST_DEBUG_OBJECT (self, "finalized");
614         G_OBJECT_CLASS (parent_class)->finalize (gobject);
615 }