win32: remove outdated build cruft
[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   PROP_SEED = 1,
69   PROP_MINIMUM,
70   PROP_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, PROP_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, PROP_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, PROP_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_OBJECT_FLAG_SET (self->sinkpad, GST_PAD_FLAG_PROXY_CAPS);
163   GST_OBJECT_FLAG_SET (self->sinkpad, GST_PAD_FLAG_PROXY_ALLOCATION);
164   GST_OBJECT_FLAG_SET (self->sinkpad, GST_PAD_FLAG_PROXY_SCHEDULING);
165   gst_element_add_pad (GST_ELEMENT (self), self->sinkpad);
166
167   self->srcpad = gst_pad_new_from_static_template (&src_template, "src");
168   gst_pad_set_event_function (self->srcpad,
169       GST_DEBUG_FUNCPTR (gst_rnd_buffer_size_src_event));
170   GST_OBJECT_FLAG_SET (self->srcpad, GST_PAD_FLAG_PROXY_CAPS);
171   GST_OBJECT_FLAG_SET (self->srcpad, GST_PAD_FLAG_PROXY_ALLOCATION);
172   GST_OBJECT_FLAG_SET (self->srcpad, GST_PAD_FLAG_PROXY_SCHEDULING);
173   gst_element_add_pad (GST_ELEMENT (self), self->srcpad);
174 }
175
176
177 static void
178 gst_rnd_buffer_size_finalize (GObject * object)
179 {
180   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
181
182   if (self->rand) {
183     g_rand_free (self->rand);
184     self->rand = NULL;
185   }
186
187   G_OBJECT_CLASS (parent_class)->finalize (object);
188 }
189
190
191 static void
192 gst_rnd_buffer_size_set_property (GObject * object, guint prop_id,
193     const GValue * value, GParamSpec * pspec)
194 {
195   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
196
197   switch (prop_id) {
198     case PROP_SEED:
199       self->seed = g_value_get_uint (value);
200       break;
201     case PROP_MINIMUM:
202       self->min = g_value_get_int (value);
203       break;
204     case PROP_MAXIMUM:
205       self->max = g_value_get_int (value);
206       break;
207     default:
208       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
209       break;
210   }
211 }
212
213
214 static void
215 gst_rnd_buffer_size_get_property (GObject * object, guint prop_id,
216     GValue * value, GParamSpec * pspec)
217 {
218   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (object);
219
220   switch (prop_id) {
221     case PROP_SEED:
222       g_value_set_uint (value, self->seed);
223       break;
224     case PROP_MINIMUM:
225       g_value_set_int (value, self->min);
226       break;
227     case PROP_MAXIMUM:
228       g_value_set_int (value, self->max);
229       break;
230     default:
231       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
232       break;
233   }
234 }
235
236
237 static gboolean
238 gst_rnd_buffer_size_activate (GstPad * pad, GstObject * parent)
239 {
240   GstQuery *query;
241   gboolean pull_mode;
242
243   query = gst_query_new_scheduling ();
244
245   if (gst_pad_peer_query (pad, query))
246     pull_mode = gst_query_has_scheduling_mode_with_flags (query,
247         GST_PAD_MODE_PULL, GST_SCHEDULING_FLAG_SEEKABLE);
248   else
249     pull_mode = FALSE;
250
251   gst_query_unref (query);
252
253   if (pull_mode) {
254     GST_DEBUG_OBJECT (pad, "activating pull");
255     return gst_pad_activate_mode (pad, GST_PAD_MODE_PULL, TRUE);
256   } else {
257     GST_DEBUG_OBJECT (pad, "activating push");
258     return gst_pad_activate_mode (pad, GST_PAD_MODE_PUSH, TRUE);
259   }
260 }
261
262
263 static gboolean
264 gst_rnd_buffer_size_activate_mode (GstPad * pad, GstObject * parent,
265     GstPadMode mode, gboolean active)
266 {
267   gboolean res;
268   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (parent);
269
270   switch (mode) {
271     case GST_PAD_MODE_PULL:
272       if (active) {
273         GST_INFO_OBJECT (self, "starting pull");
274         res =
275             gst_pad_start_task (pad, (GstTaskFunction) gst_rnd_buffer_size_loop,
276             self, NULL);
277         self->need_newsegment = TRUE;
278       } else {
279         GST_INFO_OBJECT (self, "stopping pull");
280         res = gst_pad_stop_task (pad);
281       }
282       break;
283     case GST_PAD_MODE_PUSH:
284       GST_INFO_OBJECT (self, "%sactivating in push mode", (active) ? "" : "de");
285       res = TRUE;
286       break;
287     default:
288       res = FALSE;
289       break;
290   }
291   return res;
292 }
293
294 static gboolean
295 gst_rnd_buffer_size_src_event (GstPad * pad, GstObject * parent,
296     GstEvent * event)
297 {
298   GstRndBufferSize *self;
299   GstSeekType start_type;
300   GstSeekFlags flags;
301   GstFormat format;
302   gint64 start;
303
304   if (GST_EVENT_TYPE (event) != GST_EVENT_SEEK) {
305     return gst_pad_event_default (pad, parent, event);
306   }
307
308   self = GST_RND_BUFFER_SIZE (parent);
309   gst_event_parse_seek (event, NULL, &format, &flags, &start_type, &start,
310       NULL, NULL);
311
312   if (format != GST_FORMAT_BYTES) {
313     GST_WARNING_OBJECT (pad, "only BYTE format supported");
314     return FALSE;
315   }
316   if (start_type != GST_SEEK_TYPE_SET) {
317     GST_WARNING_OBJECT (pad, "only SEEK_TYPE_SET supported");
318     return FALSE;
319   }
320
321   if ((flags & GST_SEEK_FLAG_FLUSH)) {
322     gst_pad_push_event (self->srcpad, gst_event_new_flush_start ());
323     gst_pad_push_event (self->sinkpad, gst_event_new_flush_start ());
324   } else {
325     gst_pad_pause_task (self->sinkpad);
326   }
327
328   GST_PAD_STREAM_LOCK (self->sinkpad);
329
330   if ((flags & GST_SEEK_FLAG_FLUSH)) {
331     gst_pad_push_event (self->srcpad, gst_event_new_flush_stop (TRUE));
332     gst_pad_push_event (self->sinkpad, gst_event_new_flush_stop (TRUE));
333   }
334
335   GST_INFO_OBJECT (pad, "seeking to offset %" G_GINT64_FORMAT, start);
336
337   self->offset = start;
338   self->need_newsegment = TRUE;
339
340   gst_pad_start_task (self->sinkpad, (GstTaskFunction) gst_rnd_buffer_size_loop,
341       self, NULL);
342
343   GST_PAD_STREAM_UNLOCK (self->sinkpad);
344   return TRUE;
345 }
346
347 static GstFlowReturn
348 gst_rnd_buffer_size_drain_adapter (GstRndBufferSize * self, gboolean eos)
349 {
350   GstFlowReturn flow;
351   GstBuffer *buf;
352   guint num_bytes, avail;
353
354   flow = GST_FLOW_OK;
355
356   if (G_UNLIKELY (self->min > self->max))
357     goto bogus_minmax;
358
359   do {
360     if (self->min != self->max) {
361       num_bytes = g_rand_int_range (self->rand, self->min, self->max);
362     } else {
363       num_bytes = self->min;
364     }
365
366     GST_LOG_OBJECT (self, "pulling %u bytes out of adapter", num_bytes);
367
368     buf = gst_adapter_take_buffer (self->adapter, num_bytes);
369
370     if (buf == NULL) {
371       if (!eos) {
372         GST_LOG_OBJECT (self, "not enough bytes in adapter");
373         break;
374       }
375
376       avail = gst_adapter_available (self->adapter);
377
378       if (avail == 0)
379         break;
380
381       if (avail < self->min) {
382         GST_WARNING_OBJECT (self, "discarding %u bytes at end (min=%u)",
383             avail, self->min);
384         gst_adapter_clear (self->adapter);
385         break;
386       }
387       buf = gst_adapter_take_buffer (self->adapter, avail);
388       g_assert (buf != NULL);
389     }
390
391     flow = gst_pad_push (self->srcpad, buf);
392   }
393   while (flow == GST_FLOW_OK);
394
395   return flow;
396
397 /* ERRORS */
398 bogus_minmax:
399   {
400     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
401         ("The minimum buffer size is smaller than the maximum buffer size."),
402         ("buffer sizes: max=%d, min=%d", self->min, self->max));
403     return GST_FLOW_ERROR;
404   }
405 }
406
407 static gboolean
408 gst_rnd_buffer_size_sink_event (GstPad * pad, GstObject * parent,
409     GstEvent * event)
410 {
411   GstRndBufferSize *rnd = GST_RND_BUFFER_SIZE (parent);
412
413   switch (GST_EVENT_TYPE (event)) {
414     case GST_EVENT_EOS:
415       gst_rnd_buffer_size_drain_adapter (rnd, TRUE);
416       break;
417     case GST_EVENT_FLUSH_STOP:
418       if (rnd->adapter != NULL)
419         gst_adapter_clear (rnd->adapter);
420       break;
421     default:
422       break;
423   }
424
425   return gst_pad_event_default (pad, parent, event);
426 }
427
428 static GstFlowReturn
429 gst_rnd_buffer_size_chain (GstPad * pad, GstObject * parent, GstBuffer * buf)
430 {
431   GstRndBufferSize *rnd = GST_RND_BUFFER_SIZE (parent);
432   GstFlowReturn flow;
433
434   if (rnd->adapter == NULL)
435     rnd->adapter = gst_adapter_new ();
436
437   gst_adapter_push (rnd->adapter, buf);
438
439   flow = gst_rnd_buffer_size_drain_adapter (rnd, FALSE);
440
441   if (flow != GST_FLOW_OK)
442     GST_INFO_OBJECT (rnd, "flow: %s", gst_flow_get_name (flow));
443
444   return flow;
445 }
446
447 static void
448 gst_rnd_buffer_size_loop (GstRndBufferSize * self)
449 {
450   GstBuffer *buf = NULL;
451   GstFlowReturn ret;
452   guint num_bytes, size;
453
454   if (G_UNLIKELY (self->min > self->max))
455     goto bogus_minmax;
456
457   if (G_UNLIKELY (self->min != self->max)) {
458     num_bytes = g_rand_int_range (self->rand, self->min, self->max);
459   } else {
460     num_bytes = self->min;
461   }
462
463   GST_LOG_OBJECT (self, "pulling %u bytes at offset %" G_GUINT64_FORMAT,
464       num_bytes, self->offset);
465
466   ret = gst_pad_pull_range (self->sinkpad, self->offset, num_bytes, &buf);
467
468   if (ret != GST_FLOW_OK)
469     goto pull_failed;
470
471   size = gst_buffer_get_size (buf);
472
473   if (size < num_bytes) {
474     GST_WARNING_OBJECT (self, "short buffer: %u bytes", size);
475   }
476
477   if (self->need_newsegment) {
478     GstSegment segment;
479
480     gst_segment_init (&segment, GST_FORMAT_BYTES);
481     segment.start = self->offset;
482     gst_pad_push_event (self->srcpad, gst_event_new_segment (&segment));
483     self->need_newsegment = FALSE;
484   }
485
486   self->offset += size;
487
488   ret = gst_pad_push (self->srcpad, buf);
489
490   if (ret != GST_FLOW_OK)
491     goto push_failed;
492
493   return;
494
495 pause_task:
496   {
497     GST_DEBUG_OBJECT (self, "pausing task");
498     gst_pad_pause_task (self->sinkpad);
499     return;
500   }
501
502 pull_failed:
503   {
504     if (ret == GST_FLOW_EOS) {
505       GST_DEBUG_OBJECT (self, "eos");
506       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
507     } else {
508       GST_WARNING_OBJECT (self, "pull_range flow: %s", gst_flow_get_name (ret));
509     }
510     goto pause_task;
511   }
512
513 push_failed:
514   {
515     GST_DEBUG_OBJECT (self, "push flow: %s", gst_flow_get_name (ret));
516     if (ret == GST_FLOW_EOS) {
517       GST_DEBUG_OBJECT (self, "eos");
518       gst_pad_push_event (self->srcpad, gst_event_new_eos ());
519     } else if (ret < GST_FLOW_EOS || ret == GST_FLOW_NOT_LINKED) {
520       GST_ELEMENT_ERROR (self, STREAM, FAILED,
521           ("Internal data stream error."),
522           ("streaming stopped, reason: %s", gst_flow_get_name (ret)));
523     }
524     goto pause_task;
525   }
526
527 bogus_minmax:
528   {
529     GST_ELEMENT_ERROR (self, LIBRARY, SETTINGS,
530         ("The minimum buffer size is smaller than the maximum buffer size."),
531         ("buffer sizes: max=%d, min=%d", self->min, self->max));
532     goto pause_task;
533   }
534 }
535
536 static GstStateChangeReturn
537 gst_rnd_buffer_size_change_state (GstElement * element,
538     GstStateChange transition)
539 {
540   GstRndBufferSize *self = GST_RND_BUFFER_SIZE (element);
541   GstStateChangeReturn ret;
542
543   switch (transition) {
544     case GST_STATE_CHANGE_NULL_TO_READY:
545       break;
546     case GST_STATE_CHANGE_READY_TO_PAUSED:
547       self->offset = 0;
548       if (!self->rand) {
549         self->rand = g_rand_new_with_seed (self->seed);
550       }
551       break;
552     case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
553       break;
554     default:
555       break;
556   }
557
558   ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
559
560   switch (transition) {
561     case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
562       break;
563     case GST_STATE_CHANGE_PAUSED_TO_READY:
564       if (self->rand) {
565         g_rand_free (self->rand);
566         self->rand = NULL;
567       }
568       break;
569     case GST_STATE_CHANGE_READY_TO_NULL:
570       if (self->adapter) {
571         g_object_unref (self->adapter);
572         self->adapter = NULL;
573       }
574       break;
575     default:
576       break;
577   }
578
579   return ret;
580 }