2 * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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.
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.
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 St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
26 #include "gstsegmentclip.h"
28 static void gst_segment_clip_reset (GstSegmentClip * self);
30 static GstStateChangeReturn gst_segment_clip_change_state (GstElement * element,
31 GstStateChange transition);
33 static GstFlowReturn gst_segment_clip_sink_chain (GstPad * pad,
34 GstObject * parent, GstBuffer * buffer);
35 static gboolean gst_segment_clip_sink_setcaps (GstSegmentClip * self,
38 static gboolean gst_segment_clip_event (GstPad * pad, GstObject * parent,
40 static GstCaps *gst_segment_clip_getcaps (GstSegmentClip * self, GstPad * pad,
42 static gboolean gst_segment_clip_query (GstPad * pad, GstObject * parent,
45 GST_DEBUG_CATEGORY_STATIC (gst_segment_clip_debug);
46 #define GST_CAT_DEFAULT gst_segment_clip_debug
48 static void gst_segment_clip_class_init (GstSegmentClipClass * klass);
49 static void gst_segment_clip_init (GstSegmentClip * clip,
50 GstSegmentClipClass * g_class);
52 static GstElementClass *parent_class;
54 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
55 * method to get to the padtemplates */
57 gst_segment_clip_get_type (void)
59 static gsize segment_clip_type = 0;
61 if (g_once_init_enter (&segment_clip_type)) {
64 _type = g_type_register_static_simple (GST_TYPE_ELEMENT,
65 "GstSegmentClip", sizeof (GstSegmentClipClass),
66 (GClassInitFunc) gst_segment_clip_class_init, sizeof (GstSegmentClip),
67 (GInstanceInitFunc) gst_segment_clip_init, G_TYPE_FLAG_ABSTRACT);
69 g_once_init_leave (&segment_clip_type, _type);
71 return segment_clip_type;
75 gst_segment_clip_class_init (GstSegmentClipClass * klass)
77 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
79 parent_class = g_type_class_peek_parent (klass);
81 GST_DEBUG_CATEGORY_INIT (gst_segment_clip_debug, "segmentclip", 0,
82 "segmentclip base class");
84 gstelement_class->change_state =
85 GST_DEBUG_FUNCPTR (gst_segment_clip_change_state);
87 gst_type_mark_as_plugin_api (GST_TYPE_SEGMENT_CLIP, 0);
91 gst_segment_clip_init (GstSegmentClip * self, GstSegmentClipClass * g_class)
93 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94 GstPadTemplate *templ;
96 templ = gst_element_class_get_pad_template (element_class, "sink");
99 self->sinkpad = gst_pad_new_from_template (templ, "sink");
100 gst_pad_set_chain_function (self->sinkpad,
101 GST_DEBUG_FUNCPTR (gst_segment_clip_sink_chain));
102 gst_pad_set_event_function (self->sinkpad,
103 GST_DEBUG_FUNCPTR (gst_segment_clip_event));
104 gst_pad_set_query_function (self->sinkpad,
105 GST_DEBUG_FUNCPTR (gst_segment_clip_query));
106 GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
107 gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
109 templ = gst_element_class_get_pad_template (element_class, "src");
112 self->srcpad = gst_pad_new_from_template (templ, "src");
113 gst_pad_set_event_function (self->srcpad,
114 GST_DEBUG_FUNCPTR (gst_segment_clip_event));
115 gst_pad_set_query_function (self->srcpad,
116 GST_DEBUG_FUNCPTR (gst_segment_clip_query));
117 gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
119 gst_segment_clip_reset (self);
123 gst_segment_clip_reset (GstSegmentClip * self)
125 GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
127 GST_DEBUG_OBJECT (self, "Resetting internal state");
129 gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
135 gst_segment_clip_sink_setcaps (GstSegmentClip * self, GstCaps * caps)
137 GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
140 GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
141 ret = klass->set_caps (self, caps);
143 /* pass along caps* */
145 ret = gst_pad_set_caps (self->srcpad, caps);
151 gst_segment_clip_getcaps (GstSegmentClip * self, GstPad * pad, GstCaps * filter)
156 otherpad = (pad == self->srcpad) ? self->sinkpad : self->srcpad;
158 tmp = gst_pad_peer_query_caps (otherpad, filter);
160 ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
161 gst_caps_unref (tmp);
163 ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
166 GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
172 gst_segment_clip_query (GstPad * pad, GstObject * parent, GstQuery * query)
174 GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
177 GST_LOG_OBJECT (pad, "Handling query of type '%s'",
178 gst_query_type_get_name (GST_QUERY_TYPE (query)));
180 if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
183 gst_query_parse_caps (query, &caps);
184 caps = gst_segment_clip_getcaps (self, pad, caps);
185 gst_query_set_caps_result (query, caps);
186 gst_caps_unref (caps);
189 ret = gst_pad_query_default (pad, parent, query);
196 gst_segment_clip_sink_chain (GstPad * pad, GstObject * parent,
199 GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
201 GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
202 GstBuffer *outbuf = NULL;
205 "Handling buffer with timestamp %" GST_TIME_FORMAT " and duration %"
206 GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
207 GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
209 ret = klass->clip_buffer (self, buffer, &outbuf);
210 if (ret == GST_FLOW_OK && outbuf)
211 ret = gst_pad_push (self->srcpad, outbuf);
216 static GstStateChangeReturn
217 gst_segment_clip_change_state (GstElement * element, GstStateChange transition)
219 GstSegmentClip *self = GST_SEGMENT_CLIP (element);
220 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
222 if (GST_ELEMENT_CLASS (parent_class)->change_state)
223 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
225 switch (transition) {
226 case GST_STATE_CHANGE_READY_TO_PAUSED:
227 case GST_STATE_CHANGE_PAUSED_TO_READY:
228 gst_segment_clip_reset (self);
238 gst_segment_clip_event (GstPad * pad, GstObject * parent, GstEvent * event)
240 GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
243 GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
245 switch (GST_EVENT_TYPE (event)) {
250 /* should be only downstream */
251 g_assert (pad == self->sinkpad);
252 gst_event_parse_caps (event, &caps);
253 ret = gst_segment_clip_sink_setcaps (self, caps);
256 case GST_EVENT_SEGMENT:
258 const GstSegment *segment;
260 gst_event_parse_segment (event, &segment);
261 GST_DEBUG_OBJECT (pad, "Got NEWSEGMENT event %" GST_SEGMENT_FORMAT,
263 gst_segment_copy_into (segment, &self->segment);
266 case GST_EVENT_FLUSH_STOP:
267 gst_segment_clip_reset (self);
274 ret = gst_pad_event_default (pad, parent, event);
276 gst_event_unref (event);