move new_caps logic into getcaps, ease debuglevels for caps negotiation
[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 GType gst_dreamvideosource_input_mode_get_type (void)
30 {
31         static volatile gsize input_mode_type = 0;
32         static const GEnumValue input_mode[] = {
33                 {GST_DREAMVIDEOSOURCE_INPUT_MODE_LIVE, "GST_DREAMVIDEOSOURCE_INPUT_MODE_LIVE", "live"},
34                 {GST_DREAMVIDEOSOURCE_INPUT_MODE_HDMI_IN, "GST_DREAMVIDEOSOURCE_INPUT_MODE_HDMI_IN", "hdmi_in"},
35                 {GST_DREAMVIDEOSOURCE_INPUT_MODE_BACKGROUND, "GST_DREAMVIDEOSOURCE_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 ("GstDreamVideoSourceInputMode", 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
52 enum
53 {
54         ARG_0,
55         ARG_CAPS,
56         ARG_BITRATE,
57         ARG_INPUT_MODE
58 };
59
60 static guint gst_dreamvideosource_signals[LAST_SIGNAL] = { 0 };
61
62 #define DEFAULT_BITRATE     2048
63 #define DEFAULT_FRAMERATE   25
64 #define DEFAULT_WIDTH       1280
65 #define DEFAULT_HEIGHT      720
66 #define DEFAULT_INPUT_MODE  GST_DREAMVIDEOSOURCE_INPUT_MODE_LIVE
67 #define DEFAULT_BUFFER_SIZE 16
68
69 static GstStaticPadTemplate srctemplate =
70     GST_STATIC_PAD_TEMPLATE ("src",
71         GST_PAD_SRC,
72         GST_PAD_ALWAYS,
73         GST_STATIC_CAPS ("video/x-h264, "
74         "width = { 720, 1280, 1920 }, "
75         "height = { 576, 720, 1080 }, "
76         "framerate = { 25/1, 30/1, 50/1, 60/1 }, "
77         "pixel-aspect-ratio = { 5/4, 16/9 }, "
78         "stream-format = (string) byte-stream, "
79         "profile = (string) main")
80     );
81
82 #define gst_dreamvideosource_parent_class parent_class
83 G_DEFINE_TYPE (GstDreamVideoSource, gst_dreamvideosource, GST_TYPE_PUSH_SRC);
84
85 static GstCaps *gst_dreamvideosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
86 static gboolean gst_dreamvideosource_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
87 static GstCaps *gst_dreamvideosource_fixate (GstBaseSrc * bsrc, GstCaps * caps);
88
89 static gboolean gst_dreamvideosource_start (GstBaseSrc * bsrc);
90 static gboolean gst_dreamvideosource_stop (GstBaseSrc * bsrc);
91 static gboolean gst_dreamvideosource_unlock (GstBaseSrc * bsrc);
92 static gboolean gst_dreamvideosource_unlock_stop (GstBaseSrc * bsrc);
93 static void gst_dreamvideosource_dispose (GObject * gobject);
94 static GstFlowReturn gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf);
95
96 static void gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
97 static void gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
98
99 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition);
100 static gint64 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self);
101
102 static gboolean gst_dreamvideosource_encoder_init (GstDreamVideoSource * self);
103 static void gst_dreamvideosource_encoder_release (GstDreamVideoSource * self);
104
105 static void gst_dreamvideosource_read_thread_func (GstDreamVideoSource * self);
106
107 #ifdef PROVIDE_CLOCK
108 static GstClock *gst_dreamvideosource_provide_clock (GstElement * elem);
109 // static GstClockTime gst_dreamvideosource_get_encoder_time_ (GstClock * clock, GstBaseSrc * bsrc);
110 #endif
111
112 static void
113 gst_dreamvideosource_class_init (GstDreamVideoSourceClass * klass)
114 {
115         GObjectClass *gobject_class;
116         GstElementClass *gstelement_class;
117         GstBaseSrcClass *gstbsrc_class;
118         GstPushSrcClass *gstpush_src_class;
119
120         gobject_class = (GObjectClass *) klass;
121         gstelement_class = (GstElementClass *) klass;
122         gstbsrc_class = (GstBaseSrcClass *) klass;
123         gstpush_src_class = (GstPushSrcClass *) klass;
124
125         gobject_class->set_property = gst_dreamvideosource_set_property;
126         gobject_class->get_property = gst_dreamvideosource_get_property;
127         gobject_class->dispose = gst_dreamvideosource_dispose;
128
129         gst_element_class_add_pad_template (gstelement_class,
130                                             gst_static_pad_template_get (&srctemplate));
131
132         gst_element_class_set_static_metadata (gstelement_class,
133             "Dream Video source", "Source/Video",
134             "Provide an h.264 video elementary stream from Dreambox encoder device",
135             "Andreas Frisch <fraxinas@opendreambox.org>");
136
137         gstelement_class->change_state = gst_dreamvideosource_change_state;
138
139         gstbsrc_class->get_caps = gst_dreamvideosource_getcaps;
140         gstbsrc_class->set_caps = gst_dreamvideosource_setcaps;
141         gstbsrc_class->fixate = gst_dreamvideosource_fixate;
142         gstbsrc_class->start = gst_dreamvideosource_start;
143         gstbsrc_class->stop = gst_dreamvideosource_stop;
144         gstbsrc_class->unlock = gst_dreamvideosource_unlock;
145         gstbsrc_class->unlock_stop = gst_dreamvideosource_unlock_stop;
146
147         gstpush_src_class->create = gst_dreamvideosource_create;
148
149 #ifdef PROVIDE_CLOCK
150         gstelement_class->provide_clock = GST_DEBUG_FUNCPTR (gst_dreamvideosource_provide_clock);
151 //      g_type_class_ref (GST_TYPE_SYSTEM_CLOCK);
152 #endif
153
154         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
155           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
156             "Bitrate in kbit/sec", 16, 200000, DEFAULT_BITRATE,
157             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
158
159         g_object_class_install_property (gobject_class, ARG_CAPS,
160           g_param_spec_boxed ("caps", "Caps",
161             "The caps for the source stream", GST_TYPE_CAPS,
162             G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
163
164         g_object_class_install_property (gobject_class, ARG_INPUT_MODE,
165           g_param_spec_enum ("input-mode", "Input Mode",
166             "Select the input source of the video stream",
167             GST_TYPE_DREAMVIDEOSOURCE_INPUT_MODE, DEFAULT_INPUT_MODE,
168             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
169
170         gst_dreamvideosource_signals[SIGNAL_GET_BASE_PTS] =
171                 g_signal_new ("get-base-pts",
172                 G_TYPE_FROM_CLASS (klass),
173                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
174                 G_STRUCT_OFFSET (GstDreamVideoSourceClass, get_base_pts),
175                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
176
177         klass->get_base_pts = gst_dreamvideosource_get_base_pts;
178 }
179
180 static gint64
181 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self)
182 {
183         GST_DEBUG_OBJECT (self, "gst_dreamvideosource_get_base_pts %" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
184         return self->base_pts;
185 }
186
187 static void gst_dreamvideosource_set_bitrate (GstDreamVideoSource * self, uint32_t bitrate)
188 {
189         g_mutex_lock (&self->mutex);
190         uint32_t vbr = bitrate*1000;
191         if (!self->encoder || !self->encoder->fd)
192         {
193                 self->video_info.bitrate = bitrate;
194                 g_mutex_unlock (&self->mutex);
195                 return;
196         }
197
198         int ret = ioctl(self->encoder->fd, VENC_SET_BITRATE, &vbr);
199         if (ret != 0)
200         {
201                 GST_WARNING_OBJECT (self, "can't set video bitrate to %i bytes/s!", vbr);
202                 g_mutex_unlock (&self->mutex);
203                 return;
204         }
205         GST_INFO_OBJECT (self, "set video bitrate to %i kBytes/s", bitrate);
206         self->video_info.bitrate = bitrate;
207         g_mutex_unlock (&self->mutex);
208 }
209
210 static gboolean gst_dreamvideosource_set_format (GstDreamVideoSource * self, VideoFormatInfo * info)
211 {
212         g_mutex_lock (&self->mutex);
213         if (!self->encoder || !self->encoder->fd)
214         {
215                 self->video_info = *info;
216                 g_mutex_unlock (&self->mutex);
217                 return TRUE;
218         }
219
220         if (info->fps_n > 0)
221         {
222                 int venc_fps = 0;
223                 switch (info->fps_n) {
224                         case 25:
225                                 venc_fps = rate_25;
226                                 break;
227                         case 30:
228                                 venc_fps = rate_30;
229                                 break;
230                         case 50:
231                                 venc_fps = rate_50;
232                                 break;
233                         case 60:
234                                 venc_fps = rate_60;
235                                 break;
236                         default:
237                                 GST_ERROR_OBJECT (self, "invalid framerate %d/%d", info->fps_n, info->fps_d);
238                                 goto fail;
239                 }
240                 if (!ioctl(self->encoder->fd, VENC_SET_FRAMERATE, &venc_fps))
241                         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);
242                 else
243                 {
244                         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);
245                         goto fail;
246                 }
247         }
248
249         if (info->width && info->height)
250         {
251                 int venc_size = 0;
252                 if ( info->width == 720 && info->height == 576 )
253                         venc_size = fmt_720x576;
254                 else if ( info->width == 1280 && info->height == 720)
255                         venc_size = fmt_1280x720;
256                 else if ( info->width == 1920 && info->height == 1080)
257                         venc_size = fmt_1920x1080;
258                 else
259                 {
260                         GST_ERROR_OBJECT (self, "invalid resolution %dx%d", info->width, info->height);
261                         goto fail;
262                 }
263                 if (!ioctl(self->encoder->fd, VENC_SET_RESOLUTION, &venc_size))
264                         GST_INFO_OBJECT (self, "set resolution to %dx%d -> ioctrl(%d, VENC_SET_RESOLUTION, &%d)", info->width, info->height, self->encoder->fd, venc_size);
265                 else
266                 {
267                         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);
268                         goto fail;
269                 }
270         }
271
272         self->video_info = *info;
273         g_mutex_unlock (&self->mutex);
274         return TRUE;
275
276 fail:
277         g_mutex_unlock (&self->mutex);
278         return FALSE;
279 }
280
281 void gst_dreamvideosource_set_input_mode (GstDreamVideoSource *self, GstDreamVideoSourceInputMode mode)
282 {
283         g_return_if_fail (GST_IS_DREAMVIDEOSOURCE (self));
284         GEnumValue *val = g_enum_get_value (G_ENUM_CLASS (g_type_class_ref (GST_TYPE_DREAMVIDEOSOURCE_INPUT_MODE)), mode);
285         if (!val)
286         {
287                 GST_ERROR_OBJECT (self, "no such input_mode %i!", mode);
288                 return;
289         }
290         const gchar *value_nick = val->value_nick;
291
292         g_mutex_lock (&self->mutex);
293         if (!self->encoder || !self->encoder->fd)
294         {
295                 self->input_mode = mode;
296                 goto out;
297         }
298         int int_mode = mode;
299         int ret = ioctl(self->encoder->fd, VENC_SET_SOURCE, &int_mode);
300         if (ret != 0)
301         {
302                 GST_WARNING_OBJECT (self, "can't set input mode to %s (%i) error: %s", value_nick, mode, strerror(errno));
303                 goto out;
304         }
305         GST_INFO_OBJECT (self, "successfully set input mode to %s (%i)", value_nick, mode);
306         self->input_mode = mode;
307
308 out:
309         g_mutex_unlock (&self->mutex);
310         return;
311 }
312
313 GstDreamVideoSourceInputMode gst_dreamvideosource_get_input_mode (GstDreamVideoSource *self)
314 {
315         GstDreamVideoSourceInputMode result;
316         g_return_val_if_fail (GST_IS_DREAMVIDEOSOURCE (self), -1);
317         g_mutex_lock (&self->mutex);
318         result =self->input_mode;
319         g_mutex_unlock (&self->mutex);
320         return result;
321 }
322
323 gboolean
324 gst_dreamvideosource_plugin_init (GstPlugin *plugin)
325 {
326         GST_DEBUG_CATEGORY_INIT (dreamvideosource_debug, "dreamvideosource", 0, "dreamvideosource");
327         return gst_element_register (plugin, "dreamvideosource", GST_RANK_PRIMARY, GST_TYPE_DREAMVIDEOSOURCE);
328 }
329
330 static void
331 gst_dreamvideosource_init (GstDreamVideoSource * self)
332 {
333         GstPadTemplate *pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
334         self->current_caps = NULL;
335         self->new_caps = NULL;
336
337         self->encoder = NULL;
338         self->descriptors_available = 0;
339         self->input_mode = DEFAULT_INPUT_MODE;
340
341         self->buffer_size = DEFAULT_BUFFER_SIZE;
342         g_queue_init (&self->current_frames);
343         self->readthread = NULL;
344
345         g_mutex_init (&self->mutex);
346         g_cond_init (&self->cond);
347         READ_SOCKET (self) = -1;
348         WRITE_SOCKET (self) = -1;
349
350         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
351         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
352
353         self->encoder = NULL;
354         self->encoder_clock = NULL;
355
356 #ifdef dump
357         self->dumpfd = open("/media/hdd/movie/dreamvideosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
358         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
359 #endif
360 }
361
362 static gboolean gst_dreamvideosource_encoder_init (GstDreamVideoSource * self)
363 {
364         GST_LOG_OBJECT (self, "initializating encoder...");
365         self->encoder = malloc (sizeof(EncoderInfo));
366
367         if (!self->encoder) {
368                 GST_ERROR_OBJECT (self,"out of space");
369                 return FALSE;
370         }
371
372         char fn_buf[32];
373         sprintf(fn_buf, "/dev/venc%d", 0);
374         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
375         if (self->encoder->fd <= 0) {
376                 GST_ERROR_OBJECT (self,"cannot open device %s (%s)", fn_buf, strerror(errno));
377                 free(self->encoder);
378                 self->encoder = NULL;
379                 return FALSE;
380         }
381
382         self->encoder->buffer = malloc(VBUFSIZE);
383         if (!self->encoder->buffer) {
384                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
385                 return FALSE;
386         }
387
388         self->encoder->cdb = (unsigned char *)mmap (0, VMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
389
390         if (!self->encoder->cdb || self->encoder->cdb == MAP_FAILED) {
391                 GST_ERROR_OBJECT(self,"cannot alloc buffer: %s (%d)", strerror(errno));
392                 self->encoder->cdb = NULL;
393                 return FALSE;
394         }
395
396         int control_sock[2];
397         if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
398         {
399                 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE, (NULL), GST_ERROR_SYSTEM);
400                 return GST_STATE_CHANGE_FAILURE;
401         }
402         READ_SOCKET (self) = control_sock[0];
403         WRITE_SOCKET (self) = control_sock[1];
404         fcntl (READ_SOCKET (self), F_SETFL, O_NONBLOCK);
405         fcntl (WRITE_SOCKET (self), F_SETFL, O_NONBLOCK);
406
407         gst_dreamvideosource_set_bitrate (self, self->video_info.bitrate);
408         gst_dreamvideosource_set_format (self, &self->video_info);
409         gst_dreamvideosource_set_input_mode (self, self->input_mode);
410
411 #ifdef PROVIDE_CLOCK
412         self->encoder_clock = gst_dreamsource_clock_new ("GstDreamVideoSinkClock", self->encoder->fd);
413         GST_OBJECT_FLAG_SET (self, GST_ELEMENT_FLAG_PROVIDE_CLOCK);
414 #endif
415
416         GST_LOG_OBJECT (self, "encoder %s successfully initialized", fn_buf);
417         return TRUE;
418 }
419
420 static void gst_dreamvideosource_encoder_release (GstDreamVideoSource * self)
421 {
422         GST_LOG_OBJECT (self, "releasing encoder...");
423         if (self->encoder) {
424                 if (self->encoder->buffer)
425                         free(self->encoder->buffer);
426                 if (self->encoder->cdb)
427                         munmap(self->encoder->cdb, VMMAPSIZE);
428                 if (self->encoder->fd)
429                         close(self->encoder->fd);
430                 free(self->encoder);
431         }
432         self->encoder = NULL;
433         close (READ_SOCKET (self));
434         close (WRITE_SOCKET (self));
435         READ_SOCKET (self) = -1;
436         WRITE_SOCKET (self) = -1;
437         if (self->encoder_clock) {
438                 gst_object_unref (self->encoder_clock);
439                 self->encoder_clock = NULL;
440         }
441 }
442
443 static void
444 gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
445 {
446         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
447
448         switch (prop_id) {
449                 case ARG_CAPS:
450                 {
451                         if (self->new_caps)
452                                 gst_caps_unref (self->new_caps);
453                         self->new_caps = gst_caps_copy (gst_value_get_caps (value));
454                         gst_pad_mark_reconfigure (GST_BASE_SRC_PAD (GST_BASE_SRC(object)));
455                         break;
456                 }
457                 case ARG_BITRATE:
458                         gst_dreamvideosource_set_bitrate(self, g_value_get_int (value));
459                         break;
460                 case ARG_INPUT_MODE:
461                         gst_dreamvideosource_set_input_mode (self, g_value_get_enum (value));
462                         break;
463                 default:
464                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
465                         break;
466         }
467 }
468
469 static void
470 gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
471 {
472         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
473
474         switch (prop_id) {
475                 case ARG_CAPS:
476                         g_value_take_boxed (value, gst_dreamvideosource_getcaps (GST_BASE_SRC(object), GST_CAPS_ANY));
477                         break;
478                 case ARG_BITRATE:
479                         g_value_set_int (value, self->video_info.bitrate/1000);
480                         break;
481                 case ARG_INPUT_MODE:
482                         g_value_set_enum (value, gst_dreamvideosource_get_input_mode (self));
483                         break;
484                 default:
485                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
486                         break;
487         }
488 }
489
490 static GstCaps *
491 gst_dreamvideosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
492 {
493         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
494         GstCaps *caps;
495
496         if (self->new_caps)
497         {
498                 GST_LOG_OBJECT (self, "gst_dreamvideosource_getcaps has new_caps %" GST_PTR_FORMAT "", self->new_caps);
499                 if (self->current_caps)
500                 {
501                         gst_caps_replace (&caps, self->new_caps);
502                         self->new_caps = NULL;
503                 }
504                 else
505                         caps = gst_caps_copy (self->new_caps);
506         } else if (self->current_caps == NULL) {
507                 GstPadTemplate *pad_template;
508                 pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
509                 g_return_val_if_fail (pad_template != NULL, NULL);
510                 caps = gst_pad_template_get_caps (pad_template);
511         } else
512                 caps = gst_caps_copy(self->current_caps);
513
514         GST_LOG_OBJECT (self, "gst_dreamvideosource_getcaps %" GST_PTR_FORMAT " filter %" GST_PTR_FORMAT, caps, filter);
515
516         if (filter) {
517                 GstCaps *intersection;
518                 intersection = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
519                 gst_caps_unref (caps);
520                 caps = intersection;
521         }
522
523         GST_LOG_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
524         return caps;
525 }
526
527 static gboolean
528 gst_dreamvideosource_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
529 {
530         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
531         GstBaseSrcClass *bclass = GST_BASE_SRC_GET_CLASS (bsrc);
532         GstCaps *current_caps;
533         const GstStructure *structure;
534         VideoFormatInfo info;
535         gboolean ret;
536         int width, height;
537         const GValue *framerate, *par;
538
539         g_mutex_lock (&self->mutex);
540         structure = gst_caps_get_structure (caps, 0);
541
542         current_caps = gst_pad_get_current_caps (GST_BASE_SRC_PAD (bsrc));
543
544         GST_LOG_OBJECT (self, "gst_dreamvideosource_setcaps parameter %" GST_PTR_FORMAT " self->current_caps=%" GST_PTR_FORMAT " pad's current_caps=%" GST_PTR_FORMAT "  new_caps=%" GST_PTR_FORMAT"", caps, current_caps, self->current_caps, self->new_caps);
545
546         if (current_caps && gst_caps_is_equal (current_caps, caps)) {
547                 GST_LOG_OBJECT (self, "New caps equal to old ones: %" GST_PTR_FORMAT, caps);
548                 ret = TRUE;
549         } else {
550                 GstState state;
551                 gst_element_get_state (GST_ELEMENT(self), &state, NULL, 1*GST_MSECOND);
552                 if (state == GST_STATE_PLAYING)
553                 {
554                         GST_WARNING_OBJECT (self, "can't change caps while in PLAYING state %" GST_PTR_FORMAT, caps);
555                         g_mutex_unlock (&self->mutex);
556                         return TRUE;
557                 }
558                 else if (gst_structure_has_name (structure, "video/x-h264"))
559                 {
560                         memset (&info, 0, sizeof(VideoFormatInfo));
561                         ret = gst_structure_get_int (structure, "width", &info.width);
562                         ret &= gst_structure_get_int (structure, "height", &info.height);
563                         framerate = gst_structure_get_value (structure, "framerate");
564                         if (GST_VALUE_HOLDS_FRACTION(framerate)) {
565                                 info.fps_n = gst_value_get_fraction_numerator (framerate);
566                                 info.fps_d = gst_value_get_fraction_denominator (framerate);
567                         }
568                         GST_DEBUG_OBJECT (self, "set caps %" GST_PTR_FORMAT, caps);
569                         gst_caps_replace (&self->current_caps, caps);
570
571                         g_mutex_unlock (&self->mutex);
572                         if (gst_caps_is_fixed(caps) && gst_dreamvideosource_set_format(self, &info))
573                                 ret = gst_pad_push_event (bsrc->srcpad, gst_event_new_caps (caps));
574                 }
575                 else {
576                         GST_WARNING_OBJECT (self, "unsupported caps: %" GST_PTR_FORMAT, caps);
577                         ret = FALSE;
578                 }
579         }
580         if (current_caps)
581                 gst_caps_unref (current_caps);
582         g_mutex_unlock (&self->mutex);
583         return ret;
584 }
585
586 static GstCaps *
587 gst_dreamvideosource_fixate (GstBaseSrc * bsrc, GstCaps * caps)
588 {
589         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
590         GstStructure *structure;
591
592         caps = gst_caps_make_writable (caps);
593         structure = gst_caps_get_structure (caps, 0);
594
595         if (gst_structure_has_field (structure, "width"))
596                 gst_structure_fixate_field_nearest_int (structure, "width", DEFAULT_WIDTH);
597         if (gst_structure_has_field (structure, "height"))
598                 gst_structure_fixate_field_nearest_int (structure, "height", DEFAULT_HEIGHT);
599         if (gst_structure_has_field (structure, "framerate"))
600                 gst_structure_fixate_field_nearest_fraction (structure, "framerate", DEFAULT_FRAMERATE, 1);
601         if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
602                 gst_structure_fixate_field_nearest_fraction (structure, "pixel-aspect-ratio", DEFAULT_WIDTH, DEFAULT_HEIGHT);
603
604         caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
605         GST_DEBUG_OBJECT (bsrc, "fixated caps: %" GST_PTR_FORMAT, caps);
606         return caps;
607 }
608
609 static gboolean gst_dreamvideosource_unlock (GstBaseSrc * bsrc)
610 {
611         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
612         GST_DEBUG_OBJECT (self, "stop creating buffers");
613         g_mutex_lock (&self->mutex);
614         self->flushing = TRUE;
615         GST_DEBUG_OBJECT (self, "set flushing TRUE");
616         g_cond_signal (&self->cond);
617         g_mutex_unlock (&self->mutex);
618         return TRUE;
619 }
620
621 static gboolean gst_dreamvideosource_unlock_stop (GstBaseSrc * bsrc)
622 {
623         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
624         GST_DEBUG_OBJECT (self, "start creating buffers...");
625         g_mutex_lock (&self->mutex);
626         self->flushing = FALSE;
627         g_queue_foreach (&self->current_frames, (GFunc) gst_buffer_unref, NULL);
628         g_queue_clear (&self->current_frames);
629         g_mutex_unlock (&self->mutex);
630         return TRUE;
631 }
632
633 static void gst_dreamvideosource_read_thread_func (GstDreamVideoSource * self)
634 {
635         EncoderInfo *enc = self->encoder;
636         GstBuffer *readbuf;
637
638         if (!enc) {
639                 GST_WARNING_OBJECT (self, "encoder device not opened!");
640                 return;
641         }
642
643         GST_DEBUG_OBJECT (self, "enter read thread");
644
645         GstMessage *message;
646         GValue val = { 0 };
647
648         message = gst_message_new_stream_status (GST_OBJECT_CAST (self), GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (GST_OBJECT_PARENT(self)));
649         g_value_init (&val, GST_TYPE_G_THREAD);
650         g_value_set_boxed (&val, self->readthread);
651         gst_message_set_stream_status_object (message, &val);
652         g_value_unset (&val);
653         GST_DEBUG_OBJECT (self, "posting ENTER stream status");
654         gst_element_post_message (GST_ELEMENT_CAST (self), message);
655
656         while (TRUE) {
657                 readbuf = NULL;
658                 {
659                         struct pollfd rfd[2];
660                         int timeout, nfds;
661
662                         rfd[0].fd = READ_SOCKET (self);
663                         rfd[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
664
665                         if (self->descriptors_available == 0)
666                         {
667                                 rfd[1].fd = enc->fd;
668                                 rfd[1].events = POLLIN;
669                                 self->descriptors_count = 0;
670                                 timeout = 200;
671                                 nfds = 2;
672                         }
673                         else
674                         {
675                                 rfd[1].revents = 0;
676                                 nfds = 1;
677                                 timeout = 0;
678                         }
679
680                         int ret = poll(rfd, nfds, timeout);
681
682                         if (G_UNLIKELY (ret == -1))
683                         {
684                                 GST_ERROR_OBJECT (self, "SELECT ERROR!");
685                                 goto stop_running;
686                         }
687                         else if ( ret == 0 && self->descriptors_available == 0 )
688                         {
689                                 gst_clock_get_time(self->encoder_clock);
690                                 g_mutex_lock (&self->mutex);
691                                 if (self->flushing)
692                                 {
693                                         GST_DEBUG_OBJECT (self, "FLUSHING!");
694                                         g_cond_signal (&self->cond);
695                                         g_mutex_unlock (&self->mutex);
696                                         continue;
697                                 }
698                                 g_mutex_unlock (&self->mutex);
699                                 GST_DEBUG_OBJECT (self, "SELECT TIMEOUT");
700                                 //!!! TODO generate valid dummy payload
701                                 readbuf = gst_buffer_new();
702                         }
703                         else if ( rfd[0].revents )
704                         {
705                                 char command;
706                                 READ_COMMAND (self, command, ret);
707                                 if (command == CONTROL_STOP)
708                                 {
709                                         GST_DEBUG_OBJECT (self, "CONTROL_STOP!");
710                                         goto stop_running;
711                                 }
712                         }
713                         else if ( G_LIKELY(rfd[1].revents & POLLIN) )
714                         {
715                                 int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
716                                 if (rlen <= 0 || rlen % VBDSIZE ) {
717                                         if ( errno == 512 )
718                                                 goto stop_running;
719                                         GST_WARNING_OBJECT (self, "read error %s (%i)", strerror(errno), errno);
720                                         goto stop_running;
721                                 }
722                                 self->descriptors_available = rlen / VBDSIZE;
723                                 GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
724                         }
725                 }
726
727                 while (self->descriptors_count < self->descriptors_available)
728                 {
729                         off_t offset = self->descriptors_count * VBDSIZE;
730                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
731
732                         uint32_t f = desc->stCommon.uiFlags;
733
734                         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);
735
736                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
737                         {
738                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
739                                 self->descriptors_count = self->descriptors_available;
740                                 continue;
741                         }
742
743                         readbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, VMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, self, NULL);
744
745                         GstClockTime buffer_dts = GST_CLOCK_TIME_NONE;
746                         GstClockTime buffer_pts = GST_CLOCK_TIME_NONE;
747
748                         // uiDTS since kernel driver booted
749                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
750                         {
751                                 buffer_dts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
752                                 GST_LOG_OBJECT (self, "f & VBD_FLAG_DTS_VALID && encoder's uiDTS=%" GST_TIME_FORMAT"", GST_TIME_ARGS(buffer_dts));
753
754                                 g_mutex_lock (&self->mutex);
755                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
756                                 {
757                                         if (self->dreamaudiosrc)
758                                         {
759                                                 guint64 audiosource_base_pts;
760                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
761                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
762                                                 {
763                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
764                                                         self->base_pts = audiosource_base_pts;
765                                                 }
766                                         }
767                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
768                                         {
769                                                 self->base_pts = buffer_dts;
770                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT" (%lld)", GST_TIME_ARGS (self->base_pts), desc->uiDTS);
771                                         }
772                                 }
773                                 g_mutex_unlock (&self->mutex);
774
775                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_dts >= self->base_pts )
776                                 {
777                                         buffer_dts -= self->base_pts;
778                                         GST_BUFFER_DTS(readbuf) = buffer_dts;
779                                         GST_LOG_OBJECT (self, "corrected dts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (GST_BUFFER_DTS(readbuf)));
780                                 }
781                         }
782
783                         if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
784                         {
785                                 GST_DEBUG_OBJECT (self, "self->base_pts == GST_CLOCK_TIME_NONE! skip this frame");
786                                 self->descriptors_count++;
787                                 break;
788                         }
789
790                         if (self->video_info.fps_d)
791                                 GST_BUFFER_DURATION(readbuf) = gst_util_uint64_scale (GST_SECOND, self->video_info.fps_d, self->video_info.fps_n);
792
793 //                      if (f & CDB_FLAG_PTS_VALID)
794 //                      {
795 //                              GstClock *clock = gst_element_get_clock (GST_ELEMENT(self));
796 //                              if (clock)
797 //                              {
798 //                                      GstClockTime clock_time, base_time;
799 //                                      clock_time = gst_clock_get_time (clock);
800 //                                      base_time = gst_element_get_base_time (GST_ELEMENT(self));
801 // GST_ERROR_OBJECT (self, "\n%" GST_TIME_FORMAT "=mpeg pts\n%" GST_TIME_FORMAT "=dreamsource_clock_time\n%" GST_TIME_FORMAT "=base_time\n%" GST_TIME_FORMAT "=base_pts",
802 // GST_TIME_ARGS (MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS)), GST_TIME_ARGS (clock_time), GST_TIME_ARGS (base_time), GST_TIME_ARGS(self->base_pts) );
803 // 
804 //                                      if (clock_time && clock_time > base_time)
805 //                                      {
806 //                                              buffer_time = clock_time - base_time;
807 //                                              gst_object_unref (clock);
808 //      //                                      buffer_time -= GST_BUFFER_DURATION(readbuf);
809 //                                              GST_BUFFER_PTS(readbuf) = buffer_time;
810 //                                              GST_BUFFER_DTS(readbuf) = buffer_time;
811 //
812 //                                      }
813 //                                      else
814 //                                              buffer_time = GST_CLOCK_TIME_NONE;
815 //                              }
816 //                      }
817
818                         if (/*(!clock || buffer_time == GST_CLOCK_TIME_NONE) && */f & CDB_FLAG_PTS_VALID)
819                         {
820                                 buffer_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
821                                 GST_LOG_OBJECT (self, "f & CDB_FLAG_PTS_VALID && encoder's  uiPTS=%" GST_TIME_FORMAT"", GST_TIME_ARGS(buffer_pts));
822                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_pts >= self->base_pts )
823                                 {
824                                         buffer_pts -= self->base_pts/* + GST_BUFFER_DURATION(readbuf)*/;
825                                         GST_BUFFER_PTS(readbuf) = buffer_pts;
826                                         GST_INFO_OBJECT (self, "currected pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (GST_BUFFER_PTS(readbuf)));
827                                 }
828                         }
829
830 #ifdef dump
831                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
832                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
833 #endif
834                         self->descriptors_count++;
835                         break;
836                 }
837
838                 if (self->descriptors_count == self->descriptors_available)
839                 {
840                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
841                         /* release consumed descs */
842                         if (write(enc->fd, &self->descriptors_count, sizeof(self->descriptors_count)) != sizeof(self->descriptors_count)) {
843                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
844                                 goto stop_running;
845                         }
846                         self->descriptors_available = 0;
847                 }
848
849                 if (readbuf)
850                 {
851                         g_mutex_lock (&self->mutex);
852                         if (!self->flushing)
853                         {
854                                 while (g_queue_get_length (&self->current_frames) >= self->buffer_size)
855                                 {
856                                         GstBuffer * oldbuf = g_queue_pop_head (&self->current_frames);
857                                         GST_WARNING_OBJECT (self, "dropping %" GST_PTR_FORMAT " because of queue overflow! buffers count=%i", oldbuf, g_queue_get_length (&self->current_frames));
858                                         gst_buffer_unref(oldbuf);
859                                 }
860                                 g_queue_push_tail (&self->current_frames, readbuf);
861                         }
862                         else
863                                 gst_buffer_unref(readbuf);
864                         g_cond_signal (&self->cond);
865                         GST_INFO_OBJECT (self, "read %" GST_PTR_FORMAT " to queue", readbuf );
866                         g_mutex_unlock (&self->mutex);
867                 }
868         }
869
870         g_assert_not_reached ();
871         return;
872
873         stop_running:
874         {
875                 g_mutex_unlock (&self->mutex);
876                 g_cond_signal (&self->cond);
877                 GST_DEBUG ("stop running, exit thread");
878                 message = gst_message_new_stream_status (GST_OBJECT_CAST (self), GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (GST_OBJECT_PARENT(self)));
879                 g_value_init (&val, GST_TYPE_G_THREAD);
880                 g_value_set_boxed (&val, self->readthread);
881                 gst_message_set_stream_status_object (message, &val);
882                 g_value_unset (&val);
883                 GST_DEBUG_OBJECT (self, "posting LEAVE stream status");
884                 gst_element_post_message (GST_ELEMENT_CAST (self), message);
885                 return;
886         }
887 }
888
889 static GstFlowReturn
890 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
891 {
892         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
893
894         GST_LOG_OBJECT (self, "new buffer requested");
895
896         g_mutex_lock (&self->mutex);
897         while (g_queue_is_empty (&self->current_frames) && !self->flushing)
898         {
899                 g_cond_wait (&self->cond, &self->mutex);
900                 GST_INFO_OBJECT (self, "waiting for buffer from encoder");
901         }
902
903         *outbuf = g_queue_pop_head (&self->current_frames);
904         g_mutex_unlock (&self->mutex);
905
906         if (*outbuf)
907         {
908                 GST_INFO_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
909                 return GST_FLOW_OK;
910         }
911         GST_INFO_OBJECT (self, "FLUSHING");
912         return GST_FLOW_FLUSHING;
913 }
914
915
916 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition)
917 {
918         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
919         GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
920         int ret;
921
922         switch (transition) {
923                 case GST_STATE_CHANGE_NULL_TO_READY:
924                 {
925                         if (!gst_dreamvideosource_encoder_init (self))
926                                 return GST_STATE_CHANGE_FAILURE;
927                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_NULL_TO_READY");
928                         break;
929                 }
930                 case GST_STATE_CHANGE_READY_TO_PAUSED:
931                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_READY_TO_PAUSED");
932 #ifdef PROVIDE_CLOCK
933                         GstMessage* msg;
934                         msg = gst_message_new_clock_provide (GST_OBJECT_CAST (element), self->encoder_clock, TRUE);
935                         GST_INFO_OBJECT (self, "clock: %" GST_PTR_FORMAT " %" GST_PTR_FORMAT " typename=%s", self->encoder_clock, msg, GST_MESSAGE_TYPE_NAME(msg));
936                         gst_element_post_message (element, msg);
937 #endif
938                         self->flushing = TRUE;
939                         self->readthread = g_thread_try_new ("dreamvideosrc-read", (GThreadFunc) gst_dreamvideosource_read_thread_func, self, NULL);
940                         GST_DEBUG_OBJECT (self, "started readthread @%p", self->readthread );
941                         break;
942                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
943                         g_mutex_lock (&self->mutex);
944                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
945                         self->base_pts = GST_CLOCK_TIME_NONE;
946                         ret = ioctl(self->encoder->fd, VENC_START);
947                         if ( ret != 0 )
948                                 goto fail;
949                         self->descriptors_available = 0;
950                         CLEAR_COMMAND (self);
951                         GST_INFO_OBJECT (self, "started encoder!");
952                         g_mutex_unlock (&self->mutex);
953                         break;
954                 default:
955                         break;
956         }
957
958         if (GST_ELEMENT_CLASS (parent_class)->change_state)
959                 sret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
960
961         switch (transition) {
962                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
963                         g_mutex_lock (&self->mutex);
964                         self->flushing = TRUE;
965                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED self->descriptors_count=%i self->descriptors_available=%i", self->descriptors_count, self->descriptors_available);
966                         while (self->descriptors_count < self->descriptors_available)
967                         {
968                                 GST_LOG_OBJECT (self, "flushing self->descriptors_count=%i");
969                                 self->descriptors_count++;
970                         }
971                         if (self->descriptors_count)
972                                 write(self->encoder->fd, &self->descriptors_count, sizeof(self->descriptors_count));
973                         ret = ioctl(self->encoder->fd, VENC_STOP);
974                         if ( ret != 0 )
975                                 goto fail;
976 #ifdef PROVIDE_CLOCK
977                         gst_clock_set_master (self->encoder_clock, NULL);
978 #endif
979                         GST_INFO_OBJECT (self, "stopped encoder!");
980                         g_mutex_unlock (&self->mutex);
981                         break;
982                 case GST_STATE_CHANGE_PAUSED_TO_READY:
983                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PAUSED_TO_READY");
984 #ifdef PROVIDE_CLOCK
985                         gst_element_post_message (element, gst_message_new_clock_lost (GST_OBJECT_CAST (element), self->encoder_clock));
986 #endif
987                         GST_DEBUG_OBJECT (self, "stopping readthread @%p...", self->readthread);
988                         SEND_COMMAND (self, CONTROL_STOP);
989                         g_thread_join (self->readthread);
990                         break;
991                 case GST_STATE_CHANGE_READY_TO_NULL:
992                         gst_dreamvideosource_encoder_release (self);
993                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_READY_TO_NULL, close control sockets");
994                         break;
995                 default:
996                         break;
997         }
998
999         return sret;
1000 fail:
1001         GST_ERROR_OBJECT(self,"can't perform encoder ioctl!");
1002         g_mutex_unlock (&self->mutex);
1003         return GST_STATE_CHANGE_FAILURE;
1004 }
1005
1006 static gboolean
1007 gst_dreamvideosource_start (GstBaseSrc * bsrc)
1008 {
1009         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
1010         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
1011         GST_DEBUG_OBJECT (self, "started. reference to dreamaudiosource=%" GST_PTR_FORMAT"", self->dreamaudiosrc);
1012         return TRUE;
1013 }
1014
1015 static gboolean
1016 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
1017 {
1018         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
1019         if (self->dreamaudiosrc)
1020                 gst_object_unref(self->dreamaudiosrc);
1021         GST_DEBUG_OBJECT (self, "stop");
1022         return TRUE;
1023 }
1024
1025 static void
1026 gst_dreamvideosource_dispose (GObject * gobject)
1027 {
1028         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
1029 #ifdef PROVIDE_CLOCK
1030         if (self->encoder_clock) {
1031                 gst_object_unref (self->encoder_clock);
1032                 self->encoder_clock = NULL;
1033         }
1034 #endif
1035 #ifdef dump
1036         close(self->dumpfd);
1037 #endif
1038         if (self->current_caps)
1039                 gst_caps_unref(self->current_caps);
1040         if (self->new_caps)
1041                 gst_caps_unref(self->new_caps);
1042         g_mutex_clear (&self->mutex);
1043         g_cond_clear (&self->cond);
1044         GST_DEBUG_OBJECT (self, "disposed");
1045         G_OBJECT_CLASS (parent_class)->dispose (gobject);
1046 }
1047
1048 #ifdef PROVIDE_CLOCK
1049 static GstClock *gst_dreamvideosource_provide_clock (GstElement * element)
1050 {
1051         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
1052
1053         if (!self->encoder || self->encoder->fd < 0)
1054         {
1055                 GST_DEBUG_OBJECT (self, "encoder device not started, can't provide clock!");
1056                 return NULL;
1057         }
1058
1059         return GST_CLOCK_CAST (gst_object_ref (self->encoder_clock));
1060 }
1061 #endif