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 14.4 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 26 27
#include "gst_private.h"

#include "gstinfo.h"
28
#include "gstevent.h"
Wim Taymans's avatar
Wim Taymans committed
29
#include "gstenumtypes.h"
30
#include "gstutils.h"
Benjamin Otte's avatar
Benjamin Otte committed
31

32 33 34 35
static void gst_event_init (GTypeInstance * instance, gpointer g_class);
static void gst_event_class_init (gpointer g_class, gpointer class_data);
static void gst_event_finalize (GstEvent * event);
static GstEvent *_gst_event_copy (GstEvent * event);
36 37 38 39

void
_gst_event_initialize (void)
{
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  gst_event_get_type ();
}

GType
gst_event_get_type (void)
{
  static GType _gst_event_type;

  if (G_UNLIKELY (_gst_event_type == 0)) {
    static const GTypeInfo event_info = {
      sizeof (GstEventClass),
      NULL,
      NULL,
      gst_event_class_init,
      NULL,
      NULL,
      sizeof (GstEvent),
      0,
      gst_event_init,
      NULL
    };

    _gst_event_type = g_type_register_static (GST_TYPE_MINI_OBJECT,
        "GstEvent", &event_info, 0);
  }
65

66
  return _gst_event_type;
67 68
}

69 70
static void
gst_event_class_init (gpointer g_class, gpointer class_data)
Wim Taymans's avatar
Wim Taymans committed
71
{
72
  GstEventClass *event_class = GST_EVENT_CLASS (g_class);
Wim Taymans's avatar
Wim Taymans committed
73

74 75 76 77 78
  event_class->mini_object_class.copy =
      (GstMiniObjectCopyFunction) _gst_event_copy;
  event_class->mini_object_class.finalize =
      (GstMiniObjectFinalizeFunction) gst_event_finalize;
}
Wim Taymans's avatar
Wim Taymans committed
79

80 81 82 83
static void
gst_event_init (GTypeInstance * instance, gpointer g_class)
{
  GstEvent *event;
84

85
  event = GST_EVENT (instance);
Wim Taymans's avatar
Wim Taymans committed
86

87
  GST_EVENT_TIMESTAMP (event) = GST_CLOCK_TIME_NONE;
Wim Taymans's avatar
Wim Taymans committed
88 89
}

90
static void
91
gst_event_finalize (GstEvent * event)
92
{
93 94 95
  g_return_if_fail (event != NULL);
  g_return_if_fail (GST_IS_EVENT (event));

96
  GST_CAT_INFO (GST_CAT_EVENT, "freeing event %p", event);
Wim Taymans's avatar
Wim Taymans committed
97

98 99
  if (GST_EVENT_SRC (event)) {
    gst_object_unref (GST_EVENT_SRC (event));
100
    GST_EVENT_SRC (event) = NULL;
101
  }
Wim Taymans's avatar
Wim Taymans committed
102 103 104
  if (event->structure) {
    gst_structure_set_parent_refcount (event->structure, NULL);
    gst_structure_free (event->structure);
105
  }
106 107 108 109 110 111 112
}

static GstEvent *
_gst_event_copy (GstEvent * event)
{
  GstEvent *copy;

Wim Taymans's avatar
Wim Taymans committed
113
  copy = (GstEvent *) gst_mini_object_new (GST_TYPE_EVENT);
114

Wim Taymans's avatar
Wim Taymans committed
115 116
  GST_EVENT_TYPE (copy) = GST_EVENT_TYPE (event);
  GST_EVENT_TIMESTAMP (copy) = GST_EVENT_TIMESTAMP (event);
117

Wim Taymans's avatar
Wim Taymans committed
118 119 120 121 122 123 124
  if (GST_EVENT_SRC (event)) {
    GST_EVENT_SRC (copy) = gst_object_ref (GST_EVENT_SRC (event));
  }
  if (event->structure) {
    copy->structure = gst_structure_copy (event->structure);
    gst_structure_set_parent_refcount (copy->structure,
        &event->mini_object.refcount);
125 126
  }
  return copy;
127 128
}

