gtk-xoverlay.c 4.14 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* GStreamer
 * Copyright (C) <2010> Stefan Kost <ensonic@users.sf.net>
 *
 * gtk-xoverlay: demonstrate overlay handling using gtk
 *
 * 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 <glib.h>
#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include <gst/gst.h>
#include <gst/interfaces/xoverlay.h>

33
34
#include <string.h>

35
36
37
38
39
static void
window_closed (GtkWidget * widget, GdkEvent * event, gpointer user_data)
{
  GstElement *pipeline = user_data;

40
  gtk_widget_hide (widget);
41
42
43
44
  gst_element_set_state (pipeline, GST_STATE_NULL);
  gtk_main_quit ();
}

45
46
47
/* slightly convoluted way to find a working video sink that's not a bin,
 * one could use autovideosink from gst-plugins-good instead
 */
48
49
50
51
52
53
static GstElement *
find_video_sink (void)
{
  GstStateChangeReturn sret;
  GstElement *sink;

54
55
56
57
  if ((sink = gst_element_factory_make ("xvimagesink", NULL))) {
    sret = gst_element_set_state (sink, GST_STATE_READY);
    if (sret == GST_STATE_CHANGE_SUCCESS)
      return sink;
58

59
60
    gst_element_set_state (sink, GST_STATE_NULL);
  }
61
62
  gst_object_unref (sink);

63
64
65
66
  if ((sink = gst_element_factory_make ("ximagesink", NULL))) {
    sret = gst_element_set_state (sink, GST_STATE_READY);
    if (sret == GST_STATE_CHANGE_SUCCESS)
      return sink;
67

68
69
    gst_element_set_state (sink, GST_STATE_NULL);
  }
70
71
72
73
74
75
  gst_object_unref (sink);

  if (strcmp (DEFAULT_VIDEOSINK, "xvimagesink") == 0 ||
      strcmp (DEFAULT_VIDEOSINK, "ximagesink") == 0)
    return NULL;

76
77
78
79
80
  if ((sink = gst_element_factory_make (DEFAULT_VIDEOSINK, NULL))) {
    if (GST_IS_BIN (sink)) {
      gst_object_unref (sink);
      return NULL;
    }
81

82
83
84
    sret = gst_element_set_state (sink, GST_STATE_READY);
    if (sret == GST_STATE_CHANGE_SUCCESS)
      return sink;
85

86
87
    gst_element_set_state (sink, GST_STATE_NULL);
  }
88
89
90
91
  gst_object_unref (sink);
  return NULL;
}

92
93
94
int
main (int argc, char **argv)
{
95
  GdkWindow *video_window_xwindow;
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  GtkWidget *window, *video_window;
  GstElement *pipeline, *src, *sink;
  gulong embed_xid;
  GstStateChangeReturn sret;

  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);
111
112
113
114
115
  sink = find_video_sink ();

  if (sink == NULL)
    g_error ("Couldn't find a working video sink.");

116
117
118
119
120
121
122
123
124
  gst_bin_add_many (GST_BIN (pipeline), src, sink, NULL);
  gst_element_link (src, sink);

  /* prepare the ui */

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  g_signal_connect (G_OBJECT (window), "delete-event",
      G_CALLBACK (window_closed), (gpointer) pipeline);
  gtk_window_set_default_size (GTK_WINDOW (window), 320, 240);
125
  gtk_window_set_title (GTK_WINDOW (window), "GstXOverlay Gtk+ demo");
126
127
128
129

  video_window = gtk_drawing_area_new ();
  gtk_widget_set_double_buffered (video_window, FALSE);
  gtk_container_add (GTK_CONTAINER (window), video_window);
130
  gtk_container_set_border_width (GTK_CONTAINER (window), 16);
131
132
133
134

  gtk_widget_show_all (window);
  gtk_widget_realize (window);

135
136
  video_window_xwindow = gtk_widget_get_window (video_window);
  embed_xid = GDK_WINDOW_XID (video_window_xwindow);
137
  gst_x_overlay_set_window_handle (GST_X_OVERLAY (sink), embed_xid);
138
139
140
141
142
143
144
145
146
147
148
149

  /* run the pipeline */

  sret = gst_element_set_state (pipeline, GST_STATE_PLAYING);
  if (sret == GST_STATE_CHANGE_FAILURE)
    gst_element_set_state (pipeline, GST_STATE_NULL);
  else
    gtk_main ();

  gst_object_unref (pipeline);
  return 0;
}