2 initially based on an example by Tristan Matthews
3 http://tristanswork.blogspot.com/2008/09/fullscreen-video-in-gstreamer-with-gtk.html
7 #include <gst/interfaces/xoverlay.h>
13 static void hide_mouse(GtkWidget
*widget
){
14 GdkWindow
*w
= GDK_WINDOW(widget
->window
);
15 GdkDisplay
*display
= gdk_display_get_default();
16 GdkCursor
*cursor
= gdk_cursor_new_for_display(display
, GDK_BLANK_CURSOR
);
17 gdk_window_set_cursor(w
, cursor
);
18 gdk_cursor_unref (cursor
);
22 post_tee_pipeline(GstPipeline
*pipeline
, GstElement
*tee
, GstElement
*sink
,
23 int rngseed
, int colour
, int timer
, int debug
){
24 GstElement
*queue
= gst_element_factory_make("queue", NULL
);
25 GstElement
*sparrow
= gst_element_factory_make("sparrow", NULL
);
26 GstElement
*caps_posteriori
= gst_element_factory_make("capsfilter", NULL
);
27 GstElement
*cs_posteriori
= gst_element_factory_make("ffmpegcolorspace", NULL
);
29 g_object_set(G_OBJECT(caps_posteriori
), "caps",
30 gst_caps_new_simple ("video/x-raw-rgb",
33 g_object_set(G_OBJECT(sparrow
),
38 //"reload", "dumpfiles/gtk.dump",
39 //"save", "dumpfiles/gtk.dump",
42 gst_bin_add_many (GST_BIN(pipeline
),
50 gst_element_link_many(tee
,
60 pre_tee_pipeline(GstPipeline
*pipeline
){
61 if (pipeline
== NULL
){
62 pipeline
= GST_PIPELINE(gst_pipeline_new("sparrow_pipeline"));
64 GstElement
*src
= gst_element_factory_make("v4l2src", NULL
);
65 GstElement
*caps_priori
= gst_element_factory_make("capsfilter", NULL
);
66 GstElement
*cs_priori
= gst_element_factory_make("ffmpegcolorspace", NULL
);
67 GstElement
*caps_interiori
= gst_element_factory_make("capsfilter", NULL
);
68 GstElement
*tee
= gst_element_factory_make ("tee", NULL
);
70 g_object_set(G_OBJECT(caps_priori
), "caps",
71 gst_caps_new_simple ("video/x-raw-yuv",
72 "format", GST_TYPE_FOURCC
, GST_MAKE_FOURCC('Y', 'U', 'Y', '2'),
75 g_object_set(G_OBJECT(caps_interiori
), "caps",
76 gst_caps_new_simple ("video/x-raw-rgb",
79 gst_bin_add_many(GST_BIN(pipeline
),
87 gst_element_link_many(src
,
98 make_multi_pipeline(windows_t
*windows
, int count
)
100 GstPipeline
*pipeline
= GST_PIPELINE(gst_pipeline_new("sparrow_pipeline"));
101 GstElement
*tee
= pre_tee_pipeline(pipeline
);
104 for (i
= 0; i
< count
; i
++){
105 GstElement
*sink
= windows
->sinks
[i
];
107 //(pipeline, tee, sink, int rngseed, int colour, timer flag, int debug flag)
108 /* timer should only run on one of them. colour >= 3 is undefined */
109 int debug
= option_debug
&& i
== 0;
110 post_tee_pipeline(pipeline
, tee
, sink
, i
, i
+ 1, i
== 0, debug
);
117 bus_call(GstBus
* bus
, GstMessage
*msg
, gpointer data
)
119 windows_t
*windows
= (windows_t
*)data
;
120 if (GST_MESSAGE_TYPE(msg
) == GST_MESSAGE_ELEMENT
&&
121 gst_structure_has_name(msg
->structure
, "prepare-xwindow-id")){
122 g_print("Got prepare-xwindow-id msg. option screens: %d\n", option_screens
);
123 for (int i
= 0; i
< option_screens
; i
++){
124 gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(windows
->sinks
[i
]),
125 windows
->xwindows
[i
]);
126 g_print("connected sink %d to window %lu\n", i
, windows
->xwindows
[i
]);
127 hide_mouse(windows
->gtk_windows
[i
]);
133 toggle_fullscreen(GtkWidget
*widget
){
134 GdkWindowState state
= gdk_window_get_state(GDK_WINDOW(widget
->window
));
135 if (state
== GDK_WINDOW_STATE_FULLSCREEN
){
136 gtk_window_unfullscreen(GTK_WINDOW(widget
));
139 gtk_window_fullscreen(GTK_WINDOW(widget
));
144 key_press_event_cb(GtkWidget
*widget
, GdkEventKey
*event
, gpointer data
)
146 g_print("got key %c\n", event
->keyval
);
147 switch (event
->keyval
){
149 toggle_fullscreen(widget
);
152 g_signal_emit_by_name(widget
, "destroy");
160 void destroy_cb(GtkWidget
* widget
, gpointer data
)
162 GMainLoop
*loop
= (GMainLoop
*) data
;
163 g_print("Window destroyed\n");
164 g_main_loop_quit(loop
);
168 video_widget_realize_cb(GtkWidget
*widget
, gpointer data
)
170 windows_t
*windows
= (windows_t
*)data
;
171 int r
= windows
->realised
;
172 if (r
< MAX_SCREENS
){
173 windows
->xwindows
[r
] = GDK_WINDOW_XID(GDK_WINDOW(widget
->window
));
174 g_print("realised window %d with XID %lu\n", r
, windows
->xwindows
[r
]);
177 g_print("wtf, there seem to be %d windows!\n", r
);
185 set_up_window(GMainLoop
*loop
, GtkWidget
*window
, int screen_no
){
186 static const GdkColor black
= {0, 0, 0, 0};
187 gtk_window_set_default_size(GTK_WINDOW(window
), WIDTH
, HEIGHT
);
189 if (option_fullscreen
){
190 gtk_window_fullscreen(GTK_WINDOW(window
));
193 /*if more than one screen is requested, set the screen number.
194 otherwise let it fall were it falls */
195 if (option_screens
> 1){
196 /* "screen" is not the same thing as "monitor" */
197 GdkScreen
* screen
= gdk_screen_get_default();
198 int width
= gdk_screen_get_width(screen
);
199 //int monitor = gdk_screen_get_monitor_at_point(screen,
200 // width / 2 + width * screen_no, 50);
201 gtk_window_move(GTK_WINDOW(window
), (width
/ 2 * screen_no
+ 50), 50);
204 //GdkDisplay *display = gdk_display_get_default();
205 //GdkScreen *screen = gdk_display_get_screen(display, screen_no);
206 //gtk_window_set_screen(GTK_WINDOW(window), screen);
209 // attach key press signal to key press callback
210 gtk_widget_set_events(window
, GDK_KEY_PRESS_MASK
);
211 g_signal_connect(G_OBJECT(window
), "key-press-event", G_CALLBACK(key_press_event_cb
), NULL
);
212 g_signal_connect(G_OBJECT(window
), "destroy", G_CALLBACK(destroy_cb
), loop
);
214 gtk_widget_modify_bg(window
, GTK_STATE_NORMAL
, &black
);
215 gtk_widget_show_all(window
);
220 static GOptionEntry entries
[] =
222 { "full-screen", 'f', 0, G_OPTION_ARG_NONE
, &option_fullscreen
, "run full screen", NULL
},
223 { "screens", 's', 2, G_OPTION_ARG_INT
, &option_screens
, "Use this many screens", "S" },
224 { "debug", 'd', 2, G_OPTION_ARG_NONE
, &option_debug
, "Save debug images in /tmp", NULL
},
225 // { "overlay", 'o', 0, G_OPTION_ARG_NONE, &option_overlay, "Use some kind of overlay", NULL },
226 { NULL
, 0, 0, 0, NULL
, NULL
, NULL
}
230 gint
main (gint argc
, gchar
*argv
[])
232 //initialise threads before any gtk stuff (because not using gtk_init)
234 /*this is more complicated than plain gtk_init/gst_init, so that options from
235 all over can be gathered and presented together.
237 GOptionGroup
*gst_opts
= gst_init_get_option_group();
238 GOptionGroup
*gtk_opts
= gtk_get_option_group(TRUE
);
239 GOptionContext
*ctx
= g_option_context_new("...!");
240 g_option_context_add_main_entries(ctx
, entries
, NULL
);
241 g_option_context_add_group(ctx
, gst_opts
);
242 g_option_context_add_group(ctx
, gtk_opts
);
243 GError
*error
= NULL
;
244 if (!g_option_context_parse(ctx
, &argc
, &argv
, &error
)){
245 g_print ("Error initializing: %s\n", GST_STR_NULL(error
->message
));
248 g_option_context_free(ctx
);
250 GMainLoop
*loop
= g_main_loop_new(NULL
, FALSE
);
253 windows
.realised
= 0;
256 for (i
= 0; i
< option_screens
; i
++){
257 GtkWidget
*window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
258 g_signal_connect(window
, "realize",
259 G_CALLBACK(video_widget_realize_cb
), &windows
);
260 /* set up sink here */
261 GstElement
*sink
= gst_element_factory_make("ximagesink", NULL
);
262 set_up_window(loop
, window
, i
);
263 windows
.gtk_windows
[i
] = window
;
264 windows
.sinks
[i
] = sink
;
267 GstElement
*pipeline
= (GstElement
*)make_multi_pipeline(&windows
, option_screens
);
269 GstBus
*bus
= gst_pipeline_get_bus(GST_PIPELINE(pipeline
));
270 gst_bus_add_watch(bus
, (GstBusFunc
)bus_call
, &windows
);
271 gst_object_unref(bus
);
273 gst_element_set_state(pipeline
, GST_STATE_PLAYING
);
275 g_main_loop_run(loop
);
277 gst_element_set_state (pipeline
, GST_STATE_NULL
);
278 gst_object_unref (pipeline
);