gstoggmux.c 52.2 KB
Newer Older
Wim Taymans's avatar
Wim Taymans committed
1
2
/* OGG muxer plugin for GStreamer
 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
3
 * Copyright (C) 2006 Thomas Vander Stichele <thomas at apestaart dot org>
Wim Taymans's avatar
Wim Taymans committed
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
/**
 * SECTION:element-oggmux
Stefan Kost's avatar
Stefan Kost committed
23
 * @see_also: <link linkend="gst-plugins-base-plugins-oggdemux">oggdemux</link>
24
25
 *
 * This element merges streams (audio and video) into ogg files.
26
27
 *
 * <refsect2>
28
 * <title>Example pipelines</title>
29
 * |[
30
 * gst-launch v4l2src num-buffers=500 ! video/x-raw-yuv,width=320,height=240 ! ffmpegcolorspace ! theoraenc ! oggmux ! filesink location=video.ogg
31
 * ]| Encodes a video stream captured from a v4l2-compatible camera to Ogg/Theora
32
 * (the encoding will stop automatically after 500 frames)
33
34
35
36
37
 * </refsect2>
 *
 * Last reviewed on 2008-02-06 (0.10.17)
 */

Wim Taymans's avatar
Wim Taymans committed
38
39
40
41
42
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <gst/gst.h>
Wim Taymans's avatar
Wim Taymans committed
43
#include <gst/base/gstcollectpads.h>
44
#include <gst/tag/tag.h>
Wim Taymans's avatar
Wim Taymans committed
45

46
47
#include "gstoggmux.h"

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
48
/* memcpy - if someone knows a way to get rid of it, please speak up
Wim Taymans's avatar
Wim Taymans committed
49
50
51
 * note: the ogg docs even say you need this... */
#include <string.h>
#include <time.h>
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
52
#include <stdlib.h>             /* rand, srand, atoi */
Wim Taymans's avatar
Wim Taymans committed
53
54
55
56

GST_DEBUG_CATEGORY_STATIC (gst_ogg_mux_debug);
#define GST_CAT_DEFAULT gst_ogg_mux_debug

57
58
59
60
61
62
63
64
/* This isn't generally what you'd want with an end-time macro, because
   technically the end time of a buffer with invalid duration is invalid. But
   for sorting ogg pages this is what we want. */
#define GST_BUFFER_END_TIME(buf) \
    (GST_BUFFER_DURATION_IS_VALID (buf) \
    ? GST_BUFFER_TIMESTAMP (buf) + GST_BUFFER_DURATION (buf) \
    : GST_BUFFER_TIMESTAMP (buf))

65
#define GST_GP_FORMAT "[gp %8" G_GINT64_FORMAT "]"
66
#define GST_GP_CAST(_gp) ((gint64) _gp)
67

Wim Taymans's avatar
Wim Taymans committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81
typedef enum
{
  GST_OGG_FLAG_BOS = GST_ELEMENT_FLAG_LAST,
  GST_OGG_FLAG_EOS
}
GstOggFlag;

/* OggMux signals and args */
enum
{
  /* FILL ME */
  LAST_SIGNAL
};

Wim Taymans's avatar
Wim Taymans committed
82
/* set to 0.5 seconds by default */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
83
84
#define DEFAULT_MAX_DELAY       G_GINT64_CONSTANT(500000000)
#define DEFAULT_MAX_PAGE_DELAY  G_GINT64_CONSTANT(500000000)
Wim Taymans's avatar
Wim Taymans committed
85
86
enum
{
87
88
  ARG_0,
  ARG_MAX_DELAY,
89
  ARG_MAX_PAGE_DELAY,
Wim Taymans's avatar
Wim Taymans committed
90
91
92
93
94
95
96
97
98
99
100
};

static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
    GST_PAD_SRC,
    GST_PAD_ALWAYS,
    GST_STATIC_CAPS ("application/ogg")
    );

static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink_%d",
    GST_PAD_SINK,
    GST_PAD_REQUEST,
101
    GST_STATIC_CAPS ("video/x-theora; "
102
        "audio/x-vorbis; audio/x-flac; audio/x-speex; audio/x-celt; "
103
        "application/x-ogm-video; application/x-ogm-audio; video/x-dirac; "
104
        "video/x-smoke; video/x-vp8; text/x-cmml, encoded = (boolean) TRUE; "
105
        "subtitle/x-kate; application/x-kate")
Wim Taymans's avatar
Wim Taymans committed
106
107
108
109
110
    );

static void gst_ogg_mux_base_init (gpointer g_class);
static void gst_ogg_mux_class_init (GstOggMuxClass * klass);
static void gst_ogg_mux_init (GstOggMux * ogg_mux);
111
static void gst_ogg_mux_finalize (GObject * object);
Wim Taymans's avatar
Wim Taymans committed
112

Wim Taymans's avatar
Wim Taymans committed
113
114
static GstFlowReturn
gst_ogg_mux_collected (GstCollectPads * pads, GstOggMux * ogg_mux);
Wim Taymans's avatar
Wim Taymans committed
115
116
117
static gboolean gst_ogg_mux_handle_src_event (GstPad * pad, GstEvent * event);
static GstPad *gst_ogg_mux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * name);
118
119
static void gst_ogg_mux_release_pad (GstElement * element, GstPad * pad);

Wim Taymans's avatar
Wim Taymans committed
120
121
122
123
static void gst_ogg_mux_set_property (GObject * object,
    guint prop_id, const GValue * value, GParamSpec * pspec);
static void gst_ogg_mux_get_property (GObject * object,
    guint prop_id, GValue * value, GParamSpec * pspec);
124
125
static GstStateChangeReturn gst_ogg_mux_change_state (GstElement * element,
    GstStateChange transition);
Wim Taymans's avatar
Wim Taymans committed
126
127
128
129
130
131
132
133
134
135

static GstElementClass *parent_class = NULL;

/*static guint gst_ogg_mux_signals[LAST_SIGNAL] = { 0 }; */