Wim Taymans's avatar
Wim Taymans committed
129 130
static GstEvent *
gst_event_new (GstEventType type)
131
{
Wim Taymans's avatar
Wim Taymans committed
132
  GstEvent *event;
133

Wim Taymans's avatar
Wim Taymans committed
134
  event = (GstEvent *) gst_mini_object_new (GST_TYPE_EVENT);
135

Wim Taymans's avatar
Wim Taymans committed
136
  GST_CAT_INFO (GST_CAT_EVENT, "creating new event %p %d", event, type);
137

Wim Taymans's avatar
Wim Taymans committed
138 139 140
  event->type = type;
  event->src = NULL;
  event->structure = NULL;
141

Wim Taymans's avatar
Wim Taymans committed
142
  return event;
143 144
}

145
/**
Wim Taymans's avatar
Wim Taymans committed
146
 * gst_event_new_custom:
147
 * @type: The type of the new event
Wim Taymans's avatar
Wim Taymans committed
148 149
 * @structure: The structure for the event. The event will take ownership of
 * the structure.
150
 *
Wim Taymans's avatar
Wim Taymans committed
151 152 153
 * Create a new custom-typed event. This can be used for anything not
 * handled by other event-specific functions to pass an event to another
 * element.
154
 *
Wim Taymans's avatar
Wim Taymans committed
155 156 157 158 159 160 161 162
 * Make sure to allocate an event type with the #GST_EVENT_MAKE_TYPE macro,
 * assigning a free number and filling in the correct direction and
 * serialization flags.
 *
 * New custom events can also be created by subclassing the event type if
 * needed.
 *
 * Returns: The new custom event.
163
 */
164
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
165
gst_event_new_custom (GstEventType type, GstStructure * structure)
166 167 168
{
  GstEvent *event;

Wim Taymans's avatar
Wim Taymans committed
169 170 171 172 173
  event = gst_event_new (type);
  if (structure) {
    gst_structure_set_parent_refcount (structure, &event->mini_object.refcount);
    event->structure = structure;
  }
174
  return event;
175 176
}

177
/**
Wim Taymans's avatar
Wim Taymans committed
178 179
 * gst_event_get_structure:
 * @event: The #GstEvent.
180
 *
Wim Taymans's avatar
Wim Taymans committed
181
 * Access the structure of the event.
182
 *
Wim Taymans's avatar
Wim Taymans committed
183 184 185 186 187
 * Returns: The structure of the event. The structure is still
 * owned by the event, which means that you should not free it and
 * that the pointer becomes invalid when you free the event.
 *
 * MT safe.
188
 */
Wim Taymans's avatar
Wim Taymans committed
189 190
const GstStructure *
gst_event_get_structure (GstEvent * event)
191
{
Wim Taymans's avatar
Wim Taymans committed
192
  g_return_val_if_fail (GST_IS_EVENT (event), NULL);
193

Wim Taymans's avatar
Wim Taymans committed
194
  return event->structure;
195
}
196

Wim Taymans's avatar
Wim Taymans committed
197
/**
Wim Taymans's avatar
Wim Taymans committed
198 199 200 201 202 203 204 205
 * gst_event_new_flush_start:
 *
 * Allocate a new flush start event. The flush start event can be send
 * upstream and downstream and travels out-of-bounds with the dataflow.
 * It marks pads as being in a WRONG_STATE to process more data.
 *
 * Elements unlock and blocking functions and exit their streaming functions
 * as fast as possible. 
Wim Taymans's avatar
Wim Taymans committed
206
 *
Wim Taymans's avatar
Wim Taymans committed
207 208
 * This event is typically generated after a seek to minimize the latency
 * after the seek.
Wim Taymans's avatar
Wim Taymans committed
209
 *
Wim Taymans's avatar
Wim Taymans committed
210
 * Returns: A new flush start event.
Wim Taymans's avatar
Wim Taymans committed
211
 */
212
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
213
gst_event_new_flush_start (void)
214
{
Wim Taymans's avatar
Wim Taymans committed
215
  return gst_event_new (GST_EVENT_FLUSH_START);
216 217
}

