gstreamer/gst/gstmessage.c

272 lines
6.1 KiB
C
Raw Normal View History

/* GStreamer
* Copyright (C) 2004 Wim Taymans <wim@fluendo.com>
*
* gstmessage.c: GstMessage subsystem
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <string.h> /* memcpy */
#include "gst_private.h"
#include "gstdata_private.h"
#include "gstinfo.h"
#include "gstmemchunk.h"
#include "gstmessage.h"
#include "gsttag.h"
#ifndef GST_DISABLE_TRACE
/* #define GST_WITH_ALLOC_TRACE */
#include "gsttrace.h"
static GstAllocTrace *_message_trace;
#endif
static GstMemChunk *chunk;
/* #define MEMPROF */
GType _gst_message_type;
void
_gst_message_initialize (void)
{
/* register the type */
_gst_message_type = g_boxed_type_register_static ("GstMessage",
(GBoxedCopyFunc) gst_data_copy, (GBoxedFreeFunc) gst_data_unref);
#ifndef GST_DISABLE_TRACE
_message_trace = gst_alloc_trace_register (GST_MESSAGE_TRACE_NAME);
#endif
chunk = gst_mem_chunk_new ("GstMessageChunk", sizeof (GstMessage),
sizeof (GstMessage) * 50, 0);
}
static GstMessage *
_gst_message_copy (GstMessage * message)
{
GstMessage *copy;
More MT fixes, added design document describing refcounting policies used in GStreamer and locking involved. Original commit message from CVS: * docs/design/part-MT-refcounting.txt: * docs/design/part-conventions.txt: * gst/gstbin.c: (gst_bin_set_index), (gst_bin_set_clock), (gst_bin_add_func), (gst_bin_remove_func), (gst_bin_iterate_elements), (gst_bin_change_state), (gst_bin_dispose), (gst_bin_get_by_name_recurse_up): * gst/gstcaps.c: * gst/gstelement.c: (gst_element_add_pad), (gst_element_remove_pad), (pad_compare_name), (gst_element_get_static_pad), (gst_element_get_request_pad), (gst_element_get_pad), (gst_element_iterate_pads), (gst_element_class_get_pad_template_list), (gst_element_class_get_pad_template), (gst_element_get_random_pad), (gst_element_get_event_masks), (gst_element_send_event), (gst_element_seek), (gst_element_get_query_types), (gst_element_query), (gst_element_get_formats), (gst_element_convert), (gst_element_post_message), (gst_element_set_locked_state), (gst_element_get_state), (gst_element_set_state), (gst_element_pads_activate), (gst_element_dispose), (gst_element_set_manager_func), (gst_element_get_manager): * gst/gstelement.h: * gst/gstiterator.c: (gst_iterator_new), (gst_list_iterator_next), (gst_list_iterator_resync), (gst_list_iterator_free), (gst_iterator_new_list): * gst/gstiterator.h: * gst/gstmessage.c: (_gst_message_copy): * gst/gstobject.c: (gst_object_class_init), (gst_object_init), (gst_object_ref), (gst_object_unref), (gst_object_sink), (gst_object_replace), (gst_object_dispose), (gst_object_dispatch_properties_changed), (gst_object_set_name), (gst_object_set_parent), (gst_object_get_parent), (gst_object_unparent), (gst_object_check_uniqueness), (gst_object_get_path_string): * gst/gstobject.h: * gst/gstpad.c: (gst_pad_dispose), (gst_pad_set_active), (gst_pad_is_active), (gst_pad_set_blocked_async), (gst_pad_is_blocked), (gst_pad_unlink), (gst_pad_is_linked), (gst_pad_link_prepare_filtered), (gst_pad_link_filtered), (gst_pad_get_real_parent), (gst_pad_relink_filtered), (gst_pad_get_peer), (gst_pad_realize), (gst_pad_get_allowed_caps), (gst_pad_alloc_buffer), (gst_pad_push), (gst_pad_pull), (gst_pad_pull_range), (gst_pad_push_event): * gst/gstpad.h: * gst/gstpipeline.c: (gst_pipeline_init), (gst_pipeline_dispose), (is_eos), (pipeline_bus_handler): * gst/gstutils.c: (gst_element_get_compatible_pad_filtered), (gst_element_link_pads_filtered), (gst_element_unlink): * gst/parse/grammar.y: * tools/gst-compprep.c: (main): * tools/gst-inspect.c: (print_pad_info): * tools/gst-launch.c: (main): * tools/gst-xmlinspect.c: (print_element_info): More MT fixes, added design document describing refcounting policies used in GStreamer and locking involved. Fixed unsafe ghostpad dereffing. Removed old unsafe methods.
2004-12-13 11:33:55 +00:00
GST_CAT_INFO (GST_CAT_MESSAGE, "copy message %p", message);
copy = gst_mem_chunk_alloc (chunk);
#ifndef GST_DISABLE_TRACE
gst_alloc_trace_new (_message_trace, copy);
#endif
memcpy (copy, message, sizeof (GstMessage));
More MT fixes, added design document describing refcounting policies used in GStreamer and locking involved. Original commit message from CVS: * docs/design/part-MT-refcounting.txt: * docs/design/part-conventions.txt: * gst/gstbin.c: (gst_bin_set_index), (gst_bin_set_clock), (gst_bin_add_func), (gst_bin_remove_func), (gst_bin_iterate_elements), (gst_bin_change_state), (gst_bin_dispose), (gst_bin_get_by_name_recurse_up): * gst/gstcaps.c: * gst/gstelement.c: (gst_element_add_pad), (gst_element_remove_pad), (pad_compare_name), (gst_element_get_static_pad), (gst_element_get_request_pad), (gst_element_get_pad), (gst_element_iterate_pads), (gst_element_class_get_pad_template_list), (gst_element_class_get_pad_template), (gst_element_get_random_pad), (gst_element_get_event_masks), (gst_element_send_event), (gst_element_seek), (gst_element_get_query_types), (gst_element_query), (gst_element_get_formats), (gst_element_convert), (gst_element_post_message), (gst_element_set_locked_state), (gst_element_get_state), (gst_element_set_state), (gst_element_pads_activate), (gst_element_dispose), (gst_element_set_manager_func), (gst_element_get_manager): * gst/gstelement.h: * gst/gstiterator.c: (gst_iterator_new), (gst_list_iterator_next), (gst_list_iterator_resync), (gst_list_iterator_free), (gst_iterator_new_list): * gst/gstiterator.h: * gst/gstmessage.c: (_gst_message_copy): * gst/gstobject.c: (gst_object_class_init), (gst_object_init), (gst_object_ref), (gst_object_unref), (gst_object_sink), (gst_object_replace), (gst_object_dispose), (gst_object_dispatch_properties_changed), (gst_object_set_name), (gst_object_set_parent), (gst_object_get_parent), (gst_object_unparent), (gst_object_check_uniqueness), (gst_object_get_path_string): * gst/gstobject.h: * gst/gstpad.c: (gst_pad_dispose), (gst_pad_set_active), (gst_pad_is_active), (gst_pad_set_blocked_async), (gst_pad_is_blocked), (gst_pad_unlink), (gst_pad_is_linked), (gst_pad_link_prepare_filtered), (gst_pad_link_filtered), (gst_pad_get_real_parent), (gst_pad_relink_filtered), (gst_pad_get_peer), (gst_pad_realize), (gst_pad_get_allowed_caps), (gst_pad_alloc_buffer), (gst_pad_push), (gst_pad_pull), (gst_pad_pull_range), (gst_pad_push_event): * gst/gstpad.h: * gst/gstpipeline.c: (gst_pipeline_init), (gst_pipeline_dispose), (is_eos), (pipeline_bus_handler): * gst/gstutils.c: (gst_element_get_compatible_pad_filtered), (gst_element_link_pads_filtered), (gst_element_unlink): * gst/parse/grammar.y: * tools/gst-compprep.c: (main): * tools/gst-inspect.c: (print_pad_info): * tools/gst-launch.c: (main): * tools/gst-xmlinspect.c: (print_element_info): More MT fixes, added design document describing refcounting policies used in GStreamer and locking involved. Fixed unsafe ghostpad dereffing. Removed old unsafe methods.
2004-12-13 11:33:55 +00:00
if (GST_MESSAGE_SRC (copy)) {
gst_object_ref (GST_MESSAGE_SRC (copy));
}
/* FIXME copy/ref additional fields */
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_TAG:
copy->message_data.structure.structure =
gst_tag_list_copy ((GstTagList *) message->message_data.structure.
structure);
break;
default:
break;
}
return copy;
}
static void
_gst_message_free (GstMessage * message)
{
GST_CAT_INFO (GST_CAT_MESSAGE, "freeing message %p", message);
if (GST_MESSAGE_SRC (message)) {
gst_object_unref (GST_MESSAGE_SRC (message));
}
if (message->lock) {
GST_MESSAGE_LOCK (message);
GST_MESSAGE_SIGNAL (message);
GST_MESSAGE_UNLOCK (message);
}
switch (GST_MESSAGE_TYPE (message)) {
case GST_MESSAGE_ERROR:
case GST_MESSAGE_WARNING:
g_error_free (GST_MESSAGE_ERROR_GERROR (message));
g_free (GST_MESSAGE_ERROR_DEBUG (message));
break;
case GST_MESSAGE_TAG:
if (GST_IS_TAG_LIST (message->message_data.tag.list)) {
gst_tag_list_free (message->message_data.tag.list);
} else {
g_warning ("tag message %p didn't contain a valid tag list!", message);
GST_ERROR ("tag message %p didn't contain a valid tag list!", message);
}
break;
default:
break;
}
_GST_DATA_DISPOSE (GST_DATA (message));
#ifndef GST_DISABLE_TRACE
gst_alloc_trace_free (_message_trace, message);
#endif
gst_mem_chunk_free (chunk, message);
}
GType
gst_message_get_type (void)
{
return _gst_message_type;
}
/**
* gst_message_new:
* @type: The type of the new message
*
* Allocate a new message of the given type.
*
* Returns: A new message.
*
* MT safe.
*/
GstMessage *
gst_message_new (GstMessageType type, GstObject * src)
{
GstMessage *message;
message = gst_mem_chunk_alloc0 (chunk);
#ifndef GST_DISABLE_TRACE
gst_alloc_trace_new (_message_trace, message);
#endif
GST_CAT_INFO (GST_CAT_MESSAGE, "creating new message %p %d", message, type);
_GST_DATA_INIT (GST_DATA (message),
_gst_message_type,
0,
(GstDataFreeFunction) _gst_message_free,
(GstDataCopyFunction) _gst_message_copy);
GST_MESSAGE_TYPE (message) = type;
GST_MESSAGE_TIMESTAMP (message) = G_GINT64_CONSTANT (0);
gst_object_ref (src);
GST_MESSAGE_SRC (message) = src;
return message;
}
/**
* gst_message_new_eos:
*
* Create a new eos message.
*
* Returns: The new eos message.
*
* MT safe.
*/
GstMessage *
gst_message_new_eos (GstObject * src)
{
GstMessage *message;
message = gst_message_new (GST_MESSAGE_EOS, src);
return message;
}
/**
* gst_message_new_error:
*
* Create a new error message.
*
* Returns: The new error message.
*
* MT safe.
*/
GstMessage *
gst_message_new_error (GstObject * src, GError * error, gchar * debug)
{
GstMessage *message;
message = gst_message_new (GST_MESSAGE_ERROR, src);
GST_MESSAGE_ERROR_GERROR (message) = error;
GST_MESSAGE_ERROR_DEBUG (message) = debug;
return message;
}
/**
* gst_message_new_warning:
*
* Create a new warning message.
*
* Returns: The new warning message.
*
* MT safe.
*/
GstMessage *
gst_message_new_warning (GstObject * src, GError * error, gchar * debug)
{
GstMessage *message;
message = gst_message_new (GST_MESSAGE_WARNING, src);
GST_MESSAGE_WARNING_GERROR (message) = error;
GST_MESSAGE_WARNING_DEBUG (message) = debug;
return message;
}
/**
* gst_message_new_tag:
*
* Create a new tag message.
*
* Returns: The new tag message.
*
* MT safe.
*/
GstMessage *
gst_message_new_tag (GstObject * src, GstTagList * tag_list)
{
GstMessage *message;
message = gst_message_new (GST_MESSAGE_TAG, src);
GST_MESSAGE_TAG_LIST (message) = tag_list;
return message;
}
gst/gstmessage.h (GstMessageType): Turned into flags. Added Original commit message from CVS: 2005-02-21 Andy Wingo <wingo@pobox.com> * gst/gstmessage.h (GstMessageType): Turned into flags. Added GST_MESSAGE_ANY as an OR of all flags. (GST_MESSAGE_PARSE_STATE_CHANGED): New terrible macro. Will be made into a function soon. (GstMessage): Add a state_changed structure to the union. The union will die soon in favor of a single GstStructure tho. (gst_message_new_state_changed): New API. * gst/gstmessage.c (gst_message_new_state_changed): New API. * tools/gst-launch.c (check_intr): Set the state of the pipeline to PAUSED here; the poll will catch the state change. (event_loop): New function, polls the pipeline bus for events. Can block until eos/error/state change, or just handle the pending events. (main): Changed to use event_loop instead of running a main loop. * gst/gstbus.h (gst_bus_poll): Added. (gst_bus_peek): Return non-const; the message is refcounted anyway. * gst/gstbus.c (gst_bus_init): Replace the GAsyncQueue with a GQueue+mutex to allow for _peek. (The wake-up functionality provided by GAsyncQueue is already done by our socketpair.) All queue users changed to lock, operate, and unlock. (gst_bus_post): Check the retval of write(2) and handle errno. (gst_bus_peek): Implemented. (gst_bus_pop, gst_bus_peek, bus_callback): Because the socketpair is used to wake up the GSource, read off the character in the GSource handler and not in pop/peek. This is because a peek will require a pop in the future, and you can't read off the char twice. Deal with errno in the read. (bus_callback): Interpret the handler return value as whether or not to pop the message from the bus. (poll_handler, poll_timeout, gst_bus_poll): New API. gst_bus_poll is meant to replace the while(gst_bin_iterate()) idiom.
2005-02-21 11:54:55 +00:00
/**
* gst_message_new_state_change:
*
* Create a state change message.
*
* Returns: The new state change message.
*
* MT safe.
*/
GstMessage *
gst_message_new_state_changed (GstObject * src, GstElementState old,
GstElementState new)
{
GstMessage *message;
message = gst_message_new (GST_MESSAGE_STATE_CHANGED, src);
message->message_data.state_changed.old = old;
message->message_data.state_changed.new = new;
return message;
}