Blame gst-libs/gst/video/video-event.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
Packit 971217
 * Library       <2002> Ronald Bultje <rbultje@ronald.bitfreak.net>
Packit 971217
 * Copyright (C) 2007 David A. Schleef <ds@schleef.org>
Packit 971217
 *
Packit 971217
 * This library is free software; you can redistribute it and/or
Packit 971217
 * modify it under the terms of the GNU Library General Public
Packit 971217
 * License as published by the Free Software Foundation; either
Packit 971217
 * version 2 of the License, or (at your option) any later version.
Packit 971217
 *
Packit 971217
 * This library is distributed in the hope that it will be useful,
Packit 971217
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 971217
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 971217
 * Library General Public License for more details.
Packit 971217
 *
Packit 971217
 * You should have received a copy of the GNU Library General Public
Packit 971217
 * License along with this library; if not, write to the
Packit 971217
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
Packit 971217
 * Boston, MA 02110-1301, USA.
Packit 971217
 */
Packit 971217
Packit 971217
#include "video-event.h"
Packit 971217
Packit 971217
#define GST_VIDEO_EVENT_STILL_STATE_NAME "GstEventStillFrame"
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_new_still_frame:
Packit 971217
 * @in_still: boolean value for the still-frame state of the event.
Packit 971217
 *
Packit 971217
 * Creates a new Still Frame event. If @in_still is %TRUE, then the event
Packit 971217
 * represents the start of a still frame sequence. If it is %FALSE, then
Packit 971217
 * the event ends a still frame sequence.
Packit 971217
 *
Packit 971217
 * To parse an event created by gst_video_event_new_still_frame() use
Packit 971217
 * gst_video_event_parse_still_frame().
Packit 971217
 *
Packit 971217
 * Returns: The new GstEvent
Packit 971217
 */
