whitespace cleanup
[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
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         g_mutex_init (&self->mutex);
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 = malloc(sizeof(EncoderInfo));
255
256         if(!self->encoder) {
257                 GST_ERROR_OBJECT(self,"out of space");
258                 return;
259         }
260
261         char fn_buf[32];
262         sprintf(fn_buf, "/dev/venc%d", 0);
263         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
264         if(self->encoder->fd <= 0) {
265                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
266                 free(self->encoder);
267                 self->encoder = NULL;
268                 return;
269         }
270
271         self->encoder->buffer = malloc(VBUFSIZE);
272         if(!self->encoder->buffer) {
273                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
274                 return;
275         }
276
277         self->encoder->cdb = (unsigned char *)mmap(0, VMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
278
279         if(!self->encoder->cdb) {
280                 GST_ERROR_OBJECT(self,"cannot mmap cdb");
281                 return;
282         }
283
284 #ifdef dump
285         self->dumpfd = open("/media/hdd/movie/dreamvideosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
286         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
287 #endif
288
289         gst_dreamvideosource_set_bitrate(self, DEFAULT_BITRATE);
290 }
291
292 static void
293 gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
294 {
295         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
296
297         switch (prop_id) {
298                 case ARG_CAPS:
299                 {
300                         GstCaps *caps = gst_caps_copy (gst_value_get_caps (value));
301                         gst_dreamvideosource_setcaps(GST_BASE_SRC(object), caps);
302                         gst_caps_unref (caps);
303                         break;
304                 }
305                 case ARG_BITRATE:
306                         gst_dreamvideosource_set_bitrate(self, g_value_get_int (value));
307                         break;
308                 default:
309                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310                         break;
311         }
312 }
313
314 static void
315 gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
316 {
317         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
318
319         switch (prop_id) {
320                 case ARG_CAPS:
321                         g_value_take_boxed (value, gst_dreamvideosource_getcaps (GST_BASE_SRC(object), GST_CAPS_ANY));
322                         break;
323                 case ARG_BITRATE:
324                         g_value_set_int (value, self->video_info.bitrate/1000);
325                         break;
326                 default:
327                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
328                         break;
329         }
330 }
331
332 static GstCaps *
333 gst_dreamvideosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
334 {
335         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
336         GstCaps *caps = gst_caps_copy(self->current_caps);
337
338         GST_LOG_OBJECT (self, "gst_dreamvideosource_getcaps %" GST_PTR_FORMAT " filter %" GST_PTR_FORMAT, caps, filter);
339
340         if (filter) {
341                 GstCaps *intersection;
342                 intersection = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
343                 gst_caps_unref (caps);
344                 caps = intersection;
345         }
346
347         GST_DEBUG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
348         return caps;
349 }
350
351 static gboolean
352 gst_dreamvideosource_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
353 {
354         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
355         GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
356         GstCaps *current_caps;
357         const GstStructure *structure;
358         VideoFormatInfo info;
359         gboolean ret;
360         int width, height;
361         const GValue *framerate, *par;
362         structure = gst_caps_get_structure (caps, 0);
363
364         current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
365         if (current_caps && gst_caps_is_equal (current_caps, caps)) {
366                 GST_DEBUG_OBJECT (self, "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
367                 ret = TRUE;
368         } else {
369                 GstState state;
370                 gst_element_get_state (GST_ELEMENT(self), &state, NULL, 1*GST_MSECOND);
371                 if (state == GST_STATE_PLAYING)
372                 {
373                         GST_WARNING_OBJECT (self, "can't change caps while in PLAYING state %" GST_PTR_FORMAT, caps);
374                         return FALSE;
375                 }
376                 else if (gst_structure_has_name (structure, "video/x-h264"))
377                 {
378                         memset (&info, 0, sizeof(VideoFormatInfo));
379                         ret = gst_structure_get_int (structure, "width", &info.width);
380                         ret &= gst_structure_get_int (structure, "height", &info.height);
381                         framerate = gst_structure_get_value (structure, "framerate");
382                         if (GST_VALUE_HOLDS_FRACTION(framerate)) {
383                                 info.fps_n = gst_value_get_fraction_numerator (framerate);
384                                 info.fps_d = gst_value_get_fraction_denominator (framerate);
385                         }
386                         GST_INFO_OBJECT (self, "set caps %" GST_PTR_FORMAT, caps);
387                         gst_caps_replace (&self->current_caps, caps);
388
389                         if (gst_dreamvideosource_set_format(self, &info) && gst_caps_is_fixed(caps))
390                                 ret = gst_pad_push_event (bsrc->srcpad, gst_event_new_caps (caps));
391                 }
392                 else {
393                         GST_WARNING_OBJECT (self, "unsupported caps: %" GST_PTR_FORMAT, caps);
394                         ret = FALSE;
395                 }
396         }
397         if (current_caps)
398                 gst_caps_unref (current_caps);
399         return ret;
400 }
401
402 static GstCaps *
403 gst_dreamvideosource_fixate (GstBaseSrc * bsrc, GstCaps * caps)
404 {
405         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
406         GstStructure *structure;
407
408         caps = gst_caps_make_writable (caps);
409         structure = gst_caps_get_structure (caps, 0);
410
411         gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
412         gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
413         gst_structure_fixate_field_nearest_fraction (structure, "framerate", DEFAULT_FRAMERATE, 1);
414         gst_structure_fixate_field_nearest_fraction (structure, "pixel-aspect-ratio", DEFAULT_WIDTH, DEFAULT_HEIGHT);
415
416         caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
417         GST_DEBUG_OBJECT (bsrc, "fixate caps: %" GST_PTR_FORMAT, caps);
418         return caps;
419 }
420
421 static void
422 gst_dreamvideosource_free_buffer (GstDreamVideoSource * self)
423 {
424         GST_LOG_OBJECT (self, "gst_dreamvideosource_free_buffer");
425 }
426
427 static GstFlowReturn
428 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
429 {
430         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
431         EncoderInfo *enc = self->encoder;
432
433         GST_LOG_OBJECT (self, "new buffer requested");
434
435         if (!enc) {
436                 GST_WARNING_OBJECT (self, "encoder device not opened!");
437                 return GST_FLOW_ERROR;
438         }
439
440         while (1)
441         {
442                 *outbuf = NULL;
443
444                 if (self->descriptors_available == 0)
445                 {
446                         self->descriptors_count = 0;
447                         int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
448                         if (rlen <= 0 || rlen % VBDSIZE ) {
449                                 if ( errno == 512 )
450                                         return GST_FLOW_FLUSHING;
451                                 GST_WARNING_OBJECT (self, "read error %s", strerror(errno));
452                                 return GST_FLOW_ERROR;
453                         }
454                         self->descriptors_available = rlen / VBDSIZE;
455                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
456                 }
457
458                 while (self->descriptors_count < self->descriptors_available) {
459                         off_t offset = self->descriptors_count * VBDSIZE;
460                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
461
462                         uint32_t f = desc->stCommon.uiFlags;
463
464                         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);
465
466                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
467                         {
468                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
469                                 self->descriptors_count = self->descriptors_available;
470                                 continue;
471                         }
472
473                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
474                         {
475                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
476                                 {
477                                         if (self->dreamaudiosrc)
478                                         {
479                                                 g_mutex_lock (&self->mutex);
480                                                 guint64 audiosource_base_pts;
481                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
482                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
483                                                 {
484                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
485                                                         self->base_pts = audiosource_base_pts;
486                                                 }
487                                                 g_mutex_unlock (&self->mutex);
488                                         }
489                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
490                                         {
491                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
492                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
493                                         }
494                                 }
495                         }
496
497                         *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);
498
499                         if (f & CDB_FLAG_PTS_VALID)
500                         {
501                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
502                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
503                                 {
504                                         buffer_time -= self->base_pts;
505                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
506                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
507                                 }
508                         }
509 #ifdef dump
510                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
511                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
512 #endif
513
514                         self->descriptors_count++;
515
516                         break;
517                 }
518
519                 if (self->descriptors_count == self->descriptors_available) {
520                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
521                         /* release consumed descs */
522                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
523                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
524                                 return GST_FLOW_ERROR;
525                         }
526                         self->descriptors_available = 0;
527                 }
528
529                 if (*outbuf)
530                 {
531                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
532                         return GST_FLOW_OK;
533                 }
534
535         }
536         return GST_FLOW_ERROR;
537 }
538
539 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition)
540 {
541         g_return_val_if_fail (GST_DREAMVIDEOSOURCE (element), FALSE);
542         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
543         int ret;
544
545         switch (transition) {
546                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
547                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
548                         self->base_pts = GST_CLOCK_TIME_NONE;
549                         ret = ioctl(self->encoder->fd, VENC_START);
550                         if ( ret != 0 )
551                         {
552                                 GST_ERROR_OBJECT(self,"can't start encoder ioctl!");
553                                 return GST_STATE_CHANGE_FAILURE;
554                         }
555                         GST_INFO_OBJECT (self, "started encoder!");
556                         break;
557                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
558                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED");
559                         ret = ioctl(self->encoder->fd, VENC_STOP);
560                         if ( ret != 0 )
561                         {
562                                 GST_ERROR_OBJECT(self,"can't stop encoder ioctl!");
563                                 return GST_STATE_CHANGE_FAILURE;
564                         }
565                         GST_INFO_OBJECT (self, "stopped encoder!");
566                         break;
567                 default:
568                         break;
569         }
570
571         if (GST_ELEMENT_CLASS (parent_class)->change_state)
572                 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
573
574         return GST_STATE_CHANGE_SUCCESS;
575 }
576
577 static gboolean
578 gst_dreamvideosource_start (GstBaseSrc * bsrc)
579 {
580         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
581         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
582         GST_DEBUG_OBJECT (self, "started. reference to dreamaudiosource=%" GST_PTR_FORMAT"", self->dreamaudiosrc);
583         return TRUE;
584 }
585
586 static gboolean
587 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
588 {
589         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
590         GST_DEBUG_OBJECT (self, "stop");
591         return TRUE;
592 }
593
594 static void
595 gst_dreamvideosource_finalize (GObject * gobject)
596 {
597         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
598         if (self->encoder) {
599                 if (self->encoder->buffer)
600                         free(self->encoder->buffer);
601                 if (self->encoder->cdb)
602                         munmap(self->encoder->cdb, VMMAPSIZE);
603                 free(self->encoder);
604         }
605 #ifdef dump
606         close(self->dumpfd);
607 #endif
608         if (self->current_caps)
609                 gst_caps_unref(self->current_caps);
610         g_mutex_clear (&self->mutex);
611         GST_DEBUG_OBJECT (self, "finalized");
612         G_OBJECT_CLASS (parent_class)->finalize (gobject);
613 }