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
gst_segment_clip_get_type(void)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
gst_segment_clip_class_init(GstSegmentClipClass * klass)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 
88 static void
gst_segment_clip_init(GstSegmentClip * self,GstSegmentClipClass * g_class)89 gst_segment_clip_init (GstSegmentClip * self, GstSegmentClipClass * g_class)
90 {
91   GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
92   GstPadTemplate *templ;
93 
94   templ = gst_element_class_get_pad_template (element_class, "sink");
95   g_assert (templ);
96 
97   self->sinkpad = gst_pad_new_from_template (templ, "sink");
98   gst_pad_set_chain_function (self->sinkpad,
99       GST_DEBUG_FUNCPTR (gst_segment_clip_sink_chain));
100   gst_pad_set_event_function (self->sinkpad,
101       GST_DEBUG_FUNCPTR (gst_segment_clip_event));
102   gst_pad_set_query_function (self->sinkpad,
103       GST_DEBUG_FUNCPTR (gst_segment_clip_query));
104   GST_PAD_SET_PROXY_ALLOCATION (self->sinkpad);
105   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
106 
107   templ = gst_element_class_get_pad_template (element_class, "src");
108   g_assert (templ);
109 
110   self->srcpad = gst_pad_new_from_template (templ, "src");
111   gst_pad_set_event_function (self->srcpad,
112       GST_DEBUG_FUNCPTR (gst_segment_clip_event));
113   gst_pad_set_query_function (self->srcpad,
114       GST_DEBUG_FUNCPTR (gst_segment_clip_query));
115   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
116 
117   gst_segment_clip_reset (self);
118 }
119 
120 static void
gst_segment_clip_reset(GstSegmentClip * self)121 gst_segment_clip_reset (GstSegmentClip * self)
122 {
123   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
124 
125   GST_DEBUG_OBJECT (self, "Resetting internal state");
126 
127   gst_segment_init (&self->segment, GST_FORMAT_UNDEFINED);
128   if (klass->reset)
129     klass->reset (self);
130 }
131 
132 static gboolean
gst_segment_clip_sink_setcaps(GstSegmentClip * self,GstCaps * caps)133 gst_segment_clip_sink_setcaps (GstSegmentClip * self, GstCaps * caps)
134 {
135   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
136   gboolean ret;
137 
138   GST_DEBUG_OBJECT (self, "Setting caps: %" GST_PTR_FORMAT, caps);
139   ret = klass->set_caps (self, caps);
140 
141   /* pass along caps* */
142   if (ret)
143     ret = gst_pad_set_caps (self->srcpad, caps);
144 
145   return ret;
146 }
147 
148 static GstCaps *
gst_segment_clip_getcaps(GstSegmentClip * self,GstPad * pad,GstCaps * filter)149 gst_segment_clip_getcaps (GstSegmentClip * self, GstPad * pad, GstCaps * filter)
150 {
151   GstPad *otherpad;
152   GstCaps *tmp, *ret;
153 
154   otherpad = (pad == self->srcpad) ? self->sinkpad : self->srcpad;
155 
156   tmp = gst_pad_peer_query_caps (otherpad, filter);
157   if (tmp) {
158     ret = gst_caps_intersect (tmp, gst_pad_get_pad_template_caps (pad));
159     gst_caps_unref (tmp);
160   } else {
161     ret = gst_caps_copy (gst_pad_get_pad_template_caps (pad));
162   }
163 
164   GST_LOG_OBJECT (pad, "Returning caps: %" GST_PTR_FORMAT, ret);
165 
166   return ret;
167 }
168 
169 static gboolean
gst_segment_clip_query(GstPad * pad,GstObject * parent,GstQuery * query)170 gst_segment_clip_query (GstPad * pad, GstObject * parent, GstQuery * query)
171 {
172   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
173   gboolean ret;
174 
175   GST_LOG_OBJECT (pad, "Handling query of type '%s'",
176       gst_query_type_get_name (GST_QUERY_TYPE (query)));
177 
178   if (GST_QUERY_TYPE (query) == GST_QUERY_CAPS) {
179     GstCaps *caps;
180 
181     gst_query_parse_caps (query, &caps);
182     caps = gst_segment_clip_getcaps (self, pad, caps);
183     gst_query_set_caps_result (query, caps);
184     gst_caps_unref (caps);
185     ret = TRUE;
186   } else {
187     ret = gst_pad_query_default (pad, parent, query);
188   }
189 
190   return ret;
191 }
192 
193 static GstFlowReturn
gst_segment_clip_sink_chain(GstPad * pad,GstObject * parent,GstBuffer * buffer)194 gst_segment_clip_sink_chain (GstPad * pad, GstObject * parent,
195     GstBuffer * buffer)
196 {
197   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
198   GstFlowReturn ret;
199   GstSegmentClipClass *klass = GST_SEGMENT_CLIP_GET_CLASS (self);
200   GstBuffer *outbuf = NULL;
201 
202   GST_LOG_OBJECT (pad,
203       "Handling buffer with timestamp %" GST_TIME_FORMAT " and duration %"
204       GST_TIME_FORMAT, GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buffer)),
205       GST_TIME_ARGS (GST_BUFFER_DURATION (buffer)));
206 
207   ret = klass->clip_buffer (self, buffer, &outbuf);
208   if (ret == GST_FLOW_OK && outbuf)
209     ret = gst_pad_push (self->srcpad, outbuf);
210 
211   return ret;
212 }
213 
214 static GstStateChangeReturn
gst_segment_clip_change_state(GstElement * element,GstStateChange transition)215 gst_segment_clip_change_state (GstElement * element, GstStateChange transition)
216 {
217   GstSegmentClip *self = GST_SEGMENT_CLIP (element);
218   GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
219 
220   if (GST_ELEMENT_CLASS (parent_class)->change_state)
221     ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
222 
223   switch (transition) {
224     case GST_STATE_CHANGE_READY_TO_PAUSED:
225     case GST_STATE_CHANGE_PAUSED_TO_READY:
226       gst_segment_clip_reset (self);
227       break;
228     default:
229       break;
230   }
231 
232   return ret;
233 }
234 
235 static gboolean
gst_segment_clip_event(GstPad * pad,GstObject * parent,GstEvent * event)236 gst_segment_clip_event (GstPad * pad, GstObject * parent, GstEvent * event)
237 {
238   GstSegmentClip *self = GST_SEGMENT_CLIP (parent);
239   gboolean ret = TRUE;
240 
241   GST_LOG_OBJECT (pad, "Got %s event", GST_EVENT_TYPE_NAME (event));
242 
243   switch (GST_EVENT_TYPE (event)) {
244     case GST_EVENT_CAPS:
245     {
246       GstCaps *caps;
247 
248       /* should be only downstream */
249       g_assert (pad == self->sinkpad);
250       gst_event_parse_caps (event, &caps);
251       ret = gst_segment_clip_sink_setcaps (self, caps);
252       break;
253     }
254     case GST_EVENT_SEGMENT:
255     {
256       const GstSegment *segment;
257 
258       gst_event_parse_segment (event, &segment);
259       GST_DEBUG_OBJECT (pad, "Got NEWSEGMENT event %" GST_SEGMENT_FORMAT,
260           segment);
261       gst_segment_copy_into (segment, &self->segment);
262       break;
263     }
264     case GST_EVENT_FLUSH_STOP:
265       gst_segment_clip_reset (self);
266       break;
267     default:
268       break;
269   }
270 
271   if (ret)
272     ret = gst_pad_event_default (pad, parent, event);
273   else
274     gst_event_unref (event);
275 
276   return ret;
277 }
278