gstevent.c 8.58 KB
Newer Older
1 2 3 4
/* GStreamer
 * Copyright (C) 1999,2000 Erik Walthinsen <omega@cse.ogi.edu>
 *                    2000 Wim Taymans <wim.taymans@chello.be>
 *
5
 * gstevent.c: GstEvent subsystem
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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.
 */

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 31
#include "gstevent.h"
#include "gstlog.h"
Benjamin Otte's avatar
Benjamin Otte committed
32 33
#include "gsttag.h"

Wim Taymans's avatar
Wim Taymans committed
34
#ifndef GST_DISABLE_TRACE
35 36
/* #define GST_WITH_ALLOC_TRACE */
#include "gsttrace.h"
Wim Taymans's avatar
Wim Taymans committed
37 38
static GstAllocTrace *_event_trace;
#endif
39

40 41
static GstMemChunk *chunk;

42 43
/* #define MEMPROF */

44 45 46 47 48
GType _gst_event_type;

void
_gst_event_initialize (void)
{
49
  /* register the type */
50
  _gst_event_type = g_boxed_type_register_static ("GstEvent",
51
      (GBoxedCopyFunc) gst_data_copy, (GBoxedFreeFunc) gst_data_unref);
52

Wim Taymans's avatar
Wim Taymans committed
53
#ifndef GST_DISABLE_TRACE
54
  _event_trace = gst_alloc_trace_register (GST_EVENT_TRACE_NAME);
Wim Taymans's avatar
Wim Taymans committed
55
#endif
56 57

  chunk = gst_mem_chunk_new ("GstEventChunk", sizeof (GstEvent),
58
      sizeof (GstEvent) * 50, 0);
59 60
}

61 62
static GstEvent *
_gst_event_copy (GstEvent * event)
Wim Taymans's avatar
Wim Taymans committed
63 64 65
{
  GstEvent *copy;

66
  copy = gst_mem_chunk_alloc (chunk);
Wim Taymans's avatar
Wim Taymans committed
67
#ifndef GST_DISABLE_TRACE
68
  gst_alloc_trace_new (_event_trace, copy);
Wim Taymans's avatar
Wim Taymans committed
69
#endif
Wim Taymans's avatar
Wim Taymans committed
70 71

  memcpy (copy, event, sizeof (GstEvent));
72

Wim Taymans's avatar
Wim Taymans committed
73
  /* FIXME copy/ref additional fields */
Benjamin Otte's avatar
Benjamin Otte committed
74 75
  switch (GST_EVENT_TYPE (event)) {
    case GST_EVENT_TAG:
76
      copy->event_data.structure.structure =
77 78
          gst_tag_list_copy ((GstTagList *) event->event_data.structure.
          structure);
79 80
      break;
    case GST_EVENT_NAVIGATION:
81
      copy->event_data.structure.structure =
82
          gst_structure_copy (event->event_data.structure.structure);
Benjamin Otte's avatar
Benjamin Otte committed
83 84 85
    default:
      break;
  }
Wim Taymans's avatar
Wim Taymans committed
86 87 88 89

  return copy;
}

90
static void
91
_gst_event_free (GstEvent * event)
92
{
93
  GST_CAT_INFO (GST_CAT_EVENT, "freeing event %p", event);
Wim Taymans's avatar
Wim Taymans committed
94

95 96 97 98
  if (GST_EVENT_SRC (event)) {
    gst_object_unref (GST_EVENT_SRC (event));
  }
  switch (GST_EVENT_TYPE (event)) {
Benjamin Otte's avatar
Benjamin Otte committed
99
    case GST_EVENT_TAG:
100
      if (GST_IS_TAG_LIST (event->event_data.structure.structure)) {
101
        gst_tag_list_free (event->event_data.structure.structure);
102
      } else {
103 104
        g_warning ("tag event %p didn't contain a valid tag list!", event);
        GST_ERROR ("tag event %p didn't contain a valid tag list!", event);
105
      }
106
      break;
107 108
    case GST_EVENT_NAVIGATION:
      gst_structure_free (event->event_data.structure.structure);
109
      break;
110 111 112
    default:
      break;
  }
113
  _GST_DATA_DISPOSE (GST_DATA (event));
Wim Taymans's avatar
Wim Taymans committed
114
#ifndef GST_DISABLE_TRACE
115
  gst_alloc_trace_free (_event_trace, event);
Wim Taymans's avatar
Wim Taymans committed
116
#endif
117
  gst_mem_chunk_free (chunk, event);
118 119
}

