Tizen 2.0 Release
[framework/multimedia/gst-plugins-good0.10.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
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
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   int width;
73   int height;
74 } CairoOverlayState;
75
76 /* Store the information from the caps that we are interested in. */
77 static void
78 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
79 {
80   CairoOverlayState *state = (CairoOverlayState *) user_data;
81
82   gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
83   state->valid = TRUE;
84 }
85
86 /* Draw the overlay. 
87  * This function draws a cute "beating" heart. */
88 static void
89 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
90     guint64 duration, gpointer user_data)
91 {
92   CairoOverlayState *s = (CairoOverlayState *) user_data;
93   double scale;
94
95   if (!s->valid)
96     return;
97
98   scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
99   cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
100   cairo_scale (cr, scale, scale);
101
102   cairo_move_to (cr, 0, 0);
103   cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
104   cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
105   cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
106   cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
107   cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
108   cairo_fill (cr);
109 }
110
111 static GstElement *
112 setup_gst_pipeline (CairoOverlayState * overlay_state)
113 {
114   GstElement *pipeline;
115   GstElement *cairo_overlay;
116   GstElement *source, *adaptor1, *adaptor2, *sink;
117
118   pipeline = gst_pipeline_new ("cairo-overlay-example");
119
120   /* Adaptors needed because cairooverlay only supports ARGB data */
121   source = gst_element_factory_make ("videotestsrc", "source");
122   adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
123   cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
124   adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
125   sink = gst_element_factory_make ("autovideosink", "sink");
126
127   /* If failing, the element could not be created */
128   g_assert (cairo_overlay);
129
130   /* Hook up the neccesary signals for cairooverlay */
131   g_signal_connect (cairo_overlay, "draw",
132       G_CALLBACK (draw_overlay), overlay_state);
133   g_signal_connect (cairo_overlay, "caps-changed",
134       G_CALLBACK (prepare_overlay), overlay_state);
135
136   gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
137       cairo_overlay, adaptor2, sink, NULL);
138
139   if (!gst_element_link_many (source, adaptor1,
140           cairo_overlay, adaptor2, sink, NULL)) {
141     g_warning ("Failed to link elements!");
142   }
143
144   return pipeline;
145 }
146
147 int
148 main (int argc, char **argv)
149 {
150   GMainLoop *loop;
151   GstElement *pipeline;
152   GstBus *bus;
153   CairoOverlayState overlay_state = { FALSE, 0, 0 };
154
155   gst_init (&argc, &argv);
156   loop = g_main_loop_new (NULL, FALSE);
157
158   pipeline = setup_gst_pipeline (&overlay_state);
159
160   bus = gst_pipeline_get_bus (GST_PIPELINE (pipeline));
161   gst_bus_add_signal_watch (bus);
162   g_signal_connect (G_OBJECT (bus), "message", G_CALLBACK (on_message), loop);
163   gst_object_unref (GST_OBJECT (bus));
164
165   gst_element_set_state (pipeline, GST_STATE_PLAYING);
166   g_main_loop_run (loop);
167
168   gst_element_set_state (pipeline, GST_STATE_NULL);
169   gst_object_unref (pipeline);
170
171   return 0;
172 }