gstflvmux.c 45.8 KB
Newer Older
1
2
/* GStreamer
 *
3
 * Copyright (c) 2008,2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *
 * 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.
 */

21
22
23
24
25
26
27
28
/**
 * SECTION:element-flvmux
 *
 * flvmux muxes different streams into an FLV file.
 *
 * <refsect2>
 * <title>Example launch line</title>
 * |[
29
 * gst-launch -v filesrc location=/path/to/audio ! decodebin2 ! queue ! flvmux name=m ! filesink location=file.flv   filesrc location=/path/to/video ! decodebin2 ! queue ! m.
30
31
32
33
 * ]| This pipeline muxes an audio and video file into a single FLV file.
 * </refsect2>
 */

34
35
36
37
38
39
40
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <math.h>
#include <string.h>

Wim Taymans's avatar
Wim Taymans committed
41
42
#include <gst/audio/audio.h>

43
#include "gstflvmux.h"
44
#include "amfdefs.h"
45
46
47
48

GST_DEBUG_CATEGORY_STATIC (flvmux_debug);
#define GST_CAT_DEFAULT flvmux_debug

Jan Urbański's avatar
Jan Urbański committed
49
50
enum
{
51
  PROP_0,
52
  PROP_STREAMABLE
Jan Urbański's avatar
Jan Urbański committed
53
54
};

55
#define DEFAULT_STREAMABLE FALSE
56
#define MAX_INDEX_ENTRIES 128
Jan Urbański's avatar
Jan Urbański committed
57

58
59
60
61
62
63
64
65
66
67
68
static GstStaticPadTemplate src_templ = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("video/x-flv")
    );

static GstStaticPadTemplate videosink_templ = GST_STATIC_PAD_TEMPLATE ("video",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS ("video/x-flash-video; "
        "video/x-flash-screen; "
69
70
        "video/x-vp6-flash; " "video/x-vp6-alpha; "
        "video/x-h264, stream-format=avc;")
71
72
73
74
75
76
77
    );

static GstStaticPadTemplate audiosink_templ = GST_STATIC_PAD_TEMPLATE ("audio",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
    GST_STATIC_CAPS
    ("audio/x-adpcm, layout = (string) swf, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
78
        "audio/mpeg, mpegversion = (int) 1, layer = (int) 3, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 22050, 44100 }, parsed = (boolean) TRUE; "
79
80
        "audio/mpeg, mpegversion = (int) 2, framed = (boolean) TRUE; "
        "audio/mpeg, mpegversion = (int) 4, stream-format = (string) raw, framed = (boolean) TRUE; "
81
        "audio/x-nellymoser, channels = (int) { 1, 2 }, rate = (int) { 5512, 8000, 11025, 16000, 22050, 44100 }; "
Wim Taymans's avatar
Wim Taymans committed
82
        "audio/x-raw, format = (string) { U8, S16LE}, layout = (string) interleaved, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
83
        "audio/x-alaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
84
85
        "audio/x-mulaw, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 }; "
        "audio/x-speex, channels = (int) { 1, 2 }, rate = (int) { 5512, 11025, 22050, 44100 };")
86
87
    );

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
88
89
90
#define gst_flv_mux_parent_class parent_class
G_DEFINE_TYPE_WITH_CODE (GstFlvMux, gst_flv_mux, GST_TYPE_ELEMENT,
    G_IMPLEMENT_INTERFACE (GST_TYPE_TAG_SETTER, NULL));
91
92
93

static void gst_flv_mux_finalize (GObject * object);
static GstFlowReturn
94
95
gst_flv_mux_handle_buffer (GstCollectPads2 * pads, GstCollectData2 * cdata,
    GstBuffer * buf, gpointer user_data);
96
97
98
static gboolean
gst_flv_mux_handle_sink_event (GstCollectPads2 * pads, GstCollectData2 * data,
    GstEvent * event, gpointer user_data);
99

Wim Taymans's avatar
Wim Taymans committed
100
101
static gboolean gst_flv_mux_handle_src_event (GstPad * pad, GstObject * parent,
    GstEvent * event);
102
static GstPad *gst_flv_mux_request_new_pad (GstElement * element,
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
103
    GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps);
104
105
static void gst_flv_mux_release_pad (GstElement * element, GstPad * pad);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
106
107
108
static gboolean gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps);
static gboolean gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps);

Jan Urbański's avatar
Jan Urbański committed
109
110
111
112
113
static void gst_flv_mux_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
static void gst_flv_mux_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);

114
115
116
117
static GstStateChangeReturn
gst_flv_mux_change_state (GstElement * element, GstStateChange transition);

static void gst_flv_mux_reset (GstElement * element);
118
119
static void gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * pad,
    gboolean video);
120

121
122
123
124
125
126
127
128
129
130
131
132
typedef struct
{
  gdouble position;
  gdouble time;
} GstFlvMuxIndexEntry;

static void
gst_flv_mux_index_entry_free (GstFlvMuxIndexEntry * entry)
{
  g_slice_free (GstFlvMuxIndexEntry, entry);
}

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
133
134
static GstBuffer *
_gst_buffer_new_wrapped (gpointer mem, gsize size, GFreeFunc free_func)
135
{
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
136
  GstBuffer *buf;
137

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
138
139
140
  buf = gst_buffer_new ();
  gst_buffer_take_memory (buf, -1,
      gst_memory_new_wrapped (free_func ? 0 : GST_MEMORY_FLAG_READONLY,
Wim Taymans's avatar
Wim Taymans committed
141
          mem, size, 0, size, mem, free_func));
142

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
143
144
145
146
147
148
149
150
151
152
153
  return buf;
}

static void
_gst_buffer_new_and_alloc (gsize size, GstBuffer ** buffer, guint8 ** data)
{
  g_return_if_fail (data != NULL);
  g_return_if_fail (buffer != NULL);

  *data = g_malloc (size);
  *buffer = _gst_buffer_new_wrapped (*data, size, g_free);
154
155
156
157
158
159
160
161
162
163
164
165
166
}

