2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2005 Wim Taymans <wim@fluendo.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 * SECTION:element-fakesink
25 * @see_also: #GstFakeSrc
27 * Dummy sink that swallows everything.
29 * ## Example launch line
31 * gst-launch-1.0 audiotestsrc num-buffers=1000 ! fakesink sync=false
32 * ]| Render 1000 audio buffers (of default size) as fast as possible.
42 #include "gstelements_private.h"
43 #include "gstfakesink.h"
44 #include "gstcoreelementselements.h"
46 static GstStaticPadTemplate sinktemplate = GST_STATIC_PAD_TEMPLATE ("sink",
51 GST_DEBUG_CATEGORY_STATIC (gst_fake_sink_debug);
52 #define GST_CAT_DEFAULT gst_fake_sink_debug
54 /* FakeSink signals and args */
59 SIGNAL_PREROLL_HANDOFF,
63 #define DEFAULT_SYNC FALSE
64 #define DEFAULT_DROP_OUT_OF_SEGMENT TRUE
66 #define DEFAULT_STATE_ERROR FAKE_SINK_STATE_ERROR_NONE
67 #define DEFAULT_SILENT TRUE
68 #define DEFAULT_DUMP FALSE
69 #define DEFAULT_SIGNAL_HANDOFFS FALSE
70 #define DEFAULT_LAST_MESSAGE NULL
71 #define DEFAULT_CAN_ACTIVATE_PUSH TRUE
72 #define DEFAULT_CAN_ACTIVATE_PULL FALSE
73 #define DEFAULT_NUM_BUFFERS -1
82 PROP_DROP_OUT_OF_SEGMENT,
84 PROP_CAN_ACTIVATE_PUSH,
85 PROP_CAN_ACTIVATE_PULL,
89 #define GST_TYPE_FAKE_SINK_STATE_ERROR (gst_fake_sink_state_error_get_type())
91 gst_fake_sink_state_error_get_type (void)
93 static GType fakesink_state_error_type = 0;
94 static const GEnumValue fakesink_state_error[] = {
95 {FAKE_SINK_STATE_ERROR_NONE, "No state change errors", "none"},
96 {FAKE_SINK_STATE_ERROR_NULL_READY,
97 "Fail state change from NULL to READY", "null-to-ready"},
98 {FAKE_SINK_STATE_ERROR_READY_PAUSED,
99 "Fail state change from READY to PAUSED", "ready-to-paused"},
100 {FAKE_SINK_STATE_ERROR_PAUSED_PLAYING,
101 "Fail state change from PAUSED to PLAYING", "paused-to-playing"},
102 {FAKE_SINK_STATE_ERROR_PLAYING_PAUSED,
103 "Fail state change from PLAYING to PAUSED", "playing-to-paused"},
104 {FAKE_SINK_STATE_ERROR_PAUSED_READY,
105 "Fail state change from PAUSED to READY", "paused-to-ready"},
106 {FAKE_SINK_STATE_ERROR_READY_NULL,
107 "Fail state change from READY to NULL", "ready-to-null"},
111 if (!fakesink_state_error_type) {
112 fakesink_state_error_type =
113 g_enum_register_static ("GstFakeSinkStateError", fakesink_state_error);
115 return fakesink_state_error_type;
119 GST_DEBUG_CATEGORY_INIT (gst_fake_sink_debug, "fakesink", 0, "fakesink element");
120 #define gst_fake_sink_parent_class parent_class
121 GST_ELEMENT_REGISTER_DEFINE (fakesink, "fakesink", GST_RANK_NONE,
123 G_DEFINE_TYPE_WITH_CODE (GstFakeSink, gst_fake_sink, GST_TYPE_BASE_SINK,
126 static void gst_fake_sink_set_property (GObject * object, guint prop_id,
127 const GValue * value, GParamSpec * pspec);
128 static void gst_fake_sink_get_property (GObject * object, guint prop_id,
129 GValue * value, GParamSpec * pspec);
130 static void gst_fake_sink_finalize (GObject * obj);
132 static GstStateChangeReturn gst_fake_sink_change_state (GstElement * element,
133 GstStateChange transition);
135 static GstFlowReturn gst_fake_sink_preroll (GstBaseSink * bsink,
137 static GstFlowReturn gst_fake_sink_render (GstBaseSink * bsink,
139 static gboolean gst_fake_sink_event (GstBaseSink * bsink, GstEvent * event);
140 static gboolean gst_fake_sink_query (GstBaseSink * bsink, GstQuery * query);
142 static guint gst_fake_sink_signals[LAST_SIGNAL] = { 0 };
144 static GParamSpec *pspec_last_message = NULL;
147 gst_fake_sink_class_init (GstFakeSinkClass * klass)
149 GObjectClass *gobject_class;
150 GstElementClass *gstelement_class;
151 GstBaseSinkClass *gstbase_sink_class;
153 gobject_class = G_OBJECT_CLASS (klass);
154 gstelement_class = GST_ELEMENT_CLASS (klass);
155 gstbase_sink_class = GST_BASE_SINK_CLASS (klass);
157 gobject_class->set_property = gst_fake_sink_set_property;
158 gobject_class->get_property = gst_fake_sink_get_property;
159 gobject_class->finalize = gst_fake_sink_finalize;
161 g_object_class_install_property (gobject_class, PROP_STATE_ERROR,
162 g_param_spec_enum ("state-error", "State Error",
163 "Generate a state change error", GST_TYPE_FAKE_SINK_STATE_ERROR,
164 DEFAULT_STATE_ERROR, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
165 pspec_last_message = g_param_spec_string ("last-message", "Last Message",
166 "The message describing current status", DEFAULT_LAST_MESSAGE,
167 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS);
168 g_object_class_install_property (gobject_class, PROP_LAST_MESSAGE,
170 g_object_class_install_property (gobject_class, PROP_SIGNAL_HANDOFFS,
171 g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
172 "Send a signal before unreffing the buffer", DEFAULT_SIGNAL_HANDOFFS,
173 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
174 g_object_class_install_property (gobject_class, PROP_DROP_OUT_OF_SEGMENT,
175 g_param_spec_boolean ("drop-out-of-segment",
176 "Drop out-of-segment buffers",
177 "Drop and don't render / hand off out-of-segment buffers",
178 DEFAULT_DROP_OUT_OF_SEGMENT,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
180 g_object_class_install_property (gobject_class, PROP_SILENT,
181 g_param_spec_boolean ("silent", "Silent",
182 "Don't produce last_message events", DEFAULT_SILENT,
183 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
184 G_PARAM_STATIC_STRINGS));
185 g_object_class_install_property (gobject_class, PROP_DUMP,
186 g_param_spec_boolean ("dump", "Dump", "Dump buffer contents to stdout",
188 G_PARAM_READWRITE | GST_PARAM_MUTABLE_PLAYING |
189 G_PARAM_STATIC_STRINGS));
190 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PUSH,
191 g_param_spec_boolean ("can-activate-push", "Can activate push",
192 "Can activate in push mode", DEFAULT_CAN_ACTIVATE_PUSH,
193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
194 g_object_class_install_property (gobject_class, PROP_CAN_ACTIVATE_PULL,
195 g_param_spec_boolean ("can-activate-pull", "Can activate pull",
196 "Can activate in pull mode", DEFAULT_CAN_ACTIVATE_PULL,
197 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
198 g_object_class_install_property (gobject_class, PROP_NUM_BUFFERS,
199 g_param_spec_int ("num-buffers", "num-buffers",
200 "Number of buffers to accept going EOS", -1, G_MAXINT,
201 DEFAULT_NUM_BUFFERS, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
204 * GstFakeSink::handoff:
205 * @fakesink: the fakesink instance
206 * @buffer: the buffer that just has been received
207 * @pad: the pad that received it
209 * This signal gets emitted before unreffing the buffer.
211 gst_fake_sink_signals[SIGNAL_HANDOFF] =
212 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
213 G_STRUCT_OFFSET (GstFakeSinkClass, handoff), NULL, NULL,
214 NULL, G_TYPE_NONE, 2, GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE,
218 * GstFakeSink::preroll-handoff:
219 * @fakesink: the fakesink instance
220 * @buffer: the buffer that just has been received
221 * @pad: the pad that received it
223 * This signal gets emitted before unreffing the buffer.
225 gst_fake_sink_signals[SIGNAL_PREROLL_HANDOFF] =
226 g_signal_new ("preroll-handoff", G_TYPE_FROM_CLASS (klass),
227 G_SIGNAL_RUN_LAST, G_STRUCT_OFFSET (GstFakeSinkClass, preroll_handoff),
228 NULL, NULL, NULL, G_TYPE_NONE, 2,
229 GST_TYPE_BUFFER | G_SIGNAL_TYPE_STATIC_SCOPE, GST_TYPE_PAD);
231 gst_element_class_set_static_metadata (gstelement_class,
234 "Black hole for data",
235 "Erik Walthinsen <omega@cse.ogi.edu>, "
236 "Wim Taymans <wim@fluendo.com>, "
237 "Mr. 'frag-me-more' Vanderwingo <wingo@fluendo.com>");
239 gst_element_class_add_static_pad_template (gstelement_class, &sinktemplate);
241 gstelement_class->change_state =
242 GST_DEBUG_FUNCPTR (gst_fake_sink_change_state);
244 gstbase_sink_class->event = GST_DEBUG_FUNCPTR (gst_fake_sink_event);
245 gstbase_sink_class->preroll = GST_DEBUG_FUNCPTR (gst_fake_sink_preroll);
246 gstbase_sink_class->render = GST_DEBUG_FUNCPTR (gst_fake_sink_render);
247 gstbase_sink_class->query = GST_DEBUG_FUNCPTR (gst_fake_sink_query);
249 gst_type_mark_as_plugin_api (GST_TYPE_FAKE_SINK_STATE_ERROR, 0);
253 gst_fake_sink_init (GstFakeSink * fakesink)
255 fakesink->silent = DEFAULT_SILENT;
256 fakesink->dump = DEFAULT_DUMP;
257 fakesink->last_message = g_strdup (DEFAULT_LAST_MESSAGE);
258 fakesink->state_error = DEFAULT_STATE_ERROR;
259 fakesink->signal_handoffs = DEFAULT_SIGNAL_HANDOFFS;
260 fakesink->num_buffers = DEFAULT_NUM_BUFFERS;
262 gst_base_sink_set_sync (GST_BASE_SINK (fakesink), DEFAULT_SYNC);
263 gst_base_sink_set_drop_out_of_segment (GST_BASE_SINK (fakesink),
264 DEFAULT_DROP_OUT_OF_SEGMENT);
268 gst_fake_sink_finalize (GObject * obj)
270 G_OBJECT_CLASS (parent_class)->finalize (obj);
274 gst_fake_sink_set_property (GObject * object, guint prop_id,
275 const GValue * value, GParamSpec * pspec)
279 sink = GST_FAKE_SINK (object);
282 case PROP_STATE_ERROR:
283 sink->state_error = (GstFakeSinkStateError) g_value_get_enum (value);
286 sink->silent = g_value_get_boolean (value);
289 sink->dump = g_value_get_boolean (value);
291 case PROP_SIGNAL_HANDOFFS:
292 sink->signal_handoffs = g_value_get_boolean (value);
294 case PROP_DROP_OUT_OF_SEGMENT:
295 gst_base_sink_set_drop_out_of_segment (GST_BASE_SINK (object),
296 g_value_get_boolean (value));
298 case PROP_CAN_ACTIVATE_PUSH:
299 GST_BASE_SINK (sink)->can_activate_push = g_value_get_boolean (value);
301 case PROP_CAN_ACTIVATE_PULL:
302 GST_BASE_SINK (sink)->can_activate_pull = g_value_get_boolean (value);
304 case PROP_NUM_BUFFERS:
305 sink->num_buffers = g_value_get_int (value);
308 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
314 gst_fake_sink_get_property (GObject * object, guint prop_id, GValue * value,
319 sink = GST_FAKE_SINK (object);
322 case PROP_STATE_ERROR:
323 g_value_set_enum (value, sink->state_error);
326 g_value_set_boolean (value, sink->silent);
329 g_value_set_boolean (value, sink->dump);
331 case PROP_SIGNAL_HANDOFFS:
332 g_value_set_boolean (value, sink->signal_handoffs);
334 case PROP_DROP_OUT_OF_SEGMENT:
335 g_value_set_boolean (value,
336 gst_base_sink_get_drop_out_of_segment (GST_BASE_SINK (object)));
338 case PROP_LAST_MESSAGE:
339 GST_OBJECT_LOCK (sink);
340 g_value_set_string (value, sink->last_message);
341 GST_OBJECT_UNLOCK (sink);
343 case PROP_CAN_ACTIVATE_PUSH:
344 g_value_set_boolean (value, GST_BASE_SINK (sink)->can_activate_push);
346 case PROP_CAN_ACTIVATE_PULL:
347 g_value_set_boolean (value, GST_BASE_SINK (sink)->can_activate_pull);
349 case PROP_NUM_BUFFERS:
350 g_value_set_int (value, sink->num_buffers);
353 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
359 gst_fake_sink_notify_last_message (GstFakeSink * sink)
361 g_object_notify_by_pspec ((GObject *) sink, pspec_last_message);
365 gst_fake_sink_event (GstBaseSink * bsink, GstEvent * event)
367 GstFakeSink *sink = GST_FAKE_SINK (bsink);
370 const GstStructure *s;
374 GST_OBJECT_LOCK (sink);
375 g_free (sink->last_message);
377 if (GST_EVENT_TYPE (event) == GST_EVENT_SINK_MESSAGE) {
379 const GstStructure *structure;
381 gst_event_parse_sink_message (event, &msg);
382 structure = gst_message_get_structure (msg);
383 sstr = gst_structure_to_string (structure);
385 g_strdup_printf ("message ******* (%s:%s) M (type: %d, %s) %p",
386 GST_DEBUG_PAD_NAME (GST_BASE_SINK_CAST (sink)->sinkpad),
387 GST_MESSAGE_TYPE (msg), sstr, msg);
388 gst_message_unref (msg);
390 tstr = gst_event_type_get_name (GST_EVENT_TYPE (event));
392 if ((s = gst_event_get_structure (event))) {
393 sstr = gst_structure_to_string (s);
395 sstr = g_strdup ("");
399 g_strdup_printf ("event ******* (%s:%s) E (type: %s (%d), %s) %p",
400 GST_DEBUG_PAD_NAME (GST_BASE_SINK_CAST (sink)->sinkpad),
401 tstr, GST_EVENT_TYPE (event), sstr, event);
404 GST_OBJECT_UNLOCK (sink);
406 gst_fake_sink_notify_last_message (sink);
409 return GST_BASE_SINK_CLASS (parent_class)->event (bsink, event);
413 gst_fake_sink_preroll (GstBaseSink * bsink, GstBuffer * buffer)
415 GstFakeSink *sink = GST_FAKE_SINK (bsink);
417 if (sink->num_buffers_left == 0)
421 GST_OBJECT_LOCK (sink);
422 g_free (sink->last_message);
424 sink->last_message = g_strdup_printf ("preroll ******* ");
425 GST_OBJECT_UNLOCK (sink);
427 gst_fake_sink_notify_last_message (sink);
429 if (sink->signal_handoffs) {
431 gst_fake_sink_signals[SIGNAL_PREROLL_HANDOFF], 0, buffer,
439 GST_DEBUG_OBJECT (sink, "we are EOS");
445 gst_fake_sink_render (GstBaseSink * bsink, GstBuffer * buf)
447 GstFakeSink *sink = GST_FAKE_SINK_CAST (bsink);
449 if (sink->num_buffers_left == 0)
452 if (sink->num_buffers_left != -1)
453 sink->num_buffers_left--;
456 gchar dts_str[64], pts_str[64], dur_str[64];
457 gchar *flag_str, *meta_str;
459 GST_OBJECT_LOCK (sink);
460 g_free (sink->last_message);
462 if (GST_BUFFER_DTS (buf) != GST_CLOCK_TIME_NONE) {
463 g_snprintf (dts_str, sizeof (dts_str), "%" GST_TIME_FORMAT,
464 GST_TIME_ARGS (GST_BUFFER_DTS (buf)));
466 g_strlcpy (dts_str, "none", sizeof (dts_str));
469 if (GST_BUFFER_PTS (buf) != GST_CLOCK_TIME_NONE) {
470 g_snprintf (pts_str, sizeof (pts_str), "%" GST_TIME_FORMAT,
471 GST_TIME_ARGS (GST_BUFFER_PTS (buf)));
473 g_strlcpy (pts_str, "none", sizeof (pts_str));
476 if (GST_BUFFER_DURATION (buf) != GST_CLOCK_TIME_NONE) {
477 g_snprintf (dur_str, sizeof (dur_str), "%" GST_TIME_FORMAT,
478 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)));
480 g_strlcpy (dur_str, "none", sizeof (dur_str));
483 flag_str = gst_buffer_get_flags_string (buf);
484 meta_str = gst_buffer_get_meta_string (buf);
487 g_strdup_printf ("chain ******* (%s:%s) (%u bytes, dts: %s, pts: %s"
488 ", duration: %s, offset: %" G_GINT64_FORMAT ", offset_end: %"
489 G_GINT64_FORMAT ", flags: %08x %s, meta: %s) %p",
490 GST_DEBUG_PAD_NAME (GST_BASE_SINK_CAST (sink)->sinkpad),
491 (guint) gst_buffer_get_size (buf), dts_str, pts_str,
492 dur_str, GST_BUFFER_OFFSET (buf), GST_BUFFER_OFFSET_END (buf),
493 GST_MINI_OBJECT_CAST (buf)->flags, flag_str,
494 meta_str ? meta_str : "none", buf);
497 GST_OBJECT_UNLOCK (sink);
499 gst_fake_sink_notify_last_message (sink);
501 if (sink->signal_handoffs)
502 g_signal_emit (sink, gst_fake_sink_signals[SIGNAL_HANDOFF], 0, buf,
508 if (gst_buffer_map (buf, &info, GST_MAP_READ)) {
509 gst_util_dump_mem (info.data, info.size);
510 gst_buffer_unmap (buf, &info);
513 if (sink->num_buffers_left == 0)
521 GST_DEBUG_OBJECT (sink, "we are EOS");
527 gst_fake_sink_query (GstBaseSink * bsink, GstQuery * query)
531 switch (GST_QUERY_TYPE (query)) {
532 case GST_QUERY_SEEKING:{
535 /* we don't supporting seeking */
536 gst_query_parse_seeking (query, &fmt, NULL, NULL, NULL);
537 gst_query_set_seeking (query, fmt, FALSE, 0, -1);
542 ret = GST_BASE_SINK_CLASS (parent_class)->query (bsink, query);
549 static GstStateChangeReturn
550 gst_fake_sink_change_state (GstElement * element, GstStateChange transition)
552 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
553 GstFakeSink *fakesink = GST_FAKE_SINK (element);
555 switch (transition) {
556 case GST_STATE_CHANGE_NULL_TO_READY:
557 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_NULL_READY)
560 case GST_STATE_CHANGE_READY_TO_PAUSED:
561 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_READY_PAUSED)
563 fakesink->num_buffers_left = fakesink->num_buffers;
565 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
566 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_PAUSED_PLAYING)
573 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
575 switch (transition) {
576 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
577 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_PLAYING_PAUSED)
580 case GST_STATE_CHANGE_PAUSED_TO_READY:
581 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_PAUSED_READY)
584 case GST_STATE_CHANGE_READY_TO_NULL:
585 if (fakesink->state_error == FAKE_SINK_STATE_ERROR_READY_NULL)
587 GST_OBJECT_LOCK (fakesink);
588 g_free (fakesink->last_message);
589 fakesink->last_message = NULL;
590 GST_OBJECT_UNLOCK (fakesink);
600 GST_ELEMENT_ERROR (element, CORE, STATE_CHANGE, (NULL),
601 ("Erroring out on state change as requested"));
602 return GST_STATE_CHANGE_FAILURE;