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