Debian Codegraph @

src/byzanzencodergstreamer.c

raw symbols SCIP
1 /* desktop session recorder
2 * Copyright (C) 2009 Benjamin Otte <[email protected]>
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 3 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, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21
22 #include "byzanzencodergstreamer.h"
23
24 #include <glib/gi18n-lib.h>
25 #include <gst/video/video.h>
26
27 #include "byzanzserialize.h"
28
29 G_DEFINE_TYPE (ByzanzEncoderGStreamer, byzanz_encoder_gstreamer, BYZANZ_TYPE_ENCODER)
30
31 static void
32 byzanz_encoder_gstreamer_need_data (GstAppSrc *src, guint length, gpointer data)
33 {
34 ByzanzEncoder *encoder = data;
35 ByzanzEncoderGStreamer *gst = data;
36 GstBuffer *buffer;
37 cairo_t *cr;
38 cairo_surface_t *surface;
39 cairo_region_t *region;
40 GError *error = NULL;
41 guint64 msecs;
42 int i, num_rects;
43
44 if (!byzanz_deserialize (encoder->input_stream, &msecs, &surface, &region, encoder->cancellable, &error)) {
45 gst_element_message_full (GST_ELEMENT (src), GST_MESSAGE_ERROR,
46 error->domain, error->code, g_strdup (error->message), NULL, __FILE__, GST_FUNCTION, __LINE__);
47 g_error_free (error);
48 return;
49 }
50
51 if (surface == NULL) {
52 gst_app_src_end_of_stream (gst->src);
53 if (gst->audiosrc)
54 gst_element_send_event (gst->audiosrc, gst_event_new_eos ());
55 return;
56 }
57
58 if (cairo_surface_get_reference_count (gst->surface) > 1) {
59 cairo_surface_t *copy = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
60 cairo_image_surface_get_width (gst->surface), cairo_image_surface_get_height (gst->surface));
61
62 cr = cairo_create (copy);
63 cairo_set_source_surface (cr, gst->surface, 0, 0);
64 cairo_paint (cr);
65 cairo_destroy (cr);
66
67 cairo_surface_destroy (gst->surface);
68 gst->surface = copy;
69 }
70 cr = cairo_create (gst->surface);
71 cairo_set_source_surface (cr, surface, 0, 0);
72
73 num_rects = cairo_region_num_rectangles (region);
74 for (i = 0; i < num_rects; i++) {
75 cairo_rectangle_int_t rect;
76 cairo_region_get_rectangle (region, i, &rect);
77 cairo_rectangle (cr, rect.x, rect.y,
78 rect.width, rect.height);
79 }
80
81 cairo_fill (cr);
82 cairo_destroy (cr);
83
84 /* create a buffer and send it */
85 /* FIXME: stride just works? */
86 cairo_surface_reference (gst->surface);
87 buffer = gst_buffer_new_wrapped_full (GST_MEMORY_FLAG_READONLY,
88 cairo_image_surface_get_data (gst->surface),
89 cairo_image_surface_get_stride (gst->surface) * cairo_image_surface_get_height (gst->surface),
90 0,
91 cairo_image_surface_get_stride (gst->surface) * cairo_image_surface_get_height (gst->surface),
92 gst->surface,
93 (GDestroyNotify) cairo_surface_destroy);
94 GST_BUFFER_TIMESTAMP (buffer) = msecs * GST_MSECOND;
95 gst_app_src_push_buffer (gst->src, buffer);
96 }
97
98 static GstAppSrcCallbacks callbacks = {
99 byzanz_encoder_gstreamer_need_data, NULL, NULL
100 };
101
102 static gboolean
103 byzanz_encoder_gstreamer_run (ByzanzEncoder * encoder,
104 GInputStream * input,
105 GOutputStream * output,
106 gboolean record_audio,
107 GCancellable * cancellable,
108 GError ** error)
109 {
110 ByzanzEncoderGStreamer *gstreamer = BYZANZ_ENCODER_GSTREAMER (encoder);
111 ByzanzEncoderGStreamerClass *klass = BYZANZ_ENCODER_GSTREAMER_GET_CLASS (encoder);
112 GstElement *sink;
113 guint width, height;
114 GstMessage *message;
115 GstBus *bus;
116
117 if (!byzanz_deserialize_header (input, &width, &height, cancellable, error))
118 return FALSE;
119
120 gstreamer->surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, width, height);
121
122 g_assert (klass->pipeline_string);
123 if (record_audio) {
124 if (klass->audio_pipeline_string == NULL) {
125 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
126 _("This format does not support recording audio."));
127 return FALSE;
128 }
129 gstreamer->pipeline = gst_parse_launch (klass->audio_pipeline_string, error);
130 gstreamer->audiosrc = gst_bin_get_by_name (GST_BIN (gstreamer->pipeline), "audiosrc");
131 g_assert (gstreamer->audiosrc);
132 } else {
133 gstreamer->pipeline = gst_parse_launch (klass->pipeline_string, error);
134 }
135 if (gstreamer->pipeline == NULL)
136 return FALSE;
137
138 g_assert (GST_IS_PIPELINE (gstreamer->pipeline));
139 gstreamer->src = GST_APP_SRC (gst_bin_get_by_name (GST_BIN (gstreamer->pipeline), "src"));
140 g_assert (GST_IS_APP_SRC (gstreamer->src));
141 sink = gst_bin_get_by_name (GST_BIN (gstreamer->pipeline), "sink");
142 g_assert (sink);
143 g_object_set (sink, "stream", output, NULL);
144 g_object_unref (sink);
145
146 gstreamer->caps = gst_caps_new_simple ("video/x-raw",
147 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
148 "format", G_TYPE_STRING, "BGRx",
149 #elif G_BYTE_ORDER == G_BIG_ENDIAN
150 "format", G_TYPE_STRING, "xRGB",
151 #else
152 #error "Please add the Cairo caps format here"
153 #endif
154 "width", G_TYPE_INT, width,
155 "height", G_TYPE_INT, height,
156 "framerate", GST_TYPE_FRACTION, 0, 1, NULL);
157 g_assert (gst_caps_is_fixed (gstreamer->caps));
158
159 gst_app_src_set_caps (gstreamer->src, gstreamer->caps);
160 gst_app_src_set_callbacks (gstreamer->src, &callbacks, gstreamer, NULL);
161 gst_app_src_set_stream_type (gstreamer->src, GST_APP_STREAM_TYPE_STREAM);
162 gst_app_src_set_max_bytes (gstreamer->src, 0);
163 g_object_set (gstreamer->src,
164 "format", GST_FORMAT_TIME,
165 NULL);
166
167 if (!gst_element_set_state (gstreamer->pipeline, GST_STATE_PLAYING)) {
168 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, _("Failed to start GStreamer pipeline"));
169 return FALSE;
170 }
171
172 bus = gst_pipeline_get_bus (GST_PIPELINE (gstreamer->pipeline));
173 message = gst_bus_timed_pop_filtered (bus, GST_CLOCK_TIME_NONE, GST_MESSAGE_ERROR | GST_MESSAGE_EOS);
174 g_object_unref (bus);
175
176 gst_element_set_state (gstreamer->pipeline, GST_STATE_NULL);
177
178 if (GST_MESSAGE_TYPE (message) == GST_MESSAGE_ERROR) {
179 gst_message_parse_error (message, error, NULL);
180 gst_message_unref (message);
181 return FALSE;
182 }
183 gst_message_unref (message);
184
185 return TRUE;
186 }
187
188 static void
189 byzanz_encoder_gstreamer_finalize (GObject *object)
190 {
191 ByzanzEncoderGStreamer *gstreamer = BYZANZ_ENCODER_GSTREAMER (object);
192
193 if (gstreamer->pipeline) {
194 gst_element_set_state (gstreamer->pipeline, GST_STATE_NULL);
195 g_object_unref (gstreamer->pipeline);
196 }
197 if (gstreamer->src)
198 g_object_unref (gstreamer->src);
199 if (gstreamer->caps)
200 gst_caps_unref (gstreamer->caps);
201
202 if (gstreamer->surface)
203 cairo_surface_destroy (gstreamer->surface);
204
205 G_OBJECT_CLASS (byzanz_encoder_gstreamer_parent_class)->finalize (object);
206 }
207
208 static void
209 byzanz_encoder_gstreamer_class_init (ByzanzEncoderGStreamerClass *klass)
210 {
211 GObjectClass *object_class = G_OBJECT_CLASS (klass);
212 ByzanzEncoderClass *encoder_class = BYZANZ_ENCODER_CLASS (klass);
213
214 gst_init (NULL, NULL);
215
216 object_class->finalize = byzanz_encoder_gstreamer_finalize;
217
218 encoder_class->run = byzanz_encoder_gstreamer_run;
219 }
220
221 static void
222 byzanz_encoder_gstreamer_init (ByzanzEncoderGStreamer *encoder_gstreamer)
223 {
224 }
225