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