gst_plugins_base/tests/icles/stress-xoverlay.c
changeset 0 0e761a78d257
equal deleted inserted replaced
-1:000000000000 0:0e761a78d257
       
     1 /* GStreamer
       
     2  * Copyright (C) <2005> Julien Moutte <julien@moutte.net>
       
     3  *
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Library General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Library General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Library General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 
       
    20 #ifdef HAVE_CONFIG_H
       
    21 #include "config.h"
       
    22 #endif
       
    23 
       
    24 #include <gst/gst.h>
       
    25 #include <gst/interfaces/xoverlay.h>
       
    26 
       
    27 #include <X11/Xlib.h>
       
    28 #include <X11/Xutil.h>
       
    29 
       
    30 #include <math.h>
       
    31 #include <sys/time.h>
       
    32 
       
    33 static GMainLoop *loop;
       
    34 
       
    35 static Display *disp;
       
    36 static Window root, win = 0;
       
    37 static GC gc;
       
    38 static gint width = 320, height = 240, x = 0, y = 0;
       
    39 static gint disp_width, disp_height;
       
    40 
       
    41 static inline long
       
    42 myclock (void)
       
    43 {
       
    44   struct timeval tv;
       
    45 
       
    46   gettimeofday (&tv, NULL);
       
    47   return (tv.tv_sec * 1000 + tv.tv_usec / 1000);
       
    48 }
       
    49 
       
    50 static void
       
    51 open_display (void)
       
    52 {
       
    53   gint screen_num;
       
    54 
       
    55   disp = XOpenDisplay (NULL);
       
    56   root = DefaultRootWindow (disp);
       
    57   screen_num = DefaultScreen (disp);
       
    58   disp_width = DisplayWidth (disp, screen_num);
       
    59   disp_height = DisplayHeight (disp, screen_num);
       
    60 }
       
    61 
       
    62 static void
       
    63 close_display (void)
       
    64 {
       
    65   XCloseDisplay (disp);
       
    66 }
       
    67 
       
    68 static gboolean
       
    69 resize_window (GstPipeline * pipeline)
       
    70 {
       
    71   width = (sin (myclock () / 300.0) * 200) + 640;
       
    72   height = (sin (myclock () / 300.0) * 200) + 480;
       
    73 
       
    74   XResizeWindow (disp, win, width, height);
       
    75 
       
    76   XSync (disp, FALSE);
       
    77 
       
    78   return TRUE;
       
    79 }
       
    80 
       
    81 static gboolean
       
    82 move_window (GstPipeline * pipeline)
       
    83 {
       
    84   x += 5;
       
    85   y = disp_height - height + (sin (myclock () / 300.0) * height);
       
    86   if (x > disp_width)
       
    87     x = 0;
       
    88 
       
    89   XMoveWindow (disp, win, x, y);
       
    90 
       
    91   XSync (disp, FALSE);
       
    92 
       
    93   return TRUE;
       
    94 }
       
    95 
       
    96 static gboolean
       
    97 toggle_events (GstXOverlay * ov)
       
    98 {
       
    99   static gboolean events_toggled;
       
   100 
       
   101   gst_x_overlay_handle_events (ov, events_toggled);
       
   102 
       
   103   if (events_toggled) {
       
   104     g_print ("Events are handled\n");
       
   105     events_toggled = FALSE;
       
   106   } else {
       
   107     g_print ("Events are NOT handled\n");
       
   108     events_toggled = TRUE;
       
   109   }
       
   110 
       
   111   return TRUE;
       
   112 }
       
   113 
       
   114 static gboolean
       
   115 cycle_window (GstXOverlay * ov)
       
   116 {
       
   117   XGCValues values;
       
   118   Window old_win = win;
       
   119   GC old_gc = gc;
       
   120 
       
   121   win = XCreateSimpleWindow (disp, root, 0, 0, width, height, 0, 0, 0);
       
   122 
       
   123   XSetWindowBackgroundPixmap (disp, win, None);
       
   124 
       
   125   gc = XCreateGC (disp, win, 0, &values);
       
   126 
       
   127   XMapRaised (disp, win);
       
   128 
       
   129   XSync (disp, FALSE);
       
   130 
       
   131   gst_x_overlay_set_xwindow_id (ov, win);
       
   132 
       
   133   if (old_win) {
       
   134     XDestroyWindow (disp, old_win);
       
   135     XFreeGC (disp, old_gc);
       
   136     XSync (disp, FALSE);
       
   137   }
       
   138 
       
   139   return TRUE;
       
   140 }
       
   141 
       
   142 static GstBusSyncReply
       
   143 create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
       
   144 {
       
   145   const GstStructure *s;
       
   146   GstXOverlay *ov = NULL;
       
   147 
       
   148   s = gst_message_get_structure (message);
       
   149   if (!gst_structure_has_name (s, "prepare-xwindow-id")) {
       
   150     return GST_BUS_PASS;
       
   151   }
       
   152 
       
   153   ov = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
       
   154 
       
   155   g_print ("Creating our own window\n");
       
   156 
       
   157   cycle_window (ov);
       
   158 
       
   159   g_timeout_add (50, (GSourceFunc) resize_window, pipeline);
       
   160   g_timeout_add (50, (GSourceFunc) move_window, pipeline);
       
   161   g_timeout_add (100, (GSourceFunc) cycle_window, ov);
       
   162   g_timeout_add (2000, (GSourceFunc) toggle_events, ov);
       
   163 
       
   164   return GST_BUS_DROP;
       
   165 }
       
   166 
       
   167 #if 0
       
   168 static gboolean
       
   169 terminate_playback (GstElement * pipeline)
       
   170 {
       
   171   g_print ("Terminating playback\n");
       
   172   g_main_loop_quit (loop);
       
   173   return FALSE;
       
   174 }
       
   175 #endif
       
   176 
       
   177 static gboolean
       
   178 pause_playback (GstElement * pipeline)
       
   179 {
       
   180   g_print ("Pausing playback\n");
       
   181   gst_element_set_state (pipeline, GST_STATE_PAUSED);
       
   182   return FALSE;
       
   183 }
       
   184 
       
   185 static gboolean
       
   186 start_playback (GstElement * pipeline)
       
   187 {
       
   188   g_print ("Starting playback\n");
       
   189   gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   190   return FALSE;
       
   191 }
       
   192 
       
   193 int
       
   194 main (int argc, char **argv)
       
   195 {
       
   196   GstElement *pipeline;
       
   197   GstBus *bus;
       
   198 
       
   199 #ifndef GST_DISABLE_PARSE
       
   200   GError *error = NULL;
       
   201 #endif
       
   202 
       
   203   gst_init (&argc, &argv);
       
   204 
       
   205   if (argc != 2) {
       
   206     g_print ("Usage: %s \"pipeline description with launch format\"\n",
       
   207         argv[0]);
       
   208     g_print ("The pipeline should contain an element implementing XOverlay.\n");
       
   209     g_print ("Example: %s \"videotestsrc ! ximagesink\"\n", argv[0]);
       
   210     return -1;
       
   211   }
       
   212 #ifdef GST_DISABLE_PARSE
       
   213   g_print ("GStreamer was built without pipeline parsing capabilities.\n");
       
   214   g_print
       
   215       ("Please rebuild GStreamer with pipeline parsing capabilities activated to use this example.\n");
       
   216   return 1;
       
   217 #else
       
   218   pipeline = gst_parse_launch (argv[1], &error);
       
   219   if (error) {
       
   220     g_print ("Error while parsing pipeline description: %s\n", error->message);
       
   221     return -1;
       
   222   }
       
   223 #endif
       
   224 
       
   225   loop = g_main_loop_new (NULL, FALSE);
       
   226 
       
   227   open_display ();
       
   228 
       
   229   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
       
   230   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) create_window, pipeline);
       
   231 
       
   232   gst_element_set_state (pipeline, GST_STATE_PLAYING);
       
   233 
       
   234   /* We want to get out after */
       
   235   //g_timeout_add (500000, (GSourceFunc) terminate_playback, pipeline);
       
   236   g_timeout_add (10000, (GSourceFunc) pause_playback, pipeline);
       
   237   g_timeout_add (20000, (GSourceFunc) start_playback, pipeline);
       
   238 
       
   239   g_main_loop_run (loop);
       
   240 
       
   241   close_display ();
       
   242 
       
   243   g_main_loop_unref (loop);
       
   244 
       
   245   return 0;
       
   246 }