~ubuntu-branches/ubuntu/natty/gst-entrans/natty

« back to all changes in this revision

Viewing changes to gst/entrans/gstbufferjoin.c

  • Committer: Bazaar Package Importer
  • Author(s): Jonas Smedegaard
  • Date: 2010-09-13 19:49:29 UTC
  • Revision ID: james.westby@ubuntu.com-20100913194929-qz90a14xyxln9yfz
Tags: upstream-0.10.2
ImportĀ upstreamĀ versionĀ 0.10.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* GStreamer Element
 
2
 * Copyright (C) 2008 Mark Nauwelaerts <mnauw@users.sourceforge.net>
 
3
 *
 
4
 * This library is free software; you can redistribute it and/or
 
5
 * modify it under the terms of the GNU Library General Public
 
6
 * License as published by the Free Software Foundation; either
 
7
 * version 2 of the License, or (at your option) any later version.
 
8
 *
 
9
 * This library is distributed in the hope that it will be useful,
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
 * Library General Public License for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU Library General Public
 
15
 * License along with this library; if not, write to the
 
16
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
17
 * Boston, MA 02110-1307, USA.
 
18
 */
 
19
 
 
20
/**
 
21
 * SECTION:element-bufferjoin
 
22
 *
 
23
 * <refsect2>
 
24
 * <para>
 
25
 * Joins consecutive buffers that have identical timestamps
 
26
 * (valid durations will be aggregated).
 
27
 *
 
28
 * </para>
 
29
 * </refsect2>
 
30
 *
 
31
 */
 
32
 
 
33
#ifdef HAVE_CONFIG_H
 
34
#include "config.h"
 
35
#endif
 
36
 
 
37
#include <gst/gst.h>
 
38
#include <gst/base/gstadapter.h>
 
39
 
 
40
#define GST_TYPE_BUFFER_JOIN \
 
41
  gst_buffer_join_get_type ()
 
42
#define GST_BUFFER_JOIN(obj) \
 
43
  (G_TYPE_CHECK_INSTANCE_CAST((obj), GST_TYPE_BUFFER_JOIN, GstBufferJoin))
 
44
#define GST_BUFFER_JOIN_CLASS(klass) \
 
45
  (G_TYPE_CHECK_CLASS_CAST((klass), GST_TYPE_BUFFER_JOIN, GstBufferJoinClass))
 
46
#define GST_IS_BUFFER_JOIN(obj) \
 
47
  (G_TYPE_CHECK_INSTANCE_TYPE((obj), GST_TYPE_BUFFER_JOIN))
 
48
#define GST_IS_BUFFER_JOIN_CLASS(klass) \
 
49
  (G_TYPE_CHECK_CLASS_TYPE((klass), GST_TYPE_BUFFER_JOIN))
 
50
 
 
51
typedef struct _GstBufferJoin GstBufferJoin;
 
52
typedef struct _GstBufferJoinClass GstBufferJoinClass;
 
53
 
 
54
struct _GstBufferJoin
 
55
{
 
56
  GstElement parent;
 
57
 
 
58
  /* pads */
 
59
  GstPad *sinkpad, *srcpad;
 
60
 
 
61
  /* property */
 
62
  gboolean join_none;
 
63
  gboolean join_flags;
 
64
 
 
65
  GstBuffer *buffer;
 
66
};
 
67
 
 
68
struct _GstBufferJoinClass
 
69
{
 
70
  GstElementClass parent_class;
 
71
};
 
72
 
 
73
GST_DEBUG_CATEGORY_STATIC (buffer_join_debug);
 
74
#define GST_CAT_DEFAULT buffer_join_debug
 
75
 
 
76
static GstElementDetails gst_buffer_join_details =
 
77
GST_ELEMENT_DETAILS ("Buffer Join",
 
78
    "Generic",
 
79
    "Joins consecutive buffers with identical timestamps",
 
80
    "Mark Nauwelaerts <mnauw@users.sourceforge.net>");
 
81
 
 
82
static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
 
83
    GST_PAD_SINK,
 
84
    GST_PAD_ALWAYS,
 
85
    GST_STATIC_CAPS_ANY);
 
86
 
 
87
static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
 
