1 /* GStreamer
2 * Copyright (C) 2019 Thibault Saunier <tsaunier@igalia.com>
3 *
4 * gsttranscodebin.c:
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include "gsttranscoding.h"
26 #include "gsttranscodeelements.h"
27 #include <gst/gst-i18n-plugin.h>
28 #include <gst/pbutils/pbutils.h>
29
30 #include <gst/pbutils/missing-plugins.h>
31
32
33
34 /**
35 * GstTranscodeBin!sink_%u:
36 *
37 * Extra sinkpads for the parallel transcoding of auxiliary streams.
38 *
39 * Since: 1.20
40 */
41 static GstStaticPadTemplate transcode_bin_sinks_template =
42 GST_STATIC_PAD_TEMPLATE ("sink_%u",
43 GST_PAD_SINK,
44 GST_PAD_REQUEST,
45 GST_STATIC_CAPS_ANY);
46
47 static GstStaticPadTemplate transcode_bin_sink_template =
48 GST_STATIC_PAD_TEMPLATE ("sink",
49 GST_PAD_SINK,
50 GST_PAD_ALWAYS,
51 GST_STATIC_CAPS_ANY);
52
53 /**
54 * GstTranscodeBin!src_%u:
55 *
56 * The sometimes source pad, it will be exposed depending on the
57 * #transcodebin:profile in use.
58 *
59 * Note: in GStreamer 1.18 it was a static
60 * srcpad but in the the 1.20 cycle it was decided that we should make it a
61 * sometimes pad as part of the development of #encodebin2.
62 *
63 * Since: 1.20
64 */
65 static GstStaticPadTemplate transcode_bin_src_template =
66 GST_STATIC_PAD_TEMPLATE ("src_%u",
67 GST_PAD_SRC,
68 GST_PAD_SOMETIMES,
69 GST_STATIC_CAPS_ANY);
70
71 typedef struct
72 {
73 const gchar *stream_id;
74 GstStream *stream;
75 GstPad *encodebin_pad;
76 } TranscodingStream;
77
78 static TranscodingStream *
transcoding_stream_new(GstStream * stream,GstPad * encodebin_pad)79 transcoding_stream_new (GstStream * stream, GstPad * encodebin_pad)
80 {
81 TranscodingStream *tstream = g_new0 (TranscodingStream, 1);
82
83 tstream->stream_id = gst_stream_get_stream_id (stream);
84 tstream->stream = gst_object_ref (stream);
85 tstream->encodebin_pad = encodebin_pad;
86
87 return tstream;
88 }
89
90 static void
transcoding_stream_free(TranscodingStream * tstream)91 transcoding_stream_free (TranscodingStream * tstream)
92 {
93 gst_object_unref (tstream->stream);
94 gst_object_unref (tstream->encodebin_pad);
95 }
96
97 typedef struct
98 {
99 GstBin parent;
100
101 GstElement *decodebin;
102 GstElement *encodebin;
103
104 GstEncodingProfile *profile;
105 gboolean avoid_reencoding;
106 GstPad *sinkpad;
107
108 GstElement *audio_filter;
109 GstElement *video_filter;
110
111 GPtrArray *transcoding_streams;
112 } GstTranscodeBin;
113
114 typedef struct
115 {
116 GstBinClass parent;
117
118 } GstTranscodeBinClass;
119
120 /* *INDENT-OFF* */
121 #define GST_TYPE_TRANSCODE_BIN (gst_transcode_bin_get_type ())
122 #define GST_TRANSCODE_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_TRANSCODE_BIN, GstTranscodeBin))
123
124 #define DEFAULT_AVOID_REENCODING FALSE
125
126 G_DEFINE_TYPE (GstTranscodeBin, gst_transcode_bin, GST_TYPE_BIN);
127 GST_ELEMENT_REGISTER_DEFINE_WITH_CODE (transcodebin, "transcodebin", GST_RANK_NONE,
128 GST_TYPE_TRANSCODE_BIN, transcodebin_element_init (plugin));
129
130 enum
131 {
132 PROP_0,
133 PROP_PROFILE,
134 PROP_AVOID_REENCODING,
135 PROP_VIDEO_FILTER,
136 PROP_AUDIO_FILTER,
137 LAST_PROP
138 };
139
140 static void
post_missing_plugin_error(GstElement * dec,const gchar * element_name)141 post_missing_plugin_error (GstElement * dec, const gchar * element_name)
142 {
143 GstMessage *msg;
144
145 msg = gst_missing_element_message_new (dec, element_name);
146 gst_element_post_message (dec, msg);
147
148 GST_ELEMENT_ERROR (dec, CORE, MISSING_PLUGIN,
149 ("Missing element '%s' - check your GStreamer installation.",
150 element_name), (NULL));
151 }
152 /* *INDENT-ON* */
153
154 static gboolean
filter_handles_any(GstElement * filter)155 filter_handles_any (GstElement * filter)
156 {
157 GList *tmp;
158
159 for (tmp = gst_element_get_pad_template_list (filter); tmp; tmp = tmp->next) {
160 GstPadTemplate *tmpl = tmp->data;
161 GstCaps *caps = gst_pad_template_get_caps (tmpl);
162
163 if (!gst_caps_is_any (caps)) {
164 gst_caps_unref (caps);
165 return FALSE;
166 }
167
168 gst_caps_unref (caps);
169 }
170
171 return gst_element_get_pad_template_list (filter) != NULL;
172 }
173
174 static GstPad *
_insert_filter(GstTranscodeBin * self,GstPad * sinkpad,GstPad * pad,GstCaps * caps)175 _insert_filter (GstTranscodeBin * self, GstPad * sinkpad, GstPad * pad,
176 GstCaps * caps)
177 {
178 GstPad *filter_src = NULL, *filter_sink = NULL, *convert_sink, *convert_src;
179 GstElement *filter = NULL, *convert;
180 GstObject *filter_parent;
181 const gchar *media_type;
182 gboolean audio = TRUE;
183
184 media_type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
185
186 if (self->video_filter && g_str_has_prefix (media_type, "video")) {
187 audio = FALSE;
188
189 if (!g_strcmp0 (media_type, "video/x-raw")
190 || filter_handles_any (self->video_filter))
191 filter = self->video_filter;
192 else
193 GST_ERROR_OBJECT (pad, "decodebin pad does not produce raw data (%"
194 GST_PTR_FORMAT "), cannot add video filter '%s'", caps,
195 GST_ELEMENT_NAME (self->video_filter));
196 } else if (self->audio_filter && g_str_has_prefix (media_type, "audio")) {
197 if (!g_strcmp0 (media_type, "audio/x-raw")
198 || filter_handles_any (self->audio_filter))
199 filter = self->audio_filter;
200 else
201 GST_ERROR_OBJECT (pad, "decodebin pad does not produce raw data (%"
202 GST_PTR_FORMAT "), cannot add audio filter '%s'", caps,
203 GST_ELEMENT_NAME (self->audio_filter));
204 }
205
206 if (!filter)
207 return pad;
208
209 filter_parent = gst_object_get_parent (GST_OBJECT (filter));
210 if (filter_parent != GST_OBJECT_CAST (self)) {
211 GST_WARNING_OBJECT (self,
212 "Filter already in use (inside %" GST_PTR_FORMAT ").", filter_parent);
213 GST_FIXME_OBJECT (self,
214 "Handle transcoding several streams of a same kind.");
215 gst_object_unref (filter_parent);
216
217 return pad;
218 }
219
220 gst_object_unref (filter_parent);
221 /* We are guaranteed filters only have 1 unique sinkpad and srcpad */
222 GST_OBJECT_LOCK (filter);
223 filter_sink = filter->sinkpads->data;
224 filter_src = filter->srcpads->data;
225 GST_OBJECT_UNLOCK (filter);
226
227 if (filter_handles_any (filter))
228 convert = gst_element_factory_make ("identity", NULL);
229 else if (audio)
230 convert = gst_element_factory_make ("audioconvert", NULL);
231 else
232 convert = gst_element_factory_make ("videoconvert", NULL);
233
234 if (!convert) {
235 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN,
236 (_("Missing element '%s' - check your GStreamer installation."),
237 audio ? "audioconvert" : "videoconvert"),
238 ("Cannot add filter as %s element is missing",
239 audio ? "audioconvert" : "videoconvert"));
240 return pad;
241 }
242
243 gst_bin_add_many (GST_BIN (self), convert, NULL);
244
245 convert_sink = gst_element_get_static_pad (convert, "sink");
246 g_assert (convert_sink);
247
248 if (G_UNLIKELY (gst_pad_link (pad, convert_sink) != GST_PAD_LINK_OK)) {
249 GstCaps *othercaps = gst_pad_get_pad_template_caps (convert_sink);
250 caps = gst_pad_get_current_caps (pad);
251
252 GST_ELEMENT_ERROR (self, CORE, PAD,
253 (NULL),
254 ("Couldn't link pads \n\n %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT
255 "\n\n and \n\n %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT
256 "\n\n", pad, caps, convert_sink, othercaps));
257
258 gst_object_unref (convert_sink);
259 gst_caps_unref (caps);
260 gst_caps_unref (othercaps);
261 }
262
263 gst_object_unref (convert_sink);
264
265 convert_src = gst_element_get_static_pad (convert, "src");
266 g_assert (convert_src);
267
268 if (G_UNLIKELY (gst_pad_link (convert_src, filter_sink) != GST_PAD_LINK_OK)) {
269 GstCaps *othercaps = gst_pad_get_pad_template_caps (filter_sink);
270 caps = gst_pad_get_pad_template_caps (convert_src);
271
272 GST_ELEMENT_ERROR (self, CORE, PAD,
273 (NULL),
274 ("Couldn't link pads \n\n %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT
275 "\n\n and \n\n %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT
276 "\n\n", convert_src, caps, filter_sink, othercaps));
277
278 gst_object_unref (convert_src);
279 gst_caps_unref (caps);
280 gst_caps_unref (othercaps);
281 }
282
283 gst_object_unref (convert_src);
284
285 gst_element_sync_state_with_parent (convert);
286 gst_element_sync_state_with_parent (filter);
287
288 GST_DEBUG_OBJECT (self, "added %s filter '%s'",
289 audio ? "audio" : "video", GST_ELEMENT_NAME (filter));
290
291 return filter_src;
292 }
293
294 static TranscodingStream *
find_stream(GstTranscodeBin * self,const gchar * stream_id,GstPad * pad)295 find_stream (GstTranscodeBin * self, const gchar * stream_id, GstPad * pad)
296 {
297 gint i;
298 TranscodingStream *res = NULL;
299
300 GST_OBJECT_LOCK (self);
301 for (i = 0; i < self->transcoding_streams->len; i = i + 1) {
302 TranscodingStream *s = self->transcoding_streams->pdata[i];
303
304 if (stream_id && !g_strcmp0 (s->stream_id, stream_id)) {
305 res = s;
306 goto done;
307 } else if (pad && s->encodebin_pad == pad) {
308 res = s;
309 goto done;
310 }
311 }
312
313 done:
314 GST_OBJECT_UNLOCK (self);
315
316 return res;
317 }
318
319 static void
gst_transcode_bin_link_encodebin_pad(GstTranscodeBin * self,GstPad * pad,const gchar * stream_id)320 gst_transcode_bin_link_encodebin_pad (GstTranscodeBin * self, GstPad * pad,
321 const gchar * stream_id)
322 {
323 GstCaps *caps;
324 GstPadLinkReturn lret;
325 TranscodingStream *stream = find_stream (self, stream_id, NULL);
326
327 if (!stream) {
328 GST_ERROR_OBJECT (self, "%s -> Got not stream, decodebin3 bug?", stream_id);
329 return;
330 }
331
332 caps = gst_pad_query_caps (pad, NULL);
333 pad = _insert_filter (self, stream->encodebin_pad, pad, caps);
334 lret = gst_pad_link (pad, stream->encodebin_pad);
335 switch (lret) {
336 case GST_PAD_LINK_OK:
337 break;
338 case GST_PAD_LINK_WAS_LINKED:
339 GST_FIXME_OBJECT (self, "Pad %" GST_PTR_FORMAT " was already linked",
340 stream->encodebin_pad);
341 break;
342 default:
343 {
344 GstCaps *othercaps = gst_pad_query_caps (stream->encodebin_pad, NULL);
345 caps = gst_pad_get_current_caps (pad);
346
347 if (!caps)
348 caps = gst_pad_query_caps (pad, NULL);
349
350 GST_ELEMENT_ERROR_WITH_DETAILS (self, CORE, PAD,
351 (NULL),
352 ("Couldn't link pads:\n %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT
353 "\nand:\n"
354 " %" GST_PTR_FORMAT ": %" GST_PTR_FORMAT "\n\n Error: %s\n",
355 pad, caps, stream->encodebin_pad, othercaps,
356 gst_pad_link_get_name (lret)),
357 ("linking-error", GST_TYPE_PAD_LINK_RETURN, lret,
358 "source-pad", GST_TYPE_PAD, pad,
359 "source-caps", GST_TYPE_CAPS, caps,
360 "sink-pad", GST_TYPE_PAD, stream->encodebin_pad,
361 "sink-caps", GST_TYPE_CAPS, othercaps, NULL));
362
363 gst_clear_caps (&caps);
364 if (othercaps)
365 gst_caps_unref (othercaps);
366 }
367 }
368 }
369
370 static GstPadProbeReturn
wait_stream_start_probe(GstPad * pad,GstPadProbeInfo * info,GstTranscodeBin * self)371 wait_stream_start_probe (GstPad * pad,
372 GstPadProbeInfo * info, GstTranscodeBin * self)
373 {
374 const gchar *stream_id;
375
376 if (GST_EVENT_TYPE (info->data) != GST_EVENT_STREAM_START)
377 return GST_PAD_PROBE_OK;
378
379 gst_event_parse_stream_start (info->data, &stream_id);
380 GST_INFO_OBJECT (self, "Got pad %" GST_PTR_FORMAT " with stream ID: %s",
381 pad, stream_id);
382 gst_transcode_bin_link_encodebin_pad (self, pad, stream_id);
383
384 return GST_PAD_PROBE_REMOVE;
385 }
386
387 static void
decodebin_pad_added_cb(GstElement * decodebin,GstPad * pad,GstTranscodeBin * self)388 decodebin_pad_added_cb (GstElement * decodebin, GstPad * pad,
389 GstTranscodeBin * self)
390 {
391 const gchar *stream_id;
392 GstEvent *sstart_event;
393
394 if (GST_PAD_IS_SINK (pad))
395 return;
396
397 sstart_event = gst_pad_get_sticky_event (pad, GST_EVENT_STREAM_START, -1);
398 if (sstart_event) {
399 gst_event_parse_stream_start (sstart_event, &stream_id);
400 GST_INFO_OBJECT (self, "Got pad %" GST_PTR_FORMAT " with stream ID: %s",
401 pad, stream_id);
402 gst_transcode_bin_link_encodebin_pad (self, pad, stream_id);
403 return;
404 }
405
406 GST_INFO_OBJECT (self, "Waiting for stream ID for pad %" GST_PTR_FORMAT, pad);
407 gst_pad_add_probe (pad, GST_PAD_PROBE_TYPE_EVENT_DOWNSTREAM,
408 (GstPadProbeCallback) wait_stream_start_probe, self, NULL);
409 }
410
411 static void
encodebin_pad_added_cb(GstElement * encodebin,GstPad * pad,GstElement * self)412 encodebin_pad_added_cb (GstElement * encodebin, GstPad * pad, GstElement * self)
413 {
414 GstPadTemplate *template;
415 GstPad *new_pad;
416 gchar *name;
417
418 if (!GST_PAD_IS_SRC (pad))
419 return;
420
421 template = gst_element_get_pad_template (self, "src_%u");
422
423 GST_OBJECT_LOCK (self);
424 name = g_strdup_printf ("src_%u", GST_ELEMENT (self)->numsrcpads);
425 GST_OBJECT_UNLOCK (self);
426 new_pad = gst_ghost_pad_new_from_template (name, pad, template);
427 g_free (name);
428 GST_DEBUG_OBJECT (self, "Encodebin exposed srcpad: %" GST_PTR_FORMAT, pad);
429
430 gst_element_add_pad (self, new_pad);
431 }
432
433 static gboolean
make_encodebin(GstTranscodeBin * self)434 make_encodebin (GstTranscodeBin * self)
435 {
436 GST_INFO_OBJECT (self, "making new encodebin");
437
438 if (!self->profile)
439 goto no_profile;
440
441 self->encodebin = gst_element_factory_make ("encodebin2", NULL);
442 if (!self->encodebin)
443 goto no_encodebin;
444
445 gst_bin_add (GST_BIN (self), self->encodebin);
446
447 g_signal_connect (self->encodebin, "pad-added",
448 G_CALLBACK (encodebin_pad_added_cb), self);
449
450 g_object_set (self->encodebin, "profile", self->profile, NULL);
451
452 return gst_element_sync_state_with_parent (self->encodebin);
453
454 /* ERRORS */
455 no_encodebin:
456 {
457 post_missing_plugin_error (GST_ELEMENT_CAST (self), "encodebin");
458
459 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
460 ("No encodebin element, check your installation"));
461
462 return FALSE;
463 }
464 /* ERRORS */
465 no_profile:
466 {
467 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
468 ("No GstEncodingProfile set, can not run."));
469
470 return FALSE;
471 }
472 }
473
474 static GstPad *
get_encodebin_pad_for_caps(GstTranscodeBin * self,GstCaps * srccaps)475 get_encodebin_pad_for_caps (GstTranscodeBin * self, GstCaps * srccaps)
476 {
477 GstPad *res = NULL;
478 GstIterator *pads;
479 gboolean done = FALSE;
480 GValue paditem = { 0, };
481
482 if (G_UNLIKELY (srccaps == NULL))
483 goto no_caps;
484
485 pads = gst_element_iterate_sink_pads (self->encodebin);
486
487 GST_DEBUG_OBJECT (self, "srccaps %" GST_PTR_FORMAT, srccaps);
488
489 while (!done) {
490 switch (gst_iterator_next (pads, &paditem)) {
491 case GST_ITERATOR_OK:
492 {
493 GstPad *testpad = g_value_get_object (&paditem);
494
495 if (!gst_pad_is_linked (testpad) && !find_stream (self, NULL, testpad)) {
496 GstCaps *sinkcaps = gst_pad_query_caps (testpad, NULL);
497
498 GST_DEBUG_OBJECT (self, "sinkccaps %" GST_PTR_FORMAT, sinkcaps);
499
500 if (gst_caps_can_intersect (srccaps, sinkcaps)) {
501 res = gst_object_ref (testpad);
502 done = TRUE;
503 }
504 gst_caps_unref (sinkcaps);
505 }
506 g_value_reset (&paditem);
507 }
508 break;
509 case GST_ITERATOR_DONE:
510 case GST_ITERATOR_ERROR:
511 done = TRUE;
512 break;
513 case GST_ITERATOR_RESYNC:
514 gst_iterator_resync (pads);
515 break;
516 }
517 }
518 g_value_reset (&paditem);
519 gst_iterator_free (pads);
520
521 if (!res)
522 g_signal_emit_by_name (self->encodebin, "request-pad", srccaps, &res);
523
524 return res;
525
526 no_caps:
527 {
528 GST_DEBUG_OBJECT (self, "No caps, can't do anything");
529 return NULL;
530 }
531 }
532
533 static gboolean
caps_is_raw(GstCaps * caps,GstStreamType stype)534 caps_is_raw (GstCaps * caps, GstStreamType stype)
535 {
536 const gchar *media_type;
537
538 if (!caps || !gst_caps_get_size (caps))
539 return FALSE;
540
541 media_type = gst_structure_get_name (gst_caps_get_structure (caps, 0));
542 if (stype == GST_STREAM_TYPE_VIDEO)
543 return !g_strcmp0 (media_type, "video/x-raw");
544 else if (stype == GST_STREAM_TYPE_AUDIO)
545 return !g_strcmp0 (media_type, "audio/x-raw");
546 /* FIXME: Handle more types ? */
547
548 return FALSE;
549 }
550
551 static GstPad *
get_encodebin_pad_from_stream(GstTranscodeBin * self,GstEncodingProfile * profile,GstStream * stream)552 get_encodebin_pad_from_stream (GstTranscodeBin * self,
553 GstEncodingProfile * profile, GstStream * stream)
554 {
555 GstCaps *caps = gst_stream_get_caps (stream);
556 GstPad *sinkpad = get_encodebin_pad_for_caps (self, caps);
557
558 if (!sinkpad && !caps_is_raw (caps, gst_stream_get_stream_type (stream))) {
559 gst_clear_caps (&caps);
560 switch (gst_stream_get_stream_type (stream)) {
561 case GST_STREAM_TYPE_AUDIO:
562 caps = gst_caps_from_string ("audio/x-raw");
563 break;
564 case GST_STREAM_TYPE_VIDEO:
565 caps = gst_caps_from_string ("video/x-raw");
566 break;
567 default:
568 GST_INFO_OBJECT (self, "Unsupported stream type: %" GST_PTR_FORMAT,
569 stream);
570 return NULL;
571 }
572 sinkpad = get_encodebin_pad_for_caps (self, caps);
573 }
574
575 return sinkpad;
576 }
577
578 static gint
select_stream_cb(GstElement * decodebin,GstStreamCollection * collection,GstStream * stream,GstTranscodeBin * self)579 select_stream_cb (GstElement * decodebin,
580 GstStreamCollection * collection, GstStream * stream,
581 GstTranscodeBin * self)
582 {
583 gint i;
584 gboolean transcode_stream = FALSE;
585 guint len = 0;
586
587 GST_OBJECT_LOCK (self);
588 len = self->transcoding_streams->len;
589 GST_OBJECT_UNLOCK (self);
590
591 if (len) {
592 transcode_stream =
593 find_stream (self, gst_stream_get_stream_id (stream), NULL) != NULL;
594 if (transcode_stream)
595 goto done;
596 }
597
598 for (i = 0; i < gst_stream_collection_get_size (collection); i++) {
599 GstStream *tmpstream = gst_stream_collection_get_stream (collection, i);
600 GstPad *encodebin_pad =
601 get_encodebin_pad_from_stream (self, self->profile, tmpstream);
602
603 if (encodebin_pad) {
604 if (stream == tmpstream)
605 transcode_stream = TRUE;
606
607 GST_INFO_OBJECT (self,
608 "Going to transcode stream %s (encodebin pad: %" GST_PTR_FORMAT,
609 gst_stream_get_stream_id (tmpstream), encodebin_pad);
610
611 GST_OBJECT_LOCK (self);
612 g_ptr_array_add (self->transcoding_streams,
613 transcoding_stream_new (tmpstream, encodebin_pad));
614 GST_OBJECT_UNLOCK (self);
615 }
616 }
617
618 GST_OBJECT_LOCK (self);
619 len = self->transcoding_streams->len;
620 GST_OBJECT_UNLOCK (self);
621
622 if (len) {
623 transcode_stream =
624 find_stream (self, gst_stream_get_stream_id (stream), NULL) != NULL;
625 }
626
627 done:
628 if (!transcode_stream)
629 GST_INFO_OBJECT (self, "Discarding stream: %" GST_PTR_FORMAT, stream);
630
631 return transcode_stream;
632 }
633
634 /* Called with OBJECT_LOCK */
635 static void
_setup_avoid_reencoding(GstTranscodeBin * self)636 _setup_avoid_reencoding (GstTranscodeBin * self)
637 {
638 const GList *tmp;
639 GstCaps *decodecaps;
640
641 if (!self->avoid_reencoding)
642 return;
643
644 if (!GST_IS_ENCODING_CONTAINER_PROFILE (self->profile))
645 return;
646
647 g_object_get (self->decodebin, "caps", &decodecaps, NULL);
648 decodecaps = gst_caps_make_writable (decodecaps);
649 tmp =
650 gst_encoding_container_profile_get_profiles
651 (GST_ENCODING_CONTAINER_PROFILE (self->profile));
652 for (; tmp; tmp = tmp->next) {
653 GstEncodingProfile *profile = tmp->data;
654 GstCaps *restrictions, *encodecaps;
655 GstElement *filter = NULL;
656
657 restrictions = gst_encoding_profile_get_restriction (profile);
658
659 if (restrictions && gst_caps_is_any (restrictions)) {
660 gst_caps_unref (restrictions);
661 continue;
662 }
663
664 encodecaps = gst_encoding_profile_get_format (profile);
665 filter = NULL;
666
667 /* Filter operates on raw data so don't allow decodebin to produce
668 * encoded data if one is defined. */
669 if (GST_IS_ENCODING_VIDEO_PROFILE (profile) && self->video_filter)
670 filter = self->video_filter;
671 else if (GST_IS_ENCODING_AUDIO_PROFILE (profile)
672 && self->audio_filter)
673 filter = self->audio_filter;
674
675 if (!filter || filter_handles_any (filter)) {
676 GST_DEBUG_OBJECT (self,
677 "adding %" GST_PTR_FORMAT " as output caps to decodebin", encodecaps);
678 gst_caps_append (decodecaps, encodecaps);
679 }
680 }
681
682 GST_OBJECT_UNLOCK (self);
683
684 g_object_set (self->decodebin, "caps", decodecaps, NULL);
685 gst_caps_unref (decodecaps);
686
687 GST_OBJECT_LOCK (self);
688 }
689
690 static gboolean
make_decodebin(GstTranscodeBin * self)691 make_decodebin (GstTranscodeBin * self)
692 {
693 GstPad *pad;
694 GST_INFO_OBJECT (self, "making new decodebin");
695
696 self->decodebin = gst_element_factory_make ("decodebin3", NULL);
697
698 g_signal_connect (self->decodebin, "pad-added",
699 G_CALLBACK (decodebin_pad_added_cb), self);
700 g_signal_connect (self->decodebin, "select-stream",
701 G_CALLBACK (select_stream_cb), self);
702
703 gst_bin_add (GST_BIN (self), self->decodebin);
704 pad = gst_element_get_static_pad (self->decodebin, "sink");
705 if (!gst_ghost_pad_set_target (GST_GHOST_PAD_CAST (self->sinkpad), pad)) {
706
707 gst_object_unref (pad);
708 GST_ERROR_OBJECT (self, "Could not ghost %" GST_PTR_FORMAT " sinkpad",
709 self->decodebin);
710
711 return FALSE;
712 }
713
714 gst_object_unref (pad);
715 return TRUE;
716
717 /* ERRORS */
718 }
719
720 static void
remove_all_children(GstTranscodeBin * self)721 remove_all_children (GstTranscodeBin * self)
722 {
723 if (self->encodebin) {
724 gst_element_set_state (self->encodebin, GST_STATE_NULL);
725 gst_bin_remove (GST_BIN (self), self->encodebin);
726 self->encodebin = NULL;
727 }
728
729 if (self->video_filter && GST_OBJECT_PARENT (self->video_filter)) {
730 gst_element_set_state (self->video_filter, GST_STATE_NULL);
731 gst_bin_remove (GST_BIN (self), self->video_filter);
732 }
733
734 if (self->audio_filter && GST_OBJECT_PARENT (self->audio_filter)) {
735 gst_element_set_state (self->audio_filter, GST_STATE_NULL);
736 gst_bin_remove (GST_BIN (self), self->audio_filter);
737 }
738 }
739
740 static GstStateChangeReturn
gst_transcode_bin_change_state(GstElement * element,GstStateChange transition)741 gst_transcode_bin_change_state (GstElement * element, GstStateChange transition)
742 {
743 GstStateChangeReturn ret;
744 GstTranscodeBin *self = GST_TRANSCODE_BIN (element);
745
746 switch (transition) {
747 case GST_STATE_CHANGE_READY_TO_PAUSED:
748
749 if (!self->decodebin) {
750 post_missing_plugin_error (GST_ELEMENT_CAST (self), "decodebin3");
751 GST_ELEMENT_ERROR (self, CORE, MISSING_PLUGIN, (NULL),
752 ("No decodebin element, check your installation"));
753
754 goto setup_failed;
755 }
756
757 if (!make_encodebin (self))
758 goto setup_failed;
759
760 break;
761 default:
762 break;
763 }
764
765 ret =
766 GST_ELEMENT_CLASS (gst_transcode_bin_parent_class)->change_state (element,
767 transition);
768 if (ret == GST_STATE_CHANGE_FAILURE)
769 goto beach;
770
771 switch (transition) {
772 case GST_STATE_CHANGE_PAUSED_TO_READY:
773 GST_OBJECT_LOCK (self);
774 g_ptr_array_remove_range (self->transcoding_streams, 0,
775 self->transcoding_streams->len);
776 GST_OBJECT_UNLOCK (self);
777
778 g_signal_handlers_disconnect_by_data (self->decodebin, self);
779
780 remove_all_children (self);
781 break;
782 default:
783 break;
784 }
785
786 beach:
787 return ret;
788
789 setup_failed:
790 remove_all_children (self);
791 return GST_STATE_CHANGE_FAILURE;
792 }
793
794 static void
gst_transcode_bin_dispose(GObject * object)795 gst_transcode_bin_dispose (GObject * object)
796 {
797 GstTranscodeBin *self = (GstTranscodeBin *) object;
798
799 g_clear_object (&self->video_filter);
800 g_clear_object (&self->audio_filter);
801 g_clear_pointer (&self->transcoding_streams, g_ptr_array_unref);
802
803 G_OBJECT_CLASS (gst_transcode_bin_parent_class)->dispose (object);
804 }
805
806 static void
gst_transcode_bin_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)807 gst_transcode_bin_get_property (GObject * object,
808 guint prop_id, GValue * value, GParamSpec * pspec)
809 {
810 GstTranscodeBin *self = GST_TRANSCODE_BIN (object);
811
812 switch (prop_id) {
813 case PROP_PROFILE:
814 GST_OBJECT_LOCK (self);
815 g_value_set_object (value, self->profile);
816 GST_OBJECT_UNLOCK (self);
817 break;
818 case PROP_AVOID_REENCODING:
819 GST_OBJECT_LOCK (self);
820 g_value_set_boolean (value, self->avoid_reencoding);
821 GST_OBJECT_UNLOCK (self);
822 break;
823 case PROP_AUDIO_FILTER:
824 GST_OBJECT_LOCK (self);
825 g_value_set_object (value, self->audio_filter);
826 GST_OBJECT_UNLOCK (self);
827 break;
828 case PROP_VIDEO_FILTER:
829 GST_OBJECT_LOCK (self);
830 g_value_set_object (value, self->video_filter);
831 GST_OBJECT_UNLOCK (self);
832 break;
833 default:
834 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
835 }
836 }
837
838 static GstPad *
gst_transcode_bin_request_pad(GstElement * element,GstPadTemplate * temp,const gchar * name,const GstCaps * caps)839 gst_transcode_bin_request_pad (GstElement * element, GstPadTemplate * temp,
840 const gchar * name, const GstCaps * caps)
841 {
842 GstTranscodeBin *self = (GstTranscodeBin *) element;
843 GstPad *gpad, *decodebin_pad =
844 gst_element_request_pad_simple (self->decodebin, "sink_%u");
845
846 if (!decodebin_pad) {
847 GST_ERROR_OBJECT (element,
848 "Could not request decodebin3 pad for %" GST_PTR_FORMAT, caps);
849
850 return NULL;
851 }
852
853 gpad = gst_ghost_pad_new_from_template (name, decodebin_pad, temp);
854 gst_element_add_pad (element, GST_PAD (gpad));
855 gst_object_unref (decodebin_pad);
856
857 return gpad;
858 }
859
860 static void
_set_filter(GstTranscodeBin * self,GstElement * filter,GstElement ** mfilter)861 _set_filter (GstTranscodeBin * self, GstElement * filter, GstElement ** mfilter)
862 {
863 if (filter) {
864 GST_OBJECT_LOCK (filter);
865 if (filter->numsinkpads != 1) {
866 GST_ERROR_OBJECT (self, "Can not use %" GST_PTR_FORMAT
867 " as filter as it does not have "
868 " one and only one sinkpad", filter);
869 goto bail_out;
870 } else if (filter->numsrcpads != 1) {
871 GST_ERROR_OBJECT (self, "Can not use %" GST_PTR_FORMAT
872 " as filter as it does not have " " one and only one srcpad", filter);
873 goto bail_out;
874 }
875 GST_OBJECT_UNLOCK (filter);
876
877 gst_bin_add (GST_BIN (self), gst_object_ref (filter));
878 }
879
880 GST_OBJECT_LOCK (self);
881 *mfilter = filter;
882 GST_OBJECT_UNLOCK (self);
883
884 return;
885
886 bail_out:
887 GST_OBJECT_UNLOCK (filter);
888 }
889
890 static void
_set_profile(GstTranscodeBin * self,GstEncodingProfile * profile)891 _set_profile (GstTranscodeBin * self, GstEncodingProfile * profile)
892 {
893 GST_OBJECT_LOCK (self);
894 self->profile = profile;
895 _setup_avoid_reencoding (self);
896 GST_OBJECT_UNLOCK (self);
897 }
898
899 static void
gst_transcode_bin_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)900 gst_transcode_bin_set_property (GObject * object,
901 guint prop_id, const GValue * value, GParamSpec * pspec)
902 {
903 GstTranscodeBin *self = GST_TRANSCODE_BIN (object);
904
905 switch (prop_id) {
906 case PROP_PROFILE:
907 _set_profile (self, g_value_dup_object (value));
908 break;
909 case PROP_AVOID_REENCODING:
910 GST_OBJECT_LOCK (self);
911 self->avoid_reencoding = g_value_get_boolean (value);
912 _setup_avoid_reencoding (self);
913 GST_OBJECT_UNLOCK (self);
914 break;
915 case PROP_AUDIO_FILTER:
916 _set_filter (self, g_value_dup_object (value), &self->audio_filter);
917 break;
918 case PROP_VIDEO_FILTER:
919 _set_filter (self, g_value_dup_object (value), &self->video_filter);
920 break;
921 default:
922 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
923 }
924 }
925
926 static void
gst_transcode_bin_class_init(GstTranscodeBinClass * klass)927 gst_transcode_bin_class_init (GstTranscodeBinClass * klass)
928 {
929 GObjectClass *object_class = G_OBJECT_CLASS (klass);
930 GstElementClass *gstelement_klass;
931
932 object_class->dispose = gst_transcode_bin_dispose;
933 object_class->get_property = gst_transcode_bin_get_property;
934 object_class->set_property = gst_transcode_bin_set_property;
935
936 gstelement_klass = (GstElementClass *) klass;
937 gstelement_klass->change_state =
938 GST_DEBUG_FUNCPTR (gst_transcode_bin_change_state);
939 gstelement_klass->request_new_pad =
940 GST_DEBUG_FUNCPTR (gst_transcode_bin_request_pad);
941
942 gst_element_class_add_pad_template (gstelement_klass,
943 gst_static_pad_template_get (&transcode_bin_sink_template));
944 gst_element_class_add_pad_template (gstelement_klass,
945 gst_static_pad_template_get (&transcode_bin_sinks_template));
946 gst_element_class_add_pad_template (gstelement_klass,
947 gst_static_pad_template_get (&transcode_bin_src_template));
948
949 gst_element_class_set_static_metadata (gstelement_klass,
950 "Transcode Bin", "Generic/Bin/Encoding",
951 "Autoplug and transcoder a stream",
952 "Thibault Saunier <tsaunier@igalia.com>");
953
954 /**
955 * GstTranscodeBin:profile:
956 *
957 * The #GstEncodingProfile to use. This property must be set before going
958 * to %GST_STATE_PAUSED or higher.
959 */
960 g_object_class_install_property (object_class, PROP_PROFILE,
961 g_param_spec_object ("profile", "Profile",
962 "The GstEncodingProfile to use", GST_TYPE_ENCODING_PROFILE,
963 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
964 GST_PARAM_MUTABLE_READY));
965
966 /**
967 * GstTranscodeBin:avoid-reencoding:
968 *
969 * See #encodebin:avoid-reencoding
970 */
971 g_object_class_install_property (object_class, PROP_AVOID_REENCODING,
972 g_param_spec_boolean ("avoid-reencoding", "Avoid re-encoding",
973 "Whether to re-encode portions of compatible video streams that lay on segment boundaries",
974 DEFAULT_AVOID_REENCODING,
975 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
976 GST_PARAM_MUTABLE_READY));
977
978 /**
979 * GstTranscodeBin:video-filter:
980 *
981 * Set the video filter element/bin to use.
982 */
983 g_object_class_install_property (object_class, PROP_VIDEO_FILTER,
984 g_param_spec_object ("video-filter", "Video filter",
985 "the video filter(s) to apply, if possible",
986 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
987 GST_PARAM_MUTABLE_READY));
988 /**
989 * GstTranscodeBin:audio-filter:
990 *
991 * Set the audio filter element/bin to use.
992 */
993 g_object_class_install_property (object_class, PROP_AUDIO_FILTER,
994 g_param_spec_object ("audio-filter", "Audio filter",
995 "the audio filter(s) to apply, if possible",
996 GST_TYPE_ELEMENT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
997 GST_PARAM_MUTABLE_READY));
998 }
999
1000 static void
gst_transcode_bin_init(GstTranscodeBin * self)1001 gst_transcode_bin_init (GstTranscodeBin * self)
1002 {
1003 GstPadTemplate *pad_tmpl;
1004
1005 pad_tmpl = gst_static_pad_template_get (&transcode_bin_sink_template);
1006 self->sinkpad = gst_ghost_pad_new_no_target_from_template ("sink", pad_tmpl);
1007 gst_pad_set_active (self->sinkpad, TRUE);
1008 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
1009
1010 gst_object_unref (pad_tmpl);
1011
1012 self->transcoding_streams =
1013 g_ptr_array_new_with_free_func ((GDestroyNotify) transcoding_stream_free);
1014
1015 make_decodebin (self);
1016 }
1017