1 /* GStreamer Smart Video Encoder element
2 * Copyright (C) <2010> Edward Hervey <bilboed@gmail.com>
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 St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /* TODO:
21 * * Implement get_caps/set_caps (store/forward caps)
22 * * Adjust template caps to the formats we can support
23 **/
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <string.h>
30 #include "gstsmartencoder.h"
31
32 GST_DEBUG_CATEGORY_STATIC (smart_encoder_debug);
33 #define GST_CAT_DEFAULT smart_encoder_debug
34
35 /* FIXME : Update this with new caps */
36 /* WARNING : We can only allow formats with closed-GOP */
37 #define ALLOWED_CAPS "video/x-h263;video/x-intel-h263;"\
38 "video/mpeg,mpegversion=(int)1,systemstream=(boolean)false;"\
39 "video/mpeg,mpegversion=(int)2,systemstream=(boolean)false;"
40
41 static GstStaticPadTemplate src_template =
42 GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC,
43 GST_PAD_ALWAYS,
44 GST_STATIC_CAPS (ALLOWED_CAPS)
45 );
46
47 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
48 GST_PAD_SINK,
49 GST_PAD_ALWAYS,
50 GST_STATIC_CAPS (ALLOWED_CAPS)
51 );
52
53 static GQuark INTERNAL_ELEMENT;
54
55 /* GstSmartEncoder signals and args */
56 enum
57 {
58 /* FILL ME */
59 LAST_SIGNAL
60 };
61
62 enum
63 {
64 PROP_0
65 /* FILL ME */
66 };
67
68 static void
_do_init(void)69 _do_init (void)
70 {
71 INTERNAL_ELEMENT = g_quark_from_static_string ("internal-element");
72 };
73
74 G_DEFINE_TYPE_EXTENDED (GstSmartEncoder, gst_smart_encoder, GST_TYPE_ELEMENT, 0,
75 _do_init ());
76
77 static void gst_smart_encoder_dispose (GObject * object);
78
79 static gboolean setup_recoder_pipeline (GstSmartEncoder * smart_encoder);
80
81 static GstFlowReturn gst_smart_encoder_chain (GstPad * pad, GstObject * parent,
82 GstBuffer * buf);
83 static gboolean smart_encoder_sink_event (GstPad * pad, GstObject * parent,
84 GstEvent * event);
85 static gboolean smart_encoder_sink_query (GstPad * pad, GstObject * parent,
86 GstQuery * query);
87 static GstCaps *smart_encoder_sink_getcaps (GstPad * pad, GstCaps * filter);
88 static GstStateChangeReturn
89 gst_smart_encoder_change_state (GstElement * element,
90 GstStateChange transition);
91
92 static void
gst_smart_encoder_class_init(GstSmartEncoderClass * klass)93 gst_smart_encoder_class_init (GstSmartEncoderClass * klass)
94 {
95 GObjectClass *gobject_class;
96 GstElementClass *element_class;
97
98 element_class = (GstElementClass *) klass;
99 gobject_class = G_OBJECT_CLASS (klass);
100
101 gst_smart_encoder_parent_class = g_type_class_peek_parent (klass);
102
103 gst_element_class_add_static_pad_template (element_class, &src_template);
104 gst_element_class_add_static_pad_template (element_class, &sink_template);
105
106 gst_element_class_set_static_metadata (element_class, "Smart Video Encoder",
107 "Codec/Recoder/Video",
108 "Re-encodes portions of Video that lay on segment boundaries",
109 "Edward Hervey <bilboed@gmail.com>");
110
111 gobject_class->dispose = (GObjectFinalizeFunc) (gst_smart_encoder_dispose);
112 element_class->change_state = gst_smart_encoder_change_state;
113
114 GST_DEBUG_CATEGORY_INIT (smart_encoder_debug, "smartencoder", 0,
115 "Smart Encoder");
116 }
117
118 static void
smart_encoder_reset(GstSmartEncoder * smart_encoder)119 smart_encoder_reset (GstSmartEncoder * smart_encoder)
120 {
121 gst_segment_init (smart_encoder->segment, GST_FORMAT_UNDEFINED);
122
123 if (smart_encoder->encoder) {
124 /* Clean up/remove elements */
125 gst_element_set_state (smart_encoder->encoder, GST_STATE_NULL);
126 gst_element_set_state (smart_encoder->decoder, GST_STATE_NULL);
127 gst_element_set_bus (smart_encoder->encoder, NULL);
128 gst_element_set_bus (smart_encoder->decoder, NULL);
129 gst_pad_set_active (smart_encoder->internal_srcpad, FALSE);
130 gst_pad_set_active (smart_encoder->internal_sinkpad, FALSE);
131 gst_object_unref (smart_encoder->encoder);
132 gst_object_unref (smart_encoder->decoder);
133 gst_object_unref (smart_encoder->internal_srcpad);
134 gst_object_unref (smart_encoder->internal_sinkpad);
135
136 smart_encoder->encoder = NULL;
137 smart_encoder->decoder = NULL;
138 smart_encoder->internal_sinkpad = NULL;
139 smart_encoder->internal_srcpad = NULL;
140 }
141
142 if (smart_encoder->newsegment) {
143 gst_event_unref (smart_encoder->newsegment);
144 smart_encoder->newsegment = NULL;
145 }
146 }
147
148
149 static void
gst_smart_encoder_init(GstSmartEncoder * smart_encoder)150 gst_smart_encoder_init (GstSmartEncoder * smart_encoder)
151 {
152 smart_encoder->sinkpad =
153 gst_pad_new_from_static_template (&sink_template, "sink");
154 gst_pad_set_chain_function (smart_encoder->sinkpad, gst_smart_encoder_chain);
155 gst_pad_set_event_function (smart_encoder->sinkpad, smart_encoder_sink_event);
156 gst_pad_set_query_function (smart_encoder->sinkpad, smart_encoder_sink_query);
157 gst_element_add_pad (GST_ELEMENT (smart_encoder), smart_encoder->sinkpad);
158
159 smart_encoder->srcpad =
160 gst_pad_new_from_static_template (&src_template, "src");
161 gst_pad_use_fixed_caps (smart_encoder->srcpad);
162 gst_element_add_pad (GST_ELEMENT (smart_encoder), smart_encoder->srcpad);
163
164 smart_encoder->segment = gst_segment_new ();
165
166 smart_encoder_reset (smart_encoder);
167 }
168
169 void
gst_smart_encoder_dispose(GObject * object)170 gst_smart_encoder_dispose (GObject * object)
171 {
172 GstSmartEncoder *smart_encoder = (GstSmartEncoder *) object;
173
174 if (smart_encoder->segment)
175 gst_segment_free (smart_encoder->segment);
176 smart_encoder->segment = NULL;
177 if (smart_encoder->available_caps)
178 gst_caps_unref (smart_encoder->available_caps);
179 smart_encoder->available_caps = NULL;
180 G_OBJECT_CLASS (gst_smart_encoder_parent_class)->dispose (object);
181 }
182
183 static GstFlowReturn
gst_smart_encoder_reencode_gop(GstSmartEncoder * smart_encoder)184 gst_smart_encoder_reencode_gop (GstSmartEncoder * smart_encoder)
185 {
186 GstFlowReturn res = GST_FLOW_OK;
187 GList *tmp;
188
189 if (smart_encoder->encoder == NULL) {
190 if (!setup_recoder_pipeline (smart_encoder))
191 return GST_FLOW_ERROR;
192 }
193
194 /* Activate elements */
195 /* Set elements to PAUSED */
196 gst_element_set_state (smart_encoder->encoder, GST_STATE_PAUSED);
197 gst_element_set_state (smart_encoder->decoder, GST_STATE_PAUSED);
198
199 GST_INFO ("Pushing Flush start/stop to clean decoder/encoder");
200 gst_pad_push_event (smart_encoder->internal_srcpad,
201 gst_event_new_flush_start ());
202 gst_pad_push_event (smart_encoder->internal_srcpad,
203 gst_event_new_flush_stop (TRUE));
204
205 /* push newsegment */
206 GST_INFO ("Pushing newsegment %" GST_PTR_FORMAT, smart_encoder->newsegment);
207 gst_pad_push_event (smart_encoder->internal_srcpad,
208 gst_event_ref (smart_encoder->newsegment));
209
210 /* Push buffers through our pads */
211 GST_DEBUG ("Pushing pending buffers");
212
213 for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
214 GstBuffer *buf = (GstBuffer *) tmp->data;
215
216 res = gst_pad_push (smart_encoder->internal_srcpad, buf);
217 if (G_UNLIKELY (res != GST_FLOW_OK))
218 break;
219 }
220
221 if (G_UNLIKELY (res != GST_FLOW_OK)) {
222 GST_WARNING ("Error pushing pending buffers : %s", gst_flow_get_name (res));
223 /* Remove pending bfufers */
224 for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
225 gst_buffer_unref ((GstBuffer *) tmp->data);
226 }
227 } else {
228 GST_INFO ("Pushing out EOS to flush out decoder/encoder");
229 gst_pad_push_event (smart_encoder->internal_srcpad, gst_event_new_eos ());
230 }
231
232 /* Activate elements */
233 /* Set elements to PAUSED */
234 gst_element_set_state (smart_encoder->encoder, GST_STATE_NULL);
235 gst_element_set_state (smart_encoder->decoder, GST_STATE_NULL);
236
237 g_list_free (smart_encoder->pending_gop);
238 smart_encoder->pending_gop = NULL;
239
240 return res;
241 }
242
243 static GstFlowReturn
gst_smart_encoder_push_pending_gop(GstSmartEncoder * smart_encoder)244 gst_smart_encoder_push_pending_gop (GstSmartEncoder * smart_encoder)
245 {
246 guint64 cstart, cstop;
247 GList *tmp;
248 GstFlowReturn res = GST_FLOW_OK;
249
250 GST_DEBUG ("Pushing pending GOP (%" GST_TIME_FORMAT " -- %" GST_TIME_FORMAT
251 ")", GST_TIME_ARGS (smart_encoder->gop_start),
252 GST_TIME_ARGS (smart_encoder->gop_stop));
253
254 /* If GOP is entirely within segment, just push downstream */
255 if (gst_segment_clip (smart_encoder->segment, GST_FORMAT_TIME,
256 smart_encoder->gop_start, smart_encoder->gop_stop, &cstart, &cstop)) {
257 if ((cstart != smart_encoder->gop_start)
258 || (cstop != smart_encoder->gop_stop)) {
259 GST_DEBUG ("GOP needs to be re-encoded from %" GST_TIME_FORMAT " to %"
260 GST_TIME_FORMAT, GST_TIME_ARGS (cstart), GST_TIME_ARGS (cstop));
261 res = gst_smart_encoder_reencode_gop (smart_encoder);
262 } else {
263 /* The whole GOP is within the segment, push all pending buffers downstream */
264 GST_DEBUG ("GOP doesn't need to be modified, pushing downstream");
265 for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
266 GstBuffer *buf = (GstBuffer *) tmp->data;
267 res = gst_pad_push (smart_encoder->srcpad, buf);
268 if (G_UNLIKELY (res != GST_FLOW_OK))
269 break;
270 }
271 }
272 } else {
273 /* The whole GOP is outside the segment, there's most likely
274 * a bug somewhere. */
275 GST_WARNING
276 ("GOP is entirely outside of the segment, upstream gave us too much data");
277 for (tmp = smart_encoder->pending_gop; tmp; tmp = tmp->next) {
278 gst_buffer_unref ((GstBuffer *) tmp->data);
279 }
280 }
281
282 if (smart_encoder->pending_gop) {
283 g_list_free (smart_encoder->pending_gop);
284 smart_encoder->pending_gop = NULL;
285 }
286 smart_encoder->gop_start = GST_CLOCK_TIME_NONE;
287 smart_encoder->gop_stop = GST_CLOCK_TIME_NONE;
288
289 return res;
290 }
291
292 static GstFlowReturn
gst_smart_encoder_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)293 gst_smart_encoder_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
294 {
295 GstSmartEncoder *smart_encoder;
296 GstFlowReturn res = GST_FLOW_OK;
297 gboolean discont, keyframe;
298
299 smart_encoder = GST_SMART_ENCODER (parent);
300
301 discont = GST_BUFFER_IS_DISCONT (buf);
302 keyframe = !GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT);
303
304 GST_DEBUG ("New buffer %s %s %" GST_TIME_FORMAT,
305 discont ? "discont" : "",
306 keyframe ? "keyframe" : "", GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)));
307
308 if (keyframe) {
309 GST_DEBUG ("Got a keyframe");
310
311 /* If there's a pending GOP, flush it out */
312 if (smart_encoder->pending_gop) {
313 /* Mark gop_stop */
314 smart_encoder->gop_stop = GST_BUFFER_TIMESTAMP (buf);
315
316 /* flush pending */
317 res = gst_smart_encoder_push_pending_gop (smart_encoder);
318 if (G_UNLIKELY (res != GST_FLOW_OK))
319 goto beach;
320 }
321
322 /* Mark gop_start for new gop */
323 smart_encoder->gop_start = GST_BUFFER_TIMESTAMP (buf);
324 }
325
326 /* Store buffer */
327 smart_encoder->pending_gop = g_list_append (smart_encoder->pending_gop, buf);
328 /* Update GOP stop position */
329 if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) {
330 smart_encoder->gop_stop = GST_BUFFER_TIMESTAMP (buf);
331 if (GST_BUFFER_DURATION_IS_VALID (buf))
332 smart_encoder->gop_stop += GST_BUFFER_DURATION (buf);
333 }
334
335 GST_DEBUG ("Buffer stored , Current GOP : %" GST_TIME_FORMAT " -- %"
336 GST_TIME_FORMAT, GST_TIME_ARGS (smart_encoder->gop_start),
337 GST_TIME_ARGS (smart_encoder->gop_stop));
338
339 beach:
340 return res;
341 }
342
343 static gboolean
smart_encoder_sink_event(GstPad * pad,GstObject * parent,GstEvent * event)344 smart_encoder_sink_event (GstPad * pad, GstObject * parent, GstEvent * event)
345 {
346 gboolean res = TRUE;
347 GstSmartEncoder *smart_encoder = GST_SMART_ENCODER (parent);
348
349 switch (GST_EVENT_TYPE (event)) {
350 case GST_EVENT_FLUSH_STOP:
351 smart_encoder_reset (smart_encoder);
352 break;
353 case GST_EVENT_SEGMENT:
354 {
355 gst_event_copy_segment (event, smart_encoder->segment);
356
357 GST_DEBUG_OBJECT (smart_encoder, "segment: %" GST_SEGMENT_FORMAT,
358 smart_encoder->segment);
359 if (smart_encoder->segment->format != GST_FORMAT_TIME) {
360 GST_ERROR
361 ("smart_encoder can not handle streams not specified in GST_FORMAT_TIME");
362 gst_event_unref (event);
363 return FALSE;
364 }
365
366 /* And keep a copy for further usage */
367 if (smart_encoder->newsegment)
368 gst_event_unref (smart_encoder->newsegment);
369 smart_encoder->newsegment = gst_event_ref (event);
370 }
371 break;
372 case GST_EVENT_EOS:
373 GST_DEBUG ("Eos, flushing remaining data");
374 if (smart_encoder->segment->format == GST_FORMAT_TIME)
375 gst_smart_encoder_push_pending_gop (smart_encoder);
376 break;
377 default:
378 break;
379 }
380
381 res = gst_pad_push_event (smart_encoder->srcpad, event);
382
383 return res;
384 }
385
386 static GstCaps *
smart_encoder_sink_getcaps(GstPad * pad,GstCaps * filter)387 smart_encoder_sink_getcaps (GstPad * pad, GstCaps * filter)
388 {
389 GstCaps *peer, *tmpl, *res;
390 GstSmartEncoder *smart_encoder = GST_SMART_ENCODER (gst_pad_get_parent (pad));
391
392 /* Use computed caps */
393 if (smart_encoder->available_caps)
394 tmpl = gst_caps_ref (smart_encoder->available_caps);
395 else
396 tmpl = gst_static_pad_template_get_caps (&src_template);
397
398 /* Try getting it from downstream */
399 peer = gst_pad_peer_query_caps (smart_encoder->srcpad, tmpl);
400
401 if (peer == NULL) {
402 res = tmpl;
403 } else {
404 res = peer;
405 gst_caps_unref (tmpl);
406 }
407
408 gst_object_unref (smart_encoder);
409 return res;
410 }
411
412 static gboolean
smart_encoder_sink_query(GstPad * pad,GstObject * parent,GstQuery * query)413 smart_encoder_sink_query (GstPad * pad, GstObject * parent, GstQuery * query)
414 {
415 gboolean res;
416
417 switch (GST_QUERY_TYPE (query)) {
418 case GST_QUERY_CAPS:
419 {
420 GstCaps *filter, *caps;
421
422 gst_query_parse_caps (query, &filter);
423 caps = smart_encoder_sink_getcaps (pad, filter);
424 gst_query_set_caps_result (query, caps);
425 gst_caps_unref (caps);
426 res = TRUE;
427 break;
428 }
429 default:
430 res = gst_pad_query_default (pad, parent, query);
431 break;
432 }
433 return res;
434 }
435
436 /*****************************************
437 * Internal encoder/decoder pipeline *
438 ******************************************/
439
440 static GstElementFactory *
get_decoder_factory(GstCaps * caps)441 get_decoder_factory (GstCaps * caps)
442 {
443 GstElementFactory *fact = NULL;
444 GList *decoders, *tmp;
445
446 tmp =
447 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_DECODER,
448 GST_RANK_MARGINAL);
449 decoders = gst_element_factory_list_filter (tmp, caps, GST_PAD_SINK, FALSE);
450 gst_plugin_feature_list_free (tmp);
451
452 for (tmp = decoders; tmp; tmp = tmp->next) {
453 /* We just pick the first one */
454 fact = (GstElementFactory *) tmp->data;
455 gst_object_ref (fact);
456 break;
457 }
458
459 gst_plugin_feature_list_free (decoders);
460
461 return fact;
462 }
463
464 static GstElementFactory *
get_encoder_factory(GstCaps * caps)465 get_encoder_factory (GstCaps * caps)
466 {
467 GstElementFactory *fact = NULL;
468 GList *encoders, *tmp;
469
470 tmp =
471 gst_element_factory_list_get_elements (GST_ELEMENT_FACTORY_TYPE_ENCODER,
472 GST_RANK_MARGINAL);
473 encoders = gst_element_factory_list_filter (tmp, caps, GST_PAD_SRC, FALSE);
474 gst_plugin_feature_list_free (tmp);
475
476 for (tmp = encoders; tmp; tmp = tmp->next) {
477 /* We just pick the first one */
478 fact = (GstElementFactory *) tmp->data;
479 gst_object_ref (fact);
480 break;
481 }
482
483 gst_plugin_feature_list_free (encoders);
484
485 return fact;
486 }
487
488 static GstElement *
get_decoder(GstCaps * caps)489 get_decoder (GstCaps * caps)
490 {
491 GstElementFactory *fact = get_decoder_factory (caps);
492 GstElement *res = NULL;
493
494 if (fact) {
495 res = gst_element_factory_create (fact, "internal-decoder");
496 gst_object_unref (fact);
497 }
498 return res;
499 }
500
501 static GstElement *
get_encoder(GstCaps * caps)502 get_encoder (GstCaps * caps)
503 {
504 GstElementFactory *fact = get_encoder_factory (caps);
505 GstElement *res = NULL;
506
507 if (fact) {
508 res = gst_element_factory_create (fact, "internal-encoder");
509 gst_object_unref (fact);
510 }
511 return res;
512 }
513
514 static GstFlowReturn
internal_chain(GstPad * pad,GstObject * parent,GstBuffer * buf)515 internal_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
516 {
517 GstSmartEncoder *smart_encoder =
518 g_object_get_qdata ((GObject *) pad, INTERNAL_ELEMENT);
519
520 return gst_pad_push (smart_encoder->srcpad, buf);
521 }
522
523 static gboolean
setup_recoder_pipeline(GstSmartEncoder * smart_encoder)524 setup_recoder_pipeline (GstSmartEncoder * smart_encoder)
525 {
526 GstPad *tmppad;
527 GstCaps *caps;
528
529 /* Fast path */
530 if (G_UNLIKELY (smart_encoder->encoder))
531 return TRUE;
532
533 GST_DEBUG ("Creating internal decoder and encoder");
534
535 /* Create decoder/encoder */
536 caps = gst_pad_get_current_caps (smart_encoder->sinkpad);
537 smart_encoder->decoder = get_decoder (caps);
538 if (G_UNLIKELY (smart_encoder->decoder == NULL))
539 goto no_decoder;
540 gst_caps_unref (caps);
541 gst_element_set_bus (smart_encoder->decoder, GST_ELEMENT_BUS (smart_encoder));
542
543 caps = gst_pad_get_current_caps (smart_encoder->sinkpad);
544 smart_encoder->encoder = get_encoder (caps);
545 if (G_UNLIKELY (smart_encoder->encoder == NULL))
546 goto no_encoder;
547 gst_caps_unref (caps);
548 gst_element_set_bus (smart_encoder->encoder, GST_ELEMENT_BUS (smart_encoder));
549
550 GST_DEBUG ("Creating internal pads");
551
552 /* Create internal pads */
553
554 /* Source pad which we'll use to feed data to decoders */
555 smart_encoder->internal_srcpad = gst_pad_new ("internal_src", GST_PAD_SRC);
556 g_object_set_qdata ((GObject *) smart_encoder->internal_srcpad,
557 INTERNAL_ELEMENT, smart_encoder);
558 gst_pad_set_active (smart_encoder->internal_srcpad, TRUE);
559
560 /* Sink pad which will get the buffers from the encoder.
561 * Note: We don't need an event function since we'll be discarding all
562 * of them. */
563 smart_encoder->internal_sinkpad = gst_pad_new ("internal_sink", GST_PAD_SINK);
564 g_object_set_qdata ((GObject *) smart_encoder->internal_sinkpad,
565 INTERNAL_ELEMENT, smart_encoder);
566 gst_pad_set_chain_function (smart_encoder->internal_sinkpad, internal_chain);
567 gst_pad_set_active (smart_encoder->internal_sinkpad, TRUE);
568
569 GST_DEBUG ("Linking pads to elements");
570
571 /* Link everything */
572 tmppad = gst_element_get_static_pad (smart_encoder->encoder, "src");
573 if (GST_PAD_LINK_FAILED (gst_pad_link (tmppad,
574 smart_encoder->internal_sinkpad)))
575 goto sinkpad_link_fail;
576 gst_object_unref (tmppad);
577
578 if (!gst_element_link (smart_encoder->decoder, smart_encoder->encoder))
579 goto encoder_decoder_link_fail;
580
581 tmppad = gst_element_get_static_pad (smart_encoder->decoder, "sink");
582 if (GST_PAD_LINK_FAILED (gst_pad_link (smart_encoder->internal_srcpad,
583 tmppad)))
584 goto srcpad_link_fail;
585 gst_object_unref (tmppad);
586
587 GST_DEBUG ("Done creating internal elements/pads");
588
589 return TRUE;
590
591 no_decoder:
592 {
593 GST_WARNING ("Couldn't find a decoder for %" GST_PTR_FORMAT, caps);
594 gst_caps_unref (caps);
595 return FALSE;
596 }
597
598 no_encoder:
599 {
600 GST_WARNING ("Couldn't find an encoder for %" GST_PTR_FORMAT, caps);
601 gst_caps_unref (caps);
602 return FALSE;
603 }
604
605 srcpad_link_fail:
606 {
607 gst_object_unref (tmppad);
608 GST_WARNING ("Couldn't link internal srcpad to decoder");
609 return FALSE;
610 }
611
612 sinkpad_link_fail:
613 {
614 gst_object_unref (tmppad);
615 GST_WARNING ("Couldn't link encoder to internal sinkpad");
616 return FALSE;
617 }
618
619 encoder_decoder_link_fail:
620 {
621 GST_WARNING ("Couldn't link decoder to encoder");
622 return FALSE;
623 }
624 }
625
626 static GstStateChangeReturn
gst_smart_encoder_find_elements(GstSmartEncoder * smart_encoder)627 gst_smart_encoder_find_elements (GstSmartEncoder * smart_encoder)
628 {
629 guint i, n;
630 GstCaps *tmpl, *st, *res;
631 GstElementFactory *dec, *enc;
632 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
633
634 if (G_UNLIKELY (smart_encoder->available_caps))
635 goto beach;
636
637 /* Iterate over all pad template caps and see if we have both an
638 * encoder and a decoder for those media types */
639 tmpl = gst_static_pad_template_get_caps (&src_template);
640 res = gst_caps_new_empty ();
641 n = gst_caps_get_size (tmpl);
642
643 for (i = 0; i < n; i++) {
644 st = gst_caps_copy_nth (tmpl, i);
645 GST_DEBUG_OBJECT (smart_encoder,
646 "Checking for available decoder and encoder for %" GST_PTR_FORMAT, st);
647 if (!(dec = get_decoder_factory (st))) {
648 gst_caps_unref (st);
649 continue;
650 }
651 gst_object_unref (dec);
652 if (!(enc = get_encoder_factory (st))) {
653 gst_caps_unref (st);
654 continue;
655 }
656 gst_object_unref (enc);
657 GST_DEBUG_OBJECT (smart_encoder, "OK");
658 gst_caps_append (res, st);
659 }
660
661 gst_caps_unref (tmpl);
662
663 if (gst_caps_is_empty (res)) {
664 gst_caps_unref (res);
665 ret = GST_STATE_CHANGE_FAILURE;
666 } else
667 smart_encoder->available_caps = res;
668
669 GST_DEBUG_OBJECT (smart_encoder, "Done, available_caps:%" GST_PTR_FORMAT,
670 smart_encoder->available_caps);
671
672 beach:
673 return ret;
674 }
675
676 /******************************************
677 * GstElement vmethod implementations *
678 ******************************************/
679
680 static GstStateChangeReturn
gst_smart_encoder_change_state(GstElement * element,GstStateChange transition)681 gst_smart_encoder_change_state (GstElement * element, GstStateChange transition)
682 {
683 GstSmartEncoder *smart_encoder;
684 GstStateChangeReturn ret;
685
686 g_return_val_if_fail (GST_IS_SMART_ENCODER (element),
687 GST_STATE_CHANGE_FAILURE);
688
689 smart_encoder = GST_SMART_ENCODER (element);
690
691 switch (transition) {
692 case GST_STATE_CHANGE_NULL_TO_READY:
693 /* Figure out which elements are available */
694 if ((ret =
695 gst_smart_encoder_find_elements (smart_encoder)) ==
696 GST_STATE_CHANGE_FAILURE)
697 goto beach;
698 break;
699 default:
700 break;
701 }
702
703 ret =
704 GST_ELEMENT_CLASS (gst_smart_encoder_parent_class)->change_state (element,
705 transition);
706
707 switch (transition) {
708 case GST_STATE_CHANGE_PAUSED_TO_READY:
709 smart_encoder_reset (smart_encoder);
710 break;
711 default:
712 break;
713 }
714
715 beach:
716 return ret;
717 }
718