88
    GST_PAD_SRC,
 
89
    GST_PAD_ALWAYS,
 
90
    GST_STATIC_CAPS_ANY);
 
91
 
 
92
/* properties */
 
93
enum
 
94
{
 
95
  PROP_0,
 
96
  PROP_JOIN_NONE,
 
97
  PROP_JOIN_FLAGS
 
98
};
 
99
 
 
100
#define DEFAULT_PROP_JOIN_NONE   FALSE
 
101
#define DEFAULT_PROP_JOIN_FLAGS  FALSE
 
102
 
 
103
static void gst_buffer_join_finalize (GObject * object);
 
104
static GstFlowReturn gst_buffer_join_flush (GstBufferJoin * filter,
 
105
    gboolean send);
 
106
static GstCaps *gst_buffer_join_getcaps (GstPad * pad);
 
107
static gboolean gst_buffer_join_setcaps (GstPad * pad, GstCaps * caps);
 
108
static gboolean gst_buffer_join_event (GstPad * pad, GstEvent * event);
 
109
static GstFlowReturn gst_buffer_join_chain (GstPad * pad, GstBuffer * buf);
 
110
static GstStateChangeReturn gst_buffer_join_change_state (GstElement * element,
 
111
    GstStateChange transition);
 
112
 
 
113
/* properties */
 
114
static void gst_buffer_join_set_property (GObject * object,
 
115
    guint prop_id, const GValue * value, GParamSpec * pspec);
 
116
static void gst_buffer_join_get_property (GObject * object,
 
117
    guint prop_id, GValue * value, GParamSpec * pspec);
 
118
 
 
119
GST_BOILERPLATE (GstBufferJoin, gst_buffer_join, GstElement, GST_TYPE_ELEMENT);
 
120
 
 
121
static void
 
122
gst_buffer_join_base_init (gpointer klass)
 
123
{
 
124
  GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
 
125
 
 
126
  gst_element_class_set_details (element_class, &gst_buffer_join_details);
 
127
 
 
128
  gst_element_class_add_pad_template (element_class,
 
129
      gst_static_pad_template_get (&src_template));
 
130
  gst_element_class_add_pad_template (element_class,
 
131
      gst_static_pad_template_get (&sink_template));
 
132
}
 
133
 
 
134
static void
 
135
gst_buffer_join_class_init (GstBufferJoinClass * klass)
 
136
{
 
137
  GstElementClass *element_class;
 
138
  GObjectClass *gobject_class;
 
139
 
 
140
  gobject_class = G_OBJECT_CLASS (klass);
 
141
  element_class = GST_ELEMENT_CLASS (klass);
 
142
 
 
143
  GST_DEBUG_CATEGORY_INIT (buffer_join_debug, "bufferjoin", 0, "Buffer Join");
 
144
 
 
145
  gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_buffer_join_finalize);
 
146
 
 
147
  gobject_class->set_property = gst_buffer_join_set_property;
 
148
  gobject_class->get_property = gst_buffer_join_get_property;
 
149
 
 
150
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_JOIN_NONE,
 
151
      g_param_spec_boolean ("join-none", "Join None",
 
152
          "Join buffers with invalid time",
 
153
          DEFAULT_PROP_JOIN_NONE, G_PARAM_READWRITE));
 
154
 
 
155
  g_object_class_install_property (G_OBJECT_CLASS (klass), PROP_JOIN_FLAGS,
 
156
      g_param_spec_boolean ("join-flags", "Join Flags",
 
157
          "Join buffers with different flags",
 
158
          DEFAULT_PROP_JOIN_NONE, G_PARAM_READWRITE));
 
159
 
 
160
  element_class->change_state =
 
161
      GST_DEBUG_FUNCPTR (gst_buffer_join_change_state);
 
162
}
 
163
 
 
164
static void
 
165
gst_buffer_join_init (GstBufferJoin * filter, GstBufferJoinClass * klass)
 