Packit 971217
GstEvent *
Packit 971217
gst_video_event_new_still_frame (gboolean in_still)
Packit 971217
{
Packit 971217
  GstEvent *still_event;
Packit 971217
  GstStructure *s;
Packit 971217
Packit 971217
  s = gst_structure_new (GST_VIDEO_EVENT_STILL_STATE_NAME,
Packit 971217
      "still-state", G_TYPE_BOOLEAN, in_still, NULL);
Packit 971217
  still_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
Packit 971217
Packit 971217
  return still_event;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_parse_still_frame:
Packit 971217
 * @event: A #GstEvent to parse
Packit 971217
 * @in_still: (out):
Packit 971217
 *     A boolean to receive the still-frame status from the event, or NULL
Packit 971217
 *
Packit 971217
 * Parse a #GstEvent, identify if it is a Still Frame event, and
Packit 971217
 * return the still-frame state from the event if it is.
Packit 971217
 * If the event represents the start of a still frame, the in_still
Packit 971217
 * variable will be set to TRUE, otherwise FALSE. It is OK to pass NULL for the
Packit 971217
 * in_still variable order to just check whether the event is a valid still-frame
Packit 971217
 * event.
Packit 971217
 *
Packit 971217
 * Create a still frame event using gst_video_event_new_still_frame()
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if the event is a valid still-frame event. %FALSE if not
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_event_parse_still_frame (GstEvent * event, gboolean * in_still)
Packit 971217
{
Packit 971217
  const GstStructure *s;
Packit 971217
  gboolean ev_still_state;
Packit 971217
Packit 971217
  g_return_val_if_fail (event != NULL, FALSE);
Packit 971217
Packit 971217
  if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
Packit 971217
    return FALSE;               /* Not a still frame event */
Packit 971217
Packit 971217
  s = gst_event_get_structure (event);
Packit 971217
  if (s == NULL
Packit 971217
      || !gst_structure_has_name (s, GST_VIDEO_EVENT_STILL_STATE_NAME))
Packit 971217
    return FALSE;               /* Not a still frame event */
Packit 971217
  if (!gst_structure_get_boolean (s, "still-state", &ev_still_state))
Packit 971217
    return FALSE;               /* Not a still frame event */
Packit 971217
  if (in_still)
Packit 971217
    *in_still = ev_still_state;
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
#define GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME "GstForceKeyUnit"
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_new_downstream_force_key_unit:
Packit 971217
 * @timestamp: the timestamp of the buffer that starts a new key unit
Packit 971217
 * @stream_time: the stream_time of the buffer that starts a new key unit
Packit 971217
 * @running_time: the running_time of the buffer that starts a new key unit
Packit 971217
 * @all_headers: %TRUE to produce headers when starting a new key unit
Packit 971217
 * @count: integer that can be used to number key units
Packit 971217
 *
Packit 971217
 * Creates a new downstream force key unit event. A downstream force key unit
Packit 971217
 * event can be sent down the pipeline to request downstream elements to produce
Packit 971217
 * a key unit. A downstream force key unit event must also be sent when handling
Packit 971217
 * an upstream force key unit event to notify downstream that the latter has been
Packit 971217
 * handled.
Packit 971217
 *
Packit 971217
 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
Packit 971217
 * gst_video_event_parse_downstream_force_key_unit().
Packit 971217
 *
Packit 971217
 * Returns: The new GstEvent
Packit 971217
 */
Packit 971217
GstEvent *
Packit 971217
gst_video_event_new_downstream_force_key_unit (GstClockTime timestamp,
Packit 971217
    GstClockTime stream_time, GstClockTime running_time, gboolean all_headers,
Packit 971217
    guint count)
Packit 971217
{
Packit 971217
  GstEvent *force_key_unit_event;
Packit 971217
  GstStructure *s;
Packit 971217
Packit 971217
  s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
Packit 971217
      "timestamp", G_TYPE_UINT64, timestamp,
Packit 971217
      "stream-time", G_TYPE_UINT64, stream_time,
Packit 971217
      "running-time", G_TYPE_UINT64, running_time,
Packit 971217
      "all-headers", G_TYPE_BOOLEAN, all_headers,
Packit 971217
      "count", G_TYPE_UINT, count, NULL);
Packit 971217
  force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
Packit 971217
Packit 971217
  return force_key_unit_event;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_new_upstream_force_key_unit:
Packit 971217
 * @running_time: the running_time at which a new key unit should be produced
Packit 971217
 * @all_headers: %TRUE to produce headers when starting a new key unit
Packit 971217
 * @count: integer that can be used to number key units
Packit 971217
 *
Packit 971217
 * Creates a new upstream force key unit event. An upstream force key unit event
Packit 971217
 * can be sent to request upstream elements to produce a key unit.
Packit 971217
 *
Packit 971217
 * @running_time can be set to request a new key unit at a specific
Packit 971217
 * running_time. If set to GST_CLOCK_TIME_NONE, upstream elements will produce a
Packit 971217
 * new key unit as soon as possible.
Packit 971217
 *
Packit 971217
 * To parse an event created by gst_video_event_new_downstream_force_key_unit() use
Packit 971217
 * gst_video_event_parse_downstream_force_key_unit().
Packit 971217
 *
Packit 971217
 * Returns: The new GstEvent
Packit 971217
 */
Packit 971217
GstEvent *
Packit 971217
gst_video_event_new_upstream_force_key_unit (GstClockTime running_time,
Packit 971217
    gboolean all_headers, guint count)
Packit 971217
{
Packit 971217
  GstEvent *force_key_unit_event;
Packit 971217
  GstStructure *s;
Packit 971217
Packit 971217
  s = gst_structure_new (GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME,
Packit 971217
      "running-time", GST_TYPE_CLOCK_TIME, running_time,
Packit 971217
      "all-headers", G_TYPE_BOOLEAN, all_headers,
Packit 971217
      "count", G_TYPE_UINT, count, NULL);
Packit 971217
  force_key_unit_event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
Packit 971217
Packit 971217
  return force_key_unit_event;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_is_force_key_unit:
Packit 971217
 * @event: A #GstEvent to check
Packit 971217
 *
Packit 971217
 * Checks if an event is a force key unit event. Returns true for both upstream
Packit 971217
 * and downstream force key unit events.
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if the event is a valid force key unit event
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_event_is_force_key_unit (GstEvent * event)
Packit 971217
{
Packit 971217
  const GstStructure *s;
Packit 971217
Packit 971217
  g_return_val_if_fail (event != NULL, FALSE);
Packit 971217
Packit 971217
  if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM &&
Packit 971217
      GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
Packit 971217
    return FALSE;               /* Not a force key unit event */
Packit 971217
Packit 971217
  s = gst_event_get_structure (event);
Packit 971217
  if (s == NULL
Packit 971217
      || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_parse_downstream_force_key_unit:
Packit 971217
 * @event: A #GstEvent to parse
Packit 971217
 * @timestamp: (out): A pointer to the timestamp in the event
Packit 971217
 * @stream_time: (out): A pointer to the stream-time in the event
Packit 971217
 * @running_time: (out): A pointer to the running-time in the event
Packit 971217
 * @all_headers: (out): A pointer to the all_headers flag in the event
Packit 971217
 * @count: (out): A pointer to the count field of the event
Packit 971217
 *
Packit 971217
 * Get timestamp, stream-time, running-time, all-headers and count in the force
Packit 971217
 * key unit event. See gst_video_event_new_downstream_force_key_unit() for a
Packit 971217
 * full description of the downstream force key unit event.
Packit 971217
 *
Packit 971217
 * @running_time will be adjusted for any pad offsets of pads it was passing through.
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if the event is a valid downstream force key unit event.
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_event_parse_downstream_force_key_unit (GstEvent * event,
Packit 971217
    GstClockTime * timestamp, GstClockTime * stream_time,
Packit 971217
    GstClockTime * running_time, gboolean * all_headers, guint * count)
Packit 971217
{
Packit 971217
  const GstStructure *s;
Packit 971217
  GstClockTime ev_timestamp, ev_stream_time, ev_running_time;
Packit 971217
  gboolean ev_all_headers;
Packit 971217
  guint ev_count;
Packit 971217
Packit 971217
  g_return_val_if_fail (event != NULL, FALSE);
Packit 971217
Packit 971217
  if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_DOWNSTREAM)
Packit 971217
    return FALSE;               /* Not a force key unit event */
Packit 971217
Packit 971217
  s = gst_event_get_structure (event);
Packit 971217
  if (s == NULL
Packit 971217
      || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  if (!gst_structure_get_clock_time (s, "timestamp", &ev_timestamp))
Packit 971217
    ev_timestamp = GST_CLOCK_TIME_NONE;
Packit 971217
  if (!gst_structure_get_clock_time (s, "stream-time", &ev_stream_time))
Packit 971217
    ev_stream_time = GST_CLOCK_TIME_NONE;
Packit 971217
  if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
Packit 971217
    ev_running_time = GST_CLOCK_TIME_NONE;
Packit 971217
  if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
Packit 971217
    ev_all_headers = FALSE;
Packit 971217
  if (!gst_structure_get_uint (s, "count", &ev_count))
Packit 971217
    ev_count = 0;
Packit 971217
Packit 971217
  if (timestamp)
Packit 971217
    *timestamp = ev_timestamp;
Packit 971217
Packit 971217
  if (stream_time)
Packit 971217
    *stream_time = ev_stream_time;
Packit 971217
Packit 971217
  if (running_time) {
Packit 971217
    gint64 offset = gst_event_get_running_time_offset (event);
Packit 971217
Packit 971217
    *running_time = ev_running_time;
Packit 971217
    /* Catch underflows */
Packit 971217
    if (*running_time > -offset)
Packit 971217
      *running_time += offset;
Packit 971217
    else
Packit 971217
      *running_time = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  if (all_headers)
Packit 971217
    *all_headers = ev_all_headers;
Packit 971217
Packit 971217
  if (count)
Packit 971217
    *count = ev_count;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
/**
Packit 971217
 * gst_video_event_parse_upstream_force_key_unit:
Packit 971217
 * @event: A #GstEvent to parse
Packit 971217
 * @running_time: (out): A pointer to the running_time in the event
Packit 971217
 * @all_headers: (out): A pointer to the all_headers flag in the event
Packit 971217
 * @count: (out): A pointer to the count field in the event
Packit 971217
 *
Packit 971217
 * Get running-time, all-headers and count in the force key unit event. See
Packit 971217
 * gst_video_event_new_upstream_force_key_unit() for a full description of the
Packit 971217
 * upstream force key unit event.
Packit 971217
 *
Packit 971217
 * Create an upstream force key unit event using  gst_video_event_new_upstream_force_key_unit()
Packit 971217
 *
Packit 971217
 * @running_time will be adjusted for any pad offsets of pads it was passing through.
Packit 971217
 *
Packit 971217
 * Returns: %TRUE if the event is a valid upstream force-key-unit event. %FALSE if not
Packit 971217
 */
Packit 971217
gboolean
Packit 971217
gst_video_event_parse_upstream_force_key_unit (GstEvent * event,
Packit 971217
    GstClockTime * running_time, gboolean * all_headers, guint * count)
Packit 971217
{
Packit 971217
  const GstStructure *s;
Packit 971217
  GstClockTime ev_running_time;
Packit 971217
  gboolean ev_all_headers;
Packit 971217
  guint ev_count;
Packit 971217
Packit 971217
  g_return_val_if_fail (event != NULL, FALSE);
Packit 971217
Packit 971217
  if (GST_EVENT_TYPE (event) != GST_EVENT_CUSTOM_UPSTREAM)
Packit 971217
    return FALSE;               /* Not a force key unit event */
Packit 971217
Packit 971217
  s = gst_event_get_structure (event);
Packit 971217
  if (s == NULL
Packit 971217
      || !gst_structure_has_name (s, GST_VIDEO_EVENT_FORCE_KEY_UNIT_NAME))
Packit 971217
    return FALSE;
Packit 971217
Packit 971217
  if (!gst_structure_get_clock_time (s, "running-time", &ev_running_time))
Packit 971217
    ev_running_time = GST_CLOCK_TIME_NONE;
Packit 971217
  if (!gst_structure_get_boolean (s, "all-headers", &ev_all_headers))
Packit 971217
    ev_all_headers = FALSE;
Packit 971217
  if (!gst_structure_get_uint (s, "count", &ev_count))
Packit 971217
    ev_count = 0;
Packit 971217
Packit 971217
Packit 971217
  if (running_time) {
Packit 971217
    gint64 offset = gst_event_get_running_time_offset (event);
Packit 971217
Packit 971217
    *running_time = ev_running_time;
Packit 971217
    /* Catch underflows */
Packit 971217
    if (*running_time > -offset)
Packit 971217
      *running_time += offset;
Packit 971217
    else
Packit 971217
      *running_time = 0;
Packit 971217
  }
Packit 971217
Packit 971217
  if (all_headers)
Packit 971217
    *all_headers = ev_all_headers;
Packit 971217
Packit 971217
  if (count)
Packit 971217
    *count = ev_count;
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}