static void
gst_flv_mux_class_init (GstFlvMuxClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

  GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");

  gobject_class = (GObjectClass *) klass;
  gstelement_class = (GstElementClass *) klass;

Jan Urbański's avatar
Jan Urbański committed
167
168
  gobject_class->get_property = gst_flv_mux_get_property;
  gobject_class->set_property = gst_flv_mux_set_property;
169
170
  gobject_class->finalize = gst_flv_mux_finalize;

171
172
  /* FIXME: ideally the right mode of operation should be detected
   * automatically using queries when parameter not specified. */
173
174
175
176
177
178
179
180
  /**
   * GstFlvMux:streamable
   *
   * If True, the output will be streaming friendly. (ie without indexes and
   * duration)
   *
   * Since: 0.10.24
   **/
181
182
183
184
185
  g_object_class_install_property (gobject_class, PROP_STREAMABLE,
      g_param_spec_boolean ("streamable", "streamable",
          "If set to true, the output should be as if it is to be streamed "
          "and hence no indexes written or duration written.",
          DEFAULT_STREAMABLE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
Jan Urbański's avatar
Jan Urbański committed
186

187
188
189
190
  gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_flv_mux_change_state);
  gstelement_class->request_new_pad =
      GST_DEBUG_FUNCPTR (gst_flv_mux_request_new_pad);
  gstelement_class->release_pad = GST_DEBUG_FUNCPTR (gst_flv_mux_release_pad);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
191
192
193
194
195
196
197
198
199
200
201
202
203

  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&videosink_templ));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&audiosink_templ));
  gst_element_class_add_pad_template (gstelement_class,
      gst_static_pad_template_get (&src_templ));
  gst_element_class_set_details_simple (gstelement_class, "FLV muxer",
      "Codec/Muxer",
      "Muxes video/audio streams into a FLV stream",
      "Sebastian Dröge <sebastian.droege@collabora.co.uk>");

  GST_DEBUG_CATEGORY_INIT (flvmux_debug, "flvmux", 0, "FLV muxer");
204
205
206
}

static void
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
207
gst_flv_mux_init (GstFlvMux * mux)
208
209
210
211
212
{
  mux->srcpad = gst_pad_new_from_static_template (&src_templ, "src");
  gst_pad_set_event_function (mux->srcpad, gst_flv_mux_handle_src_event);
  gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);

Jan Urbański's avatar
Jan Urbański committed
213
  /* property */
214
  mux->streamable = DEFAULT_STREAMABLE;
Jan Urbański's avatar
Jan Urbański committed
215

216
217
  mux->new_tags = FALSE;

218
  mux->collect = gst_collect_pads2_new ();
219
220
  gst_collect_pads2_set_buffer_function (mux->collect,
      GST_DEBUG_FUNCPTR (gst_flv_mux_handle_buffer), mux);
221
222
  gst_collect_pads2_set_event_function (mux->collect,
      GST_DEBUG_FUNCPTR (gst_flv_mux_handle_sink_event), mux);
223
224
  gst_collect_pads2_set_clip_function (mux->collect,
      GST_DEBUG_FUNCPTR (gst_collect_pads2_clip_running_time), mux);
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244

  gst_flv_mux_reset (GST_ELEMENT (mux));
}

static void
gst_flv_mux_finalize (GObject * object)
{
  GstFlvMux *mux = GST_FLV_MUX (object);

  gst_object_unref (mux->collect);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gst_flv_mux_reset (GstElement * element)
{
  GstFlvMux *mux = GST_FLV_MUX (element);
  GSList *sl;

245
  for (sl = mux->collect->data; sl != NULL; sl = g_slist_next (sl)) {
246
247
    GstFlvPad *cpad = (GstFlvPad *) sl->data;

248
    gst_flv_mux_reset_pad (mux, cpad, cpad->video);
249
250
  }

251
252
253
254
255
  g_list_foreach (mux->index, (GFunc) gst_flv_mux_index_entry_free, NULL);
  g_list_free (mux->index);
  mux->index = NULL;
  mux->byte_count = 0;

256
  mux->have_audio = mux->have_video = FALSE;
257
  mux->duration = GST_CLOCK_TIME_NONE;
258
  mux->new_tags = FALSE;
259

260
  mux->state = GST_FLV_MUX_STATE_HEADER;
261
262
263

  /* tags */
  gst_tag_setter_reset_tags (GST_TAG_SETTER (mux));
264
265
266
}

static gboolean
Wim Taymans's avatar
Wim Taymans committed
267
268
gst_flv_mux_handle_src_event (GstPad * pad, GstObject * parent,
    GstEvent * event)
269
270
271
272
273
274
275
276
277
278
279
280
281
{
  GstEventType type;

  type = event ? GST_EVENT_TYPE (event) : GST_EVENT_UNKNOWN;

  switch (type) {
    case GST_EVENT_SEEK:
      /* disable seeking for now */
      return FALSE;
    default:
      break;
  }

Wim Taymans's avatar
Wim Taymans committed
282
  return gst_pad_event_default (pad, parent, event);
283
284
285
}

static gboolean
286
287
gst_flv_mux_handle_sink_event (GstCollectPads2 * pads, GstCollectData2 * data,
    GstEvent * event, gpointer user_data)
288
{
289
  GstFlvMux *mux = GST_FLV_MUX (user_data);
290
  gboolean ret = FALSE;
291
292

  switch (GST_EVENT_TYPE (event)) {
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
293
294
295
296
297
298
299
300
    case GST_EVENT_CAPS:
    {
      GstCaps *caps;
      GstFlvPad *flvpad;

      gst_event_parse_caps (event, &caps);

      /* find stream data */
301
      flvpad = (GstFlvPad *) data;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
302
303
304
      g_assert (flvpad);

      if (flvpad->video) {
305
        ret = gst_flv_mux_video_pad_setcaps (data->pad, caps);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
306
      } else {
307
        ret = gst_flv_mux_audio_pad_setcaps (data->pad, caps);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
308
309
310
311
312
      }
      /* and eat */
      gst_event_unref (event);
      break;
    }
313
    case GST_EVENT_TAG:{
314
315
      GstTagList *list;
      GstTagSetter *setter = GST_TAG_SETTER (mux);
316
      const GstTagMergeMode mode = gst_tag_setter_get_tag_merge_mode (setter);
317

318
319
      gst_event_parse_tag (event, &list);
      gst_tag_setter_merge_tags (setter, list, mode);
320
      mux->new_tags = TRUE;
321
322
      ret = TRUE;
      gst_event_unref (event);
323
      break;
324
    }
325
326
327
328
329
    case GST_EVENT_EOS:
    case GST_EVENT_SEGMENT:
      gst_event_unref (event);
      ret = TRUE;
      break;
330
    default:
331
      ret = gst_pad_event_default (data->pad, GST_OBJECT (mux), event);
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
      break;
  }

  return ret;
}

static gboolean
gst_flv_mux_video_pad_setcaps (GstPad * pad, GstCaps * caps)
{
  GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
  GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
  gboolean ret = TRUE;
  GstStructure *s;

  s = gst_caps_get_structure (caps, 0);

  if (strcmp (gst_structure_get_name (s), "video/x-flash-video") == 0) {
    cpad->video_codec = 2;
  } else if (strcmp (gst_structure_get_name (s), "video/x-flash-screen") == 0) {
    cpad->video_codec = 3;
  } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-flash") == 0) {
    cpad->video_codec = 4;
  } else if (strcmp (gst_structure_get_name (s), "video/x-vp6-alpha") == 0) {
    cpad->video_codec = 5;
  } else if (strcmp (gst_structure_get_name (s), "video/x-h264") == 0) {
    cpad->video_codec = 7;
  } else {
    ret = FALSE;
  }

  if (ret && gst_structure_has_field (s, "codec_data")) {
    const GValue *val = gst_structure_get_value (s, "codec_data");

365
    if (val)
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
      cpad->video_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
  }

  gst_object_unref (mux);

  return ret;
}