120 121 122 123 124 125 126 127 128 129
/**
 * gst_event_masks_contains:
 * @masks: The eventmask array to search
 * @mask: the event mask to find
 *
 * See if the given eventmask is inside the eventmask array.
 *
 * Returns: TRUE if the eventmask is found inside the array
 */
gboolean
130
gst_event_masks_contains (const GstEventMask * masks, GstEventMask * mask)
131 132 133 134 135
{
  g_return_val_if_fail (mask != NULL, FALSE);

  if (!masks)
    return FALSE;
136

137 138
  while (masks->type) {
    if (masks->type == mask->type &&
139
        (masks->flags & mask->flags) == mask->flags)
140 141 142 143 144 145 146 147
      return TRUE;

    masks++;
  }

  return FALSE;
}

148 149 150 151 152 153
GType
gst_event_get_type (void)
{
  return _gst_event_type;
}

154 155 156 157 158 159 160 161
/**
 * gst_event_new:
 * @type: The type of the new event
 *
 * Allocate a new event of the given type.
 *
 * Returns: A new event.
 */
162
GstEvent *
163 164 165 166
gst_event_new (GstEventType type)
{
  GstEvent *event;

167
  event = gst_mem_chunk_alloc0 (chunk);
Wim Taymans's avatar
Wim Taymans committed
168
#ifndef GST_DISABLE_TRACE
169
  gst_alloc_trace_new (_event_trace, event);
Wim Taymans's avatar
Wim Taymans committed
170
#endif
171

172
  GST_CAT_INFO (GST_CAT_EVENT, "creating new event %p %d", event, type);
173 174

  _GST_DATA_INIT (GST_DATA (event),
175 176 177 178
      _gst_event_type,
      0,
      (GstDataFreeFunction) _gst_event_free,
      (GstDataCopyFunction) _gst_event_copy);
179 180

  GST_EVENT_TYPE (event) = type;
Wim Taymans's avatar
Wim Taymans committed
181
  GST_EVENT_TIMESTAMP (event) = G_GINT64_CONSTANT (0);
182 183 184
  GST_EVENT_SRC (event) = NULL;

  return event;
185 186
}

187 188 189 190 191 192 193 194 195
/**
 * gst_event_new_seek:
 * @type: The type of the seek event
 * @offset: The offset of the seek
 *
 * Allocate a new seek event with the given parameters.
 *
 * Returns: A new seek event.
 */
196
GstEvent *
197
gst_event_new_seek (GstSeekType type, gint64 offset)
198 199 200 201
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SEEK);
202

203 204
  GST_EVENT_SEEK_TYPE (event) = type;
  GST_EVENT_SEEK_OFFSET (event) = offset;
205
  GST_EVENT_SEEK_ENDOFFSET (event) = -1;
206 207 208

  return event;
}
209

Wim Taymans's avatar
Wim Taymans committed
210
/**
211
 * gst_event_new_discontinuous_valist:
Wim Taymans's avatar
Wim Taymans committed
212 213
 * @new_media: A flag indicating a new media type starts
 * @format1: The format of the discont value
214
 * @var_args: more discont values and formats
Wim Taymans's avatar
Wim Taymans committed
215
 *
216 217 218 219
 * Allocate a new discontinuous event with the given format/value pairs. Note
 * that the values are of type gint64 - you may not use simple integers such
 * as "0" when calling this function, always cast them like "(gint64) 0".
 * Terminate the list with #GST_FORMAT_UNDEFINED.
Wim Taymans's avatar
Wim Taymans committed
220 221 222
 *
 * Returns: A new discontinuous event.
 */
223 224 225
GstEvent *
gst_event_new_discontinuous_valist (gboolean new_media, GstFormat format1,
    va_list var_args)
