1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
|
/* GStreamer
* Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#ifdef HAVE_FFMPEG_UNINSTALLED
#include <avformat.h>
#else
#include <libavformat/avformat.h>
#endif
#include <gst/gst.h>
#include <gst/base/gstcollectpads.h>
#include "gstffmpeg.h"
#include "gstffmpegcodecmap.h"
#include "gstffmpegutils.h"
typedef struct _GstFFMpegMux GstFFMpegMux;
typedef struct _GstFFMpegMuxPad GstFFMpegMuxPad;
struct _GstFFMpegMuxPad
{
GstCollectData collect; /* we extend the CollectData */
gint padnum;
};
struct _GstFFMpegMux
{
GstElement element;
GstCollectPads *collect;
/* We need to keep track of our pads, so we do so here. */
GstPad *srcpad;
AVFormatContext *context;
gboolean opened;
gint videopads, audiopads;
/*< private > */
/* event_function is the collectpads default eventfunction */
GstPadEventFunction event_function;
int preload;
int max_delay;
};
typedef struct _GstFFMpegMuxClassParams
{
AVOutputFormat *in_plugin;
GstCaps *srccaps, *videosinkcaps, *audiosinkcaps;
} GstFFMpegMuxClassParams;
typedef struct _GstFFMpegMuxClass GstFFMpegMuxClass;
struct _GstFFMpegMuxClass
{
GstElementClass parent_class;
AVOutputFormat *in_plugin;
};
#define GST_TYPE_FFMPEGMUX \
(gst_ffmpegdec_get_type())
#define GST_FFMPEGMUX(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_FFMPEGMUX,GstFFMpegMux))
#define GST_FFMPEGMUX_CLASS(klass) \
(G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_FFMPEGMUX,GstFFMpegMuxClass))
#define GST_IS_FFMPEGMUX(obj) \
(G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_FFMPEGMUX))
#define GST_IS_FFMPEGMUX_CLASS(klass) \
(G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_FFMPEGMUX))
enum
{
/* FILL ME */
LAST_SIGNAL
};
enum
{
ARG_0,
/* FILL ME */
};
enum
{
PROP_0,
PROP_PRELOAD,
PROP_MAXDELAY
};
/* A number of function prototypes are given so we can refer to them later. */
static void gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass);
static void gst_ffmpegmux_base_init (gpointer g_class);
static void gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux,
GstFFMpegMuxClass * g_class);
static void gst_ffmpegmux_finalize (GObject * object);
static gboolean gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps);
static GstPad *gst_ffmpegmux_request_new_pad (GstElement * element,
GstPadTemplate * templ, const gchar * name);
static GstFlowReturn gst_ffmpegmux_collected (GstCollectPads * pads,
gpointer user_data);
static gboolean gst_ffmpegmux_sink_event (GstPad * pad, GstEvent * event);
static GstStateChangeReturn gst_ffmpegmux_change_state (GstElement * element,
GstStateChange transition);
static void gst_ffmpegmux_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec);
static void gst_ffmpegmux_get_property (GObject * object, guint prop_id,
GValue * value, GParamSpec * pspec);
#define GST_FFMUX_PARAMS_QDATA g_quark_from_static_string("ffmux-params")
static GstElementClass *parent_class = NULL;
/*static guint gst_ffmpegmux_signals[LAST_SIGNAL] = { 0 }; */
static void
gst_ffmpegmux_base_init (gpointer g_class)
{
GstFFMpegMuxClass *klass = (GstFFMpegMuxClass *) g_class;
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
GstElementDetails details;
GstFFMpegMuxClassParams *params;
GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
params =
(GstFFMpegMuxClassParams *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
GST_FFMUX_PARAMS_QDATA);
g_assert (params != NULL);
/* construct the element details struct */
details.longname = g_strdup_printf ("FFmpeg %s muxer",
params->in_plugin->long_name);
details.klass = g_strdup ("Codec/Muxer");
details.description = g_strdup_printf ("FFmpeg %s muxer",
params->in_plugin->long_name);
details.author = "Wim Taymans <wim.taymans@chello.be>, "
"Ronald Bultje <rbultje@ronald.bitfreak.net>";
gst_element_class_set_details (element_class, &details);
g_free (details.longname);
g_free (details.klass);
g_free (details.description);
/* pad templates */
srctempl = gst_pad_template_new ("src", GST_PAD_SRC,
GST_PAD_ALWAYS, params->srccaps);
gst_element_class_add_pad_template (element_class, srctempl);
if (params->audiosinkcaps) {
audiosinktempl = gst_pad_template_new ("audio_%d",
GST_PAD_SINK, GST_PAD_REQUEST, params->audiosinkcaps);
gst_element_class_add_pad_template (element_class, audiosinktempl);
}
if (params->videosinkcaps) {
videosinktempl = gst_pad_template_new ("video_%d",
GST_PAD_SINK, GST_PAD_REQUEST, params->videosinkcaps);
gst_element_class_add_pad_template (element_class, videosinktempl);
}
klass->in_plugin = params->in_plugin;
}
static void
gst_ffmpegmux_class_init (GstFFMpegMuxClass * klass)
{
GObjectClass *gobject_class;
GstElementClass *gstelement_class;
gobject_class = (GObjectClass *) klass;
gstelement_class = (GstElementClass *) klass;
parent_class = g_type_class_peek_parent (klass);
gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_set_property);
gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_ffmpegmux_get_property);
g_object_class_install_property (gobject_class, PROP_PRELOAD,
g_param_spec_int ("preload", "preload",
"Set the initial demux-decode delay (in microseconds)", 0, G_MAXINT,
0, G_PARAM_READWRITE));
g_object_class_install_property (gobject_class, PROP_MAXDELAY,
g_param_spec_int ("maxdelay", "maxdelay",
"Set the maximum demux-decode delay (in microseconds)", 0, G_MAXINT,
0, G_PARAM_READWRITE));
gstelement_class->request_new_pad = gst_ffmpegmux_request_new_pad;
gstelement_class->change_state = gst_ffmpegmux_change_state;
gobject_class->finalize = gst_ffmpegmux_finalize;
}
static void
gst_ffmpegmux_init (GstFFMpegMux * ffmpegmux, GstFFMpegMuxClass * g_class)
{
GstElementClass *klass = GST_ELEMENT_CLASS (g_class);
GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass *) klass;
GstPadTemplate *templ = gst_element_class_get_pad_template (klass, "src");
ffmpegmux->srcpad = gst_pad_new_from_template (templ, "src");
gst_pad_set_caps (ffmpegmux->srcpad, gst_pad_template_get_caps (templ));
gst_element_add_pad (GST_ELEMENT (ffmpegmux), ffmpegmux->srcpad);
ffmpegmux->collect = gst_collect_pads_new ();
gst_collect_pads_set_function (ffmpegmux->collect,
(GstCollectPadsFunction) gst_ffmpegmux_collected, ffmpegmux);
ffmpegmux->context = g_new0 (AVFormatContext, 1);
ffmpegmux->context->oformat = oclass->in_plugin;
ffmpegmux->context->nb_streams = 0;
g_snprintf (ffmpegmux->context->filename,
sizeof (ffmpegmux->context->filename),
"gstreamer://%p", ffmpegmux->srcpad);
ffmpegmux->opened = FALSE;
ffmpegmux->videopads = 0;
ffmpegmux->audiopads = 0;
ffmpegmux->preload = 0;
ffmpegmux->max_delay = 0;
}
static void
gst_ffmpegmux_set_property (GObject * object, guint prop_id,
const GValue * value, GParamSpec * pspec)
{
GstFFMpegMux *src;
src = (GstFFMpegMux *) object;
switch (prop_id) {
case PROP_PRELOAD:
src->preload = g_value_get_int (value);
break;
case PROP_MAXDELAY:
src->max_delay = g_value_get_int (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_ffmpegmux_get_property (GObject * object, guint prop_id, GValue * value,
GParamSpec * pspec)
{
GstFFMpegMux *src;
src = (GstFFMpegMux *) object;
switch (prop_id) {
case PROP_PRELOAD:
g_value_set_int (value, src->preload);
break;
case PROP_MAXDELAY:
g_value_set_int (value, src->max_delay);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gst_ffmpegmux_finalize (GObject * object)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) object;
g_free (ffmpegmux->context);
gst_object_unref (ffmpegmux->collect);
if (G_OBJECT_CLASS (parent_class)->finalize)
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static GstPad *
gst_ffmpegmux_request_new_pad (GstElement * element,
GstPadTemplate * templ, const gchar * name)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
GstFFMpegMuxClass *oclass = (GstFFMpegMuxClass *) klass;
GstFFMpegMuxPad *collect_pad;
gchar *padname;
GstPad *pad;
AVStream *st;
enum CodecType type;
gint bitrate = 0, framesize = 0;
g_return_val_if_fail (templ != NULL, NULL);
g_return_val_if_fail (templ->direction == GST_PAD_SINK, NULL);
g_return_val_if_fail (ffmpegmux->opened == FALSE, NULL);
/* figure out a name that *we* like */
if (templ == gst_element_class_get_pad_template (klass, "video_%d")) {
padname = g_strdup_printf ("video_%d", ffmpegmux->videopads++);
type = CODEC_TYPE_VIDEO;
bitrate = 64 * 1024;
framesize = 1152;
} else if (templ == gst_element_class_get_pad_template (klass, "audio_%d")) {
padname = g_strdup_printf ("audio_%d", ffmpegmux->audiopads++);
type = CODEC_TYPE_AUDIO;
bitrate = 285 * 1024;
} else {
g_warning ("ffmux: unknown pad template!");
return NULL;
}
/* create pad */
pad = gst_pad_new_from_template (templ, padname);
collect_pad = (GstFFMpegMuxPad *)
gst_collect_pads_add_pad (ffmpegmux->collect, pad,
sizeof (GstFFMpegMuxPad));
collect_pad->padnum = ffmpegmux->context->nb_streams;
/* small hack to put our own event pad function and chain up to collect pad */
ffmpegmux->event_function = GST_PAD_EVENTFUNC (pad);
gst_pad_set_event_function (pad,
GST_DEBUG_FUNCPTR (gst_ffmpegmux_sink_event));
gst_pad_set_setcaps_function (pad, GST_DEBUG_FUNCPTR (gst_ffmpegmux_setcaps));
gst_element_add_pad (element, pad);
/* AVStream needs to be created */
st = av_new_stream (ffmpegmux->context, collect_pad->padnum);
st->codec->codec_type = type;
st->codec->codec_id = CODEC_ID_NONE; /* this is a check afterwards */
st->stream_copy = 1; /* we're not the actual encoder */
st->codec->bit_rate = bitrate;
st->codec->frame_size = framesize;
/* we fill in codec during capsnego */
/* we love debug output (c) (tm) (r) */
GST_DEBUG ("Created %s pad for ffmux_%s element",
padname, oclass->in_plugin->name);
g_free (padname);
return pad;
}
/**
* gst_ffmpegmux_setcaps
* @pad: #GstPad
* @caps: New caps.
*
* Set caps to pad.
*
* Returns: #TRUE on success.
*/
static gboolean
gst_ffmpegmux_setcaps (GstPad * pad, GstCaps * caps)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (gst_pad_get_parent (pad));
GstFFMpegMuxPad *collect_pad;
AVStream *st;
collect_pad = (GstFFMpegMuxPad *) gst_pad_get_element_private (pad);
st = ffmpegmux->context->streams[collect_pad->padnum];
ffmpegmux->context->preload = ffmpegmux->preload;
ffmpegmux->context->max_delay = ffmpegmux->max_delay;
/* for the format-specific guesses, we'll go to
* our famous codec mapper */
if (gst_ffmpeg_caps_to_codecid (caps, st->codec) == CODEC_ID_NONE)
goto not_accepted;
/* copy over the aspect ratios, ffmpeg expects the stream aspect to match the
* codec aspect. */
st->sample_aspect_ratio = st->codec->sample_aspect_ratio;
GST_LOG_OBJECT (pad, "accepted caps %" GST_PTR_FORMAT, caps);
return TRUE;
/* ERRORS */
not_accepted:
{
GST_LOG_OBJECT (pad, "rejecting caps %" GST_PTR_FORMAT, caps);
return FALSE;
}
}
static gboolean
gst_ffmpegmux_sink_event (GstPad * pad, GstEvent * event)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) gst_pad_get_parent (pad);
gboolean res = TRUE;
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_TAG:{
GstTagList *taglist;
GstTagSetter *setter = GST_TAG_SETTER (ffmpegmux);
const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
gst_event_parse_tag (event, &taglist);
gst_tag_setter_merge_tags (setter, taglist, mode);
break;
}
default:
break;
}
/* chaining up to collectpads default event function */
res = ffmpegmux->event_function (pad, event);
gst_object_unref (ffmpegmux);
return res;
}
static GstFlowReturn
gst_ffmpegmux_collected (GstCollectPads * pads, gpointer user_data)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
GSList *collected;
GstFFMpegMuxPad *best_pad;
GstClockTime best_time;
const GstTagList *tags;
/* open "file" (gstreamer protocol to next element) */
if (!ffmpegmux->opened) {
int open_flags = URL_WRONLY;
/* we do need all streams to have started capsnego,
* or things will go horribly wrong */
for (collected = ffmpegmux->collect->data; collected;
collected = g_slist_next (collected)) {
GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
AVStream *st = ffmpegmux->context->streams[collect_pad->padnum];
/* check whether the pad has successfully completed capsnego */
if (st->codec->codec_id == CODEC_ID_NONE) {
GST_ELEMENT_ERROR (ffmpegmux, CORE, NEGOTIATION, (NULL),
("no caps set on stream %d (%s)", collect_pad->padnum,
(st->codec->codec_type == CODEC_TYPE_VIDEO) ?
"video" : "audio"));
return GST_FLOW_ERROR;
}
/* set framerate for audio */
if (st->codec->codec_type == CODEC_TYPE_AUDIO) {
switch (st->codec->codec_id) {
case CODEC_ID_PCM_S16LE:
case CODEC_ID_PCM_S16BE:
case CODEC_ID_PCM_U16LE:
case CODEC_ID_PCM_U16BE:
case CODEC_ID_PCM_S8:
case CODEC_ID_PCM_U8:
st->codec->frame_size = 1;
break;
default:
{
GstBuffer *buffer;
/* FIXME : This doesn't work for RAW AUDIO...
* in fact I'm wondering if it even works for any kind of audio... */
buffer = gst_collect_pads_peek (ffmpegmux->collect,
(GstCollectData *) collect_pad);
if (buffer) {
st->codec->frame_size =
st->codec->sample_rate *
GST_BUFFER_DURATION (buffer) / GST_SECOND;
gst_buffer_unref (buffer);
}
}
}
}
}
/* tags */
tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (ffmpegmux));
if (tags) {
gint i;
gchar *s;
/* get the interesting ones */
if (gst_tag_list_get_string (tags, GST_TAG_TITLE, &s)) {
strncpy (ffmpegmux->context->title, s,
sizeof (ffmpegmux->context->title));
}
if (gst_tag_list_get_string (tags, GST_TAG_ARTIST, &s)) {
strncpy (ffmpegmux->context->author, s,
sizeof (ffmpegmux->context->author));
}
if (gst_tag_list_get_string (tags, GST_TAG_COPYRIGHT, &s)) {
strncpy (ffmpegmux->context->copyright, s,
sizeof (ffmpegmux->context->copyright));
}
if (gst_tag_list_get_string (tags, GST_TAG_COMMENT, &s)) {
strncpy (ffmpegmux->context->comment, s,
sizeof (ffmpegmux->context->comment));
}
if (gst_tag_list_get_string (tags, GST_TAG_ALBUM, &s)) {
strncpy (ffmpegmux->context->album, s,
sizeof (ffmpegmux->context->album));
}
if (gst_tag_list_get_string (tags, GST_TAG_GENRE, &s)) {
strncpy (ffmpegmux->context->genre, s,
sizeof (ffmpegmux->context->genre));
}
if (gst_tag_list_get_int (tags, GST_TAG_TRACK_NUMBER, &i)) {
ffmpegmux->context->track = i;
}
}
/* set the streamheader flag for gstffmpegprotocol if codec supports it */
if (!strcmp (ffmpegmux->context->oformat->name, "flv")) {
open_flags |= GST_FFMPEG_URL_STREAMHEADER;
}
if (url_fopen (&ffmpegmux->context->pb,
ffmpegmux->context->filename, open_flags) < 0) {
GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, TOO_LAZY, (NULL),
("Failed to open stream context in ffmux"));
return GST_FLOW_ERROR;
}
if (av_set_parameters (ffmpegmux->context, NULL) < 0) {
GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, INIT, (NULL),
("Failed to initialize muxer"));
return GST_FLOW_ERROR;
}
/* now open the mux format */
if (av_write_header (ffmpegmux->context) < 0) {
GST_ELEMENT_ERROR (ffmpegmux, LIBRARY, SETTINGS, (NULL),
("Failed to write file header - check codec settings"));
return GST_FLOW_ERROR;
}
/* we're now opened */
ffmpegmux->opened = TRUE;
/* flush the header so it will be used as streamheader */
put_flush_packet (ffmpegmux->context->pb);
}
/* take the one with earliest timestamp,
* and push it forward */
best_pad = NULL;
best_time = GST_CLOCK_TIME_NONE;
for (collected = ffmpegmux->collect->data; collected;
collected = g_slist_next (collected)) {
GstFFMpegMuxPad *collect_pad = (GstFFMpegMuxPad *) collected->data;
GstBuffer *buffer = gst_collect_pads_peek (ffmpegmux->collect,
(GstCollectData *) collect_pad);
/* if there's no buffer, just continue */
if (buffer == NULL) {
continue;
}
/* if we have no buffer yet, just use the first one */
if (best_pad == NULL) {
best_pad = collect_pad;
best_time = GST_BUFFER_TIMESTAMP (buffer);
goto next_pad;
}
/* if we do have one, only use this one if it's older */
if (GST_BUFFER_TIMESTAMP (buffer) < best_time) {
best_time = GST_BUFFER_TIMESTAMP (buffer);
best_pad = collect_pad;
}
next_pad:
gst_buffer_unref (buffer);
/* Mux buffers with invalid timestamp first */
if (!GST_CLOCK_TIME_IS_VALID (best_time))
break;
}
/* now handle the buffer, or signal EOS if we have
* no buffers left */
if (best_pad != NULL) {
GstBuffer *buf;
AVPacket pkt;
gboolean need_free = FALSE;
/* push out current buffer */
buf = gst_collect_pads_pop (ffmpegmux->collect,
(GstCollectData *) best_pad);
ffmpegmux->context->streams[best_pad->padnum]->codec->frame_number++;
/* set time */
pkt.pts = gst_ffmpeg_time_gst_to_ff (GST_BUFFER_TIMESTAMP (buf),
ffmpegmux->context->streams[best_pad->padnum]->time_base);
pkt.dts = pkt.pts;
if (strcmp (ffmpegmux->context->oformat->name, "gif") == 0) {
AVStream *st = ffmpegmux->context->streams[best_pad->padnum];
AVPicture src, dst;
need_free = TRUE;
pkt.size = st->codec->width * st->codec->height * 3;
pkt.data = g_malloc (pkt.size);
dst.data[0] = pkt.data;
dst.data[1] = NULL;
dst.data[2] = NULL;
dst.linesize[0] = st->codec->width * 3;
gst_ffmpeg_avpicture_fill (&src, GST_BUFFER_DATA (buf),
PIX_FMT_RGB24, st->codec->width, st->codec->height);
av_picture_copy (&dst, &src, PIX_FMT_RGB24,
st->codec->width, st->codec->height);
} else {
pkt.data = GST_BUFFER_DATA (buf);
pkt.size = GST_BUFFER_SIZE (buf);
}
pkt.stream_index = best_pad->padnum;
pkt.flags = 0;
if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
pkt.flags |= PKT_FLAG_KEY;
if (GST_BUFFER_DURATION_IS_VALID (buf))
pkt.duration =
gst_ffmpeg_time_gst_to_ff (GST_BUFFER_DURATION (buf),
ffmpegmux->context->streams[best_pad->padnum]->time_base);
else
pkt.duration = 0;
av_write_frame (ffmpegmux->context, &pkt);
gst_buffer_unref (buf);
if (need_free)
g_free (pkt.data);
} else {
/* close down */
av_write_trailer (ffmpegmux->context);
ffmpegmux->opened = FALSE;
put_flush_packet (ffmpegmux->context->pb);
url_fclose (ffmpegmux->context->pb);
gst_pad_push_event (ffmpegmux->srcpad, gst_event_new_eos ());
return GST_FLOW_UNEXPECTED;
}
return GST_FLOW_OK;
}
static GstStateChangeReturn
gst_ffmpegmux_change_state (GstElement * element, GstStateChange transition)
{
GstFlowReturn ret;
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) (element);
switch (transition) {
case GST_STATE_CHANGE_NULL_TO_READY:
break;
case GST_STATE_CHANGE_READY_TO_PAUSED:
gst_collect_pads_start (ffmpegmux->collect);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_collect_pads_stop (ffmpegmux->collect);
break;
default:
break;
}
ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
switch (transition) {
case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_tag_setter_reset_tags (GST_TAG_SETTER (ffmpegmux));
if (ffmpegmux->opened) {
ffmpegmux->opened = FALSE;
url_fclose (ffmpegmux->context->pb);
}
break;
case GST_STATE_CHANGE_READY_TO_NULL:
break;
default:
break;
}
return ret;
}
GstCaps *
gst_ffmpegmux_get_id_caps (enum CodecID * id_list)
{
GstCaps *caps, *t;
gint i;
caps = gst_caps_new_empty ();
for (i = 0; id_list[i] != CODEC_ID_NONE; i++) {
if ((t = gst_ffmpeg_codecid_to_caps (id_list[i], NULL, TRUE)))
gst_caps_append (caps, t);
}
if (gst_caps_is_empty (caps)) {
gst_caps_unref (caps);
return NULL;
}
return caps;
}
/* set a list of integer values on the caps, e.g. for sample rates */
static void
gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps, const gchar * field,
guint num, const gint * values)
{
GValue list = { 0, };
GValue val = { 0, };
gint i;
g_return_if_fail (GST_CAPS_IS_SIMPLE (caps));
g_value_init (&list, GST_TYPE_LIST);
g_value_init (&val, G_TYPE_INT);
for (i = 0; i < num; ++i) {
g_value_set_int (&val, values[i]);
gst_value_list_append_value (&list, &val);
}
gst_structure_set_value (gst_caps_get_structure (caps, 0), field, &list);
g_value_unset (&val);
g_value_unset (&list);
}
gboolean
gst_ffmpegmux_register (GstPlugin * plugin)
{
GTypeInfo typeinfo = {
sizeof (GstFFMpegMuxClass),
(GBaseInitFunc) gst_ffmpegmux_base_init,
NULL,
(GClassInitFunc) gst_ffmpegmux_class_init,
NULL,
NULL,
sizeof (GstFFMpegMux),
0,
(GInstanceInitFunc) gst_ffmpegmux_init,
};
static const GInterfaceInfo tag_setter_info = {
NULL, NULL, NULL
};
GType type;
AVOutputFormat *in_plugin;
GstFFMpegMuxClassParams *params;
in_plugin = av_oformat_next (NULL);
GST_LOG ("Registering muxers");
while (in_plugin) {
gchar *type_name;
gchar *p;
GstCaps *srccaps, *audiosinkcaps, *videosinkcaps;
enum CodecID *video_ids = NULL, *audio_ids = NULL;
if ((!strncmp (in_plugin->name, "u16", 3)) ||
(!strncmp (in_plugin->name, "s16", 3)) ||
(!strncmp (in_plugin->name, "u24", 3)) ||
(!strncmp (in_plugin->name, "s24", 3)) ||
(!strncmp (in_plugin->name, "u8", 2)) ||
(!strncmp (in_plugin->name, "s8", 2)) ||
(!strncmp (in_plugin->name, "u32", 3)) ||
(!strncmp (in_plugin->name, "s32", 3)) ||
(!strncmp (in_plugin->name, "f32", 3)) ||
(!strncmp (in_plugin->name, "f64", 3)) ||
(!strncmp (in_plugin->name, "raw", 3)) ||
(!strncmp (in_plugin->name, "crc", 3)) ||
(!strncmp (in_plugin->name, "null", 4)) ||
(!strncmp (in_plugin->name, "gif", 3)) ||
(!strncmp (in_plugin->name, "frame", 5)) ||
(!strncmp (in_plugin->name, "image", 5)) ||
(!strncmp (in_plugin->name, "mulaw", 5)) ||
(!strncmp (in_plugin->name, "alaw", 4)) ||
(!strncmp (in_plugin->name, "h26", 3))
) {
GST_LOG ("Ignoring muxer %s", in_plugin->name);
goto next;
}
/* Try to find the caps that belongs here */
srccaps = gst_ffmpeg_formatid_to_caps (in_plugin->name);
if (!srccaps) {
GST_WARNING ("Couldn't get source caps for muxer %s", in_plugin->name);
goto next;
}
if (!gst_ffmpeg_formatid_get_codecids (in_plugin->name,
&video_ids, &audio_ids, in_plugin)) {
gst_caps_unref (srccaps);
GST_WARNING
("Couldn't get sink caps for muxer %s. Most likely because no input format mapping exists.",
in_plugin->name);
goto next;
}
videosinkcaps = video_ids ? gst_ffmpegmux_get_id_caps (video_ids) : NULL;
audiosinkcaps = audio_ids ? gst_ffmpegmux_get_id_caps (audio_ids) : NULL;
/* construct the type */
type_name = g_strdup_printf ("ffmux_%s", in_plugin->name);
p = type_name;
while (*p) {
if (*p == '.')
*p = '_';
p++;
}
/* if it's already registered, drop it */
if (g_type_from_name (type_name)) {
g_free (type_name);
gst_caps_unref (srccaps);
if (audiosinkcaps)
gst_caps_unref (audiosinkcaps);
if (videosinkcaps)
gst_caps_unref (videosinkcaps);
goto next;
}
/* fix up allowed caps for some muxers */
if (strcmp (in_plugin->name, "flv") == 0) {
const gint rates[] = { 44100, 22050, 11025 };
gst_ffmpeg_mux_simple_caps_set_int_list (audiosinkcaps, "rate", 3, rates);
} else if (strcmp (in_plugin->name, "gif") == 0) {
if (videosinkcaps)
gst_caps_unref (videosinkcaps);
videosinkcaps =
gst_caps_from_string ("video/x-raw-rgb, bpp=(int)24, depth=(int)24");
}
/* create a cache for these properties */
params = g_new0 (GstFFMpegMuxClassParams, 1);
params->in_plugin = in_plugin;
params->srccaps = srccaps;
params->videosinkcaps = videosinkcaps;
params->audiosinkcaps = audiosinkcaps;
/* create the type now */
type = g_type_register_static (GST_TYPE_ELEMENT, type_name, &typeinfo, 0);
g_type_set_qdata (type, GST_FFMUX_PARAMS_QDATA, (gpointer) params);
g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
if (!gst_element_register (plugin, type_name, GST_RANK_NONE, type)) {
g_free (type_name);
gst_caps_unref (srccaps);
if (audiosinkcaps)
gst_caps_unref (audiosinkcaps);
if (videosinkcaps)
gst_caps_unref (videosinkcaps);
return FALSE;
}
g_free (type_name);
next:
in_plugin = av_oformat_next (in_plugin);
}
GST_LOG ("Finished registering muxers");
return TRUE;
}
|