static gboolean
gst_flv_mux_audio_pad_setcaps (GstPad * pad, GstCaps * caps)
{
  GstFlvMux *mux = GST_FLV_MUX (gst_pad_get_parent (pad));
  GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);
  gboolean ret = TRUE;
  GstStructure *s;

  s = gst_caps_get_structure (caps, 0);

  if (strcmp (gst_structure_get_name (s), "audio/x-adpcm") == 0) {
    const gchar *layout = gst_structure_get_string (s, "layout");
    if (layout && strcmp (layout, "swf") == 0) {
      cpad->audio_codec = 1;
    } else {
      ret = FALSE;
    }
  } else if (strcmp (gst_structure_get_name (s), "audio/mpeg") == 0) {
    gint mpegversion;

    if (gst_structure_get_int (s, "mpegversion", &mpegversion)) {
      if (mpegversion == 1) {
        gint layer;

        if (gst_structure_get_int (s, "layer", &layer) && layer == 3) {
          gint rate;

          if (gst_structure_get_int (s, "rate", &rate) && rate == 8000)
            cpad->audio_codec = 14;
          else
            cpad->audio_codec = 2;
        } else {
          ret = FALSE;
        }
408
      } else if (mpegversion == 4 || mpegversion == 2) {
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
        cpad->audio_codec = 10;
      } else {
        ret = FALSE;
      }
    } else {
      ret = FALSE;
    }
  } else if (strcmp (gst_structure_get_name (s), "audio/x-nellymoser") == 0) {
    gint rate, channels;

    if (gst_structure_get_int (s, "rate", &rate)
        && gst_structure_get_int (s, "channels", &channels)) {
      if (channels == 1 && rate == 16000)
        cpad->audio_codec = 4;
      else if (channels == 1 && rate == 8000)
        cpad->audio_codec = 5;
425
426
      else
        cpad->audio_codec = 6;
427
428
429
    } else {
      cpad->audio_codec = 6;
    }
Wim Taymans's avatar
Wim Taymans committed
430
431
  } else if (strcmp (gst_structure_get_name (s), "audio/x-raw") == 0) {
    GstAudioInfo info;
432

Wim Taymans's avatar
Wim Taymans committed
433
    if (gst_audio_info_from_caps (&info, caps)) {
434
      cpad->audio_codec = 3;
Wim Taymans's avatar
Wim Taymans committed
435
436
437
438
439
440
441
442

      if (GST_AUDIO_INFO_WIDTH (&info) == 8)
        cpad->width = 0;
      else if (GST_AUDIO_INFO_WIDTH (&info) == 16)
        cpad->width = 1;
      else
        ret = FALSE;
    } else
443
444
      ret = FALSE;
  } else if (strcmp (gst_structure_get_name (s), "audio/x-alaw") == 0) {
445
    cpad->audio_codec = 7;
446
  } else if (strcmp (gst_structure_get_name (s), "audio/x-mulaw") == 0) {
447
    cpad->audio_codec = 8;
448
449
  } else if (strcmp (gst_structure_get_name (s), "audio/x-speex") == 0) {
    cpad->audio_codec = 11;
450
451
452
453
454
  } else {
    ret = FALSE;
  }

  if (ret) {
Wim Taymans's avatar
Wim Taymans committed
455
    gint rate, channels;
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

    if (gst_structure_get_int (s, "rate", &rate)) {
      if (cpad->audio_codec == 10)
        cpad->rate = 3;
      else if (rate == 5512)
        cpad->rate = 0;
      else if (rate == 11025)
        cpad->rate = 1;
      else if (rate == 22050)
        cpad->rate = 2;
      else if (rate == 44100)
        cpad->rate = 3;
      else if (rate == 8000 && (cpad->audio_codec == 5
              || cpad->audio_codec == 14))
        cpad->rate = 0;
      else if (rate == 16000 && cpad->audio_codec == 4)
        cpad->rate = 0;
      else
        ret = FALSE;
    } else if (cpad->audio_codec == 10) {
      cpad->rate = 3;
    } else {
      ret = FALSE;
    }

    if (gst_structure_get_int (s, "channels", &channels)) {
      if (cpad->audio_codec == 4 || cpad->audio_codec == 5
          || cpad->audio_codec == 6)
        cpad->channels = 0;
      else if (cpad->audio_codec == 10)
        cpad->channels = 1;
      else if (channels == 1)
        cpad->channels = 0;
      else if (channels == 2)
        cpad->channels = 1;
      else
        ret = FALSE;
    } else if (cpad->audio_codec == 4 || cpad->audio_codec == 5
        || cpad->audio_codec == 6) {
      cpad->channels = 0;
    } else if (cpad->audio_codec == 10) {
      cpad->channels = 1;
    } else {
      ret = FALSE;
    }

Wim Taymans's avatar
Wim Taymans committed
502
    if (cpad->audio_codec != 3)
503
504
505
506
507
508
      cpad->width = 1;
  }

  if (ret && gst_structure_has_field (s, "codec_data")) {
    const GValue *val = gst_structure_get_value (s, "codec_data");

509
    if (val)
510
511
512
513
514
515
516
517
      cpad->audio_codec_data = gst_buffer_ref (gst_value_get_buffer (val));
  }

  gst_object_unref (mux);

  return ret;
}

