Tizen 2.0 Release
[framework/multimedia/gst-plugins-bad0.10.git] / gst / freeze / gstfreeze.c
1 /* gst-freeze -- Source freezer
2  * Copyright (C) 2005 Gergely Nagy <gergely.nagy@neteyes.hu>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public License
6  * version 2.1, as published by the Free Software Foundation.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
16  */
17
18 /**
19  * SECTION:element-freeze
20  *
21  * Makes a stream from buffers of data.
22  * 
23  * <refsect2>
24  * <title>Example launch line</title>
25  * <para>
26  * |[
27  * gst-launch-0.10 filesrc location=gnome-home.png blocksize=4135 !  pngdec ! freeze ! ffmpegcolorspace ! xvimagesink
28  * ]| Produces a video stream from one picture.
29  * </para>
30  * </refsect2>
31  */
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <gst/gst.h>
37
38 #include "gstfreeze.h"
39
40 GST_DEBUG_CATEGORY_STATIC (freeze_debug);
41 #define GST_CAT_DEFAULT freeze_debug
42
43 enum
44 {
45   ARG_0,
46   ARG_MAX_BUFFERS,
47 };
48
49 static GstStaticPadTemplate gst_freeze_src_template =
50 GST_STATIC_PAD_TEMPLATE ("src",
51     GST_PAD_SRC,
52     GST_PAD_ALWAYS,
53     GST_STATIC_CAPS_ANY);
54
55 static GstStaticPadTemplate gst_freeze_sink_template =
56 GST_STATIC_PAD_TEMPLATE ("sink",
57     GST_PAD_SINK,
58     GST_PAD_ALWAYS,
59     GST_STATIC_CAPS_ANY);
60
61 static void gst_freeze_dispose (GObject * object);
62 static void gst_freeze_set_property (GObject * object, guint prop_id,
63     const GValue * value, GParamSpec * pspec);
64 static void gst_freeze_get_property (GObject * object, guint prop_id,
65     GValue * value, GParamSpec * pspec);
66 static GstFlowReturn gst_freeze_chain (GstPad * pad, GstBuffer * buffer);
67 static GstStateChangeReturn gst_freeze_change_state (GstElement * element,
68     GstStateChange transition);
69 static GstFlowReturn gst_freeze_play (GstPad * pad, GstBuffer * buff);
70 static void gst_freeze_loop (GstPad * pad);
71 static gboolean gst_freeze_sink_activate (GstPad * sinkpad);
72 static gboolean gst_freeze_sink_activate_pull (GstPad * sinkpad,
73     gboolean active);
74 static gboolean gst_freeze_sink_event (GstPad * pad, GstEvent * event);
75 static void gst_freeze_clear_buffer (GstFreeze * freeze);
76 static void gst_freeze_buffer_free (gpointer data, gpointer user_data);
77
78
79 GST_BOILERPLATE (GstFreeze, gst_freeze, GstElement, GST_TYPE_ELEMENT);
80
81 static void
82 gst_freeze_base_init (gpointer klass)
83 {
84   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
85
86   gst_element_class_set_details_simple (element_class, "Stream freezer",
87       "Generic",
88       "Makes a stream from buffers of data",
89       "Gergely Nagy <gergely.nagy@neteyes.hu>,"
90       " Renato Filho <renato.filho@indt.org.br>");
91
92   gst_element_class_add_static_pad_template (element_class,
93       &gst_freeze_sink_template);
94   gst_element_class_add_static_pad_template (element_class,
95       &gst_freeze_src_template);
96
97 }
98
99 static void
100 gst_freeze_class_init (GstFreezeClass * klass)
101 {
102   GObjectClass *object_class = G_OBJECT_CLASS (klass);
103   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
104
105   element_class->change_state = gst_freeze_change_state;
106   object_class->set_property = gst_freeze_set_property;
107   object_class->get_property = gst_freeze_get_property;
108
109   g_object_class_install_property (object_class,
110       ARG_MAX_BUFFERS,
111       g_param_spec_uint ("max-buffers",
112           "max-buffers",
113           "Maximum number of buffers", 0, G_MAXUINT, 1,
114           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
115
116   object_class->dispose = gst_freeze_dispose;
117
118 }
119
120 static void
121 gst_freeze_init (GstFreeze * freeze, GstFreezeClass * klass)
122 {
123   freeze->sinkpad =
124       gst_pad_new_from_static_template (&gst_freeze_sink_template, "sink");
125   gst_element_add_pad (GST_ELEMENT (freeze), freeze->sinkpad);
126   gst_pad_set_activate_function (freeze->sinkpad, gst_freeze_sink_activate);
127   gst_pad_set_activatepull_function (freeze->sinkpad,
128       gst_freeze_sink_activate_pull);
129   gst_pad_set_chain_function (freeze->sinkpad, gst_freeze_chain);
130   gst_pad_set_getcaps_function (freeze->sinkpad, gst_pad_proxy_getcaps);
131   gst_pad_set_event_function (freeze->sinkpad, gst_freeze_sink_event);
132
133   freeze->srcpad =
134       gst_pad_new_from_static_template (&gst_freeze_src_template, "src");
135   gst_element_add_pad (GST_ELEMENT (freeze), freeze->srcpad);
136   gst_pad_set_getcaps_function (freeze->srcpad, gst_pad_proxy_getcaps);
137
138   freeze->timestamp_offset = 0;
139   freeze->running_time = 0;
140   freeze->current = NULL;
141   freeze->max_buffers = 1;
142   freeze->on_flush = FALSE;
143   freeze->buffers = g_queue_new ();
144 }
145
146 static void
147 gst_freeze_dispose (GObject * object)
148 {
149   GstFreeze *freeze = GST_FREEZE (object);
150
151   gst_freeze_clear_buffer (freeze);
152
153   g_queue_free (freeze->buffers);
154
155   G_OBJECT_CLASS (parent_class)->dispose (object);
156 }
157
158 static void
159 gst_freeze_set_property (GObject * object, guint prop_id,
160     const GValue * value, GParamSpec * pspec)
161 {
162   GstFreeze *freeze = GST_FREEZE (object);
163
164   switch (prop_id) {
165     case ARG_MAX_BUFFERS:
166       freeze->max_buffers = g_value_get_uint (value);
167       break;
168     default:
169       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
170       break;
171   }
172 }
173
174 static void
175 gst_freeze_get_property (GObject * object, guint prop_id,
176     GValue * value, GParamSpec * pspec)
177 {
178   GstFreeze *freeze = GST_FREEZE (object);
179
180   switch (prop_id) {
181     case ARG_MAX_BUFFERS:
182       g_value_set_uint (value, freeze->max_buffers);
183       break;
184     default:
185       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
186       break;
187   }
188 }
189
190 static GstFlowReturn
191 gst_freeze_chain (GstPad * pad, GstBuffer * buffer)
192 {
193   return gst_freeze_play (pad, buffer);
194 }
195
196
197 static GstStateChangeReturn
198 gst_freeze_change_state (GstElement * element, GstStateChange transition)
199 {
200   GstFreeze *freeze = GST_FREEZE (element);
201   GstStateChangeReturn return_val = GST_STATE_CHANGE_SUCCESS;
202
203   switch (transition) {
204     case GST_STATE_CHANGE_READY_TO_PAUSED:
205       break;
206     case GST_STATE_CHANGE_NULL_TO_READY:
207     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
208       freeze->timestamp_offset = freeze->running_time = 0;
209       break;
210     default:
211       break;
212   }
213
214   if (GST_ELEMENT_CLASS (parent_class)->change_state)
215     return_val =
216         GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
217
218   switch (transition) {
219     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
220     case GST_STATE_CHANGE_READY_TO_NULL:
221       break;
222     case GST_STATE_CHANGE_PAUSED_TO_READY:
223       freeze->timestamp_offset = freeze->running_time = 0;
224       break;
225     default:
226       break;
227   }
228
229   return return_val;
230 }
231
232 static GstFlowReturn
233 gst_freeze_play (GstPad * pad, GstBuffer * buff)
234 {
235   GstFreeze *freeze;
236   GstFlowReturn ret = GST_FLOW_OK;
237
238   freeze = GST_FREEZE (gst_pad_get_parent (pad));
239
240   if (freeze->on_flush) {
241     g_object_unref (freeze);
242     return GST_FLOW_WRONG_STATE;
243   }
244
245   /* If it is working in push mode this function will be called by "_chain"
246      and buff will never be NULL. In pull mode this function will be called
247      by _loop and buff will be NULL */
248   if (!buff) {
249     ret =
250         gst_pad_pull_range (GST_PAD (freeze->sinkpad), freeze->offset, 4096,
251         &buff);
252     if (ret != GST_FLOW_OK) {
253       gst_object_unref (freeze);
254       return ret;
255     }
256
257     freeze->offset += GST_BUFFER_SIZE (buff);
258
259   }
260
261   if (g_queue_get_length (freeze->buffers) < freeze->max_buffers ||
262       freeze->max_buffers == 0) {
263     g_queue_push_tail (freeze->buffers, buff);
264     GST_DEBUG_OBJECT (freeze, "accepted buffer %u",
265         g_queue_get_length (freeze->buffers) - 1);
266   } else {
267     gst_buffer_unref (buff);
268   }
269
270
271   if (freeze->current != NULL) {
272     GST_DEBUG_OBJECT (freeze, "switching to next buffer");
273     freeze->current = g_queue_peek_nth (freeze->buffers,
274         g_queue_index (freeze->buffers, (gpointer) freeze->current) + 1);
275   }
276
277   if (freeze->current == NULL) {
278     if (freeze->max_buffers > 1)
279       GST_DEBUG_OBJECT (freeze, "restarting the loop");
280     freeze->current = g_queue_peek_head (freeze->buffers);
281   }
282
283   GST_BUFFER_TIMESTAMP (freeze->current) = freeze->timestamp_offset +
284       freeze->running_time;
285   freeze->running_time += GST_BUFFER_DURATION (freeze->current);
286
287   gst_buffer_ref (freeze->current);
288   ret = gst_pad_push (freeze->srcpad, freeze->current);
289
290   gst_object_unref (freeze);
291
292   return ret;
293 }
294
295 static void
296 gst_freeze_loop (GstPad * pad)
297 {
298   gst_freeze_play (pad, NULL);
299 }
300
301 static gboolean
302 gst_freeze_sink_activate (GstPad * sinkpad)
303 {
304   if (gst_pad_check_pull_range (sinkpad)) {
305     return gst_pad_activate_pull (sinkpad, TRUE);
306   } else {
307     return gst_pad_activate_push (sinkpad, TRUE);
308   }
309 }
310
311 static gboolean
312 gst_freeze_sink_activate_pull (GstPad * sinkpad, gboolean active)
313 {
314   gboolean result;
315
316   if (active) {
317     /* if we have a scheduler we can start the task */
318     result = gst_pad_start_task (sinkpad,
319         (GstTaskFunction) gst_freeze_loop, sinkpad);
320   } else {
321     result = gst_pad_stop_task (sinkpad);
322   }
323
324   return result;
325 }
326
327 static void
328 gst_freeze_buffer_free (gpointer data, gpointer user_data)
329 {
330   gst_buffer_unref (GST_BUFFER (data));
331 }
332
333 static void
334 gst_freeze_clear_buffer (GstFreeze * freeze)
335 {
336   if (freeze->buffers != NULL) {
337     g_queue_foreach (freeze->buffers, gst_freeze_buffer_free, NULL);
338   }
339   freeze->current = NULL;
340   freeze->running_time = 0;
341 }
342
343 static gboolean
344 gst_freeze_sink_event (GstPad * pad, GstEvent * event)
345 {
346   gboolean ret = TRUE;
347   GstFreeze *freeze = GST_FREEZE (gst_pad_get_parent (pad));
348
349
350   switch (GST_EVENT_TYPE (event)) {
351     case GST_EVENT_EOS:
352       GST_DEBUG_OBJECT (freeze, "EOS on sink pad %s",
353           gst_pad_get_name (GST_PAD (freeze->sinkpad)));
354       gst_event_unref (event);
355       break;
356     case GST_EVENT_NEWSEGMENT:
357       /* FALL TROUGH */
358     case GST_EVENT_FLUSH_STOP:
359       gst_freeze_clear_buffer (freeze);
360       /* FALL TROUGH */
361     default:
362       ret = gst_pad_event_default (GST_PAD (freeze->sinkpad), event);
363       break;
364   }
365
366   gst_object_unref (freeze);
367   return ret;
368
369 }
370
371 static gboolean
372 plugin_init (GstPlugin * plugin)
373 {
374   GST_DEBUG_CATEGORY_INIT (freeze_debug, "freeze", 0, "Stream freezer");
375
376   return gst_element_register (plugin, "freeze", GST_RANK_NONE,
377       GST_TYPE_FREEZE);
378 }
379
380 GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
381     GST_VERSION_MINOR,
382     "freeze",
383     "Stream freezer",
384     plugin_init, VERSION, GST_LICENSE, GST_PACKAGE_NAME, GST_PACKAGE_ORIGIN);
385
386 /*
387  * Local variables:
388  * mode: c
389  * file-style: k&r
390  * c-basic-offset: 2
391  * arch-tag: fb0ee62b-cf74-46c0-8e62-93b58bacc0ed
392  * End:
393  */