-
Notifications
You must be signed in to change notification settings - Fork 24
/
bacon-video-widget.c
6384 lines (5370 loc) · 194 KB
/
bacon-video-widget.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
/*
* Copyright (C) 2003-2007 the GStreamer project
* Julien Moutte <julien@moutte.net>
* Ronald Bultje <rbultje@ronald.bitfreak.net>
* Copyright (C) 2003-2014 Bastien Nocera <hadess@hadess.net>
* Copyright (C) 2005-2008 Tim-Philipp Müller <tim centricular net>
* Copyright (C) 2009 Sebastian Dröge <sebastian.droege@collabora.co.uk>
* Copyright © 2009 Christian Persch
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* The Totem project hereby grant permission for non-gpl compatible GStreamer
* plugins to be used and distributed together with GStreamer and Totem. This
* permission is above and beyond the permissions granted by the GPL license
* Totem is covered by.
*
* Monday 7th February 2005: Christian Schaller: Add exception clause.
* See license_change file for details.
*
*/
/**
* SECTION:bacon-video-widget
* @short_description: video playing widget and abstraction
* @stability: Unstable
* @include: bacon-video-widget.h
*
* #BaconVideoWidget is a widget to play audio or video streams, with support for visualisations for audio-only streams. It has a GStreamer
* backend, and abstracts away the differences to provide a simple interface to the functionality required by Totem. It handles all the low-level
* audio and video work for Totem (or passes the work off to the backend).
**/
#include <config.h>
#define GST_USE_UNSTABLE_API 1
#include <gst/gst.h>
/* GStreamer Interfaces */
#include <gst/video/navigation.h>
#include <gst/video/colorbalance.h>
/* for detecting sources of errors */
#include <gst/video/gstvideosink.h>
#include <gst/video/video.h>
#include <gst/audio/audio.h>
#include <gst/audio/streamvolume.h>
/* for missing decoder/demuxer detection */
#include <gst/pbutils/pbutils.h>
/* for the cover metadata info */
#include <gst/tag/tag.h>
#include <clutter-gst/clutter-gst.h>
#include "totem-aspect-frame.h"
/* system */
#include <unistd.h>
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <math.h>
/* gtk+/gnome */
#include <gtk/gtk.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <gdesktop-enums.h>
#include "totem-gst-helpers.h"
#include "totem-gst-pixbuf-helpers.h"
#include "bacon-video-widget.h"
#include "bacon-video-widget-gst-missing-plugins.h"
#include "bacon-video-controls-actor.h"
#include "bacon-video-spinner-actor.h"
#include "bacon-video-widget-enums.h"
#define DEFAULT_USER_AGENT "Videos/"VERSION
#define DEFAULT_CONTROLS_WIDTH 600 /* In pixels */
#define LOGO_SIZE 256 /* Maximum size of the logo */
#define REWIND_OR_PREVIOUS 4000
#define POPUP_HIDING_TIMEOUT 2
#define MAX_NETWORK_SPEED 10752
#define BUFFERING_LEFT_RATIO 1.1
/* Helper constants */
#define NANOSECS_IN_SEC 1000000000
#define SEEK_TIMEOUT NANOSECS_IN_SEC / 10
#define FORWARD_RATE 1.0
#define REVERSE_RATE -1.0
#define DIRECTION_STR (forward == FALSE ? "reverse" : "forward")
#define is_error(e, d, c) \
(e->domain == GST_##d##_ERROR && \
e->code == GST_##d##_ERROR_##c)
#define I_(string) (g_intern_static_string (string))
static void bacon_video_widget_initable_iface_init (GInitableIface *iface);
G_DEFINE_TYPE_WITH_CODE (BaconVideoWidget, bacon_video_widget, GTK_CLUTTER_TYPE_EMBED,
G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
bacon_video_widget_initable_iface_init))
/* Signals */
enum
{
SIGNAL_ERROR,
SIGNAL_EOS,
SIGNAL_REDIRECT,
SIGNAL_CHANNELS_CHANGE,
SIGNAL_TICK,
SIGNAL_GOT_METADATA,
SIGNAL_BUFFERING,
SIGNAL_MISSING_PLUGINS,
SIGNAL_DOWNLOAD_BUFFERING,
SIGNAL_SEEK_REQUESTED,
SIGNAL_TRACK_SKIP_REQUESTED,
SIGNAL_VOLUME_CHANGE_REQUESTED,
LAST_SIGNAL
};
/* Properties */
enum
{
PROP_0,
PROP_LOGO_MODE,
PROP_POSITION,
PROP_CURRENT_TIME,
PROP_STREAM_LENGTH,
PROP_PLAYING,
PROP_REFERRER,
PROP_SEEKABLE,
PROP_USER_AGENT,
PROP_VOLUME,
PROP_DOWNLOAD_FILENAME,
PROP_DEINTERLACING,
PROP_BRIGHTNESS,
PROP_CONTRAST,
PROP_SATURATION,
PROP_HUE,
PROP_AUDIO_OUTPUT_TYPE,
PROP_AV_OFFSET,
PROP_REVEAL_CONTROLS
};
static const gchar *video_props_str[4] = {
"brightness",
"contrast",
"saturation",
"hue"
};
struct BaconVideoWidgetPrivate
{
char *user_agent;
char *referrer;
char *mrl;
char *subtitle_uri;
BvwAspectRatio ratio_type;
GstElement *play;
GstElement *video_sink;
GstNavigation *navigation;
guint update_id;
guint fill_id;
GdkPixbuf *logo_pixbuf;
GdkPixbuf *cover_pixbuf; /* stream-specific image */
gboolean media_has_video;
gboolean media_has_audio;
gint seekable; /* -1 = don't know, FALSE = no */
gint64 stream_length;
gint64 current_time;
gdouble current_position;
gboolean is_live;
GstTagList *tagcache;
GstTagList *audiotags;
GstTagList *videotags;
GAsyncQueue *tag_update_queue;
guint tag_update_id;
gboolean got_redirect;
ClutterActor *stage;
ClutterActor *texture;
ClutterActor *frame;
ClutterActor *header_controls;
ClutterActor *controls;
ClutterActor *spinner;
ClutterActor *logo_frame;
ClutterContent *logo;
GdkCursor *cursor;
/* Controls */
gboolean reveal_controls;
guint transition_timeout_id;
GHashTable *busy_popup_ht; /* key=reason string, value=gboolean */
/* Visual effects */
GstElement *audio_capsfilter;
GstElement *audio_pitchcontrol;
/* Other stuff */
gboolean logo_mode;
gboolean cursor_shown;
gboolean uses_audio_fakesink;
gdouble volume;
gboolean is_menu;
gboolean has_angles;
GList *chapters;
BvwRotation rotation;
gint video_width; /* Movie width */
gint video_height; /* Movie height */
gint movie_par_n; /* Movie pixel aspect ratio numerator */
gint movie_par_d; /* Movie pixel aspect ratio denominator */
gint video_width_pixels; /* Scaled movie width */
gint video_height_pixels; /* Scaled movie height */
gint video_fps_n;
gint video_fps_d;
BvwAudioOutputType speakersetup;
GstBus *bus;
gulong sig_bus_async;
gint eos_id;
/* When seeking, queue up the seeks if they happen before
* the previous one finished */
GMutex seek_mutex;
GstClock *clock;
GstClockTime seek_req_time;
gint64 seek_time;
/* state we want to be in, as opposed to actual pipeline state
* which may change asynchronously or during buffering */
GstState target_state;
gboolean buffering;
gboolean download_buffering;
char *download_filename;
/* used to compute when the download buffer has gone far
* enough to start playback, not "amount of buffering time left
* to reach 100% fill-level" */
gint64 buffering_left;
/* for easy codec installation */
GList *missing_plugins; /* GList of GstMessages */
gboolean plugin_install_in_progress;
GCancellable *missing_plugins_cancellable;
/* for mounting locations if necessary */
GCancellable *mount_cancellable;
gboolean mount_in_progress;
/* for auth */
GMountOperation *auth_dialog;
GMountOperationResult auth_last_result;
char *user_id, *user_pw;
/* for stepping */
float rate;
};
static void bacon_video_widget_set_property (GObject * object,
guint property_id,
const GValue * value,
GParamSpec * pspec);
static void bacon_video_widget_get_property (GObject * object,
guint property_id,
GValue * value,
GParamSpec * pspec);
static void bacon_video_widget_finalize (GObject * object);
static void bvw_reconfigure_fill_timeout (BaconVideoWidget *bvw, guint msecs);
static void size_changed_cb (GdkScreen *screen, BaconVideoWidget *bvw);
static void bvw_stop_play_pipeline (BaconVideoWidget * bvw);
static GError* bvw_error_from_gst_error (BaconVideoWidget *bvw, GstMessage *m);
static gboolean bvw_check_for_cover_pixbuf (BaconVideoWidget * bvw);
static const GdkPixbuf * bvw_get_logo_pixbuf (BaconVideoWidget * bvw);
static gboolean bvw_set_playback_direction (BaconVideoWidget *bvw, gboolean forward);
static gboolean bacon_video_widget_seek_time_no_lock (BaconVideoWidget *bvw,
gint64 _time,
GstSeekFlags flag,
GError **error);
static void set_controls_visibility (BaconVideoWidget *bvw,
gboolean visible,
gboolean animate);
typedef struct {
GstTagList *tags;
const gchar *type;
} UpdateTagsDelayedData;
static void update_tags_delayed_data_destroy (UpdateTagsDelayedData *data);
static GtkWidgetClass *parent_class = NULL;
static int bvw_signals[LAST_SIGNAL] = { 0 };
GST_DEBUG_CATEGORY (_totem_gst_debug_cat);
#define GST_CAT_DEFAULT _totem_gst_debug_cat
typedef gchar * (* MsgToStrFunc) (GstMessage * msg);
static const gchar *
get_type_name (GType class_type, int type)
{
GEnumClass *eclass;
GEnumValue *value;
eclass = G_ENUM_CLASS (g_type_class_peek (class_type));
value = g_enum_get_value (eclass, type);
if (value == NULL)
return "unknown";
return value->value_nick;
}
static gchar **
bvw_get_missing_plugins_foo (const GList * missing_plugins, MsgToStrFunc func)
{
GPtrArray *arr = g_ptr_array_new ();
while (missing_plugins != NULL) {
g_ptr_array_add (arr, func (GST_MESSAGE (missing_plugins->data)));
missing_plugins = missing_plugins->next;
}
g_ptr_array_add (arr, NULL);
return (gchar **) g_ptr_array_free (arr, FALSE);
}
static gchar **
bvw_get_missing_plugins_details (const GList * missing_plugins)
{
return bvw_get_missing_plugins_foo (missing_plugins,
gst_missing_plugin_message_get_installer_detail);
}
static gchar **
bvw_get_missing_plugins_descriptions (const GList * missing_plugins)
{
return bvw_get_missing_plugins_foo (missing_plugins,
gst_missing_plugin_message_get_description);
}
static void
bvw_clear_missing_plugins_messages (BaconVideoWidget * bvw)
{
g_list_free_full (bvw->priv->missing_plugins, (GDestroyNotify) gst_mini_object_unref);
bvw->priv->missing_plugins = NULL;
}
static void
bvw_check_if_video_decoder_is_missing (BaconVideoWidget * bvw)
{
GList *l;
if (bvw->priv->media_has_video || bvw->priv->missing_plugins == NULL)
return;
for (l = bvw->priv->missing_plugins; l != NULL; l = l->next) {
GstMessage *msg = GST_MESSAGE (l->data);
gchar *d, *f;
if ((d = gst_missing_plugin_message_get_installer_detail (msg))) {
if ((f = strstr (d, "|decoder-")) && strstr (f, "video")) {
GError *err;
/* create a fake GStreamer error so we get a nice warning message */
err = g_error_new (GST_CORE_ERROR, GST_CORE_ERROR_MISSING_PLUGIN, "x");
msg = gst_message_new_error (GST_OBJECT (bvw->priv->play), err, NULL);
g_error_free (err);
err = bvw_error_from_gst_error (bvw, msg);
gst_message_unref (msg);
g_signal_emit (bvw, bvw_signals[SIGNAL_ERROR], 0, err->message, FALSE);
g_error_free (err);
g_free (d);
break;
}
g_free (d);
}
}
}
static void
set_display_pixel_aspect_ratio (GdkScreen *screen,
int monitor,
GValue *value)
{
static const gint par[][2] = {
{1, 1}, /* regular screen */
{16, 15}, /* PAL TV */
{11, 10}, /* 525 line Rec.601 video */
{54, 59}, /* 625 line Rec.601 video */
{64, 45}, /* 1280x1024 on 16:9 display */
{5, 3}, /* 1280x1024 on 4:3 display */
{4, 3} /* 800x600 on 16:9 display */
};
guint i;
gint par_index;
gdouble ratio;
gdouble delta;
GdkRectangle rect;
#define DELTA(idx) (ABS (ratio - ((gdouble) par[idx][0] / par[idx][1])))
/* first calculate the "real" ratio based on the X values;
* which is the "physical" w/h divided by the w/h in pixels of the display */
gdk_screen_get_monitor_geometry (screen, monitor, &rect);
ratio = (gdouble) (gdk_screen_get_monitor_width_mm (screen, monitor) * rect.height) /
(gdk_screen_get_monitor_height_mm (screen, monitor) * rect.width);
GST_DEBUG ("calculated pixel aspect ratio: %f", ratio);
/* now find the one from par[][2] with the lowest delta to the real one */
delta = DELTA (0);
par_index = 0;
for (i = 1; i < sizeof (par) / (sizeof (gint) * 2); ++i) {
gdouble this_delta = DELTA (i);
if (this_delta < delta) {
par_index = i;
delta = this_delta;
}
}
GST_DEBUG ("Decided on index %d (%d/%d)", par_index,
par[par_index][0], par[par_index][1]);
gst_value_set_fraction (value, par[par_index][0], par[par_index][1]);
}
static void
get_media_size (BaconVideoWidget *bvw, gint *width, gint *height)
{
if (bvw->priv->logo_mode) {
const GdkPixbuf *pixbuf;
pixbuf = bvw_get_logo_pixbuf (bvw);
if (pixbuf) {
*width = gdk_pixbuf_get_width (pixbuf);
*height = gdk_pixbuf_get_height (pixbuf);
if (*width == *height) {
/* The icons will be square, so lie so we get a 16:9
* ratio */
*width = (int) ((float) *height / 9. * 16.);
}
} else {
*width = 0;
*height = 0;
}
} else {
if (bvw->priv->media_has_video) {
GValue disp_par = {0, };
guint movie_par_n, movie_par_d, disp_par_n, disp_par_d, num, den;
/* Create and init the fraction value */
g_value_init (&disp_par, GST_TYPE_FRACTION);
/* Square pixel is our default */
gst_value_set_fraction (&disp_par, 1, 1);
/* Now try getting display's pixel aspect ratio */
if (gtk_widget_get_realized (GTK_WIDGET (bvw))) {
GdkScreen *screen;
GdkWindow *window;
int monitor = 0;
screen = gtk_widget_get_screen (GTK_WIDGET (bvw));
window = gtk_widget_get_window (GTK_WIDGET (bvw));
if (window)
monitor = gdk_screen_get_monitor_at_window (screen, window);
set_display_pixel_aspect_ratio (screen, monitor, &disp_par);
}
disp_par_n = gst_value_get_fraction_numerator (&disp_par);
disp_par_d = gst_value_get_fraction_denominator (&disp_par);
GST_DEBUG ("display PAR is %d/%d", disp_par_n, disp_par_d);
/* If movie pixel aspect ratio is enforced, use that */
if (bvw->priv->ratio_type != BVW_RATIO_AUTO) {
switch (bvw->priv->ratio_type) {
case BVW_RATIO_SQUARE:
movie_par_n = 1;
movie_par_d = 1;
break;
case BVW_RATIO_FOURBYTHREE:
movie_par_n = 4 * bvw->priv->video_height;
movie_par_d = 3 * bvw->priv->video_width;
break;
case BVW_RATIO_ANAMORPHIC:
movie_par_n = 16 * bvw->priv->video_height;
movie_par_d = 9 * bvw->priv->video_width;
break;
case BVW_RATIO_DVB:
movie_par_n = 20 * bvw->priv->video_height;
movie_par_d = 9 * bvw->priv->video_width;
break;
/* handle these to avoid compiler warnings */
case BVW_RATIO_AUTO:
default:
movie_par_n = 0;
movie_par_d = 0;
g_assert_not_reached ();
}
} else {
/* Use the movie pixel aspect ratio if any */
movie_par_n = bvw->priv->movie_par_n;
movie_par_d = bvw->priv->movie_par_d;
}
GST_DEBUG ("movie PAR is %d/%d", movie_par_n, movie_par_d);
if (bvw->priv->video_width == 0 || bvw->priv->video_height == 0) {
GST_DEBUG ("width and/or height 0, assuming 1/1 ratio");
num = 1;
den = 1;
} else if (!gst_video_calculate_display_ratio (&num, &den,
bvw->priv->video_width, bvw->priv->video_height,
movie_par_n, movie_par_d, disp_par_n, disp_par_d)) {
GST_WARNING ("overflow calculating display aspect ratio!");
num = 1; /* FIXME: what values to use here? */
den = 1;
}
GST_DEBUG ("calculated scaling ratio %d/%d for video %dx%d", num, den,
bvw->priv->video_width, bvw->priv->video_height);
/* now find a width x height that respects this display ratio.
* prefer those that have one of w/h the same as the incoming video
* using wd / hd = num / den */
/* start with same height, because of interlaced video */
/* check hd / den is an integer scale factor, and scale wd with the PAR */
if (bvw->priv->video_height % den == 0) {
GST_DEBUG ("keeping video height");
bvw->priv->video_width_pixels =
(guint) gst_util_uint64_scale (bvw->priv->video_height, num, den);
bvw->priv->video_height_pixels = bvw->priv->video_height;
} else if (bvw->priv->video_width % num == 0) {
GST_DEBUG ("keeping video width");
bvw->priv->video_width_pixels = bvw->priv->video_width;
bvw->priv->video_height_pixels =
(guint) gst_util_uint64_scale (bvw->priv->video_width, den, num);
} else {
GST_DEBUG ("approximating while keeping video height");
bvw->priv->video_width_pixels =
(guint) gst_util_uint64_scale (bvw->priv->video_height, num, den);
bvw->priv->video_height_pixels = bvw->priv->video_height;
}
GST_DEBUG ("scaling to %dx%d", bvw->priv->video_width_pixels,
bvw->priv->video_height_pixels);
*width = bvw->priv->video_width_pixels;
*height = bvw->priv->video_height_pixels;
/* Free the PAR fraction */
g_value_unset (&disp_par);
}
else {
*width = 0;
*height = 0;
}
}
}
static gboolean
leave_notify_cb (GtkWidget *widget,
GdkEventCrossing *event,
gpointer user_data)
{
gboolean res = GDK_EVENT_PROPAGATE;
BaconVideoWidget *bvw = BACON_VIDEO_WIDGET (user_data);
GdkDevice *device;
if (event->detail != GDK_NOTIFY_NONLINEAR &&
event->detail != GDK_NOTIFY_NONLINEAR_VIRTUAL)
return res;
device = gdk_event_get_source_device ((GdkEvent *) event);
if (gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN)
return res;
if (bvw->priv->reveal_controls) {
gboolean not_busy;
not_busy = g_hash_table_size (bvw->priv->busy_popup_ht) == 0;
if (not_busy) {
GST_DEBUG ("will hide because we're not busy and cursor left");
set_controls_visibility (bvw, FALSE, TRUE);
}
}
return res;
}
static void
bacon_video_widget_realize (GtkWidget * widget)
{
BaconVideoWidget *bvw = BACON_VIDEO_WIDGET (widget);
GtkWidget *toplevel;
GTK_WIDGET_CLASS (parent_class)->realize (widget);
gtk_widget_set_realized (widget, TRUE);
/* get screen size changes */
g_signal_connect (G_OBJECT (gtk_widget_get_screen (widget)),
"size-changed", G_CALLBACK (size_changed_cb), bvw);
/* setup the toplevel, ready to be resized */
toplevel = gtk_widget_get_toplevel (widget);
gtk_window_set_geometry_hints (GTK_WINDOW (toplevel), widget, NULL, 0);
g_signal_connect (G_OBJECT (toplevel), "leave-notify-event",
G_CALLBACK (leave_notify_cb), bvw);
bvw->priv->missing_plugins_cancellable = g_cancellable_new ();
g_object_set_data_full (G_OBJECT (bvw), "missing-plugins-cancellable",
bvw->priv->missing_plugins_cancellable, g_object_unref);
bacon_video_widget_gst_missing_plugins_setup (bvw);
}
static void
bacon_video_widget_unrealize (GtkWidget *widget)
{
BaconVideoWidget *bvw = BACON_VIDEO_WIDGET (widget);
GtkWidget *toplevel;
GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
gtk_widget_set_realized (widget, FALSE);
g_signal_handlers_disconnect_by_func (G_OBJECT (gtk_widget_get_screen (widget)),
size_changed_cb, bvw);
toplevel = gtk_widget_get_toplevel (widget);
g_signal_handlers_disconnect_by_func (G_OBJECT (toplevel),
leave_notify_cb, bvw);
g_cancellable_cancel (bvw->priv->missing_plugins_cancellable);
bvw->priv->missing_plugins_cancellable = NULL;
g_object_set_data (G_OBJECT (bvw), "missing-plugins-cancellable", NULL);
}
static void
size_changed_cb (GdkScreen *screen, BaconVideoWidget *bvw)
{
bvw_check_for_cover_pixbuf (bvw);
}
static void
set_current_actor (BaconVideoWidget *bvw)
{
gboolean draw_logo;
if (bvw->priv->stage == NULL)
return;
/* If there's only audio and no visualisation, draw the logo as well.
* If we have a cover image to display, we display it regardless of whether we're
* doing visualisations. */
draw_logo = bvw->priv->media_has_audio &&
!bvw->priv->media_has_video && bvw->priv->cover_pixbuf;
if (bvw->priv->logo_mode || draw_logo) {
const GdkPixbuf *pixbuf;
pixbuf = bvw_get_logo_pixbuf (bvw);
if (pixbuf != NULL) {
gboolean ret;
GError *err = NULL;
ret = clutter_image_set_data (CLUTTER_IMAGE (bvw->priv->logo),
gdk_pixbuf_get_pixels (pixbuf),
gdk_pixbuf_get_has_alpha (pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
gdk_pixbuf_get_width (pixbuf),
gdk_pixbuf_get_height (pixbuf),
gdk_pixbuf_get_rowstride (pixbuf),
&err);
if (ret == FALSE) {
g_warning ("clutter_image_set_data() failed %s", err->message);
g_error_free (err);
} else {
clutter_actor_show (CLUTTER_ACTOR (bvw->priv->logo_frame));
clutter_actor_hide (CLUTTER_ACTOR (bvw->priv->frame));
return;
}
}
}
clutter_actor_show (CLUTTER_ACTOR (bvw->priv->frame));
clutter_actor_hide (CLUTTER_ACTOR (bvw->priv->logo_frame));
}
static void
unschedule_hiding_popup (BaconVideoWidget *bvw)
{
if (bvw->priv->transition_timeout_id > 0)
g_source_remove (bvw->priv->transition_timeout_id);
bvw->priv->transition_timeout_id = 0;
}
static gboolean
hide_popup_timeout_cb (BaconVideoWidget *bvw)
{
set_controls_visibility (bvw, FALSE, TRUE);
unschedule_hiding_popup (bvw);
return G_SOURCE_REMOVE;
}
static void
schedule_hiding_popup (BaconVideoWidget *bvw)
{
unschedule_hiding_popup (bvw);
bvw->priv->transition_timeout_id = g_timeout_add_seconds (POPUP_HIDING_TIMEOUT, (GSourceFunc) hide_popup_timeout_cb, bvw);
g_source_set_name_by_id (bvw->priv->transition_timeout_id, "[totem] hide_popup_timeout_cb");
}
static void
set_show_cursor (BaconVideoWidget *bvw,
gboolean show_cursor)
{
GdkWindow *window;
bvw->priv->cursor_shown = show_cursor;
window = gtk_widget_get_window (GTK_WIDGET (bvw));
if (!window)
return;
if (show_cursor == FALSE) {
GdkCursor *cursor;
cursor = gdk_cursor_new (GDK_BLANK_CURSOR);
gdk_window_set_cursor (window, cursor);
g_object_unref (cursor);
} else {
gdk_window_set_cursor (window, bvw->priv->cursor);
}
}
static void
set_controls_visibility (BaconVideoWidget *bvw,
gboolean visible,
gboolean animate)
{
guint8 opacity = visible ? OVERLAY_OPACITY : 0;
gint header_controls_height;
gfloat header_controls_y;
guint duration;
gtk_widget_get_preferred_height (gtk_clutter_actor_get_widget (GTK_CLUTTER_ACTOR (bvw->priv->header_controls)),
NULL,
&header_controls_height);
header_controls_y = visible ? 0 : -header_controls_height;
duration = animate ? 250 : 0;
/* FIXME:
* Using a show/hide seems to not trigger the
* controls to redraw, so let's change the opacity instead */
clutter_actor_set_easing_duration (bvw->priv->controls, duration);
clutter_actor_set_easing_duration (bvw->priv->header_controls, duration);
clutter_actor_set_opacity (bvw->priv->controls, opacity);
clutter_actor_set_y (bvw->priv->header_controls, header_controls_y);
set_show_cursor (bvw, visible);
if (visible && animate)
schedule_hiding_popup (bvw);
bvw->priv->reveal_controls = visible;
g_object_notify (G_OBJECT (bvw), "reveal-controls");
}
static void
translate_coords (GtkWidget *widget,
GdkWindow *window,
int x,
int y,
int *out_x,
int *out_y)
{
GtkWidget *src;
gdk_window_get_user_data (window, (gpointer *)&src);
if (src && src != widget) {
gtk_widget_translate_coordinates (src, widget, x, y, out_x, out_y);
} else {
*out_x = x;
*out_y = y;
}
}
static gboolean
ignore_event (BaconVideoWidget *bvw,
int x,
int y)
{
ClutterActor *actor;
actor = clutter_stage_get_actor_at_pos (CLUTTER_STAGE (bvw->priv->stage), CLUTTER_PICK_REACTIVE, x, y);
/* Eat the GTK+ event if we're not clicking on the video itself */
if (actor == bvw->priv->controls)
return TRUE;
return FALSE;
}
/* need to use gstnavigation interface for these vmethods, to allow for the sink
to map screen coordinates to video coordinates in the presence of e.g.
hardware scaling */
static gboolean
bacon_video_widget_motion_notify (GtkWidget *widget, GdkEventMotion *event)
{
gboolean res = GDK_EVENT_PROPAGATE;
BaconVideoWidget *bvw = BACON_VIDEO_WIDGET (widget);
GdkDevice *device;
int x, y;
g_return_val_if_fail (bvw->priv->play != NULL, FALSE);
if (bvw->priv->navigation && !bvw->priv->logo_mode)
gst_navigation_send_mouse_event (bvw->priv->navigation, "mouse-move", 0, event->x, event->y);
if (GTK_WIDGET_CLASS (parent_class)->motion_notify_event)
res |= GTK_WIDGET_CLASS (parent_class)->motion_notify_event (widget, event);
device = gdk_event_get_source_device ((GdkEvent *) event);
if (gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN)
return res;
if (!bvw->priv->reveal_controls)
set_controls_visibility (bvw, TRUE, TRUE);
translate_coords (widget, event->window, event->x, event->y, &x, &y);
if (ignore_event (bvw, x, y)) {
/* Is the mouse on the popups? */
unschedule_hiding_popup (bvw);
} else {
schedule_hiding_popup (bvw);
}
return res;
}
static gboolean
bacon_video_widget_button_press_or_release (GtkWidget *widget, GdkEventButton *event)
{
gboolean res = FALSE;
BaconVideoWidget *bvw = BACON_VIDEO_WIDGET (widget);
int x, y;
GdkDevice *device;
device = gdk_event_get_source_device ((GdkEvent *) event);
if (gdk_device_get_source (device) == GDK_SOURCE_TOUCHSCREEN)
return FALSE;
g_return_val_if_fail (bvw->priv->play != NULL, FALSE);
translate_coords (widget, event->window, event->x, event->y, &x, &y);
if (ignore_event (bvw, x, y))
return GDK_EVENT_STOP;
if (event->type != GDK_BUTTON_PRESS &&
event->type != GDK_BUTTON_RELEASE)
goto bail;
if (bvw->priv->navigation &&
!bvw->priv->logo_mode &&
event->button == 1 &&
bvw->priv->is_menu != FALSE) {
const char *event_str;
event_str = (event->type == GDK_BUTTON_PRESS) ? "mouse-button-press" : "mouse-button-release";
gst_navigation_send_mouse_event (bvw->priv->navigation,
event_str, event->button, x, y);
/* FIXME need to check whether the backend will have handled
* the button press
res = TRUE; */
}
bail:
if (event->type == GDK_BUTTON_PRESS && GTK_WIDGET_CLASS (parent_class)->button_press_event)
res |= GTK_WIDGET_CLASS (parent_class)->button_press_event (widget, event);
if (event->type == GDK_BUTTON_RELEASE && GTK_WIDGET_CLASS (parent_class)->button_release_event)
res |= GTK_WIDGET_CLASS (parent_class)->button_release_event (widget, event);
return res;
}
static gboolean
bacon_video_widget_tap (ClutterTapAction *action,
ClutterActor *actor,
BaconVideoWidget *bvw)
{
ClutterInputDevice *device;
const ClutterEvent *event;
gboolean value;
GST_DEBUG ("Tap event received");
event = clutter_gesture_action_get_last_event (CLUTTER_GESTURE_ACTION (action), 0);
if (!event)
return CLUTTER_EVENT_PROPAGATE;
device = clutter_event_get_source_device (event);
if (device == NULL ||
clutter_input_device_get_device_type (device) != CLUTTER_TOUCHSCREEN_DEVICE)
return CLUTTER_EVENT_PROPAGATE;
value = (clutter_actor_get_opacity (bvw->priv->controls) == 0);
set_controls_visibility (bvw, value, FALSE);
return CLUTTER_EVENT_STOP;
}
static gboolean
bacon_video_widget_swipe (ClutterSwipeAction *action,
ClutterActor *actor,
ClutterSwipeDirection direction,
BaconVideoWidget *bvw)
{
GST_DEBUG ("Swipe event received");
if ((direction & CLUTTER_SWIPE_DIRECTION_UP) ||
(direction & CLUTTER_SWIPE_DIRECTION_DOWN)) {
if ((direction & CLUTTER_SWIPE_DIRECTION_LEFT) ||
(direction & CLUTTER_SWIPE_DIRECTION_RIGHT))
GST_DEBUG ("Ignoring diagonal swipe 0x%X", direction);
return CLUTTER_EVENT_PROPAGATE;
}
if (direction & CLUTTER_SWIPE_DIRECTION_LEFT)
g_signal_emit (G_OBJECT (bvw), bvw_signals[SIGNAL_SEEK_REQUESTED], 0,
gtk_widget_get_direction (GTK_WIDGET (bvw)) == GTK_TEXT_DIR_RTL);
if (direction & CLUTTER_SWIPE_DIRECTION_RIGHT)
g_signal_emit (G_OBJECT (bvw), bvw_signals[SIGNAL_SEEK_REQUESTED], 0,
gtk_widget_get_direction (GTK_WIDGET (bvw)) == GTK_TEXT_DIR_LTR);
return CLUTTER_EVENT_STOP;
}
static gboolean
bacon_video_widget_handle_scroll (GtkWidget *widget,
GdkEventScroll *event,
BaconVideoWidget *bvw)
{
int x, y;
gboolean forward;
gdouble delta_y;
g_return_val_if_fail (bvw->priv->play != NULL, FALSE);
if (event->direction != GDK_SCROLL_SMOOTH)
return GDK_EVENT_PROPAGATE;
if (widget == (gpointer) bvw) {
translate_coords (widget, event->window, event->x, event->y, &x, &y);
if (ignore_event (bvw, x, y))
return GDK_EVENT_STOP;
}
gdk_event_get_scroll_deltas ((GdkEvent *) event, NULL, &delta_y);
if (delta_y == 0.0)
return GDK_EVENT_PROPAGATE;
forward = delta_y >= 0.0 ? FALSE : TRUE;
if (widget == (gpointer) bvw ||
widget == g_object_get_data (G_OBJECT (bvw->priv->controls), "seek_scale")) {
if (bvw->priv->seekable > 0)
g_signal_emit (G_OBJECT (bvw), bvw_signals[SIGNAL_SEEK_REQUESTED], 0, forward);
} else if (widget == g_object_get_data (G_OBJECT (bvw->priv->controls), "volume_button")) {
if (bacon_video_widget_can_set_volume (bvw))
g_signal_emit (G_OBJECT (bvw), bvw_signals[SIGNAL_VOLUME_CHANGE_REQUESTED], 0, forward);