518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
static void
gst_flv_mux_reset_pad (GstFlvMux * mux, GstFlvPad * cpad, gboolean video)
{
  cpad->video = video;

  if (cpad->audio_codec_data)
    gst_buffer_unref (cpad->audio_codec_data);
  cpad->audio_codec_data = NULL;
  cpad->audio_codec = G_MAXUINT;
  cpad->rate = G_MAXUINT;
  cpad->width = G_MAXUINT;
  cpad->channels = G_MAXUINT;

  if (cpad->video_codec_data)
    gst_buffer_unref (cpad->video_codec_data);
  cpad->video_codec_data = NULL;
  cpad->video_codec = G_MAXUINT;
  cpad->last_timestamp = 0;
}

538
539
static GstPad *
gst_flv_mux_request_new_pad (GstElement * element,
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
540
    GstPadTemplate * templ, const gchar * req_name, const GstCaps * caps)
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
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (element);
  GstFlvMux *mux = GST_FLV_MUX (element);
  GstFlvPad *cpad;
  GstPad *pad = NULL;
  const gchar *name = NULL;
  gboolean video;

  if (mux->state != GST_FLV_MUX_STATE_HEADER) {
    GST_WARNING_OBJECT (mux, "Can't request pads after writing header");
    return NULL;
  }

  if (templ == gst_element_class_get_pad_template (klass, "audio")) {
    if (mux->have_audio) {
      GST_WARNING_OBJECT (mux, "Already have an audio pad");
      return NULL;
    }
    mux->have_audio = TRUE;
    name = "audio";
    video = FALSE;
  } else if (templ == gst_element_class_get_pad_template (klass, "video")) {
    if (mux->have_video) {
      GST_WARNING_OBJECT (mux, "Already have a video pad");
      return NULL;
    }
    mux->have_video = TRUE;
    name = "video";
    video = TRUE;
  } else {
    GST_WARNING_OBJECT (mux, "Invalid template");
    return NULL;
  }

  pad = gst_pad_new_from_template (templ, name);
  cpad = (GstFlvPad *)
577
      gst_collect_pads2_add_pad (mux->collect, pad, sizeof (GstFlvPad));
578
579
580

  cpad->audio_codec_data = NULL;
  cpad->video_codec_data = NULL;
581
  gst_flv_mux_reset_pad (mux, cpad, video);
582

583
584
585
586
587
588
589
590
591
592
593
594
  gst_pad_set_active (pad, TRUE);
  gst_element_add_pad (element, pad);

  return pad;
}

static void
gst_flv_mux_release_pad (GstElement * element, GstPad * pad)
{
  GstFlvMux *mux = GST_FLV_MUX (GST_PAD_PARENT (pad));
  GstFlvPad *cpad = (GstFlvPad *) gst_pad_get_element_private (pad);

595
  gst_flv_mux_reset_pad (mux, cpad, cpad->video);
596
  gst_collect_pads2_remove_pad (mux->collect, pad);
597
598
599
  gst_element_remove_pad (element, pad);
}

600
601
602
static GstFlowReturn
gst_flv_mux_push (GstFlvMux * mux, GstBuffer * buffer)
{
603
604
  /* pushing the buffer that rewrites the header will make it no longer be the
   * total output size in bytes, but it doesn't matter at that point */
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
605
  mux->byte_count += gst_buffer_get_size (buffer);
606

607
608
609
  return gst_pad_push (mux->srcpad, buffer);
}

Jan Urbański's avatar
Jan Urbański committed
610
611
612
613
614
615
static GstBuffer *
gst_flv_mux_create_header (GstFlvMux * mux)
{
  GstBuffer *header;
  guint8 *data;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
616
  _gst_buffer_new_and_alloc (9 + 4, &header, &data);
Jan Urbański's avatar
Jan Urbański committed
617
618
619
620
621
622
623
624
625
626
627
628
629

  data[0] = 'F';
  data[1] = 'L';
  data[2] = 'V';
  data[3] = 0x01;               /* Version */

  data[4] = (mux->have_audio << 2) | mux->have_video;   /* flags */
  GST_WRITE_UINT32_BE (data + 5, 9);    /* data offset */
  GST_WRITE_UINT32_BE (data + 9, 0);    /* previous tag size */

  return header;
}

630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
static GstBuffer *
gst_flv_mux_preallocate_index (GstFlvMux * mux)
{
  GstBuffer *tmp;
  guint8 *data;
  gint preallocate_size;

  /* preallocate index of size:
   *  - 'keyframes' ECMA array key: 2 + 9 = 11 bytes
   *  - nested ECMA array header, length and end marker: 8 bytes
   *  - 'times' and 'filepositions' keys: 22 bytes
   *  - two strict arrays headers and lengths: 10 bytes
   *  - each index entry: 18 bytes
   */
  preallocate_size = 11 + 8 + 22 + 10 + MAX_INDEX_ENTRIES * 18;
  GST_DEBUG_OBJECT (mux, "preallocating %d bytes for the index",
      preallocate_size);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
648
  _gst_buffer_new_and_alloc (preallocate_size, &tmp, &data);
649
650
651
652

  /* prefill the space with a gstfiller: <spaces> script tag variable */
  GST_WRITE_UINT16_BE (data, 9);        /* 9 characters */
  memcpy (data + 2, "gstfiller", 9);
653
  GST_WRITE_UINT8 (data + 11, AMF0_STRING_MARKER);      /* a string value */
654
655
656
657
658
  GST_WRITE_UINT16_BE (data + 12, preallocate_size - 14);
  memset (data + 14, ' ', preallocate_size - 14);       /* the rest is spaces */
  return tmp;
}

659
660
661
static GstBuffer *
gst_flv_mux_create_number_script_value (const gchar * name, gdouble value)
{
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
662
663
  GstBuffer *tmp;
  guint8 *data;
664
  gsize len = strlen (name);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
665

Wim Taymans's avatar
Wim Taymans committed
666
  _gst_buffer_new_and_alloc (2 + len + 1 + 8, &tmp, &data);
667

668
669
670
671
672
673
  GST_WRITE_UINT16_BE (data, len);
  data += 2;                    /* name length */
  memcpy (data, name, len);
  data += len;
  *data++ = AMF0_NUMBER_MARKER; /* double type */
  GST_WRITE_DOUBLE_BE (data, value);
674
675
676
677

  return tmp;
}

