We are currently experiencing downtime impacting viewing & cloning the Mesa repo, and some GitLab pages returning 503. Please see #freedesktop on IRC for more updates.

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

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

39 40
static GstMemChunk *chunk;

41 42
/* #define MEMPROF */

43 44 45 46 47
GType _gst_event_type;

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

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

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

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

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

  memcpy (copy, event, sizeof (GstEvent));
71 72 73
  if (GST_EVENT_SRC (copy)) {
    gst_object_ref (GST_EVENT_SRC (copy));
  }
74

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

  return copy;
}

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

97 98 99 100
  if (GST_EVENT_SRC (event)) {
    gst_object_unref (GST_EVENT_SRC (event));
  }
  switch (GST_EVENT_TYPE (event)) {
Benjamin Otte's avatar
Benjamin Otte committed
101
    case GST_EVENT_TAG:
102
      if (GST_IS_TAG_LIST (event->event_data.structure.structure)) {
103
        gst_tag_list_free (event->event_data.structure.structure);
104
      } else {
105 106
        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);
107
      }
108
      break;
109 110
    case GST_EVENT_NAVIGATION:
      gst_structure_free (event->event_data.structure.structure);
111
      break;
112 113 114
    default:
      break;
  }
115
  _GST_DATA_DISPOSE (GST_DATA (event));
Wim Taymans's avatar
Wim Taymans committed
116
#ifndef GST_DISABLE_TRACE
117
  gst_alloc_trace_free (_event_trace, event);
Wim Taymans's avatar
Wim Taymans committed
118
#endif
119
  gst_mem_chunk_free (chunk, event);
120 121
}

122 123 124 125 126 127 128 129 130 131
/**
 * 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
132
gst_event_masks_contains (const GstEventMask * masks, GstEventMask * mask)
133 134 135 136 137
{
  g_return_val_if_fail (mask != NULL, FALSE);

  if (!masks)
    return FALSE;
138

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

    masks++;
  }

  return FALSE;
}

150 151 152 153 154 155
GType
gst_event_get_type (void)
{
  return _gst_event_type;
}

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

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

Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
174
  GST_CAT_INFO (GST_CAT_EVENT, "creating new event type %d: %p", type, event);
175 176

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

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

  return event;
187 188
}

189 190 191 192 193 194 195 196 197
/**
 * 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.
 */
198
GstEvent *
199
gst_event_new_seek (GstSeekType type, gint64 offset)
200 201 202 203
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SEEK);
204

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

  return event;
}
211

Wim Taymans's avatar
Wim Taymans committed
212
/**
213
 * gst_event_new_discontinuous_valist:
Wim Taymans's avatar
Wim Taymans committed
214 215
 * @new_media: A flag indicating a new media type starts
 * @format1: The format of the discont value
216
 * @var_args: more discont values and formats
Wim Taymans's avatar
Wim Taymans committed
217
 *
218 219 220 221
 * 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
222 223 224
 *
 * Returns: A new discontinuous event.
 */
225
GstEvent *
226
gst_event_new_discontinuous_valist (gdouble rate, GstFormat format1,
227
    va_list var_args)
228 229 230 231 232
{
  GstEvent *event;
  gint count = 0;

  event = gst_event_new (GST_EVENT_DISCONTINUOUS);
233
  GST_EVENT_DISCONT_RATE (event) = rate;
234

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

237
    GST_EVENT_DISCONT_OFFSET (event, count).format =
238
        format1 & GST_SEEK_FORMAT_MASK;
239 240 241 242
    GST_EVENT_DISCONT_OFFSET (event, count).start_value =
        va_arg (var_args, gint64);
    GST_EVENT_DISCONT_OFFSET (event, count).end_value =
        va_arg (var_args, gint64);
243

244
    format1 = va_arg (var_args, GstFormat);
245 246 247 248 249

    count++;
  }

  GST_EVENT_DISCONT_OFFSET_LEN (event) = count;
