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

Benjamin Otte's avatar
Benjamin Otte committed
23
#include <string.h>		/* memcpy */
24

25 26 27 28 29
#include "gstinfo.h"
#include "gstdata_private.h"
#include "gstevent.h"
#include "gstlog.h"

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

36 37
/* #define MEMPROF */

38 39 40 41 42
GType _gst_event_type;

void
_gst_event_initialize (void)
{
43
  /* register the type */
44 45 46
  _gst_event_type = g_boxed_type_register_static ("GstEvent",
                                               (GBoxedCopyFunc) gst_data_ref,
                                               (GBoxedFreeFunc) gst_data_unref);
47

Wim Taymans's avatar
Wim Taymans committed
48
#ifndef GST_DISABLE_TRACE
49
  _event_trace = gst_alloc_trace_register (GST_EVENT_TRACE_NAME);
Wim Taymans's avatar
Wim Taymans committed
50
#endif
51 52
}

53 54
static GstEvent*
_gst_event_copy (GstEvent *event)
Wim Taymans's avatar
Wim Taymans committed
55 56 57
{
  GstEvent *copy;

58
  copy = g_new0(GstEvent, 1);
Wim Taymans's avatar
Wim Taymans committed
59
#ifndef GST_DISABLE_TRACE
60
  gst_alloc_trace_new (_event_trace, copy);
Wim Taymans's avatar
Wim Taymans committed
61
#endif
Wim Taymans's avatar
Wim Taymans committed
62 63 64 65 66 67 68 69

  memcpy (copy, event, sizeof (GstEvent));
  
  /* FIXME copy/ref additional fields */

  return copy;
}

70 71
static void
_gst_event_free (GstEvent* event)
72
{
Wim Taymans's avatar
Wim Taymans committed
73 74
  GST_INFO (GST_CAT_EVENT, "freeing event %p", event);

75 76 77 78 79 80 81
  if (GST_EVENT_SRC (event)) {
    gst_object_unref (GST_EVENT_SRC (event));
  }
  switch (GST_EVENT_TYPE (event)) {
    default:
      break;
  }
82
  _GST_DATA_DISPOSE (GST_DATA (event));
Wim Taymans's avatar
Wim Taymans committed
83
#ifndef GST_DISABLE_TRACE
84
  gst_alloc_trace_free (_event_trace, event);
Wim Taymans's avatar
Wim Taymans committed
85
#endif
86

87
  g_free (event);
88 89
}

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
/**
 * 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
gst_event_masks_contains (const GstEventMask *masks, GstEventMask *mask)
{
  g_return_val_if_fail (mask != NULL, FALSE);

  if (!masks)
    return FALSE;
  
  while (masks->type) {
    if (masks->type == mask->type &&
        (masks->flags & mask->flags) == mask->flags)
      return TRUE;

    masks++;
  }

  return FALSE;
}

118 119 120 121 122 123 124 125 126 127 128 129 130 131
/**
 * gst_event_new:
 * @type: The type of the new event
 *
 * Allocate a new event of the given type.
 *
 * Returns: A new event.
 */
GstEvent*
gst_event_new (GstEventType type)
{
  GstEvent *event;

  event = g_new0(GstEvent, 1);
Wim Taymans's avatar
Wim Taymans committed
132
#ifndef GST_DISABLE_TRACE
133
  gst_alloc_trace_new (_event_trace, event);
Wim Taymans's avatar
Wim Taymans committed
134
#endif
135

Wim Taymans's avatar
Wim Taymans committed
136
  GST_INFO (GST_CAT_EVENT, "creating new event %p %d", event, type);
137 138 139 140 141 142 143 144

  _GST_DATA_INIT (GST_DATA (event),
		  _gst_event_type,
		  0,
		  (GstDataFreeFunction) _gst_event_free,
		  (GstDataCopyFunction) _gst_event_copy);

  GST_EVENT_TYPE (event) = type;
Wim Taymans's avatar
Wim Taymans committed
145
  GST_EVENT_TIMESTAMP (event) = G_GINT64_CONSTANT (0);
146 147 148
  GST_EVENT_SRC (event) = NULL;

  return event;
149 150
}

