6d002b24f81f871f0e48eef98f3cf1aeb162cdae
[platform/upstream/gstreamer.git] / gst / segmentclip / gstsegmentclip.c
1 /* GStreamer
2  * Copyright (C) 2010 Sebastian Dröge <sebastian.droege@collabora.co.uk>
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 St, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <gst/gst.h>
25
26 #include "gstsegmentclip.h"
27
28 static void gst_segment_clip_reset (GstSegmentClip * self);
29
30 static GstStateChangeReturn gst_segment_clip_change_state (GstElement * element,
31     GstStateChange transition);
32
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,
36     GstCaps * caps);
37
38 static gboolean gst_segment_clip_event (GstPad * pad, GstObject * parent,
39     GstEvent * event);
40 static GstCaps *gst_segment_clip_getcaps (GstSegmentClip * self, GstPad * pad,
41     GstCaps * filter);
42 static gboolean gst_segment_clip_query (GstPad * pad, GstObject * parent,
43     GstQuery * query);
44
45 GST_DEBUG_CATEGORY_STATIC (gst_segment_clip_debug);
46 #define GST_CAT_DEFAULT gst_segment_clip_debug
47
48 static void gst_segment_clip_class_init (GstSegmentClipClass * klass);
49 static void gst_segment_clip_init (GstSegmentClip * clip,
50     GstSegmentClipClass * g_class);
51
52 static GstElementClass *parent_class;
53
54 /* we can't use G_DEFINE_ABSTRACT_TYPE because we need the klass in the _init
55  * method to get to the padtemplates */
56 GType
57 gst_segment_clip_get_type (void)
58 {
59   static volatile gsize segment_clip_type = 0;
60
61   if (g_once_init_enter (&segment_clip_type)) {
62     GType _type;
63
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);
68
69     g_once_init_leave (&segment_clip_type, _type);
70   }
71   return segment_clip_type;
72 }
73
74 static void
75 gst_segment_clip_class_init (GstSegmentClipClass * klass)
76 {
77   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
78
79   parent_class = g_type_class_peek_parent (klass);
80
81   GST_DEBUG_CATEGORY_INIT (gst_segment_clip_debug, "segmentclip", 0,
82       "segmentclip base class");
83
84   gstelement_class->change_state =
85       GST_DEBUG_FUNCPTR (gst_segment_clip_change_state);
86
87   gst_type_mark_as_plugin_api (GST_TYPE_SEGMENT_CLIP, 0);
88 }
89
90 static void
91 gst_segment_clip_init (GstSegmentClip * self, GstSegmentClipClass * g_class)
92 {
93   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
94   GstPadTemplate *templ;
95
96   templ = gst_element_class_get_pad_template (element_class, "sink");
97   g_assert (templ);
98
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);
108
109   templ = gst_element_class_get_pad_template (element_class, "src");
110   g_assert (templ);
111
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);
118
119   gst_segment_clip_reset (self);
120 }
121
122 static void
123 gst_segment_clip_reset (GstSegmentClip * self)
124 {
125   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
126
127   GST_DEBUG_OBJECT (self, "Resetting internal state");
128
129   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
130   if (klass->reset)
131     klass->reset (self);
132 }
133
134 static gboolean
135 gst_segment_clip_sink_setcaps (GstSegmentClip * self, GstCaps * caps)
136 {
137   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
138   gboolean ret;
139
140   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
141   ret = klass->set_caps (self, caps);
142
143   /* pass along caps* */
144   if (ret)
145     ret = gst_pad_set_caps (self->srcpad, caps);
146
147   return ret;
148 }
149
150 static GstCaps *
151 gst_segment_clip_getcaps (GstSegmentClip * self, GstPad * pad, GstCaps * filter)
152 {
153   GstPad *otherpad;
154   GstCaps *tmp, *ret;
155
156   otherpad = (pad == self->srcpad) ? self->sinkpad : self->srcpad;
157
158   tmp = gst_pad_peer_query_caps (otherpad, filter);
159   if (tmp) {
160     ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
161     gst_caps_unref (tmp);
162   } else {
163     ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
164   }
165
166   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
167
168   return ret;
169 }
170
171 static gboolean
172 gst_segment_clip_query (GstPad * pad, GstObject * parent, GstQuery * query)
173 {
174   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
175   gboolean ret;
176
177   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
178       gst_query_type_get_name (GST_QUERY_TYPE (query)));
179
180   if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
181     GstCaps *caps;
182
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);
187     ret = TRUE;
188   } else {
189     ret = gst_pad_query_default (pad, parent, query);
190   }
191
192   return ret;
193 }
194
195 static GstFlowReturn
196 gst_segment_clip_sink_chain (GstPad * pad, GstObject * parent,
197     GstBuffer * buffer)
198 {
199   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
200   GstFlowReturn ret;
201   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
202   GstBuffer *outbuf = NULL;
203
204   GST_LOG_OBJECT (pad,
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)));
208
209   ret = klass->clip_buffer (self, buffer, &outbuf);
210   if (ret == GST_FLOW_OK && outbuf)
211     ret = gst_pad_push (self->srcpad, outbuf);
212
213   return ret;
214 }
215
216 static GstStateChangeReturn
217 gst_segment_clip_change_state (GstElement * element, GstStateChange transition)
218 {
219   GstSegmentClip *self = GST_SEGMENT_CLIP (element);
220   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
221
222   if (GST_ELEMENT_CLASS (parent_class)->change_state)
223     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
224
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);
229       break;
230     default:
231       break;
232   }
233
234   return ret;
235 }
236
237 static gboolean
238 gst_segment_clip_event (GstPad * pad, GstObject * parent, GstEvent * event)
239 {
240   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
241   gboolean ret = TRUE;
242
243   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
244
245   switch (GST_EVENT_TYPE (event)) {
246     case GST_EVENT_CAPS:
247     {
248       GstCaps *caps;
249
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);
254       break;
255     }
256     case GST_EVENT_SEGMENT:
257     {
258       const GstSegment *segment;
259
260       gst_event_parse_segment (event, &segment);
261       GST_DEBUG_OBJECT (pad, "Got NEWSEGMENT event %" GST_SEGMENT_FORMAT,
262           segment);
263       gst_segment_copy_into (segment, &self->segment);
264       break;
265     }
266     case GST_EVENT_FLUSH_STOP:
267       gst_segment_clip_reset (self);
268       break;
269     default:
270       break;
271   }
272
273   if (ret)
274     ret = gst_pad_event_default (pad, parent, event);
275   else
276     gst_event_unref (event);
277
278   return ret;
279 }