rndbuffersize: must send flush-stop after acquiring the stream lock
[platform/upstream/gst-plugins-good.git] / gst / debugutils / rndbuffersize.c
1 /* GStreamer
2  * Copyright (C) 2007 Nokia Corporation (contact <stefan.kost@nokia.com>)
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., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 /**
20  * SECTION:element-rndbuffersize
21  *
22  * This element pulls buffers with random sizes from the source.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <gst/gst.h>
30
31 GST_DEBUG_CATEGORY_STATIC (gst_rnd_buffer_size_debug);
32 #define GST_CAT_DEFAULT gst_rnd_buffer_size_debug
33
34 #define GST_TYPE_RND_BUFFER_SIZE            (gst_rnd_buffer_size_get_type())
35 #define GST_RND_BUFFER_SIZE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSize))
36 #define GST_RND_BUFFER_SIZE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSizeClass))
37 #define GST_IS_RND_BUFFER_SIZE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RND_BUFFER_SIZE))
38 #define GST_IS_RND_BUFFER_SIZE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RND_BUFFER_SIZE))
39
40 typedef struct _GstRndBufferSize GstRndBufferSize;
41 typedef struct _GstRndBufferSizeClass GstRndBufferSizeClass;
42
43 struct _GstRndBufferSize
44 {
45   GstElement parent;
46
47   /*< private > */
48   GRand *rand;
49   gulong seed;
50   glong min, max;
51
52   GstPad *sinkpad, *srcpad;
53   guint64 offset;
54
55   gboolean need_newsegment;
56 };
57
58 struct _GstRndBufferSizeClass
59 {
60   GstElementClass parent_class;
61 };
62
63 enum
64 {
65   ARG_SEED = 1,
66   ARG_MINIMUM,
67   ARG_MAXIMUM
68 };
69
70 #define DEFAULT_SEED 0
71 #define DEFAULT_MIN  1
72 #define DEFAULT_MAX  (8*1024)
73
74 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
75     GST_PAD_SRC,
76     GST_PAD_ALWAYS,
77     GST_STATIC_CAPS_ANY);
78
79 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
80     GST_PAD_SINK,
81     GST_PAD_ALWAYS,
82     GST_STATIC_CAPS_ANY);
83
84 static void gst_rnd_buffer_size_finalize (GObject * object);
85 static void gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
86     const GValue * value, GParamSpec * pspec);
87 static void gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
88     GValue * value, GParamSpec * pspec);
89
90 static gboolean gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent);
91 static gboolean gst_rnd_buffer_size_activate_mode (GstPad * pad,
92     GstObject * parent, GstPadMode mode, gboolean active);
93 static void gst_rnd_buffer_size_loop (GstRndBufferSize * self);
94 static GstStateChangeReturn gst_rnd_buffer_size_change_state (GstElement *
95     element, GstStateChange transition);
96 static gboolean gst_rnd_buffer_size_src_event (GstPad * pad,
97     GstObject * parent, GstEvent * event);
98
99 GType gst_rnd_buffer_size_get_type (void);
100 #define gst_rnd_buffer_size_parent_class parent_class
101 G_DEFINE_TYPE (GstRndBufferSize, gst_rnd_buffer_size, GST_TYPE_ELEMENT);
102
103 static void
104 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
105 {
106   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
107   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
108
109   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
110       "rndbuffersize element");
111
112   gobject_class->set_property = gst_rnd_buffer_size_set_property;
113   gobject_class->get_property = gst_rnd_buffer_size_get_property;
114   gobject_class->finalize = gst_rnd_buffer_size_finalize;
115
116   gst_element_class_add_pad_template (gstelement_class,
117       gst_static_pad_template_get (&sink_template));
118   gst_element_class_add_pad_template (gstelement_class,
119       gst_static_pad_template_get (&src_template));
120
121   gst_element_class_set_static_metadata (gstelement_class, "Random buffer size",
122       "Testing", "pull random sized buffers",
123       "Stefan Kost <stefan.kost@nokia.com>");
124
125   gstelement_class->change_state =
126       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
127
128   /* FIXME 0.11: these should all be int instead of long, to avoid bugs
129    * when passing these as varargs with g_object_set(), and there was no
130    * reason to use long in the first place here */
131   g_object_class_install_property (gobject_class, ARG_SEED,
132       g_param_spec_ulong ("seed", "random number seed",
133           "seed for randomness (initialized when going from READY to PAUSED)",
134           0, G_MAXUINT32, DEFAULT_SEED,
135           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
136   g_object_class_install_property (gobject_class, ARG_MINIMUM,
137       g_param_spec_long ("min", "mininum", "mininum buffer size",
138           0, G_MAXINT32, DEFAULT_MIN,
139           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
140   g_object_class_install_property (gobject_class, ARG_MAXIMUM,
141       g_param_spec_long ("max", "maximum", "maximum buffer size",
142           1, G_MAXINT32, DEFAULT_MAX,
143           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
144 }
145
146 static void
147 gst_rnd_buffer_size_init (GstRndBufferSize * self)
148 {
149   self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
150   gst_pad_set_activate_function (self->sinkpad,
151       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
152   gst_pad_set_activatemode_function (self->sinkpad,
153       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_mode));
154   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
155
156   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
157   gst_pad_set_event_function (self->srcpad,
158       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_src_event));
159   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
160 }
161
162
163 static void
164 gst_rnd_buffer_size_finalize (GObject * object)
165 {
166   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
167
168   if (self->rand) {
169     g_rand_free (self->rand);
170     self->rand = NULL;
171   }
172
173   G_OBJECT_CLASS (parent_class)->finalize (object);
174 }
175
176
177 static void
178 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
179     const GValue * value, GParamSpec * pspec)
180 {
181   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
182
183   switch (prop_id) {
184     case ARG_SEED:
185       self->seed = g_value_get_ulong (value);
186       break;
187     case ARG_MINIMUM:
188       self->min = g_value_get_long (value);
189       break;
190     case ARG_MAXIMUM:
191       self->max = g_value_get_long (value);
192       break;
193     default:
194       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
195       break;
196   }
197 }
198
199
200 static void
201 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
202     GValue * value, GParamSpec * pspec)
203 {
204   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
205
206   switch (prop_id) {
207     case ARG_SEED:
208       g_value_set_ulong (value, self->seed);
209       break;
210     case ARG_MINIMUM:
211       g_value_set_long (value, self->min);
212       break;
213     case ARG_MAXIMUM:
214       g_value_set_long (value, self->max);
215       break;
216     default:
217       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218       break;
219   }
220 }
221
222
223 static gboolean
224 gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent)
225 {
226   GstQuery *query;
227   gboolean pull_mode;
228
229   query = gst_query_new_scheduling ();
230
231   if (!gst_pad_peer_query (pad, query)) {
232     gst_query_unref (query);
233     goto no_pull;
234   }
235
236   pull_mode = gst_query_has_scheduling_mode (query, GST_PAD_MODE_PULL);
237   gst_query_unref (query);
238
239   if (!pull_mode)
240     goto no_pull;
241
242   GST_DEBUG_OBJECT (pad, "activating pull");
243   return gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
244
245   /* ERRORS */
246 no_pull:
247   {
248     GST_DEBUG_OBJECT (pad, "pull mode not supported");
249     return FALSE;
250   }
251 }
252
253
254 static gboolean
255 gst_rnd_buffer_size_activate_mode (GstPad * pad, GstObject * parent,
256     GstPadMode mode, gboolean active)
257 {
258   gboolean res;
259   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (parent);
260
261   switch (mode) {
262     case GST_PAD_MODE_PULL:
263       if (active) {
264         GST_INFO_OBJECT (self, "starting pull");
265         res =
266             gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
267             self);
268         self->need_newsegment = TRUE;
269       } else {
270         GST_INFO_OBJECT (self, "stopping pull");
271         res = gst_pad_stop_task (pad);
272       }
273       break;
274     default:
275       res = FALSE;
276       break;
277   }
278   return res;
279 }
280
281 static gboolean
282 gst_rnd_buffer_size_src_event (GstPad * pad, GstObject * parent,
283     GstEvent * event)
284 {
285   GstRndBufferSize *self;
286   GstSeekType start_type;
287   GstSeekFlags flags;
288   GstFormat format;
289   gint64 start;
290
291   if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK) {
292     GST_WARNING_OBJECT (pad, "dropping %s event", GST_EVENT_TYPE_NAME (event));
293     return FALSE;
294   }
295
296   self = GST_RND_BUFFER_SIZE (parent);
297   gst_event_parse_seek (event, NULL, &format, &flags, &start_type, &start,
298       NULL, NULL);
299
300   if (format != GST_FORMAT_BYTES) {
301     GST_WARNING_OBJECT (pad, "only BYTE format supported");
302     return FALSE;
303   }
304   if (start_type != GST_SEEK_TYPE_SET) {
305     GST_WARNING_OBJECT (pad, "only SEEK_TYPE_SET supported");
306     return FALSE;
307   }
308
309   if ((flags & GST_SEEK_FLAG_FLUSH)) {
310     gst_pad_push_event (self->srcpad, gst_event_new_flush_start ());
311     gst_pad_push_event (self->sinkpad, gst_event_new_flush_start ());
312   } else {
313     gst_pad_pause_task (self->sinkpad);
314   }
315
316   GST_PAD_STREAM_LOCK (self->sinkpad);
317
318   gst_pad_push_event (self->srcpad, gst_event_new_flush_stop (TRUE));
319   gst_pad_push_event (self->sinkpad, gst_event_new_flush_stop (TRUE));
320
321   GST_INFO_OBJECT (pad, "seeking to offset %" G_GINT64_FORMAT, start);
322
323   self->offset = start;
324   self->need_newsegment = TRUE;
325
326   gst_pad_start_task (self->sinkpad, (GstTaskFunction) gst_rnd_buffer_size_loop,
327       self);
328
329   GST_PAD_STREAM_UNLOCK (self->sinkpad);
330   return TRUE;
331 }
332
333 static void
334 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
335 {
336   GstBuffer *buf = NULL;
337   GstFlowReturn ret;
338   guint num_bytes, size;
339
340   if (G_UNLIKELY (self->min > self->max))
341     goto bogus_minmax;
342
343   if (G_UNLIKELY (self->min != self->max)) {
344     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
345   } else {
346     num_bytes = self->min;
347   }
348
349   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
350       num_bytes, self->offset);
351
352   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
353
354   if (ret != GST_FLOW_OK)
355     goto pull_failed;
356
357   size = gst_buffer_get_size (buf);
358
359   if (size < num_bytes) {
360     GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
361   }
362
363   if (self->need_newsegment) {
364     GstSegment segment;
365
366     gst_segment_init (&segment, GST_FORMAT_BYTES);
367     segment.start = self->offset;
368     gst_pad_push_event (self->srcpad, gst_event_new_segment (&segment));
369     self->need_newsegment = FALSE;
370   }
371
372   self->offset += size;
373
374   ret = gst_pad_push (self->srcpad, buf);
375
376   if (ret != GST_FLOW_OK)
377     goto push_failed;
378
379   return;
380
381 pause_task:
382   {
383     GST_DEBUG_OBJECT (self, "pausing task");
384     gst_pad_pause_task (self->sinkpad);
385     return;
386   }
387
388 pull_failed:
389   {
390     if (ret == GST_FLOW_EOS) {
391       GST_DEBUG_OBJECT (self, "eos");
392       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
393     } else {
394       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
395     }
396     goto pause_task;
397   }
398
399 push_failed:
400   {
401     GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
402     if (ret == GST_FLOW_EOS) {
403       GST_DEBUG_OBJECT (self, "eos");
404       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
405     } else if (ret < GST_FLOW_EOS || ret == GST_FLOW_NOT_LINKED) {
406       GST_ELEMENT_ERROR (self, STREAM, FAILED,
407           ("Internal data stream error."),
408           ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
409     }
410     goto pause_task;
411   }
412
413 bogus_minmax:
414   {
415     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
416         ("The minimum buffer size is smaller than the maximum buffer size."),
417         ("buffer sizes: max=%ld, min=%ld", self->min, self->max));
418     goto pause_task;
419   }
420 }
421
422 static GstStateChangeReturn
423 gst_rnd_buffer_size_change_state (GstElement * element,
424     GstStateChange transition)
425 {
426   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
427   GstStateChangeReturn ret;
428
429   switch (transition) {
430     case GST_STATE_CHANGE_NULL_TO_READY:
431       break;
432     case GST_STATE_CHANGE_READY_TO_PAUSED:
433       self->offset = 0;
434       if (!self->rand) {
435         self->rand = g_rand_new_with_seed (self->seed);
436       }
437       break;
438     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
439       break;
440     default:
441       break;
442   }
443
444   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
445
446   switch (transition) {
447     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
448       break;
449     case GST_STATE_CHANGE_PAUSED_TO_READY:
450       if (self->rand) {
451         g_rand_free (self->rand);
452         self->rand = NULL;
453       }
454       break;
455     case GST_STATE_CHANGE_READY_TO_NULL:
456       break;
457     default:
458       break;
459   }
460
461   return ret;
462 }