-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgst-play-mod.c
1433 lines (1184 loc) · 39 KB
/
gst-play-mod.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
/* Based on the GStreamer command line playback testing utility
*
* Copyright (C) 2013-2014 Tim-Philipp Müller <tim centricular net>
* Copyright (C) 2013 Collabora Ltd.
* Copyright (C) 2015 Centricular Ltd
* Copyright (C) 2021 No Isolation AS
*
* This application is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This application 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <gst/audio/audio.h>
#include <gst/video/video.h>
#include <gst/math-compat.h>
#include <xcb/xcb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gprintf.h>
#include <dbus/dbus.h>
#include <dbus/dbus-glib-lowlevel.h>
#define PROGRAM_NAME "gst-play-mod"
#define VERSION_STRING "1.0"
#define VOLUME_STEPS 20
GST_DEBUG_CATEGORY (play_debug);
#define GST_CAT_DEFAULT play_debug
#define INTERFACE_NAME "com.noisolation.MediaPlayer"
#define OBJECT_PATH "/com/noisolation/MediaPlayer"
typedef enum
{
GST_PLAY_TRICK_MODE_NONE = 0,
GST_PLAY_TRICK_MODE_DEFAULT,
GST_PLAY_TRICK_MODE_DEFAULT_NO_AUDIO,
GST_PLAY_TRICK_MODE_KEY_UNITS,
GST_PLAY_TRICK_MODE_KEY_UNITS_NO_AUDIO,
GST_PLAY_TRICK_MODE_LAST
} GstPlayTrickMode;
typedef enum
{
GST_PLAY_TRACK_TYPE_INVALID = 0,
GST_PLAY_TRACK_TYPE_AUDIO,
GST_PLAY_TRACK_TYPE_VIDEO,
GST_PLAY_TRACK_TYPE_SUBTITLE
} GstPlayTrackType;
typedef struct
{
gchar **uris;
guint num_uris;
gint cur_idx;
GstElement *playbin;
GstStreamCollection *collection;
gchar *cur_audio_sid;
gchar *cur_video_sid;
gchar *cur_text_sid;
GMutex selection_lock;
GMainLoop *loop;
guint bus_watch;
guint timeout;
gboolean buffering;
gboolean is_live;
GstState desired_state;
gulong deep_notify_id;
/* configuration */
gboolean gapless;
GstPlayTrickMode trick_mode;
gdouble rate;
} GstPlay;
static gboolean quiet = FALSE;
static gboolean play_bus_msg (GstBus * bus, GstMessage * msg, gpointer data);
static gboolean play_next (GstPlay * play);
static gboolean play_prev (GstPlay * play);
static gboolean play_timeout (gpointer user_data);
static void play_about_to_finish (GstElement * playbin, gpointer user_data);
static void play_reset (GstPlay * play);
static void play_set_relative_volume (GstPlay * play, gdouble volume_step);
static void play_set_playback_rate (GstPlay * play, gdouble rate);
static void play_set_relative_playback_rate (GstPlay * play, gdouble rate_step, gboolean reverse_direction);
static void play_switch_trick_mode (GstPlay * play);
static void play_cycle_track_selection (GstPlay * play, GstPlayTrackType track_type);
static void play_toggle_audio_mute (GstPlay * play);
static gboolean play_do_seek (GstPlay * play, gint64 pos, gdouble rate, GstPlayTrickMode mode);
static void toggle_paused (GstPlay * play);
static void relative_seek (GstPlay * play, gdouble percent);
/* *INDENT-OFF* */
static void gst_play_printf (const gchar * format, ...) G_GNUC_PRINTF (1, 2);
/* *INDENT-ON* */
static DBusHandlerResult
server_message_handler (DBusConnection *conn, DBusMessage *message, void *user_data)
{
DBusError err;
GstPlay *play = (GstPlay *) user_data;
gboolean quit = FALSE;
gst_printerr ("Got D-Bus request: %s.%s on %s\n",
dbus_message_get_interface (message),
dbus_message_get_member (message),
dbus_message_get_path (message));
dbus_error_init (&err);
if (dbus_message_is_signal (message, INTERFACE_NAME, "Quit")) {
quit = TRUE;
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "TogglePaused")) {
toggle_paused (play);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "PlayNext")) {
if (!play_next (play)) {
gst_print ("\n%s\n", "Reached end of play list.");
quit = TRUE;
}
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "PlayPrevious")) {
play_prev (play);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "IncreasePlaybackRate")) {
if (play->rate > -0.2 && play->rate < 0.0)
play_set_relative_playback_rate (play, 0.0, TRUE);
else if (ABS (play->rate) < 2.0)
play_set_relative_playback_rate (play, 0.1, FALSE);
else if (ABS (play->rate) < 4.0)
play_set_relative_playback_rate (play, 0.5, FALSE);
else
play_set_relative_playback_rate (play, 1.0, FALSE);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "DecreasePlaybackRate")) {
if (play->rate > 0.0 && play->rate < 0.20)
play_set_relative_playback_rate (play, 0.0, TRUE);
else if (ABS (play->rate) <= 2.0)
play_set_relative_playback_rate (play, -0.1, FALSE);
else if (ABS (play->rate) <= 4.0)
play_set_relative_playback_rate (play, -0.5, FALSE);
else
play_set_relative_playback_rate (play, -1.0, FALSE);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ChangePlaybackDirection")) {
play_set_relative_playback_rate (play, 0.0, TRUE);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ToggleTrickMode")) {
play_switch_trick_mode (play);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ChangeAudioTrack")) {
play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_AUDIO);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ChangeVideoTrack")) {
play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_VIDEO);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ChangeSubtitleTrack")) {
play_cycle_track_selection (play, GST_PLAY_TRACK_TYPE_SUBTITLE);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "SeekToBeginning")) {
play_do_seek (play, 0, play->rate, play->trick_mode);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "ToggleAudioMute")) {
play_toggle_audio_mute (play);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "IncreaseAudioVolume")) {
play_set_relative_volume (play, +1.0 / VOLUME_STEPS);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "DecreaseAudioVolume")) {
play_set_relative_volume (play, -1.0 / VOLUME_STEPS);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "SeekRight")) {
relative_seek (play, +0.08);
} else if (dbus_message_is_signal (message, INTERFACE_NAME, "SeekLeft")) {
relative_seek (play, +0.08);
} else {
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
if (dbus_error_is_set (&err)) {
dbus_error_free (&err);
}
if (quit) {
gst_printerr ("Server exiting...\n");
g_main_loop_quit (play->loop);
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static gboolean
server_setup(GstPlay *play)
{
DBusConnection *connection;
DBusError error;
int rv;
dbus_error_init (&error);
connection = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (!connection) {
gst_printerr ("Failed to get a session DBus connection: %s\n", error.message);
dbus_error_free (&error);
return FALSE;
}
rv = dbus_bus_request_name(connection, INTERFACE_NAME, DBUS_NAME_FLAG_REPLACE_EXISTING , &error);
if (rv != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
gst_printerr ("Failed to request name on bus: %s\n", error.message);
dbus_error_free (&error);
return FALSE;
}
dbus_bus_add_match (connection, "type='signal',interface='com.noisolation.MediaPlayer'", &error);
if (dbus_error_is_set (&error)) {
gst_printerr ("Failed to add match: %s\n", error.message);
dbus_error_free (&error);
return FALSE;
}
if (!dbus_connection_add_filter (connection, server_message_handler, play, NULL)) {
gst_printerr ("Failed to add filter: %s\n", error.message);
dbus_error_free (&error);
return FALSE;
}
dbus_connection_setup_with_g_main (connection, NULL);
return TRUE;
}
static void
client_send_signal(const char * signal)
{
DBusConnection *connection;
DBusMessage *message;
DBusError error;
dbus_error_init (&error);
connection = dbus_bus_get (DBUS_BUS_SESSION, &error);
if (!connection) {
gst_printerr (" Failed to connect to the D-BUS daemon: %s", error.message);
dbus_error_free (&error);
return;
}
message = dbus_message_new_signal (OBJECT_PATH, INTERFACE_NAME, signal);
if (!message) {
gst_printerr("Error creating DBus message\n");
dbus_connection_unref(connection);
return;
}
if (!dbus_connection_send (connection, message, NULL)) {
gst_printerr (" Failed to send signal");
dbus_error_free (&error);
dbus_message_unref (message);
dbus_connection_unref (connection);
return;
}
dbus_connection_flush (connection);
dbus_message_unref (message);
dbus_connection_unref (connection);
gst_print (" Signal sendt: %s\n", signal);
}
static void
create_window (GstBus * bus, GstMessage * message, GstPipeline * pipeline)
{
xcb_connection_t *connection;
xcb_screen_t *screen;
xcb_window_t window;
uint32_t values[2];
connection = xcb_connect (NULL, NULL);
screen = xcb_setup_roots_iterator (xcb_get_setup (connection)).data;
window = xcb_generate_id(connection);
values[0] = screen->black_pixel;
values[1] = XCB_EVENT_MASK_EXPOSURE;
xcb_create_window (connection, XCB_COPY_FROM_PARENT, window, screen->root,
0, 0, screen->width_in_pixels, screen->height_in_pixels, 0,
XCB_WINDOW_CLASS_INPUT_OUTPUT, screen->root_visual,
XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK, values);
xcb_map_window (connection, window);
xcb_flush (connection);
gst_video_overlay_set_window_handle (
GST_VIDEO_OVERLAY (GST_MESSAGE_SRC (message)), window);
}
static void
gst_play_printf (const gchar * format, ...)
{
gchar *str = NULL;
va_list args;
int len;
if (quiet)
return;
va_start (args, format);
len = g_vasprintf (&str, format, args);
va_end (args);
if (len > 0 && str != NULL)
gst_print ("%s", str);
g_free (str);
}
#define gst_print gst_play_printf
static GstPlay *
play_new (gchar ** uris, const gchar * audio_sink, const gchar * video_sink,
gboolean gapless, gdouble initial_volume, gdouble initial_rate, gboolean verbose,
const gchar * flags_string)
{
GstElement *sink, *playbin;
GstPlay *play;
playbin = gst_element_factory_make ("playbin", "playbin");
if (playbin == NULL)
return NULL;
play = g_new0 (GstPlay, 1);
play->uris = uris;
play->num_uris = g_strv_length (uris);
play->cur_idx = -1;
play->playbin = playbin;
g_mutex_init (&play->selection_lock);
if (audio_sink != NULL) {
if (strchr (audio_sink, ' ') != NULL)
sink = gst_parse_bin_from_description (audio_sink, TRUE, NULL);
else
sink = gst_element_factory_make (audio_sink, NULL);
if (sink != NULL)
g_object_set (play->playbin, "audio-sink", sink, NULL);
else
g_warning ("Couldn't create specified audio sink '%s'", audio_sink);
}
if (video_sink != NULL) {
if (strchr (video_sink, ' ') != NULL)
sink = gst_parse_bin_from_description (video_sink, TRUE, NULL);
else
sink = gst_element_factory_make (video_sink, NULL);
if (sink != NULL)
g_object_set (play->playbin, "video-sink", sink, NULL);
else
g_warning ("Couldn't create specified video sink '%s'", video_sink);
}
if (flags_string != NULL) {
GParamSpec *pspec;
GValue val = { 0, };
pspec =
g_object_class_find_property (G_OBJECT_GET_CLASS (playbin), "flags");
g_value_init (&val, pspec->value_type);
if (gst_value_deserialize (&val, flags_string))
g_object_set_property (G_OBJECT (play->playbin), "flags", &val);
else
gst_printerr ("Couldn't convert '%s' to playbin flags!\n", flags_string);
g_value_unset (&val);
}
if (verbose) {
play->deep_notify_id =
gst_element_add_property_deep_notify_watch (play->playbin, NULL, TRUE);
}
play->loop = g_main_loop_new (NULL, FALSE);
play->bus_watch = gst_bus_add_watch (GST_ELEMENT_BUS (play->playbin),
play_bus_msg, play);
/* FIXME: make configurable incl. 0 for disable */
play->timeout = g_timeout_add (100, play_timeout, play);
play->buffering = FALSE;
play->is_live = FALSE;
play->desired_state = GST_STATE_PLAYING;
play->gapless = gapless;
if (gapless) {
g_signal_connect (play->playbin, "about-to-finish",
G_CALLBACK (play_about_to_finish), play);
}
if (initial_volume != -1)
play_set_relative_volume (play, initial_volume - 1.0);
play->rate = 1.0;
play->trick_mode = GST_PLAY_TRICK_MODE_NONE;
if (initial_rate != -1)
play->rate = initial_rate;
return play;
}
static void
play_free (GstPlay * play)
{
/* No need to see all those pad caps going to NULL etc., it's just noise */
if (play->deep_notify_id != 0)
g_signal_handler_disconnect (play->playbin, play->deep_notify_id);
play_reset (play);
gst_element_set_state (play->playbin, GST_STATE_NULL);
gst_object_unref (play->playbin);
g_source_remove (play->bus_watch);
g_source_remove (play->timeout);
g_main_loop_unref (play->loop);
g_strfreev (play->uris);
if (play->collection)
gst_object_unref (play->collection);
g_free (play->cur_audio_sid);
g_free (play->cur_video_sid);
g_free (play->cur_text_sid);
g_mutex_clear (&play->selection_lock);
g_free (play);
}
/* reset for new file/stream */
static void
play_reset (GstPlay * play)
{
play->buffering = FALSE;
play->is_live = FALSE;
}
static void
play_set_relative_volume (GstPlay * play, gdouble volume_step)
{
gdouble volume;
volume = gst_stream_volume_get_volume (GST_STREAM_VOLUME (play->playbin),
GST_STREAM_VOLUME_FORMAT_CUBIC);
volume = round ((volume + volume_step) * VOLUME_STEPS) / VOLUME_STEPS;
volume = CLAMP (volume, 0.0, 10.0);
gst_stream_volume_set_volume (GST_STREAM_VOLUME (play->playbin),
GST_STREAM_VOLUME_FORMAT_CUBIC, volume);
gst_print ("Volume: %.0f%%", volume * 100);
gst_print (" \n");
}
static void
play_toggle_audio_mute (GstPlay * play)
{
gboolean mute;
mute = gst_stream_volume_get_mute (GST_STREAM_VOLUME (play->playbin));
mute = !mute;
gst_stream_volume_set_mute (GST_STREAM_VOLUME (play->playbin), mute);
if (mute)
gst_print ("Mute: on");
else
gst_print ("Mute: off");
gst_print (" \n");
}
static gboolean
play_bus_msg (GstBus * bus, GstMessage * msg, gpointer user_data)
{
GstPlay *play = user_data;
if (gst_is_video_overlay_prepare_window_handle_message (msg))
create_window (bus, msg, GST_PIPELINE (play->playbin));
switch (GST_MESSAGE_TYPE (msg)) {
case GST_MESSAGE_ASYNC_DONE:
/* dump graph on preroll */
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.async-done");
gst_print ("Prerolled.\r");
break;
case GST_MESSAGE_BUFFERING:{
gint percent;
if (!play->buffering)
gst_print ("\n");
gst_message_parse_buffering (msg, &percent);
gst_print ("%s %d%% \r", "Buffering...", percent);
if (percent == 100) {
/* a 100% message means buffering is done */
if (play->buffering) {
play->buffering = FALSE;
/* no state management needed for live pipelines */
if (!play->is_live)
gst_element_set_state (play->playbin, GST_STATE_PLAYING);
}
} else {
/* buffering... */
if (!play->buffering) {
if (!play->is_live)
gst_element_set_state (play->playbin, GST_STATE_PAUSED);
play->buffering = TRUE;
}
}
break;
}
case GST_MESSAGE_CLOCK_LOST:{
gst_print ("Clock lost, selecting a new one\n");
gst_element_set_state (play->playbin, GST_STATE_PAUSED);
gst_element_set_state (play->playbin, GST_STATE_PLAYING);
break;
}
case GST_MESSAGE_LATENCY:
gst_print ("Redistribute latency...\n");
gst_bin_recalculate_latency (GST_BIN (play->playbin));
break;
case GST_MESSAGE_REQUEST_STATE:{
GstState state;
gchar *name;
name = gst_object_get_path_string (GST_MESSAGE_SRC (msg));
gst_message_parse_request_state (msg, &state);
gst_print ("Setting state to %s as requested by %s...\n",
gst_element_state_get_name (state), name);
gst_element_set_state (play->playbin, state);
g_free (name);
break;
}
case GST_MESSAGE_EOS:
/* print final position at end */
play_timeout (play);
gst_print ("\n");
/* and switch to next item in list */
if (!play_next (play)) {
gst_print ("%s\n", "Reached end of play list.");
g_main_loop_quit (play->loop);
}
break;
case GST_MESSAGE_WARNING:{
GError *err;
gchar *dbg = NULL;
/* dump graph on warning */
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.warning");
gst_message_parse_warning (msg, &err, &dbg);
gst_printerr ("WARNING %s\n", err->message);
if (dbg != NULL)
gst_printerr ("WARNING debug information: %s\n", dbg);
g_clear_error (&err);
g_free (dbg);
break;
}
case GST_MESSAGE_ERROR:{
GError *err;
gchar *dbg;
/* dump graph on error */
GST_DEBUG_BIN_TO_DOT_FILE_WITH_TS (GST_BIN (play->playbin),
GST_DEBUG_GRAPH_SHOW_ALL, "gst-play.error");
gst_message_parse_error (msg, &err, &dbg);
gst_printerr ("ERROR %s for %s\n", err->message,
play->uris[play->cur_idx]);
if (dbg != NULL)
gst_printerr ("ERROR debug information: %s\n", dbg);
g_clear_error (&err);
g_free (dbg);
/* flush any other error messages from the bus and clean up */
gst_element_set_state (play->playbin, GST_STATE_NULL);
/* try next item in list then */
if (!play_next (play)) {
gst_print ("%s\n", "Reached end of play list.");
g_main_loop_quit (play->loop);
}
break;
}
case GST_MESSAGE_PROPERTY_NOTIFY:{
const GValue *val;
const gchar *name;
GstObject *obj;
gchar *val_str = NULL;
gchar *obj_name;
gst_message_parse_property_notify (msg, &obj, &name, &val);
obj_name = gst_object_get_path_string (GST_OBJECT (obj));
if (val != NULL) {
if (G_VALUE_HOLDS_STRING (val))
val_str = g_value_dup_string (val);
else if (G_VALUE_TYPE (val) == GST_TYPE_CAPS)
val_str = gst_caps_to_string (g_value_get_boxed (val));
else if (G_VALUE_TYPE (val) == GST_TYPE_TAG_LIST)
val_str = gst_tag_list_to_string (g_value_get_boxed (val));
else
val_str = gst_value_serialize (val);
} else {
val_str = g_strdup ("(no value)");
}
gst_play_printf ("%s: %s = %s\n", obj_name, name, val_str);
g_free (obj_name);
g_free (val_str);
break;
}
case GST_MESSAGE_STREAM_COLLECTION:
{
GstStreamCollection *collection = NULL;
gst_message_parse_stream_collection (msg, &collection);
if (collection) {
g_mutex_lock (&play->selection_lock);
gst_object_replace ((GstObject **) & play->collection,
(GstObject *) collection);
g_mutex_unlock (&play->selection_lock);
}
break;
}
case GST_MESSAGE_STREAMS_SELECTED:
{
GstStreamCollection *collection = NULL;
guint i, len;
gst_message_parse_streams_selected (msg, &collection);
if (collection) {
g_mutex_lock (&play->selection_lock);
gst_object_replace ((GstObject **) & play->collection,
(GstObject *) collection);
/* Free all last stream-ids */
g_free (play->cur_audio_sid);
g_free (play->cur_video_sid);
g_free (play->cur_text_sid);
play->cur_audio_sid = NULL;
play->cur_video_sid = NULL;
play->cur_text_sid = NULL;
len = gst_message_streams_selected_get_size (msg);
for (i = 0; i < len; i++) {
GstStream *stream = gst_message_streams_selected_get_stream (msg, i);
if (stream) {
GstStreamType type = gst_stream_get_stream_type (stream);
const gchar *stream_id = gst_stream_get_stream_id (stream);
if (type & GST_STREAM_TYPE_AUDIO) {
play->cur_audio_sid = g_strdup (stream_id);
} else if (type & GST_STREAM_TYPE_VIDEO) {
play->cur_video_sid = g_strdup (stream_id);
} else if (type & GST_STREAM_TYPE_TEXT) {
play->cur_text_sid = g_strdup (stream_id);
} else {
gst_print ("Unknown stream type with stream-id %s", stream_id);
}
gst_object_unref (stream);
}
}
gst_object_unref (collection);
g_mutex_unlock (&play->selection_lock);
}
break;
}
default:
break;
}
return TRUE;
}
static gboolean
play_timeout (gpointer user_data)
{
GstPlay *play = user_data;
gint64 pos = -1, dur = -1;
const gchar *paused = "Paused";
gchar *status;
if (play->buffering)
return TRUE;
gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos);
gst_element_query_duration (play->playbin, GST_FORMAT_TIME, &dur);
gint len = g_utf8_strlen (paused, -1);
status = g_newa (gchar, len + 1);
memset (status, ' ', len);
status[len] = '\0';
if (pos >= 0 && dur > 0) {
gchar dstr[32], pstr[32];
/* FIXME: pretty print in nicer format */
g_snprintf (pstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (pos));
pstr[9] = '\0';
g_snprintf (dstr, 32, "%" GST_TIME_FORMAT, GST_TIME_ARGS (dur));
dstr[9] = '\0';
gst_print ("%s / %s %s\r", pstr, dstr, status);
}
return TRUE;
}
static gchar *
play_uri_get_display_name (GstPlay * play, const gchar * uri)
{
gchar *loc;
if (gst_uri_has_protocol (uri, "file")) {
loc = g_filename_from_uri (uri, NULL, NULL);
} else if (gst_uri_has_protocol (uri, "pushfile")) {
loc = g_filename_from_uri (uri + 4, NULL, NULL);
} else {
loc = g_strdup (uri);
}
/* Maybe additionally use glib's filename to display name function */
return loc;
}
static void
play_uri (GstPlay * play, const gchar * next_uri)
{
gchar *loc;
gst_element_set_state (play->playbin, GST_STATE_READY);
play_reset (play);
loc = play_uri_get_display_name (play, next_uri);
gst_print ("Now playing %s\n", loc);
g_free (loc);
g_object_set (play->playbin, "uri", next_uri, NULL);
switch (gst_element_set_state (play->playbin, GST_STATE_PAUSED)) {
case GST_STATE_CHANGE_FAILURE:
/* ignore, we should get an error message posted on the bus */
break;
case GST_STATE_CHANGE_NO_PREROLL:
gst_print ("Pipeline is live.\n");
play->is_live = TRUE;
break;
case GST_STATE_CHANGE_ASYNC:
gst_print ("Prerolling...\r");
break;
default:
break;
}
gst_element_set_state (play->playbin, GST_STATE_PLAYING);
gst_print ("checking state...\r");
GstState cur_state;
do {
gst_element_get_state (play->playbin, &cur_state, NULL, 0);
} while (cur_state <= GST_STATE_READY);
gst_print ("setting playback rate to %f...\r", play->rate);
play_set_playback_rate (play, play->rate);
}
/* returns FALSE if we have reached the end of the playlist */
static gboolean
play_next (GstPlay * play)
{
if ((play->cur_idx + 1) >= play->num_uris)
return FALSE;
play_uri (play, play->uris[++play->cur_idx]);
return TRUE;
}
/* returns FALSE if we have reached the beginning of the playlist */
static gboolean
play_prev (GstPlay * play)
{
if (play->cur_idx == 0 || play->num_uris <= 1)
return FALSE;
play_uri (play, play->uris[--play->cur_idx]);
return TRUE;
}
static void
play_about_to_finish (GstElement * playbin, gpointer user_data)
{
GstPlay *play = user_data;
const gchar *next_uri;
gchar *loc;
guint next_idx;
if (!play->gapless)
return;
next_idx = play->cur_idx + 1;
if (next_idx >= play->num_uris)
return;
next_uri = play->uris[next_idx];
loc = play_uri_get_display_name (play, next_uri);
gst_print ("About to finish, preparing next title: %s", loc);
gst_print ("\n");
g_free (loc);
g_object_set (play->playbin, "uri", next_uri, NULL);
play->cur_idx = next_idx;
}
static void
do_play (GstPlay * play)
{
gint i;
/* dump playlist */
for (i = 0; i < play->num_uris; ++i)
GST_INFO ("%4u : %s", i, play->uris[i]);
if (!play_next (play))
return;
g_main_loop_run (play->loop);
}
static gint
compare (gconstpointer a, gconstpointer b)
{
gchar *a1, *b1;
gint ret;
a1 = g_utf8_collate_key_for_filename ((gchar *) a, -1);
b1 = g_utf8_collate_key_for_filename ((gchar *) b, -1);
ret = strcmp (a1, b1);
g_free (a1);
g_free (b1);
return ret;
}
static void
add_to_playlist (GPtrArray * playlist, const gchar * filename)
{
GDir *dir;
gchar *uri;
if (gst_uri_is_valid (filename)) {
g_ptr_array_add (playlist, g_strdup (filename));
return;
}
if ((dir = g_dir_open (filename, 0, NULL))) {
const gchar *entry;
GList *l, *files = NULL;
while ((entry = g_dir_read_name (dir))) {
gchar *path;
path = g_build_filename (filename, entry, NULL);
files = g_list_insert_sorted (files, path, compare);
}
g_dir_close (dir);
for (l = files; l != NULL; l = l->next) {
gchar *path = (gchar *) l->data;
add_to_playlist (playlist, path);
g_free (path);
}
g_list_free (files);
return;
}
uri = gst_filename_to_uri (filename, NULL);
if (uri != NULL)
g_ptr_array_add (playlist, uri);
else
g_warning ("Could not make URI out of filename '%s'", filename);
}
static void
shuffle_uris (gchar ** uris, guint num)
{
gchar *tmp;
guint i, j;
if (num < 2)
return;
for (i = num - 1; i >= 1; i--) {
/* +1 because number returned will be in range [a;b[ so excl. stop */
j = g_random_int_range (0, i + 1);
tmp = uris[j];
uris[j] = uris[i];
uris[i] = tmp;
}
}
static void
toggle_paused (GstPlay * play)
{
if (play->desired_state == GST_STATE_PLAYING)
play->desired_state = GST_STATE_PAUSED;
else
play->desired_state = GST_STATE_PLAYING;
if (!play->buffering) {
gst_element_set_state (play->playbin, play->desired_state);
} else if (play->desired_state == GST_STATE_PLAYING) {
gst_print ("\nWill play as soon as buffering finishes)\n");
}
}
static void
relative_seek (GstPlay * play, gdouble percent)
{
GstQuery *query;
gboolean seekable = FALSE;
gint64 dur = -1, pos = -1, step;
g_return_if_fail (percent >= -1.0 && percent <= 1.0);
if (!gst_element_query_position (play->playbin, GST_FORMAT_TIME, &pos))
goto seek_failed;
query = gst_query_new_seeking (GST_FORMAT_TIME);
if (!gst_element_query (play->playbin, query)) {
gst_query_unref (query);
goto seek_failed;
}
gst_query_parse_seeking (query, NULL, &seekable, NULL, &dur);
gst_query_unref (query);
if (!seekable || dur <= 0)
goto seek_failed;
step = dur * percent;
if (ABS (step) < GST_SECOND)
step = (percent < 0) ? -GST_SECOND : GST_SECOND;
pos = pos + step;
if (pos > dur) {
if (!play_next (play)) {
gst_print ("\n%s\n", "Reached end of play list.");
g_main_loop_quit (play->loop);
}
} else {
if (pos < 0)
pos = 0;
play_do_seek (play, pos, play->rate, play->trick_mode);
}
return;
seek_failed:
{