Fixed cleanup of qmafw-gst-subtitles-renderer package
[mafwsubrenderer] / mafw-gst-subtitles-renderer / libmafw-gst-renderer / gstscreenshot.c
1 /* Small helper element for format conversion
2  * (c) 2004 Ronald Bultje <rbultje@ronald.bitfreak.net>
3  * Portion Copyright © 2009 Nokia Corporation and/or its
4  * subsidiary(-ies).* All rights reserved. *
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include <gst/gst.h>
27 #include <string.h>
28
29 #include "gstscreenshot.h"
30
31 typedef struct {
32         GstBuffer *result;
33         GstElement *src;
34         GstElement *sink;
35         GstElement *pipeline;
36         BvwFrameConvCb cb;
37         gpointer cb_data;
38 } GstScreenshotData;
39
40 /* GST_DEBUG_CATEGORY_EXTERN (_totem_gst_debug_cat); */
41 /* #define GST_CAT_DEFAULT _totem_gst_debug_cat */
42
43 static void feed_fakesrc(GstElement *src, GstBuffer *buf, GstPad *pad,
44                          gpointer data)
45 {
46         GstBuffer *in_buf = GST_BUFFER(data);
47
48         g_assert(GST_BUFFER_SIZE(buf) >= GST_BUFFER_SIZE(in_buf));
49         g_assert(!GST_BUFFER_FLAG_IS_SET(buf, GST_BUFFER_FLAG_READONLY));
50
51         gst_buffer_set_caps(buf, GST_BUFFER_CAPS(in_buf));
52
53         memcpy(GST_BUFFER_DATA(buf), GST_BUFFER_DATA(in_buf),
54                GST_BUFFER_SIZE(in_buf));
55
56         GST_BUFFER_SIZE(buf) = GST_BUFFER_SIZE(in_buf);
57
58         GST_DEBUG("feeding buffer %p, size %u, caps %" GST_PTR_FORMAT,
59                   buf, GST_BUFFER_SIZE(buf), GST_BUFFER_CAPS(buf));
60
61         gst_buffer_unref(in_buf);
62 }
63
64 static void save_result(GstElement *sink, GstBuffer *buf, GstPad *pad,
65                         gpointer data)
66 {
67         GstScreenshotData *gsd = data;
68
69         gsd->result = gst_buffer_ref(buf);
70
71         GST_DEBUG("received converted buffer %p with caps %" GST_PTR_FORMAT,
72                   gsd->result, GST_BUFFER_CAPS(gsd->result));
73 }
74
75 static gboolean create_element(const gchar *factory_name, GstElement **element,
76                                GError **err)
77 {
78         *element = gst_element_factory_make(factory_name, NULL);
79         if (*element)
80                 return TRUE;
81
82         if (err && *err == NULL) {
83                 *err = g_error_new(
84                         GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
85                         "cannot create element '%s' - please check your "
86                         "GStreamer installation", factory_name);
87         }
88
89         return FALSE;
90 }
91
92 static gboolean finalize_process(GstScreenshotData *gsd)
93 {
94         g_signal_handlers_disconnect_matched(gsd->sink, (GSignalMatchType)
95                                              G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
96                                              save_result, NULL);
97         g_signal_handlers_disconnect_matched(gsd->src, (GSignalMatchType)
98                                              G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
99                                              feed_fakesrc, NULL);
100         gst_element_set_state(gsd->pipeline, GST_STATE_NULL);
101
102         g_free(gsd);
103
104         return FALSE;
105 }
106
107 static gboolean async_bus_handler(GstBus *bus, GstMessage *msg,
108                                   gpointer data)
109 {
110         GstScreenshotData *gsd = data;
111         gboolean keep_watch = TRUE;
112
113         switch (GST_MESSAGE_TYPE(msg)) {
114         case GST_MESSAGE_EOS: {
115                 if (gsd->result != NULL) {
116                         GST_DEBUG("conversion successful: result = %p",
117                                   gsd->result);
118                 } else {
119                         GST_WARNING("EOS but no result frame?!");
120                 }
121                 gsd->cb(gsd->result, gsd->cb_data);
122                 keep_watch = finalize_process(gsd);
123                 break;
124         }
125         case GST_MESSAGE_ERROR: {
126                 gchar *dbg = NULL;
127                 GError *error = NULL;
128
129                 gst_message_parse_error(msg, &error, &dbg);
130                 if (error != NULL) {
131                         g_warning("Could not take screenshot: %s",
132                                   error->message);
133                         GST_DEBUG("%s [debug: %s]", error->message,
134                                   GST_STR_NULL(dbg));
135                         g_error_free(error);
136                 } else {
137                         g_warning("Could not take screenshot(and "
138                                   "NULL error!)");
139                 }
140                 g_free(dbg);
141                 gsd->result = NULL;
142                 gsd->cb(gsd->result, gsd->cb_data);
143                 keep_watch = finalize_process(gsd);
144                 break;
145         }
146         default:
147                 break;
148         }
149
150         return keep_watch;
151 }
152
153 /* takes ownership of the input buffer */
154 gboolean bvw_frame_conv_convert(GstBuffer *buf, GstCaps *to_caps,
155                                 BvwFrameConvCb cb, gpointer cb_data)
156 {
157         static GstElement *src = NULL, *sink = NULL, *pipeline = NULL,
158                 *filter1 = NULL, *filter2 = NULL;
159         static GstBus *bus;
160         GError *error = NULL;
161         GstCaps *to_caps_no_par;
162         GstScreenshotData *gsd;
163
164         g_return_val_if_fail(GST_BUFFER_CAPS(buf) != NULL, FALSE);
165         g_return_val_if_fail(cb != NULL, FALSE);
166
167         if (pipeline == NULL) {
168                 GstElement *csp, *vscale;
169
170                 pipeline = gst_pipeline_new("screenshot-pipeline");
171                 if(pipeline == NULL) {
172                         g_warning("Could not take screenshot: "
173                                   "no pipeline (unknown error)");
174                         return FALSE;
175                 }
176
177                 /* videoscale is here to correct for the
178                  * pixel-aspect-ratio for us */
179                 GST_DEBUG("creating elements");
180                 if(!create_element("fakesrc", &src, &error) ||
181                    !create_element("ffmpegcolorspace", &csp, &error) ||
182                    !create_element("videoscale", &vscale, &error) ||
183                    !create_element("capsfilter", &filter1, &error) ||
184                    !create_element("capsfilter", &filter2, &error) ||
185                    !create_element("fakesink", &sink, &error)) {
186                         g_warning("Could not take screenshot: %s",
187                                   error->message);
188                         g_error_free(error);
189                         return FALSE;
190                 }
191
192                 GST_DEBUG("adding elements");
193                 gst_bin_add_many(GST_BIN(pipeline), src, csp, filter1, vscale,
194                                  filter2, sink, NULL);
195
196                 g_object_set(sink, "preroll-queue-len", 1,
197                              "signal-handoffs", TRUE, NULL);
198
199                 /* set to 'fixed' sizetype */
200                 g_object_set(src, "sizetype", 2, "num-buffers", 1,
201                              "signal-handoffs", TRUE, NULL);
202
203                 /* FIXME: linking is still way too expensive, profile
204                  * this properly */
205                 GST_DEBUG("linking src->csp");
206                 if(!gst_element_link_pads(src, "src", csp, "sink"))
207                         return FALSE;
208
209                 GST_DEBUG("linking csp->filter1");
210                 if(!gst_element_link_pads(csp, "src", filter1, "sink"))
211                         return FALSE;
212
213                 GST_DEBUG("linking filter1->vscale");
214                 if(!gst_element_link_pads(filter1, "src", vscale, "sink"))
215                         return FALSE;
216
217                 GST_DEBUG("linking vscale->capsfilter");
218                 if(!gst_element_link_pads(vscale, "src", filter2, "sink"))
219                         return FALSE;
220
221                 GST_DEBUG("linking capsfilter->sink");
222                 if(!gst_element_link_pads(filter2, "src", sink, "sink"))
223                         return FALSE;
224
225                 bus = gst_element_get_bus(pipeline);
226         }
227
228         /* adding this superfluous capsfilter makes linking cheaper */
229         to_caps_no_par = gst_caps_copy(to_caps);
230         gst_structure_remove_field(gst_caps_get_structure(to_caps_no_par, 0),
231                                    "pixel-aspect-ratio");
232         g_object_set(filter1, "caps", to_caps_no_par, NULL);
233         gst_caps_unref(to_caps_no_par);
234
235         g_object_set(filter2, "caps", to_caps, NULL);
236         gst_caps_unref(to_caps);
237
238         gsd = g_new0(GstScreenshotData, 1);
239
240         gsd->src = src;
241         gsd->sink = sink;
242         gsd->pipeline = pipeline;
243         gsd->cb = cb;
244         gsd->cb_data = cb_data;
245
246         g_signal_connect(sink, "handoff", G_CALLBACK(save_result), gsd);
247
248         g_signal_connect(src, "handoff", G_CALLBACK(feed_fakesrc), buf);
249
250         gst_bus_add_watch(bus, async_bus_handler, gsd);
251
252         /* set to 'fixed' sizetype */
253         g_object_set(src, "sizemax", GST_BUFFER_SIZE(buf), NULL);
254
255         GST_DEBUG("running conversion pipeline");
256         gst_element_set_state(pipeline, GST_STATE_PLAYING);
257
258         return TRUE;
259 }