GType
gst_ogg_mux_get_type (void)
{
  static GType ogg_mux_type = 0;

136
  if (G_UNLIKELY (ogg_mux_type == 0)) {
Wim Taymans's avatar
Wim Taymans committed
137
138
139
140
141
142
143
144
145
146
147
    static const GTypeInfo ogg_mux_info = {
      sizeof (GstOggMuxClass),
      gst_ogg_mux_base_init,
      NULL,
      (GClassInitFunc) gst_ogg_mux_class_init,
      NULL,
      NULL,
      sizeof (GstOggMux),
      0,
      (GInstanceInitFunc) gst_ogg_mux_init,
    };
148
149
150
151
152
    static const GInterfaceInfo preset_info = {
      NULL,
      NULL,
      NULL
    };
Wim Taymans's avatar
Wim Taymans committed
153
154
155
156

    ogg_mux_type =
        g_type_register_static (GST_TYPE_ELEMENT, "GstOggMux", &ogg_mux_info,
        0);
157
158

    g_type_add_interface_static (ogg_mux_type, GST_TYPE_PRESET, &preset_info);
Wim Taymans's avatar
Wim Taymans committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  }
  return ogg_mux_type;
}

static void
gst_ogg_mux_base_init (gpointer g_class)
{
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);

  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&src_factory));
  gst_element_class_add_pad_template (element_class,
      gst_static_pad_template_get (&sink_factory));

173
174
175
176
  gst_element_class_set_details_simple (element_class,
      "Ogg muxer", "Codec/Muxer",
      "mux ogg streams (info about ogg: http://xiph.org)",
      "Wim Taymans <wim@fluendo.com>");
Wim Taymans's avatar
Wim Taymans committed
177
178
179
180
181
182
183
184
185
186
187
}

static void
gst_ogg_mux_class_init (GstOggMuxClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

188
  parent_class = g_type_class_peek_parent (klass);
Wim Taymans's avatar
Wim Taymans committed
189

190
  gobject_class->finalize = gst_ogg_mux_finalize;
191
192
193
  gobject_class->get_property = gst_ogg_mux_get_property;
  gobject_class->set_property = gst_ogg_mux_set_property;

Wim Taymans's avatar
Wim Taymans committed
194
  gstelement_class->request_new_pad = gst_ogg_mux_request_new_pad;
195
  gstelement_class->release_pad = gst_ogg_mux_release_pad;
Wim Taymans's avatar
Wim Taymans committed
196

197
198
199
  g_object_class_install_property (gobject_class, ARG_MAX_DELAY,
      g_param_spec_uint64 ("max-delay", "Max delay",
          "Maximum delay in multiplexing streams", 0, G_MAXUINT64,
200
201
          DEFAULT_MAX_DELAY,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
202
203
204
  g_object_class_install_property (gobject_class, ARG_MAX_PAGE_DELAY,
      g_param_spec_uint64 ("max-page-delay", "Max page delay",
          "Maximum delay for sending out a page", 0, G_MAXUINT64,
205
206
          DEFAULT_MAX_PAGE_DELAY,
          (GParamFlags) G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
207

Wim Taymans's avatar
Wim Taymans committed
208
209
210
211
  gstelement_class->change_state = gst_ogg_mux_change_state;

}

Wim Taymans's avatar
Wim Taymans committed
212
#if 0
Wim Taymans's avatar
Wim Taymans committed
213
214
215
216
217
static const GstEventMask *
gst_ogg_mux_get_sink_event_masks (GstPad * pad)
{
  static const GstEventMask gst_ogg_mux_sink_event_masks[] = {
    {GST_EVENT_EOS, 0},
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
218
    {GST_EVENT_DISCONTINUOUS, 0},
Wim Taymans's avatar
Wim Taymans committed
219
220
221
222
223
    {0,}
  };

  return gst_ogg_mux_sink_event_masks;
}
Wim Taymans's avatar
Wim Taymans committed
224
#endif
Wim Taymans's avatar
Wim Taymans committed
225

226
227
228
229
230
231
static void
gst_ogg_mux_clear (GstOggMux * ogg_mux)
{
  ogg_mux->pulling = NULL;
  ogg_mux->need_headers = TRUE;
  ogg_mux->delta_pad = NULL;
232
233
  ogg_mux->offset = 0;
  ogg_mux->next_ts = 0;
234
  ogg_mux->last_ts = GST_CLOCK_TIME_NONE;
235
236
}

Wim Taymans's avatar
Wim Taymans committed
237
238
239
240
241
242
243
244
245
246
247
static void
gst_ogg_mux_init (GstOggMux * ogg_mux)
{
  GstElementClass *klass = GST_ELEMENT_GET_CLASS (ogg_mux);

  ogg_mux->srcpad =
      gst_pad_new_from_template (gst_element_class_get_pad_template (klass,
          "src"), "src");
  gst_pad_set_event_function (ogg_mux->srcpad, gst_ogg_mux_handle_src_event);
  gst_element_add_pad (GST_ELEMENT (ogg_mux), ogg_mux->srcpad);

248
  GST_OBJECT_FLAG_SET (GST_ELEMENT (ogg_mux), GST_OGG_FLAG_BOS);
Wim Taymans's avatar
Wim Taymans committed
249
250
251
252

  /* seed random number generator for creation of serial numbers */
  srand (time (NULL));

253
254
  ogg_mux->collect = gst_collect_pads_new ();
  gst_collect_pads_set_function (ogg_mux->collect,
255
256
      (GstCollectPadsFunction) GST_DEBUG_FUNCPTR (gst_ogg_mux_collected),
      ogg_mux);
257

258
259
260
  ogg_mux->max_delay = DEFAULT_MAX_DELAY;
  ogg_mux->max_page_delay = DEFAULT_MAX_PAGE_DELAY;

261
  gst_ogg_mux_clear (ogg_mux);
Wim Taymans's avatar
Wim Taymans committed
262
263
}

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
static void
gst_ogg_mux_finalize (GObject * object)
{
  GstOggMux *ogg_mux;

  ogg_mux = GST_OGG_MUX (object);

  if (ogg_mux->collect) {
    gst_object_unref (ogg_mux->collect);
    ogg_mux->collect = NULL;
  }

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

279
280
281
static void
gst_ogg_mux_ogg_pad_destroy_notify (GstCollectData * data)
{
282
  GstOggPadData *oggpad = (GstOggPadData *) data;
283
284
285
286
287
288
289
290
291
292
293
294
295
  GstBuffer *buf;

  ogg_stream_clear (&oggpad->stream);

  if (oggpad->pagebuffers) {
    while ((buf = g_queue_pop_head (oggpad->pagebuffers)) != NULL) {
      gst_buffer_unref (buf);
    }
    g_queue_free (oggpad->pagebuffers);
    oggpad->pagebuffers = NULL;
  }
}

Wim Taymans's avatar
Wim Taymans committed
296
static GstPadLinkReturn
297
gst_ogg_mux_sinkconnect (GstPad * pad, GstPad * peer)
Wim Taymans's avatar
Wim Taymans committed
298
299
300
301
302
{
  GstOggMux *ogg_mux;

  ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));

303
  GST_DEBUG_OBJECT (ogg_mux, "sinkconnect triggered on %s", GST_PAD_NAME (pad));
304

305
  gst_object_unref (ogg_mux);
Wim Taymans's avatar
Wim Taymans committed
306
307
308
309

  return GST_PAD_LINK_OK;
}

310
311
312
313
static gboolean
gst_ogg_mux_sink_event (GstPad * pad, GstEvent * event)
{
  GstOggMux *ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));
314
  GstOggPadData *ogg_pad = (GstOggPadData *) gst_pad_get_element_private (pad);
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  gboolean ret;

  GST_DEBUG ("Got %s event on pad %s:%s", GST_EVENT_TYPE_NAME (event),
      GST_DEBUG_PAD_NAME (pad));

  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_NEWSEGMENT:
      /* We don't support NEWSEGMENT events */
      gst_event_unref (event);
      ret = FALSE;
      break;
    default:
      ret = TRUE;
      break;
  }

  /* now GstCollectPads can take care of the rest, e.g. EOS */
  if (ret)
    ret = ogg_pad->collect_event (pad, event);

  gst_object_unref (ogg_mux);
  return ret;
}

