gsttaglibmux.c 11.2 KB
Newer Older
1
2
3
4
/* GStreamer taglib-based muxer base class
 * Copyright (C) 2006 Christophe Fergeau  <teuf@gnome.org>
 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <gst/gsttagsetter.h>
27
#include <gst/tag/tag.h>
28
29
30
31
32
#include "gsttaglib.h"

GST_DEBUG_CATEGORY_STATIC (gst_tag_lib_mux_debug);
#define GST_CAT_DEFAULT gst_tag_lib_mux_debug

33
34
35
36
static GstStaticPadTemplate gst_tag_lib_mux_sink_template =
GST_STATIC_PAD_TEMPLATE ("sink",
    GST_PAD_SINK,
    GST_PAD_ALWAYS,
37
    GST_STATIC_CAPS ("ANY"));
38

39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
static void
gst_tag_lib_mux_iface_init (GType taglib_type)
{
  static const GInterfaceInfo tag_setter_info = {
    NULL,
    NULL,
    NULL
  };

  g_type_add_interface_static (taglib_type, GST_TYPE_TAG_SETTER,
      &tag_setter_info);
}

GST_BOILERPLATE_FULL (GstTagLibMux, gst_tag_lib_mux,
    GstElement, GST_TYPE_ELEMENT, gst_tag_lib_mux_iface_init);


static GstStateChangeReturn
gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition);
static GstFlowReturn gst_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer);
static gboolean gst_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event);

static void
gst_tag_lib_mux_finalize (GObject * obj)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
64
  GstTagLibMux *mux = GST_TAG_LIB_MUX (obj);
65

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
66
67
68
  if (mux->newsegment_ev) {
    gst_event_unref (mux->newsegment_ev);
    mux->newsegment_ev = NULL;
69
70
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
71
72
73
  if (mux->event_tags) {
    gst_tag_list_free (mux->event_tags);
    mux->event_tags = NULL;
74
  }
75

76
77
  G_OBJECT_CLASS (parent_class)->finalize (obj);
}
78
79
80
81
82
83
84
85

static void
gst_tag_lib_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 (&gst_tag_lib_mux_sink_template));
86
87
88

  GST_DEBUG_CATEGORY_INIT (gst_tag_lib_mux_debug, "taglibmux", 0,
      "taglib-based muxer");
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
}

static void
gst_tag_lib_mux_class_init (GstTagLibMuxClass * klass)
{
  GObjectClass *gobject_class;
  GstElementClass *gstelement_class;

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

  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_tag_lib_mux_finalize);
  gstelement_class->change_state =
      GST_DEBUG_FUNCPTR (gst_tag_lib_mux_change_state);
}

static void
106
gst_tag_lib_mux_init (GstTagLibMux * mux, GstTagLibMuxClass * mux_class)
107
{
108
109
  GstElementClass *element_klass = GST_ELEMENT_CLASS (mux_class);
  GstPadTemplate *tmpl;
110
111

  /* pad through which data comes in to the element */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
112
  mux->sinkpad =
113
      gst_pad_new_from_static_template (&gst_tag_lib_mux_sink_template, "sink");
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
114
  gst_pad_set_chain_function (mux->sinkpad,
115
      GST_DEBUG_FUNCPTR (gst_tag_lib_mux_chain));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
116
  gst_pad_set_event_function (mux->sinkpad,
117
      GST_DEBUG_FUNCPTR (gst_tag_lib_mux_sink_event));
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
118
  gst_element_add_pad (GST_ELEMENT (mux), mux->sinkpad);
119
120

  /* pad through which data goes out of the element */
121
122
123
124
125
126
  tmpl = gst_element_class_get_pad_template (element_klass, "src");
  if (tmpl) {
    mux->srcpad = gst_pad_new_from_template (tmpl, "src");
    gst_pad_use_fixed_caps (mux->srcpad);
    gst_pad_set_caps (mux->srcpad, gst_pad_template_get_caps (tmpl));
    gst_element_add_pad (GST_ELEMENT (mux), mux->srcpad);
127
  }
128

129
  mux->render_tag = TRUE;
130
131
132
}

