Blame tests/icles/stress-videooverlay.c

Packit 971217
/* GStreamer
Packit 971217
 * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
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
#ifdef HAVE_CONFIG_H
Packit 971217
#include "config.h"
Packit 971217
#endif
Packit 971217
Packit 971217
#include <gst/gst.h>
Packit 971217
#include <gst/video/videooverlay.h>
Packit 971217
Packit 971217
#include <X11/Xlib.h>
Packit 971217
#include <X11/Xutil.h>
Packit 971217
Packit 971217
#include <math.h>
Packit 971217
#include <sys/time.h>
Packit 971217
Packit 971217
static GMainLoop *loop;
Packit 971217
Packit 971217
static Display *disp;
Packit 971217
static Window root, win = 0;
Packit 971217
static GC gc;
Packit 971217
static gint width = 320, height = 240, x = 0, y = 0;
Packit 971217
static gint disp_width, disp_height;
Packit 971217
Packit 971217
static inline long
Packit 971217
myclock (void)
Packit 971217
{
Packit 971217
  struct timeval tv;
Packit 971217
Packit 971217
  gettimeofday (&tv, NULL);
Packit 971217
  return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
open_display (void)
Packit 971217
{
Packit 971217
  gint screen_num;
Packit 971217
Packit 971217
  disp = XOpenDisplay (NULL);
Packit 971217
  root = DefaultRootWindow (disp);
Packit 971217
  screen_num = DefaultScreen (disp);
Packit 971217
  disp_width = DisplayWidth (disp, screen_num);
Packit 971217
  disp_height = DisplayHeight (disp, screen_num);
Packit 971217
}
Packit 971217
Packit 971217
static void
Packit 971217
close_display (void)
Packit 971217
{
Packit 971217
  XCloseDisplay (disp);
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
resize_window (GstPipeline * pipeline)
Packit 971217
{
Packit 971217
  width = (sin (myclock () / 300.0) * 200) + 640;
Packit 971217
  height = (sin (myclock () / 300.0) * 200) + 480;
Packit 971217
Packit 971217
  XResizeWindow (disp, win, width, height);
Packit 971217
Packit 971217
  XSync (disp, FALSE);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
move_window (GstPipeline * pipeline)
Packit 971217
{
Packit 971217
  x += 5;
Packit 971217
  y = disp_height - height + (sin (myclock () / 300.0) * height);
Packit 971217
  if (x > disp_width)
Packit 971217
    x = 0;
Packit 971217
Packit 971217
  XMoveWindow (disp, win, x, y);
Packit 971217
Packit 971217
  XSync (disp, FALSE);
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
toggle_events (GstVideoOverlay * ov)
Packit 971217
{
Packit 971217
  static gboolean events_toggled;
Packit 971217
Packit 971217
  gst_video_overlay_handle_events (ov, events_toggled);
Packit 971217
Packit 971217
  if (events_toggled) {
Packit 971217
    g_print ("Events are handled\n");
Packit 971217
    events_toggled = FALSE;
Packit 971217
  } else {
Packit 971217
    g_print ("Events are NOT handled\n");
Packit 971217
    events_toggled = TRUE;
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
cycle_window (GstVideoOverlay * ov)
Packit 971217
{
Packit 971217
  XGCValues values;
Packit 971217
  Window old_win = win;
Packit 971217
  GC old_gc = gc;
Packit 971217
Packit 971217
  win = XCreateSimpleWindow (disp, root, 0, 0, width, height, 0, 0, 0);
Packit 971217
Packit 971217
  XSetWindowBackgroundPixmap (disp, win, None);
Packit 971217
Packit 971217
  gc = XCreateGC (disp, win, 0, &values);
Packit 971217
Packit 971217
  XMapRaised (disp, win);
Packit 971217
Packit 971217
  XSync (disp, FALSE);
Packit 971217
Packit 971217
  gst_video_overlay_set_window_handle (ov, win);
Packit 971217
Packit 971217
  if (old_win) {
Packit 971217
    XDestroyWindow (disp, old_win);
Packit 971217
    XFreeGC (disp, old_gc);
Packit 971217
    XSync (disp, FALSE);
Packit 971217
  }
Packit 971217
Packit 971217
  return TRUE;
Packit 971217
}
Packit 971217
Packit 971217
static GstBusSyncReply
Packit 971217
create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
Packit 971217
{
Packit 971217
  GstVideoOverlay *ov = NULL;
Packit 971217
Packit 971217
  if (!gst_is_video_overlay_prepare_window_handle_message (message))
Packit 971217
    return GST_BUS_PASS;
Packit 971217
Packit 971217
  ov = GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message));
Packit 971217
Packit 971217
  g_print ("Creating our own window\n");
Packit 971217
Packit 971217
  cycle_window (ov);
Packit 971217
Packit 971217
  g_timeout_add (50, (GSourceFunc) resize_window, pipeline);
Packit 971217
  g_timeout_add (50, (GSourceFunc) move_window, pipeline);
Packit 971217
  g_timeout_add (100, (GSourceFunc) cycle_window, ov);
Packit 971217
  g_timeout_add_seconds (2, (GSourceFunc) toggle_events, ov);
Packit 971217
Packit 971217
  gst_message_unref (message);
Packit 971217
  return GST_BUS_DROP;
Packit 971217
}
Packit 971217
Packit 971217
#if 0
Packit 971217
static gboolean
Packit 971217
terminate_playback (GstElement * pipeline)
Packit 971217
{
Packit 971217
  g_print ("Terminating playback\n");
Packit 971217
  g_main_loop_quit (loop);
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
#endif
Packit 971217
Packit 971217
static gboolean
Packit 971217
pause_playback (GstElement * pipeline)
Packit 971217
{
Packit 971217
  g_print ("Pausing playback\n");
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_PAUSED);
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
static gboolean
Packit 971217
start_playback (GstElement * pipeline)
Packit 971217
{
Packit 971217
  g_print ("Starting playback\n");
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
  return FALSE;
Packit 971217
}
Packit 971217
Packit 971217
int
Packit 971217
main (int argc, char **argv)
Packit 971217
{
Packit 971217
  GstElement *pipeline;
Packit 971217
  GstBus *bus;
Packit 971217
Packit 971217
#ifndef GST_DISABLE_PARSE
Packit 971217
  GError *error = NULL;
Packit 971217
#endif
Packit 971217
Packit 971217
  gst_init (&argc, &argv);
Packit 971217
Packit 971217
  if (argc != 2) {
Packit 971217
    g_print ("Usage: %s \"pipeline description with launch format\"\n",
Packit 971217
        argv[0]);
Packit 971217
    g_print
Packit 971217
        ("The pipeline should contain an element implementing GstVideoOverlay.\n");
Packit 971217
    g_print ("Example: %s \"videotestsrc ! ximagesink\"\n", argv[0]);
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
#ifdef GST_DISABLE_PARSE
Packit 971217
  g_print ("GStreamer was built without pipeline parsing capabilities.\n");
Packit 971217
  g_print
Packit 971217
      ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
Packit 971217
  return 1;
Packit 971217
#else
Packit 971217
  pipeline = gst_parse_launch (argv[1], &error);
Packit 971217
  if (error) {
Packit 971217
    g_print ("Error while parsing pipeline description: %s\n", error->message);
Packit 971217
    return -1;
Packit 971217
  }
Packit 971217
#endif
Packit 971217
Packit 971217
  loop = g_main_loop_new (NULL, FALSE);
Packit 971217
Packit 971217
  open_display ();
Packit 971217
Packit 971217
  bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
Packit 971217
  gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, pipeline,
Packit 971217
      NULL);
Packit 971217
Packit 971217
  gst_element_set_state (pipeline, GST_STATE_PLAYING);
Packit 971217
Packit 971217
  /* We want to get out after */
Packit 971217
  //g_timeout_add (500000, (GSourceFunc) terminate_playback, pipeline);
Packit 971217
  g_timeout_add_seconds (10, (GSourceFunc) pause_playback, pipeline);
Packit 971217
  g_timeout_add_seconds (20, (GSourceFunc) start_playback, pipeline);
Packit 971217
Packit 971217
  g_main_loop_run (loop);
Packit 971217
Packit 971217
  close_display ();
Packit 971217
Packit 971217
  g_main_loop_unref (loop);
Packit 971217
Packit 971217
  gst_object_unref (bus);
Packit 971217
Packit 971217
  return 0;
Packit 971217
}