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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
|
/* 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/gstcollectpads2.h>
#include "gstffmpeg.h"
#include "gstffmpegcodecmap.h"
#include "gstffmpegutils.h"
typedef struct _GstFFMpegMux GstFFMpegMux;
typedef struct _GstFFMpegMuxPad GstFFMpegMuxPad;
struct _GstFFMpegMuxPad
{
GstCollectData2 collect; /* we extend the CollectData2 */
gint padnum;
};
struct _GstFFMpegMux
{
GstElement element;
GstCollectPads2 *collect;
/* We need to keep track of our pads, so we do so here. */
GstPad *srcpad;
AVFormatContext *context;
gboolean opened;
guint videopads, audiopads;
/*< private > */
/* event_function is the collectpads default eventfunction */
GstPadEventFunction event_function;
int preload;
int max_delay;
};
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, const GstCaps * caps);
static GstFlowReturn gst_ffmpegmux_collected (GstCollectPads2 * pads,
gpointer user_data);
static gboolean gst_ffmpegmux_sink_event (GstPad * pad, GstObject * parent,
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);
static GstCaps *gst_ffmpegmux_get_id_caps (enum CodecID *id_list);
static void gst_ffmpeg_mux_simple_caps_set_int_list (GstCaps * caps,
const gchar * field, guint num, const gint * values);
#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 }; */
typedef struct
{
const char *name;
const char *replacement;
} GstFFMpegMuxReplacement;
static const char *
gst_ffmpegmux_get_replacement (const char *name)
{
static const GstFFMpegMuxReplacement blacklist[] = {
{"avi", "avimux"},
{"matroska", "matroskamux"},
{"mov", "qtmux"},
{"mpegts", "mpegtsmux"},
{"mp4", "mp4mux"},
{"mpjpeg", "multipartmux"},
{"ogg", "oggmux"},
{"wav", "wavenc"},
{"webm", "webmmux"},
{"mxf", "mxfmux"},
{"3gp", "gppmux"},
{"yuv4mpegpipe", "y4menc"},
{"aiff", "aiffmux"},
{"adts", "aacparse"},
{"asf", "asfmux"},
{"asf_stream", "asfmux"},
{"flv", "flvmux"},
{"mp3", "id3v2mux"},
{"mp2", "id3v2mux"}
};
int i;
for (i = 0; i < sizeof (blacklist) / sizeof (blacklist[0]); i++) {
if (strcmp (blacklist[i].name, name) == 0) {
return blacklist[i].replacement;
}
}
return NULL;
}
static gboolean
gst_ffmpegmux_is_formatter (const char *name)
{
static const char *replace[] = {
"mp2", "mp3", NULL
};
int i;
for (i = 0; replace[i]; i++)
if (strcmp (replace[i], name) == 0)
return TRUE;
return FALSE;
}
static void
gst_ffmpegmux_base_init (gpointer g_class)
{
GstFFMpegMuxClass *klass = (GstFFMpegMuxClass *) g_class;
GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
GstPadTemplate *videosinktempl, *audiosinktempl, *srctempl;
AVOutputFormat *in_plugin;
GstCaps *srccaps, *audiosinkcaps, *videosinkcaps;
enum CodecID *video_ids = NULL, *audio_ids = NULL;
gchar *longname, *description;
const char *replacement;
gboolean is_formatter;
in_plugin =
(AVOutputFormat *) g_type_get_qdata (G_OBJECT_CLASS_TYPE (klass),
GST_FFMUX_PARAMS_QDATA);
g_assert (in_plugin != NULL);
/* construct the element details struct */
replacement = gst_ffmpegmux_get_replacement (in_plugin->name);
is_formatter = gst_ffmpegmux_is_formatter (in_plugin->name);
if (replacement != NULL) {
longname =
g_strdup_printf ("FFmpeg %s %s (not recommended, use %s instead)",
in_plugin->long_name, is_formatter ? "formatter" : "muxer",
replacement);
description =
g_strdup_printf ("FFmpeg %s %s (not recommended, use %s instead)",
in_plugin->long_name, is_formatter ? "formatter" : "muxer",
replacement);
} else {
longname = g_strdup_printf ("FFmpeg %s %s", in_plugin->long_name,
is_formatter ? "formatter" : "muxer");
description = g_strdup_printf ("FFmpeg %s %s", in_plugin->long_name,
is_formatter ? "formatter" : "muxer");
}
gst_element_class_set_details_simple (element_class, longname,
is_formatter ? "Formatter/Metadata" : "Codec/Muxer", description,
"Wim Taymans <wim.taymans@chello.be>, "
"Ronald Bultje <rbultje@ronald.bitfreak.net>");
g_free (longname);
g_free (description);
/* Try to find the caps that belongs here */
srccaps = gst_ffmpeg_formatid_to_caps (in_plugin->name);
if (!srccaps) {
GST_DEBUG ("Couldn't get source caps for muxer '%s', skipping format",
in_plugin->name);
goto beach;
}
if (!gst_ffmpeg_formatid_get_codecids (in_plugin->name,
&video_ids, &audio_ids, in_plugin)) {
gst_caps_unref (srccaps);
GST_DEBUG
("Couldn't get sink caps for muxer '%s'. Most likely because no input format mapping exists.",
in_plugin->name);
goto beach;
}
videosinkcaps = video_ids ? gst_ffmpegmux_get_id_caps (video_ids) : NULL;
audiosinkcaps = audio_ids ? gst_ffmpegmux_get_id_caps (audio_ids) : NULL;
/* fix up allowed caps for some muxers */
/* FIXME : This should be in gstffmpegcodecmap.c ! */
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, format=(string)RGB");
}
/* pad templates */
srctempl = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, srccaps);
gst_element_class_add_pad_template (element_class, srctempl);
if (audiosinkcaps) {
audiosinktempl = gst_pad_template_new ("audio_%u",
GST_PAD_SINK, GST_PAD_REQUEST, audiosinkcaps);
gst_element_class_add_pad_template (element_class, audiosinktempl);
}
if (videosinkcaps) {
videosinktempl = gst_pad_template_new ("video_%u",
GST_PAD_SINK, GST_PAD_REQUEST, videosinkcaps);
gst_element_class_add_pad_template (element_class, videosinktempl);
}
beach:
klass->in_plugin = 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_PARAM_STATIC_STRINGS));
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 | G_PARAM_STATIC_STRINGS));
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_pads2_new ();
gst_collect_pads2_set_function (ffmpegmux->collect,
(GstCollectPads2Function) 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, const GstCaps * caps)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) element;
GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
GstFFMpegMuxPad *collect_pad;
gchar *padname;
GstPad *pad;
AVStream *st;
enum AVMediaType 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_%u")) {
padname = g_strdup_printf ("video_%u", ffmpegmux->videopads++);
type = AVMEDIA_TYPE_VIDEO;
bitrate = 64 * 1024;
framesize = 1152;
} else if (templ == gst_element_class_get_pad_template (klass, "audio_%u")) {
padname = g_strdup_printf ("audio_%u", ffmpegmux->audiopads++);
type = AVMEDIA_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_pads2_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_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, ((GstFFMpegMuxClass *) klass)->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, GstObject * parent, GstEvent * event)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) parent;
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;
}
case GST_EVENT_CAPS:{
GstCaps *caps;
gst_event_parse_caps (event, &caps);
if (!(res = gst_ffmpegmux_setcaps (pad, caps)))
goto beach;
break;
}
default:
break;
}
/* chaining up to collectpads default event function */
res = ffmpegmux->event_function (pad, parent, event);
beach:
return res;
}
static GstFlowReturn
gst_ffmpegmux_collected (GstCollectPads2 * pads, gpointer user_data)
{
GstFFMpegMux *ffmpegmux = (GstFFMpegMux *) user_data;
GSList *collected;
GstFFMpegMuxPad *best_pad;
GstClockTime best_time;
#if 0
/* Re-enable once converted to new AVMetaData API
* See #566605
*/
const GstTagList *tags;
#endif
/* 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 == AVMEDIA_TYPE_VIDEO) ?
"video" : "audio"));
return GST_FLOW_ERROR;
}
/* set framerate for audio */
if (st->codec->codec_type == AVMEDIA_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_pads2_peek (ffmpegmux->collect,
(GstCollectData2 *) collect_pad);
if (buffer) {
st->codec->frame_size =
st->codec->sample_rate *
GST_BUFFER_DURATION (buffer) / GST_SECOND;
gst_buffer_unref (buffer);
}
}
}
}
}
#if 0
/* Re-enable once converted to new AVMetaData API
* See #566605
*/
/* 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;
}
}
#endif
/* 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_pads2_peek (ffmpegmux->collect,
(GstCollectData2 *) 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;
GstMapInfo map;
/* push out current buffer */
buf = gst_collect_pads2_pop (ffmpegmux->collect,
(GstCollectData2 *) 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_buffer_map (buf, &map, GST_MAP_READ);
gst_ffmpeg_avpicture_fill (&src, map.data,
PIX_FMT_RGB24, st->codec->width, st->codec->height);
av_picture_copy (&dst, &src, PIX_FMT_RGB24,
st->codec->width, st->codec->height);
gst_buffer_unmap (buf, &map);
} else {
gst_buffer_map (buf, &map, GST_MAP_READ);
pkt.data = map.data;
pkt.size = map.size;
}
pkt.stream_index = best_pad->padnum;
pkt.flags = 0;
if (!GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
pkt.flags |= AV_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);
if (need_free) {
g_free (pkt.data);
} else {
gst_buffer_unmap (buf, &map);
}
gst_buffer_unref (buf);
} 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_EOS;
}
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_pads2_start (ffmpegmux->collect);
break;
case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
break;
case GST_STATE_CHANGE_PAUSED_TO_READY:
gst_collect_pads2_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;
}
static 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;
in_plugin = av_oformat_next (NULL);
GST_LOG ("Registering muxers");
while (in_plugin) {
gchar *type_name;
gchar *p;
GstRank rank = GST_RANK_MARGINAL;
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)) ||
(!strncmp (in_plugin->name, "rtp", 3)) ||
(!strncmp (in_plugin->name, "ass", 3)) ||
(!strncmp (in_plugin->name, "ffmetadata", 10)) ||
(!strncmp (in_plugin->name, "srt", 3))
) {
GST_LOG ("Ignoring muxer %s", in_plugin->name);
goto next;
}
if ((!strncmp (in_plugin->long_name, "raw ", 4))) {
GST_LOG ("Ignoring raw muxer %s", in_plugin->name);
goto next;
}
if (gst_ffmpegmux_get_replacement (in_plugin->name))
rank = GST_RANK_NONE;
/* FIXME : We need a fast way to know whether we have mappings for this
* muxer type. */
/* construct the type */
type_name = g_strdup_printf ("ffmux_%s", in_plugin->name);
p = type_name;
while (*p) {
if (*p == '.')
*p = '_';
p++;
}
type = g_type_from_name (type_name);
if (!type) {
/* 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) in_plugin);
g_type_add_interface_static (type, GST_TYPE_TAG_SETTER, &tag_setter_info);
}
if (!gst_element_register (plugin, type_name, rank, type)) {
g_free (type_name);
return FALSE;
}
g_free (type_name);
next:
in_plugin = av_oformat_next (in_plugin);
}
GST_LOG ("Finished registering muxers");
return TRUE;
}
|