Wim Taymans's avatar
Wim Taymans committed
339
340
341
342
343
344
static GstPad *
gst_ogg_mux_request_new_pad (GstElement * element,
    GstPadTemplate * templ, const gchar * req_name)
{
  GstOggMux *ogg_mux;
  GstPad *newpad;
Wim Taymans's avatar
Wim Taymans committed
345
  GstElementClass *klass;
Wim Taymans's avatar
Wim Taymans committed
346
347
348

  g_return_val_if_fail (templ != NULL, NULL);

Wim Taymans's avatar
Wim Taymans committed
349
350
  if (templ->direction != GST_PAD_SINK)
    goto wrong_direction;
Wim Taymans's avatar
Wim Taymans committed
351
352
353
354

  g_return_val_if_fail (GST_IS_OGG_MUX (element), NULL);
  ogg_mux = GST_OGG_MUX (element);

Wim Taymans's avatar
Wim Taymans committed
355
356
357
358
359
360
  klass = GST_ELEMENT_GET_CLASS (element);

  if (templ != gst_element_class_get_pad_template (klass, "sink_%d"))
    goto wrong_template;

  {
Wim Taymans's avatar
Wim Taymans committed
361
362
363
364
365
366
367
368
369
370
371
    gint serial;
    gchar *name;

    if (req_name == NULL || strlen (req_name) < 6) {
      /* no name given when requesting the pad, use random serial number */
      serial = rand ();
    } else {
      /* parse serial number from requested padname */
      serial = atoi (&req_name[5]);
    }
    /* create new pad with the name */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
372
    GST_DEBUG_OBJECT (ogg_mux, "Creating new pad for serial %d", serial);
Wim Taymans's avatar
Wim Taymans committed
373
374
375
376
377
378
379
    name = g_strdup_printf ("sink_%d", serial);
    newpad = gst_pad_new_from_template (templ, name);
    g_free (name);

    /* construct our own wrapper data structure for the pad to
     * keep track of its status */
    {
380
      GstOggPadData *oggpad;
Wim Taymans's avatar
Wim Taymans committed
381

382
      oggpad = (GstOggPadData *)
383
          gst_collect_pads_add_pad_full (ogg_mux->collect, newpad,
384
          sizeof (GstOggPadData), gst_ogg_mux_ogg_pad_destroy_notify);
385
      ogg_mux->active_pads++;
Wim Taymans's avatar
Wim Taymans committed
386
387
388
389
390
391
392
393

      oggpad->serial = serial;
      ogg_stream_init (&oggpad->stream, serial);
      oggpad->packetno = 0;
      oggpad->pageno = 0;
      oggpad->eos = FALSE;
      /* we assume there will be some control data first for this pad */
      oggpad->state = GST_OGG_PAD_STATE_CONTROL;
394
395
396
      oggpad->new_page = TRUE;
      oggpad->first_delta = FALSE;
      oggpad->prev_delta = FALSE;
397
      oggpad->data_pushed = FALSE;
398
      oggpad->pagebuffers = g_queue_new ();
399
400
401
402

      oggpad->collect_event = (GstPadEventFunction) GST_PAD_EVENTFUNC (newpad);
      gst_pad_set_event_function (newpad,
          GST_DEBUG_FUNCPTR (gst_ogg_mux_sink_event));
Wim Taymans's avatar
Wim Taymans committed
403
404
405
406
407
    }
  }

  /* setup some pad functions */
  gst_pad_set_link_function (newpad, gst_ogg_mux_sinkconnect);
408

Wim Taymans's avatar
Wim Taymans committed
409
410
411
412
  /* dd the pad to the element */
  gst_element_add_pad (element, newpad);

  return newpad;
Wim Taymans's avatar
Wim Taymans committed
413
414
415
416
417
418
419
420
421
422
423
424

  /* ERRORS */
wrong_direction:
  {
    g_warning ("ogg_mux: request pad that is not a SINK pad\n");
    return NULL;
  }
wrong_template:
  {
    g_warning ("ogg_mux: this is not our template!\n");
    return NULL;
  }
Wim Taymans's avatar
Wim Taymans committed
425
426
}

427
428
429
430
431
432
433
434
static void
gst_ogg_mux_release_pad (GstElement * element, GstPad * pad)
{
  GstOggMux *ogg_mux;

  ogg_mux = GST_OGG_MUX (gst_pad_get_parent (pad));

  gst_collect_pads_remove_pad (ogg_mux->collect, pad);
435
  gst_element_remove_pad (element, pad);
436
437

  gst_object_unref (ogg_mux);
438
439
}

