Merge branch 'dtmf-moved-from-bad'
[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., 51 Franklin St, Fifth Floor,
17  * Boston, MA 02110-1301, 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 #include <gst/base/gstadapter.h>
31
32 GST_DEBUG_CATEGORY_STATIC (gst_rnd_buffer_size_debug);
33 #define GST_CAT_DEFAULT gst_rnd_buffer_size_debug
34
35 #define GST_TYPE_RND_BUFFER_SIZE            (gst_rnd_buffer_size_get_type())
36 #define GST_RND_BUFFER_SIZE(obj)            (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSize))
37 #define GST_RND_BUFFER_SIZE_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_RND_BUFFER_SIZE,GstRndBufferSizeClass))
38 #define GST_IS_RND_BUFFER_SIZE(obj)         (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_RND_BUFFER_SIZE))
39 #define GST_IS_RND_BUFFER_SIZE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_RND_BUFFER_SIZE))
40
41 typedef struct _GstRndBufferSize GstRndBufferSize;
42 typedef struct _GstRndBufferSizeClass GstRndBufferSizeClass;
43
44 struct _GstRndBufferSize
45 {
46   GstElement parent;
47
48   /*< private > */
49   GRand *rand;
50   guint seed;
51   gint min, max;
52
53   GstPad *sinkpad, *srcpad;
54   guint64 offset;
55
56   gboolean need_newsegment;
57
58   GstAdapter *adapter;
59 };
60
61 struct _GstRndBufferSizeClass
62 {
63   GstElementClass parent_class;
64 };
65
66 enum
67 {
68   ARG_SEED = 1,
69   ARG_MINIMUM,
70   ARG_MAXIMUM
71 };
72
73 #define DEFAULT_SEED 0
74 #define DEFAULT_MIN  1
75 #define DEFAULT_MAX  (8*1024)
76
77 static GstStaticPadTemplate src_template = GST_STATIC_PAD_TEMPLATE ("src",
78     GST_PAD_SRC,
79     GST_PAD_ALWAYS,
80     GST_STATIC_CAPS_ANY);
81
82 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE ("sink",
83     GST_PAD_SINK,
84     GST_PAD_ALWAYS,
85     GST_STATIC_CAPS_ANY);
86
87 static void gst_rnd_buffer_size_finalize (GObject * object);
88 static void gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
89     const GValue * value, GParamSpec * pspec);
90 static void gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
91     GValue * value, GParamSpec * pspec);
92
93 static gboolean gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent);
94 static gboolean gst_rnd_buffer_size_activate_mode (GstPad * pad,
95     GstObject * parent, GstPadMode mode, gboolean active);
96 static void gst_rnd_buffer_size_loop (GstRndBufferSize * self);
97 static GstStateChangeReturn gst_rnd_buffer_size_change_state (GstElement *
98     element, GstStateChange transition);
99 static gboolean gst_rnd_buffer_size_src_event (GstPad * pad,
100     GstObject * parent, GstEvent * event);
101 static gboolean gst_rnd_buffer_size_sink_event (GstPad * pad,
102     GstObject * parent, GstEvent * event);
103 static GstFlowReturn gst_rnd_buffer_size_chain (GstPad * pad,
104     GstObject * parent, GstBuffer * buffer);
105
106 GType gst_rnd_buffer_size_get_type (void);
107 #define gst_rnd_buffer_size_parent_class parent_class
108 G_DEFINE_TYPE (GstRndBufferSize, gst_rnd_buffer_size, GST_TYPE_ELEMENT);
109
110 static void
111 gst_rnd_buffer_size_class_init (GstRndBufferSizeClass * klass)
112 {
113   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114   GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
115
116   GST_DEBUG_CATEGORY_INIT (gst_rnd_buffer_size_debug, "rndbuffersize", 0,
117       "rndbuffersize element");
118
119   gobject_class->set_property = gst_rnd_buffer_size_set_property;
120   gobject_class->get_property = gst_rnd_buffer_size_get_property;
121   gobject_class->finalize = gst_rnd_buffer_size_finalize;
122
123   gst_element_class_add_pad_template (gstelement_class,
124       gst_static_pad_template_get (&sink_template));
125   gst_element_class_add_pad_template (gstelement_class,
126       gst_static_pad_template_get (&src_template));
127
128   gst_element_class_set_static_metadata (gstelement_class, "Random buffer size",
129       "Testing", "pull random sized buffers",
130       "Stefan Kost <stefan.kost@nokia.com>");
131
132   gstelement_class->change_state =
133       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_change_state);
134
135   g_object_class_install_property (gobject_class, ARG_SEED,
136       g_param_spec_uint ("seed", "random number seed",
137           "seed for randomness (initialized when going from READY to PAUSED)",
138           0, G_MAXUINT32, DEFAULT_SEED,
139           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
140   g_object_class_install_property (gobject_class, ARG_MINIMUM,
141       g_param_spec_int ("min", "mininum", "mininum buffer size",
142           0, G_MAXINT32, DEFAULT_MIN,
143           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
144   g_object_class_install_property (gobject_class, ARG_MAXIMUM,
145       g_param_spec_int ("max", "maximum", "maximum buffer size",
146           1, G_MAXINT32, DEFAULT_MAX,
147           G_PARAM_READWRITE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
148 }
149
150 static void
151 gst_rnd_buffer_size_init (GstRndBufferSize * self)
152 {
153   self->sinkpad = gst_pad_new_from_static_template (&sink_template, "sink");
154   gst_pad_set_activate_function (self->sinkpad,
155       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate));
156   gst_pad_set_activatemode_function (self->sinkpad,
157       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_activate_mode));
158   gst_pad_set_event_function (self->sinkpad,
159       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_sink_event));
160   gst_pad_set_chain_function (self->sinkpad,
161       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_chain));
162   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
163
164   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
165   gst_pad_set_event_function (self->srcpad,
166       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_src_event));
167   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
168 }
169
170
171 static void
172 gst_rnd_buffer_size_finalize (GObject * object)
173 {
174   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
175
176   if (self->rand) {
177     g_rand_free (self->rand);
178     self->rand = NULL;
179   }
180
181   G_OBJECT_CLASS (parent_class)->finalize (object);
182 }
183
184
185 static void
186 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
187     const GValue * value, GParamSpec * pspec)
188 {
189   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
190
191   switch (prop_id) {
192     case ARG_SEED:
193       self->seed = g_value_get_uint (value);
194       break;
195     case ARG_MINIMUM:
196       self->min = g_value_get_int (value);
197       break;
198     case ARG_MAXIMUM:
199       self->max = g_value_get_int (value);
200       break;
201     default:
202       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
203       break;
204   }
205 }
206
207
208 static void
209 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
210     GValue * value, GParamSpec * pspec)
211 {
212   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
213
214   switch (prop_id) {
215     case ARG_SEED:
216       g_value_set_uint (value, self->seed);
217       break;
218     case ARG_MINIMUM:
219       g_value_set_int (value, self->min);
220       break;
221     case ARG_MAXIMUM:
222       g_value_set_int (value, self->max);
223       break;
224     default:
225       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226       break;
227   }
228 }
229
230
231 static gboolean
232 gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent)
233 {
234   GstQuery *query;
235   gboolean pull_mode;
236
237   query = gst_query_new_scheduling ();
238
239   if (gst_pad_peer_query (pad, query))
240     pull_mode = gst_query_has_scheduling_mode_with_flags (query,
241         GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
242   else
243     pull_mode = FALSE;
244
245   gst_query_unref (query);
246
247   if (pull_mode) {
248     GST_DEBUG_OBJECT (pad, "activating pull");
249     return gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
250   } else {
251     GST_DEBUG_OBJECT (pad, "activating push");
252     return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
253   }
254 }
255
256
257 static gboolean
258 gst_rnd_buffer_size_activate_mode (GstPad * pad, GstObject * parent,
259     GstPadMode mode, gboolean active)
260 {
261   gboolean res;
262   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (parent);
263
264   switch (mode) {
265     case GST_PAD_MODE_PULL:
266       if (active) {
267         GST_INFO_OBJECT (self, "starting pull");
268         res =
269             gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
270             self, NULL);
271         self->need_newsegment = TRUE;
272       } else {
273         GST_INFO_OBJECT (self, "stopping pull");
274         res = gst_pad_stop_task (pad);
275       }
276       break;
277     case GST_PAD_MODE_PUSH:
278       GST_INFO_OBJECT (self, "%sactivating in push mode", (active) ? "" : "de");
279       res = TRUE;
280       break;
281     default:
282       res = FALSE;
283       break;
284   }
285   return res;
286 }
287
288 static gboolean
289 gst_rnd_buffer_size_src_event (GstPad * pad, GstObject * parent,
290     GstEvent * event)
291 {
292   GstRndBufferSize *self;
293   GstSeekType start_type;
294   GstSeekFlags flags;
295   GstFormat format;
296   gint64 start;
297
298   if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK) {
299     GST_WARNING_OBJECT (pad, "dropping %s event", GST_EVENT_TYPE_NAME (event));
300     return FALSE;
301   }
302
303   self = GST_RND_BUFFER_SIZE (parent);
304   gst_event_parse_seek (event, NULL, &format, &flags, &start_type, &start,
305       NULL, NULL);
306
307   if (format != GST_FORMAT_BYTES) {
308     GST_WARNING_OBJECT (pad, "only BYTE format supported");
309     return FALSE;
310   }
311   if (start_type != GST_SEEK_TYPE_SET) {
312     GST_WARNING_OBJECT (pad, "only SEEK_TYPE_SET supported");
313     return FALSE;
314   }
315
316   if ((flags & GST_SEEK_FLAG_FLUSH)) {
317     gst_pad_push_event (self->srcpad, gst_event_new_flush_start ());
318     gst_pad_push_event (self->sinkpad, gst_event_new_flush_start ());
319   } else {
320     gst_pad_pause_task (self->sinkpad);
321   }
322
323   GST_PAD_STREAM_LOCK (self->sinkpad);
324
325   if ((flags & GST_SEEK_FLAG_FLUSH)) {
326     gst_pad_push_event (self->srcpad, gst_event_new_flush_stop (TRUE));
327     gst_pad_push_event (self->sinkpad, gst_event_new_flush_stop (TRUE));
328   }
329
330   GST_INFO_OBJECT (pad, "seeking to offset %" G_GINT64_FORMAT, start);
331
332   self->offset = start;
333   self->need_newsegment = TRUE;
334
335   gst_pad_start_task (self->sinkpad, (GstTaskFunction) gst_rnd_buffer_size_loop,
336       self, NULL);
337
338   GST_PAD_STREAM_UNLOCK (self->sinkpad);
339   return TRUE;
340 }
341
342 static GstFlowReturn
343 gst_rnd_buffer_size_drain_adapter (GstRndBufferSize * self, gboolean eos)
344 {
345   GstFlowReturn flow;
346   GstBuffer *buf;
347   guint num_bytes, avail;
348
349   flow = GST_FLOW_OK;
350
351   if (G_UNLIKELY (self->min > self->max))
352     goto bogus_minmax;
353
354   do {
355     if (self->min != self->max) {
356       num_bytes = g_rand_int_range (self->rand, self->min, self->max);
357     } else {
358       num_bytes = self->min;
359     }
360
361     GST_LOG_OBJECT (self, "pulling %u bytes out of adapter", num_bytes);
362
363     buf = gst_adapter_take_buffer (self->adapter, num_bytes);
364
365     if (buf == NULL) {
366       if (!eos) {
367         GST_LOG_OBJECT (self, "not enough bytes in adapter");
368         break;
369       }
370
371       avail = gst_adapter_available (self->adapter);
372
373       if (avail == 0)
374         break;
375
376       if (avail < self->min) {
377         GST_WARNING_OBJECT (self, "discarding %u bytes at end (min=%u)",
378             avail, self->min);
379         gst_adapter_clear (self->adapter);
380         break;
381       }
382       buf = gst_adapter_take_buffer (self->adapter, avail);
383       g_assert (buf != NULL);
384     }
385
386     flow = gst_pad_push (self->srcpad, buf);
387   }
388   while (flow == GST_FLOW_OK);
389
390   return flow;
391
392 /* ERRORS */
393 bogus_minmax:
394   {
395     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
396         ("The minimum buffer size is smaller than the maximum buffer size."),
397         ("buffer sizes: max=%d, min=%d", self->min, self->max));
398     return GST_FLOW_ERROR;
399   }
400 }
401
402 static gboolean
403 gst_rnd_buffer_size_sink_event (GstPad * pad, GstObject * parent,
404     GstEvent * event)
405 {
406   GstRndBufferSize *rnd = GST_RND_BUFFER_SIZE (parent);
407
408   switch (GST_EVENT_TYPE (event)) {
409     case GST_EVENT_EOS:
410       gst_rnd_buffer_size_drain_adapter (rnd, TRUE);
411       break;
412     case GST_EVENT_FLUSH_STOP:
413       if (rnd->adapter != NULL)
414         gst_adapter_clear (rnd->adapter);
415       break;
416     default:
417       break;
418   }
419
420   return gst_pad_push_event (rnd->srcpad, event);
421 }
422
423 static GstFlowReturn
424 gst_rnd_buffer_size_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
425 {
426   GstRndBufferSize *rnd = GST_RND_BUFFER_SIZE (parent);
427   GstFlowReturn flow;
428
429   if (rnd->adapter == NULL)
430     rnd->adapter = gst_adapter_new ();
431
432   gst_adapter_push (rnd->adapter, buf);
433
434   flow = gst_rnd_buffer_size_drain_adapter (rnd, FALSE);
435
436   if (flow != GST_FLOW_OK)
437     GST_INFO_OBJECT (rnd, "flow: %s", gst_flow_get_name (flow));
438
439   return flow;
440 }
441
442 static void
443 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
444 {
445   GstBuffer *buf = NULL;
446   GstFlowReturn ret;
447   guint num_bytes, size;
448
449   if (G_UNLIKELY (self->min > self->max))
450     goto bogus_minmax;
451
452   if (G_UNLIKELY (self->min != self->max)) {
453     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
454   } else {
455     num_bytes = self->min;
456   }
457
458   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
459       num_bytes, self->offset);
460
461   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
462
463   if (ret != GST_FLOW_OK)
464     goto pull_failed;
465
466   size = gst_buffer_get_size (buf);
467
468   if (size < num_bytes) {
469     GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
470   }
471
472   if (self->need_newsegment) {
473     GstSegment segment;
474
475     gst_segment_init (&segment, GST_FORMAT_BYTES);
476     segment.start = self->offset;
477     gst_pad_push_event (self->srcpad, gst_event_new_segment (&segment));
478     self->need_newsegment = FALSE;
479   }
480
481   self->offset += size;
482
483   ret = gst_pad_push (self->srcpad, buf);
484
485   if (ret != GST_FLOW_OK)
486     goto push_failed;
487
488   return;
489
490 pause_task:
491   {
492     GST_DEBUG_OBJECT (self, "pausing task");
493     gst_pad_pause_task (self->sinkpad);
494     return;
495   }
496
497 pull_failed:
498   {
499     if (ret == GST_FLOW_EOS) {
500       GST_DEBUG_OBJECT (self, "eos");
501       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
502     } else {
503       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
504     }
505     goto pause_task;
506   }
507
508 push_failed:
509   {
510     GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
511     if (ret == GST_FLOW_EOS) {
512       GST_DEBUG_OBJECT (self, "eos");
513       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
514     } else if (ret < GST_FLOW_EOS || ret == GST_FLOW_NOT_LINKED) {
515       GST_ELEMENT_ERROR (self, STREAM, FAILED,
516           ("Internal data stream error."),
517           ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
518     }
519     goto pause_task;
520   }
521
522 bogus_minmax:
523   {
524     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
525         ("The minimum buffer size is smaller than the maximum buffer size."),
526         ("buffer sizes: max=%d, min=%d", self->min, self->max));
527     goto pause_task;
528   }
529 }
530
531 static GstStateChangeReturn
532 gst_rnd_buffer_size_change_state (GstElement * element,
533     GstStateChange transition)
534 {
535   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
536   GstStateChangeReturn ret;
537
538   switch (transition) {
539     case GST_STATE_CHANGE_NULL_TO_READY:
540       break;
541     case GST_STATE_CHANGE_READY_TO_PAUSED:
542       self->offset = 0;
543       if (!self->rand) {
544         self->rand = g_rand_new_with_seed (self->seed);
545       }
546       break;
547     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
548       break;
549     default:
550       break;
551   }
552
553   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
554
555   switch (transition) {
556     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
557       break;
558     case GST_STATE_CHANGE_PAUSED_TO_READY:
559       if (self->rand) {
560         g_rand_free (self->rand);
561         self->rand = NULL;
562       }
563       break;
564     case GST_STATE_CHANGE_READY_TO_NULL:
565       if (self->adapter) {
566         g_object_unref (self->adapter);
567         self->adapter = NULL;
568       }
569       break;
570     default:
571       break;
572   }
573
574   return ret;
575 }