Jan Urbański's avatar
Jan Urbański committed
678
static GstBuffer *
679
gst_flv_mux_create_metadata (GstFlvMux * mux, gboolean full)
680
{
681
  const GstTagList *tags;
682
  GstBuffer *script_tag, *tmp;
Wim Taymans's avatar
Wim Taymans committed
683
  GstMapInfo map;
684
685
686
  guint8 *data;
  gint i, n_tags, tags_written = 0;

687
  tags = gst_tag_setter_get_tag_list (GST_TAG_SETTER (mux));
688

689
  GST_DEBUG_OBJECT (mux, "tags = %" GST_PTR_FORMAT, tags);
690

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
691
692
693
  /* FIXME perhaps some bytewriter'ing here ... */

  _gst_buffer_new_and_alloc (11, &script_tag, &data);
694
695
696
697
698
699
700
701
702
703
704
705
706
707

  data[0] = 18;

  /* Data size, unknown for now */
  data[1] = 0;
  data[2] = 0;
  data[3] = 0;

  /* Timestamp */
  data[4] = data[5] = data[6] = data[7] = 0;

  /* Stream ID */
  data[8] = data[9] = data[10] = 0;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
708
  _gst_buffer_new_and_alloc (13, &tmp, &data);
709
  data[0] = AMF0_STRING_MARKER; /* string */
710
  data[1] = 0;
711
712
  data[2] = 10;                 /* length 10 */
  memcpy (&data[3], "onMetaData", 10);
713
714
715

  script_tag = gst_buffer_join (script_tag, tmp);

716
  n_tags = (tags) ? gst_structure_n_fields ((GstStructure *) tags) : 0;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
717
  _gst_buffer_new_and_alloc (5, &tmp, &data);
718
719
720
721
  data[0] = 8;                  /* ECMA array */
  GST_WRITE_UINT32_BE (data + 1, n_tags);
  script_tag = gst_buffer_join (script_tag, tmp);

722
723
724
  if (!full)
    goto tags;

725
726
  /* Some players expect the 'duration' to be always set. Fill it out later,
     after querying the pads or after getting EOS */
727
728
729
730
  if (!mux->streamable) {
    tmp = gst_flv_mux_create_number_script_value ("duration", 86400);
    script_tag = gst_buffer_join (script_tag, tmp);
    tags_written++;
731

732
733
    /* Sometimes the information about the total file size is useful for the
       player. It will be filled later, after getting EOS */
734
735
736
737
    tmp = gst_flv_mux_create_number_script_value ("filesize", 0);
    script_tag = gst_buffer_join (script_tag, tmp);
    tags_written++;

738
    /* Preallocate space for the index to be written at EOS */
739
740
741
    tmp = gst_flv_mux_preallocate_index (mux);
    script_tag = gst_buffer_join (script_tag, tmp);
  } else {
742
    GST_DEBUG_OBJECT (mux, "not preallocating index, streamable mode");
743
744
  }

745
tags:
746
  for (i = 0; tags && i < n_tags; i++) {
747
    const gchar *tag_name =
748
        gst_structure_nth_field_name ((const GstStructure *) tags, i);
749
750
751
    if (!strcmp (tag_name, GST_TAG_DURATION)) {
      guint64 dur;

752
      if (!gst_tag_list_get_uint64 (tags, GST_TAG_DURATION, &dur))
753
        continue;
754
      mux->duration = dur;
755
756
757
    } else if (!strcmp (tag_name, GST_TAG_ARTIST) ||
        !strcmp (tag_name, GST_TAG_TITLE)) {
      gchar *s;
James Andrewartha's avatar
James Andrewartha committed
758
      const gchar *t = NULL;
759
760
761
762
763
764

      if (!strcmp (tag_name, GST_TAG_ARTIST))
        t = "creator";
      else if (!strcmp (tag_name, GST_TAG_TITLE))
        t = "title";

765
      if (!gst_tag_list_get_string (tags, tag_name, &s))
766
767
        continue;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
768
769
      _gst_buffer_new_and_alloc (2 + strlen (t) + 1 + 2 + strlen (s),
          &tmp, &data);
770
771
772
773
774
775
776
777
778
779
780
781
782
783
      data[0] = 0;              /* tag name length */
      data[1] = strlen (t);
      memcpy (&data[2], t, strlen (t));
      data[2 + strlen (t)] = 2; /* string */
      data[3 + strlen (t)] = (strlen (s) >> 8) & 0xff;
      data[4 + strlen (t)] = (strlen (s)) & 0xff;
      memcpy (&data[5 + strlen (t)], s, strlen (s));
      script_tag = gst_buffer_join (script_tag, tmp);

      g_free (s);
      tags_written++;
    }
  }

784
785
786
  if (!full)
    goto end;

787
  if (mux->duration == GST_CLOCK_TIME_NONE) {
788
789
790
791
    GSList *l;
    guint64 dur;

    for (l = mux->collect->data; l; l = l->next) {
792
      GstCollectData2 *cdata = l->data;
793

794
      if (gst_pad_peer_query_duration (cdata->pad, GST_FORMAT_TIME,
795
              (gint64 *) & dur) && dur != GST_CLOCK_TIME_NONE) {
796
797
        if (mux->duration == GST_CLOCK_TIME_NONE)
          mux->duration = dur;
798
        else
799
          mux->duration = MAX (dur, mux->duration);
800
801
802
803
      }
    }
  }

804
  if (!mux->streamable && mux->duration != GST_CLOCK_TIME_NONE) {
805
    gdouble d;
Wim Taymans's avatar
Wim Taymans committed
806
807
    GstMapInfo map;

808
    d = gst_guint64_to_gdouble (mux->duration);
809
810
    d /= (gdouble) GST_SECOND;

811
    GST_DEBUG_OBJECT (mux, "determined the duration to be %f", d);
Wim Taymans's avatar
Wim Taymans committed
812
813
814
    gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
    GST_WRITE_DOUBLE_BE (map.data + 29 + 2 + 8 + 1, d);
    gst_buffer_unmap (script_tag, &map);
815
816
  }

817
  if (mux->have_video) {
818
    GstPad *video_pad = NULL;
819
    GstFlvPad *cpad;
820
821
822
    GSList *l = mux->collect->data;

    for (; l; l = l->next) {
823
      cpad = l->data;
824
825
826
827
828
829
      if (cpad && cpad->video) {
        video_pad = cpad->collect.pad;
        break;
      }
    }

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
830
831
832
    if (video_pad && gst_pad_has_current_caps (video_pad)) {
      GstCaps *caps;
      GstStructure *s;
833
      gint size;
834
835
836
837
838
839
840
841
842
      gint num, den;

      GST_DEBUG_OBJECT (mux, "putting videocodecid %d in the metadata",
          cpad->video_codec);

      tmp = gst_flv_mux_create_number_script_value ("videocodecid",
          cpad->video_codec);
      script_tag = gst_buffer_join (script_tag, tmp);
      tags_written++;
843

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
844
845
846
847
      caps = gst_pad_get_current_caps (video_pad);
      s = gst_caps_get_structure (caps, 0);
      gst_caps_unref (caps);

848
849
850
      if (gst_structure_get_int (s, "width", &size)) {
        GST_DEBUG_OBJECT (mux, "putting width %d in the metadata", size);

851
        tmp = gst_flv_mux_create_number_script_value ("width", size);
852
853
854
855
856
857
858
        script_tag = gst_buffer_join (script_tag, tmp);
        tags_written++;
      }

      if (gst_structure_get_int (s, "height", &size)) {
        GST_DEBUG_OBJECT (mux, "putting height %d in the metadata", size);

859
        tmp = gst_flv_mux_create_number_script_value ("height", size);
860
861
862
863
        script_tag = gst_buffer_join (script_tag, tmp);
        tags_written++;
      }

864
      if (gst_structure_get_fraction (s, "pixel-aspect-ratio", &num, &den)) {
865
866
        gdouble d;

867
        d = num;
868
869
        GST_DEBUG_OBJECT (mux, "putting AspectRatioX %f in the metadata", d);

870
        tmp = gst_flv_mux_create_number_script_value ("AspectRatioX", d);
871
872
873
        script_tag = gst_buffer_join (script_tag, tmp);
        tags_written++;

874
        d = den;
875
876
        GST_DEBUG_OBJECT (mux, "putting AspectRatioY %f in the metadata", d);

877
878
879
880
881
882
883
884
885
886
887
888
        tmp = gst_flv_mux_create_number_script_value ("AspectRatioY", d);
        script_tag = gst_buffer_join (script_tag, tmp);
        tags_written++;
      }

      if (gst_structure_get_fraction (s, "framerate", &num, &den)) {
        gdouble d;

        gst_util_fraction_to_double (num, den, &d);
        GST_DEBUG_OBJECT (mux, "putting framerate %f in the metadata", d);

        tmp = gst_flv_mux_create_number_script_value ("framerate", d);
889
890
891
892
893
894
        script_tag = gst_buffer_join (script_tag, tmp);
        tags_written++;
      }
    }
  }

895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
  if (mux->have_audio) {
    GstPad *audio_pad = NULL;
    GstFlvPad *cpad;
    GSList *l = mux->collect->data;

    for (; l; l = l->next) {
      cpad = l->data;
      if (cpad && !cpad->video) {
        audio_pad = cpad->collect.pad;
        break;
      }
    }

    if (audio_pad) {
      GST_DEBUG_OBJECT (mux, "putting audiocodecid %d in the metadata",
          cpad->audio_codec);

      tmp = gst_flv_mux_create_number_script_value ("audiocodecid",
          cpad->audio_codec);
      script_tag = gst_buffer_join (script_tag, tmp);
      tags_written++;
    }
  }

919
920
921
  {
    const gchar *s = "GStreamer FLV muxer";

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
922
    _gst_buffer_new_and_alloc (2 + 15 + 1 + 2 + strlen (s), &tmp, &data);
923
924
    data[0] = 0;                /* 15 bytes name */
    data[1] = 15;
925
    memcpy (&data[2], "metadatacreator", 15);
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
    data[17] = 2;               /* string */
    data[18] = (strlen (s) >> 8) & 0xff;
    data[19] = (strlen (s)) & 0xff;
    memcpy (&data[20], s, strlen (s));
    script_tag = gst_buffer_join (script_tag, tmp);

    tags_written++;
  }

  {
    GTimeVal tv = { 0, };
    time_t secs;
    struct tm *tm;
    gchar *s;
    static const gchar *weekdays[] = {
      "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
    };
    static const gchar *months[] = {
      "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
      "Aug", "Sep", "Oct", "Nov", "Dec"
    };

    g_get_current_time (&tv);
    secs = tv.tv_sec;
    tm = gmtime (&secs);

    s = g_strdup_printf ("%s %s %d %d:%d:%d %d", weekdays[tm->tm_wday],
        months[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec,
        tm->tm_year + 1900);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
956
    _gst_buffer_new_and_alloc (2 + 12 + 1 + 2 + strlen (s), &tmp, &data);
957
958
    data[0] = 0;                /* 12 bytes name */
    data[1] = 12;
959
    memcpy (&data[2], "creationdate", 12);
960
961
962
963
964
965
966
967
968
969
    data[14] = 2;               /* string */
    data[15] = (strlen (s) >> 8) & 0xff;
    data[16] = (strlen (s)) & 0xff;
    memcpy (&data[17], s, strlen (s));
    script_tag = gst_buffer_join (script_tag, tmp);

    g_free (s);
    tags_written++;
  }

970
971
972
973
end:

  if (!tags_written) {
    gst_buffer_unref (script_tag);
974
    script_tag = NULL;
975
976
977
    goto exit;
  }

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
978
  _gst_buffer_new_and_alloc (2 + 0 + 1, &tmp, &data);
979
980
981
982
983
984
  data[0] = 0;                  /* 0 byte size */
  data[1] = 0;
  data[2] = 9;                  /* end marker */
  script_tag = gst_buffer_join (script_tag, tmp);
  tags_written++;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
985
986
  _gst_buffer_new_and_alloc (4, &tmp, &data);
  GST_WRITE_UINT32_BE (data, gst_buffer_get_size (script_tag));
987
988
  script_tag = gst_buffer_join (script_tag, tmp);

Wim Taymans's avatar
Wim Taymans committed
989
990
991
992
  gst_buffer_map (script_tag, &map, GST_MAP_WRITE);
  map.data[1] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 16) & 0xff;
  map.data[2] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 8) & 0xff;
  map.data[3] = ((gst_buffer_get_size (script_tag) - 11 - 4) >> 0) & 0xff;