Wim Taymans's avatar
Wim Taymans committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/* handle events */
static gboolean
gst_ogg_mux_handle_src_event (GstPad * pad, GstEvent * event)
{
  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;
  }

  return gst_pad_event_default (pad, event);
}

459
static GstBuffer *
460
gst_ogg_mux_buffer_from_page (GstOggMux * mux, ogg_page * page, gboolean delta)
Wim Taymans's avatar
Wim Taymans committed
461
462
463
464
{
  GstBuffer *buffer;

  /* allocate space for header and body */
Wim Taymans's avatar
Wim Taymans committed
465
  buffer = gst_buffer_new_and_alloc (page->header_len + page->body_len);
Wim Taymans's avatar
Wim Taymans committed
466
467
468
469
  memcpy (GST_BUFFER_DATA (buffer), page->header, page->header_len);
  memcpy (GST_BUFFER_DATA (buffer) + page->header_len,
      page->body, page->body_len);

470
471
472
473
  /* Here we set granulepos as our OFFSET_END to give easy direct access to
   * this value later. Before we push it, we reset this to OFFSET + SIZE
   * (see gst_ogg_mux_push_buffer). */
  GST_BUFFER_OFFSET_END (buffer) = ogg_page_granulepos (page);
474
  if (delta)
475
    GST_BUFFER_FLAG_SET (buffer, GST_BUFFER_FLAG_DELTA_UNIT);
Wim Taymans's avatar
Wim Taymans committed
476

477
  GST_LOG_OBJECT (mux, GST_GP_FORMAT
478
479
      " created buffer %p from ogg page",
      GST_GP_CAST (ogg_page_granulepos (page)), buffer);
480

481
482
483
  return buffer;
}

Wim Taymans's avatar
Wim Taymans committed
484
static GstFlowReturn
485
gst_ogg_mux_push_buffer (GstOggMux * mux, GstBuffer * buffer)
486
{
487
488
  GstCaps *caps;

489
490
491
492
  /* fix up OFFSET and OFFSET_END again */
  GST_BUFFER_OFFSET (buffer) = mux->offset;
  mux->offset += GST_BUFFER_SIZE (buffer);
  GST_BUFFER_OFFSET_END (buffer) = mux->offset;
493

494
495
  /* Ensure we have monotonically increasing timestamps in the output. */
  if (GST_BUFFER_TIMESTAMP_IS_VALID (buffer)) {
496
497
    if (mux->last_ts != GST_CLOCK_TIME_NONE &&
        GST_BUFFER_TIMESTAMP (buffer) < mux->last_ts)
498
499
500
501
502
      GST_BUFFER_TIMESTAMP (buffer) = mux->last_ts;
    else
      mux->last_ts = GST_BUFFER_TIMESTAMP (buffer);
  }

503
  caps = gst_pad_get_negotiated_caps (mux->srcpad);
504
  gst_buffer_set_caps (buffer, caps);
505
506
  if (caps)
    gst_caps_unref (caps);
507

508
509
510
511
512
513
514
515
516
  return gst_pad_push (mux->srcpad, buffer);
}

/* if all queues have at least one page, dequeue the page with the lowest
 * timestamp */