166
{
 
167
  filter->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
 
168
  gst_pad_set_setcaps_function (filter->sinkpad,
 
169
      GST_DEBUG_FUNCPTR (gst_buffer_join_setcaps));
 
170
  gst_pad_set_event_function (filter->sinkpad,
 
171
      GST_DEBUG_FUNCPTR (gst_buffer_join_event));
 
172
  gst_pad_set_chain_function (filter->sinkpad,
 
173
      GST_DEBUG_FUNCPTR (gst_buffer_join_chain));
 
174
  gst_pad_set_getcaps_function (filter->sinkpad,
 
175
      GST_DEBUG_FUNCPTR (gst_buffer_join_getcaps));
 
176
  gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
 
177
 
 
178
  filter->srcpad = gst_pad_new_from_static_template (&src_template, "src");
 
179
  gst_pad_set_getcaps_function (filter->srcpad,
 
180
      GST_DEBUG_FUNCPTR (gst_buffer_join_getcaps));
 
181
  gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
 
182
 
 
183
  filter->buffer = NULL;
 
184
  filter->join_none = DEFAULT_PROP_JOIN_NONE;
 
185
  filter->join_flags = DEFAULT_PROP_JOIN_FLAGS;
 
186
  gst_buffer_join_flush (filter, FALSE);
 
187
}
 
188
 
 
189
 
 
190
static void
 
191
gst_buffer_join_finalize (GObject * object)
 
192
{
 
193
  GstBufferJoin *filter = GST_BUFFER_JOIN (object);
 
194
 
 
195
  if (filter->buffer)
 
196
    gst_buffer_unref (filter->buffer);
 
197
 
 
198
  G_OBJECT_CLASS (parent_class)->finalize (object);
 
199
}
 
200
 
 
201
 
 
202
static GstCaps *
 
203
gst_buffer_join_getcaps (GstPad * pad)
 
204
{
 
205
  GstBufferJoin *filter;
 
206
  GstPad *otherpad;
 
207
  GstCaps *result;
 
208
 
 
209
  filter = GST_BUFFER_JOIN (GST_PAD_PARENT (pad));
 
210
 
 
211
  otherpad = (pad == filter->srcpad ? filter->sinkpad : filter->srcpad);
 
212
  result = gst_pad_peer_get_caps (otherpad);
 
213
  if (result == NULL)
 
214
    result = gst_caps_new_any ();
 
215
 
 
216
  return result;
 
217
}
 
218
 
 
219
static gboolean
 
220
gst_buffer_join_setcaps (GstPad * pad, GstCaps * caps)
 
221
{
 
222
  GstBufferJoin *filter = GST_BUFFER_JOIN (GST_PAD_PARENT (pad));
 
223
 
 
224
  /* may be a renegotiation; flush previous and accept new caps */
 
225
  gst_buffer_join_flush (filter, TRUE);
 
226
 
 
227
  return gst_pad_set_caps (filter->srcpad, caps);
 
228
}
 
229
 
 
230
 
 
231
static GstFlowReturn
 
232
gst_buffer_join_flush (GstBufferJoin * filter, gboolean send)
 
233
{
 
234
  GstFlowReturn ret = GST_FLOW_OK;
 
235
 
 
236
  if (filter->buffer) {
 
237
    if (send) {
 
238
      ret = gst_pad_push (filter->srcpad, filter->buffer);
 
239
      filter->buffer = NULL;
 
240
    } else {
 
241
      gst_buffer_unref (filter->buffer);
 
242
      filter->buffer = NULL;
 
243
    }
 
244
  }
 
245
 
 
246
  return ret;
 
247
}
 
248
 
 
249
static gboolean
 
250
gst_buffer_join_event (GstPad * pad, GstEvent * event)
 
251
{
 
252
  GstBufferJoin *filter;
 
253
  gboolean ret;
 
254
 
 
255
  filter = GST_BUFFER_JOIN (gst_pad_get_parent (pad));
 
256
 
 
257
  switch (GST_EVENT_TYPE (event)) {
 
258
    case GST_EVENT_FLUSH_STOP:
 
259
      gst_buffer_join_flush (filter, FALSE);
 
260
      /* fall-through */
 
261
    default:
 
262
      ret = gst_pad_event_default (pad, event);
 
263
      break;
 
264
  }
 
265
 
 
266
  gst_object_unref (filter);
 
267
  return ret;
 
268
}
 
269
 
 
270
static GstFlowReturn
 