218
/**
Wim Taymans's avatar
Wim Taymans committed
219 220 221 222 223 224
 * gst_event_new_flush_stop:
 *
 * Allocate a new flush stop event. The flush start event can be send
 * upstream and downstream and travels out-of-bounds with the dataflow.
 * It is typically send after sending a FLUSH_START event to make the
 * pads accept data again.
225
 *
Wim Taymans's avatar
Wim Taymans committed
226 227
 * Elements can process this event synchronized with the dataflow since
 * the preceeding FLUSH_START event stopped the dataflow.
228
 *
Wim Taymans's avatar
Wim Taymans committed
229 230 231 232
 * This event is typically generated to complete a seek and to resume
 * dataflow.
 *
 * Returns: A new flush stop event.
233
 */
234
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
235
gst_event_new_flush_stop (void)
236
{
Wim Taymans's avatar
Wim Taymans committed
237
  return gst_event_new (GST_EVENT_FLUSH_STOP);
238 239
}

Wim Taymans's avatar
Wim Taymans committed
240
/**
Wim Taymans's avatar
Wim Taymans committed
241
 * gst_event_new_eos:
Wim Taymans's avatar
Wim Taymans committed
242
 *
Wim Taymans's avatar
Wim Taymans committed
243 244 245 246
 * Create a new EOS event. The eos event can only travel downstream
 * synchronized with the buffer flow. Elements that receive the EOS
 * event on a pad can return UNEXPECTED as a GstFlowReturn when data
 * after the EOS event arrives.
Wim Taymans's avatar
Wim Taymans committed
247
 *
Wim Taymans's avatar
Wim Taymans committed
248 249 250 251 252 253 254
 * The EOS event will travel up to the sink elements in the pipeline
 * which will then post the GST_MESSAGE_EOS on the bus.
 *
 * When all sinks have posted an EOS message, the EOS message is
 * forwarded to the application.
 *
 * Returns: The new EOS event.
Wim Taymans's avatar
Wim Taymans committed
255
 */
Wim Taymans's avatar
Wim Taymans committed
256 257
GstEvent *
gst_event_new_eos (void)
258
{
Wim Taymans's avatar
Wim Taymans committed
259
  return gst_event_new (GST_EVENT_EOS);
260 261
}

262
/**
Wim Taymans's avatar
Wim Taymans committed
263 264 265 266 267 268 269 270 271 272 273
 * gst_event_new_newsegment:
 * @rate: a new rate for playback
 * @format: The format of the segment values
 * @start_val: the start value of the segment
 * @stop_val: the stop value of the segment
 * @base: base value for buffer timestamps.
 *
 * Allocate a new newsegment event with the given format/values tripplets.
 *
 * The newsegment event marks the range of buffers to be processed. All
 * data not within the segment range is not to be processed.
274
 *
Wim Taymans's avatar
Wim Taymans committed
275 276
 * The base time of the segment is used to convert the buffer timestamps
 * into the stream time again.
277
 *
Wim Taymans's avatar
Wim Taymans committed
278 279 280 281 282
 * After a newsegment event, the buffer stream time is calculated with:
 *
 *   TIMESTAMP(buf) - start_time + base
 *
 * Returns: A new newsegment event.
283
 */
284
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
285 286
gst_event_new_newsegment (gdouble rate, GstFormat format,
    gint64 start_val, gint64 stop_val, gint64 base)
287
{
Wim Taymans's avatar
Wim Taymans committed
288 289 290 291 292 293 294
  return gst_event_new_custom (GST_EVENT_NEWSEGMENT,
      gst_structure_new ("GstEventNewsegment", "rate", G_TYPE_DOUBLE, rate,
          "format", GST_TYPE_FORMAT, format,
          "start_val", G_TYPE_INT64, start_val,
          "stop_val", G_TYPE_INT64, stop_val,
          "base", G_TYPE_INT64, base, NULL));
}
295

Wim Taymans's avatar
Wim Taymans committed
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
/**
 * gst_event_parse_newsegment:
 * @event: The event to query
 * @rate: A pointer to the rate of the segment
 * @format: A pointer to the format of the newsegment values
 * @start_value: A pointer to store the start value in
 * @stop_value: A pointer to store the stop value in
 * @base: A pointer to store the base time in
 *
 * Get the start, stop and format in the newsegment event.
 */