static gboolean
gst_ogg_mux_dequeue_page (GstOggMux * mux, GstFlowReturn * flowret)
{
  GSList *walk;
517
  GstOggPadData *opad = NULL;   /* "oldest" pad */
518
519
520
521
522
523
524
525
  GstClockTime oldest = GST_CLOCK_TIME_NONE;
  GstBuffer *buf = NULL;
  gboolean ret = FALSE;

  *flowret = GST_FLOW_OK;

  walk = mux->collect->data;
  while (walk) {
526
    GstOggPadData *pad = (GstOggPadData *) walk->data;
527
528
529
530
531
532
533

    /* We need each queue to either be at EOS, or have one or more pages
     * available with a set granulepos (i.e. not -1), otherwise we don't have
     * enough data yet to determine which stream needs to go next for correct
     * time ordering. */
    if (pad->pagebuffers->length == 0) {
      if (pad->eos) {
534
535
        GST_LOG_OBJECT (pad->collect.pad,
            "pad is EOS, skipping for dequeue decision");
536
      } else {
537
538
        GST_LOG_OBJECT (pad->collect.pad,
            "no pages in this queue, can't dequeue");
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
        return FALSE;
      }
    } else {
      /* We then need to check for a non-negative granulepos */
      int i;
      gboolean valid = FALSE;

      for (i = 0; i < pad->pagebuffers->length; i++) {
        buf = g_queue_peek_nth (pad->pagebuffers, i);
        /* Here we check the OFFSET_END, which is actually temporarily the
         * granulepos value for this buffer */
        if (GST_BUFFER_OFFSET_END (buf) != -1) {
          valid = TRUE;
          break;
        }
      }
      if (!valid) {
556
557
        GST_LOG_OBJECT (pad->collect.pad,
            "No page timestamps in queue, can't dequeue");
558
559
560
561
562
563
564
565
566
        return FALSE;
      }
    }

    walk = g_slist_next (walk);
  }

  walk = mux->collect->data;
  while (walk) {
567
    GstOggPadData *pad = (GstOggPadData *) walk->data;
568

569
    /* any page with a granulepos of -1 can be pushed immediately.
570
571
572
     * TODO: it CAN be, but it seems silly to do so? */
    buf = g_queue_peek_head (pad->pagebuffers);
    while (buf && GST_BUFFER_OFFSET_END (buf) == -1) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
573
      GST_LOG_OBJECT (pad->collect.pad, "[gp        -1] pushing page");
574
575
576
577
578
579
580
581
582
      g_queue_pop_head (pad->pagebuffers);
      *flowret = gst_ogg_mux_push_buffer (mux, buf);
      buf = g_queue_peek_head (pad->pagebuffers);
      ret = TRUE;
    }

    if (buf) {
      /* if no oldest buffer yet, take this one */
      if (oldest == GST_CLOCK_TIME_NONE) {
583
584
585
586
        GST_LOG_OBJECT (mux, "no oldest yet, taking buffer %p from pad %"
            GST_PTR_FORMAT " with gp time %" GST_TIME_FORMAT,
            buf, pad->collect.pad, GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
        oldest = GST_BUFFER_OFFSET (buf);
587
588
589
        opad = pad;
      } else {
        /* if we have an oldest, compare with this one */
590
591
592
593
594
        if (GST_BUFFER_OFFSET (buf) < oldest) {
          GST_LOG_OBJECT (mux, "older buffer %p, taking from pad %"
              GST_PTR_FORMAT " with gp time %" GST_TIME_FORMAT,
              buf, pad->collect.pad, GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
          oldest = GST_BUFFER_OFFSET (buf);
595
596
597
598
599
600
601
602
603
604
          opad = pad;
        }
      }
    }
    walk = g_slist_next (walk);
  }

  if (oldest != GST_CLOCK_TIME_NONE) {
    g_assert (opad);
    buf = g_queue_pop_head (opad->pagebuffers);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
605
    GST_LOG_OBJECT (opad->collect.pad,
606
607
608
        GST_GP_FORMAT " pushing oldest page buffer %p (granulepos time %"
        GST_TIME_FORMAT ")", GST_BUFFER_OFFSET_END (buf), buf,
        GST_TIME_ARGS (GST_BUFFER_OFFSET (buf)));
609
610
611
612
613
614
615
    *flowret = gst_ogg_mux_push_buffer (mux, buf);
    ret = TRUE;
  }

  return ret;
}

616
617
618
619
620
621
622
623
624
625
626
627
628
/* put the given ogg page on a per-pad queue, timestamping it correctly.
 * after that, dequeue and push as many pages as possible.
 * Caller should make sure:
 * pad->timestamp     was set with the timestamp of the first packet put
 *                    on the page
 * pad->timestamp_end was set with the timestamp + duration of the last packet
 *                    put on the page
 * pad->gp_time       was set with the time matching the gp of the last
 *                    packet put on the page
 *
 * will also reset timestamp and timestamp_end, so caller func can restart
 * counting.
 */
629
static GstFlowReturn
630
631
gst_ogg_mux_pad_queue_page (GstOggMux * mux, GstOggPadData * pad,
    ogg_page * page, gboolean delta)
632
{
Wim Taymans's avatar
Wim Taymans committed
633
  GstFlowReturn ret;
634
  GstBuffer *buffer = gst_ogg_mux_buffer_from_page (mux, page, delta);
635

636
  /* take the timestamp of the first packet on this page */
637
  GST_BUFFER_TIMESTAMP (buffer) = pad->timestamp;
638
  GST_BUFFER_DURATION (buffer) = pad->timestamp_end - pad->timestamp;
639
640
  /* take the gp time of the last completed packet on this page */
  GST_BUFFER_OFFSET (buffer) = pad->gp_time;
641

642
  /* the next page will start where the current page's end time leaves off */
643
644
  pad->timestamp = pad->timestamp_end;

645
  g_queue_push_tail (pad->pagebuffers, buffer);
646
647
648
  GST_LOG_OBJECT (pad->collect.pad, GST_GP_FORMAT
      " queued buffer page %p (gp time %"
      GST_TIME_FORMAT ", timestamp %" GST_TIME_FORMAT
649
      "), %d page buffers queued", GST_GP_CAST (ogg_page_granulepos (page)),
650
      buffer, GST_TIME_ARGS (GST_BUFFER_OFFSET (buffer)),
651
652
      GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
      g_queue_get_length (pad->pagebuffers));
Wim Taymans's avatar
Wim Taymans committed
653

654
655
656
657
  while (gst_ogg_mux_dequeue_page (mux, &ret)) {
    if (ret != GST_FLOW_OK)
      break;
  }
Wim Taymans's avatar
Wim Taymans committed
658
659

  return ret;
Wim Taymans's avatar
Wim Taymans committed
660
661
662
}

/*
663
664
665
666
667
668
669
 * Given two pads, compare the buffers queued on it.
 * Returns:
 *  0 if they have an equal priority
 * -1 if the first is better
 *  1 if the second is better
 * Priority decided by: a) validity, b) older timestamp, c) smaller number
 * of muxed pages
Wim Taymans's avatar
Wim Taymans committed
670
671
 */
static gint
672
673
gst_ogg_mux_compare_pads (GstOggMux * ogg_mux, GstOggPadData * first,
    GstOggPadData * second)
Wim Taymans's avatar
Wim Taymans committed
674
{
675
  guint64 firsttime, secondtime;
Wim Taymans's avatar
Wim Taymans committed
676

677
678
  /* if the first pad doesn't contain anything or is even NULL, return
   * the second pad as best candidate and vice versa */
679
  if (first == NULL || (first->buffer == NULL && first->next_buffer == NULL))
Wim Taymans's avatar
Wim Taymans committed
680
    return 1;
681
  if (second == NULL || (second->buffer == NULL && second->next_buffer == NULL))
Wim Taymans's avatar
Wim Taymans committed
682
683
    return -1;

684
  /* no timestamp on first buffer, it must go first */
685
686
687
688
  if (first->buffer)
    firsttime = GST_BUFFER_TIMESTAMP (first->buffer);
  else
    firsttime = GST_BUFFER_TIMESTAMP (first->next_buffer);
689
  if (firsttime == GST_CLOCK_TIME_NONE)
Wim Taymans's avatar
Wim Taymans committed
690
691
    return -1;

692
  /* no timestamp on second buffer, it must go first */
693
694
695
696
  if (second->buffer)
    secondtime = GST_BUFFER_TIMESTAMP (second->buffer);
  else
    secondtime = GST_BUFFER_TIMESTAMP (second->next_buffer);
697
  if (secondtime == GST_CLOCK_TIME_NONE)
Wim Taymans's avatar
Wim Taymans committed
698
699
    return 1;

700
701
  /* first buffer has higher timestamp, second one should go first */
  if (secondtime < firsttime)
Wim Taymans's avatar
Wim Taymans committed
702
    return 1;
703
704
  /* second buffer has higher timestamp, first one should go first */
  else if (secondtime > firsttime)
Wim Taymans's avatar
Wim Taymans committed
705
706
707
708
    return -1;
  else {
    /* buffers with equal timestamps, prefer the pad that has the
     * least number of pages muxed */
709
    if (second->pageno < first->pageno)
Wim Taymans's avatar
Wim Taymans committed
710
      return 1;
711
    else if (second->pageno > first->pageno)
Wim Taymans's avatar
Wim Taymans committed
712
713
714
715
716
717
718
      return -1;
  }

  /* same priority if all of the above failed */
  return 0;
}

719
720
721
722
723
724
725
726
727
728
729
730
731
/* make sure at least one buffer is queued on all pads, two if possible
 * 
 * if pad->buffer == NULL, pad->next_buffer !=  NULL, then
 *   we do not know if the buffer is the last or not
 * if pad->buffer != NULL, pad->next_buffer != NULL, then
 *   pad->buffer is not the last buffer for the pad
 * if pad->buffer != NULL, pad->next_buffer == NULL, then
 *   pad->buffer if the last buffer for the pad
 * 
 * returns a pointer to an oggpad that holds the best buffer, or
 * NULL when no pad was usable. "best" means the buffer marked
 * with the lowest timestamp. If best->buffer == NULL then nothing
 * should be done until more data arrives */
732
static GstOggPadData *
Wim Taymans's avatar
Wim Taymans committed
733
gst_ogg_mux_queue_pads (GstOggMux * ogg_mux)
Wim Taymans's avatar
Wim Taymans committed
734
{
735
  GstOggPadData *bestpad = NULL, *still_hungry = NULL;
Wim Taymans's avatar
Wim Taymans committed
736
737
738
  GSList *walk;

  /* try to make sure we have a buffer from each usable pad first */
Wim Taymans's avatar
Wim Taymans committed
739
  walk = ogg_mux->collect->data;
Wim Taymans's avatar
Wim Taymans committed
740
  while (walk) {
741
    GstOggPadData *pad;
Wim Taymans's avatar
Wim Taymans committed
742
    GstCollectData *data;
Wim Taymans's avatar
Wim Taymans committed
743

Wim Taymans's avatar
Wim Taymans committed
744
    data = (GstCollectData *) walk->data;
745
    pad = (GstOggPadData *) data;
Wim Taymans's avatar
Wim Taymans committed
746

Wim Taymans's avatar
Wim Taymans committed
747
748
    walk = g_slist_next (walk);

749
    GST_LOG_OBJECT (data->pad, "looking at pad for buffer");
750

Wim Taymans's avatar
Wim Taymans committed
751
    /* try to get a new buffer for this pad if needed and possible */
Wim Taymans's avatar
Wim Taymans committed
752
753
754
    if (pad->buffer == NULL) {
      GstBuffer *buf;
      gboolean incaps;
Wim Taymans's avatar
Wim Taymans committed
755

756
757
758
759
760
761
762
763
      /* shift the buffer along if needed (it's okay if next_buffer is NULL) */
      if (pad->buffer == NULL) {
        GST_LOG_OBJECT (data->pad, "shifting buffer %" GST_PTR_FORMAT,
            pad->next_buffer);
        pad->buffer = pad->next_buffer;
        pad->next_buffer = NULL;
      }

764
      buf = gst_collect_pads_pop (ogg_mux->collect, data);
765
      GST_LOG_OBJECT (data->pad, "popped buffer %" GST_PTR_FORMAT, buf);
Wim Taymans's avatar
Wim Taymans committed
766

767
768
      /* On EOS we get a NULL buffer */
      if (buf != NULL) {
769
770
771
772
        if (ogg_mux->delta_pad == NULL &&
            GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_DELTA_UNIT))
          ogg_mux->delta_pad = pad;

773
774
775
776
777
        incaps = GST_BUFFER_FLAG_IS_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
        /* if we need headers */
        if (pad->state == GST_OGG_PAD_STATE_CONTROL) {
          /* and we have one */
          if (incaps) {
778
779
            GST_DEBUG_OBJECT (ogg_mux,
                "got incaps buffer in control state, ignoring");
780
781
782
783
            /* just ignore */
            gst_buffer_unref (buf);
            buf = NULL;
          } else {
784
            GST_DEBUG_OBJECT (ogg_mux,
Vincent Penquerc'h's avatar
Vincent Penquerc'h committed
785
                "got data buffer in control state, switching to data mode");
786
787
788
            /* this is a data buffer so switch to data state */
            pad->state = GST_OGG_PAD_STATE_DATA;
          }
Wim Taymans's avatar
Wim Taymans committed
789
        }
790
      } else {
791
        GST_DEBUG_OBJECT (data->pad, "EOS on pad");
792
793
794
795
        if (!pad->eos) {
          ogg_page page;
          GstFlowReturn ret;

796
797
798
          /* it's no longer active */
          ogg_mux->active_pads--;

799
800
801
802
803
804
805
806
807
808
809
810
811
          /* Just gone to EOS. Flush existing page(s) */
          pad->eos = TRUE;

          while (ogg_stream_flush (&pad->stream, &page)) {
            /* Place page into the per-pad queue */
            ret = gst_ogg_mux_pad_queue_page (ogg_mux, pad, &page,
                pad->first_delta);
            /* increment the page number counter */
            pad->pageno++;
            /* mark other pages as delta */
            pad->first_delta = TRUE;
          }
        }
Wim Taymans's avatar
Wim Taymans committed
812
      }
813

814
      pad->next_buffer = buf;
Wim Taymans's avatar
Wim Taymans committed
815
    }
Wim Taymans's avatar
Wim Taymans committed
816
817
818

    /* we should have a buffer now, see if it is the best pad to
     * pull on */
819
    if (pad->buffer || pad->next_buffer) {
820
      if (gst_ogg_mux_compare_pads (ogg_mux, bestpad, pad) > 0) {
821
822
823
        GST_LOG_OBJECT (data->pad,
            "new best pad, with buffers %" GST_PTR_FORMAT
            " and %" GST_PTR_FORMAT, pad->buffer, pad->next_buffer);
824

Wim Taymans's avatar
Wim Taymans committed
825
        bestpad = pad;
826
      }
827
    } else if (!pad->eos) {
828
      GST_LOG_OBJECT (data->pad, "hungry pad");
829
      still_hungry = pad;
Wim Taymans's avatar
Wim Taymans committed
830
831
    }
  }
832
833
834
835
836
837

  if (still_hungry)
    /* drop back into collectpads... */
    return still_hungry;
  else
    return bestpad;
Wim Taymans's avatar
Wim Taymans committed
838
839
}

