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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 */
19
20 /*
21 * Example showing usage of the cairooverlay element
22 */
23
24 #include <gst/gst.h>
25 #include <gst/video/video.h>
26
27 #include <cairo.h>
28 #include <cairo-gobject.h>
29
30 #include <glib.h>
31
32
33 static gboolean
on_message(GstBus * bus,GstMessage * message,gpointer user_data)34 on_message (GstBus * bus, GstMessage * message, gpointer user_data)
35 {
36 GMainLoop *loop = (GMainLoop *) user_data;
37
38 switch (GST_MESSAGE_TYPE (message)) {
39 case GST_MESSAGE_ERROR:{
40 GError *err = NULL;
41 gchar *debug;
42
43 gst_message_parse_error (message, &err, &debug);
44 g_critical ("Got ERROR: %s (%s)", err->message, GST_STR_NULL (debug));
45 g_main_loop_quit (loop);
46 break;
47 }
48 case GST_MESSAGE_WARNING:{
49 GError *err = NULL;
50 gchar *debug;
51
52 gst_message_parse_warning (message, &err, &debug);
53 g_warning ("Got WARNING: %s (%s)", err->message, GST_STR_NULL (debug));
54 g_main_loop_quit (loop);
55 break;
56 }
57 case GST_MESSAGE_EOS:
58 g_main_loop_quit (loop);
59 break;
60 default:
61 break;
62 }
63
64 return TRUE;
65 }
66
67 /* Datastructure to share the state we are interested in between
68 * prepare and render function. */
69 typedef struct
70 {
71 gboolean valid;
72 GstVideoInfo vinfo;
73 } CairoOverlayState;
74
75 /* Store the information from the caps that we are interested in. */
76 static void
prepare_overlay(GstElement * overlay,GstCaps * caps,gpointer user_data)77 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
78 {
79 CairoOverlayState *state = (CairoOverlayState *) user_data;
80
81 state->valid = gst_video_info_from_caps (&state->vinfo, caps);
82 }
83
84 /* Draw the overlay.
85 * This function draws a cute "beating" heart. */
86 static void
draw_overlay(GstElement * overlay,cairo_t * cr,guint64 timestamp,guint64 duration,gpointer user_data)87 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
88 guint64 duration, gpointer user_data)
89 {
90 CairoOverlayState *s = (CairoOverlayState *) user_data;
91 double scale;
92 int width, height;
93
94 if (!s->valid)
95 return;
96
97 width = GST_VIDEO_INFO_WIDTH (&s->vinfo);
98 height = GST_VIDEO_INFO_HEIGHT (&s->vinfo);
99
100 scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
101 cairo_translate (cr, width / 2, (height / 2) - 30);
102
103 /* FIXME: this assumes a pixel-aspect-ratio of 1/1 */
104 cairo_scale (cr, scale, scale);
105
106 cairo_move_to (cr, 0, 0);
107 cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
108 cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
109 cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
110 cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
111 cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
112 cairo_fill (cr);
113 }
114
115 static GstElement *
setup_gst_pipeline(CairoOverlayState * overlay_state)116 setup_gst_pipeline (CairoOverlayState * overlay_state)
117 {
118 GstElement *pipeline;
119 GstElement *cairo_overlay;
120 GstElement *source, *adaptor1, *adaptor2, *sink;
121
122 pipeline = gst_pipeline_new ("cairo-overlay-example");
123
124 /* Adaptors needed because cairooverlay only supports ARGB data */
125 source = gst_element_factory_make ("videotestsrc", "source");
126 adaptor1 = gst_element_factory_make ("videoconvert", "adaptor1");
127 cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
128 adaptor2 = gst_element_factory_make ("videoconvert", "adaptor2");
129 sink = gst_element_factory_make ("ximagesink", "sink");
130 if (sink == NULL)
131 sink = gst_element_factory_make ("autovideosink", "sink");
132
133 /* If failing, the element could not be created */
134 g_assert (cairo_overlay);
135
136 /* Hook up the necessary signals for cairooverlay */
137 g_signal_connect (cairo_overlay, "draw",
138 G_CALLBACK (draw_overlay), overlay_state);
139 g_signal_connect (cairo_overlay, "caps-changed",
140 G_CALLBACK (prepare_overlay), overlay_state);
141
142 gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
143 cairo_overlay, adaptor2, sink, NULL);
144
145 if (!gst_element_link_many (source, adaptor1,
146 cairo_overlay, adaptor2, sink, NULL)) {
147 g_warning ("Failed to link elements!");
148 }
149
150 return pipeline;
151 }
152
153 int
main(int argc,char ** argv)154 main (int argc, char **argv)
155 {
156 GMainLoop *loop;
157 GstElement *pipeline;
158 GstBus *bus;
159 CairoOverlayState *overlay_state;
160
161 gst_init (&argc, &argv);
162 loop = g_main_loop_new (NULL, FALSE);
163
164 /* allocate on heap for pedagogical reasons, makes code easier to transfer */
165 overlay_state = g_new0 (CairoOverlayState, 1);
166
167 pipeline = setup_gst_pipeline (overlay_state);
168
169 bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
170 gst_bus_add_signal_watch (bus);
171 g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
172 gst_object_unref (GST_OBJECT (bus));
173
174 gst_element_set_state (pipeline, GST_STATE_PLAYING);
175 g_main_loop_run (loop);
176
177 gst_element_set_state (pipeline, GST_STATE_NULL);
178 gst_object_unref (pipeline);
179
180 g_free (overlay_state);
181 return 0;
182 }
183