993

Wim Taymans's avatar
Wim Taymans committed
994
995
  GST_WRITE_UINT32_BE (map.data + 11 + 13 + 1, tags_written);
  gst_buffer_unmap (script_tag, &map);
996

997
exit:
Jan Urbański's avatar
Jan Urbański committed
998
  return script_tag;
999
1000
}

Jan Urbański's avatar
Jan Urbański committed
1001
static GstBuffer *
1002
1003
gst_flv_mux_buffer_to_tag_internal (GstFlvMux * mux, GstBuffer * buffer,
    GstFlvPad * cpad, gboolean is_codec_data)
1004
1005
{
  GstBuffer *tag;
Wim Taymans's avatar
Wim Taymans committed
1006
  GstMapInfo map;
1007
  guint size;
1008
1009
1010
  guint32 timestamp =
      (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) ? GST_BUFFER_TIMESTAMP (buffer) /
      GST_MSECOND : cpad->last_timestamp / GST_MSECOND;
Wim Taymans's avatar
Wim Taymans committed
1011
  guint8 *data, *bdata;
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1012
1013
  gsize bsize;

Wim Taymans's avatar
Wim Taymans committed
1014
1015
1016
  gst_buffer_map (buffer, &map, GST_MAP_READ);
  bdata = map.data;
  bsize = map.size;
1017
1018
1019
1020

  size = 11;
  if (cpad->video) {
    size += 1;
Jan Urbański's avatar
Jan Urbański committed
1021
    if (cpad->video_codec == 7)
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1022
      size += 4 + bsize;
1023
    else
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1024
      size += bsize;
1025
1026
  } else {
    size += 1;
Jan Urbański's avatar
Jan Urbański committed
1027
    if (cpad->audio_codec == 10)
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1028
      size += 1 + bsize;
1029
    else
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1030
      size += bsize;
1031
1032
1033
  }
  size += 4;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1034
  _gst_buffer_new_and_alloc (size, &tag, &data);
1035
  GST_BUFFER_TIMESTAMP (tag) = timestamp * GST_MSECOND;
1036
1037
1038
1039
1040
1041
1042
1043
  memset (data, 0, size);

  data[0] = (cpad->video) ? 9 : 8;

  data[1] = ((size - 11 - 4) >> 16) & 0xff;
  data[2] = ((size - 11 - 4) >> 8) & 0xff;
  data[3] = ((size - 11 - 4) >> 0) & 0xff;

1044
1045
  /* wrap the timestamp every G_MAXINT32 miliseconds */
  timestamp &= 0x7fffffff;
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
  data[4] = (timestamp >> 16) & 0xff;
  data[5] = (timestamp >> 8) & 0xff;
  data[6] = (timestamp >> 0) & 0xff;
  data[7] = (timestamp >> 24) & 0xff;

  data[8] = data[9] = data[10] = 0;

  if (cpad->video) {
    if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
      data[11] |= 2 << 4;
    else
      data[11] |= 1 << 4;

    data[11] |= cpad->video_codec & 0x0f;

Jan Urbański's avatar
Jan Urbański committed
1061
1062
    if (cpad->video_codec == 7) {
      data[12] = is_codec_data ? 0 : 1;
1063
1064
1065
1066

      /* FIXME: what to do about composition time */
      data[13] = data[14] = data[15] = 0;

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1067
      memcpy (data + 11 + 1 + 4, bdata, bsize);
1068
    } else {
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1069
      memcpy (data + 11 + 1, bdata, bsize);
1070
1071
1072
1073
1074
1075
1076
    }
  } else {
    data[11] |= (cpad->audio_codec << 4) & 0xf0;
    data[11] |= (cpad->rate << 2) & 0x0c;
    data[11] |= (cpad->width << 1) & 0x02;
    data[11] |= (cpad->channels << 0) & 0x01;

Jan Urbański's avatar
Jan Urbański committed
1077
1078
    if (cpad->audio_codec == 10) {
      data[12] = is_codec_data ? 0 : 1;
1079

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1080
      memcpy (data + 11 + 1 + 1, bdata, bsize);
1081
    } else {
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1082
      memcpy (data + 11 + 1, bdata, bsize);
1083
1084
1085
    }
  }

Wim Taymans's avatar
Wim Taymans committed
1086
  gst_buffer_unmap (buffer, &map);
Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1087

1088
1089
  GST_WRITE_UINT32_BE (data + size - 4, size - 4);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1090
1091
1092
1093
1094
  GST_BUFFER_TIMESTAMP (tag) = GST_BUFFER_TIMESTAMP (buffer);
  GST_BUFFER_DURATION (tag) = GST_BUFFER_DURATION (buffer);
  GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET (buffer);
  GST_BUFFER_OFFSET_END (tag) = GST_BUFFER_OFFSET_END (buffer);

1095
1096
1097
1098
1099
1100
  /* mark the buffer if it's an audio buffer and there's also video being muxed
   * or it's a video interframe */
  if ((mux->have_video && !cpad->video) ||
      GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT))
    GST_BUFFER_FLAG_SET (tag, GST_BUFFER_FLAG_DELTA_UNIT);