840
static GList *
841
gst_ogg_mux_get_headers (GstOggPadData * pad)
842
843
844
{
  GList *res = NULL;
  GstStructure *structure;
845
  GstCaps *caps;
Wim Taymans's avatar
Wim Taymans committed
846
  GstPad *thepad;
847

Wim Taymans's avatar
Wim Taymans committed
848
  thepad = pad->collect.pad;
849

850
  GST_LOG_OBJECT (thepad, "getting headers");
Wim Taymans's avatar
Wim Taymans committed
851
852

  caps = gst_pad_get_negotiated_caps (thepad);
853
854
855
856
  if (caps != NULL) {
    const GValue *streamheader;

    structure = gst_caps_get_structure (caps, 0);
857
858
859
860
861
862
    streamheader = gst_structure_get_value (structure, "streamheader");
    if (streamheader != NULL) {
      GST_LOG_OBJECT (thepad, "got header");
      if (G_VALUE_TYPE (streamheader) == GST_TYPE_ARRAY) {
        GArray *bufarr = g_value_peek_pointer (streamheader);
        gint i;
863

864
        GST_LOG_OBJECT (thepad, "got fixed list");
Wim Taymans's avatar
Wim Taymans committed
865

866
867
        for (i = 0; i < bufarr->len; i++) {
          GValue *bufval = &g_array_index (bufarr, GValue, i);
868

869
870
871
          GST_LOG_OBJECT (thepad, "item %d", i);
          if (G_VALUE_TYPE (bufval) == GST_TYPE_BUFFER) {
            GstBuffer *buf = g_value_peek_pointer (bufval);
872

873
            GST_LOG_OBJECT (thepad, "adding item %d to header list", i);
Wim Taymans's avatar
Wim Taymans committed
874

875
876
            gst_buffer_ref (buf);
            res = g_list_append (res, buf);
877
878
          }
        }
Wim Taymans's avatar
Wim Taymans committed
879
      } else {
880
        GST_LOG_OBJECT (thepad, "streamheader is not fixed list");
881
      }
882
883
884
885

      /* Start a new page for every CMML buffer */
      if (gst_structure_has_name (structure, "text/x-cmml"))
        pad->always_flush_page = TRUE;
886
887
888
889
    } else if (gst_structure_has_name (structure, "video/x-dirac")) {
      res = g_list_append (res, pad->buffer);
      pad->buffer = pad->next_buffer;
      pad->next_buffer = NULL;
890
      pad->always_flush_page = TRUE;
891
892
    } else {
      GST_LOG_OBJECT (thepad, "caps don't have streamheader");
893
    }
894
    gst_caps_unref (caps);
Wim Taymans's avatar
Wim Taymans committed
895
  } else {
896
    GST_LOG_OBJECT (thepad, "got empty caps as negotiated format");
897
898
899
900
  }
  return res;
}

