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