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