forward and reverse gamma tables
[sparrow.git] / gtk-app.c
blob187bc561c7f45195fd74315204dc9ec82a6db790
1 /*
2 initially based on an example by Tristan Matthews
3 http://tristanswork.blogspot.com/2008/09/fullscreen-video-in-gstreamer-with-gtk.html
4 */
5 #include <gst/gst.h>
6 #include <gtk/gtk.h>
7 #include <gst/interfaces/xoverlay.h>
8 #include <gdk/gdk.h>
9 #include <gdk/gdkx.h>
10 #include "gtk-app.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);
21 static void
22 mjpeg_branch(GstPipeline *pipeline, GstElement *tee)
24 /* ! jpegenc ! avimux ! filesink location=mjpeg.avi */
25 GstElement *queue = gst_element_factory_make("queue", NULL);
26 GstElement *jpegenc = gst_element_factory_make("jpegenc", NULL);
27 GstElement *avimux = gst_element_factory_make("avimux", NULL);
28 GstElement *filesink = gst_element_factory_make("filesink", NULL);
29 GstElement *cs = gst_element_factory_make("ffmpegcolorspace", NULL);
30 g_object_set(G_OBJECT(filesink),
31 "location", option_avi,
32 NULL);
33 gst_bin_add_many(GST_BIN(pipeline),
34 queue,
35 cs,
36 jpegenc,
37 avimux,
38 filesink,
39 NULL);
40 gst_element_link_many(tee,
41 queue,
42 cs,
43 jpegenc,
44 avimux,
45 filesink,
46 NULL);
49 static void
50 post_tee_pipeline(GstPipeline *pipeline, GstElement *tee, GstElement *sink,
51 int rngseed, int colour, int timer, int debug, char *save, char *reload){
52 GstElement *queue = gst_element_factory_make("queue", NULL);
53 GstElement *sparrow = gst_element_factory_make("sparrow", NULL);
54 GstElement *caps_posteriori = gst_element_factory_make("capsfilter", NULL);
55 GstElement *cs_posteriori = gst_element_factory_make("ffmpegcolorspace", NULL);
57 g_object_set(G_OBJECT(caps_posteriori), "caps",
58 gst_caps_new_simple ("video/x-raw-rgb",
59 COMMON_CAPS), NULL);
61 g_object_set(G_OBJECT(sparrow),
62 "timer", timer,
63 "debug", debug,
64 "rngseed", rngseed,
65 "colour", colour,
66 NULL);
67 if (reload){
68 g_object_set(G_OBJECT(sparrow),
69 "reload", reload,
70 NULL);
72 if (save){
73 g_object_set(G_OBJECT(sparrow),
74 "save", save,
75 NULL);
78 gst_bin_add_many (GST_BIN(pipeline),
79 queue,
80 sparrow,
81 caps_posteriori,
82 cs_posteriori,
83 sink,
84 NULL);
86 gst_element_link_many(tee,
87 queue,
88 sparrow,
89 caps_posteriori,
90 cs_posteriori,
91 sink,
92 NULL);
95 static GstElement *
96 pre_tee_pipeline(GstPipeline *pipeline){
97 if (pipeline == NULL){
98 pipeline = GST_PIPELINE(gst_pipeline_new("sparrow_pipeline"));
100 GstElement *src = gst_element_factory_make("v4l2src", NULL);
101 GstElement *caps_priori = gst_element_factory_make("capsfilter", NULL);
102 GstElement *cs_priori = gst_element_factory_make("ffmpegcolorspace", NULL);
103 GstElement *caps_interiori = gst_element_factory_make("capsfilter", NULL);
104 GstElement *tee = gst_element_factory_make ("tee", NULL);
106 g_object_set(G_OBJECT(caps_priori), "caps",
107 gst_caps_new_simple ("video/x-raw-yuv",
108 "format", GST_TYPE_FOURCC, GST_MAKE_FOURCC('Y', 'U', 'Y', '2'),
109 COMMON_CAPS), NULL);
111 g_object_set(G_OBJECT(caps_interiori), "caps",
112 gst_caps_new_simple ("video/x-raw-rgb",
113 COMMON_CAPS), NULL);
115 gst_bin_add_many(GST_BIN(pipeline),
116 src,
117 caps_priori,
118 cs_priori,
119 //caps_interiori,
120 tee,
121 NULL);
123 gst_element_link_many(src,
124 caps_priori,
125 cs_priori,
126 //caps_interiori,
127 tee,
128 NULL);
129 return tee;
133 static GstPipeline *
134 make_multi_pipeline(windows_t *windows, int count)
136 GstPipeline *pipeline = GST_PIPELINE(gst_pipeline_new("sparrow_pipeline"));
137 GstElement *tee = pre_tee_pipeline(pipeline);
138 char *reload = NULL;
139 char *save = NULL;
140 int i;
141 for (i = 0; i < count; i++){
142 GstElement *sink = windows->sinks[i];
143 //args are:
144 //(pipeline, tee, sink, int rngseed, int colour, timer flag, int debug flag)
145 /* timer should only run on one of them. colour >= 3 is undefined */
146 int debug = option_debug == i;
147 if (option_reload != NULL){
148 if (option_reload[i] == NULL){
149 g_critical("You can't reload some screens and not others!");
150 exit(1);
152 reload = option_reload[i];
154 if (option_save && option_save[i]){
155 save = option_save[i];
157 post_tee_pipeline(pipeline, tee, sink, i, i + 1, i == 0, debug, save, reload);
159 if (option_avi){
160 /*add a branch saving the video to a file */
161 mjpeg_branch(pipeline, tee);
164 return pipeline;
168 static void
169 bus_call(GstBus * bus, GstMessage *msg, gpointer data)
171 windows_t *windows = (windows_t *)data;
172 if (GST_MESSAGE_TYPE(msg) == GST_MESSAGE_ELEMENT &&
173 gst_structure_has_name(msg->structure, "prepare-xwindow-id")){
174 g_print("Got prepare-xwindow-id msg. option screens: %d\n", option_screens);
175 for (int i = 0; i < option_screens; i++){
176 gst_x_overlay_set_xwindow_id(GST_X_OVERLAY(windows->sinks[i]),
177 windows->xwindows[i]);
178 g_print("connected sink %d to window %lu\n", i, windows->xwindows[i]);
179 hide_mouse(windows->gtk_windows[i]);
184 static void
185 toggle_fullscreen(GtkWidget *widget){
186 GdkWindowState state = gdk_window_get_state(GDK_WINDOW(widget->window));
187 if (state == GDK_WINDOW_STATE_FULLSCREEN){
188 gtk_window_unfullscreen(GTK_WINDOW(widget));
190 else{
191 gtk_window_fullscreen(GTK_WINDOW(widget));
195 static gboolean
196 key_press_event_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
198 g_print("got key %c\n", event->keyval);
199 switch (event->keyval){
200 case 'f':
201 toggle_fullscreen(widget);
202 break;
203 case 'q':
204 g_signal_emit_by_name(widget, "destroy");
205 break;
206 default:
207 break;
209 return TRUE;
212 void destroy_cb(GtkWidget * widget, gpointer data)
214 GMainLoop *loop = (GMainLoop*) data;
215 g_print("Window destroyed\n");
216 g_main_loop_quit(loop);
219 static void
220 video_widget_realize_cb(GtkWidget *widget, gpointer data)
222 windows_t *windows = (windows_t *)data;
223 int r = windows->realised;
224 if (r < MAX_SCREENS){
225 windows->xwindows[r] = GDK_WINDOW_XID(GDK_WINDOW(widget->window));
226 g_print("realised window %d with XID %lu\n", r, windows->xwindows[r]);
228 else {
229 g_print("wtf, there seem to be %d windows!\n", r);
231 windows->realised++;
232 hide_mouse(widget);
236 static void
237 set_up_window(GMainLoop *loop, GtkWidget *window, int screen_no){
238 static const GdkColor black = {0, 0, 0, 0};
239 gtk_window_set_default_size(GTK_WINDOW(window), WIDTH, HEIGHT);
241 if (option_fullscreen){
242 gtk_window_fullscreen(GTK_WINDOW(window));
245 /*if more than one screen is requested, set the screen number.
246 otherwise let it fall were it falls */
247 if (option_screens > 1 || option_first_screen){
248 /* "screen" is not the same thing as "monitor" */
249 GdkScreen * screen = gdk_screen_get_default();
250 int width = gdk_screen_get_width(screen);
251 /* XXX window selection is specific to equally sized windows arranged
252 horizontally. This could be generalised, perhaps using trial and
253 error */
254 gtk_window_move(GTK_WINDOW(window),
255 (width / 2 * screen_no + 200) % width, 50);
258 // attach key press signal to key press callback
259 gtk_widget_set_events(window, GDK_KEY_PRESS_MASK);
260 g_signal_connect(G_OBJECT(window), "key-press-event", G_CALLBACK(key_press_event_cb), NULL);
261 g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(destroy_cb), loop);
263 gtk_widget_modify_bg(window, GTK_STATE_NORMAL, &black);
264 gtk_widget_show_all(window);
265 hide_mouse(window);
269 static GOptionEntry entries[] =
271 { "full-screen", 'f', 0, G_OPTION_ARG_NONE, &option_fullscreen, "run full screen", NULL },
272 { "fps", 'p', 0, G_OPTION_ARG_INT, &option_fps, "speed (Frames per second)", "FPS" },
273 { "screens", 's', 0, G_OPTION_ARG_INT, &option_screens, "Use this many screens", "S" },
274 { "first-screen", 0, 0, G_OPTION_ARG_INT, &option_first_screen, "Start with this screen", "S" },
275 { "debug", 'd', 0, G_OPTION_ARG_INT, &option_debug, "Save screen's debug images in /tmp", "SCREEN" },
276 { "reload", 'r', 0, G_OPTION_ARG_FILENAME_ARRAY, &option_reload,
277 "load calibration data from FILE (one per screen)", "FILE" },
278 { "save", 'S', 0, G_OPTION_ARG_FILENAME_ARRAY, &option_save,
279 "save calibration data to FILE (one per screen)", "FILE" },
280 { "avi", 'a', 0, G_OPTION_ARG_FILENAME, &option_avi,
281 "save mjpeg video to FILE", "FILE" },
282 // { "overlay", 'o', 0, G_OPTION_ARG_NONE, &option_overlay, "Use some kind of overlay", NULL },
283 { NULL, 0, 0, 0, NULL, NULL, NULL }
287 gint main (gint argc, gchar *argv[])
289 //initialise threads before any gtk stuff (because not using gtk_init)
290 g_thread_init(NULL);
291 /*this is more complicated than plain gtk_init/gst_init, so that options from
292 all over can be gathered and presented together.
294 GOptionGroup *gst_opts = gst_init_get_option_group();
295 GOptionGroup *gtk_opts = gtk_get_option_group(TRUE);
296 GOptionContext *ctx = g_option_context_new("...!");
297 g_option_context_add_main_entries(ctx, entries, NULL);
298 g_option_context_add_group(ctx, gst_opts);
299 g_option_context_add_group(ctx, gtk_opts);
300 GError *error = NULL;
301 if (!g_option_context_parse(ctx, &argc, &argv, &error)){
302 g_print ("Error initializing: %s\n", GST_STR_NULL(error->message));
303 exit (1);
305 g_option_context_free(ctx);
307 GMainLoop *loop = g_main_loop_new(NULL, FALSE);
309 windows_t windows;
310 windows.realised = 0;
312 int i;
313 for (i = 0; i < option_screens; i++){
314 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
315 g_signal_connect(window, "realize",
316 G_CALLBACK(video_widget_realize_cb), &windows);
317 /* set up sink here */
318 GstElement *sink = gst_element_factory_make("ximagesink", NULL);
319 set_up_window(loop, window, i + option_first_screen);
320 windows.gtk_windows[i] = window;
321 windows.sinks[i] = sink;
324 GstElement *pipeline = (GstElement *)make_multi_pipeline(&windows, option_screens);
326 GstBus *bus = gst_pipeline_get_bus(GST_PIPELINE(pipeline));
327 gst_bus_add_watch(bus, (GstBusFunc)bus_call, &windows);
328 gst_object_unref(bus);
330 gst_element_set_state(pipeline, GST_STATE_PLAYING);
332 g_main_loop_run(loop);
334 gst_element_set_state (pipeline, GST_STATE_NULL);
335 gst_object_unref (pipeline);
336 return 0;