151 152 153 154 155 156 157 158 159
/**
 * 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.
 */
160
GstEvent*       
161
gst_event_new_seek (GstSeekType type, gint64 offset)
162 163 164 165
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SEEK);
166

167 168
  GST_EVENT_SEEK_TYPE (event) = type;
  GST_EVENT_SEEK_OFFSET (event) = offset;
169
  GST_EVENT_SEEK_ENDOFFSET (event) = -1;
170 171 172

  return event;
}
173

Wim Taymans's avatar
Wim Taymans committed
174 175 176 177 178 179 180 181 182 183
/**
 * 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 geven format/value pairs.
 *
 * Returns: A new discontinuous event.
 */
184
GstEvent*
185
gst_event_new_discontinuous (gboolean new_media, GstFormat format1, ...)
186 187 188 189 190 191
{
  va_list var_args;
  GstEvent *event;
  gint count = 0;

  event = gst_event_new (GST_EVENT_DISCONTINUOUS);
Wim Taymans's avatar
Wim Taymans committed
192
  GST_EVENT_DISCONT_NEW_MEDIA (event) = new_media;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

  va_start (var_args, format1);
	        
  while (format1) {

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

    format1 = va_arg (var_args, GstSeekType);

    count++;
  }
  va_end (var_args);

  GST_EVENT_DISCONT_OFFSET_LEN (event) = count;
		    
  return event;
}

Wim Taymans's avatar
Wim Taymans committed
212 213 214 215 216 217 218 219 220 221
/**
 * 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.
 */
222
gboolean
Wim Taymans's avatar
Wim Taymans committed
223
gst_event_discont_get_value (GstEvent *event, GstFormat format, gint64 *value)
224 225 226
{
  gint i, n;

Wim Taymans's avatar
Wim Taymans committed
227 228
  g_return_val_if_fail (event != NULL, FALSE);
  g_return_val_if_fail (value != NULL, FALSE);
229 230 231 232

  n = GST_EVENT_DISCONT_OFFSET_LEN (event);

  for (i = 0; i < n; i++) {
Wim Taymans's avatar
Wim Taymans committed
233
    if (GST_EVENT_DISCONT_OFFSET(event,i).format == format) {
234 235 236 237 238 239 240 241 242
      *value = GST_EVENT_DISCONT_OFFSET(event,i).value;
      return TRUE;
    }
  }
  
  return FALSE;
}


243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
/**
 * gst_event_has_new_media:
 * @event: The event to check
 *
 * Checks if a given event indicates a new media.
 *
 * Returns: True if the event indicates a new media
 */
gboolean
gst_event_has_new_media (GstEvent *event)
{
  g_return_val_if_fail (GST_IS_EVENT (event), FALSE);
  g_return_val_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_DISCONTINUOUS ||
                        GST_EVENT_TYPE (event) == GST_EVENT_NEW_MEDIA, FALSE);
  if (GST_EVENT_TYPE (event) == GST_EVENT_NEW_MEDIA)
    return TRUE;

  return GST_EVENT_DISCONT_NEW_MEDIA (event);  
}
262 263 264 265 266 267 268 269 270
/**
 * 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.
 */
271 272 273 274 275 276
GstEvent*
gst_event_new_size (GstFormat format, gint64 value)
{
  GstEvent *event;

  event = gst_event_new (GST_EVENT_SIZE);
277

278 279 280 281 282 283
  GST_EVENT_SIZE_FORMAT (event) = format;
  GST_EVENT_SIZE_VALUE (event) = value;
  
  return event;
}

284

285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/**
 * 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.
 */
GstEvent*       
gst_event_new_segment_seek (GstSeekType type, gint64 start, gint64 stop)
{
  GstEvent *event;

300 301
  g_return_val_if_fail (start < stop, NULL);

302 303 304 305 306 307 308 309
  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;
}
310