void
gst_event_parse_newsegment (GstEvent * event, gdouble * rate,
    GstFormat * format, gint64 * start_value, gint64 * stop_value,
    gint64 * base)
{
  const GstStructure *structure;
313

Wim Taymans's avatar
Wim Taymans committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
  g_return_if_fail (GST_IS_EVENT (event));
  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_NEWSEGMENT);

  structure = gst_event_get_structure (event);
  if (rate)
    *rate = g_value_get_double (gst_structure_get_value (structure, "rate"));
  if (format)
    *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
  if (start_value)
    *start_value =
        g_value_get_int64 (gst_structure_get_value (structure, "start_val"));
  if (stop_value)
    *stop_value =
        g_value_get_int64 (gst_structure_get_value (structure, "stop_val"));
  if (base)
    *base = g_value_get_int64 (gst_structure_get_value (structure, "base"));
}
331

Wim Taymans's avatar
Wim Taymans committed
332 333 334 335 336 337 338
/* tag event */
GstEvent *
gst_event_new_tag (GstTagList * taglist)
{
  g_return_val_if_fail (taglist != NULL, NULL);

  return gst_event_new_custom (GST_EVENT_TAG, (GstStructure *) taglist);
339 340
}

Wim Taymans's avatar
Wim Taymans committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
void
gst_event_parse_tag (GstEvent * event, GstTagList ** taglist)
{
  g_return_if_fail (GST_IS_EVENT (event));
  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_TAG);

  if (taglist)
    *taglist = (GstTagList *) event->structure;
}

/* filler event */
GstEvent *
gst_event_new_filler (void)
{
  return gst_event_new (GST_EVENT_FILLER);
}
357

358
/**
Wim Taymans's avatar
Wim Taymans committed
359 360 361 362 363 364 365 366 367 368 369 370 371
 * gst_event_new_qos:
 * @proportion: the proportion of the qos message
 * @diff: The time difference of the last Clock sync
 * @timestamp: The timestamp of the buffer
 *
 * Allocate a new qos event with the given values.
 * The QOS event is generated in an element that wants an upstream
 * element to either reduce or increase its rate because of
 * high/low CPU load.
 *
 * proportion is the requested adjustment in datarate, 1.0 is the normal
 * datarate, 0.75 means increase datarate by 75%, 1.5 is 150%. Negative
 * values request a slow down, so -0.75 means a decrease by 75%.
372
 *
Wim Taymans's avatar
Wim Taymans committed
373 374
 * diff is the difference against the clock in stream time of the last 
 * buffer that caused the element to generate the QOS event.
375
 *
Wim Taymans's avatar
Wim Taymans committed
376 377 378 379
 * timestamp is the timestamp of the last buffer that cause the element
 * to generate the QOS event.
 *
 * Returns: A new QOS event.
380
 */
381
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
382 383
gst_event_new_qos (gdouble proportion, GstClockTimeDiff diff,
    GstClockTime timestamp)
384
{
Wim Taymans's avatar
Wim Taymans committed
385 386 387 388 389 390
  return gst_event_new_custom (GST_EVENT_QOS,
      gst_structure_new ("GstEventQOS",
          "proportion", G_TYPE_DOUBLE, proportion,
          "diff", G_TYPE_INT64, diff,
          "timestamp", G_TYPE_UINT64, timestamp, NULL));
}
391

Wim Taymans's avatar
Wim Taymans committed
392 393 394 395 396 397 398 399 400 401 402 403 404 405
/**
 * gst_event_parse_qos:
 * @event: The event to query
 * @proportion: A pointer to store the proportion in
 * @diff: A pointer to store the diff in
 * @timestamp: A pointer to store the timestamp in
 *
 * Get the proportion, diff and timestamp in the qos event.
 */