226 227 228 229 230
{
  GstEvent *event;
  gint count = 0;

  event = gst_event_new (GST_EVENT_DISCONTINUOUS);
Wim Taymans's avatar
Wim Taymans committed
231
  GST_EVENT_DISCONT_NEW_MEDIA (event) = new_media;
232

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
233
  while (format1 != GST_FORMAT_UNDEFINED && count < 8) {
234

235
    GST_EVENT_DISCONT_OFFSET (event, count).format =
236
        format1 & GST_SEEK_FORMAT_MASK;
237 238
    GST_EVENT_DISCONT_OFFSET (event, count).value = va_arg (var_args, gint64);

239
    format1 = va_arg (var_args, GstFormat);
240 241 242 243 244

    count++;
  }

  GST_EVENT_DISCONT_OFFSET_LEN (event) = count;
245

246 247 248
  return event;
}

249 250 251 252 253 254 255 256 257 258 259 260 261
/**
 * gst_event_new_discontinuous:
 * @new_media: A flag indicating a new media type starts
 * @format1: The format of the discont value
 * @...: more discont values and formats
 *
 * Allocate a new discontinuous event with the given format/value pairs. Note
 * that the values are of type gint64 - you may not use simple integers such
 * as "0" when calling this function, always cast them like "(gint64) 0".
 * Terminate the list with #GST_FORMAT_UNDEFINED.
 *
 * Returns: A new discontinuous event.
 */
262
GstEvent *
263 264 265 266 267 268 269
gst_event_new_discontinuous (gboolean new_media, GstFormat format1, ...)
{
  va_list var_args;
  GstEvent *event;

  va_start (var_args, format1);

270
  event = gst_event_new_discontinuous_valist (new_media, format1, var_args);
271 272 273 274 275 276

  va_end (var_args);

  return event;
}

Wim Taymans's avatar
Wim Taymans committed
277 278 279 280 281 282 283 284 285 286
/**
 * gst_event_discont_get_value:
 * @event: The event to query
 * @format: The format of the discont value
 * @value: A pointer to the value
 *
 * Get the value for the given format in the dicont event.
 *
 * Returns: TRUE if the discont event caries the specified format/value pair.
 */
287
gboolean
288
gst_event_discont_get_value (GstEvent * event, GstFormat format, gint64 * value)
289 290 291
{
  gint i, n;

Wim Taymans's avatar
Wim Taymans committed
292 293
  g_return_val_if_fail (event != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);
294 295 296 297

  n = GST_EVENT_DISCONT_OFFSET_LEN (event);

  for (i = 0; i < n; i++) {
298 299
    if (GST_EVENT_DISCONT_OFFSET (event, i).format == format) {
      *value = GST_EVENT_DISCONT_OFFSET (event, i).value;
300 301 302
      return TRUE;
    }
  }
303

304 305 306 307
  return FALSE;
}


308 309 310 311 312 313 314 315 316
/**
 * gst_event_new_size:
 * @format: The format of the size value
 * @value: The value of the size event
 *
 * Create a new size event with the given values.
 *
 * Returns: The new size event.
 */
317
GstEvent *
318 319 320 321 322
gst_event_new_size (GstFormat format, gint64 value)
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SIZE);
323

324 325
  GST_EVENT_SIZE_FORMAT (event) = format;
  GST_EVENT_SIZE_VALUE (event) = value;
326

327 328 329
  return event;
}

330

331 332 333 334 335 336 337 338 339 340
/**
 * gst_event_new_segment_seek:
 * @type: The type of the seek event
 * @start: The start offset of the seek
 * @stop: The stop offset of the seek
 *
 * Allocate a new segment seek event with the given parameters. 
 *
 * Returns: A new segment seek event.
 */
341
GstEvent *
342 343 344 345
gst_event_new_segment_seek (GstSeekType type, gint64 start, gint64 stop)
{
  GstEvent *event;

346 347
  g_return_val_if_fail (start < stop, NULL);

348 349 350 351 352 353 354 355
  event = gst_event_new (GST_EVENT_SEEK_SEGMENT);

  GST_EVENT_SEEK_TYPE (event) = type;
  GST_EVENT_SEEK_OFFSET (event) = start;
  GST_EVENT_SEEK_ENDOFFSET (event) = stop;

  return event;
}