static GstBuffer *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
133
gst_tag_lib_mux_render_tag (GstTagLibMux * mux)
134
{
135
  GstTagLibMuxClass *klass;
136
  GstBuffer *buffer;
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
137
  GstTagSetter *tagsetter = GST_TAG_SETTER (mux);
138
  const GstTagList *tagsetter_tags;
139
140
141
  GstTagList *taglist;
  GstEvent *event;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
142
143
  if (mux->event_tags != NULL) {
    taglist = gst_tag_list_copy (mux->event_tags);
144
145
146
147
  } else {
    taglist = gst_tag_list_new ();
  }

148
149
150
151
152
  tagsetter_tags = gst_tag_setter_get_tag_list (tagsetter);
  if (tagsetter_tags) {
    GstTagMergeMode merge_mode;

    merge_mode = gst_tag_setter_get_tag_merge_mode (tagsetter);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
153
154
155
    GST_LOG_OBJECT (mux, "merging tags, merge mode = %d", merge_mode);
    GST_LOG_OBJECT (mux, "event tags: %" GST_PTR_FORMAT, taglist);
    GST_LOG_OBJECT (mux, "set   tags: %" GST_PTR_FORMAT, tagsetter_tags);
156
    gst_tag_list_insert (taglist, tagsetter_tags, merge_mode);
157
158
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
159
  GST_LOG_OBJECT (mux, "final tags: %" GST_PTR_FORMAT, taglist);
160

161
  klass = GST_TAG_LIB_MUX_CLASS (G_OBJECT_GET_CLASS (mux));
162

163
164
  if (klass->render_tag == NULL)
    goto no_vfunc;
165

166
  buffer = klass->render_tag (mux, taglist);
167

168
169
170
171
172
  if (buffer == NULL)
    goto render_error;

  mux->tag_size = GST_BUFFER_SIZE (buffer);
  GST_LOG_OBJECT (mux, "tag size = %d bytes", mux->tag_size);
173
174
175

  /* Send newsegment event from byte position 0, so the tag really gets
   * written to the start of the file, independent of the upstream segment */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
176
  gst_pad_push_event (mux->srcpad,
177
      gst_event_new_new_segment (FALSE, 1.0, GST_FORMAT_BYTES, 0, -1, 0));
178
179
180
181

  /* Send an event about the new tags to downstream elements */
  /* gst_event_new_tag takes ownership of the list, so no need to unref it */
  event = gst_event_new_tag (taglist);
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
182
  gst_pad_push_event (mux->srcpad, event);
183

184
185
  GST_BUFFER_OFFSET (buffer) = 0;

186
  return buffer;
187
188
189

no_vfunc:
  {
190
191
    GST_ERROR_OBJECT (mux, "Subclass does not implement render_tag vfunc!");
    return NULL;
192
193
194
195
  }

render_error:
  {
196
197
    GST_ERROR_OBJECT (mux, "Failed to render tag");
    return NULL;
198
  }
199
200
}

201
static GstEvent *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
202
gst_tag_lib_mux_adjust_event_offsets (GstTagLibMux * mux,
203
204
205
206
207
208
209
210
211
212
213
    const GstEvent * newsegment_event)
{
  GstFormat format;
  gint64 start, stop, cur;

  gst_event_parse_new_segment ((GstEvent *) newsegment_event, NULL, NULL,
      &format, &start, &stop, &cur);

  g_assert (format == GST_FORMAT_BYTES);

  if (start != -1)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
214
    start += mux->tag_size;
215
  if (stop != -1)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
216
    stop += mux->tag_size;
217
  if (cur != -1)
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
218
    cur += mux->tag_size;
219

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
220
  GST_DEBUG_OBJECT (mux, "adjusting newsegment event offsets to start=%"
221
      G_GINT64_FORMAT ", stop=%" G_GINT64_FORMAT ", cur=%" G_GINT64_FORMAT
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
222
      " (delta = +%u)", start, stop, cur, mux->tag_size);
223
224
225

  return gst_event_new_new_segment (TRUE, 1.0, format, start, stop, cur);
}
226
227
228
229

static GstFlowReturn
gst_tag_lib_mux_chain (GstPad * pad, GstBuffer * buffer)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
230
  GstTagLibMux *mux = GST_TAG_LIB_MUX (GST_OBJECT_PARENT (pad));
231

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
232
  if (mux->render_tag) {
233
    GstFlowReturn ret;
234
    GstBuffer *tag_buffer;
235

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
236
    GST_INFO_OBJECT (mux, "Adding tags to stream");
237
238
239
240
    tag_buffer = gst_tag_lib_mux_render_tag (mux);
    if (tag_buffer == NULL)
      goto no_tag_buffer;
    ret = gst_pad_push (mux->srcpad, tag_buffer);
241
    if (ret != GST_FLOW_OK) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
242
      GST_DEBUG_OBJECT (mux, "flow: %s", gst_flow_get_name (ret));
243
244
245
      gst_buffer_unref (buffer);
      return ret;
    }
246
247

    /* Now send the cached newsegment event that we got from upstream */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
248
249
250
251
252
253
    if (mux->newsegment_ev) {
      GST_DEBUG_OBJECT (mux, "sending cached newsegment event");
      gst_pad_push_event (mux->srcpad,
          gst_tag_lib_mux_adjust_event_offsets (mux, mux->newsegment_ev));
      gst_event_unref (mux->newsegment_ev);
      mux->newsegment_ev = NULL;
254
255
256
257
    } else {
      /* upstream sent no newsegment event or only one in a non-BYTE format */
    }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
258
    mux->render_tag = FALSE;
259
260
  }

261
262
  buffer = gst_buffer_make_metadata_writable (buffer);

