2 * Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
4 * gstmessage.c: GstMessage subsystem
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * @short_description: Lightweight objects to signal the application of pipeline events
24 * @see_also: #GstBus,#GstMiniObject
26 * Messages are implemented as a subclass of #GstMiniObject with a generic
27 * #GstStructure as the content. This allows for writing custom messages without
28 * requiring an API change while allowing a wide range of different types
31 * Messages are posted by objects in the pipeline and are passed to the
32 * application using the #GstBus.
35 #include <string.h> /* memcpy */
37 #include "gst_private.h"
40 #include "gstmessage.h"
41 #include "gsttaglist.h"
45 static void gst_message_init (GTypeInstance * instance, gpointer g_class);
46 static void gst_message_class_init (gpointer g_class, gpointer class_data);
47 static void gst_message_finalize (GstMessage * message);
48 static GstMessage *_gst_message_copy (GstMessage * message);
52 _gst_message_initialize (void)
56 GST_CAT_INFO (GST_CAT_GST_INIT, "init messages");
58 gst_message_get_type ();
60 /* the GstMiniObject types need to be class_ref'd once before it can be
61 * done from multiple threads;
62 * see http://bugzilla.gnome.org/show_bug.cgi?id=304551 */
63 ptr = g_type_class_ref (GST_TYPE_MESSAGE);
64 g_type_class_unref (ptr);
68 gst_message_get_type (void)
70 static GType _gst_message_type;
72 if (G_UNLIKELY (_gst_message_type == 0)) {
73 static const GTypeInfo message_info = {
74 sizeof (GstMessageClass),
77 gst_message_class_init,
86 _gst_message_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
87 "GstMessage", &message_info, 0);
89 return _gst_message_type;
93 gst_message_class_init (gpointer g_class, gpointer class_data)
95 GstMessageClass *message_class = GST_MESSAGE_CLASS (g_class);
97 message_class->mini_object_class.copy =
98 (GstMiniObjectCopyFunction) _gst_message_copy;
99 message_class->mini_object_class.finalize =
100 (GstMiniObjectFinalizeFunction) gst_message_finalize;
104 gst_message_init (GTypeInstance * instance, gpointer g_class)
106 GstMessage *message = GST_MESSAGE (instance);
108 GST_CAT_INFO (GST_CAT_MESSAGE, "new message %p", message);
109 GST_MESSAGE_TIMESTAMP (message) = GST_CLOCK_TIME_NONE;
113 gst_message_finalize (GstMessage * message)
115 g_return_if_fail (message != NULL);
117 GST_CAT_INFO (GST_CAT_MESSAGE, "finalize message %p", message);
119 if (GST_MESSAGE_SRC (message)) {
120 gst_object_unref (GST_MESSAGE_SRC (message));
121 GST_MESSAGE_SRC (message) = NULL;
125 GST_MESSAGE_LOCK (message);
126 GST_MESSAGE_SIGNAL (message);
127 GST_MESSAGE_UNLOCK (message);
130 if (message->structure) {
131 gst_structure_set_parent_refcount (message->structure, NULL);
132 gst_structure_free (message->structure);
137 _gst_message_copy (GstMessage * message)
141 GST_CAT_INFO (GST_CAT_MESSAGE, "copy message %p", message);
143 copy = (GstMessage *) gst_mini_object_new (GST_TYPE_MESSAGE);
145 /* FIXME, need to copy relevant data from the miniobject. */
146 //memcpy (copy, message, sizeof (GstMessage));
148 GST_MESSAGE_GET_LOCK (copy) = GST_MESSAGE_GET_LOCK (message);
149 GST_MESSAGE_COND (copy) = GST_MESSAGE_COND (message);
150 GST_MESSAGE_TYPE (copy) = GST_MESSAGE_TYPE (message);
151 GST_MESSAGE_TIMESTAMP (copy) = GST_MESSAGE_TIMESTAMP (message);
153 if (GST_MESSAGE_SRC (message)) {
154 GST_MESSAGE_SRC (copy) = gst_object_ref (GST_MESSAGE_SRC (message));
157 if (message->structure) {
158 copy->structure = gst_structure_copy (message->structure);
159 gst_structure_set_parent_refcount (copy->structure,
160 &message->mini_object.refcount);
167 gst_message_new (GstMessageType type, GstObject * src)
171 message = (GstMessage *) gst_mini_object_new (GST_TYPE_MESSAGE);
173 GST_CAT_INFO (GST_CAT_MESSAGE, "creating new message %p %d", message, type);
175 message->type = type;
177 message->src = gst_object_ref (src);
178 GST_CAT_DEBUG_OBJECT (GST_CAT_MESSAGE, src, "message source");
181 GST_CAT_DEBUG (GST_CAT_MESSAGE, "NULL message source");
183 message->structure = NULL;
189 * gst_message_new_eos:
190 * @src: The object originating the message.
192 * Create a new eos message. This message is generated and posted in
193 * the sink elements of a GstBin. The bin will only forward the EOS
194 * message to the application if all sinks have posted an EOS message.
196 * Returns: The new eos message.
201 gst_message_new_eos (GstObject * src)
205 message = gst_message_new (GST_MESSAGE_EOS, src);
211 * gst_message_new_error:
212 * @src: The object originating the message.
213 * @error: The GError for this message.
214 * @debug: A debugging string for something or other.
216 * Create a new error message. The message will copy @error and
217 * @debug. This message is posted by element when a fatal event
218 * occured. The pipeline will probably (partially) stop.
220 * Returns: The new error message.
225 gst_message_new_error (GstObject * src, GError * error, gchar * debug)
230 message = gst_message_new (GST_MESSAGE_ERROR, src);
231 /* gst_structure_new takes copies of the types passed in */
232 s = gst_structure_new ("GstMessageError", "gerror", GST_TYPE_G_ERROR, error,
233 "debug", G_TYPE_STRING, debug, NULL);
234 gst_structure_set_parent_refcount (s, &message->mini_object.refcount);
235 message->structure = s;
241 * gst_message_new_warning:
242 * @src: The object originating the message.
243 * @error: The GError for this message.
244 * @debug: A debugging string for something or other.
246 * Create a new warning message. The message will make copies of @error and
249 * Returns: The new warning message.
254 gst_message_new_warning (GstObject * src, GError * error, gchar * debug)
259 message = gst_message_new (GST_MESSAGE_WARNING, src);
260 /* gst_structure_new takes copies of the types passed in */
261 s = gst_structure_new ("GstMessageWarning", "gerror", GST_TYPE_G_ERROR, error,
262 "debug", G_TYPE_STRING, debug, NULL);
263 gst_structure_set_parent_refcount (s, &message->mini_object.refcount);
264 message->structure = s;
270 * gst_message_new_tag:
271 * @src: The object originating the message.
272 * @tag_list: The tag list for the message.
274 * Create a new tag message. The message will take ownership of the tag list.
275 * The message is posted by elements that discovered a new taglist.
277 * Returns: The new tag message.
282 gst_message_new_tag (GstObject * src, GstTagList * tag_list)
286 g_return_val_if_fail (GST_IS_STRUCTURE (tag_list), NULL);
288 message = gst_message_new (GST_MESSAGE_TAG, src);
289 gst_structure_set_parent_refcount (tag_list, &message->mini_object.refcount);
290 message->structure = tag_list;
296 * gst_message_new_state_change:
297 * @src: The object originating the message.
298 * @old: The previous state.
299 * @new: The new (current) state.
301 * Create a state change message. This message is posted whenever an element changed
304 * Returns: The new state change message.
309 gst_message_new_state_changed (GstObject * src, GstState old, GstState new)
314 message = gst_message_new (GST_MESSAGE_STATE_CHANGED, src);
316 s = gst_structure_new ("GstMessageState", "old-state", G_TYPE_INT, (gint) old,
317 "new-state", G_TYPE_INT, (gint) new, NULL);
318 gst_structure_set_parent_refcount (s, &message->mini_object.refcount);
319 message->structure = s;
325 * gst_message_new_segment_start:
326 * @src: The object originating the message.
327 * @timestamp: The timestamp of the segment being played
329 * Create a new segment message. This message is posted by elements that
330 * start playback of a segment as a result of a segment seek. This message
331 * is not received by the application but is used for maintenance reasons in
332 * container elements.
334 * Returns: The new segment start message.
339 gst_message_new_segment_start (GstObject * src, GstClockTime timestamp)
344 message = gst_message_new (GST_MESSAGE_SEGMENT_START, src);
346 s = gst_structure_new ("GstMessageSegmentStart", "timestamp", G_TYPE_INT64,
347 (gint64) timestamp, NULL);
348 gst_structure_set_parent_refcount (s, &message->mini_object.refcount);
349 message->structure = s;
355 * gst_message_new_segment_done:
356 * @src: The object originating the message.
357 * @timestamp: The timestamp of the segment being played
359 * Create a new segment done message. This message is posted by elements that
360 * finish playback of a segment as a result of a segment seek. This message
361 * is received by the application after all elements that posted a segment_start
362 * have posted the segment_done.
364 * Returns: The new segment done message.
369 gst_message_new_segment_done (GstObject * src, GstClockTime timestamp)
374 message = gst_message_new (GST_MESSAGE_SEGMENT_DONE, src);
376 s = gst_structure_new ("GstMessageSegmentDone", "timestamp", G_TYPE_INT64,
377 (gint64) timestamp, NULL);
378 gst_structure_set_parent_refcount (s, &message->mini_object.refcount);
379 message->structure = s;
385 * gst_message_new_custom:
386 * @type: The #GstMessageType to distinguish messages
387 * @src: The object originating the message.
388 * @structure: The structure for the message. The message will take ownership of
391 * Create a new custom-typed message. This can be used for anything not
392 * handled by other message-specific functions to pass a message to the
393 * app. The structure field can be NULL.
395 * Returns: The new message.
400 gst_message_new_custom (GstMessageType type, GstObject * src,
401 GstStructure * structure)
405 message = gst_message_new (type, src);
407 gst_structure_set_parent_refcount (structure,
408 &message->mini_object.refcount);
409 message->structure = structure;
415 * gst_message_get_structure:
416 * @message: The #GstMessage.
418 * Access the structure of the message.
420 * Returns: The structure of the message. The structure is still
421 * owned by the message, which means that you should not free it and
422 * that the pointer becomes invalid when you free the message.
427 gst_message_get_structure (GstMessage * message)
429 g_return_val_if_fail (GST_IS_MESSAGE (message), NULL);
431 return message->structure;
435 * gst_message_parse_tag:
436 * @message: A valid #GstMessage of type GST_MESSAGE_TAG.
438 * Extracts the tag list from the GstMessage. The tag list returned in the
439 * output argument is a copy; the caller must free it when done.
444 gst_message_parse_tag (GstMessage * message, GstTagList ** tag_list)
446 g_return_if_fail (GST_IS_MESSAGE (message));
447 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_TAG);
449 *tag_list = (GstTagList *) gst_structure_copy (message->structure);
453 * gst_message_parse_state_changed:
454 * @message: A valid #GstMessage of type GST_MESSAGE_STATE_CHANGED.
456 * Extracts the old and new states from the GstMessage.
461 gst_message_parse_state_changed (GstMessage * message, GstState * old,
464 g_return_if_fail (GST_IS_MESSAGE (message));
465 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_STATE_CHANGED);
467 if (!gst_structure_get_int (message->structure, "old-state", (gint *) old))
468 g_assert_not_reached ();
469 if (!gst_structure_get_int (message->structure, "new-state", (gint *) new))
470 g_assert_not_reached ();
474 * gst_message_parse_error:
475 * @message: A valid #GstMessage of type GST_MESSAGE_ERROR.
477 * Extracts the GError and debug string from the GstMessage. The values returned
478 * in the output arguments are copies; the caller must free them when done.
483 gst_message_parse_error (GstMessage * message, GError ** gerror, gchar ** debug)
485 const GValue *error_gvalue;
488 g_return_if_fail (GST_IS_MESSAGE (message));
489 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR);
491 error_gvalue = gst_structure_get_value (message->structure, "gerror");
492 g_return_if_fail (error_gvalue != NULL);
493 g_return_if_fail (G_VALUE_TYPE (error_gvalue) == GST_TYPE_G_ERROR);
495 error_val = (GError *) g_value_get_boxed (error_gvalue);
497 *gerror = g_error_copy (error_val);
500 *debug = g_strdup (gst_structure_get_string (message->structure, "debug"));
504 * gst_message_parse_warning:
505 * @message: A valid #GstMessage of type GST_MESSAGE_WARNING.
507 * Extracts the GError and debug string from the GstMessage. The values returned
508 * in the output arguments are copies; the caller must free them when done.
513 gst_message_parse_warning (GstMessage * message, GError ** gerror,
516 const GValue *error_gvalue;
519 g_return_if_fail (GST_IS_MESSAGE (message));
520 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_WARNING);
522 error_gvalue = gst_structure_get_value (message->structure, "gerror");
523 g_return_if_fail (error_gvalue != NULL);
524 g_return_if_fail (G_VALUE_TYPE (error_gvalue) == GST_TYPE_G_ERROR);
526 error_val = (GError *) g_value_get_boxed (error_gvalue);
528 *gerror = g_error_copy (error_val);
532 *debug = g_strdup (gst_structure_get_string (message->structure, "debug"));
536 * gst_message_parse_segment_start:
537 * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_START.
539 * Extracts the timestamp from the segment start message.
544 gst_message_parse_segment_start (GstMessage * message, GstClockTime * timestamp)
546 const GValue *time_gvalue;
548 g_return_if_fail (GST_IS_MESSAGE (message));
549 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_START);
551 time_gvalue = gst_structure_get_value (message->structure, "timstamp");
552 g_return_if_fail (time_gvalue != NULL);
553 g_return_if_fail (G_VALUE_TYPE (time_gvalue) == G_TYPE_INT64);
556 *timestamp = (GstClockTime) g_value_get_int64 (time_gvalue);
560 * gst_message_parse_segment_done:
561 * @message: A valid #GstMessage of type GST_MESSAGE_SEGMENT_DONE.
563 * Extracts the timestamp from the segment done message.
568 gst_message_parse_segment_done (GstMessage * message, GstClockTime * timestamp)
570 const GValue *time_gvalue;
572 g_return_if_fail (GST_IS_MESSAGE (message));
573 g_return_if_fail (GST_MESSAGE_TYPE (message) == GST_MESSAGE_SEGMENT_DONE);
575 time_gvalue = gst_structure_get_value (message->structure, "timstamp");
576 g_return_if_fail (time_gvalue != NULL);
577 g_return_if_fail (G_VALUE_TYPE (time_gvalue) == G_TYPE_INT64);
580 *timestamp = (GstClockTime) g_value_get_int64 (time_gvalue);