cairooverlay: Remove unnecessary gtk/gtk-x11 use in example.
[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
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 /* Datastructure to share the state we are interested in between
33  * prepare and render function. */
34 typedef struct
35 {
36   gboolean valid;
37   int width;
38   int height;
39 } CairoOverlayState;
40
41 /* Store the information from the caps that we are interested in. */
42 static void
43 prepare_overlay (GstElement * overlay, GstCaps * caps, gpointer user_data)
44 {
45   CairoOverlayState *state = (CairoOverlayState *) user_data;
46
47   gst_video_format_parse_caps (caps, NULL, &state->width, &state->height);
48   state->valid = TRUE;
49 }
50
51 /* Draw the overlay. 
52  * This function draws a cute "beating" heart. */
53 static void
54 draw_overlay (GstElement * overlay, cairo_t * cr, guint64 timestamp,
55     guint64 duration, gpointer user_data)
56 {
57   CairoOverlayState *s = (CairoOverlayState *) user_data;
58   double scale;
59
60   if (!s->valid)
61     return;
62
63   scale = 2 * (((timestamp / (int) 1e7) % 70) + 30) / 100.0;
64   cairo_translate (cr, s->width / 2, (s->height / 2) - 30);
65   cairo_scale (cr, scale, scale);
66
67   cairo_move_to (cr, 0, 0);
68   cairo_curve_to (cr, 0, -30, -50, -30, -50, 0);
69   cairo_curve_to (cr, -50, 30, 0, 35, 0, 60);
70   cairo_curve_to (cr, 0, 35, 50, 30, 50, 0);
71   cairo_curve_to (cr, 50, -30, 0, -30, 0, 0);
72   cairo_set_source_rgba (cr, 0.9, 0.0, 0.1, 0.7);
73   cairo_fill (cr);
74 }
75
76 static GstElement *
77 setup_gst_pipeline (CairoOverlayState * overlay_state)
78 {
79   GstElement *pipeline;
80   GstElement *cairo_overlay;
81   GstElement *source, *adaptor1, *adaptor2, *sink;
82
83   pipeline = gst_pipeline_new ("cairo-overlay-example");
84
85   /* Adaptors needed because cairooverlay only supports ARGB data */
86   source = gst_element_factory_make ("videotestsrc", "source");
87   adaptor1 = gst_element_factory_make ("ffmpegcolorspace", "adaptor1");
88   cairo_overlay = gst_element_factory_make ("cairooverlay", "overlay");
89   adaptor2 = gst_element_factory_make ("ffmpegcolorspace", "adaptor2");
90   sink = gst_element_factory_make ("autovideosink", "sink");
91
92   /* If failing, the element could not be created */
93   g_assert (cairo_overlay);
94
95   /* Hook up the neccesary signals for cairooverlay */
96   g_signal_connect (cairo_overlay, "draw",
97       G_CALLBACK (draw_overlay), overlay_state);
98   g_signal_connect (cairo_overlay, "caps-changed",
99       G_CALLBACK (prepare_overlay), overlay_state);
100
101   gst_bin_add_many (GST_BIN (pipeline), source, adaptor1,
102       cairo_overlay, adaptor2, sink, NULL);
103
104   if (!gst_element_link_many (source, adaptor1,
105           cairo_overlay, adaptor2, sink, NULL)) {
106     g_warning ("Failed to link elements!");
107   }
108
109   return pipeline;
110 }
111
112 int
113 main (int argc, char **argv)
114 {
115   GMainLoop *loop;
116   GstElement *pipeline;
117   CairoOverlayState *overlay_state;
118
119   gst_init (&argc, &argv);
120   loop = g_main_loop_new (NULL, FALSE);
121
122   overlay_state = g_new0 (CairoOverlayState, 1);
123   pipeline = setup_gst_pipeline (overlay_state);
124
125   gst_element_set_state (pipeline, GST_STATE_PLAYING);
126   g_main_loop_run (loop);
127
128   gst_object_unref (pipeline);
129   g_free (overlay_state);
130 }