gst/gstcaps.c: Move the poisoning to allow a NULL structure
[platform/upstream/gstreamer.git] / gst / gstevent.c
1 /* GStreamer
2  * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
3  *                    2000 Wim Taymans <wim.taymans@chello.be>
4  *
5  * gstevent.h: Header for GstEvent subsystem
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 #include <string.h>             /* memcpy */
24
25 #include "gst_private.h"
26 #include "gstdata_private.h"
27
28 #include "gstinfo.h"
29 #include "gstmemchunk.h"
30 #include "gstevent.h"
31 #include "gstlog.h"
32 #include "gsttag.h"
33
34 #ifndef GST_DISABLE_TRACE
35 /* #define GST_WITH_ALLOC_TRACE */
36 #include "gsttrace.h"
37 static GstAllocTrace *_event_trace;
38 #endif
39
40 static GstMemChunk *chunk;
41
42 /* #define MEMPROF */
43
44 GType _gst_event_type;
45
46 void
47 _gst_event_initialize (void)
48 {
49   /* register the type */
50   _gst_event_type = g_boxed_type_register_static ("GstEvent",
51                                                (GBoxedCopyFunc) gst_data_ref,
52                                                (GBoxedFreeFunc) gst_data_unref);
53
54 #ifndef GST_DISABLE_TRACE
55   _event_trace = gst_alloc_trace_register (GST_EVENT_TRACE_NAME);
56 #endif
57
58   chunk = gst_mem_chunk_new ("GstEventChunk", sizeof (GstEvent),
59                                  sizeof (GstEvent) * 50, 0);
60 }
61
62 static GstEvent*
63 _gst_event_copy (GstEvent *event)
64 {
65   GstEvent *copy;
66
67   copy = gst_mem_chunk_alloc (chunk);
68 #ifndef GST_DISABLE_TRACE
69   gst_alloc_trace_new (_event_trace, copy);
70 #endif
71
72   memcpy (copy, event, sizeof (GstEvent));
73   
74   /* FIXME copy/ref additional fields */
75   switch (GST_EVENT_TYPE (event)) {
76     case GST_EVENT_TAG:
77       copy->event_data.structure.structure = gst_structure_copy (event->event_data.structure.structure);
78     default:
79       break;
80   }
81
82   return copy;
83 }
84
85 static void
86 _gst_event_free (GstEvent* event)
87 {
88   GST_CAT_INFO (GST_CAT_EVENT, "freeing event %p", event);
89
90   if (GST_EVENT_SRC (event)) {
91     gst_object_unref (GST_EVENT_SRC (event));
92   }
93   switch (GST_EVENT_TYPE (event)) {
94     case GST_EVENT_TAG:
95       gst_tag_list_free (event->event_data.structure.structure);
96     case GST_EVENT_NAVIGATION:
97       gst_structure_free (event->event_data.structure.structure);
98     default:
99       break;
100   }
101   _GST_DATA_DISPOSE (GST_DATA (event));
102 #ifndef GST_DISABLE_TRACE
103   gst_alloc_trace_free (_event_trace, event);
104 #endif
105   gst_mem_chunk_free (chunk, event);
106 }
107
108 /**
109  * gst_event_masks_contains:
110  * @masks: The eventmask array to search
111  * @mask: the event mask to find
112  *
113  * See if the given eventmask is inside the eventmask array.
114  *
115  * Returns: TRUE if the eventmask is found inside the array
116  */
117 gboolean
118 gst_event_masks_contains (const GstEventMask *masks, GstEventMask *mask)
119 {
120   g_return_val_if_fail (mask != NULL, FALSE);
121
122   if (!masks)
123     return FALSE;
124   
125   while (masks->type) {
126     if (masks->type == mask->type &&
127         (masks->flags & mask->flags) == mask->flags)
128       return TRUE;
129
130     masks++;
131   }
132
133   return FALSE;
134 }
135
136 GType
137 gst_event_get_type (void)
138 {
139   return _gst_event_type;
140 }
141
142 /**
143  * gst_event_new:
144  * @type: The type of the new event
145  *
146  * Allocate a new event of the given type.
147  *
148  * Returns: A new event.
149  */
150 GstEvent*
151 gst_event_new (GstEventType type)
152 {
153   GstEvent *event;
154
155   event = gst_mem_chunk_alloc0 (chunk);
156 #ifndef GST_DISABLE_TRACE
157   gst_alloc_trace_new (_event_trace, event);
158 #endif
159
160   GST_CAT_INFO (GST_CAT_EVENT, "creating new event %p %d", event, type);
161
162   _GST_DATA_INIT (GST_DATA (event),
163                   _gst_event_type,
164                   0,
165                   (GstDataFreeFunction) _gst_event_free,
166                   (GstDataCopyFunction) _gst_event_copy);
167
168   GST_EVENT_TYPE (event) = type;
169   GST_EVENT_TIMESTAMP (event) = G_GINT64_CONSTANT (0);
170   GST_EVENT_SRC (event) = NULL;
171
172   return event;
173 }
174
175 /**
176  * gst_event_new_seek:
177  * @type: The type of the seek event
178  * @offset: The offset of the seek
179  *
180  * Allocate a new seek event with the given parameters.
181  *
182  * Returns: A new seek event.
183  */
184 GstEvent*       
185 gst_event_new_seek (GstSeekType type, gint64 offset)
186 {
187   GstEvent *event;
188
189   event = gst_event_new (GST_EVENT_SEEK);
190
191   GST_EVENT_SEEK_TYPE (event) = type;
192   GST_EVENT_SEEK_OFFSET (event) = offset;
193   GST_EVENT_SEEK_ENDOFFSET (event) = -1;
194
195   return event;
196 }
197
198 /**
199  * gst_event_new_discontinuous_valist:
200  * @new_media: A flag indicating a new media type starts
201  * @format1: The format of the discont value
202  * @var_args: more discont values and formats
203  *
204  * Allocate a new discontinuous event with the given format/value pairs. Note
205  * that the values are of type gint64 - you may not use simple integers such
206  * as "0" when calling this function, always cast them like "(gint64) 0".
207  * Terminate the list with #GST_FORMAT_UNDEFINED.
208  *
209  * Returns: A new discontinuous event.
210  */
211 GstEvent*
212 gst_event_new_discontinuous_valist (gboolean new_media, GstFormat format1, va_list var_args)
213 {
214   GstEvent *event;
215   gint count = 0;
216
217   event = gst_event_new (GST_EVENT_DISCONTINUOUS);
218   GST_EVENT_DISCONT_NEW_MEDIA (event) = new_media;
219
220   while (format1 != GST_FORMAT_UNDEFINED && count < 8) {
221
222     GST_EVENT_DISCONT_OFFSET (event, count).format = format1 & GST_SEEK_FORMAT_MASK;
223     GST_EVENT_DISCONT_OFFSET (event, count).value = va_arg (var_args, gint64);
224
225     format1 = va_arg (var_args, GstFormat);
226
227     count++;
228   }
229
230   GST_EVENT_DISCONT_OFFSET_LEN (event) = count;
231                     
232   return event;
233 }
234
235 /**
236  * gst_event_new_discontinuous:
237  * @new_media: A flag indicating a new media type starts
238  * @format1: The format of the discont value
239  * @...: more discont values and formats
240  *
241  * Allocate a new discontinuous event with the given format/value pairs. Note
242  * that the values are of type gint64 - you may not use simple integers such
243  * as "0" when calling this function, always cast them like "(gint64) 0".
244  * Terminate the list with #GST_FORMAT_UNDEFINED.
245  *
246  * Returns: A new discontinuous event.
247  */
248 GstEvent*
249 gst_event_new_discontinuous (gboolean new_media, GstFormat format1, ...)
250 {
251   va_list var_args;
252   GstEvent *event;
253
254   va_start (var_args, format1);
255
256   event = gst_event_new_discontinuous_valist (new_media, format1, var_args);
257
258   va_end (var_args);
259
260   return event;
261 }
262
263 /**
264  * gst_event_discont_get_value:
265  * @event: The event to query
266  * @format: The format of the discont value
267  * @value: A pointer to the value
268  *
269  * Get the value for the given format in the dicont event.
270  *
271  * Returns: TRUE if the discont event caries the specified format/value pair.
272  */
273 gboolean
274 gst_event_discont_get_value (GstEvent *event, GstFormat format, gint64 *value)
275 {
276   gint i, n;
277
278   g_return_val_if_fail (event != NULL, FALSE);
279   g_return_val_if_fail (value != NULL, FALSE);
280
281   n = GST_EVENT_DISCONT_OFFSET_LEN (event);
282
283   for (i = 0; i < n; i++) {
284     if (GST_EVENT_DISCONT_OFFSET(event,i).format == format) {
285       *value = GST_EVENT_DISCONT_OFFSET(event,i).value;
286       return TRUE;
287     }
288   }
289   
290   return FALSE;
291 }
292
293
294 /**
295  * gst_event_new_size:
296  * @format: The format of the size value
297  * @value: The value of the size event
298  *
299  * Create a new size event with the given values.
300  *
301  * Returns: The new size event.
302  */
303 GstEvent*
304 gst_event_new_size (GstFormat format, gint64 value)
305 {
306   GstEvent *event;
307
308   event = gst_event_new (GST_EVENT_SIZE);
309
310   GST_EVENT_SIZE_FORMAT (event) = format;
311   GST_EVENT_SIZE_VALUE (event) = value;
312   
313   return event;
314 }
315
316
317 /**
318  * gst_event_new_segment_seek:
319  * @type: The type of the seek event
320  * @start: The start offset of the seek
321  * @stop: The stop offset of the seek
322  *
323  * Allocate a new segment seek event with the given parameters. 
324  *
325  * Returns: A new segment seek event.
326  */
327 GstEvent*       
328 gst_event_new_segment_seek (GstSeekType type, gint64 start, gint64 stop)
329 {
330   GstEvent *event;
331
332   g_return_val_if_fail (start < stop, NULL);
333
334   event = gst_event_new (GST_EVENT_SEEK_SEGMENT);
335
336   GST_EVENT_SEEK_TYPE (event) = type;
337   GST_EVENT_SEEK_OFFSET (event) = start;
338   GST_EVENT_SEEK_ENDOFFSET (event) = stop;
339
340   return event;
341 }