Debian Codegraph @

src/byzanzserialize.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 "byzanzserialize.h"
23
24 #include <string.h>
25 #include <glib/gi18n.h>
26
27 #define IDENTIFICATION "ByzanzRecording"
28
29 static guchar
30 byte_order_to_uchar (void)
31 {
32 switch (G_BYTE_ORDER) {
33 case G_BIG_ENDIAN:
34 return 'B';
35 case G_LITTLE_ENDIAN:
36 return 'L';
37 default:
38 g_assert_not_reached ();
39 return 0;
40 }
41 }
42
43 gboolean
44 byzanz_serialize_header (GOutputStream * stream,
45 guint width,
46 guint height,
47 GCancellable * cancellable,
48 GError ** error)
49 {
50 guint32 w, h;
51 guchar endian;
52
53 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
54 g_return_val_if_fail (width <= G_MAXUINT32, FALSE);
55 g_return_val_if_fail (height <= G_MAXUINT32, FALSE);
56
57 w = width;
58 h = height;
59 endian = byte_order_to_uchar ();
60
61 return g_output_stream_write_all (stream, IDENTIFICATION, strlen (IDENTIFICATION), NULL, cancellable, error) &&
62 g_output_stream_write_all (stream, &endian, sizeof (guchar), NULL, cancellable, error) &&
63 g_output_stream_write_all (stream, &w, sizeof (guint32), NULL, cancellable, error) &&
64 g_output_stream_write_all (stream, &h, sizeof (guint32), NULL, cancellable, error);
65 }
66
67 gboolean
68 byzanz_deserialize_header (GInputStream * stream,
69 guint * width,
70 guint * height,
71 GCancellable * cancellable,
72 GError ** error)
73 {
74 char result[strlen (IDENTIFICATION) + 1];
75 guint32 size[2];
76 guchar endian;
77
78 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
79 g_return_val_if_fail (width != NULL, FALSE);
80 g_return_val_if_fail (height != NULL, FALSE);
81
82 if (!g_input_stream_read_all (stream, result, sizeof (result), NULL, cancellable, error))
83 return FALSE;
84
85 if (strncmp (result, IDENTIFICATION, strlen (IDENTIFICATION)) != 0) {
86 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
87 _("Not a Byzanz recording"));
88 return FALSE;
89 }
90 endian = result[strlen (IDENTIFICATION)];
91 if (endian != byte_order_to_uchar ()) {
92 g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
93 _("Unsupported byte order"));
94 return FALSE;
95 }
96
97 if (!g_input_stream_read_all (stream, &size, sizeof (size), NULL, cancellable, error))
98 return FALSE;
99
100 *width = size[0];
101 *height = size[1];
102
103 return TRUE;
104 }
105
106 gboolean
107 byzanz_serialize (GOutputStream * stream,
108 guint64 msecs,
109 cairo_surface_t * surface,
110 const cairo_region_t * region,
111 GCancellable * cancellable,
112 GError ** error)
113 {
114 guint i, stride;
115 cairo_rectangle_int_t rect, extents;
116 guchar *data;
117 guint32 n;
118 int y, n_rects;
119
120 g_return_val_if_fail (G_IS_OUTPUT_STREAM (stream), FALSE);
121 g_return_val_if_fail ((surface == NULL) == (region == NULL), FALSE);
122 g_return_val_if_fail (region == NULL || !cairo_region_is_empty (region), FALSE);
123
124 if (!g_output_stream_write_all (stream, &msecs, sizeof (guint64), NULL, cancellable, error))
125 return FALSE;
126
127 if (surface == 0) {
128 n = 0;
129 return g_output_stream_write_all (stream, &n, sizeof (guint32), NULL, cancellable, error);
130 }
131
132 n = n_rects = cairo_region_num_rectangles (region);
133 if (!g_output_stream_write_all (stream, &n, sizeof (guint32), NULL, cancellable, error))
134 return FALSE;
135 for (i = 0; i < n; i++) {
136 gint32 ints[4];
137 cairo_region_get_rectangle (region, i, &rect);
138 ints[0] = rect.x, ints[1] = rect.y, ints[2] = rect.width, ints[3] = rect.height;
139
140 g_assert (sizeof (ints) == 16);
141 if (!g_output_stream_write_all (stream, ints, sizeof (ints), NULL, cancellable, error))
142 return FALSE;
143 }
144
145 stride = cairo_image_surface_get_stride (surface);
146 cairo_region_get_extents (region, &extents);
147 for (i = 0; i < n; i++) {
148 cairo_region_get_rectangle (region, i, &rect);
149 data = cairo_image_surface_get_data (surface)
150 + stride * (rect.y - extents.y)
151 + sizeof (guint32) * (rect.x - extents.x);
152 for (y = 0; y < rect.height; y++) {
153 if (!g_output_stream_write_all (G_OUTPUT_STREAM (stream), data,
154 rect.width * sizeof (guint32), NULL, cancellable, error))
155 return FALSE;
156 data += stride;
157 }
158 }
159
160 return TRUE;
161 }
162
163 gboolean
164 byzanz_deserialize (GInputStream * stream,
165 guint64 * msecs_out,
166 cairo_surface_t ** surface_out,
167 cairo_region_t ** region_out,
168 GCancellable * cancellable,
169 GError ** error)
170 {
171 guint i, stride;
172 cairo_rectangle_int_t extents, *rects;
173 cairo_region_t *region;
174 cairo_surface_t *surface;
175 guchar *data;
176 guint32 n;
177 int y;
178
179 g_return_val_if_fail (G_IS_INPUT_STREAM (stream), FALSE);
180 g_return_val_if_fail (msecs_out != NULL, FALSE);
181 g_return_val_if_fail (surface_out != NULL, FALSE);
182 g_return_val_if_fail (region_out != NULL, FALSE);
183
184 if (!g_input_stream_read_all (stream, msecs_out, sizeof (guint64), NULL, cancellable, error) ||
185 !g_input_stream_read_all (stream, &n, sizeof (guint32), NULL, cancellable, error))
186 return FALSE;
187
188 if (n == 0) {
189 /* end of stream */
190 *surface_out = NULL;
191 *region_out = NULL;
192 return TRUE;
193 }
194
195 region = cairo_region_create ();
196 rects = g_new (cairo_rectangle_int_t, n);
197 surface = NULL;
198 for (i = 0; i < n; i++) {
199 gint ints[4];
200 if (!g_input_stream_read_all (stream, ints, sizeof (ints), NULL, cancellable, error))
201 goto fail;
202
203 rects[i].x = ints[0];
204 rects[i].y = ints[1];
205 rects[i].width = ints[2];
206 rects[i].height = ints[3];
207 cairo_region_union_rectangle (region, &rects[i]);
208 }
209
210 cairo_region_get_extents (region, &extents);
211 surface = cairo_image_surface_create (CAIRO_FORMAT_RGB24, extents.width, extents.height);
212 cairo_surface_set_device_offset (surface, -extents.x, -extents.y);
213 stride = cairo_image_surface_get_stride (surface);
214 for (i = 0; i < n; i++) {
215 data = cairo_image_surface_get_data (surface)
216 + stride * (rects[i].y - extents.y)
217 + sizeof (guint32) * (rects[i].x - extents.x);
218 for (y = 0; y < rects[i].height; y++) {
219 if (!g_input_stream_read_all (stream, data,
220 rects[i].width * sizeof (guint32), NULL, cancellable, error))
221 goto fail;
222 data += stride;
223 }
224 }
225
226 g_free (rects);
227 *region_out = region;
228 *surface_out = surface;
229 return TRUE;
230
231 fail:
232 if (surface)
233 cairo_surface_destroy (surface);
234 cairo_region_destroy (region);
235 g_free (rects);
236 return FALSE;
237 }
238