cairooverlay: Add generic Cairo overlay video element.
[platform/upstream/gst-plugins-good.git] / tests / examples / cairo / cairo_overlay.c
1 /* GStreamer
2  * Copyright (C) 2011 Jon Nordby <jononor@gmail.com>
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 /*
21  * Example showing usage of the cairooverlay element
22  * 
23  * Note: The example program not run on non-X11 platforms because
24  * it is using the xvimageoverlay element. That part of the code was
25  * roughly based on gst_x_overlay documentation.
26  */
27
28
29 #include <gst/gst.h>
30 #include <gst/video/video.h>
31 #include <gst/interfaces/xoverlay.h>
32
33 #include <gtk/gtk.h>
34 #include <cairo.h>
35 #include <cairo-gobject.h>
36
37 #ifdef GDK_WINDOWING_X11
38 #include <gdk/gdkx.h>
39 #endif
40
41 static gulong video_window_xid = 0;
42
43 static GstBusSyncReply
44 bus_sync_handler (GstBus * bus, GstMessage * message, gpointer user_data)
45 {
46   if (GST_MESSAGE_TYPE (message) != GST_MESSAGE_ELEMENT)
47     return GST_BUS_PASS;
48   if (!gst_structure_has_name (message->structure, "prepare-xwindow-id"))
49     return GST_BUS_PASS;
50
51   if (video_window_xid != 0) {
52     GstXOverlay *xoverlay;
53
54     xoverlay = GST_X_OVERLAY (GST_MESSAGE_SRC (message));
55     gst_x_overlay_set_window_handle (xoverlay, video_window_xid);
56   } else {
57     g_warning ("Should have obtained video_window_xid by now!");
58   }
59
60   gst_message_unref (message);
61   return GST_BUS_DROP;
62 }
63
64 static void
65 video_widget_realize_cb (GtkWidget * widget, gpointer data)
66 {
67 #ifdef GDK_WINDOWING_X11
68   video_window_xid = GDK_WINDOW_XID (widget->window);
69 #endif
70 }
71
72 static GtkWidget *
73 setup_gtk_window (void)
74 {
75   GtkWidget *video_window;
76   GtkWidget *app_window;
77
78   app_window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
79
80   video_window = gtk_drawing_area_new ();
81   g_signal_connect (video_window, "realize",
82       G_CALLBACK (video_widget_realize_cb), NULL);
83   gtk_widget_set_double_buffered (video_window, FALSE);
84
85   gtk_container_add (GTK_CONTAINER (app_window), video_window);
86   gtk_widget_show_all (app_window);
87
88   gtk_widget_realize (app_window);
89   g_assert (video_window_xid != 0);
90
91   return app_window;
92 }
93
94 /* Datastructure to share the state we are interested in between
95  * prepare and render function. */
96 typedef struct
97 {
98   gboolean valid;
99   int width;
100   int height;
101 } CairoOverlayState;
102
103 /* Store the information from the caps that we are interested in. */
104 static void
105 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
106 {
107   CairoOverlayState *state = (CairoOverlayState *) user_data;
108
109   gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
110   state->valid = TRUE;
111 }
112
113 /* Draw the overlay. 
114  * This function draws a cute "beating" heart. */
115 static void
116 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
117     guint64 duration, gpointer user_data)
118 {
119   CairoOverlayState *s = (CairoOverlayState *) user_data;
120   double scale;
121
122   if (!s->valid)
123     return;
124
125   scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
126   cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
127   cairo_scale (cr, scale, scale);
128
129   cairo_move_to (cr, 0, 0);
130   cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
131   cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
132   cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
133   cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
134   cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
135   cairo_fill (cr);
136 }
137
138 static GstElement *
139 setup_gst_pipeline (CairoOverlayState * overlay_state)
140 {
141   GstElement *pipeline;
142   GstElement *cairo_overlay;
143   GstElement *source, *adaptor1, *adaptor2, *sink;
144   GstBus *bus;
145
146   pipeline = gst_pipeline_new ("cairo-overlay-example");
147
148   /* Adaptors needed because cairooverlay only supports ARGB data */
149   source = gst_element_factory_make ("videotestsrc", "source");
150   adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
151   cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
152   adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
153   sink = gst_element_factory_make ("xvimagesink", "sink");
154
155   /* If failing, the element could not be created */
156   g_assert (cairo_overlay);
157
158   /* Hook up the neccesary signals for cairooverlay */
159   g_signal_connect (cairo_overlay, "draw",
160       G_CALLBACK (draw_overlay), overlay_state);
161   g_signal_connect (cairo_overlay, "caps-changed",
162       G_CALLBACK (prepare_overlay), overlay_state);
163
164   gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
165       cairo_overlay, adaptor2, sink, NULL);
166
167   if (!gst_element_link_many (source, adaptor1,
168           cairo_overlay, adaptor2, sink, NULL)) {
169     g_warning ("Failed to link elements!");
170   }
171
172   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
173   gst_bus_set_sync_handler (bus, (GstBusSyncHandler) bus_sync_handler, NULL);
174   gst_object_unref (bus);
175
176   return pipeline;
177 }
178
179 int
180 main (int argc, char **argv)
181 {
182   GtkWidget *window;
183   GstElement *pipeline;
184   CairoOverlayState *overlay_state;
185
186   gtk_init (&argc, &argv);
187   gst_init (&argc, &argv);
188
189   window = setup_gtk_window ();
190   overlay_state = g_new (CairoOverlayState, 1);
191   pipeline = setup_gst_pipeline (overlay_state);
192
193   gst_element_set_state (pipeline, GST_STATE_PLAYING);
194   gtk_main ();
195
196   gst_object_unref (pipeline);
197   gtk_widget_destroy (GTK_WIDGET (window));
198   g_free (overlay_state);
199 }