1 /* GStreamer
2 * Copyright (C) 2011 David A. Schleef <ds@schleef.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19 /**
20 * SECTION:element-intervideosrc
21 * @title: gstintervideosrc
22 *
23 * The intervideosrc element is a video source element. It is used
24 * in connection with a intervideosink element in a different pipeline,
25 * similar to interaudiosink and interaudiosrc.
26 *
27 * ## Example launch line
28 * |[
29 * gst-launch-1.0 -v intervideosrc ! queue ! xvimagesink
30 * ]|
31 *
32 * The intersubsrc element cannot be used effectively with gst-launch-1.0,
33 * as it requires a second pipeline in the application to send subtitles.
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include <gst/gst.h>
42 #include <gst/base/gstbasesrc.h>
43 #include <gst/video/video.h>
44 #include "gstintervideosrc.h"
45 #include <string.h>
46
47 GST_DEBUG_CATEGORY_STATIC (gst_inter_video_src_debug_category);
48 #define GST_CAT_DEFAULT gst_inter_video_src_debug_category
49
50 /* prototypes */
51 static void gst_inter_video_src_set_property (GObject * object,
52 guint property_id, const GValue * value, GParamSpec * pspec);
53 static void gst_inter_video_src_get_property (GObject * object,
54 guint property_id, GValue * value, GParamSpec * pspec);
55 static void gst_inter_video_src_finalize (GObject * object);
56
57 static GstCaps *gst_inter_video_src_get_caps (GstBaseSrc * src,
58 GstCaps * filter);
59 static gboolean gst_inter_video_src_set_caps (GstBaseSrc * src, GstCaps * caps);
60 static GstCaps *gst_inter_video_src_fixate (GstBaseSrc * src, GstCaps * caps);
61 static gboolean gst_inter_video_src_start (GstBaseSrc * src);
62 static gboolean gst_inter_video_src_stop (GstBaseSrc * src);
63 static void
64 gst_inter_video_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
65 GstClockTime * start, GstClockTime * end);
66 static GstFlowReturn
67 gst_inter_video_src_create (GstBaseSrc * src, guint64 offset, guint size,
68 GstBuffer ** buf);
69
70 enum
71 {
72 PROP_0,
73 PROP_CHANNEL,
74 PROP_TIMEOUT
75 };
76
77 #define DEFAULT_CHANNEL ("default")
78 #define DEFAULT_TIMEOUT (GST_SECOND)
79
80 /* pad templates */
81 static GstStaticPadTemplate gst_inter_video_src_src_template =
82 GST_STATIC_PAD_TEMPLATE ("src",
83 GST_PAD_SRC,
84 GST_PAD_ALWAYS,
85 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE (GST_VIDEO_FORMATS_ALL))
86 );
87
88
89 /* class initialization */
90 #define parent_class gst_inter_video_src_parent_class
91 G_DEFINE_TYPE (GstInterVideoSrc, gst_inter_video_src, GST_TYPE_BASE_SRC);
92 GST_ELEMENT_REGISTER_DEFINE (intervideosrc, "intervideosrc",
93 GST_RANK_NONE, GST_TYPE_INTER_VIDEO_SRC);
94
95 static void
gst_inter_video_src_class_init(GstInterVideoSrcClass * klass)96 gst_inter_video_src_class_init (GstInterVideoSrcClass * klass)
97 {
98 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
99 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
100 GstBaseSrcClass *base_src_class = GST_BASE_SRC_CLASS (klass);
101
102 GST_DEBUG_CATEGORY_INIT (gst_inter_video_src_debug_category, "intervideosrc",
103 0, "debug category for intervideosrc element");
104
105 gst_element_class_add_static_pad_template (element_class,
106 &gst_inter_video_src_src_template);
107
108 gst_element_class_set_static_metadata (element_class,
109 "Internal video source",
110 "Source/Video",
111 "Virtual video source for internal process communication",
112 "David Schleef <ds@schleef.org>");
113
114 gobject_class->set_property = gst_inter_video_src_set_property;
115 gobject_class->get_property = gst_inter_video_src_get_property;
116 gobject_class->finalize = gst_inter_video_src_finalize;
117 base_src_class->get_caps = GST_DEBUG_FUNCPTR (gst_inter_video_src_get_caps);
118 base_src_class->set_caps = GST_DEBUG_FUNCPTR (gst_inter_video_src_set_caps);
119 base_src_class->fixate = GST_DEBUG_FUNCPTR (gst_inter_video_src_fixate);
120 base_src_class->start = GST_DEBUG_FUNCPTR (gst_inter_video_src_start);
121 base_src_class->stop = GST_DEBUG_FUNCPTR (gst_inter_video_src_stop);
122 base_src_class->get_times = GST_DEBUG_FUNCPTR (gst_inter_video_src_get_times);
123 base_src_class->create = GST_DEBUG_FUNCPTR (gst_inter_video_src_create);
124
125 g_object_class_install_property (gobject_class, PROP_CHANNEL,
126 g_param_spec_string ("channel", "Channel",
127 "Channel name to match inter src and sink elements",
128 DEFAULT_CHANNEL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
129
130 g_object_class_install_property (gobject_class, PROP_TIMEOUT,
131 g_param_spec_uint64 ("timeout", "Timeout",
132 "Timeout after which to start outputting black frames",
133 0, G_MAXUINT64, DEFAULT_TIMEOUT,
134 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
135 }
136
137 static void
gst_inter_video_src_init(GstInterVideoSrc * intervideosrc)138 gst_inter_video_src_init (GstInterVideoSrc * intervideosrc)
139 {
140 gst_base_src_set_format (GST_BASE_SRC (intervideosrc), GST_FORMAT_TIME);
141 gst_base_src_set_live (GST_BASE_SRC (intervideosrc), TRUE);
142
143 intervideosrc->channel = g_strdup (DEFAULT_CHANNEL);
144 intervideosrc->timeout = DEFAULT_TIMEOUT;
145 }
146
147 void
gst_inter_video_src_set_property(GObject * object,guint property_id,const GValue * value,GParamSpec * pspec)148 gst_inter_video_src_set_property (GObject * object, guint property_id,
149 const GValue * value, GParamSpec * pspec)
150 {
151 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (object);
152
153 switch (property_id) {
154 case PROP_CHANNEL:
155 g_free (intervideosrc->channel);
156 intervideosrc->channel = g_value_dup_string (value);
157 break;
158 case PROP_TIMEOUT:
159 intervideosrc->timeout = g_value_get_uint64 (value);
160 break;
161 default:
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 break;
164 }
165 }
166
167 void
gst_inter_video_src_get_property(GObject * object,guint property_id,GValue * value,GParamSpec * pspec)168 gst_inter_video_src_get_property (GObject * object, guint property_id,
169 GValue * value, GParamSpec * pspec)
170 {
171 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (object);
172
173 switch (property_id) {
174 case PROP_CHANNEL:
175 g_value_set_string (value, intervideosrc->channel);
176 break;
177 case PROP_TIMEOUT:
178 g_value_set_uint64 (value, intervideosrc->timeout);
179 break;
180 default:
181 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
182 break;
183 }
184 }
185
186 void
gst_inter_video_src_finalize(GObject * object)187 gst_inter_video_src_finalize (GObject * object)
188 {
189 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (object);
190
191 /* clean up object here */
192 g_free (intervideosrc->channel);
193
194 G_OBJECT_CLASS (gst_inter_video_src_parent_class)->finalize (object);
195 }
196
197 static GstCaps *
gst_inter_video_src_get_caps(GstBaseSrc * src,GstCaps * filter)198 gst_inter_video_src_get_caps (GstBaseSrc * src, GstCaps * filter)
199 {
200 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (src);
201 GstCaps *caps;
202
203 GST_DEBUG_OBJECT (intervideosrc, "get_caps");
204
205 if (!intervideosrc->surface)
206 return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
207
208 g_mutex_lock (&intervideosrc->surface->mutex);
209 if (intervideosrc->surface->video_info.finfo) {
210 caps = gst_video_info_to_caps (&intervideosrc->surface->video_info);
211 gst_caps_set_simple (caps, "framerate", GST_TYPE_FRACTION_RANGE, 1,
212 G_MAXINT, G_MAXINT, 1, NULL);
213
214 if (filter) {
215 GstCaps *tmp;
216
217 tmp = gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
218 gst_caps_unref (caps);
219 caps = tmp;
220 }
221 } else {
222 caps = NULL;
223 }
224 g_mutex_unlock (&intervideosrc->surface->mutex);
225
226 if (caps)
227 return caps;
228 else
229 return GST_BASE_SRC_CLASS (parent_class)->get_caps (src, filter);
230 }
231
232 static gboolean
gst_inter_video_src_set_caps(GstBaseSrc * base,GstCaps * caps)233 gst_inter_video_src_set_caps (GstBaseSrc * base, GstCaps * caps)
234 {
235 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (base);
236 GstVideoConverter *converter;
237 GstVideoFrame src_frame, dest_frame;
238 GstBuffer *src, *dest;
239 GstVideoInfo black_info;
240
241 GST_DEBUG_OBJECT (intervideosrc, "set_caps");
242
243 if (!gst_video_info_from_caps (&intervideosrc->info, caps)) {
244 GST_ERROR_OBJECT (intervideosrc, "Failed to parse caps %" GST_PTR_FORMAT,
245 caps);
246 return FALSE;
247 }
248
249 /* Create a black frame */
250 gst_buffer_replace (&intervideosrc->black_frame, NULL);
251 gst_video_info_set_format (&black_info, GST_VIDEO_FORMAT_ARGB,
252 intervideosrc->info.width, intervideosrc->info.height);
253 black_info.fps_n = intervideosrc->info.fps_n;
254 black_info.fps_d = intervideosrc->info.fps_d;
255 src = gst_buffer_new_and_alloc (black_info.size);
256 dest = gst_buffer_new_and_alloc (intervideosrc->info.size);
257 gst_buffer_memset (src, 0, 0, black_info.size);
258 gst_video_frame_map (&src_frame, &black_info, src, GST_MAP_READ);
259 gst_video_frame_map (&dest_frame, &intervideosrc->info, dest, GST_MAP_WRITE);
260 converter = gst_video_converter_new (&black_info, &intervideosrc->info, NULL);
261 gst_video_converter_frame (converter, &src_frame, &dest_frame);
262 gst_video_converter_free (converter);
263 gst_video_frame_unmap (&src_frame);
264 gst_video_frame_unmap (&dest_frame);
265 gst_buffer_unref (src);
266 intervideosrc->black_frame = dest;
267
268 return TRUE;
269 }
270
271 static gboolean
gst_inter_video_src_start(GstBaseSrc * src)272 gst_inter_video_src_start (GstBaseSrc * src)
273 {
274 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (src);
275
276 GST_DEBUG_OBJECT (intervideosrc, "start");
277
278 intervideosrc->surface = gst_inter_surface_get (intervideosrc->channel);
279 intervideosrc->timestamp_offset = 0;
280 intervideosrc->n_frames = 0;
281
282 return TRUE;
283 }
284
285 static gboolean
gst_inter_video_src_stop(GstBaseSrc * src)286 gst_inter_video_src_stop (GstBaseSrc * src)
287 {
288 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (src);
289
290 GST_DEBUG_OBJECT (intervideosrc, "stop");
291
292 gst_inter_surface_unref (intervideosrc->surface);
293 intervideosrc->surface = NULL;
294 gst_buffer_replace (&intervideosrc->black_frame, NULL);
295
296 return TRUE;
297 }
298
299 static void
gst_inter_video_src_get_times(GstBaseSrc * src,GstBuffer * buffer,GstClockTime * start,GstClockTime * end)300 gst_inter_video_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
301 GstClockTime * start, GstClockTime * end)
302 {
303 GST_DEBUG_OBJECT (src, "get_times");
304
305 /* for live sources, sync on the timestamp of the buffer */
306 if (gst_base_src_is_live (src)) {
307 GstClockTime timestamp = GST_BUFFER_PTS (buffer);
308
309 if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
310 /* get duration to calculate end time */
311 GstClockTime duration = GST_BUFFER_DURATION (buffer);
312
313 if (GST_CLOCK_TIME_IS_VALID (duration)) {
314 *end = timestamp + duration;
315 }
316 *start = timestamp;
317 }
318 } else {
319 *start = -1;
320 *end = -1;
321 }
322 }
323
324 static GstFlowReturn
gst_inter_video_src_create(GstBaseSrc * src,guint64 offset,guint size,GstBuffer ** buf)325 gst_inter_video_src_create (GstBaseSrc * src, guint64 offset, guint size,
326 GstBuffer ** buf)
327 {
328 GstInterVideoSrc *intervideosrc = GST_INTER_VIDEO_SRC (src);
329 GstCaps *caps;
330 GstBuffer *buffer;
331 guint64 frames;
332 gboolean is_gap = FALSE;
333
334 GST_DEBUG_OBJECT (intervideosrc, "create");
335
336 caps = NULL;
337 buffer = NULL;
338
339 frames = gst_util_uint64_scale_ceil (intervideosrc->timeout,
340 GST_VIDEO_INFO_FPS_N (&intervideosrc->info),
341 GST_VIDEO_INFO_FPS_D (&intervideosrc->info) * GST_SECOND);
342
343 g_mutex_lock (&intervideosrc->surface->mutex);
344 if (intervideosrc->surface->video_info.finfo) {
345 GstVideoInfo tmp_info = intervideosrc->surface->video_info;
346
347 /* We negotiate the framerate ourselves */
348 tmp_info.fps_n = intervideosrc->info.fps_n;
349 tmp_info.fps_d = intervideosrc->info.fps_d;
350 if (intervideosrc->info.flags & GST_VIDEO_FLAG_VARIABLE_FPS)
351 tmp_info.flags |= GST_VIDEO_FLAG_VARIABLE_FPS;
352 else
353 tmp_info.flags &= ~GST_VIDEO_FLAG_VARIABLE_FPS;
354
355 if (!gst_video_info_is_equal (&tmp_info, &intervideosrc->info)) {
356 caps = gst_video_info_to_caps (&tmp_info);
357 intervideosrc->timestamp_offset +=
358 gst_util_uint64_scale (GST_SECOND * intervideosrc->n_frames,
359 GST_VIDEO_INFO_FPS_D (&intervideosrc->info),
360 GST_VIDEO_INFO_FPS_N (&intervideosrc->info));
361 intervideosrc->n_frames = 0;
362 }
363 }
364
365 if (intervideosrc->surface->video_buffer) {
366 /* We have a buffer to push */
367 buffer = gst_buffer_ref (intervideosrc->surface->video_buffer);
368
369 /* Can only be true if timeout > 0 */
370 if (intervideosrc->surface->video_buffer_count == frames) {
371 gst_buffer_unref (intervideosrc->surface->video_buffer);
372 intervideosrc->surface->video_buffer = NULL;
373 }
374 }
375
376 if (intervideosrc->surface->video_buffer_count != 0 &&
377 intervideosrc->surface->video_buffer_count != (frames + 1)) {
378 /* This is a repeat of the stored buffer or of a black frame */
379 is_gap = TRUE;
380 }
381
382 intervideosrc->surface->video_buffer_count++;
383 g_mutex_unlock (&intervideosrc->surface->mutex);
384
385 if (caps) {
386 gboolean ret;
387 GstStructure *s;
388 GstCaps *downstream_caps;
389 GstCaps *tmp, *negotiated_caps;
390 gint fps_n = 0, fps_d = 1;
391
392 /* Negotiate a framerate with downstream */
393 downstream_caps = gst_pad_get_allowed_caps (GST_BASE_SRC_PAD (src));
394
395 /* Remove all framerates */
396 tmp = gst_caps_copy (caps);
397 s = gst_caps_get_structure (tmp, 0);
398 gst_structure_get_fraction (s, "framerate", &fps_n, &fps_d);
399 if (fps_n == 0)
400 gst_structure_get_fraction (s, "max-framerate", &fps_n, &fps_d);
401 gst_structure_remove_field (s, "framerate");
402 gst_structure_remove_field (s, "max-framerate");
403
404 negotiated_caps =
405 gst_caps_intersect_full (downstream_caps, tmp,
406 GST_CAPS_INTERSECT_FIRST);
407 gst_caps_unref (tmp);
408 gst_caps_unref (downstream_caps);
409
410 if (gst_caps_is_empty (negotiated_caps)) {
411 GST_ERROR_OBJECT (src, "Failed to negotiate caps %" GST_PTR_FORMAT, caps);
412 if (buffer)
413 gst_buffer_unref (buffer);
414 gst_caps_unref (caps);
415 return GST_FLOW_NOT_NEGOTIATED;
416 }
417 gst_caps_unref (caps);
418 caps = NULL;
419
420 /* Prefer what the source produces, otherwise 30 fps */
421 if (fps_n == 0) {
422 fps_n = 30;
423 fps_d = 1;
424 }
425
426 negotiated_caps = gst_caps_truncate (negotiated_caps);
427 s = gst_caps_get_structure (negotiated_caps, 0);
428 if (!gst_structure_has_field (s, "framerate"))
429 gst_structure_set (s, "framerate", GST_TYPE_FRACTION, fps_n, fps_d, NULL);
430 else
431 gst_structure_fixate_field_nearest_fraction (s, "framerate", fps_n,
432 fps_d);
433
434 ret = gst_base_src_set_caps (src, negotiated_caps);
435 if (!ret) {
436 GST_ERROR_OBJECT (src, "Failed to set caps %" GST_PTR_FORMAT,
437 negotiated_caps);
438 if (buffer)
439 gst_buffer_unref (buffer);
440 gst_caps_unref (negotiated_caps);
441 return GST_FLOW_NOT_NEGOTIATED;
442 }
443 gst_caps_unref (negotiated_caps);
444 }
445
446 if (buffer == NULL) {
447 GST_DEBUG_OBJECT (intervideosrc, "Creating black frame");
448 buffer = gst_buffer_copy (intervideosrc->black_frame);
449 }
450
451 buffer = gst_buffer_make_writable (buffer);
452
453 if (is_gap)
454 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_GAP);
455
456 GST_BUFFER_PTS (buffer) = intervideosrc->timestamp_offset +
457 gst_util_uint64_scale (GST_SECOND * intervideosrc->n_frames,
458 GST_VIDEO_INFO_FPS_D (&intervideosrc->info),
459 GST_VIDEO_INFO_FPS_N (&intervideosrc->info));
460 GST_BUFFER_DTS (buffer) = GST_CLOCK_TIME_NONE;
461 GST_DEBUG_OBJECT (intervideosrc, "create ts %" GST_TIME_FORMAT,
462 GST_TIME_ARGS (GST_BUFFER_PTS (buffer)));
463 GST_BUFFER_DURATION (buffer) = intervideosrc->timestamp_offset +
464 gst_util_uint64_scale (GST_SECOND * (intervideosrc->n_frames + 1),
465 GST_VIDEO_INFO_FPS_D (&intervideosrc->info),
466 GST_VIDEO_INFO_FPS_N (&intervideosrc->info)) - GST_BUFFER_PTS (buffer);
467 GST_BUFFER_OFFSET (buffer) = intervideosrc->n_frames;
468 GST_BUFFER_OFFSET_END (buffer) = -1;
469 GST_BUFFER_FLAG_UNSET (buffer, GST_BUFFER_FLAG_DISCONT);
470 if (intervideosrc->n_frames == 0) {
471 GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DISCONT);
472 }
473 intervideosrc->n_frames++;
474
475 *buf = buffer;
476
477 return GST_FLOW_OK;
478 }
479
480 static GstCaps *
gst_inter_video_src_fixate(GstBaseSrc * src,GstCaps * caps)481 gst_inter_video_src_fixate (GstBaseSrc * src, GstCaps * caps)
482 {
483 GstStructure *structure;
484
485 GST_DEBUG_OBJECT (src, "fixate");
486
487 caps = gst_caps_make_writable (caps);
488 caps = gst_caps_truncate (caps);
489 structure = gst_caps_get_structure (caps, 0);
490
491 gst_structure_fixate_field_string (structure, "format", "I420");
492 gst_structure_fixate_field_nearest_int (structure, "width", 320);
493 gst_structure_fixate_field_nearest_int (structure, "height", 240);
494 gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
495 if (gst_structure_has_field (structure, "pixel-aspect-ratio"))
496 gst_structure_fixate_field_nearest_fraction (structure,
497 "pixel-aspect-ratio", 1, 1);
498 if (gst_structure_has_field (structure, "color-matrix"))
499 gst_structure_fixate_field_string (structure, "color-matrix", "sdtv");
500 if (gst_structure_has_field (structure, "chroma-site"))
501 gst_structure_fixate_field_string (structure, "chroma-site", "mpeg2");
502
503 if (gst_structure_has_field (structure, "interlace-mode"))
504 gst_structure_fixate_field_string (structure, "interlace-mode",
505 "progressive");
506
507 return caps;
508 }
509