| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
#ifdef HAVE_CONFIG_H |
| 19 |
#include "config.h" |
| 20 |
#endif |
| 21 |
|
| 22 |
#include "byzanzencoder.h" |
| 23 |
|
| 24 |
#include <glib/gi18n-lib.h> |
| 25 |
|
| 26 |
#include "byzanzserialize.h" |
| 27 |
|
| 28 |
typedef struct _ByzanzEncoderJob ByzanzEncoderJob; |
| 29 |
struct _ByzanzEncoderJob { |
| 30 |
GTimeVal tv; |
| 31 |
cairo_surface_t * surface; |
| 32 |
cairo_region_t * region; |
| 33 |
}; |
| 34 |
|
| 35 |
static void |
| 36 |
byzanz_encoder_job_free (ByzanzEncoderJob *job) |
| 37 |
{ |
| 38 |
if (job->surface) |
| 39 |
cairo_surface_destroy (job->surface); |
| 40 |
if (job->region) |
| 41 |
cairo_region_destroy (job->region); |
| 42 |
|
| 43 |
g_slice_free (ByzanzEncoderJob, job); |
| 44 |
} |
| 45 |
|
| 46 |
static gboolean |
| 47 |
byzanz_encoder_finished (gpointer data) |
| 48 |
{ |
| 49 |
ByzanzEncoder *encoder = data; |
| 50 |
ByzanzEncoderJob *job; |
| 51 |
|
| 52 |
encoder->error = g_thread_join (encoder->thread); |
| 53 |
encoder->thread = NULL; |
| 54 |
|
| 55 |
while ((job = g_async_queue_try_pop (encoder->jobs))) |
| 56 |
byzanz_encoder_job_free (job); |
| 57 |
|
| 58 |
g_object_freeze_notify (G_OBJECT (encoder)); |
| 59 |
g_object_notify (G_OBJECT (encoder), "running"); |
| 60 |
if (encoder->error) |
| 61 |
g_object_notify (G_OBJECT (encoder), "error"); |
| 62 |
g_object_thaw_notify (G_OBJECT (encoder)); |
| 63 |
g_object_unref (encoder); |
| 64 |
|
| 65 |
return FALSE; |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
static gboolean |
| 71 |
byzanz_encoder_run (ByzanzEncoder * encoder, |
| 72 |
GInputStream * input, |
| 73 |
GOutputStream * output, |
| 74 |
gboolean record_audio, |
| 75 |
GCancellable * cancellable, |
| 76 |
GError ** error) |
| 77 |
{ |
| 78 |
ByzanzEncoderClass *klass = BYZANZ_ENCODER_GET_CLASS (encoder); |
| 79 |
guint width, height; |
| 80 |
cairo_surface_t *surface; |
| 81 |
cairo_region_t *region; |
| 82 |
guint64 msecs; |
| 83 |
gboolean success; |
| 84 |
|
| 85 |
if (record_audio) { |
| 86 |
g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED, |
| 87 |
_("This format does not support recording audio.")); |
| 88 |
return FALSE; |
| 89 |
} |
| 90 |
|
| 91 |
if (!byzanz_deserialize_header (input, &width, &height, cancellable, error) || |
| 92 |
!klass->setup (encoder, output, width, height, cancellable, error)) |
| 93 |
return FALSE; |
| 94 |
|
| 95 |
for (;;) { |
| 96 |
if (!byzanz_deserialize (input , &msecs, &surface, ®ion, cancellable, error)) |
| 97 |
return FALSE; |
| 98 |
|
| 99 |
|
| 100 |
if (surface == NULL) { |
| 101 |
return klass->close (encoder, output, msecs, cancellable, error) && |
| 102 |
g_output_stream_close (output, cancellable, error); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
success = klass->process (encoder, output, msecs, surface, region, cancellable, error); |
| 107 |
cairo_surface_destroy (surface); |
| 108 |
cairo_region_destroy (region); |
| 109 |
if (!success) |
| 110 |
return FALSE; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
static gpointer |
| 115 |
byzanz_encoder_thread (gpointer enc) |
| 116 |
{ |
| 117 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (enc); |
| 118 |
ByzanzEncoderClass *klass = BYZANZ_ENCODER_GET_CLASS (encoder); |
| 119 |
GError *error = NULL; |
| 120 |
|
| 121 |
klass->run (encoder, encoder->input_stream, encoder->output_stream, |
| 122 |
encoder->record_audio, encoder->cancellable, &error); |
| 123 |
|
| 124 |
g_idle_add_full (G_PRIORITY_DEFAULT, byzanz_encoder_finished, enc, NULL); |
| 125 |
return error; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
enum { |
| 131 |
PROP_0, |
| 132 |
PROP_INPUT, |
| 133 |
PROP_OUTPUT, |
| 134 |
PROP_SOUND, |
| 135 |
PROP_CANCELLABLE, |
| 136 |
PROP_ERROR, |
| 137 |
PROP_RUNNING |
| 138 |
}; |
| 139 |
|
| 140 |
static void |
| 141 |
byzanz_encoder_base_init (gpointer klass) |
| 142 |
{ |
| 143 |
ByzanzEncoderClass *encoder_class = klass; |
| 144 |
|
| 145 |
encoder_class->filter = NULL; |
| 146 |
} |
| 147 |
|
| 148 |
static void |
| 149 |
byzanz_encoder_base_finalize (gpointer klass) |
| 150 |
{ |
| 151 |
ByzanzEncoderClass *encoder_class = klass; |
| 152 |
|
| 153 |
if (encoder_class->filter) |
| 154 |
g_object_unref (encoder_class->filter); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
static void byzanz_encoder_init (GTypeInstance *instance, gpointer klass); |
| 161 |
static void byzanz_encoder_class_init (ByzanzEncoderClass *klass); |
| 162 |
static gpointer byzanz_encoder_parent_class = NULL; |
| 163 |
static void byzanz_encoder_class_intern_init (gpointer klass, gpointer data) |
| 164 |
{ |
| 165 |
byzanz_encoder_parent_class = g_type_class_peek_parent (klass); |
| 166 |
byzanz_encoder_class_init ((ByzanzEncoderClass*) klass); |
| 167 |
} |
| 168 |
|
| 169 |
GType |
| 170 |
byzanz_encoder_get_type (void) |
| 171 |
{ |
| 172 |
static volatile gsize g_define_type_id__volatile = 0; |
| 173 |
if (g_once_init_enter (&g_define_type_id__volatile)) { |
| 174 |
GTypeInfo info = { |
| 175 |
sizeof (ByzanzEncoderClass), |
| 176 |
byzanz_encoder_base_init, |
| 177 |
byzanz_encoder_base_finalize, |
| 178 |
byzanz_encoder_class_intern_init, |
| 179 |
NULL, |
| 180 |
NULL, |
| 181 |
sizeof (ByzanzEncoder), |
| 182 |
0, |
| 183 |
byzanz_encoder_init, |
| 184 |
NULL |
| 185 |
}; |
| 186 |
GType g_define_type_id; |
| 187 |
|
| 188 |
g_define_type_id = g_type_register_static (G_TYPE_OBJECT, g_intern_static_string ("ByzanzEncoder"), |
| 189 |
&info, G_TYPE_FLAG_ABSTRACT); |
| 190 |
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); |
| 191 |
} |
| 192 |
return g_define_type_id__volatile; |
| 193 |
} |
| 194 |
|
| 195 |
static void |
| 196 |
byzanz_encoder_get_property (GObject *object, guint param_id, GValue *value, |
| 197 |
GParamSpec * pspec) |
| 198 |
{ |
| 199 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (object); |
| 200 |
|
| 201 |
switch (param_id) { |
| 202 |
case PROP_INPUT: |
| 203 |
g_value_set_object (value, encoder->input_stream); |
| 204 |
break; |
| 205 |
case PROP_OUTPUT: |
| 206 |
g_value_set_object (value, encoder->output_stream); |
| 207 |
break; |
| 208 |
case PROP_SOUND: |
| 209 |
g_value_set_boolean (value, encoder->record_audio); |
| 210 |
break; |
| 211 |
case PROP_CANCELLABLE: |
| 212 |
g_value_set_object (value, encoder->cancellable); |
| 213 |
break; |
| 214 |
case PROP_ERROR: |
| 215 |
g_value_set_pointer (value, encoder->error); |
| 216 |
break; |
| 217 |
case PROP_RUNNING: |
| 218 |
g_value_set_boolean (value, encoder->thread != NULL); |
| 219 |
break; |
| 220 |
default: |
| 221 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
static void |
| 227 |
byzanz_encoder_set_property (GObject *object, guint param_id, const GValue *value, |
| 228 |
GParamSpec * pspec) |
| 229 |
{ |
| 230 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (object); |
| 231 |
|
| 232 |
switch (param_id) { |
| 233 |
case PROP_INPUT: |
| 234 |
encoder->input_stream = g_value_dup_object (value); |
| 235 |
g_assert (encoder->input_stream != NULL); |
| 236 |
break; |
| 237 |
case PROP_OUTPUT: |
| 238 |
encoder->output_stream = g_value_dup_object (value); |
| 239 |
g_assert (encoder->output_stream != NULL); |
| 240 |
break; |
| 241 |
case PROP_SOUND: |
| 242 |
encoder->record_audio = g_value_get_boolean (value); |
| 243 |
break; |
| 244 |
case PROP_CANCELLABLE: |
| 245 |
encoder->cancellable = g_value_dup_object (value); |
| 246 |
break; |
| 247 |
default: |
| 248 |
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec); |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
static void |
| 254 |
byzanz_encoder_finalize (GObject *object) |
| 255 |
{ |
| 256 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (object); |
| 257 |
|
| 258 |
g_assert (encoder->thread == NULL); |
| 259 |
|
| 260 |
g_object_unref (encoder->input_stream); |
| 261 |
g_object_unref (encoder->output_stream); |
| 262 |
if (encoder->cancellable) |
| 263 |
g_object_unref (encoder->cancellable); |
| 264 |
if (encoder->error) |
| 265 |
g_error_free (encoder->error); |
| 266 |
|
| 267 |
g_async_queue_unref (encoder->jobs); |
| 268 |
|
| 269 |
G_OBJECT_CLASS (byzanz_encoder_parent_class)->finalize (object); |
| 270 |
} |
| 271 |
|
| 272 |
static void |
| 273 |
byzanz_encoder_constructed (GObject *object) |
| 274 |
{ |
| 275 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (object); |
| 276 |
|
| 277 |
encoder->thread = g_thread_new ("encoder", byzanz_encoder_thread, encoder); |
| 278 |
if (encoder->thread) |
| 279 |
g_object_ref (encoder); |
| 280 |
|
| 281 |
if (G_OBJECT_CLASS (byzanz_encoder_parent_class)->constructed) |
| 282 |
G_OBJECT_CLASS (byzanz_encoder_parent_class)->constructed (object); |
| 283 |
} |
| 284 |
|
| 285 |
static void |
| 286 |
byzanz_encoder_class_init (ByzanzEncoderClass *klass) |
| 287 |
{ |
| 288 |
GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 289 |
|
| 290 |
object_class->get_property = byzanz_encoder_get_property; |
| 291 |
object_class->set_property = byzanz_encoder_set_property; |
| 292 |
object_class->finalize = byzanz_encoder_finalize; |
| 293 |
object_class->constructed = byzanz_encoder_constructed; |
| 294 |
|
| 295 |
g_object_class_install_property (object_class, PROP_INPUT, |
| 296 |
g_param_spec_object ("input", "input", "stream to read data from", |
| 297 |
G_TYPE_INPUT_STREAM, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 298 |
g_object_class_install_property (object_class, PROP_OUTPUT, |
| 299 |
g_param_spec_object ("output", "output", "stream to write data to", |
| 300 |
G_TYPE_OUTPUT_STREAM, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 301 |
g_object_class_install_property (object_class, PROP_SOUND, |
| 302 |
g_param_spec_boolean ("record-audio", "record audio", "TRUE when recording audio", |
| 303 |
FALSE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 304 |
g_object_class_install_property (object_class, PROP_CANCELLABLE, |
| 305 |
g_param_spec_object ("cancellable", "cancellable", "cancellable for stopping the thread", |
| 306 |
G_TYPE_CANCELLABLE, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); |
| 307 |
g_object_class_install_property (object_class, PROP_ERROR, |
| 308 |
g_param_spec_pointer ("error", "error", "error that happened on the thread", |
| 309 |
G_PARAM_READABLE)); |
| 310 |
g_object_class_install_property (object_class, PROP_RUNNING, |
| 311 |
g_param_spec_boolean ("running", "running", "TRUE while the encoding thread is running", |
| 312 |
TRUE, G_PARAM_READABLE)); |
| 313 |
|
| 314 |
klass->run = byzanz_encoder_run; |
| 315 |
} |
| 316 |
|
| 317 |
static void |
| 318 |
byzanz_encoder_init (GTypeInstance *instance, gpointer klass) |
| 319 |
{ |
| 320 |
ByzanzEncoder *encoder = BYZANZ_ENCODER (instance); |
| 321 |
|
| 322 |
encoder->jobs = g_async_queue_new (); |
| 323 |
} |
| 324 |
|
| 325 |
ByzanzEncoder * |
| 326 |
byzanz_encoder_new (GType encoder_type, |
| 327 |
GInputStream * input, |
| 328 |
GOutputStream * output, |
| 329 |
gboolean record_audio, |
| 330 |
GCancellable * cancellable) |
| 331 |
{ |
| 332 |
ByzanzEncoder *encoder; |
| 333 |
|
| 334 |
g_return_val_if_fail (g_type_is_a (encoder_type, BYZANZ_TYPE_ENCODER), NULL); |
| 335 |
g_return_val_if_fail (G_IS_INPUT_STREAM (input), NULL); |
| 336 |
g_return_val_if_fail (G_IS_OUTPUT_STREAM (output), NULL); |
| 337 |
g_return_val_if_fail (cancellable == NULL || G_IS_CANCELLABLE (cancellable), NULL); |
| 338 |
|
| 339 |
encoder = g_object_new (encoder_type, "input", input, "output", output, |
| 340 |
"record-audio", record_audio, "cancellable", cancellable, NULL); |
| 341 |
|
| 342 |
return encoder; |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
gboolean |
| 392 |
byzanz_encoder_is_running (ByzanzEncoder *encoder) |
| 393 |
{ |
| 394 |
g_return_val_if_fail (BYZANZ_IS_ENCODER (encoder), FALSE); |
| 395 |
|
| 396 |
return encoder->thread != NULL; |
| 397 |
} |
| 398 |
|
| 399 |
const GError * |
| 400 |
byzanz_encoder_get_error (ByzanzEncoder *encoder) |
| 401 |
{ |
| 402 |
g_return_val_if_fail (BYZANZ_IS_ENCODER (encoder), FALSE); |
| 403 |
|
| 404 |
return encoder->error; |
| 405 |
} |
| 406 |
|
| 407 |
GtkFileFilter * |
| 408 |
byzanz_encoder_type_get_filter (GType encoder_type) |
| 409 |
{ |
| 410 |
ByzanzEncoderClass *klass; |
| 411 |
GtkFileFilter *filter; |
| 412 |
|
| 413 |
g_return_val_if_fail (g_type_is_a (encoder_type, BYZANZ_TYPE_ENCODER), NULL); |
| 414 |
|
| 415 |
klass = g_type_class_ref (encoder_type); |
| 416 |
filter = klass->filter; |
| 417 |
if (filter) { |
| 418 |
g_assert (!g_object_is_floating (filter)); |
| 419 |
g_object_ref (filter); |
| 420 |
g_object_set_data (G_OBJECT (filter), "byzanz-encoder-type", |
| 421 |
GSIZE_TO_POINTER (encoder_type)); |
| 422 |
} |
| 423 |
|
| 424 |
g_type_class_unref (klass); |
| 425 |
return filter; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
#include "byzanzencoderbyzanz.h" |
| 430 |
#include "byzanzencoderflv.h" |
| 431 |
#include "byzanzencodergif.h" |
| 432 |
#include "byzanzencoderogv.h" |
| 433 |
#include "byzanzencoderwebm.h" |
| 434 |
|
| 435 |
typedef GType (* TypeFunc) (void); |
| 436 |
static const TypeFunc functions[] = { |
| 437 |
byzanz_encoder_gif_get_type, |
| 438 |
byzanz_encoder_webm_get_type, |
| 439 |
byzanz_encoder_ogv_get_type, |
| 440 |
byzanz_encoder_flv_get_type, |
| 441 |
|
| 442 |
byzanz_encoder_byzanz_get_type, |
| 443 |
}; |
| 444 |
#define BYZANZ_ENCODER_DEFAULT_TYPE (functions[0] ()) |
| 445 |
|
| 446 |
GType |
| 447 |
byzanz_encoder_type_iter_init (ByzanzEncoderIter *iter) |
| 448 |
{ |
| 449 |
g_return_val_if_fail (iter != NULL, G_TYPE_NONE); |
| 450 |
|
| 451 |
*iter = GSIZE_TO_POINTER (0); |
| 452 |
|
| 453 |
return functions[0] (); |
| 454 |
} |
| 455 |
|
| 456 |
GType |
| 457 |
byzanz_encoder_type_iter_next (ByzanzEncoderIter *iter) |
| 458 |
{ |
| 459 |
guint id; |
| 460 |
|
| 461 |
g_return_val_if_fail (iter != NULL, G_TYPE_NONE); |
| 462 |
|
| 463 |
id = GPOINTER_TO_SIZE (*iter); |
| 464 |
|
| 465 |
id++; |
| 466 |
if (id >= G_N_ELEMENTS (functions)) |
| 467 |
return G_TYPE_NONE; |
| 468 |
|
| 469 |
*iter = GSIZE_TO_POINTER (id); |
| 470 |
|
| 471 |
return functions[id] (); |
| 472 |
} |
| 473 |
|
| 474 |
GType |
| 475 |
byzanz_encoder_get_type_from_filter (GtkFileFilter *filter) |
| 476 |
{ |
| 477 |
GType type; |
| 478 |
|
| 479 |
g_return_val_if_fail (filter == NULL || GTK_IS_FILE_FILTER (filter), BYZANZ_ENCODER_DEFAULT_TYPE); |
| 480 |
|
| 481 |
if (filter == NULL) |
| 482 |
return BYZANZ_ENCODER_DEFAULT_TYPE; |
| 483 |
|
| 484 |
type = GPOINTER_TO_SIZE (g_object_get_data (G_OBJECT (filter), "byzanz-encoder-type")); |
| 485 |
if (type == 0) |
| 486 |
return BYZANZ_ENCODER_DEFAULT_TYPE; |
| 487 |
|
| 488 |
return type; |
| 489 |
} |
| 490 |
|
| 491 |
GType |
| 492 |
byzanz_encoder_get_type_from_file (GFile *file) |
| 493 |
{ |
| 494 |
ByzanzEncoderIter iter; |
| 495 |
GtkFileFilterInfo info; |
| 496 |
GType type; |
| 497 |
|
| 498 |
g_return_val_if_fail (G_IS_FILE (file), BYZANZ_ENCODER_DEFAULT_TYPE); |
| 499 |
|
| 500 |
info.contains = 0; |
| 501 |
|
| 502 |
info.filename = g_file_get_path (file); |
| 503 |
if (info.filename) |
| 504 |
info.contains |= GTK_FILE_FILTER_FILENAME; |
| 505 |
|
| 506 |
info.uri = g_file_get_uri (file); |
| 507 |
if (info.uri) |
| 508 |
info.contains |= GTK_FILE_FILTER_URI; |
| 509 |
|
| 510 |
|
| 511 |
info.display_name = g_file_get_parse_name (file); |
| 512 |
if (info.display_name) |
| 513 |
info.contains |= GTK_FILE_FILTER_DISPLAY_NAME; |
| 514 |
|
| 515 |
for (type = byzanz_encoder_type_iter_init (&iter); |
| 516 |
type != G_TYPE_NONE; |
| 517 |
type = byzanz_encoder_type_iter_next (&iter)) { |
| 518 |
GtkFileFilter *filter = byzanz_encoder_type_get_filter (type); |
| 519 |
if (filter == NULL) |
| 520 |
continue; |
| 521 |
|
| 522 |
if (gtk_file_filter_filter (filter, &info)) { |
| 523 |
g_object_unref (filter); |
| 524 |
break; |
| 525 |
} |
| 526 |
|
| 527 |
g_object_unref (filter); |
| 528 |
} |
| 529 |
if (type == G_TYPE_NONE) |
| 530 |
type = BYZANZ_ENCODER_DEFAULT_TYPE; |
| 531 |
|
| 532 |
g_free ((char *) info.filename); |
| 533 |
g_free ((char *) info.uri); |
| 534 |
g_free ((char *) info.display_name); |
| 535 |
|
| 536 |
return type; |
| 537 |
} |
| 538 |
|