271
gst_buffer_join_chain (GstPad * pad, GstBuffer * buf)
 
272
{
 
273
  GstBufferJoin *filter;
 
274
  GstFlowReturn ret = GST_FLOW_OK;
 
275
  GstClockTime time;
 
276
  GstBufferFlag flags;
 
277
 
 
278
  filter = GST_BUFFER_JOIN (GST_PAD_PARENT (pad));
 
279
 
 
280
  time = GST_BUFFER_TIMESTAMP (buf);
 
281
  flags = GST_BUFFER_FLAGS (buf);
 
282
  if (filter->buffer &&
 
283
      ((time != GST_BUFFER_TIMESTAMP (filter->buffer)) ||
 
284
          (!filter->join_none && time == GST_CLOCK_TIME_NONE &&
 
285
              GST_BUFFER_TIMESTAMP (filter->buffer) == GST_CLOCK_TIME_NONE) ||
 
286
          (!filter->join_flags && flags != GST_BUFFER_FLAGS (filter->buffer))))
 
287
    ret = gst_buffer_join_flush (filter, TRUE);
 
288
 
 
289
  if (ret != GST_FLOW_OK)
 
290
    goto exit;
 
291
 
 
292
  if (filter->buffer) {
 
293
    GST_DEBUG_OBJECT (filter, "joining buffers at time %" GST_TIME_FORMAT,
 
294
        GST_TIME_ARGS (time));
 
295
    flags = GST_BUFFER_FLAGS (filter->buffer);
 
296
    filter->buffer = gst_buffer_join (filter->buffer, buf);
 
297
    GST_BUFFER_FLAGS (filter->buffer) = flags;
 
298
  } else
 
299
    filter->buffer = buf;
 
300
 
 
301
exit:
 
302
  return ret;
 
303
}
 
304
 
 
305
 
 
306
static void
 
307
gst_buffer_join_set_property (GObject * object, guint prop_id,
 
308
    const GValue * value, GParamSpec * pspec)
 
309
{
 
310
  GstBufferJoin *filter;
 
311
 
 
312
  g_return_if_fail (GST_IS_BUFFER_JOIN (object));
 
313
 
 
314
  filter = GST_BUFFER_JOIN (object);
 
315
 
 
316
  switch (prop_id) {
 
317
    case PROP_JOIN_NONE:
 
318
      filter->join_none = g_value_get_boolean (value);
 
319
      break;
 
320
    case PROP_JOIN_FLAGS:
 
321
      filter->join_flags = g_value_get_boolean (value);
 
322
      break;
 
323
    default:
 
324
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
325
      break;
 
326
  }
 
327
}
 
328
 
 
329
static void
 
330
gst_buffer_join_get_property (GObject * object, guint prop_id, GValue * value,
 
331
    GParamSpec * pspec)
 
332
{
 
333
  GstBufferJoin *filter;
 
334
 
 
335
  g_return_if_fail (GST_IS_BUFFER_JOIN (object));
 
336
 
 
337
  filter = GST_BUFFER_JOIN (object);
 
338
 
 
339
  switch (prop_id) {
 
340
    case PROP_JOIN_NONE:
 
341
      g_value_set_boolean (value, filter->join_none);
 
342
      break;
 
343
    case PROP_JOIN_FLAGS:
 
344
      g_value_set_boolean (value, filter->join_flags);
 
345
      break;
 
346
    default:
 
347
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
 
348
      break;
 
349
  }
 
350
}
 
351
 
 
352
static GstStateChangeReturn
 
353
gst_buffer_join_change_state (GstElement * element, GstStateChange transition)
 
354
{
 
355
  GstStateChangeReturn ret;
 
356
  GstBufferJoin *filter = GST_BUFFER_JOIN (element);
 
357
 
 
358
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
 
359
  if (ret == GST_STATE_CHANGE_FAILURE)
 
360
    goto done;
 
361
 
 
362
  switch (transition) {
 
363
    case GST_STATE_CHANGE_PAUSED_TO_READY:
 
364
      gst_buffer_join_flush (filter, FALSE);
 
365
      break;
 
366
    default:
 
367
      break;
 
368
  }
 
369
 
 
370
done:
 
371
  return ret;
 
372
}