-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathmpris.c
1143 lines (974 loc) · 39.8 KB
/
mpris.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
#include <gio/gio.h>
#include <glib-unix.h>
#include <mpv/client.h>
#include <libavformat/avformat.h>
#include <inttypes.h>
#include <string.h>
static const char *introspection_xml =
"<node>\n"
" <interface name=\"org.mpris.MediaPlayer2\">\n"
" <method name=\"Raise\">\n"
" </method>\n"
" <method name=\"Quit\">\n"
" </method>\n"
" <property name=\"CanQuit\" type=\"b\" access=\"read\"/>\n"
" <property name=\"Fullscreen\" type=\"b\" access=\"readwrite\"/>\n"
" <property name=\"CanSetFullscreen\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanRaise\" type=\"b\" access=\"read\"/>\n"
" <property name=\"HasTrackList\" type=\"b\" access=\"read\"/>\n"
" <property name=\"Identity\" type=\"s\" access=\"read\"/>\n"
" <property name=\"DesktopEntry\" type=\"s\" access=\"read\"/>\n"
" <property name=\"SupportedUriSchemes\" type=\"as\" access=\"read\"/>\n"
" <property name=\"SupportedMimeTypes\" type=\"as\" access=\"read\"/>\n"
" </interface>\n"
" <interface name=\"org.mpris.MediaPlayer2.Player\">\n"
" <method name=\"Next\">\n"
" </method>\n"
" <method name=\"Previous\">\n"
" </method>\n"
" <method name=\"Pause\">\n"
" </method>\n"
" <method name=\"PlayPause\">\n"
" </method>\n"
" <method name=\"Stop\">\n"
" </method>\n"
" <method name=\"Play\">\n"
" </method>\n"
" <method name=\"Seek\">\n"
" <arg type=\"x\" name=\"Offset\" direction=\"in\"/>\n"
" </method>\n"
" <method name=\"SetPosition\">\n"
" <arg type=\"o\" name=\"TrackId\" direction=\"in\"/>\n"
" <arg type=\"x\" name=\"Offset\" direction=\"in\"/>\n"
" </method>\n"
" <method name=\"OpenUri\">\n"
" <arg type=\"s\" name=\"Uri\" direction=\"in\"/>\n"
" </method>\n"
" <signal name=\"Seeked\">\n"
" <arg type=\"x\" name=\"Position\" direction=\"out\"/>\n"
" </signal>\n"
" <property name=\"PlaybackStatus\" type=\"s\" access=\"read\"/>\n"
" <property name=\"LoopStatus\" type=\"s\" access=\"readwrite\"/>\n"
" <property name=\"Rate\" type=\"d\" access=\"readwrite\"/>\n"
" <property name=\"Shuffle\" type=\"b\" access=\"readwrite\"/>\n"
" <property name=\"Metadata\" type=\"a{sv}\" access=\"read\"/>\n"
" <property name=\"Volume\" type=\"d\" access=\"readwrite\"/>\n"
" <property name=\"Position\" type=\"x\" access=\"read\"/>\n"
" <property name=\"MinimumRate\" type=\"d\" access=\"read\"/>\n"
" <property name=\"MaximumRate\" type=\"d\" access=\"read\"/>\n"
" <property name=\"CanGoNext\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanGoPrevious\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanPlay\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanPause\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanSeek\" type=\"b\" access=\"read\"/>\n"
" <property name=\"CanControl\" type=\"b\" access=\"read\"/>\n"
" </interface>\n"
"</node>\n";
typedef struct UserData
{
mpv_handle *mpv;
GMainLoop *loop;
gint bus_id;
GDBusConnection *connection;
GDBusInterfaceInfo *root_interface_info;
GDBusInterfaceInfo *player_interface_info;
guint root_interface_id;
guint player_interface_id;
const char *status;
const char *loop_status;
GHashTable *changed_properties;
GVariant *metadata;
gboolean seek_expected;
gboolean idle;
gboolean paused;
} UserData;
static const char *STATUS_PLAYING = "Playing";
static const char *STATUS_PAUSED = "Paused";
static const char *STATUS_STOPPED = "Stopped";
static const char *LOOP_NONE = "None";
static const char *LOOP_TRACK = "Track";
static const char *LOOP_PLAYLIST = "Playlist";
static gchar *string_to_utf8(gchar *maybe_utf8)
{
gchar *attempted_validation;
attempted_validation = g_utf8_make_valid(maybe_utf8, -1);
if (g_utf8_validate(attempted_validation, -1, NULL)) {
return attempted_validation;
} else {
g_free(attempted_validation);
return g_strdup("<invalid utf8>");
}
}
static void add_metadata_item_string(mpv_handle *mpv, GVariantDict *dict,
const char *property, const char *tag)
{
char *temp = mpv_get_property_string(mpv, property);
if (temp) {
char *utf8 = string_to_utf8(temp);
g_variant_dict_insert(dict, tag, "s", utf8);
g_free(utf8);
mpv_free(temp);
}
}
static void add_metadata_item_int(mpv_handle *mpv, GVariantDict *dict,
const char *property, const char *tag)
{
int64_t value;
int res = mpv_get_property(mpv, property, MPV_FORMAT_INT64, &value);
if (res >= 0) {
g_variant_dict_insert(dict, tag, "x", value);
}
}
static void add_metadata_item_string_list(mpv_handle *mpv, GVariantDict *dict,
const char *property, const char *tag)
{
char *temp = mpv_get_property_string(mpv, property);
if (temp) {
GVariantBuilder builder;
char **list = g_strsplit(temp, ", ", 0);
char **iter = list;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
for (; *iter; iter++) {
char *item = *iter;
char *utf8 = string_to_utf8(item);
g_variant_builder_add(&builder, "s", utf8);
g_free(utf8);
}
g_variant_dict_insert(dict, tag, "as", &builder);
g_strfreev(list);
mpv_free(temp);
}
}
static gchar *path_to_uri(mpv_handle *mpv, char *path)
{
#if GLIB_CHECK_VERSION(2, 58, 0)
// version which uses g_canonicalize_filename which expands .. and .
// and makes the uris neater
char* working_dir;
gchar* canonical;
gchar *uri;
working_dir = mpv_get_property_string(mpv, "working-directory");
canonical = g_canonicalize_filename(path, working_dir);
uri = g_filename_to_uri(canonical, NULL, NULL);
mpv_free(working_dir);
g_free(canonical);
return uri;
#else
// for compatibility with older versions of glib
gchar *converted;
if (g_path_is_absolute(path)) {
converted = g_filename_to_uri(path, NULL, NULL);
} else {
char* working_dir;
gchar* absolute;
working_dir = mpv_get_property_string(mpv, "working-directory");
absolute = g_build_filename(working_dir, path, NULL);
converted = g_filename_to_uri(absolute, NULL, NULL);
mpv_free(working_dir);
g_free(absolute);
}
return converted;
#endif
}
static void add_metadata_uri(mpv_handle *mpv, GVariantDict *dict)
{
char *path;
char *uri;
path = mpv_get_property_string(mpv, "path");
if (!path) {
return;
}
uri = g_uri_parse_scheme(path);
if (uri) {
g_variant_dict_insert(dict, "xesam:url", "s", path);
g_free(uri);
} else {
gchar *converted = path_to_uri(mpv, path);
g_variant_dict_insert(dict, "xesam:url", "s", converted);
g_free(converted);
}
mpv_free(path);
}
// Copied from https://github.com/videolan/vlc/blob/master/modules/meta_engine/folder.c
static const char art_files[][20] = {
"Folder.jpg", /* Windows */
"Folder.png",
"AlbumArtSmall.jpg", /* Windows */
"AlbumArt.jpg", /* Windows */
"Album.jpg",
".folder.png", /* KDE? */
"cover.jpg", /* rockbox */
"cover.png",
"cover.gif",
"front.jpg",
"front.png",
"front.gif",
"front.bmp",
"thumb.jpg",
};
static const int art_files_count = sizeof(art_files) / sizeof(art_files[0]);
static gchar* try_get_local_art(mpv_handle *mpv, char *path)
{
gchar *dirname = g_path_get_dirname(path), *out = NULL;
gboolean found = FALSE;
for (int i = 0; i < art_files_count; i++) {
gchar *filename = g_build_filename(dirname, art_files[i], NULL);
if (g_file_test(filename, G_FILE_TEST_EXISTS)) {
out = path_to_uri(mpv, filename);
found = TRUE;
}
g_free(filename);
if (found) {
break;
}
}
g_free(dirname);
return out;
}
static const char *youtube_url_pattern =
"^https?:\\/\\/(?:youtu.be\\/|(?:www\\.)?youtube\\.com\\/watch\\?v=)(?<id>[a-zA-Z0-9_-]*)\\??.*";
static GRegex *youtube_url_regex;
static gchar* try_get_youtube_thumbnail(char *path)
{
gchar *out = NULL;
if (!youtube_url_regex) {
youtube_url_regex = g_regex_new(youtube_url_pattern, 0, 0, NULL);
}
GMatchInfo *match_info;
gboolean matched = g_regex_match(youtube_url_regex, path, 0, &match_info);
if (matched) {
gchar *video_id = g_match_info_fetch_named(match_info, "id");
out = g_strconcat("https://i1.ytimg.com/vi/",
video_id, "/hqdefault.jpg", NULL);
g_free(video_id);
}
g_match_info_free(match_info);
return out;
}
static gchar* extract_embedded_art(AVFormatContext *context) {
AVPacket *packet = NULL;
for (unsigned int i = 0; i < context->nb_streams; i++) {
if (context->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC) {
packet = &context->streams[i]->attached_pic;
}
}
if (!packet) {
return NULL;
}
gchar *data = g_base64_encode(packet->data, packet->size);
gchar *img = g_strconcat("data:image/jpeg;base64,", data, NULL);
g_free(data);
return img;
}
static gchar* try_get_embedded_art(char *path)
{
gchar *out = NULL;
AVFormatContext *context = NULL;
if (!avformat_open_input(&context, path, NULL, NULL)) {
out = extract_embedded_art(context);
avformat_close_input(&context);
}
return out;
}
// cached last file path, owned by mpv
static char *cached_path = NULL;
// cached last artwork url, owned by glib
static gchar *cached_art_url = NULL;
static void add_metadata_art(mpv_handle *mpv, GVariantDict *dict)
{
char *path = mpv_get_property_string(mpv, "path");
if (!path) {
return;
}
// mpv may call create_metadata multiple times, so cache to save CPU
if (!cached_path || strcmp(path, cached_path)) {
mpv_free(cached_path);
g_free(cached_art_url);
cached_path = path;
if (g_str_has_prefix(path, "http")) {
cached_art_url = try_get_youtube_thumbnail(path);
} else {
cached_art_url = try_get_embedded_art(path);
if (!cached_art_url) {
cached_art_url = try_get_local_art(mpv, path);
}
}
} else {
mpv_free(path);
}
if (cached_art_url) {
g_variant_dict_insert(dict, "mpris:artUrl", "s", cached_art_url);
}
}
static void add_metadata_content_created(mpv_handle *mpv, GVariantDict *dict)
{
char *date_str = mpv_get_property_string(mpv, "metadata/by-key/Date");
if (!date_str) {
return;
}
GDate* date = g_date_new();
if (strlen(date_str) == 4) {
gint64 year = g_ascii_strtoll(date_str, NULL, 10);
if (year != 0) {
g_date_set_dmy(date, 1, 1, year);
}
} else {
g_date_set_parse(date, date_str);
}
if (g_date_valid(date)) {
gchar iso8601[20];
g_date_strftime(iso8601, 20, "%Y-%m-%dT00:00:00Z", date);
g_variant_dict_insert(dict, "xesam:contentCreated", "s", iso8601);
}
g_date_free(date);
mpv_free(date_str);
}
static GVariant *create_metadata(UserData *ud)
{
GVariantDict dict;
int64_t track;
double duration;
char *temp_str;
int res;
g_variant_dict_init(&dict, NULL);
// mpris:trackid
mpv_get_property(ud->mpv, "playlist-pos", MPV_FORMAT_INT64, &track);
// playlist-pos < 0 if there is no playlist or current track
if (track < 0) {
temp_str = g_strdup("/noplaylist");
} else {
temp_str = g_strdup_printf("/%" PRId64, track);
}
g_variant_dict_insert(&dict, "mpris:trackid", "o", temp_str);
g_free(temp_str);
// mpris:length
res = mpv_get_property(ud->mpv, "duration", MPV_FORMAT_DOUBLE, &duration);
if (res == MPV_ERROR_SUCCESS) {
g_variant_dict_insert(&dict, "mpris:length", "x", (int64_t)(duration * 1000000.0));
}
// initial value. Replaced with metadata value if available
add_metadata_item_string(ud->mpv, &dict, "media-title", "xesam:title");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/Title", "xesam:title");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/Album", "xesam:album");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/Genre", "xesam:genre");
/* Musicbrainz metadata mappings
(https://picard-docs.musicbrainz.org/en/appendices/tag_mapping.html) */
// IDv3 metadata format
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Artist Id", "mb:artistId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Track Id", "mb:trackId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Album Artist Id", "mb:albumArtistId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Album Id", "mb:albumId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Release Track Id", "mb:releaseTrackId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MusicBrainz Work Id", "mb:workId");
// Vorbis & APEv2 metadata format
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_ARTISTID", "mb:artistId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_TRACKID", "mb:trackId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_ALBUMARTISTID", "mb:albumArtistId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_ALBUMID", "mb:albumId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_RELEASETRACKID", "mb:releaseTrackId");
add_metadata_item_string(ud->mpv, &dict, "metadata/by-key/MUSICBRAINZ_WORKID", "mb:workId");
add_metadata_item_string_list(ud->mpv, &dict, "metadata/by-key/uploader", "xesam:artist");
add_metadata_item_string_list(ud->mpv, &dict, "metadata/by-key/Artist", "xesam:artist");
add_metadata_item_string_list(ud->mpv, &dict, "metadata/by-key/Album_Artist", "xesam:albumArtist");
add_metadata_item_string_list(ud->mpv, &dict, "metadata/by-key/Composer", "xesam:composer");
add_metadata_item_int(ud->mpv, &dict, "metadata/by-key/Track", "xesam:trackNumber");
add_metadata_item_int(ud->mpv, &dict, "metadata/by-key/Disc", "xesam:discNumber");
add_metadata_uri(ud->mpv, &dict);
add_metadata_art(ud->mpv, &dict);
add_metadata_content_created(ud->mpv, &dict);
return g_variant_dict_end(&dict);
}
static void method_call_root(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *object_path,
G_GNUC_UNUSED const char *interface_name,
const char *method_name,
G_GNUC_UNUSED GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
if (g_strcmp0(method_name, "Quit") == 0) {
const char *cmd[] = {"quit", NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Raise") == 0) {
// Can't raise
g_dbus_method_invocation_return_value(invocation, NULL);
} else {
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_METHOD,
"Unknown method");
}
}
static GVariant *get_property_root(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *object_path,
G_GNUC_UNUSED const char *interface_name,
const char *property_name,
G_GNUC_UNUSED GError **error,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
GVariant *ret;
if (g_strcmp0(property_name, "CanQuit") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "Fullscreen") == 0) {
int fullscreen;
mpv_get_property(ud->mpv, "fullscreen", MPV_FORMAT_FLAG, &fullscreen);
ret = g_variant_new_boolean(fullscreen);
} else if (g_strcmp0(property_name, "CanSetFullscreen") == 0) {
int can_fullscreen;
mpv_get_property(ud->mpv, "vo-configured", MPV_FORMAT_FLAG, &can_fullscreen);
ret = g_variant_new_boolean(can_fullscreen);
} else if (g_strcmp0(property_name, "CanRaise") == 0) {
ret = g_variant_new_boolean(FALSE);
} else if (g_strcmp0(property_name, "HasTrackList") == 0) {
ret = g_variant_new_boolean(FALSE);
} else if (g_strcmp0(property_name, "Identity") == 0) {
ret = g_variant_new_string("mpv");
} else if (g_strcmp0(property_name, "DesktopEntry") == 0) {
ret = g_variant_new_string("mpv");
} else if (g_strcmp0(property_name, "SupportedUriSchemes") == 0) {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
g_variant_builder_add(&builder, "s", "ftp");
g_variant_builder_add(&builder, "s", "http");
g_variant_builder_add(&builder, "s", "https");
g_variant_builder_add(&builder, "s", "mms");
g_variant_builder_add(&builder, "s", "rtmp");
g_variant_builder_add(&builder, "s", "rtsp");
g_variant_builder_add(&builder, "s", "sftp");
g_variant_builder_add(&builder, "s", "smb");
ret = g_variant_builder_end(&builder);
} else if (g_strcmp0(property_name, "SupportedMimeTypes") == 0) {
GVariantBuilder builder;
g_variant_builder_init(&builder, G_VARIANT_TYPE("as"));
g_variant_builder_add(&builder, "s", "application/ogg");
g_variant_builder_add(&builder, "s", "audio/mpeg");
// TODO add the rest
ret = g_variant_builder_end(&builder);
} else {
ret = NULL;
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_PROPERTY,
"Unknown property %s", property_name);
}
return ret;
}
static gboolean set_property_root(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *object_path,
G_GNUC_UNUSED const char *interface_name,
const char *property_name,
GVariant *value,
G_GNUC_UNUSED GError **error,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
if (g_strcmp0(property_name, "Fullscreen") == 0) {
int fullscreen;
g_variant_get(value, "b", &fullscreen);
mpv_set_property(ud->mpv, "fullscreen", MPV_FORMAT_FLAG, &fullscreen);
} else {
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_PROPERTY,
"Cannot set property %s", property_name);
return FALSE;
}
return TRUE;
}
static GDBusInterfaceVTable vtable_root = {
method_call_root, get_property_root, set_property_root, {0}
};
static void method_call_player(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *_object_path,
G_GNUC_UNUSED const char *interface_name,
const char *method_name,
G_GNUC_UNUSED GVariant *parameters,
GDBusMethodInvocation *invocation,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
if (g_strcmp0(method_name, "Pause") == 0) {
int paused = TRUE;
mpv_set_property(ud->mpv, "pause", MPV_FORMAT_FLAG, &paused);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "PlayPause") == 0) {
int paused;
if (ud->status == STATUS_PAUSED) {
paused = FALSE;
} else {
paused = TRUE;
}
mpv_set_property(ud->mpv, "pause", MPV_FORMAT_FLAG, &paused);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Play") == 0) {
int paused = FALSE;
mpv_set_property(ud->mpv, "pause", MPV_FORMAT_FLAG, &paused);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Stop") == 0) {
const char *cmd[] = {"stop", NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Next") == 0) {
const char *cmd[] = {"playlist_next", NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Previous") == 0) {
const char *cmd[] = {"playlist_prev", NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "Seek") == 0) {
int64_t offset_us; // in microseconds
char *offset_str;
g_variant_get(parameters, "(x)", &offset_us);
double offset_s = offset_us / 1000000.0;
offset_str = g_strdup_printf("%f", offset_s);
const char *cmd[] = {"seek", offset_str, NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
g_free(offset_str);
} else if (g_strcmp0(method_name, "SetPosition") == 0) {
int64_t current_id;
char *object_path;
double new_position_s;
int64_t new_position_us;
mpv_get_property(ud->mpv, "playlist-pos", MPV_FORMAT_INT64, ¤t_id);
g_variant_get(parameters, "(&ox)", &object_path, &new_position_us);
new_position_s = ((float)new_position_us) / 1000000.0; // us -> s
if (current_id == g_ascii_strtoll(object_path + 1, NULL, 10)) {
mpv_set_property(ud->mpv, "time-pos", MPV_FORMAT_DOUBLE, &new_position_s);
}
g_dbus_method_invocation_return_value(invocation, NULL);
} else if (g_strcmp0(method_name, "OpenUri") == 0) {
char *uri;
g_variant_get(parameters, "(&s)", &uri);
const char *cmd[] = {"loadfile", uri, NULL};
mpv_command_async(ud->mpv, 0, cmd);
g_dbus_method_invocation_return_value(invocation, NULL);
} else {
g_dbus_method_invocation_return_error(invocation, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_METHOD,
"Unknown method");
}
}
static GVariant *get_property_player(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *object_path,
G_GNUC_UNUSED const char *interface_name,
const char *property_name,
GError **error,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
GVariant *ret;
if (g_strcmp0(property_name, "PlaybackStatus") == 0) {
ret = g_variant_new_string(ud->status);
} else if (g_strcmp0(property_name, "LoopStatus") == 0) {
ret = g_variant_new_string(ud->loop_status);
} else if (g_strcmp0(property_name, "Rate") == 0) {
double rate;
mpv_get_property(ud->mpv, "speed", MPV_FORMAT_DOUBLE, &rate);
ret = g_variant_new_double(rate);
} else if (g_strcmp0(property_name, "Shuffle") == 0) {
int shuffle;
mpv_get_property(ud->mpv, "playlist-shuffle", MPV_FORMAT_FLAG, &shuffle);
ret = g_variant_new_boolean(shuffle);
} else if (g_strcmp0(property_name, "Metadata") == 0) {
if (!ud->metadata) {
ud->metadata = create_metadata(ud);
}
// Increase reference count to prevent it from being freed after returning
g_variant_ref(ud->metadata);
ret = ud->metadata;
} else if (g_strcmp0(property_name, "Volume") == 0) {
double volume;
mpv_get_property(ud->mpv, "volume", MPV_FORMAT_DOUBLE, &volume);
volume /= 100;
ret = g_variant_new_double(volume);
} else if (g_strcmp0(property_name, "Position") == 0) {
double position_s;
int64_t position_us;
mpv_get_property(ud->mpv, "time-pos", MPV_FORMAT_DOUBLE, &position_s);
position_us = position_s * 1000000.0; // s -> us
ret = g_variant_new_int64(position_us);
} else if (g_strcmp0(property_name, "MinimumRate") == 0) {
ret = g_variant_new_double(0.01);
} else if (g_strcmp0(property_name, "MaximumRate") == 0) {
ret = g_variant_new_double(100);
} else if (g_strcmp0(property_name, "CanGoNext") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "CanGoPrevious") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "CanPlay") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "CanPause") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "CanSeek") == 0) {
ret = g_variant_new_boolean(TRUE);
} else if (g_strcmp0(property_name, "CanControl") == 0) {
ret = g_variant_new_boolean(TRUE);
} else {
ret = NULL;
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_PROPERTY,
"Unknown property %s", property_name);
}
return ret;
}
static gboolean set_property_player(G_GNUC_UNUSED GDBusConnection *connection,
G_GNUC_UNUSED const char *sender,
G_GNUC_UNUSED const char *object_path,
G_GNUC_UNUSED const char *interface_name,
const char *property_name,
GVariant *value,
G_GNUC_UNUSED GError **error,
gpointer user_data)
{
UserData *ud = (UserData*)user_data;
if (g_strcmp0(property_name, "LoopStatus") == 0) {
const char *status;
int t = TRUE;
int f = FALSE;
status = g_variant_get_string(value, NULL);
if (g_strcmp0(status, "Track") == 0) {
mpv_set_property(ud->mpv, "loop-file", MPV_FORMAT_FLAG, &t);
mpv_set_property(ud->mpv, "loop-playlist", MPV_FORMAT_FLAG, &f);
} else if (g_strcmp0(status, "Playlist") == 0) {
mpv_set_property(ud->mpv, "loop-file", MPV_FORMAT_FLAG, &f);
mpv_set_property(ud->mpv, "loop-playlist", MPV_FORMAT_FLAG, &t);
} else {
mpv_set_property(ud->mpv, "loop-file", MPV_FORMAT_FLAG, &f);
mpv_set_property(ud->mpv, "loop-playlist", MPV_FORMAT_FLAG, &f);
}
} else if (g_strcmp0(property_name, "Rate") == 0) {
double rate = g_variant_get_double(value);
mpv_set_property(ud->mpv, "speed", MPV_FORMAT_DOUBLE, &rate);
} else if (g_strcmp0(property_name, "Shuffle") == 0) {
int shuffle = g_variant_get_boolean(value);
mpv_set_property(ud->mpv, "playlist-shuffle", MPV_FORMAT_FLAG, &shuffle);
} else if (g_strcmp0(property_name, "Volume") == 0) {
double volume = g_variant_get_double(value);
volume *= 100;
mpv_set_property(ud->mpv, "volume", MPV_FORMAT_DOUBLE, &volume);
} else {
g_set_error(error, G_DBUS_ERROR,
G_DBUS_ERROR_UNKNOWN_PROPERTY,
"Cannot set property %s", property_name);
return FALSE;
}
return TRUE;
}
static GDBusInterfaceVTable vtable_player = {
method_call_player, get_property_player, set_property_player, {0}
};
static gboolean emit_property_changes(gpointer data)
{
UserData *ud = (UserData*)data;
GError *error = NULL;
gpointer prop_name, prop_value;
GHashTableIter iter;
if (g_hash_table_size(ud->changed_properties) > 0) {
GVariant *params;
GVariantBuilder *properties = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
GVariantBuilder *invalidated = g_variant_builder_new(G_VARIANT_TYPE("as"));
g_hash_table_iter_init(&iter, ud->changed_properties);
while (g_hash_table_iter_next(&iter, &prop_name, &prop_value)) {
if (prop_value) {
g_variant_builder_add(properties, "{sv}", prop_name, prop_value);
} else {
g_variant_builder_add(invalidated, "s", prop_name);
}
}
params = g_variant_new("(sa{sv}as)",
"org.mpris.MediaPlayer2.Player", properties, invalidated);
g_variant_builder_unref(properties);
g_variant_builder_unref(invalidated);
g_dbus_connection_emit_signal(ud->connection, NULL,
"/org/mpris/MediaPlayer2",
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
params, &error);
if (error != NULL) {
g_printerr("%s", error->message);
}
g_hash_table_remove_all(ud->changed_properties);
}
return TRUE;
}
static void emit_seeked_signal(UserData *ud)
{
GVariant *params;
double position_s;
int64_t position_us;
GError *error = NULL;
mpv_get_property(ud->mpv, "time-pos", MPV_FORMAT_DOUBLE, &position_s);
position_us = position_s * 1000000.0; // s -> us
params = g_variant_new("(x)", position_us);
g_dbus_connection_emit_signal(ud->connection, NULL,
"/org/mpris/MediaPlayer2",
"org.mpris.MediaPlayer2.Player",
"Seeked",
params, &error);
if (error != NULL) {
g_printerr("%s", error->message);
}
}
static GVariant * set_playback_status(UserData *ud)
{
if (ud->idle) {
ud->status = STATUS_STOPPED;
} else if (ud->paused) {
ud->status = STATUS_PAUSED;
} else {
ud->status = STATUS_PLAYING;
}
return g_variant_new_string(ud->status);
}
static void set_stopped_status(UserData *ud)
{
const char *prop_name = "PlaybackStatus";
GVariant *prop_value = g_variant_new_string(STATUS_STOPPED);
ud->status = STATUS_STOPPED;
g_hash_table_insert(ud->changed_properties,
(gpointer)prop_name, prop_value);
emit_property_changes(ud);
}
// Register D-Bus object and interfaces
static void on_bus_acquired(GDBusConnection *connection,
G_GNUC_UNUSED const char *name,
gpointer user_data)
{
GError *error = NULL;
UserData *ud = user_data;
ud->connection = connection;
ud->root_interface_id =
g_dbus_connection_register_object(connection, "/org/mpris/MediaPlayer2",
ud->root_interface_info,
&vtable_root,
user_data, NULL, &error);
if (error != NULL) {
g_printerr("%s", error->message);
}
ud->player_interface_id =
g_dbus_connection_register_object(connection, "/org/mpris/MediaPlayer2",
ud->player_interface_info,
&vtable_player,
user_data, NULL, &error);
if (error != NULL) {
g_printerr("%s", error->message);
}
}
static void on_name_lost(GDBusConnection *connection,
G_GNUC_UNUSED const char *_name,
gpointer user_data)
{
if (connection) {
UserData *ud = user_data;
pid_t pid = getpid();
char *name = g_strdup_printf("org.mpris.MediaPlayer2.mpv.instance%d", pid);
ud->bus_id = g_bus_own_name(G_BUS_TYPE_SESSION,
name,
G_BUS_NAME_OWNER_FLAGS_NONE,
NULL, NULL, NULL,
&ud, NULL);
g_free(name);
}
}
static void handle_property_change(const char *name, void *data, UserData *ud)
{
const char *prop_name = NULL;
GVariant *prop_value = NULL;
if (g_strcmp0(name, "pause") == 0) {
ud->paused = *(int*)data;
prop_name = "PlaybackStatus";
prop_value = set_playback_status(ud);
} else if (g_strcmp0(name, "idle-active") == 0) {
ud->idle = *(int*)data;
prop_name = "PlaybackStatus";
prop_value = set_playback_status(ud);
} else if (g_strcmp0(name, "media-title") == 0 ||
g_strcmp0(name, "duration") == 0) {
// Free existing metadata object
if (ud->metadata) {
g_variant_unref(ud->metadata);
}
ud->metadata = create_metadata(ud);
prop_name = "Metadata";
prop_value = ud->metadata;
} else if (g_strcmp0(name, "speed") == 0) {
double *rate = data;
prop_name = "Rate";
prop_value = g_variant_new_double(*rate);
} else if (g_strcmp0(name, "volume") == 0) {
double *volume = data;
*volume /= 100;
prop_name = "Volume";
prop_value = g_variant_new_double(*volume);
} else if (g_strcmp0(name, "loop-file") == 0) {
char *status = *(char **)data;
if (g_strcmp0(status, "no") != 0) {
ud->loop_status = LOOP_TRACK;
} else {
char *playlist_status;
mpv_get_property(ud->mpv, "loop-playlist", MPV_FORMAT_STRING, &playlist_status);
if (g_strcmp0(playlist_status, "no") != 0) {
ud->loop_status = LOOP_PLAYLIST;
} else {
ud->loop_status = LOOP_NONE;
}
mpv_free(playlist_status);
}
prop_name = "LoopStatus";
prop_value = g_variant_new_string(ud->loop_status);
} else if (g_strcmp0(name, "loop-playlist") == 0) {
char *status = *(char **)data;
if (g_strcmp0(status, "no") != 0) {
ud->loop_status = LOOP_PLAYLIST;
} else {
char *file_status;
mpv_get_property(ud->mpv, "loop-file", MPV_FORMAT_STRING, &file_status);
if (g_strcmp0(file_status, "no") != 0) {
ud->loop_status = LOOP_TRACK;
} else {
ud->loop_status = LOOP_NONE;
}
mpv_free(file_status);
}
prop_name = "LoopStatus";
prop_value = g_variant_new_string(ud->loop_status);
} else if (g_strcmp0(name, "fullscreen") == 0) {
gboolean *status = data;
prop_name = "Fullscreen";
prop_value = g_variant_new_boolean(*status);
}
if (prop_name) {
if (prop_value) {
g_variant_ref(prop_value);