add signal to request each other's base pts value, don't dump by default, various...
[gst-plugin-dreamsource.git] / src / gstdreamvideosource.c
1 /*
2  * GStreamer dreamvideosource
3  * Copyright 2014-2015 Andreas Frisch <fraxinas@opendreambox.org>
4  *
5  * This program is licensed under the Creative Commons
6  * Attribution-NonCommercial-ShareAlike 3.0 Unported
7  * License. To view a copy of this license, visit
8  * http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
9  * Creative Commons,559 Nathan Abbott Way,Stanford,California 94305,USA.
10  *
11  * Alternatively, this program may be distributed and executed on
12  * hardware which is licensed by Dream Multimedia GmbH.
13  *
14  * This program is NOT free software. It is open source, you are allowed
15  * to modify it (if you keep the license), but it may not be commercially
16  * distributed other than under the conditions noted above.
17  */
18
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
22
23 #include <gst/gst.h>
24 #include "gstdreamvideosource.h"
25
26 GST_DEBUG_CATEGORY_STATIC (dreamvideosource_debug);
27 #define GST_CAT_DEFAULT dreamvideosource_debug
28
29 enum
30 {
31         SIGNAL_GET_BASE_PTS,
32         LAST_SIGNAL
33 };
34
35 enum
36 {
37   ARG_0,
38   ARG_BITRATE,
39 };
40
41 static guint gst_dreamvideosource_signals[LAST_SIGNAL] = { 0 };
42
43
44 #define DEFAULT_BITRATE 2048
45
46 static GstStaticPadTemplate srctemplate =
47     GST_STATIC_PAD_TEMPLATE ("src",
48         GST_PAD_SRC,
49         GST_PAD_ALWAYS,
50         GST_STATIC_CAPS ("video/x-h264, "
51         "width = " GST_VIDEO_SIZE_RANGE ", "
52         "height = " GST_VIDEO_SIZE_RANGE ", "
53         "framerate = " GST_VIDEO_FPS_RANGE ", "
54         "stream-format = (string) byte-stream, "
55         "profile = (string) main")
56     );
57
58 #define gst_dreamvideosource_parent_class parent_class
59 G_DEFINE_TYPE (GstDreamVideoSource, gst_dreamvideosource, GST_TYPE_PUSH_SRC);
60
61 static GstCaps *gst_dreamvideosource_getcaps (GstBaseSrc * psrc, GstCaps * filter);
62 static gboolean gst_dreamvideosource_start (GstBaseSrc * bsrc);
63 static gboolean gst_dreamvideosource_stop (GstBaseSrc * bsrc);
64 static void gst_dreamvideosource_finalize (GObject * gobject);
65 static GstFlowReturn gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf);
66
67 static void gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec);
68 static void gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec);
69
70 static gint64 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self);
71
72 static void
73 gst_dreamvideosource_class_init (GstDreamVideoSourceClass * klass)
74 {
75         GObjectClass *gobject_class;
76         GstElementClass *gstelement_class;
77         GstBaseSrcClass *gstbasesrc_class;
78         GstPushSrcClass *gstpush_src_class;
79
80         gobject_class = (GObjectClass *) klass;
81         gstelement_class = (GstElementClass *) klass;
82         gstbasesrc_class = (GstBaseSrcClass *) klass;
83         gstpush_src_class = (GstPushSrcClass *) klass;
84
85         gobject_class->set_property = gst_dreamvideosource_set_property;
86         gobject_class->get_property = gst_dreamvideosource_get_property;
87         gobject_class->finalize = gst_dreamvideosource_finalize;
88
89         gst_element_class_add_pad_template (gstelement_class,
90                                             gst_static_pad_template_get (&srctemplate));
91
92         gst_element_class_set_static_metadata (gstelement_class,
93             "Dream Video source", "Source/Video",
94             "Provide an h.264 video elementary stream from Dreambox encoder device",
95             "Andreas Frisch <fraxinas@opendreambox.org>");
96
97         gstbasesrc_class->get_caps = gst_dreamvideosource_getcaps;
98         gstbasesrc_class->start = gst_dreamvideosource_start;
99         gstbasesrc_class->stop = gst_dreamvideosource_stop;
100
101         gstpush_src_class->create = gst_dreamvideosource_create;
102         
103         g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_BITRATE,
104           g_param_spec_int ("bitrate", "Bitrate (kb/s)",
105             "Bitrate in kbit/sec", 16, 200000, DEFAULT_BITRATE,
106             G_PARAM_CONSTRUCT | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
107
108            gst_dreamvideosource_signals[SIGNAL_GET_BASE_PTS] =
109                 g_signal_new ("get-base-pts",
110                 G_TYPE_FROM_CLASS (klass),
111                 G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
112                 G_STRUCT_OFFSET (GstDreamVideoSourceClass, get_base_pts),
113                 NULL, NULL, gst_dreamsource_marshal_INT64__VOID, G_TYPE_INT64, 0);
114
115         klass->get_base_pts = gst_dreamvideosource_get_base_pts;
116 }
117
118 static gint64
119 gst_dreamvideosource_get_base_pts (GstDreamVideoSource *self)
120 {
121         GST_DEBUG_OBJECT (self, "gst_dreamvideosource_get_base_pts " GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
122         return self->base_pts;
123 }
124
125 gboolean
126 gst_dreamvideosource_plugin_init (GstPlugin *plugin)
127 {
128         GST_DEBUG_CATEGORY_INIT (dreamvideosource_debug, "dreamvideosource", 0, "dreamvideosource");
129         return gst_element_register (plugin, "dreamvideosource", GST_RANK_PRIMARY, GST_TYPE_DREAMVIDEOSOURCE);
130 }
131
132 static void
133 gst_dreamvideosource_init (GstDreamVideoSource * self)
134 {
135         self->encoder = NULL;
136         self->descriptors_available = 0;
137         self->video_info.width = 1280;
138         self->video_info.height = 720;
139         self->video_info.par_n = 16;
140         self->video_info.par_d = 16;
141         self->video_info.fps_n = 25;
142         self->video_info.fps_d = 1;
143         self->base_pts = GST_CLOCK_TIME_NONE;
144
145         g_mutex_init (&self->mutex);
146         
147         gst_base_src_set_format (GST_BASE_SRC (self), GST_FORMAT_TIME);
148         gst_base_src_set_live (GST_BASE_SRC (self), TRUE);
149 }
150
151 static void
152 gst_dreamvideosource_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec)
153 {
154         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
155         
156         switch (prop_id) {
157                 case ARG_BITRATE:
158                         self->video_info.bitrate = g_value_get_int (value);
159                         break;
160                 default:
161                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
162                         break;
163         }
164 }
165
166 static void
167 gst_dreamvideosource_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec)
168 {
169         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (object);
170         
171         switch (prop_id) {
172                 case ARG_BITRATE:
173                         g_value_set_int (value, self->video_info.bitrate);
174                         break;
175                 default:
176                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
177                         break;
178         }
179 }
180
181 static GstCaps *
182 gst_dreamvideosource_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
183 {
184         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
185         GstPadTemplate *pad_template;
186         GstCaps *caps;
187
188         pad_template = gst_element_class_get_pad_template (GST_ELEMENT_GET_CLASS(self), "src");
189         g_return_val_if_fail (pad_template != NULL, NULL);
190
191         if (self->encoder == NULL) {
192                 GST_DEBUG_OBJECT (self, "encoder not opened -> use template caps");
193                 caps = gst_pad_template_get_caps (pad_template);
194         }
195         else
196         {
197                 caps = gst_caps_make_writable(gst_pad_template_get_caps (pad_template));
198                 gst_caps_set_simple(caps, "width", G_TYPE_INT, self->video_info.width, NULL);
199                 gst_caps_set_simple(caps, "height", G_TYPE_INT, self->video_info.height, NULL);
200                 gst_caps_set_simple(caps, "framerate", GST_TYPE_FRACTION, self->video_info.fps_n, self->video_info.fps_d, NULL);
201         }
202
203         GST_INFO_OBJECT (self, "return caps %" GST_PTR_FORMAT, caps);
204         return caps;
205 }
206
207 static void
208 gst_dreamvideosource_free_buffer (GstDreamVideoSource * self)
209 {
210         GST_LOG_OBJECT (self, "gst_dreamvideosource_free_buffer");
211 }
212
213 static GstFlowReturn
214 gst_dreamvideosource_create (GstPushSrc * psrc, GstBuffer ** outbuf)
215 {
216         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (psrc);
217         EncoderInfo *enc = self->encoder;
218         
219         GST_LOG_OBJECT (self, "new buffer requested");
220
221         if (!enc) {
222                 GST_WARNING_OBJECT (self, "encoder device not opened!");
223                 return GST_FLOW_ERROR;
224         }
225         
226         while (1)
227         {
228                 *outbuf = NULL;
229                 
230                 if (self->descriptors_available == 0)
231                 {
232                         self->descriptors_count = 0;
233                         int rlen = read(enc->fd, enc->buffer, VBUFSIZE);
234                         if (rlen <= 0 || rlen % VBDSIZE ) {
235                                 GST_WARNING_OBJECT (self, "read error %d (errno %i)", rlen, errno);
236                                 return GST_FLOW_ERROR;
237                         }
238                         self->descriptors_available = rlen / VBDSIZE;
239                         GST_LOG_OBJECT (self, "encoder buffer was empty, %d descriptors available", self->descriptors_available);
240                 }
241
242                 while (self->descriptors_count < self->descriptors_available) {
243                         off_t offset = self->descriptors_count * VBDSIZE;
244                         VideoBufferDescriptor *desc = (VideoBufferDescriptor*)(&enc->buffer[offset]);
245
246                         uint32_t f = desc->stCommon.uiFlags;
247
248                         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);
249
250                         if (G_UNLIKELY (f & CDB_FLAG_METADATA))
251                         { 
252                                 GST_LOG_OBJECT (self, "CDB_FLAG_METADATA... skip outdated packet");
253                                 self->descriptors_count = self->descriptors_available;
254                                 continue;
255                         }
256                         
257                         if (f & VBD_FLAG_DTS_VALID && desc->uiDTS)
258                         {
259                                 if (G_UNLIKELY (self->base_pts == GST_CLOCK_TIME_NONE))
260                                 {
261                                         if (self->dreamaudiosrc)
262                                         {
263                                                 g_mutex_lock (&self->mutex);
264                                                 guint64 audiosource_base_pts;
265                                                 g_signal_emit_by_name(self->dreamaudiosrc, "get-base-pts", &audiosource_base_pts);
266                                                 if (audiosource_base_pts != GST_CLOCK_TIME_NONE)
267                                                 {
268                                                         GST_DEBUG_OBJECT (self, "use DREAMAUDIOSOURCE's base_pts=%" GST_TIME_FORMAT "", GST_TIME_ARGS (audiosource_base_pts) );
269                                                         self->base_pts = audiosource_base_pts;
270                                                 }
271                                                 g_mutex_unlock (&self->mutex);
272                                         }
273                                         if (self->base_pts == GST_CLOCK_TIME_NONE)
274                                         {
275                                                 self->base_pts = MPEGTIME_TO_GSTTIME(desc->uiDTS);
276                                                 GST_DEBUG_OBJECT (self, "use mpeg stream pts as base_pts=%" GST_TIME_FORMAT"", GST_TIME_ARGS (self->base_pts) );
277                                         }
278                                 }
279                         }
280                         
281                         *outbuf = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY, enc->cdb, VMMAPSIZE, desc->stCommon.uiOffset, desc->stCommon.uiLength, self, (GDestroyNotify)gst_dreamvideosource_free_buffer);
282                         
283                         if (f & CDB_FLAG_PTS_VALID)
284                         {
285                                 GstClockTime buffer_time = MPEGTIME_TO_GSTTIME(desc->stCommon.uiPTS);
286                                 if (self->base_pts != GST_CLOCK_TIME_NONE && buffer_time > self->base_pts )
287                                 {
288                                         buffer_time -= self->base_pts;
289                                         GST_BUFFER_PTS(*outbuf) = buffer_time;
290                                         GST_BUFFER_DTS(*outbuf) = buffer_time;
291                                 }
292                         }
293 #ifdef dump
294                         int wret = write(self->dumpfd, (unsigned char*)(enc->cdb + desc->stCommon.uiOffset), desc->stCommon.uiLength);
295                         GST_LOG_OBJECT (self, "read %i dumped %i total %" G_GSIZE_FORMAT " ", desc->stCommon.uiLength, wret, gst_buffer_get_size (*outbuf) );
296 #endif
297
298                         self->descriptors_count++;
299
300                         break;
301                 }
302
303                 if (self->descriptors_count == self->descriptors_available) {
304                         GST_LOG_OBJECT (self, "self->descriptors_count == self->descriptors_available -> release %i consumed descriptors", self->descriptors_count);
305                         /* release consumed descs */
306                         if (write(enc->fd, &self->descriptors_count, 4) != 4) {
307                                 GST_WARNING_OBJECT (self, "release consumed descs write error!");
308                                 return GST_FLOW_ERROR;
309                         }
310                         self->descriptors_available = 0;
311                 }
312                 
313                 if (*outbuf)
314                 {
315                         GST_DEBUG_OBJECT (self, "pushing %" GST_PTR_FORMAT "", *outbuf );
316                         return GST_FLOW_OK;
317                 }
318
319         }
320         return GST_FLOW_ERROR;
321 }
322
323 static gboolean
324 gst_dreamvideosource_start (GstBaseSrc * bsrc)
325 {
326         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
327
328         char fn_buf[32];
329
330         self->encoder = malloc(sizeof(EncoderInfo));
331
332         if(!self->encoder) {
333                 GST_ERROR_OBJECT(self,"out of space");
334                 return FALSE;
335         }
336
337         sprintf(fn_buf, "/dev/venc%d", 0);
338         self->encoder->fd = open(fn_buf, O_RDWR | O_SYNC);
339         if(self->encoder->fd <= 0) {
340                 GST_ERROR_OBJECT(self,"cannot open device %s (%s)", fn_buf, strerror(errno));
341                 free(self->encoder);
342                 self->encoder = NULL;
343                 return FALSE;
344         }
345
346         self->encoder->buffer = malloc(VBUFSIZE);
347         if(!self->encoder->buffer) {
348                 GST_ERROR_OBJECT(self,"cannot alloc buffer");
349                 return FALSE;
350         }
351
352         self->encoder->cdb = (unsigned char *)mmap(0, VMMAPSIZE, PROT_READ, MAP_PRIVATE, self->encoder->fd, 0);
353
354         if(!self->encoder->cdb) {
355                 GST_ERROR_OBJECT(self,"cannot mmap cdb");
356                 return FALSE;
357         }
358 #ifdef dump
359         self->dumpfd = open("/media/hdd/movie/dreamvideosource.dump", O_WRONLY | O_CREAT | O_TRUNC);
360         GST_DEBUG_OBJECT (self, "dumpfd = %i (%s)", self->dumpfd, (self->dumpfd > 0) ? "OK" : strerror(errno));
361 #endif
362         uint32_t vbr = self->video_info.bitrate*1000;
363         int ret = ioctl(self->encoder->fd, VENC_SET_BITRATE, &vbr);
364         GST_DEBUG_OBJECT (self, "set bitrate to %i bytes/s ret=%i", vbr, ret);
365         
366         ret = ioctl(self->encoder->fd, VENC_START);
367         GST_INFO_OBJECT (self, "started encoder! ret=%i", ret);
368         
369         self->dreamaudiosrc = gst_bin_get_by_name_recurse_up(GST_BIN(GST_ELEMENT_PARENT(self)), "dreamaudiosource0");
370
371         return TRUE;
372 }
373
374 static gboolean
375 gst_dreamvideosource_stop (GstBaseSrc * bsrc)
376 {
377         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (bsrc);
378         if (self->encoder) {
379                 if (self->encoder->fd > 0)
380                         ioctl(self->encoder->fd, VENC_STOP);
381                 close(self->encoder->fd);
382         }
383 #ifdef dump
384         close(self->dumpfd);
385 #endif
386         GST_DEBUG_OBJECT (self, "closed");
387         return TRUE;
388 }
389
390 static void
391 gst_dreamvideosource_finalize (GObject * gobject)
392 {
393         GstDreamVideoSource *self = GST_DREAMVIDEOSOURCE (gobject);
394         if (self->encoder) {
395                 if (self->encoder->buffer)
396                         free(self->encoder->buffer);
397                 if (self->encoder->cdb) 
398                         munmap(self->encoder->cdb, VMMAPSIZE);
399                 free(self->encoder);
400         }
401         g_mutex_clear (&self->mutex);
402         GST_DEBUG_OBJECT (self, "finalized");
403         G_OBJECT_CLASS (parent_class)->finalize (gobject);
404 }