Jan Urbański's avatar
Jan Urbański committed
1101
1102
  GST_BUFFER_OFFSET (tag) = GST_BUFFER_OFFSET_END (tag) =
      GST_BUFFER_OFFSET_NONE;
1103

Jan Urbański's avatar
Jan Urbański committed
1104
1105
  return tag;
}
1106

Jan Urbański's avatar
Jan Urbański committed
1107
static inline GstBuffer *
1108
1109
gst_flv_mux_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
    GstFlvPad * cpad)
Jan Urbański's avatar
Jan Urbański committed
1110
{
1111
  return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, FALSE);
Jan Urbański's avatar
Jan Urbański committed
1112
1113
1114
}

static inline GstBuffer *
1115
1116
gst_flv_mux_codec_data_buffer_to_tag (GstFlvMux * mux, GstBuffer * buffer,
    GstFlvPad * cpad)
Jan Urbański's avatar
Jan Urbański committed
1117
{
1118
  return gst_flv_mux_buffer_to_tag_internal (mux, buffer, cpad, TRUE);
Jan Urbański's avatar
Jan Urbański committed
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
}

static void
gst_flv_mux_put_buffer_in_streamheader (GValue * streamheader,
    GstBuffer * buffer)
{
  GValue value = { 0 };
  GstBuffer *buf;

  g_value_init (&value, GST_TYPE_BUFFER);
  buf = gst_buffer_copy (buffer);
  gst_value_set_buffer (&value, buf);
  gst_buffer_unref (buf);
  gst_value_array_append_value (streamheader, &value);
  g_value_unset (&value);
}