263
  if (GST_BUFFER_OFFSET (buffer) != GST_BUFFER_OFFSET_NONE) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
264
    GST_LOG_OBJECT (mux, "Adjusting buffer offset from %" G_GINT64_FORMAT
265
        " to %" G_GINT64_FORMAT, GST_BUFFER_OFFSET (buffer),
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
266
267
        GST_BUFFER_OFFSET (buffer) + mux->tag_size);
    GST_BUFFER_OFFSET (buffer) += mux->tag_size;
268
269
  }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
270
271
  gst_buffer_set_caps (buffer, GST_PAD_CAPS (mux->srcpad));
  return gst_pad_push (mux->srcpad, buffer);
272
273
274
275
276
277
278

/* ERRORS */
no_tag_buffer:
  {
    GST_ELEMENT_ERROR (mux, LIBRARY, ENCODE, (NULL), (NULL));
    return GST_FLOW_ERROR;
  }
279
280
281
282
283
}

static gboolean
gst_tag_lib_mux_sink_event (GstPad * pad, GstEvent * event)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
284
  GstTagLibMux *mux;
285
286
  gboolean result;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
287
  mux = GST_TAG_LIB_MUX (gst_pad_get_parent (pad));
288
289
290
  result = FALSE;

  switch (GST_EVENT_TYPE (event)) {
291
    case GST_EVENT_TAG:{
292
293
294
      GstTagList *tags;

      gst_event_parse_tag (event, &tags);
295

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
296
      GST_INFO_OBJECT (mux, "Got tag event: %" GST_PTR_FORMAT, tags);
297

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
298
299
      if (mux->event_tags != NULL) {
        gst_tag_list_insert (mux->event_tags, tags, GST_TAG_MERGE_REPLACE);
300
      } else {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
301
        mux->event_tags = gst_tag_list_copy (tags);
302
      }
303

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
304
305
      GST_INFO_OBJECT (mux, "Event tags are now: %" GST_PTR_FORMAT,
          mux->event_tags);
306
307

      /* just drop the event, we'll push a new tag event in render_tag */
308
309
310
311
      gst_event_unref (event);
      result = TRUE;
      break;
    }
312
313
314
315
    case GST_EVENT_NEWSEGMENT:{
      GstFormat fmt;

      gst_event_parse_new_segment (event, NULL, NULL, &fmt, NULL, NULL, NULL);
316

317
      if (fmt != GST_FORMAT_BYTES) {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
318
        GST_WARNING_OBJECT (mux, "dropping newsegment event in %s format",
319
            gst_format_get_name (fmt));
320
        gst_event_unref (event);
321
322
323
        break;
      }

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
324
      if (mux->render_tag) {
325
326
327
328
329
        /* we have not rendered the tag yet, which means that we don't know
         * how large it is going to be yet, so we can't adjust the offsets
         * here at this point and need to cache the newsegment event for now
         * (also, there could be tag events coming after this newsegment event
         *  and before the first buffer). */
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
330
331
332
        if (mux->newsegment_ev) {
          GST_WARNING_OBJECT (mux, "discarding old cached newsegment event");
          gst_event_unref (mux->newsegment_ev);
333
        }
334

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
335
336
        GST_LOG_OBJECT (mux, "caching newsegment event for later");
        mux->newsegment_ev = event;
337
      } else {
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
338
339
340
        GST_DEBUG_OBJECT (mux, "got newsegment event, adjusting offsets");
        gst_pad_push_event (mux->srcpad,
            gst_tag_lib_mux_adjust_event_offsets (mux, event));
341
        gst_event_unref (event);
342
      }
343
344
      event = NULL;
      result = TRUE;
345
      break;
346
    }
347
348
349
350
    default:
      result = gst_pad_event_default (pad, event);
      break;
  }
351

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
352
  gst_object_unref (mux);
353
354
355
356
357
358
359
360

  return result;
}


static GstStateChangeReturn
gst_tag_lib_mux_change_state (GstElement * element, GstStateChange transition)
{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
361
  GstTagLibMux *mux;
362
363
  GstStateChangeReturn result;

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
364
  mux = GST_TAG_LIB_MUX (element);
365
366
367
368
369
370
371

  result = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
  if (result != GST_STATE_CHANGE_SUCCESS) {
    return result;
  }

  switch (transition) {
372
    case GST_STATE_CHANGE_PAUSED_TO_READY:{
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
373
374
375
      if (mux->newsegment_ev) {
        gst_event_unref (mux->newsegment_ev);
        mux->newsegment_ev = NULL;
376
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
377
378
379
      if (mux->event_tags) {
        gst_tag_list_free (mux->event_tags);
        mux->event_tags = NULL;
380
      }
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
381
382
      mux->tag_size = 0;
      mux->render_tag = TRUE;
383
      break;
384
    }
385
386
387
388
389
390
    default:
      break;
  }

  return result;
}