use pts/dts correctly
[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                                 g_mutex_lock (&self->mutex);
602                                 if (self->flushing)
603                                 {
604                                         GST_DEBUG_OBJECT (self, "FLUSHING!");
605                                         g_cond_signal (&self->cond);
606                                         continue;
607                                 }
608                                 g_mutex_unlock (&self->mutex);
609                                 GST_DEBUG_OBJECT (self, "SELECT TIMEOUT");
610                                 //!!! TODO generate valid dummy payload
611                                 readbuf = gst_buffer_new();
612                         }
613                         else if ( rfd[0].revents )
614                         {
615                                 char command;
616                                 READ_COMMAND (self, command, ret);
617                                 if (command == CONTROL_STOP)
618                                 {
619                                         GST_DEBUG_OBJECT (self, "CONTROL_STOP!");
620                                         goto stop_running;
621                                 }
622                         }
623                         else if ( G_LIKELY(rfd[1].revents & POLLIN) )
624                         {
625                                 int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
626                                 if (rlen <= 0 || rlen % VBDSIZE ) {
627                                         if ( errno == 512 )
628                                                 goto stop_running;
629                                         GST_WARNING_OBJECT (self, "read error %s (%i)", strerror(errno), errno);
630                                         goto stop_running;
631                                 }
632                                 self->descriptors_available = rlen / VBDSIZE;
633                                 GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
634                         }
635                 }
636
637                 while (self->descriptors_count < self->descriptors_available)
638                 {
639                         off_t offset = self->descriptors_count * VBDSIZE;
640                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
641
642                         uint32_t f = desc->stCommon.uiFlags;
643
644                         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);
645
646                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
647                         {
648                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
649                                 self->descriptors_count = self->descriptors_available;
650                                 continue;
651                         }
652
653                         readbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, VMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, self, NULL);
654
655                         GstClockTime buffer_dts = GST_CLOCK_TIME_NONE;
656                         GstClockTime buffer_pts = GST_CLOCK_TIME_NONE;
657
658                         // uiDTS since kernel driver booted
659                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
660                         {
661                                 buffer_dts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
662                                 GST_LOG_OBJECT (self, "f & VBD_FLAG_DTS_VALID && encoder's uiDTS=%" GST_TIME_FORMAT"", GST_TIME_ARGS(buffer_dts));
663
664                                 g_mutex_lock (&self->mutex);
665                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
666                                 {
667                                         if (self->dreamaudiosrc)
668                                         {
669                                                 guint64 audiosource_base_pts;
670                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
671                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
672                                                 {
673                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
674                                                         self->base_pts = audiosource_base_pts;
675                                                 }
676                                         }
677                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
678                                         {
679                                                 self->base_pts = buffer_dts;
680                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT" (%lld)", GST_TIME_ARGS (self->base_pts), desc->uiDTS);
681                                         }
682                                 }
683                                 g_mutex_unlock (&self->mutex);
684
685                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_dts >= self->base_pts )
686                                 {
687                                         buffer_dts -= self->base_pts;
688                                         GST_BUFFER_DTS(readbuf) = buffer_dts;
689                                         GST_LOG_OBJECT (self, "corrected dts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (GST_BUFFER_DTS(readbuf)));
690                                 }
691                         }
692
693                         if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
694                         {
695                                 GST_DEBUG_OBJECT (self, "self->base_pts == GST_CLOCK_TIME_NONE! skip this frame");
696                                 self->descriptors_count++;
697                                 break;
698                         }
699
700                         if (self->video_info.fps_d)
701                                 GST_BUFFER_DURATION(readbuf) = gst_util_uint64_scale (GST_SECOND, self->video_info.fps_d, self->video_info.fps_n);
702
703 //                      if (f & CDB_FLAG_PTS_VALID)
704 //                      {
705 //                              GstClock *clock = gst_element_get_clock (GST_ELEMENT(self));
706 //                              if (clock)
707 //                              {
708 //                                      GstClockTime clock_time, base_time;
709 //                                      clock_time = gst_clock_get_time (clock);
710 //                                      base_time = gst_element_get_base_time (GST_ELEMENT(self));
711 // 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",
712 // 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) );
713 // 
714 //                                      if (clock_time && clock_time > base_time)
715 //                                      {
716 //                                              buffer_time = clock_time - base_time;
717 //                                              gst_object_unref (clock);
718 //      //                                      buffer_time -= GST_BUFFER_DURATION(readbuf);
719 //                                              GST_BUFFER_PTS(readbuf) = buffer_time;
720 //                                              GST_BUFFER_DTS(readbuf) = buffer_time;
721 //
722 //                                      }
723 //                                      else
724 //                                              buffer_time = GST_CLOCK_TIME_NONE;
725 //                              }
726 //                      }
727
728                         if (/*(!clock || buffer_time == GST_CLOCK_TIME_NONE) && */f & CDB_FLAG_PTS_VALID)
729                         {
730                                 buffer_pts = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
731                                 GST_LOG_OBJECT (self, "f & CDB_FLAG_PTS_VALID && encoder's  uiPTS=%" GST_TIME_FORMAT"", GST_TIME_ARGS(buffer_pts));
732                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_pts >= self->base_pts )
733                                 {
734                                         buffer_pts -= self->base_pts/* + GST_BUFFER_DURATION(readbuf)*/;
735                                         GST_BUFFER_PTS(readbuf) = buffer_pts;
736                                         GST_INFO_OBJECT (self, "currected pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (GST_BUFFER_PTS(readbuf)));
737                                 }
738                         }
739
740 #ifdef dump
741                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
742                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
743 #endif
744                         self->descriptors_count++;
745                         break;
746                 }
747
748                 if (self->descriptors_count == self->descriptors_available)
749                 {
750                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
751                         /* release consumed descs */
752                         if (write(enc->fd, &self->descriptors_count, sizeof(self->descriptors_count)) != sizeof(self->descriptors_count)) {
753                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
754                                 goto stop_running;
755                         }
756                         self->descriptors_available = 0;
757                 }
758
759                 if (readbuf)
760                 {
761                         g_mutex_lock (&self->mutex);
762                         if (!self->flushing)
763                         {
764                                 while (g_queue_get_length (&self->current_frames) >= self->buffer_size)
765                                 {
766                                         GstBuffer * oldbuf = g_queue_pop_head (&self->current_frames);
767                                         GST_WARNING_OBJECT (self, "dropping %" GST_PTR_FORMAT " because of queue overflow! buffers count=%i", oldbuf, g_queue_get_length (&self->current_frames));
768                                         gst_buffer_unref(oldbuf);
769                                 }
770                                 g_queue_push_tail (&self->current_frames, readbuf);
771                         }
772                         g_cond_signal (&self->cond);
773                         GST_INFO_OBJECT (self, "read %" GST_PTR_FORMAT " to queue", readbuf );
774                         g_mutex_unlock (&self->mutex);
775                 }
776         }
777
778         g_assert_not_reached ();
779         return;
780
781         stop_running:
782         {
783                 g_mutex_unlock (&self->mutex);
784                 g_cond_signal (&self->cond);
785                 GST_DEBUG ("stop running, exit thread");
786                 message = gst_message_new_stream_status (GST_OBJECT_CAST (self), GST_STREAM_STATUS_TYPE_ENTER, GST_ELEMENT_CAST (GST_OBJECT_PARENT(self)));
787                 g_value_init (&val, GST_TYPE_G_THREAD);
788                 g_value_set_boxed (&val, self->readthread);
789                 gst_message_set_stream_status_object (message, &val);
790                 g_value_unset (&val);
791                 GST_DEBUG_OBJECT (self, "posting LEAVE stream status");
792                 gst_element_post_message (GST_ELEMENT_CAST (self), message);
793                 return;
794         }
795 }
796
797 static GstFlowReturn
798 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
799 {
800         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
801
802         GST_LOG_OBJECT (self, "new buffer requested");
803
804         g_mutex_lock (&self->mutex);
805         while (g_queue_is_empty (&self->current_frames) && !self->flushing)
806         {
807                 g_cond_wait (&self->cond, &self->mutex);
808                 GST_INFO_OBJECT (self, "waiting for buffer from encoder");
809         }
810
811         *outbuf = g_queue_pop_head (&self->current_frames);
812         g_mutex_unlock (&self->mutex);
813
814         if (*outbuf)
815         {
816                 GST_INFO_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
817                 return GST_FLOW_OK;
818         }
819         GST_INFO_OBJECT (self, "FLUSHING");
820         return GST_FLOW_FLUSHING;
821 }
822
823 static GstStateChangeReturn gst_dreamvideosource_change_state (GstElement * element, GstStateChange transition)
824 {
825         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
826         GstStateChangeReturn sret = GST_STATE_CHANGE_SUCCESS;
827         int ret;
828
829         switch (transition) {
830                 case GST_STATE_CHANGE_NULL_TO_READY:
831                 {
832                         if (!(self->encoder && self->encoder->cdb))
833                                 return GST_STATE_CHANGE_FAILURE;
834                         int control_sock[2];
835                         if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
836                         {
837                                 GST_ELEMENT_ERROR (self, RESOURCE, OPEN_READ_WRITE, (NULL), GST_ERROR_SYSTEM);
838                                 return GST_STATE_CHANGE_FAILURE;
839                         }
840                         READ_SOCKET (self) = control_sock[0];
841                         WRITE_SOCKET (self) = control_sock[1];
842                         fcntl (READ_SOCKET (self), F_SETFL, O_NONBLOCK);
843                         fcntl (WRITE_SOCKET (self), F_SETFL, O_NONBLOCK);
844                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_NULL_TO_READY");
845                         break;
846                 }
847                 case GST_STATE_CHANGE_READY_TO_PAUSED:
848                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_READY_TO_PAUSED");
849 #ifdef PROVIDE_CLOCK
850                         GstMessage* msg;
851                         msg = gst_message_new_clock_provide (GST_OBJECT_CAST (element), self->encoder_clock, TRUE);
852                         GST_INFO_OBJECT (self, "clock: %" GST_PTR_FORMAT " %" GST_PTR_FORMAT " typename=%s", self->encoder_clock, msg, GST_MESSAGE_TYPE_NAME(msg));
853                         gst_element_post_message (element, msg);
854 #endif
855                         self->flushing = TRUE;
856                         self->readthread = g_thread_try_new ("dreamvideosrc-read", (GThreadFunc) gst_dreamvideosource_read_thread_func, self, NULL);
857                         GST_DEBUG_OBJECT (self, "started readthread @%p", self->readthread );
858                         break;
859                 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
860                         g_mutex_lock (&self->mutex);
861                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_PAUSED_TO_PLAYING");
862                         self->base_pts = GST_CLOCK_TIME_NONE;
863                         ret = ioctl(self->encoder->fd, VENC_START);
864                         if ( ret != 0 )
865                                 goto fail;
866                         self->descriptors_available = 0;
867                         CLEAR_COMMAND (self);
868                         GST_INFO_OBJECT (self, "started encoder!");
869                         g_mutex_unlock (&self->mutex);
870                         break;
871                 default:
872                         break;
873         }
874
875         if (GST_ELEMENT_CLASS (parent_class)->change_state)
876                 sret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
877
878         switch (transition) {
879                 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
880                         GST_LOG_OBJECT (self, "GST_STATE_CHANGE_READY_TO_PAUSED");
881                         g_mutex_lock (&self->mutex);
882                         self->flushing = TRUE;
883                         GST_DEBUG_OBJECT (self, "GST_STATE_CHANGE_PLAYING_TO_PAUSED self->descriptors_count=%i self->descriptors_available=%i", self->descriptors_count, self->descriptors_available);
884                         while (self->descriptors_count < self->descriptors_available)
885                         {
886                                 GST_LOG_OBJECT (self, "flushing self->descriptors_count=%i");
887                                 self->descriptors_count++;
888                         }
889                         if (self->descriptors_count)
890                                 write(self->encoder->fd, &self->descriptors_count, sizeof(self->descriptors_count));
891                         ret = ioctl(self->encoder->fd, VENC_STOP);
892                         if ( ret != 0 )
893                                 goto fail;
894 #ifdef PROVIDE_CLOCK
895                         gst_clock_set_master (self->encoder_clock, NULL);
896 #endif
897                         GST_INFO_OBJECT (self, "stopped encoder!");
898                         g_mutex_unlock (&self->mutex);
899                         break;
900                 case GST_STATE_CHANGE_PAUSED_TO_READY:
901                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_PAUSED_TO_READY");
902 #ifdef PROVIDE_CLOCK
903                         gst_element_post_message (element, gst_message_new_clock_lost (GST_OBJECT_CAST (element), self->encoder_clock));
904 #endif
905                         GST_DEBUG_OBJECT (self, "stopping readthread @%p...", self->readthread);
906                         SEND_COMMAND (self, CONTROL_STOP);
907                         g_thread_join (self->readthread);
908                         break;
909                 case GST_STATE_CHANGE_READY_TO_NULL:
910                         close (READ_SOCKET (self));
911                         close (WRITE_SOCKET (self));
912                         READ_SOCKET (self) = -1;
913                         WRITE_SOCKET (self) = -1;
914                         GST_DEBUG_OBJECT (self,"GST_STATE_CHANGE_READY_TO_NULL, close control sockets");
915                         break;
916                 default:
917                         break;
918         }
919
920         return sret;
921 fail:
922         GST_ERROR_OBJECT(self,"can't perform encoder ioctl!");
923         g_mutex_unlock (&self->mutex);
924         return GST_STATE_CHANGE_FAILURE;
925 }
926
927 static gboolean
928 gst_dreamvideosource_start (GstBaseSrc * bsrc)
929 {
930         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
931         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
932         GST_DEBUG_OBJECT (self, "started. reference to dreamaudiosource=%" GST_PTR_FORMAT"", self->dreamaudiosrc);
933         return TRUE;
934 }
935
936 static gboolean
937 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
938 {
939         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
940         if (self->dreamaudiosrc)
941                 gst_object_unref(self->dreamaudiosrc);
942         GST_DEBUG_OBJECT (self, "stop");
943         return TRUE;
944 }
945
946 static void
947 gst_dreamvideosource_dispose (GObject * gobject)
948 {
949         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
950         if (self->encoder) {
951                 if (self->encoder->buffer)
952                         free(self->encoder->buffer);
953                 if (self->encoder->cdb)
954                         munmap(self->encoder->cdb, VMMAPSIZE);
955                 if (self->encoder->fd)
956                         close(self->encoder->fd);
957                 free(self->encoder);
958         }
959 #ifdef PROVIDE_CLOCK
960         if (self->encoder_clock) {
961                 gst_object_unref (self->encoder_clock);
962                 self->encoder_clock = NULL;
963         }
964 #endif
965 #ifdef dump
966         close(self->dumpfd);
967 #endif
968         if (self->current_caps)
969                 gst_caps_unref(self->current_caps);
970         g_mutex_clear (&self->mutex);
971         g_cond_clear (&self->cond);
972         GST_DEBUG_OBJECT (self, "disposed");
973         G_OBJECT_CLASS (parent_class)->dispose (gobject);
974 }
975
976 #ifdef PROVIDE_CLOCK
977 static GstClock *gst_dreamvideosource_provide_clock (GstElement * element)
978 {
979         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (element);
980
981         if (!self->encoder || self->encoder->fd < 0)
982         {
983                 GST_DEBUG_OBJECT (self, "encoder device not started, can't provide clock!");
984                 return NULL;
985         }
986
987         return GST_CLOCK_CAST (gst_object_ref (self->encoder_clock));
988 }
989 #endif