Debian Codegraph @

src/byzanzsession.c

raw symbols SCIP
1 /* desktop session recorder
2 * Copyright (C) 2005,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 "byzanzsession.h"
23
24 #include <string.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <cairo.h>
31 #include <glib/gstdio.h>
32 #include <glib/gi18n.h>
33 #include <gtk/gtk.h>
34 #include <gdk/gdkx.h>
35 #include <X11/extensions/Xdamage.h>
36 #include <X11/extensions/Xfixes.h>
37
38 #include "byzanzencoder.h"
39 #include "byzanzrecorder.h"
40 #include "byzanzserialize.h"
41
42 /*** MAIN FUNCTIONS ***/
43
44 enum {
45 PROP_0,
46 PROP_RECORDING,
47 PROP_ENCODING,
48 PROP_ERROR,
49 PROP_FILE,
50 PROP_AREA,
51 PROP_WINDOW,
52 PROP_AUDIO,
53 PROP_ENCODER_TYPE
54 };
55
56 G_DEFINE_TYPE (ByzanzSession, byzanz_session, G_TYPE_OBJECT)
57
58 static void
59 byzanz_session_get_property (GObject *object, guint param_id, GValue *value,
60 GParamSpec * pspec)
61 {
62 ByzanzSession *session = BYZANZ_SESSION (object);
63
64 switch (param_id) {
65 case PROP_ERROR:
66 g_value_set_pointer (value, session->error);
67 break;
68 case PROP_RECORDING:
69 g_value_set_boolean (value, byzanz_session_is_recording (session));
70 break;
71 case PROP_ENCODING:
72 g_value_set_boolean (value, byzanz_session_is_encoding (session));
73 break;
74 case PROP_FILE:
75 g_value_set_object (value, session->file);
76 break;
77 case PROP_AREA:
78 g_value_set_boxed (value, &session->area);
79 break;
80 case PROP_WINDOW:
81 g_value_set_object (value, session->window);
82 break;
83 case PROP_AUDIO:
84 g_value_set_boolean (value, session->record_audio);
85 break;
86 case PROP_ENCODER_TYPE:
87 g_value_set_gtype (value, session->encoder_type);
88 break;
89 default:
90 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
91 break;
92 }
93 }
94
95 static void
96 byzanz_session_set_property (GObject *object, guint param_id, const GValue *value,
97 GParamSpec * pspec)
98 {
99 ByzanzSession *session = BYZANZ_SESSION (object);
100
101 switch (param_id) {
102 case PROP_FILE:
103 session->file = g_value_dup_object (value);
104 break;
105 case PROP_AREA:
106 session->area = *(cairo_rectangle_int_t *) g_value_get_boxed (value);
107 break;
108 case PROP_WINDOW:
109 session->window = g_value_dup_object (value);
110 break;
111 case PROP_AUDIO:
112 session->record_audio = g_value_get_boolean (value);
113 break;
114 case PROP_ENCODER_TYPE:
115 session->encoder_type = g_value_get_gtype (value);
116 break;
117 default:
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
119 break;
120 }
121 }
122
123 static void
124 byzanz_session_set_error (ByzanzSession *session, const GError *error)
125 {
126 GObject *object = G_OBJECT (session);
127
128 if (session->error != NULL)
129 return;
130
131 session->error = g_error_copy (error);
132 g_object_ref (session);
133 g_object_freeze_notify (object);
134 g_object_notify (object, "error");
135 if (byzanz_recorder_get_recording (session->recorder))
136 byzanz_session_stop (session);
137 g_object_thaw_notify (object);
138 g_object_unref (session);
139 }
140
141 static void
142 byzanz_session_encoder_notify_cb (ByzanzEncoder * encoder,
143 GParamSpec * pspec,
144 ByzanzSession * session)
145 {
146 if (g_str_equal (pspec->name, "running")) {
147 g_object_notify (G_OBJECT (session), "encoding");
148 } else if (g_str_equal (pspec->name, "error")) {
149 const GError *error = byzanz_encoder_get_error (encoder);
150
151 /* Delete the file, it's broken after all. Don't throw errors if it fails though. */
152 g_file_delete (session->file, NULL, NULL);
153
154 /* Cancellation is not an error, it's been requested via _abort() */
155 if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
156 byzanz_session_set_error (session, error);
157 }
158 }
159
160 static void
161 byzanz_session_recorder_notify_cb (ByzanzRecorder * recorder,
162 GParamSpec * pspec,
163 ByzanzSession * session)
164 {
165 g_object_notify (G_OBJECT (session), "recording");
166 }
167
168 static guint64
169 byzanz_session_elapsed (ByzanzSession *session, const GTimeVal *tv)
170 {
171 guint elapsed;
172
173 if (session->start_time.tv_sec == 0 && session->start_time.tv_usec == 0) {
174 session->start_time = *tv;
175 return 0;
176 }
177
178 elapsed = tv->tv_sec - session->start_time.tv_sec;
179 elapsed *= 1000;
180 elapsed += (tv->tv_usec - session->start_time.tv_usec) / 1000;
181
182 return elapsed;
183 }
184
185 static void
186 byzanz_session_recorder_image_cb (ByzanzRecorder * recorder,
187 cairo_surface_t * surface,
188 const cairo_region_t * region,
189 const GTimeVal * tv,
190 ByzanzSession * session)
191 {
192 GOutputStream *stream;
193 GError *error = NULL;
194
195 stream = byzanz_queue_get_output_stream (session->queue);
196 if (!byzanz_serialize (stream, byzanz_session_elapsed (session, tv),
197 surface, region, session->cancellable, &error)) {
198 byzanz_session_set_error (session, error);
199 g_error_free (error);
200 }
201 }
202
203 static void
204 byzanz_session_dispose (GObject *object)
205 {
206 ByzanzSession *session = BYZANZ_SESSION (object);
207
208 byzanz_session_abort (session);
209
210 G_OBJECT_CLASS (byzanz_session_parent_class)->dispose (object);
211 }
212
213 static void
214 byzanz_session_finalize (GObject *object)
215 {
216 ByzanzSession *session = BYZANZ_SESSION (object);
217
218 g_assert (session != NULL);
219
220 g_object_unref (session->recorder);
221 if (session->encoder) {
222 g_signal_handlers_disconnect_by_func (session->encoder, byzanz_session_encoder_notify_cb, session);
223 g_object_unref (session->encoder);
224 }
225 g_object_unref (session->window);
226 g_object_unref (session->file);
227 g_object_unref (session->queue);
228
229 if (session->error)
230 g_error_free (session->error);
231
232 G_OBJECT_CLASS (byzanz_session_parent_class)->finalize (object);
233 }
234
235 static void
236 byzanz_session_constructed (GObject *object)
237 {
238 ByzanzSession *session = BYZANZ_SESSION (object);
239 GOutputStream *stream;
240
241 session->recorder = byzanz_recorder_new (session->window, &session->area);
242 g_signal_connect (session->recorder, "notify::recording",
243 G_CALLBACK (byzanz_session_recorder_notify_cb), session);
244 g_signal_connect (session->recorder, "image",
245 G_CALLBACK (byzanz_session_recorder_image_cb), session);
246
247 /* FIXME: make async */
248 stream = G_OUTPUT_STREAM (g_file_replace (session->file, NULL,
249 FALSE, G_FILE_CREATE_REPLACE_DESTINATION, session->cancellable, &session->error));
250 if (stream != NULL) {
251 session->encoder = byzanz_encoder_new (session->encoder_type,
252 byzanz_queue_get_input_stream (session->queue),
253 stream, session->record_audio, session->cancellable);
254 g_signal_connect (session->encoder, "notify",
255 G_CALLBACK (byzanz_session_encoder_notify_cb), session);
256 g_object_unref (stream);
257 if (byzanz_encoder_get_error (session->encoder))
258 byzanz_session_set_error (session, byzanz_encoder_get_error (session->encoder));
259 }
260 byzanz_serialize_header (byzanz_queue_get_output_stream (session->queue),
261 session->area.width, session->area.height, session->cancellable, &session->error);
262
263 if (G_OBJECT_CLASS (byzanz_session_parent_class)->constructed)
264 G_OBJECT_CLASS (byzanz_session_parent_class)->constructed (object);
265 }
266
267 static void
268 byzanz_session_class_init (ByzanzSessionClass *klass)
269 {
270 GObjectClass *object_class = G_OBJECT_CLASS (klass);
271
272 object_class->get_property = byzanz_session_get_property;
273 object_class->set_property = byzanz_session_set_property;
274 object_class->dispose = byzanz_session_dispose;
275 object_class->finalize = byzanz_session_finalize;
276 object_class->constructed = byzanz_session_constructed;
277
278 g_object_class_install_property (object_class, PROP_ERROR,
279 g_param_spec_pointer ("error", "error", "error that happened on the thread",
280 G_PARAM_READABLE));
281 g_object_class_install_property (object_class, PROP_RECORDING,
282 g_param_spec_boolean ("recording", "recording", "TRUE while the recorder is running",
283 FALSE, G_PARAM_READABLE));
284 g_object_class_install_property (object_class, PROP_ENCODING,
285 g_param_spec_boolean ("encoding", "encoding", "TRUE while the encoder is running",
286 TRUE, G_PARAM_READABLE));
287 g_object_class_install_property (object_class, PROP_WINDOW,
288 g_param_spec_object ("window", "window", "window to record from",
289 GDK_TYPE_WINDOW, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
290 g_object_class_install_property (object_class, PROP_AREA,
291 g_param_spec_boxed ("area", "area", "recorded area",
292 GDK_TYPE_RECTANGLE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
293 g_object_class_install_property (object_class, PROP_FILE,
294 g_param_spec_object ("file", "file", "file to record to",
295 G_TYPE_FILE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
296 g_object_class_install_property (object_class, PROP_AUDIO,
297 g_param_spec_boolean ("record-audio", "record audio", "TRUE to record audio",
298 FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
299 g_object_class_install_property (object_class, PROP_ENCODER_TYPE,
300 g_param_spec_gtype ("encoder-type", "encoder type", "type for the encoder to use",
301 BYZANZ_TYPE_ENCODER, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
302 }
303
304 static void
305 byzanz_session_init (ByzanzSession *session)
306 {
307 session->cancellable = g_cancellable_new ();
308 session->queue = byzanz_queue_new ();
309 }
310
311 /**
312 * byzanz_session_new:
313 * @file: file to record to. Any existing file will be overwritten.
314 * @encoder_type: the type of encoder to use
315 * @window: window to record
316 * @area: area of window that should be recorded
317 * @record_cursor: if the cursor image should be recorded
318 * @record_audio: if audio should be recorded
319 *
320 * Creates a new #ByzanzSession and initializes all basic variables.
321 * gtk_init() and g_thread_init() must have been called before.
322 *
323 * Returns: a new #ByzanzSession or NULL if an error occured. Most likely
324 * the XDamage extension is not available on the current X server
325 * then. Another reason would be a thread creation failure.
326 **/
327 ByzanzSession *
328 byzanz_session_new (GFile *file, GType encoder_type,
329 GdkWindow *window, const cairo_rectangle_int_t *area, gboolean record_cursor,
330 gboolean record_audio)
331 {
332 g_return_val_if_fail (G_IS_FILE (file), NULL);
333 g_return_val_if_fail (g_type_is_a (encoder_type, BYZANZ_TYPE_ENCODER), NULL);
334 g_return_val_if_fail (GDK_IS_WINDOW (window), NULL);
335 g_return_val_if_fail (area != NULL, NULL);
336 g_return_val_if_fail (area->x >= 0, NULL);
337 g_return_val_if_fail (area->y >= 0, NULL);
338 g_return_val_if_fail (area->width > 0, NULL);
339 g_return_val_if_fail (area->height > 0, NULL);
340
341 /* FIXME: handle mouse cursor */
342
343 return g_object_new (BYZANZ_TYPE_SESSION, "file", file, "encoder-type", encoder_type,
344 "window", window, "area", area, "record-audio", record_audio, NULL);
345 }
346
347 void
348 byzanz_session_start (ByzanzSession *session)
349 {
350 g_return_if_fail (BYZANZ_IS_SESSION (session));
351
352 byzanz_recorder_set_recording (session->recorder, TRUE);
353 }
354
355 void
356 byzanz_session_stop (ByzanzSession *session)
357 {
358 GOutputStream *stream;
359 GError *error = NULL;
360 GTimeVal tv;
361
362 g_return_if_fail (BYZANZ_IS_SESSION (session));
363
364 stream = byzanz_queue_get_output_stream (session->queue);
365 g_get_current_time (&tv);
366 if (!byzanz_serialize (stream, byzanz_session_elapsed (session, &tv),
367 NULL, NULL, session->cancellable, &error) ||
368 !g_output_stream_close (stream, session->cancellable, &error)) {
369 byzanz_session_set_error (session, error);
370 g_error_free (error);
371 }
372
373 byzanz_recorder_set_recording (session->recorder, FALSE);
374 }
375
376 void
377 byzanz_session_abort (ByzanzSession *session)
378 {
379 g_return_if_fail (BYZANZ_IS_SESSION (session));
380
381 g_cancellable_cancel (session->cancellable);
382 }
383
384 gboolean
385 byzanz_session_is_recording (ByzanzSession *session)
386 {
387 g_return_val_if_fail (BYZANZ_IS_SESSION (session), FALSE);
388
389 return session->error == NULL &&
390 byzanz_recorder_get_recording (session->recorder);
391 }
392
393 gboolean
394 byzanz_session_is_encoding (ByzanzSession *session)
395 {
396 g_return_val_if_fail (BYZANZ_IS_SESSION (session), FALSE);
397
398 return session->error == NULL && byzanz_encoder_is_running (session->encoder);
399 }
400
401 const GError *
402 byzanz_session_get_error (ByzanzSession *session)
403 {
404 g_return_val_if_fail (BYZANZ_IS_SESSION (session), NULL);
405
406 return session->error;
407 }
408