static GstFlowReturn
gst_flv_mux_write_header (GstFlvMux * mux)
{
  GstBuffer *header, *metadata;
  GstBuffer *video_codec_data, *audio_codec_data;
  GstCaps *caps;
  GstStructure *structure;
  GValue streamheader = { 0 };
  GSList *l;
  GstFlowReturn ret;

  header = gst_flv_mux_create_header (mux);
1148
  metadata = gst_flv_mux_create_metadata (mux, TRUE);
Jan Urbański's avatar
Jan Urbański committed
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
  video_codec_data = NULL;
  audio_codec_data = NULL;

  for (l = mux->collect->data; l != NULL; l = l->next) {
    GstFlvPad *cpad = l->data;

    /* Get H.264 and AAC codec data, if present */
    if (cpad && cpad->video && cpad->video_codec == 7) {
      if (cpad->video_codec_data == NULL)
        GST_WARNING_OBJECT (mux, "Codec data for video stream not found, "
            "output might not be playable");
      else
        video_codec_data =
1162
1163
            gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->video_codec_data,
            cpad);
Jan Urbański's avatar
Jan Urbański committed
1164
1165
1166
1167
1168
1169
    } else if (cpad && !cpad->video && cpad->audio_codec == 10) {
      if (cpad->audio_codec_data == NULL)
        GST_WARNING_OBJECT (mux, "Codec data for audio stream not found, "
            "output might not be playable");
      else
        audio_codec_data =
1170
1171
            gst_flv_mux_codec_data_buffer_to_tag (mux, cpad->audio_codec_data,
            cpad);
1172
    }
Jan Urbański's avatar
Jan Urbański committed
1173
1174
1175
  }

  /* mark buffers that will go in the streamheader */
Wim Taymans's avatar
Wim Taymans committed
1176
1177
  GST_BUFFER_FLAG_SET (header, GST_BUFFER_FLAG_HEADER);
  GST_BUFFER_FLAG_SET (metadata, GST_BUFFER_FLAG_HEADER);
1178
  if (video_codec_data != NULL) {
Wim Taymans's avatar
Wim Taymans committed
1179
    GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_HEADER);
1180
1181
1182
1183
1184
    /* mark as a delta unit, so downstream will not try to synchronize on that
     * buffer - to actually start playback you need a real video keyframe */
    GST_BUFFER_FLAG_SET (video_codec_data, GST_BUFFER_FLAG_DELTA_UNIT);
  }
  if (audio_codec_data != NULL) {
Wim Taymans's avatar
Wim Taymans committed
1185
    GST_BUFFER_FLAG_SET (audio_codec_data, GST_BUFFER_FLAG_HEADER);
1186
  }
Jan Urbański's avatar
Jan Urbański committed
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197

  /* put buffers in streamheader */
  g_value_init (&streamheader, GST_TYPE_ARRAY);
  gst_flv_mux_put_buffer_in_streamheader (&streamheader, header);
  gst_flv_mux_put_buffer_in_streamheader (&streamheader, metadata);
  if (video_codec_data != NULL)
    gst_flv_mux_put_buffer_in_streamheader (&streamheader, video_codec_data);
  if (audio_codec_data != NULL)
    gst_flv_mux_put_buffer_in_streamheader (&streamheader, audio_codec_data);

  /* create the caps and put the streamheader in them */
1198
  caps = gst_caps_new_empty_simple ("video/x-flv");
Jan Urbański's avatar
Jan Urbański committed
1199
1200
1201
1202
  structure = gst_caps_get_structure (caps, 0);
  gst_structure_set_value (structure, "streamheader", &streamheader);
  g_value_unset (&streamheader);

Mark Nauwelaerts's avatar
Mark Nauwelaerts committed
1203
  if (!gst_pad_has_current_caps (mux->srcpad))
Jan Urbański's avatar
Jan Urbański committed
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
    gst_pad_set_caps (mux->srcpad, caps);

  gst_caps_unref (caps);

  /* push the header buffer, the metadata and the codec info, if any */
  ret = gst_flv_mux_push (mux, header);
  if (ret != GST_FLOW_OK)
    return ret;
  ret = gst_flv_mux_push (mux, metadata);
  if (ret != GST_FLOW_OK)
    return ret;
  if (video_codec_data != NULL) {
    ret = gst_flv_mux_push (mux, video_codec_data);
    if (ret != GST_FLOW_OK)
      return ret;
  }
  if (audio_codec_data != NULL) {
    ret = gst_flv_mux_push (mux, audio_codec_data);
    if (ret != GST_FLOW_OK)
      return ret;
  }
  return GST_FLOW_OK;
}
1227

Jan Urbański's avatar
Jan Urbański committed
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
static void
gst_flv_mux_update_index (GstFlvMux * mux, GstBuffer * buffer, GstFlvPad * cpad)
{
  /*
   * Add the tag byte offset and to the index if it's a valid seek point, which
   * means it's either a video keyframe or if there is no video pad (in that
   * case every FLV tag is a valid seek point)
   */
  if (mux->have_video &&
      (!cpad->video ||
          GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT)))
    return;
1240

Jan Urbański's avatar
Jan Urbański committed
1241
1242
1243
1244
1245
1246
  if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
    GstFlvMuxIndexEntry *entry = g_slice_new (GstFlvMuxIndexEntry);
    entry->position = mux->byte_count;
    entry->time =
        gst_guint64_to_gdouble (GST_BUFFER_TIMESTAMP (buffer)) / GST_SECOND;
    mux->index = g_list_prepend (mux->index, entry);
1247
  }
Jan Urbański's avatar
Jan Urbański committed
1248
}
1249

Jan Urbański's avatar
Jan Urbański committed
1250
static GstFlowReturn
1251
gst_flv_mux_write_buffer (GstFlvMux * mux, GstFlvPad * cpad, GstBuffer * buffer)
Jan Urbański's avatar
Jan Urbański committed
1252
1253
1254
{
  GstBuffer *tag;
  GstFlowReturn ret;
1255

1256
  /* clipping function arranged for running_time */
1257

1258
  if (!mux->streamable)
Jan Urbański's avatar
Jan Urbański committed
1259
    gst_flv_mux_update_index (mux, buffer, cpad);