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. *
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.
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.
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.
29 #include "gstscreenshot.h"
40 /* GST_DEBUG_CATEGORY_EXTERN (_totem_gst_debug_cat); */
41 /* #define GST_CAT_DEFAULT _totem_gst_debug_cat */
43 static void feed_fakesrc(GstElement *src, GstBuffer *buf, GstPad *pad,
46 GstBuffer *in_buf = GST_BUFFER(data);
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));
51 gst_buffer_set_caps(buf, GST_BUFFER_CAPS(in_buf));
53 memcpy(GST_BUFFER_DATA(buf), GST_BUFFER_DATA(in_buf),
54 GST_BUFFER_SIZE(in_buf));
56 GST_BUFFER_SIZE(buf) = GST_BUFFER_SIZE(in_buf);
58 GST_DEBUG("feeding buffer %p, size %u, caps %" GST_PTR_FORMAT,
59 buf, GST_BUFFER_SIZE(buf), GST_BUFFER_CAPS(buf));
61 gst_buffer_unref(in_buf);
64 static void save_result(GstElement *sink, GstBuffer *buf, GstPad *pad,
67 GstScreenshotData *gsd = data;
69 gsd->result = gst_buffer_ref(buf);
71 GST_DEBUG("received converted buffer %p with caps %" GST_PTR_FORMAT,
72 gsd->result, GST_BUFFER_CAPS(gsd->result));
75 static gboolean create_element(const gchar *factory_name, GstElement **element,
78 *element = gst_element_factory_make(factory_name, NULL);
82 if (err && *err == NULL) {
84 GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN,
85 "cannot create element '%s' - please check your "
86 "GStreamer installation", factory_name);
92 static gboolean finalize_process(GstScreenshotData *gsd)
94 g_signal_handlers_disconnect_matched(gsd->sink, (GSignalMatchType)
95 G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
97 g_signal_handlers_disconnect_matched(gsd->src, (GSignalMatchType)
98 G_SIGNAL_MATCH_FUNC, 0, 0, NULL,
100 gst_element_set_state(gsd->pipeline, GST_STATE_NULL);
107 static gboolean async_bus_handler(GstBus *bus, GstMessage *msg,
110 GstScreenshotData *gsd = data;
111 gboolean keep_watch = TRUE;
113 switch (GST_MESSAGE_TYPE(msg)) {
114 case GST_MESSAGE_EOS: {
115 if (gsd->result != NULL) {
116 GST_DEBUG("conversion successful: result = %p",
119 GST_WARNING("EOS but no result frame?!");
121 gsd->cb(gsd->result, gsd->cb_data);
122 keep_watch = finalize_process(gsd);
125 case GST_MESSAGE_ERROR: {
127 GError *error = NULL;
129 gst_message_parse_error(msg, &error, &dbg);
131 g_warning("Could not take screenshot: %s",
133 GST_DEBUG("%s [debug: %s]", error->message,
137 g_warning("Could not take screenshot(and "
142 gsd->cb(gsd->result, gsd->cb_data);
143 keep_watch = finalize_process(gsd);
153 /* takes ownership of the input buffer */
154 gboolean bvw_frame_conv_convert(GstBuffer *buf, GstCaps *to_caps,
155 BvwFrameConvCb cb, gpointer cb_data)
157 static GstElement *src = NULL, *sink = NULL, *pipeline = NULL,
158 *filter1 = NULL, *filter2 = NULL;
160 GError *error = NULL;
161 GstCaps *to_caps_no_par;
162 GstScreenshotData *gsd;
164 g_return_val_if_fail(GST_BUFFER_CAPS(buf) != NULL, FALSE);
165 g_return_val_if_fail(cb != NULL, FALSE);
167 if (pipeline == NULL) {
168 GstElement *csp, *vscale;
170 pipeline = gst_pipeline_new("screenshot-pipeline");
171 if(pipeline == NULL) {
172 g_warning("Could not take screenshot: "
173 "no pipeline (unknown error)");
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",
192 GST_DEBUG("adding elements");
193 gst_bin_add_many(GST_BIN(pipeline), src, csp, filter1, vscale,
194 filter2, sink, NULL);
196 g_object_set(sink, "preroll-queue-len", 1,
197 "signal-handoffs", TRUE, NULL);
199 /* set to 'fixed' sizetype */
200 g_object_set(src, "sizetype", 2, "num-buffers", 1,
201 "signal-handoffs", TRUE, NULL);
203 /* FIXME: linking is still way too expensive, profile
205 GST_DEBUG("linking src->csp");
206 if(!gst_element_link_pads(src, "src", csp, "sink"))
209 GST_DEBUG("linking csp->filter1");
210 if(!gst_element_link_pads(csp, "src", filter1, "sink"))
213 GST_DEBUG("linking filter1->vscale");
214 if(!gst_element_link_pads(filter1, "src", vscale, "sink"))
217 GST_DEBUG("linking vscale->capsfilter");
218 if(!gst_element_link_pads(vscale, "src", filter2, "sink"))
221 GST_DEBUG("linking capsfilter->sink");
222 if(!gst_element_link_pads(filter2, "src", sink, "sink"))
225 bus = gst_element_get_bus(pipeline);
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);
235 g_object_set(filter2, "caps", to_caps, NULL);
236 gst_caps_unref(to_caps);
238 gsd = g_new0(GstScreenshotData, 1);
242 gsd->pipeline = pipeline;
244 gsd->cb_data = cb_data;
246 g_signal_connect(sink, "handoff", G_CALLBACK(save_result), gsd);
248 g_signal_connect(src, "handoff", G_CALLBACK(feed_fakesrc), buf);
250 gst_bus_add_watch(bus, async_bus_handler, gsd);
252 /* set to 'fixed' sizetype */
253 g_object_set(src, "sizemax", GST_BUFFER_SIZE(buf), NULL);
255 GST_DEBUG("running conversion pipeline");
256 gst_element_set_state(pipeline, GST_STATE_PLAYING);