250

251 252 253
  return event;
}

254 255 256 257 258 259 260 261 262 263 264 265 266
/**
 * 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.
 */
267
GstEvent *
268
gst_event_new_discontinuous (gdouble rate, GstFormat format1, ...)
269 270 271 272 273 274
{
  va_list var_args;
  GstEvent *event;

  va_start (var_args, format1);

275
  event = gst_event_new_discontinuous_valist (rate, format1, var_args);
276 277 278 279 280 281

  va_end (var_args);

  return event;
}

Wim Taymans's avatar
Wim Taymans committed
282 283 284
/**
 * gst_event_discont_get_value:
 * @event: The event to query
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
285
 * @format: The format of the discontinuous value
Wim Taymans's avatar
Wim Taymans committed
286 287
 * @value: A pointer to the value
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
288
 * Get the value for the given format in the discontinous event.
Wim Taymans's avatar
Wim Taymans committed
289
 *
Thomas Vander Stichele's avatar
Thomas Vander Stichele committed
290 291
 * Returns: TRUE if the discontinuous event carries the specified
 * format/value pair.
Wim Taymans's avatar
Wim Taymans committed
292
 */
293
gboolean
294 295
gst_event_discont_get_value (GstEvent * event, GstFormat format,
    gint64 * start_value, gint64 * end_value)
296 297 298
{
  gint i, n;

Wim Taymans's avatar
Wim Taymans committed
299
  g_return_val_if_fail (event != NULL, FALSE);
300 301
  g_return_val_if_fail (start_value != NULL, FALSE);
  g_return_val_if_fail (end_value != NULL, FALSE);
302 303 304 305

  n = GST_EVENT_DISCONT_OFFSET_LEN (event);

  for (i = 0; i < n; i++) {
306
    if (GST_EVENT_DISCONT_OFFSET (event, i).format == format) {
307 308
      *start_value = GST_EVENT_DISCONT_OFFSET (event, i).start_value;
      *end_value = GST_EVENT_DISCONT_OFFSET (event, i).end_value;
309 310 311
      return TRUE;
    }
  }
312

313 314 315 316
  return FALSE;
}


317 318 319 320 321 322 323 324 325
/**
 * 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.
 */
326
GstEvent *
327 328 329 330 331
gst_event_new_size (GstFormat format, gint64 value)
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SIZE);
332

333 334
  GST_EVENT_SIZE_FORMAT (event) = format;
  GST_EVENT_SIZE_VALUE (event) = value;
335

336 337 338
  return event;
}

339

340 341 342 343 344 345 346 347 348 349
/**
 * 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.
 */
350
GstEvent *
351 352 353 354
gst_event_new_segment_seek (GstSeekType type, gint64 start, gint64 stop)
{
  GstEvent *event;

Wim Taymans's avatar
Wim Taymans committed
355
  g_return_val_if_fail (start < stop || stop == -1, NULL);
356

Wim Taymans's avatar
Wim Taymans committed
357
  event = gst_event_new (GST_EVENT_SEEK);
358 359 360 361 362 363 364

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

  return event;
}
365 366

/**
Wim Taymans's avatar
Wim Taymans committed
367 368
 * gst_event_new_flush:
 * @done: Indicates the end of the flush
369
 *
Wim Taymans's avatar
Wim Taymans committed
370
 * Allocate a new flush event.
371
 *
Wim Taymans's avatar
Wim Taymans committed
372
 * Returns: A new flush event.
373 374
 */
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
375
gst_event_new_flush (gboolean done)
376
{
Wim Taymans's avatar
Wim Taymans committed
377
  GstEvent *event;
378

Wim Taymans's avatar
Wim Taymans committed
379 380
  event = gst_event_new (GST_EVENT_FLUSH);
  GST_EVENT_FLUSH_DONE (event) = done;
381

Wim Taymans's avatar
Wim Taymans committed
382
  return event;
383
}