2 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3 * 2000 Wim Taymans <wtay@chello.be>
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., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
28 #include "gstfakesink.h"
29 #include <gst/gstmarshal.h>
31 GST_DEBUG_CATEGORY_STATIC (gst_fakesink_debug);
32 #define GST_CAT_DEFAULT gst_fakesink_debug
34 GstElementDetails gst_fakesink_details = GST_ELEMENT_DETAILS ("Fake Sink",
36 "Black hole for data",
37 "Erik Walthinsen <omega@cse.ogi.edu>");
40 /* FakeSink signals and args */
60 GstStaticPadTemplate fakesink_sink_template = GST_STATIC_PAD_TEMPLATE ("sink%d",
65 #define GST_TYPE_FAKESINK_STATE_ERROR (gst_fakesink_state_error_get_type())
67 gst_fakesink_state_error_get_type (void)
69 static GType fakesink_state_error_type = 0;
70 static GEnumValue fakesink_state_error[] = {
71 {FAKESINK_STATE_ERROR_NONE, "0", "No state change errors"},
72 {FAKESINK_STATE_ERROR_NULL_READY, "1",
73 "Fail state change from NULL to READY"},
74 {FAKESINK_STATE_ERROR_READY_PAUSED, "2",
75 "Fail state change from READY to PAUSED"},
76 {FAKESINK_STATE_ERROR_PAUSED_PLAYING, "3",
77 "Fail state change from PAUSED to PLAYING"},
78 {FAKESINK_STATE_ERROR_PLAYING_PAUSED, "4",
79 "Fail state change from PLAYING to PAUSED"},
80 {FAKESINK_STATE_ERROR_PAUSED_READY, "5",
81 "Fail state change from PAUSED to READY"},
82 {FAKESINK_STATE_ERROR_READY_NULL, "6",
83 "Fail state change from READY to NULL"},
87 if (!fakesink_state_error_type) {
88 fakesink_state_error_type =
89 g_enum_register_static ("GstFakeSinkStateError", fakesink_state_error);
91 return fakesink_state_error_type;
94 #define _do_init(bla) \
95 GST_DEBUG_CATEGORY_INIT (gst_fakesink_debug, "fakesink", 0, "fakesink element");
97 GST_BOILERPLATE_FULL (GstFakeSink, gst_fakesink, GstElement, GST_TYPE_ELEMENT,
100 static void gst_fakesink_set_clock (GstElement * element, GstClock * clock);
101 static GstPad *gst_fakesink_request_new_pad (GstElement * element,
102 GstPadTemplate * templ, const gchar * unused);
104 static void gst_fakesink_set_property (GObject * object, guint prop_id,
105 const GValue * value, GParamSpec * pspec);
106 static void gst_fakesink_get_property (GObject * object, guint prop_id,
107 GValue * value, GParamSpec * pspec);
109 static GstElementStateReturn gst_fakesink_change_state (GstElement * element);
111 static void gst_fakesink_chain (GstPad * pad, GstData * _data);
113 static guint gst_fakesink_signals[LAST_SIGNAL] = { 0 };
116 gst_fakesink_base_init (gpointer g_class)
118 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
120 gst_element_class_set_details (gstelement_class, &gst_fakesink_details);
121 gst_element_class_add_pad_template (gstelement_class,
122 gst_static_pad_template_get (&fakesink_sink_template));
126 gst_fakesink_class_init (GstFakeSinkClass * klass)
128 GObjectClass *gobject_class;
129 GstElementClass *gstelement_class;
131 gobject_class = (GObjectClass *) klass;
132 gstelement_class = (GstElementClass *) klass;
135 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_NUM_SINKS,
136 g_param_spec_int ("num_sinks", "Number of sinks",
137 "The number of sinkpads", 1, G_MAXINT, 1, G_PARAM_READABLE));
138 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_STATE_ERROR,
139 g_param_spec_enum ("state_error", "State Error",
140 "Generate a state change error", GST_TYPE_FAKESINK_STATE_ERROR,
141 FAKESINK_STATE_ERROR_NONE, G_PARAM_READWRITE));
142 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_LAST_MESSAGE,
143 g_param_spec_string ("last_message", "Last Message",
144 "The message describing current status", NULL, G_PARAM_READABLE));
145 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SYNC,
146 g_param_spec_boolean ("sync", "Sync", "Sync on the clock", FALSE,
148 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SIGNAL_HANDOFFS,
149 g_param_spec_boolean ("signal-handoffs", "Signal handoffs",
150 "Send a signal before unreffing the buffer", FALSE,
152 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_SILENT,
153 g_param_spec_boolean ("silent", "Silent",
154 "Don't produce last_message events", FALSE, G_PARAM_READWRITE));
155 g_object_class_install_property (G_OBJECT_CLASS (klass), ARG_DUMP,
156 g_param_spec_boolean ("dump", "Dump", "Dump received bytes to stdout",
157 FALSE, G_PARAM_READWRITE));
159 gst_fakesink_signals[SIGNAL_HANDOFF] =
160 g_signal_new ("handoff", G_TYPE_FROM_CLASS (klass), G_SIGNAL_RUN_LAST,
161 G_STRUCT_OFFSET (GstFakeSinkClass, handoff), NULL, NULL,
162 gst_marshal_VOID__POINTER_OBJECT, G_TYPE_NONE, 2,
163 GST_TYPE_BUFFER, GST_TYPE_PAD);
165 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_fakesink_set_property);
166 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_fakesink_get_property);
168 gstelement_class->request_new_pad =
169 GST_DEBUG_FUNCPTR (gst_fakesink_request_new_pad);
170 gstelement_class->set_clock = GST_DEBUG_FUNCPTR (gst_fakesink_set_clock);
171 gstelement_class->change_state =
172 GST_DEBUG_FUNCPTR (gst_fakesink_change_state);
176 gst_fakesink_init (GstFakeSink * fakesink)
180 pad = gst_pad_new ("sink", GST_PAD_SINK);
181 gst_element_add_pad (GST_ELEMENT (fakesink), pad);
182 gst_pad_set_chain_function (pad, GST_DEBUG_FUNCPTR (gst_fakesink_chain));
184 fakesink->silent = FALSE;
185 fakesink->dump = FALSE;
186 fakesink->sync = FALSE;
187 fakesink->last_message = NULL;
188 fakesink->state_error = FAKESINK_STATE_ERROR_NONE;
189 fakesink->signal_handoffs = FALSE;
191 GST_FLAG_SET (fakesink, GST_ELEMENT_EVENT_AWARE);
195 gst_fakesink_set_clock (GstElement * element, GstClock * clock)
199 sink = GST_FAKESINK (element);
205 gst_fakesink_request_new_pad (GstElement * element, GstPadTemplate * templ,
206 const gchar * unused)
210 GstFakeSink *fakesink;
212 g_return_val_if_fail (GST_IS_FAKESINK (element), NULL);
214 if (templ->direction != GST_PAD_SINK) {
215 g_warning ("gstfakesink: request new pad that is not a SINK pad\n");
219 fakesink = GST_FAKESINK (element);
221 name = g_strdup_printf ("sink%d", GST_ELEMENT (fakesink)->numsinkpads);
223 sinkpad = gst_pad_new_from_template (templ, name);
225 gst_pad_set_chain_function (sinkpad, GST_DEBUG_FUNCPTR (gst_fakesink_chain));
227 gst_element_add_pad (GST_ELEMENT (fakesink), sinkpad);
233 gst_fakesink_set_property (GObject * object, guint prop_id,
234 const GValue * value, GParamSpec * pspec)
238 /* it's not null if we got it, but it might not be ours */
239 sink = GST_FAKESINK (object);
243 sink->silent = g_value_get_boolean (value);
245 case ARG_STATE_ERROR:
246 sink->state_error = g_value_get_enum (value);
249 sink->dump = g_value_get_boolean (value);
252 sink->sync = g_value_get_boolean (value);
254 case ARG_SIGNAL_HANDOFFS:
255 sink->signal_handoffs = g_value_get_boolean (value);
258 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
264 gst_fakesink_get_property (GObject * object, guint prop_id, GValue * value,
269 /* it's not null if we got it, but it might not be ours */
270 g_return_if_fail (GST_IS_FAKESINK (object));
272 sink = GST_FAKESINK (object);
276 g_value_set_int (value, GST_ELEMENT (sink)->numsinkpads);
278 case ARG_STATE_ERROR:
279 g_value_set_enum (value, sink->state_error);
282 g_value_set_boolean (value, sink->silent);
285 g_value_set_boolean (value, sink->dump);
288 g_value_set_boolean (value, sink->sync);
290 case ARG_SIGNAL_HANDOFFS:
291 g_value_set_boolean (value, sink->signal_handoffs);
293 case ARG_LAST_MESSAGE:
294 g_value_set_string (value, sink->last_message);
297 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
303 gst_fakesink_chain (GstPad * pad, GstData * _data)
305 GstBuffer *buf = GST_BUFFER (_data);
306 GstFakeSink *fakesink;
308 g_return_if_fail (pad != NULL);
309 g_return_if_fail (GST_IS_PAD (pad));
310 g_return_if_fail (buf != NULL);
312 fakesink = GST_FAKESINK (GST_OBJECT_PARENT (pad));
314 if (GST_IS_EVENT (buf)) {
315 GstEvent *event = GST_EVENT (buf);
317 if (!fakesink->silent) {
318 g_free (fakesink->last_message);
320 fakesink->last_message =
321 g_strdup_printf ("chain ******* (%s:%s)E (type: %d) %p",
322 GST_DEBUG_PAD_NAME (pad), GST_EVENT_TYPE (event), event);
324 g_object_notify (G_OBJECT (fakesink), "last_message");
327 switch (GST_EVENT_TYPE (event)) {
328 case GST_EVENT_DISCONTINUOUS:
329 if (fakesink->sync && fakesink->clock) {
330 gint64 value = GST_EVENT_DISCONT_OFFSET (event, 0).value;
332 gst_element_set_time (GST_ELEMENT (fakesink), value);
335 gst_pad_event_default (pad, event);
341 if (fakesink->sync && fakesink->clock) {
342 gst_element_wait (GST_ELEMENT (fakesink), GST_BUFFER_TIMESTAMP (buf));
345 if (!fakesink->silent) {
346 g_free (fakesink->last_message);
348 fakesink->last_message =
349 g_strdup_printf ("chain ******* (%s:%s)< (%d bytes, timestamp: %"
350 GST_TIME_FORMAT ", duration: %" GST_TIME_FORMAT ", offset: %"
351 G_GINT64_FORMAT ", flags: %d) %p", GST_DEBUG_PAD_NAME (pad),
352 GST_BUFFER_SIZE (buf), GST_TIME_ARGS (GST_BUFFER_TIMESTAMP (buf)),
353 GST_TIME_ARGS (GST_BUFFER_DURATION (buf)), GST_BUFFER_OFFSET (buf),
354 GST_BUFFER_FLAGS (buf), buf);
356 g_object_notify (G_OBJECT (fakesink), "last_message");
359 if (fakesink->signal_handoffs)
360 g_signal_emit (G_OBJECT (fakesink), gst_fakesink_signals[SIGNAL_HANDOFF], 0,
363 if (fakesink->dump) {
364 gst_util_dump_mem (GST_BUFFER_DATA (buf), GST_BUFFER_SIZE (buf));
367 gst_buffer_unref (buf);
370 static GstElementStateReturn
371 gst_fakesink_change_state (GstElement * element)
373 GstFakeSink *fakesink = GST_FAKESINK (element);
375 switch (GST_STATE_TRANSITION (element)) {
376 case GST_STATE_NULL_TO_READY:
377 if (fakesink->state_error == FAKESINK_STATE_ERROR_NULL_READY)
380 case GST_STATE_READY_TO_PAUSED:
381 if (fakesink->state_error == FAKESINK_STATE_ERROR_READY_PAUSED)
384 case GST_STATE_PAUSED_TO_PLAYING:
385 if (fakesink->state_error == FAKESINK_STATE_ERROR_PAUSED_PLAYING)
388 case GST_STATE_PLAYING_TO_PAUSED:
389 if (fakesink->state_error == FAKESINK_STATE_ERROR_PLAYING_PAUSED)
392 case GST_STATE_PAUSED_TO_READY:
393 if (fakesink->state_error == FAKESINK_STATE_ERROR_PAUSED_READY)
396 case GST_STATE_READY_TO_NULL:
397 if (fakesink->state_error == FAKESINK_STATE_ERROR_READY_NULL)
399 g_free (fakesink->last_message);
400 fakesink->last_message = NULL;
404 if (GST_ELEMENT_CLASS (parent_class)->change_state)
405 return GST_ELEMENT_CLASS (parent_class)->change_state (element);
407 return GST_STATE_SUCCESS;
410 GST_ELEMENT_ERROR (element, CORE, STATE_CHANGE, (NULL), (NULL));
411 return GST_STATE_FAILURE;