Wim Taymans's avatar
Wim Taymans committed
901
static GstCaps *
902
903
gst_ogg_mux_set_header_on_caps (GstCaps * caps, GList * buffers)
{
Wim Taymans's avatar
Wim Taymans committed
904
  GstStructure *structure;
905
  GValue array = { 0 };
906
907
  GList *walk = buffers;

Wim Taymans's avatar
Wim Taymans committed
908
909
910
911
  caps = gst_caps_make_writable (caps);

  structure = gst_caps_get_structure (caps, 0);

912
  /* put buffers in a fixed list */
913
  g_value_init (&array, GST_TYPE_ARRAY);
914
915
916

  while (walk) {
    GstBuffer *buf = GST_BUFFER (walk->data);
917
    GstBuffer *copy;
918
919
920
921
922
    GValue value = { 0 };

    walk = walk->next;

    /* mark buffer */
923
    GST_LOG ("Setting IN_CAPS on buffer of length %d", GST_BUFFER_SIZE (buf));
924
    GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_IN_CAPS);
925
926

    g_value_init (&value, GST_TYPE_BUFFER);
927
928
929
    copy = gst_buffer_copy (buf);
    gst_value_set_buffer (&value, copy);
    gst_buffer_unref (copy);
930
    gst_value_array_append_value (&array, &value);
931
932
    g_value_unset (&value);
  }
933
934
  gst_structure_set_value (structure, "streamheader", &array);
  g_value_unset (&array);
Wim Taymans's avatar
Wim Taymans committed
935
936

  return caps;
937
938
}

939
/*
940
941
942
943
944
945
946
 * For each pad we need to write out one (small) header in one
 * page that allows decoders to identify the type of the stream.
 * After that we need to write out all extra info for the decoders.
 * In the case of a codec that also needs data as configuration, we can
 * find that info in the streamcaps. 
 * After writing the headers we must start a new page for the data.
 */
