-
Notifications
You must be signed in to change notification settings - Fork 1
/
photobooth.c
2980 lines (2664 loc) · 107 KB
/
photobooth.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* photobooth.c
* Copyright 2016 Andreas Frisch <fraxinas@opendreambox.org>
*
* This program is licensed under the Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 Unported
* License. To view a copy of this license, visit
* http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter to
* Creative Commons,559 Nathan Abbott Way,Stanford,California 94305,USA.
*
* This program is NOT free software. It is open source, you are allowed
* to modify it (if you keep the license), but it may not be commercially
* distributed other than under the conditions noted above.
*/
#include <poll.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <gst/video/videooverlay.h>
#include <gst/video/gstvideosink.h>
#include <gst/app/app.h>
#include <curl/curl.h>
#include <X11/Xlib.h>
// #ifdef HAVE_LIBCANBERRA
#include <canberra-gtk.h>
// #endif
#include "photobooth.h"
#include "photoboothwin.h"
#include "photoboothled.h"
#include "photoboothmasquerade.h"
#include <glib/gstdio.h>
#include <gio/gio.h>
#define G_SETTINGS_ENABLE_BACKEND
#include <gio/gsettingsbackend.h>
#define photo_booth_parent_class parent_class
#define _(key) (G_strings_table && g_hash_table_contains (G_strings_table, key) ? g_hash_table_lookup (G_strings_table, key) : key)
typedef enum { NONE, ACK_SOUND, ERROR_SOUND } sound_t;
typedef enum { SAVE_NEVER, SAVE_ASK, SAVE_PRINTED, SAVE_ALL } save_t;
typedef enum { UPLOAD_NEVER, UPLOAD_ASK, UPLOAD_PRINTED, UPLOAD_ALL } upload_t;
typedef enum { FACEDETECT_DISABLED, FACEDETECT_ENABLEABLE, FACEDETECT_ENABLED } facedetect_t;
typedef struct _PhotoBoothPrivate PhotoBoothPrivate;
struct _PhotoBoothPrivate
{
PhotoboothState state;
PhotoBoothWindow *win;
GstVideoRectangle video_size;
GThread *capture_thread;
gulong video_block_id, photo_block_id, sink_block_id;
gint state_change_watchdog_timeout_id;
guint32 countdown;
gint preview_timeout;
gulong preview_timeout_id;
gchar *overlay_image;
gboolean do_flip;
gboolean hide_cursor;
save_t do_save_photos;
gchar *save_path_template;
guint photos_taken, photos_printed;
guint save_filename_count;
gchar *printer_backend;
gchar *gutenprint_path;
gint print_copies_min, print_copies_default, print_copies_max, print_copies;
gint print_dpi, print_width, print_height;
gdouble print_x_offset, print_y_offset;
gchar *print_icc_profile;
gint prints_remaining;
GstBuffer *print_buffer;
GtkPrintSettings *printer_settings;
GMutex processing_mutex;
gboolean drop_thumbnails;
gint preview_fps, preview_width, preview_height;
gboolean cam_reeinit_before_snapshot, cam_reeinit_after_snapshot;
gboolean cam_keep_files;
gchar *cam_icc_profile;
GstElement *audio_pipeline;
GstElement *audio_playbin;
GstElement *screensaver_playbin;
gboolean paused_callback_id;
gchar *countdown_audio_uri;
gchar *error_sound;
gchar *ack_sound;
gchar *screensaver_uri;
gint screensaver_timeout;
guint screensaver_timeout_id;
gint64 last_play_pos;
gint upload_timeout;
upload_t do_linx_upload;
gchar *linx_put_uri;
gchar *linx_api_key;
gint linx_expiry;
GThread *linx_upload_thread;
gchar *uuid;
gchar *facebook_put_uri;
gchar *imgur_album_id;
gchar *imgur_access_token;
gchar *imgur_description;
GThread *publish_thread;
GMutex upload_mutex;
gboolean curl_cancelled;
gchar *twitter_bridge_host;
guint twitter_bridge_port;
gboolean do_qrcode;
gchar *qrcode_base_uri;
gint qrcode_x_offset;
gint qrcode_y_offset;
gfloat qrcode_scale;
PhotoBoothMasquerade *masquerade;
facedetect_t enable_facedetect;
gboolean do_masquerade;
gchar *masks_dir;
gchar *masks_json;
GstElement *mask_bin;
gboolean enable_repositioning;
PhotoBoothLed *led;
};
#define MOVIEPIPE "moviepipe.mjpg"
#define DEFAULT_CONFIG "default.ini"
#define PREVIEW_FPS 19
#define DEFAULT_COUNTDOWN 5
#define DEFAULT_SAVE_PHOTOS SAVE_NEVER
#define DEFAULT_SAVE_PATH_TEMPLATE "./snapshot%03d.jpg"
#define DEFAULT_SCREENSAVER_TIMEOUT -1
#define DEFAULT_FLIP TRUE
#define DEFAULT_HIDE_CURSOR TRUE
#define DEFAULT_FACEDETECT FACEDETECT_DISABLED
#define DEFAULT_ENABLE_REPOSITIONING FALSE
#define DEFAULT_GUTENPRINT_PATH "/usr/lib/cups/backend/gutenprint53+usb"
#define PRINT_DPI 346
#define PRINT_WIDTH 2076
#define PRINT_HEIGHT 1384
#define PREVIEW_WIDTH 640
#define PREVIEW_HEIGHT 424
#define PT_PER_IN 72
#define IMGUR_UPLOAD_URI "https://api.imgur.com/3/upload"
#define DEFAULT_TWITTER_BRIDGE_HOST NULL
#define DEFAULT_TWITTER_BRIDGE_PORT 0
#define DEFAULT_QRCODE FALSE
#define DEFAULT_QRCODE_X -1
#define DEFAULT_QRCODE_Y -1
#define DEFAULT_QRCODE_SCALE 4.0
#define DEFAULT_QRCODE_BASE_URI NULL
#define DEFAULT_LINX_UPLOAD UPLOAD_NEVER
gchar *G_template_filename;
gchar *G_stylesheet_filename;
GHashTable *G_strings_table;
G_DEFINE_TYPE_WITH_PRIVATE (PhotoBooth, photo_booth, GTK_TYPE_APPLICATION)
GST_DEBUG_CATEGORY_STATIC (photo_booth_debug);
#define GST_CAT_DEFAULT photo_booth_debug
/* GObject / GApplication */
static void photo_booth_activate (GApplication *app);
static void photo_booth_open (GApplication *app, GFile **files, gint n_files, const gchar *hint);
static void photo_booth_dispose (GObject *object);
static void photo_booth_finalize (GObject *object);
PhotoBooth *photo_booth_new (void);
void photo_booth_background_clicked (GtkWidget *widget, GdkEventButton *event, PhotoBoothWindow *win);
void photo_booth_flip_toggled (GtkToggleButton *widget, PhotoBoothWindow *win);
void photo_booth_button_cancel_clicked (GtkButton *button, PhotoBoothWindow *win);
void photo_booth_cancel (PhotoBooth *pb);
/* general private functions */
const gchar* photo_booth_state_get_name (PhotoboothState state);
static void photo_booth_change_state (PhotoBooth *pb, PhotoboothState state);
static gboolean photo_booth_quit_signal (gpointer);
static void photo_booth_window_destroyed_signal (PhotoBoothWindow *win, PhotoBooth *pb);
static void photo_booth_setup_window (PhotoBooth *pb);
static gboolean photo_booth_video_widget_ready (PhotoBooth *pb);
static gboolean photo_booth_preview (PhotoBooth *pb);
static gboolean photo_booth_preview_ready (PhotoBooth *pb);
static void photo_booth_snapshot_start (PhotoBooth *pb);
static gboolean photo_booth_snapshot_prepare (PhotoBooth *pb);
static gboolean photo_booth_snapshot_trigger (PhotoBooth *pb);
static gboolean photo_booth_snapshot_taken (PhotoBooth *pb);
static gboolean photo_booth_screensaver (PhotoBooth *pb);
static gboolean photo_booth_screensaver_stop (PhotoBooth *pb);
static gboolean photo_booth_watchdog_timedout (PhotoBooth *pb);
static gboolean photo_booth_capture_paused_cb (PhotoBooth *pb);
/* libgphoto2 */
static gboolean photo_booth_cam_init (CameraInfo **cam_info);
static gboolean photo_booth_cam_close (CameraInfo **cam_info);
static gboolean photo_booth_focus (CameraInfo *cam_info);
static gboolean photo_booth_take_photo (PhotoBooth *pb);
static void photo_booth_flush_pipe (int fd);
static gpointer photo_booth_capture_thread_func (gpointer user_data);
static void _gphoto_err(GPLogLevel level, const char *domain, const char *str, void *data);
/* gstreamer functions */
static GstElement *build_video_bin (PhotoBooth *pb);
static GstElement *build_photo_bin (PhotoBooth *pb);
static gboolean photo_booth_setup_gstreamer (PhotoBooth *pb);
static gboolean photo_booth_bus_callback (GstBus *bus, GstMessage *message, PhotoBooth *pb);
static GstPadProbeReturn photo_booth_drop_thumbnails (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
static GstPadProbeReturn photo_booth_catch_photo_buffer (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
static gboolean photo_booth_process_photo_plug_elements (PhotoBooth *pb);
static gboolean photo_booth_push_photo_buffer (gpointer user_data);
static GstFlowReturn photo_booth_catch_print_buffer (GstElement * appsink, gpointer user_data);
static gboolean photo_booth_process_photo_remove_elements (PhotoBooth *pb);
static GstPadProbeReturn photo_booth_screensaver_unplug_continue (GstPad * pad, GstPadProbeInfo * info, gpointer user_data);
static gboolean photo_booth_preview_timedout (PhotoBooth *pb);
/* printing functions */
static gboolean photo_booth_get_printer_status (PhotoBooth *pb);
void photo_booth_button_print_clicked (GtkButton *button, PhotoBoothWindow *win);
static gboolean photo_booth_print (gpointer user_data);
static void photo_booth_begin_print (GtkPrintOperation *operation, GtkPrintContext *context, gpointer user_data);
static void photo_booth_draw_page (GtkPrintOperation *operation, GtkPrintContext *context, int page_nr, gpointer user_data);
static void photo_booth_print_done (GtkPrintOperation *operation, GtkPrintOperationResult result, gpointer user_data);
static void photo_booth_printing_error_dialog (PhotoBoothWindow *window, GError *print_error);
/* upload functions */
void photo_booth_button_upload_clicked (GtkButton *button, PhotoBoothWindow *win);
void photo_booth_button_publish_clicked (GtkButton *button, PhotoBoothWindow *win);
static gpointer photo_booth_public_post_thread_func (gpointer user_data);
static gpointer photo_booth_linx_post_thread_func (gpointer user_data);
static gboolean photo_booth_publish_timedout (PhotoBooth *pb);
static void photo_booth_class_init (PhotoBoothClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
GApplicationClass *gapplication_class = G_APPLICATION_CLASS (klass);
GST_DEBUG_CATEGORY_INIT (photo_booth_debug, "photobooth", GST_DEBUG_BOLD | GST_DEBUG_FG_YELLOW | GST_DEBUG_BG_BLUE, "PhotoBooth");
GST_DEBUG ("photo_booth_class_init");
gp_log_add_func(GP_LOG_ERROR, _gphoto_err, NULL);
gobject_class->finalize = photo_booth_finalize;
gobject_class->dispose = photo_booth_dispose;
gapplication_class->activate = photo_booth_activate;
gapplication_class->open = photo_booth_open;
}
static void photo_booth_init (PhotoBooth *pb)
{
PhotoBoothPrivate *priv;
priv = photo_booth_get_instance_private (pb);
GST_DEBUG_OBJECT (pb, "photo_booth_init init object!");
int control_sock[2];
if (socketpair (PF_UNIX, SOCK_STREAM, 0, control_sock) < 0)
{
GST_ERROR ("cannot create control sockets: %s (%i)", strerror(errno), errno);
g_application_quit (G_APPLICATION (pb));
}
READ_SOCKET (pb) = control_sock[0];
WRITE_SOCKET (pb) = control_sock[1];
fcntl (READ_SOCKET (pb), F_SETFL, O_NONBLOCK);
fcntl (WRITE_SOCKET (pb), F_SETFL, O_NONBLOCK);
pb->cam_info = NULL;
pb->pipeline = NULL;
priv->state = PB_STATE_NONE;
priv->video_block_id = 0;
priv->photo_block_id = 0;
priv->sink_block_id = 0;
if (mkfifo(MOVIEPIPE, 0666) == -1 && errno != EEXIST)
{
GST_ERROR ("cannot create moviepipe file %s: %s (%i)", MOVIEPIPE, strerror(errno), errno);
g_application_quit (G_APPLICATION (pb));
}
pb->video_fd = open(MOVIEPIPE, O_RDWR);
if (pb->video_fd == -1)
{
GST_ERROR ("cannot open moviepipe file %s: %s (%i)", MOVIEPIPE, strerror(errno), errno);
g_application_quit (G_APPLICATION (pb));
}
priv->capture_thread = NULL;
priv->countdown = DEFAULT_COUNTDOWN;
priv->do_flip = DEFAULT_FLIP;
priv->hide_cursor = DEFAULT_HIDE_CURSOR;
priv->preview_timeout = 0;
priv->preview_timeout_id = 0;
priv->preview_fps = PREVIEW_FPS;
priv->preview_width = PREVIEW_WIDTH;
priv->preview_height = PREVIEW_HEIGHT;
priv->print_copies_min = priv->print_copies_max = priv->print_copies_default = 1;
priv->print_copies = 1;
priv->print_dpi = PRINT_DPI;
priv->print_width = PRINT_WIDTH;
priv->print_height = PRINT_HEIGHT;
priv->print_x_offset = priv->print_y_offset = 0;
priv->print_buffer = NULL;
priv->print_icc_profile = NULL;
priv->cam_icc_profile = NULL;
priv->cam_keep_files = FALSE;
priv->printer_backend = NULL;
priv->gutenprint_path = DEFAULT_GUTENPRINT_PATH;
priv->printer_settings = NULL;
priv->overlay_image = NULL;
priv->countdown_audio_uri = NULL;
priv->ack_sound = NULL;
priv->error_sound = NULL;
priv->screensaver_uri = NULL;
priv->screensaver_timeout = DEFAULT_SCREENSAVER_TIMEOUT;
priv->screensaver_timeout_id = 0;
priv->paused_callback_id = 0;
priv->last_play_pos = GST_CLOCK_TIME_NONE;
priv->save_path_template = g_strdup (DEFAULT_SAVE_PATH_TEMPLATE);
priv->photos_taken = priv->photos_printed = 0;
priv->save_filename_count = 0;
priv->upload_timeout = 0;
priv->do_linx_upload = DEFAULT_LINX_UPLOAD;
priv->linx_put_uri = NULL;
priv->linx_api_key = NULL;
priv->linx_expiry = 60;
priv->linx_upload_thread = NULL;
priv->facebook_put_uri = NULL;
priv->imgur_album_id = NULL;
priv->imgur_access_token = NULL;
priv->imgur_description = NULL;
priv->publish_thread = NULL;
priv->twitter_bridge_host = g_strdup (DEFAULT_TWITTER_BRIDGE_HOST);
priv->twitter_bridge_port = DEFAULT_TWITTER_BRIDGE_PORT;
priv->do_qrcode = DEFAULT_QRCODE;
priv->qrcode_x_offset = DEFAULT_QRCODE_X;
priv->qrcode_y_offset = DEFAULT_QRCODE_Y;
priv->qrcode_scale = DEFAULT_QRCODE_SCALE;
priv->qrcode_base_uri = DEFAULT_QRCODE_BASE_URI;
priv->state_change_watchdog_timeout_id = 0;
priv->enable_facedetect = DEFAULT_FACEDETECT;
priv->do_masquerade = FALSE;
priv->masquerade = NULL;
priv->masks_dir = NULL;
priv->masks_json = NULL;
priv->enable_repositioning = DEFAULT_ENABLE_REPOSITIONING;
priv->led = photo_booth_led_new ();
G_stylesheet_filename = NULL;
G_template_filename = NULL;
G_strings_table = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
g_mutex_init (&priv->processing_mutex);
g_mutex_init (&priv->upload_mutex);
}
static void photo_booth_change_state (PhotoBooth *pb, PhotoboothState newstate)
{
PhotoBoothPrivate *priv;
priv = photo_booth_get_instance_private (pb);
GST_DEBUG_OBJECT (pb, "change state %s -> %s", photo_booth_state_get_name (priv->state), photo_booth_state_get_name (newstate));
gchar *dot_filename = g_strdup_printf ("state_change_%s_to_%s", photo_booth_state_get_name (priv->state), photo_booth_state_get_name (newstate));
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (pb->pipeline), GST_DEBUG_GRAPH_SHOW_ALL, dot_filename);
g_free (dot_filename);
priv->state = newstate;
if (priv->state_change_watchdog_timeout_id)
{
GST_LOG ("removed watchdog timeout");
g_source_remove (priv->state_change_watchdog_timeout_id);
priv->state_change_watchdog_timeout_id = 0;
}
}
static void photo_booth_setup_window (PhotoBooth *pb)
{
PhotoBoothPrivate *priv;
priv = photo_booth_get_instance_private (pb);
priv->win = photo_booth_window_new (pb);
gtk_window_present (GTK_WINDOW (priv->win));
g_signal_connect (G_OBJECT (priv->win), "destroy", G_CALLBACK (photo_booth_window_destroyed_signal), pb);
priv->capture_thread = g_thread_try_new ("gphoto-capture", (GThreadFunc) photo_booth_capture_thread_func, pb, NULL);
photo_booth_setup_gstreamer (pb);
photo_booth_get_printer_status (pb);
gtk_toggle_button_set_active (priv->win->toggle_flip, priv->do_flip);
}
static void photo_booth_activate (GApplication *app)
{
GST_DEBUG_OBJECT (app, "photo_booth_activate");
photo_booth_setup_window (PHOTO_BOOTH (app));
}
static void photo_booth_open (GApplication *app, G_GNUC_UNUSED GFile **files, G_GNUC_UNUSED gint n_files, G_GNUC_UNUSED const gchar *hint)
{
GST_DEBUG_OBJECT (app, "photo_booth_open");
photo_booth_setup_window (PHOTO_BOOTH (app));
}
static void photo_booth_finalize (GObject *object)
{
PhotoBooth *pb = PHOTO_BOOTH (object);
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
GST_INFO_OBJECT (pb, "finalize");
SEND_COMMAND (pb, CONTROL_QUIT);
photo_booth_flush_pipe (pb->video_fd);
g_thread_join (priv->capture_thread);
if (pb->cam_info)
photo_booth_cam_close (&pb->cam_info);
if (pb->video_fd)
{
close (pb->video_fd);
unlink (MOVIEPIPE);
}
if (priv->publish_thread)
g_thread_join (priv->publish_thread);
if (priv->linx_upload_thread)
g_thread_join (priv->linx_upload_thread);
if (priv->audio_pipeline) {
gst_element_set_state (priv->audio_pipeline, GST_STATE_NULL);
gst_object_unref (priv->audio_pipeline);
}
if (pb->pipeline) {
gst_element_set_state (pb->pipeline, GST_STATE_NULL);
gst_object_unref (pb->pipeline);
}
g_object_unref (priv->led);
}
static void photo_booth_dispose (GObject *object)
{
PhotoBoothPrivate *priv;
priv = photo_booth_get_instance_private (PHOTO_BOOTH (object));
g_free (priv->printer_backend);
g_free (priv->gutenprint_path);
if (priv->printer_settings != NULL)
g_object_unref (priv->printer_settings);
g_free (priv->countdown_audio_uri);
g_free (priv->ack_sound);
g_free (priv->error_sound);
g_free (priv->screensaver_uri);
g_free (priv->print_icc_profile);
g_free (priv->cam_icc_profile);
g_free (priv->overlay_image);
g_free (priv->save_path_template);
g_free (priv->linx_put_uri);
g_free (priv->linx_api_key);
g_free (priv->facebook_put_uri);
g_free (priv->imgur_album_id);
g_free (priv->imgur_access_token);
g_free (priv->imgur_description);
g_free (priv->twitter_bridge_host);
g_free (priv->qrcode_base_uri);
g_free (priv->masks_dir);
g_free (priv->masks_json);
if (priv->masquerade)
g_object_unref (priv->masquerade);
g_hash_table_destroy (G_strings_table);
G_strings_table = NULL;
g_mutex_clear (&priv->processing_mutex);
g_mutex_clear (&priv->upload_mutex);
G_OBJECT_CLASS (photo_booth_parent_class)->dispose (object);
g_free (G_stylesheet_filename);
g_free (G_template_filename);
}
#define READ_INT_INI_KEY(var, gkf, grp, key) { \
GError *err = NULL; \
gint i = g_key_file_get_integer (gkf, grp, key, &err); \
if (!err) { \
var = i; GST_TRACE ("read integer ini key [%s]:%s = %d", grp, key, var); \
} else { \
GST_TRACE ("ini key [%s]:%s not present. keep default value %d", grp, key, var); \
g_error_free (err); \
} \
}
#define READ_DBL_INI_KEY(var, gkf, grp, key) { \
GError *err = NULL; \
gdouble i = g_key_file_get_double (gkf, grp, key, &err); \
if (!err) { \
var = i; GST_TRACE ("read double ini key [%s]:%s = %f", grp, key, var); \
} else { \
GST_TRACE ("ini key [%s]:%s not present. keep default value %f", grp, key, var); \
g_error_free (err); \
} \
}
#define READ_STR_INI_KEY(var, gkf, grp, key) { \
GError *err = NULL; \
gchar *str = g_key_file_get_string (gkf, grp, key, &err); \
if (!err) { \
var = g_strdup(str); \
GST_TRACE ("read string ini key [%s]:%s = %s", grp, key, var); \
g_free(str); \
} else { \
GST_TRACE ("ini key [%s]:%s not present. keep default value %s", grp, key, var); \
g_error_free (err); \
} \
}
#define READ_BOOL_INI_KEY(var, gkf, grp, key) { \
GError *err = NULL; \
gboolean b = g_key_file_get_boolean (gkf, grp, key, &err); \
if (!err) { \
var = b; \
GST_TRACE ("read boolean ini key [%s]:%s = %s", grp, key, var ? "TRUE" : "FALSE"); \
} else { \
GST_TRACE ("ini key [%s]:%s not present. keep default value %i", grp, key, var); \
g_error_free (err); \
} \
}
void photo_booth_load_settings (PhotoBooth *pb, const gchar *filename)
{
GKeyFile* gkf;
GError *error = NULL;
guint keyidx;
gsize num_keys;
gchar **keys, *val;
gchar *key;
gchar *value;
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
GST_DEBUG ("loading settings from file %s", filename);
gkf = g_key_file_new();
if (g_key_file_load_from_file (gkf, filename, G_KEY_FILE_NONE, &error))
{
if (g_key_file_has_group (gkf, "strings"))
{
keys = g_key_file_get_keys (gkf, "strings", &num_keys, &error);
for (keyidx = 0; keyidx < num_keys; keyidx++)
{
val = g_key_file_get_value(gkf, "strings", keys[keyidx], &error);
key = g_strdup(keys[keyidx]);
value = g_strdup(val);
g_hash_table_insert (G_strings_table, key, value);
GST_TRACE ("key %u/%"G_GSIZE_FORMAT":\t'%s' => '%s'", keyidx, num_keys-1, key, value);
g_free (val);
}
if (error)
{
GST_INFO ( "can't read string: %s", error->message);
g_error_free (error);
}
g_strfreev (keys);
}
if (g_key_file_has_group (gkf, "general"))
{
gchar *screensaverfile = NULL, *save_path_template = NULL;
READ_STR_INI_KEY (G_template_filename, gkf, "general", "template");
READ_STR_INI_KEY (G_stylesheet_filename, gkf, "general", "stylesheet");
READ_INT_INI_KEY (priv->countdown, gkf, "general", "countdown");
READ_INT_INI_KEY (priv->preview_timeout, gkf, "general", "preview_timeout");
READ_STR_INI_KEY (priv->overlay_image, gkf, "general", "overlay_image");
READ_INT_INI_KEY (priv->screensaver_timeout, gkf, "general", "screensaver_timeout");
READ_STR_INI_KEY (screensaverfile, gkf, "general", "screensaver_file");
READ_INT_INI_KEY (priv->enable_facedetect, gkf, "general", "facedetection");
READ_BOOL_INI_KEY (priv->hide_cursor, gkf, "general", "hide_cursor");
if (screensaverfile)
{
gchar *screensaverabsfilename;
if (!g_path_is_absolute (screensaverfile))
{
gchar *cdir = g_get_current_dir ();
screensaverabsfilename = g_strdup_printf ("%s/%s", cdir, screensaverfile);
g_free (cdir);
}
else
screensaverabsfilename = g_strdup (screensaverfile);
priv->screensaver_uri = g_filename_to_uri (screensaverabsfilename, NULL, NULL);
g_free (screensaverfile);
g_free (screensaverabsfilename);
}
READ_INT_INI_KEY (priv->do_save_photos, gkf, "general", "save_photos");
READ_STR_INI_KEY (save_path_template, gkf, "general", "save_path_template");
if (save_path_template)
{
gchar *cdir;
if (!g_path_is_absolute (save_path_template))
{
cdir = g_get_current_dir ();
priv->save_path_template = g_strdup_printf ("%s/%s", cdir, save_path_template);
g_free (cdir);
}
else
{
cdir = g_path_get_dirname (save_path_template);
priv->save_path_template = g_strdup (save_path_template);
}
g_free (save_path_template);
}
}
if (g_key_file_has_group (gkf, "sounds"))
{
gchar *countdownaudiofile = NULL;
READ_STR_INI_KEY (countdownaudiofile, gkf, "sounds", "countdown_audio_file");
if (countdownaudiofile)
{
gchar *audioabsfilename;
if (!g_path_is_absolute (countdownaudiofile))
{
gchar *cdir = g_get_current_dir ();
audioabsfilename = g_strdup_printf ("%s/%s", cdir, countdownaudiofile);
g_free (cdir);
}
else
audioabsfilename = g_strdup (countdownaudiofile);
priv->countdown_audio_uri = g_filename_to_uri (audioabsfilename, NULL, NULL);
g_free (countdownaudiofile);
g_free (audioabsfilename);
}
READ_STR_INI_KEY (priv->ack_sound, gkf, "sounds", "ack_sound");
READ_STR_INI_KEY (priv->error_sound, gkf, "sounds", "error_sound");
}
if (g_key_file_has_group (gkf, "printer"))
{
READ_STR_INI_KEY (priv->printer_backend, gkf, "printer", "backend");
READ_STR_INI_KEY (priv->gutenprint_path, gkf, "printer", "gutenprint_path");
READ_INT_INI_KEY (priv->print_copies_min, gkf, "printer", "copies_min");
READ_INT_INI_KEY (priv->print_copies_max, gkf, "printer", "copies_max");
READ_INT_INI_KEY (priv->print_copies_default, gkf, "printer", "copies_default");
READ_INT_INI_KEY (priv->print_dpi, gkf, "printer", "dpi");
READ_INT_INI_KEY (priv->print_width, gkf, "printer", "width");
READ_INT_INI_KEY (priv->print_height, gkf, "printer", "height");
READ_STR_INI_KEY (priv->print_icc_profile, gkf, "printer", "icc_profile");
READ_DBL_INI_KEY (priv->print_x_offset, gkf, "printer", "offset_x");
READ_DBL_INI_KEY (priv->print_y_offset, gkf, "printer", "offset_y");
}
if (g_key_file_has_group (gkf, "camera"))
{
READ_INT_INI_KEY (priv->preview_fps, gkf, "camera", "preview_fps")
READ_INT_INI_KEY (priv->preview_width, gkf, "camera", "preview_width");
READ_INT_INI_KEY (priv->preview_height, gkf, "camera", "preview_height");
READ_BOOL_INI_KEY (priv->cam_reeinit_before_snapshot, gkf, "camera", "cam_reeinit_before_snapshot");
READ_BOOL_INI_KEY (priv->cam_reeinit_after_snapshot, gkf, "camera", "cam_reeinit_after_snapshot");
READ_BOOL_INI_KEY (priv->cam_keep_files, gkf, "camera", "cam_keep_files");
}
if (g_key_file_has_group (gkf, "upload"))
{
READ_STR_INI_KEY (priv->qrcode_base_uri, gkf, "upload", "qrcode_base_uri");
READ_INT_INI_KEY (priv->qrcode_x_offset, gkf, "upload", "qrcode_x_offset");
READ_INT_INI_KEY (priv->qrcode_y_offset, gkf, "upload", "qrcode_y_offset");
READ_DBL_INI_KEY (priv->qrcode_scale, gkf, "upload", "qrcode_scale");
READ_INT_INI_KEY (priv->do_linx_upload, gkf, "upload", "linx_upload");
READ_STR_INI_KEY (priv->linx_put_uri, gkf, "upload", "linx_put_uri");
READ_STR_INI_KEY (priv->linx_api_key, gkf, "upload", "linx_api_key");
READ_INT_INI_KEY (priv->linx_expiry, gkf, "upload", "linx_expiry");
READ_INT_INI_KEY (priv->upload_timeout, gkf, "upload", "upload_timeout");
READ_STR_INI_KEY (priv->facebook_put_uri, gkf, "upload", "facebook_put_uri");
READ_STR_INI_KEY (priv->imgur_album_id, gkf, "upload", "imgur_album_id");
READ_STR_INI_KEY (priv->imgur_access_token, gkf, "upload", "imgur_access_token");
READ_STR_INI_KEY (priv->imgur_description, gkf, "upload", "imgur_description");
READ_STR_INI_KEY (priv->twitter_bridge_host, gkf, "upload", "twitter_bridge_host");
READ_INT_INI_KEY (priv->twitter_bridge_port, gkf, "upload", "twitter_bridge_port");
priv->do_qrcode = !!priv->qrcode_base_uri;
}
if (g_key_file_has_group (gkf, "masks"))
{
READ_STR_INI_KEY (priv->masks_dir, gkf, "masks", "directory");
READ_STR_INI_KEY (priv->masks_json, gkf, "masks", "list");
}
}
gchar *save_path_basename = g_path_get_basename (priv->save_path_template);
gchar *pos = g_strstr_len ((const gchar*) save_path_basename, strlen (save_path_basename), "%");
if (pos)
{
gchar *filenameprefix = g_strndup (save_path_basename, pos-save_path_basename);
GDir *save_dir;
GError *error = NULL;
const gchar *save_path_dirname = g_path_get_dirname (priv->save_path_template);
save_dir = g_dir_open (save_path_dirname, 0, &error);
if (error) {
GST_WARNING ("couldn't open save directory '%s': %s", priv->save_path_template, error->message);
}
else if (save_dir)
{
const gchar *filename;
GMatchInfo *match_info;
GRegex *regex;
const gchar *pattern = g_strdup_printf("(?<filename>%s)(?<number>\\d+)", filenameprefix);
GST_TRACE ("save_path_base_name regex pattern = '%s'", pattern);
regex = g_regex_new (pattern, 0, 0, &error);
if (error) {
g_critical ("%s\n", error->message);
}
while ((filename = g_dir_read_name (save_dir)))
{
if (g_regex_match (regex, filename, 0, &match_info))
{
gint count = atoi(g_match_info_fetch_named (match_info, "number"));
gchar *name = g_match_info_fetch_named (match_info, "filename");
if (count > (int) priv->save_filename_count)
priv->save_filename_count = count;
GST_TRACE ("save_path_template found matching file %s (prefix %s, count %d, highest %i)", filename, name, count, priv->save_filename_count);
g_free (name);
}
else
GST_TRACE ("save_path_template unmatched file %s", filename);
}
g_dir_close (save_dir);
}
GST_WARNING ("save_path_dirname %s", save_path_dirname);
}
g_free (save_path_basename);
g_key_file_free (gkf);
if (error)
{
GST_INFO ( "can't open settings file %s: %s", filename, error->message);
g_error_free (error);
}
}
static void _gphoto_err(GPLogLevel level, const char *domain, const char *str, G_GNUC_UNUSED void *data)
{
GST_DEBUG ("GPhoto %d, %s:%s", (int) level, domain, str);
}
static GstPadProbeReturn _gst_photo_probecb (GstPad * pad, G_GNUC_UNUSED GstPadProbeInfo * info, G_GNUC_UNUSED gpointer user_data)
{
GST_LOG_OBJECT (pad, "drop photo");
return GST_PAD_PROBE_DROP;
}
static GstPadProbeReturn _gst_video_probecb (GstPad * pad, G_GNUC_UNUSED GstPadProbeInfo * info, G_GNUC_UNUSED gpointer user_data)
{
GST_LOG_OBJECT (pad, "drop video");
return GST_PAD_PROBE_DROP;
}
static void _restart_screensaver_timeout (PhotoBooth *pb)
{
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
if (priv->screensaver_timeout > 0) {
if (priv->screensaver_timeout_id)
g_source_remove (priv->screensaver_timeout_id);
priv->screensaver_timeout_id = g_timeout_add_seconds (priv->screensaver_timeout, (GSourceFunc) photo_booth_screensaver, pb);
}
}
void _play_event_sound (PhotoBoothPrivate *priv, sound_t sound)
{
gchar *soundfile = NULL;
switch (sound) {
case ACK_SOUND:
soundfile = priv->ack_sound;
break;
case ERROR_SOUND:
soundfile = priv->error_sound;
break;
default:
break;
}
if (soundfile)
ca_context_play (ca_gtk_context_get(), 0, CA_PROP_MEDIA_FILENAME, soundfile, NULL);
}
static void photo_booth_delete_file (PhotoBooth *pb)
{
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
g_mutex_lock (&priv->upload_mutex);
const gchar *filename = g_strdup_printf (priv->save_path_template, priv->save_filename_count);
if (g_unlink (filename)) {
GST_ERROR ("error deleting file '%s': %s (%i)", filename, strerror(errno), errno);
}
g_mutex_unlock (&priv->upload_mutex);
}
static gboolean photo_booth_cam_init (CameraInfo **cam_info)
{
int retval;
if (*cam_info)
{
GST_ERROR_OBJECT (*cam_info, "tried to do cam_init before cam_info was closed!");
return FALSE;
}
*cam_info = (CameraInfo*)malloc(sizeof(struct _CameraInfo));
if (!cam_info)
return FALSE;
g_mutex_init (&(*cam_info)->mutex);
g_mutex_lock (&(*cam_info)->mutex);
(*cam_info)->preview_capture_count = 0;
(*cam_info)->size = 0;
(*cam_info)->data = NULL;
(*cam_info)->context = gp_context_new();
gp_camera_new (&(*cam_info)->camera);
retval = gp_camera_init ((*cam_info)->camera, (*cam_info)->context);
GST_DEBUG ("gp_camera_init returned %d cam_info@%p camera@%p", retval, (void*) *cam_info, cam_info ? (void*) (*cam_info)->camera : NULL);
g_mutex_unlock (&(*cam_info)->mutex);
if (retval == GP_ERROR_IO_USB_CLAIM)
{
g_usleep (G_USEC_PER_SEC);
}
if (retval != GP_OK) {
GST_WARNING ("calling photo_booth_cam_close because retval != GP_OK");
photo_booth_cam_close (&(*cam_info));
return FALSE;
}
return TRUE;
}
static gboolean photo_booth_cam_close (CameraInfo **cam_info)
{
int retval;
if (*cam_info == NULL)
{
GST_ERROR ("tried to close cam when cam_info == NULL");
return FALSE;
}
g_mutex_lock (&(*cam_info)->mutex);
retval = gp_camera_exit((*cam_info)->camera, (*cam_info)->context);
GST_DEBUG ("gp_camera_exit returned %i", retval);
gp_camera_free ((*cam_info)->camera);
gp_context_unref ((*cam_info)->context);
g_mutex_unlock (&(*cam_info)->mutex);
g_mutex_clear (&(*cam_info)->mutex);
free (*cam_info);
*cam_info = NULL;
return GP_OK ? TRUE : FALSE;
}
static void photo_booth_cam_config (PhotoBooth *pb)
{
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
int ret;
CameraWidgetType type;
CameraWidget *rootconfig = NULL, *child;
const char *name = "capturetarget";
const char *value = priv->cam_keep_files ? "1" : "0";
ret = gp_camera_get_single_config (pb->cam_info->camera, name, &child, pb->cam_info->context);
rootconfig = child;
if (ret != GP_OK)
goto fail;
ret = gp_widget_get_child_by_name (rootconfig, name, &child);
if (ret != GP_OK)
goto fail;
ret = gp_widget_get_type (child, &type);
if (ret != GP_OK)
goto fail;
if (type == GP_WIDGET_RADIO)
{
int cnt, i;
char *endptr;
cnt = gp_widget_count_choices (child);
if (cnt < GP_OK) {
ret = cnt;
goto fail;
}
ret = GP_ERROR_BAD_PARAMETERS;
for ( i=0; i<cnt; i++) {
const char *choice;
ret = gp_widget_get_choice (child, i, &choice);
if (ret != GP_OK)
continue;
if (!strcmp (choice, value)) {
ret = gp_widget_set_value (child, value);
break;
}
}
if (i != cnt)
goto fail;
i = strtol (value, &endptr, 10);
if ((value != endptr) && (*endptr == '\0')) {
if ((i>= 0) && (i < cnt)) {
const char *choice;
ret = gp_widget_get_choice (child, i, &choice);
if (ret == GP_OK)
ret = gp_widget_set_value (child, choice);
}
}
ret = gp_widget_set_value (child, value);
if (ret != GP_OK)
goto fail;
ret = gp_camera_set_single_config (pb->cam_info->camera, name, child, pb->cam_info->context);
if (ret != GP_OK)
goto fail;
GST_INFO ("capturetarget configured to %s in camera", value);
gp_widget_free (rootconfig);
return;
}
fail:
GST_WARNING ("couldn't set %s config!", name);
if (rootconfig)
gp_widget_free (rootconfig);
}
static void photo_booth_flush_pipe (int fd)
{
int rlen = 0;
unsigned char buf[1024];
const int flags = fcntl(fd, F_GETFL, 0);
fcntl (fd, F_SETFL, flags | O_NONBLOCK);
while (rlen != -1)
{
rlen = read (fd, buf, sizeof(buf));
}
fcntl (fd, F_SETFL, flags ^ O_NONBLOCK);
}
static gboolean photo_booth_quit_signal (gpointer user_data)
{
PhotoBooth *pb = PHOTO_BOOTH (user_data);
GST_INFO ("caught SIGINT! exit...");
g_application_quit (G_APPLICATION (pb));
return FALSE;
}
static void photo_booth_window_destroyed_signal (G_GNUC_UNUSED PhotoBoothWindow *win, PhotoBooth *pb)
{
GST_INFO ("main window closed! exit...");
g_application_quit (G_APPLICATION (pb));
}
static gpointer photo_booth_capture_thread_func (gpointer user_data)
{
PhotoBooth *pb = PHOTO_BOOTH (user_data);
PhotoboothCaptureThreadState state = CAPTURE_INIT;
PhotoBoothPrivate *priv = photo_booth_get_instance_private (pb);
CameraFile *gp_file = NULL;
int gpret, captured_frames = 0;
GST_DEBUG ("enter capture thread fd = %d", pb->video_fd);
if (gp_file_new_from_fd (&gp_file, pb->video_fd) != GP_OK)
{
GST_ERROR ("couldn't start capture thread because gp_file_new_from_fd (%d) failed!", pb->video_fd);
goto quit_thread;
}
while (TRUE) {
if (state == CAPTURE_QUIT)
goto quit_thread;
struct pollfd rfd[2];
int timeout = 0;
rfd[0].fd = READ_SOCKET (pb);
rfd[0].events = POLLIN | POLLERR | POLLHUP | POLLPRI;
if (state == CAPTURE_INIT || (state == CAPTURE_FAILED && !pb->cam_info))
{
if (pb->cam_info == NULL)
{
if (photo_booth_cam_init (&pb->cam_info))
{
static gsize cam_configured = 0;
GST_INFO ("photo_booth_cam_inited @ %p", (void *)pb->cam_info);
if (g_once_init_enter (&cam_configured))
{
photo_booth_cam_config (pb);
g_once_init_leave (&cam_configured, 1);
}
if (state == CAPTURE_FAILED)
{
photo_booth_window_set_spinner (priv->win, FALSE);
}
}
else {
gtk_label_set_text (priv->win->status, _("No camera connected!"));
GST_INFO ("no camera info.");
}
}
if (pb->cam_info)
{
state = CAPTURE_VIDEO;
g_main_context_invoke (NULL, (GSourceFunc) photo_booth_preview, pb);
}
timeout = 5000;
}
else if (state == CAPTURE_PAUSED)
timeout = 1000;
else
timeout = 1000 / priv->preview_fps;
int ret = poll(rfd, 1, timeout);
GST_TRACE ("poll ret=%i, state=%i, cam_info@%p", ret, state, (void *)pb->cam_info);
if (G_UNLIKELY (ret == -1))