void
gst_event_parse_qos (GstEvent * event, gdouble * proportion,
    GstClockTimeDiff * diff, GstClockTime * timestamp)
{
  const GstStructure *structure;
406

Wim Taymans's avatar
Wim Taymans committed
407 408 409 410 411 412 413 414 415 416 417 418
  g_return_if_fail (GST_IS_EVENT (event));
  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_QOS);

  structure = gst_event_get_structure (event);
  if (proportion)
    *proportion =
        g_value_get_double (gst_structure_get_value (structure, "proportion"));
  if (diff)
    *diff = g_value_get_int64 (gst_structure_get_value (structure, "diff"));
  if (timestamp)
    *timestamp =
        g_value_get_uint64 (gst_structure_get_value (structure, "timestamp"));
419
}
420 421

/**
Wim Taymans's avatar
Wim Taymans committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435
 * gst_event_new_seek:
 * @rate: The new playback rate
 * @format: The format of the seek values
 * @flags: The optional seek flags.
 * @cur_type: The type and flags for the new current position
 * @cur: The value of the new current position
 * @stop_type: The type and flags for the new stop position
 * @stop: The value of the new stop position
 *
 * Allocate a new seek event with the given parameters.
 *
 * The seek event configures playback of the pipeline from 
 * @cur to @stop at the speed given in @rate.
 * The @cur and @stop values are expressed in format @format.
436
 *
Wim Taymans's avatar
Wim Taymans committed
437 438 439
 * A @rate of 1.0 means normal playback rate, 2.0 means double speed.
 * Negatives values means backwards playback. A value of 0.0 for the
 * rate is not allowed.
440
 *
Wim Taymans's avatar
Wim Taymans committed
441 442 443 444 445
 * @cur_type and @stop_type specify how to adjust the current and stop
 * time, relative or absolute. A type of #GST_EVENT_TYPE_NONE means that
 * the position should not be updated.
 *
 * Returns: A new seek event.
446 447
 */
GstEvent *
Wim Taymans's avatar
Wim Taymans committed
448 449
gst_event_new_seek (gdouble rate, GstFormat format, GstSeekFlags flags,
    GstSeekType cur_type, gint64 cur, GstSeekType stop_type, gint64 stop)
450
{
Wim Taymans's avatar
Wim Taymans committed
451 452 453 454 455 456 457 458 459
  return gst_event_new_custom (GST_EVENT_SEEK,
      gst_structure_new ("GstEventSeek", "rate", G_TYPE_DOUBLE, rate,
          "format", GST_TYPE_FORMAT, format,
          "flags", GST_TYPE_SEEK_FLAGS, flags,
          "cur_type", GST_TYPE_SEEK_TYPE, cur_type,
          "cur", G_TYPE_INT64, cur,
          "stop_type", GST_TYPE_SEEK_TYPE, stop_type,
          "stop", G_TYPE_INT64, stop, NULL));
}
460

Wim Taymans's avatar
Wim Taymans committed
461 462 463 464 465 466 467
void
gst_event_parse_seek (GstEvent * event, gdouble * rate, GstFormat * format,
    GstSeekFlags * flags,
    GstSeekType * cur_type, gint64 * cur,
    GstSeekType * stop_type, gint64 * stop)
{
  const GstStructure *structure;
468

Wim Taymans's avatar
Wim Taymans committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
  g_return_if_fail (GST_IS_EVENT (event));
  g_return_if_fail (GST_EVENT_TYPE (event) == GST_EVENT_SEEK);

  structure = gst_event_get_structure (event);
  if (rate)
    *rate = g_value_get_double (gst_structure_get_value (structure, "rate"));
  if (format)
    *format = g_value_get_enum (gst_structure_get_value (structure, "format"));
  if (flags)
    *flags = g_value_get_flags (gst_structure_get_value (structure, "flags"));
  if (cur_type)
    *cur_type =
        g_value_get_enum (gst_structure_get_value (structure, "cur_type"));
  if (cur)
    *cur = g_value_get_int64 (gst_structure_get_value (structure, "cur"));
  if (stop_type)
    *stop_type =
        g_value_get_enum (gst_structure_get_value (structure, "stop_type"));
  if (stop)
    *stop = g_value_get_int64 (gst_structure_get_value (structure, "stop"));
}

/* navigation event */
GstEvent *
gst_event_new_navigation (GstStructure * structure)
{
  g_return_val_if_fail (structure != NULL, NULL);

  return gst_event_new_custom (GST_EVENT_NAVIGATION, structure);
498
}