Wim Taymans's avatar
Wim Taymans committed
947
static GstFlowReturn
948
949
950
951
952
gst_ogg_mux_send_headers (GstOggMux * mux)
{
  GSList *walk;
  GList *hbufs, *hwalk;
  GstCaps *caps;
Wim Taymans's avatar
Wim Taymans committed
953
  GstFlowReturn ret;
954
955

  hbufs = NULL;
Wim Taymans's avatar
Wim Taymans committed
956
  ret = GST_FLOW_OK;
957

958
  GST_LOG_OBJECT (mux, "collecting headers");
959

Wim Taymans's avatar
Wim Taymans committed
960
  walk = mux->collect->data;
961
  while (walk) {
962
    GstOggPadData *pad;
Wim Taymans's avatar
Wim Taymans committed
963
    GstPad *thepad;
964

965
    pad = (GstOggPadData *) walk->data;
Wim Taymans's avatar
Wim Taymans committed
966
    thepad = pad->collect.pad;
967

Wim Taymans's avatar
Wim Taymans committed
968
969
    walk = g_slist_next (walk);

970
    GST_LOG_OBJECT (mux, "looking at pad %s:%s", GST_DEBUG_PAD_NAME (thepad));
971
972

    /* if the pad has no buffer, we don't care */
973
    if (pad->buffer == NULL && pad->next_buffer == NULL)
974
975
976
977
978
979
      continue;

    /* now figure out the headers */
    pad->headers = gst_ogg_mux_get_headers (pad);
  }

980
  GST_LOG_OBJECT (mux, "creating BOS pages");
Wim Taymans's avatar
Wim Taymans committed
981
  walk = mux->collect->data;
982
  while (walk) {
983
    GstOggPadData *pad;
984
985
986
    GstBuffer *buf;
    ogg_packet packet;
    ogg_page page;
Wim Taymans's avatar
Wim Taymans committed
987
    GstPad *thepad;
988
989
990
    GstCaps *caps;
    GstStructure *structure;
    GstBuffer *hbuf;
Wim Taymans's avatar
Wim Taymans committed
991

992
    pad = (GstOggPadData *) walk->data;
Wim Taymans's avatar
Wim Taymans committed
993
    thepad = pad->collect.pad;
994
995
    caps = gst_pad_get_negotiated_caps (thepad);
    structure = gst_caps_get_structure (caps, 0);
996
997
998
999
1000

    walk = walk->next;

    pad->packetno = 0;

1001
    GST_LOG_OBJECT (thepad, "looping over headers");
1002
1003
1004
1005

    if (pad->headers) {
      buf = GST_BUFFER (pad->headers->data);
      pad->headers = g_list_remove (pad->headers, buf);
1006
    } else if (pad->buffer) {
1007
1008
      buf = pad->buffer;
      gst_buffer_ref (buf);
1009
1010
1011
    } else if (pad->next_buffer) {
      buf = pad->next_buffer;
      gst_buffer_ref (buf);
1012
    } else {
1013
      /* fixme -- should be caught in the previous list traversal. */
1014
      GST_OBJECT_LOCK (pad);
1015
1016
      g_critical ("No headers or buffers on pad %s:%s",
          GST_DEBUG_PAD_NAME (pad));
1017
      GST_OBJECT_UNLOCK (pad);
1018
      continue;
1019
1020
1021
1022
1023
1024
    }

    /* create a packet from the buffer */
    packet.packet = GST_BUFFER_DATA (buf);
    packet.bytes = GST_BUFFER_SIZE (buf);
    packet.granulepos = GST_BUFFER_OFFSET_END (buf);
1025
1026
    if (packet.granulepos == -1)
      packet.granulepos = 0;
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
    /* mark BOS and packet number */
    packet.b_o_s = (pad->packetno == 0);
    packet.packetno = pad->packetno++;
    /* mark EOS */
    packet.e_o_s = 0;

    /* swap the packet in */
    ogg_stream_packetin (&pad->stream, &packet);
    gst_buffer_unref (buf);

1037
1038
1039
1040
1041
    GST_LOG_OBJECT (thepad, "flushing out BOS page");
    if (!ogg_stream_flush (&pad->stream, &page))
      g_critical ("Could not flush BOS page");

    hbuf = gst_ogg_mux_buffer_from_page (mux, &page, FALSE);
1042

1043
1044
1045
    GST_LOG_OBJECT (mux, "swapped out page with mime type %s",
        gst_structure_get_name (structure));

1046
    /* quick hack: put Theora, VP8 and Dirac video pages at the front.
1047
     * Ideally, we would have a settable enum for which Ogg
1048
1049
1050
1051
1052
1053
1054
1055
1056
     * profile we work with, and order based on that.
     * (FIXME: if there is more than one video stream, shouldn't we only put
     * one's BOS into the first page, followed by an audio stream's BOS, and
     * only then followed by the remaining video and audio streams?) */
    if (gst_structure_has_name (structure, "video/x-theora")) {
      GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "Theora");
      hbufs = g_list_prepend (hbufs, hbuf);
    } else if (gst_structure_has_name (structure, "video/x-dirac")) {
      GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "Dirac");
1057
      hbufs = g_list_prepend (hbufs, hbuf);
1058
      pad->always_flush_page = TRUE;
1059
1060
1061
    } else if (gst_structure_has_name (structure, "video/x-vp8")) {
      GST_DEBUG_OBJECT (thepad, "putting %s page at the front", "VP8");
      hbufs = g_list_prepend (hbufs, hbuf);
1062
    } else {
1063
1064
      hbufs = g_list_append (hbufs, hbuf);
    }
1065
    gst_caps_unref (caps);
1066
1067
  }

1068
  GST_LOG_OBJECT (mux, "creating next headers");
Wim Taymans's avatar
Wim Taymans committed
1069
  walk = mux->collect->data;
1070
  while (walk) {
1071
    GstOggPadData *pad;
Wim Taymans's avatar
Wim Taymans committed
1072
1073
    GstPad *thepad;

1074
    pad = (GstOggPadData *) walk->data;
Wim Taymans's avatar
Wim Taymans committed
1075
    thepad = pad->collect.pad;
1076
1077
1078

    walk = walk->next;

1079
1080
    GST_LOG_OBJECT (mux, "looping over headers for pad %s:%s",
        GST_DEBUG_PAD_NAME (thepad));
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093

    hwalk = pad->headers;
    while (hwalk) {
      GstBuffer *buf = GST_BUFFER (hwalk->data);
      ogg_packet packet;
      ogg_page page;

      hwalk = hwalk->next;

      /* create a packet from the buffer */
      packet.packet = GST_BUFFER_DATA (buf);
      packet.bytes = GST_BUFFER_SIZE (buf);
      packet.granulepos = GST_BUFFER_OFFSET_END (buf);
1094
1095
      if (packet.granulepos == -1)
        packet.granulepos = 0;
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
      /* mark BOS and packet number */
      packet.b_o_s = (pad->packetno == 0);
      packet.packetno = pad->packetno++;
      /* mark EOS */
      packet.e_o_s = 0;

      /* swap the packet in */
      ogg_stream_packetin (&pad->stream, &packet);
      gst_buffer_unref (buf);

      /* if last header, flush page */
      if (hwalk == NULL) {
1108
        GST_LOG_OBJECT (mux,
Tim-Philipp Müller's avatar
Tim-Philipp Müller committed
1109
            "flushing page as packet %" G_GUINT64_FORMAT " is first or "
Vincent Penquerc'h's avatar
Vincent Penquerc'h committed
1110
            "last packet", packet.packetno);
1111
        while (ogg_stream_flush (&pad->stream, &page)) {
1112
          GstBuffer *