2008-09-04 13:46:52 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) <2008> Stefan Kost <ensonic@users.sf.net>
|
|
|
|
*
|
|
|
|
* test-colorkey: test manual colorkey handling
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2008-10-22 12:25:02 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2008-09-04 13:46:52 +00:00
|
|
|
#include <glib.h>
|
2008-10-22 12:25:02 +00:00
|
|
|
#include <gdk/gdkx.h>
|
2008-09-04 13:46:52 +00:00
|
|
|
#include <gtk/gtk.h>
|
|
|
|
|
2008-10-22 12:25:02 +00:00
|
|
|
#include <gst/gst.h>
|
2008-09-04 13:46:52 +00:00
|
|
|
#include <gst/interfaces/xoverlay.h>
|
2008-10-22 12:25:02 +00:00
|
|
|
#include <gst/interfaces/propertyprobe.h>
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2010-03-14 22:14:19 +00:00
|
|
|
#if !GTK_CHECK_VERSION (2, 17, 7)
|
|
|
|
static void
|
|
|
|
gtk_widget_get_allocation (GtkWidget * w, GtkAllocation * a)
|
|
|
|
{
|
|
|
|
*a = w->allocation;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-04 13:46:52 +00:00
|
|
|
static GtkWidget *video_window = NULL;
|
|
|
|
static GstElement *sink = NULL;
|
2009-12-29 00:53:53 +00:00
|
|
|
static gulong embed_xid = 0;
|
2010-07-24 09:48:33 +00:00
|
|
|
static GdkColor trans_color;
|
|
|
|
static gboolean trans_color_set = FALSE;
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2008-10-22 12:25:02 +00:00
|
|
|
static void
|
|
|
|
redraw_overlay (GtkWidget * widget)
|
2008-09-04 13:46:52 +00:00
|
|
|
{
|
2010-03-14 22:14:19 +00:00
|
|
|
GtkAllocation allocation;
|
|
|
|
GdkWindow *window = gtk_widget_get_window (widget);
|
2010-07-24 09:48:33 +00:00
|
|
|
cairo_t *cr;
|
2010-03-14 22:14:19 +00:00
|
|
|
|
2010-07-24 09:48:33 +00:00
|
|
|
cr = gdk_cairo_create (window);
|
2010-03-14 22:14:19 +00:00
|
|
|
gtk_widget_get_allocation (widget, &allocation);
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2010-07-24 09:48:33 +00:00
|
|
|
cairo_set_source_rgb (cr, 1, 1, 1);
|
|
|
|
cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
|
|
|
|
cairo_fill (cr);
|
|
|
|
|
|
|
|
if (trans_color_set) {
|
2008-09-04 13:46:52 +00:00
|
|
|
guint x, y;
|
2010-03-14 22:14:19 +00:00
|
|
|
guint h = allocation.height * 0.75;
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2010-07-24 09:48:33 +00:00
|
|
|
gdk_cairo_set_source_color (cr, &trans_color);
|
|
|
|
cairo_rectangle (cr, 0, 0, allocation.width, h);
|
|
|
|
cairo_fill (cr);
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2010-03-14 22:14:19 +00:00
|
|
|
for (y = h; y < allocation.height; y++) {
|
|
|
|
for (x = 0; x < allocation.width; x++) {
|
2008-09-04 13:46:52 +00:00
|
|
|
if (((x & 1) || (y & 1)) && (x & 1) != (y & 1)) {
|
2010-07-24 09:48:33 +00:00
|
|
|
cairo_move_to (cr, x, y);
|
|
|
|
cairo_paint (cr);
|
2008-09-04 13:46:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-10-22 12:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
handle_resize_cb (GtkWidget * widget, GdkEventConfigure * event, gpointer data)
|
|
|
|
{
|
|
|
|
redraw_overlay (widget);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
handle_expose_cb (GtkWidget * widget, GdkEventExpose * event, gpointer data)
|
|
|
|
{
|
|
|
|
redraw_overlay (widget);
|
2008-09-04 13:46:52 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2009-12-29 00:53:53 +00:00
|
|
|
static void
|
|
|
|
realize_cb (GtkWidget * widget, gpointer data)
|
|
|
|
{
|
2010-01-04 09:49:25 +00:00
|
|
|
#if GTK_CHECK_VERSION(2,18,0)
|
2010-03-14 22:14:19 +00:00
|
|
|
{
|
|
|
|
GdkWindow *window = gtk_widget_get_window (widget);
|
|
|
|
|
|
|
|
/* This is here just for pedagogical purposes, GDK_WINDOW_XID will call it
|
|
|
|
* as well */
|
|
|
|
if (!gdk_window_ensure_native (window))
|
|
|
|
g_error ("Couldn't create native window needed for GstXOverlay!");
|
|
|
|
}
|
2010-01-04 09:49:25 +00:00
|
|
|
#endif
|
2009-12-29 00:53:53 +00:00
|
|
|
|
2010-03-14 22:14:19 +00:00
|
|
|
{
|
|
|
|
GdkWindow *window = gtk_widget_get_window (video_window);
|
|
|
|
|
|
|
|
embed_xid = GDK_WINDOW_XID (window);
|
|
|
|
g_print ("Window realize: video window XID = %lu\n", embed_xid);
|
|
|
|
}
|
2009-12-29 00:53:53 +00:00
|
|
|
}
|
|
|
|
|
2008-09-04 13:46:52 +00:00
|
|
|
static void
|
|
|
|
msg_state_changed (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
|
|
|
|
{
|
|
|
|
const GstStructure *s;
|
|
|
|
|
|
|
|
s = gst_message_get_structure (message);
|
|
|
|
|
|
|
|
/* We only care about state changed on the pipeline */
|
|
|
|
if (s && GST_MESSAGE_SRC (message) == GST_OBJECT_CAST (pipeline)) {
|
|
|
|
GstState old, new, pending;
|
|
|
|
gint color;
|
|
|
|
|
|
|
|
gst_message_parse_state_changed (message, &old, &new, &pending);
|
|
|
|
|
|
|
|
/* When state of the pipeline changes to paused or playing we start updating scale */
|
|
|
|
switch (GST_STATE_TRANSITION (old, new)) {
|
2010-03-14 22:14:19 +00:00
|
|
|
case GST_STATE_CHANGE_READY_TO_PAUSED:{
|
2008-09-04 13:46:52 +00:00
|
|
|
g_object_get (G_OBJECT (sink), "colorkey", &color, NULL);
|
|
|
|
if (color != -1) {
|
2010-07-24 09:48:33 +00:00
|
|
|
trans_color.red = (color & 0xff0000) >> 8;
|
|
|
|
trans_color.green = (color & 0xff00);
|
|
|
|
trans_color.blue = (color & 0xff) << 8;
|
|
|
|
trans_color_set = TRUE;
|
|
|
|
} else {
|
|
|
|
trans_color_set = FALSE;
|
2008-09-04 13:46:52 +00:00
|
|
|
}
|
|
|
|
handle_resize_cb (video_window, NULL, NULL);
|
|
|
|
break;
|
2010-03-14 22:14:19 +00:00
|
|
|
}
|
2008-09-04 13:46:52 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
|
|
|
|
{
|
|
|
|
GstElement *pipeline = user_data;
|
|
|
|
|
|
|
|
g_print ("stopping\n");
|
|
|
|
gtk_widget_hide_all (widget);
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gtk_main_quit ();
|
|
|
|
}
|
|
|
|
|
2009-07-31 11:27:28 +00:00
|
|
|
static gboolean
|
|
|
|
start_pipeline (gpointer user_data)
|
|
|
|
{
|
|
|
|
GstElement *pipeline = GST_ELEMENT (user_data);
|
|
|
|
GstStateChangeReturn sret;
|
|
|
|
|
|
|
|
sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
|
|
|
|
if (sret == GST_STATE_CHANGE_FAILURE) {
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
gtk_main_quit ();
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2008-09-04 13:46:52 +00:00
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
GtkWidget *window;
|
|
|
|
GstElement *pipeline, *src;
|
|
|
|
GstBus *bus;
|
2008-10-22 12:25:02 +00:00
|
|
|
GstStateChangeReturn sret;
|
|
|
|
GstPropertyProbe *probe;
|
|
|
|
GValueArray *arr;
|
2008-09-04 13:46:52 +00:00
|
|
|
|
|
|
|
if (!g_thread_supported ())
|
|
|
|
g_thread_init (NULL);
|
|
|
|
|
|
|
|
gst_init (&argc, &argv);
|
|
|
|
gtk_init (&argc, &argv);
|
|
|
|
|
|
|
|
/* prepare the pipeline */
|
|
|
|
|
|
|
|
pipeline = gst_pipeline_new ("xvoverlay");
|
|
|
|
src = gst_element_factory_make ("videotestsrc", NULL);
|
|
|
|
sink = gst_element_factory_make ("xvimagesink", NULL);
|
|
|
|
gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
|
|
|
|
gst_element_link (src, sink);
|
|
|
|
|
2009-12-29 00:53:53 +00:00
|
|
|
#define COLOR_GRAY 0x7F7F7F
|
|
|
|
|
|
|
|
g_object_set (G_OBJECT (sink), "autopaint-colorkey", FALSE,
|
|
|
|
"force-aspect-ratio", TRUE, "draw-borders", FALSE,
|
|
|
|
"colorkey", COLOR_GRAY, NULL);
|
2008-10-22 12:25:02 +00:00
|
|
|
|
|
|
|
/* check xvimagesink capabilities */
|
|
|
|
sret = gst_element_set_state (pipeline, GST_STATE_READY);
|
|
|
|
if (sret == GST_STATE_CHANGE_FAILURE) {
|
2009-12-29 00:53:53 +00:00
|
|
|
g_printerr ("Can't set pipeline to READY\n");
|
2008-10-22 12:25:02 +00:00
|
|
|
gst_object_unref (pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
probe = GST_PROPERTY_PROBE (sink);
|
|
|
|
if (!probe) {
|
|
|
|
g_printerr ("Can't probe sink\n");
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
arr =
|
|
|
|
gst_property_probe_probe_and_get_values_name (probe,
|
|
|
|
"autopaint-colorkey");
|
|
|
|
if (!arr || !arr->n_values) {
|
|
|
|
g_printerr ("Can't disable autopaint-colorkey property\n");
|
|
|
|
gst_element_set_state (pipeline, GST_STATE_NULL);
|
|
|
|
gst_object_unref (pipeline);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (arr)
|
|
|
|
g_value_array_free (arr);
|
2008-09-04 13:46:52 +00:00
|
|
|
|
|
|
|
bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
|
|
|
|
gst_bus_add_signal_watch_full (bus, G_PRIORITY_HIGH);
|
|
|
|
g_signal_connect (bus, "message::state-changed",
|
|
|
|
G_CALLBACK (msg_state_changed), pipeline);
|
|
|
|
gst_object_unref (bus);
|
|
|
|
|
|
|
|
/* prepare the ui */
|
|
|
|
|
|
|
|
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
|
|
|
|
g_signal_connect (G_OBJECT (window), "delete-event",
|
|
|
|
G_CALLBACK (window_closed), (gpointer) pipeline);
|
2008-10-22 12:25:02 +00:00
|
|
|
gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
|
|
|
|
|
|
|
|
video_window = gtk_drawing_area_new ();
|
2008-09-04 13:46:52 +00:00
|
|
|
g_signal_connect (G_OBJECT (video_window), "configure-event",
|
|
|
|
G_CALLBACK (handle_resize_cb), NULL);
|
2008-10-22 12:25:02 +00:00
|
|
|
g_signal_connect (G_OBJECT (video_window), "expose-event",
|
|
|
|
G_CALLBACK (handle_expose_cb), NULL);
|
2009-12-29 00:53:53 +00:00
|
|
|
g_signal_connect (video_window, "realize", G_CALLBACK (realize_cb), NULL);
|
2008-10-22 12:25:02 +00:00
|
|
|
gtk_widget_set_double_buffered (video_window, FALSE);
|
|
|
|
gtk_container_add (GTK_CONTAINER (window), video_window);
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2008-10-22 12:25:02 +00:00
|
|
|
/* show the gui and play */
|
2008-09-04 13:46:52 +00:00
|
|
|
|
2008-10-22 12:25:02 +00:00
|
|
|
gtk_widget_show_all (window);
|
2009-12-29 00:53:53 +00:00
|
|
|
|
|
|
|
/* realize window now so that the video window gets created and we can
|
|
|
|
* obtain its XID before the pipeline is started up and the videosink
|
|
|
|
* asks for the XID of the window to render onto */
|
|
|
|
gtk_widget_realize (window);
|
|
|
|
|
|
|
|
/* we should have the XID now */
|
|
|
|
g_assert (embed_xid != 0);
|
|
|
|
|
|
|
|
/* we know what the video sink is in this case (xvimagesink), so we can
|
|
|
|
* just set it directly here now (instead of waiting for a prepare-xwindow-id
|
|
|
|
* element message in a sync bus handler and setting it there) */
|
|
|
|
g_print ("setting XID %lu\n", embed_xid);
|
|
|
|
gst_x_overlay_set_xwindow_id (GST_X_OVERLAY (sink), embed_xid);
|
|
|
|
|
2009-07-31 11:27:28 +00:00
|
|
|
g_idle_add (start_pipeline, pipeline);
|
2008-09-04 13:46:52 +00:00
|
|
|
gtk_main ();
|
2009-07-31 11:27:28 +00:00
|
|
|
|
2008-09-04 13:46:52 +00:00
|
|
|
gst_object_unref (pipeline);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|