-
Notifications
You must be signed in to change notification settings - Fork 1
/
player_mpris_get_info.c
608 lines (522 loc) · 22.1 KB
/
player_mpris_get_info.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include <assert.h>
#include <dbus/dbus.h>
#include "player_info_get.h"
#include "track_info.h"
#include "utils.h"
#define LOG_PREFIX "[obs_media_info] "
#include "logging.h"
#define NB_PLAYER_MAX 10
static const char MPRIS_NAME_START[] = "org.mpris.MediaPlayer2";
struct current_track_extra_info {
char* track_url;
};
static DBusConnection* dbus_connection = NULL;
static TrackInfo current_track = {0};
static struct current_track_extra_info current_track_extra = {0};
static bool playing = false;
struct track_info_has_updates {
bool has_url;
bool has_title;
bool has_art_url;
bool has_artist;
bool has_album;
};
static bool mydbus_query_players_playback_status(DBusConnection* dbus, const char* player_dbus_name);
static int decodeURIComponent (char *sSource, char *sDest) { // https://stackoverflow.com/a/20437049
assert(sSource != NULL);
assert(sDest != NULL);
int nLength;
for (nLength = 0; *sSource; nLength++) {
if (*sSource == '%' && sSource[1] && sSource[2] && isxdigit(sSource[1]) && isxdigit(sSource[2])) {
sSource[1] -= sSource[1] <= '9' ? '0' : (sSource[1] <= 'F' ? 'A' : 'a')-10;
sSource[2] -= sSource[2] <= '9' ? '0' : (sSource[2] <= 'F' ? 'A' : 'a')-10;
sDest[nLength] = 16 * sSource[1] + sSource[2];
sSource += 3;
continue;
}
sDest[nLength] = *sSource++;
}
sDest[nLength] = '\0';
return nLength;
}
#define implodeURIComponent(url) decodeURIComponent(url, url)
static char* correct_art_url(const char* url) {
assert(url != NULL);
static const char file_url[] = "file://";
size_t url_len = strlen(url);
char* ret;
if (strncmp(file_url, url, sizeof(file_url) - 1) == 0) {
ret = malloc((3 + url_len - sizeof(file_url) - 1) * sizeof(char));
allocfail_return_null(ret);
sprintf(ret, "%s", url + (sizeof(file_url) - 1));
implodeURIComponent(ret);
} else {
ret = strdup(url);
allocfail_return_null(ret);
}
return ret;
}
static bool update_data_if_diff(char** store, const char* new) {
bool ret = false;
if (new == NULL) {
if (*store != NULL) {
free(*store);
*store = NULL;
ret = true;
}
} else if (*store == NULL || estrcmp(*store, new) != 0) {
if (*store != NULL) free(*store);
*store = strdup(new);
allocfail_return_null(*store);
ret = true;
}
return ret;
}
static bool skip_if_wrong_type(DBusMessageIter* iter_go_next, DBusMessageIter* curr, int dbus_type, const char* property_name) {
bool ret = false;
int type = dbus_message_iter_get_arg_type(curr);
if (type != dbus_type) {
if (property_name != NULL) {
log_error("Error: Encontered wrong type for %s (%c, expecting %c)\n", property_name, type, dbus_type);
} else {
log_error("Error: Encontered wrong type (%c, expecting %c)\n", type, dbus_type);
}
dbus_message_iter_next(iter_go_next);
ret = true;
}
return ret;
}
static void parse_MetaData(DBusMessageIter* iter, bool* updated_data_ret, struct track_info_has_updates* has_updates) {
int current_type;
DBusMessageIter sub, subsub;
const char* property_name = NULL;
while ((current_type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID) {
switch (current_type) {
case DBUS_TYPE_DICT_ENTRY: {
dbus_message_iter_recurse(iter, &sub);
parse_MetaData(&sub, updated_data_ret, has_updates);
} break;
case DBUS_TYPE_STRING: {
dbus_message_iter_get_basic(iter, &property_name);
} break;
case DBUS_TYPE_VARIANT: {
if (property_name == NULL) {
log_error("Error: Encontered property before its name, ignoring\n");
continue;
}
if (strcmp(property_name, "xesam:title") == 0) {
char* title;
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&sub, &title);
if (update_data_if_diff(&(current_track.title), title)) {
*updated_data_ret = true;
}
has_updates->has_title = true;
} else if (strcmp(property_name, "mpris:artUrl") == 0) {
char* url;
char* correct_url;
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&sub, &url);
correct_url = correct_art_url(url);
if (update_data_if_diff(&(current_track.album_art_url), correct_url)) {
*updated_data_ret = true;
}
has_updates->has_art_url = true;
} else if (strcmp(property_name, "xesam:artist") == 0) {
char* artist;
dbus_message_iter_recurse(iter, &sub);
switch (dbus_message_iter_get_arg_type(&sub)) {
case DBUS_TYPE_ARRAY: {
dbus_message_iter_recurse(&sub, &subsub);
if (skip_if_wrong_type(iter, &subsub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&subsub, &artist);
} break;
case DBUS_TYPE_STRING: {
dbus_message_iter_get_basic(&sub, &artist);
} break;
default:
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
}
if (update_data_if_diff(&(current_track.artist), artist)) {
*updated_data_ret = true;
}
has_updates->has_artist = true;
} else if (strcmp(property_name, "xesam:album") == 0) {
char* album;
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&sub, &album);
if (update_data_if_diff(&(current_track.album), album)) {
*updated_data_ret = true;
}
has_updates->has_album = true;
} else if (strcmp(property_name, "xesam:url") == 0) {
char* url;
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&sub, &url);
if (update_data_if_diff(¤t_track_extra.track_url, url)) {
*updated_data_ret = true;
}
has_updates->has_url = true;
} else {
log_debug("ignored: %s\n", property_name);
}
} break;
default:
log_error("parse error\n");
}
dbus_message_iter_next(iter);
}
}
static void parse_array(DBusMessageIter* iter, bool* updated_data_ret, bool* updated_playing_state_ret) {
int current_type;
DBusMessageIter sub, subsub;
const char* property_name = NULL;
while ((current_type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID) {
switch (current_type) {
case DBUS_TYPE_DICT_ENTRY: {
dbus_message_iter_recurse(iter, &sub);
parse_array(&sub, updated_data_ret, updated_playing_state_ret);
} break;
case DBUS_TYPE_STRING: {
dbus_message_iter_get_basic(iter, &property_name);
log_debug("%s\n", property_name);
} break;
case DBUS_TYPE_VARIANT: {
if (property_name == NULL) {
log_error("Error: Encontered property before its name, ignoring\n");
continue;
}
if (strcmp(property_name, "PlaybackStatus") == 0) {
char* status;
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_STRING, property_name)) continue;
dbus_message_iter_get_basic(&sub, &status);
log_debug("Status: %s\n", status);
if (strcmp(status, "Playing") == 0) {
playing = true;
} else {
playing = false;
}
*updated_playing_state_ret = true;
} else if (strcmp(property_name, "Metadata") == 0) {
struct track_info_has_updates has_updates = {0};
dbus_message_iter_recurse(iter, &sub);
if (skip_if_wrong_type(iter, &sub, DBUS_TYPE_ARRAY, property_name)) continue;
dbus_message_iter_recurse(&sub, &subsub);
parse_MetaData(&subsub, updated_data_ret, &has_updates);
if (! has_updates.has_url) update_data_if_diff(&(current_track_extra.track_url) , NULL);
if (! has_updates.has_art_url) update_data_if_diff(&(current_track.album_art_url) , NULL);
if (! has_updates.has_artist) update_data_if_diff(&(current_track.artist) , NULL);
if (! has_updates.has_album) update_data_if_diff(&(current_track.album) , NULL);
if (! has_updates.has_title) {
if (has_updates.has_url) {
char* name = strrchr(current_track_extra.track_url, '/');
if (name != NULL && update_data_if_diff(&(current_track.title), name + 1)) {
*updated_data_ret = true;
}
} else {
update_data_if_diff(&(current_track.title), NULL);
}
}
} else {
log_debug("not handled %s\n", property_name);
}
} break;
default:
log_error("parse error\n");
}
dbus_message_iter_next(iter);
}
}
static DBusHandlerResult my_message_handler_mpris(DBusConnection *connection, DBusMessage *message, void *user_data) {
DBusError error;
DBusMessageIter iter, sub;
int current_type;
bool updated_data = false;
bool updated_playing_state = false;
bool initialisation_from_query = false;
dbus_error_init(&error);
const char* property_name = NULL;
const char* player = dbus_message_get_sender(message);
log_debug("\n\nreceived message from %s\n", player);
dbus_message_iter_init (message, &iter);
while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID) {
switch (current_type) {
case DBUS_TYPE_STRING: {
dbus_message_iter_get_basic(&iter, &property_name);
log_debug("%s\n", property_name);
} break;
case DBUS_TYPE_ARRAY: {
if (property_name != NULL && strcmp(property_name, "org.mpris.MediaPlayer2.Player") == 0) { // message comes from signal handler
int c = dbus_message_iter_get_element_count(&iter);
if (c == 0) break; // if empty array, skip
dbus_message_iter_recurse(&iter, &sub);
parse_array(&sub, &updated_data, &updated_playing_state);
} else if (dbus_message_get_type(message) == DBUS_MESSAGE_TYPE_METHOD_RETURN) { // message comes from mydbus_query_players
initialisation_from_query = true;
dbus_message_iter_recurse(&iter, &sub);
parse_array(&sub, &updated_data, &updated_playing_state);
}
} break;
default:
log_error("parse error\n");
}
dbus_message_iter_next(&iter);
}
if (updated_data) {
track_info_register_track_change(player, current_track);
if (! initialisation_from_query && ! updated_playing_state) { // if a track changes, some player don't update playback status
playing = mydbus_query_players_playback_status(connection, player);
updated_playing_state = true;
}
}
if (updated_playing_state) {
track_info_register_state_change(player, playing);
}
#ifdef DEBUG
track_info_print_players();
#endif
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult my_message_handler_dbus(DBusConnection *connection, DBusMessage *message, void *user_data) {
const char *name;
const char *old_name;
const char *new_name;
DBusError error;
dbus_error_init (&error);
if (!dbus_message_get_args(message, &error,
DBUS_TYPE_STRING, &name,
DBUS_TYPE_STRING, &old_name,
DBUS_TYPE_STRING, &new_name,
DBUS_TYPE_INVALID)) {
if (dbus_error_is_set(&error)) {
log_error("Error while reading new names signal (%s)\n", error.message);
dbus_error_free(&error);
}
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
bool registering_name = old_name != NULL && strlen(old_name) == 0;
if (strncmp(MPRIS_NAME_START, name, sizeof(MPRIS_NAME_START) - 1) == 0) { // if mpris name
if (registering_name) {
log_info("registration of %s as %s\n", new_name, name);
track_info_register_player(new_name, name);
} else {
log_info("unregistering of %s as %s\n", old_name, name);
track_info_unregister_player(old_name);
}
}
return DBUS_HANDLER_RESULT_HANDLED;
}
static DBusHandlerResult my_message_handler(DBusConnection *connection, DBusMessage *message, void *user_data) {
DBusHandlerResult ret = DBUS_HANDLER_RESULT_HANDLED;
const char* dest = dbus_message_get_destination(message);
const char* my_name = dbus_bus_get_unique_name(connection);
if (dest != NULL && strcmp(dest, my_name) != 0) {
log_warning("Received a message for %s I am %s, ignoring\n", dest, my_name);
return ret;
}
const char* path = dbus_message_get_path(message);
if (path == NULL) return ret;
const char* member = dbus_message_get_member(message);
if (strcmp(path, "/org/mpris/MediaPlayer2") == 0) {
ret = my_message_handler_mpris(connection, message, user_data);
} else if (strcmp(path, "/org/freedesktop/DBus") == 0 && strcmp(member, "NameOwnerChanged") == 0) {
ret = my_message_handler_dbus(connection, message, user_data);
} else {
log_warning("NOT HANDLED: message with path %s and member %s\n", path, member);
ret = DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
return ret;
}
static char* mydbus_get_name_owner(DBusConnection* dbus, const char* name) {
DBusMessage* msg, *resp;
DBusMessageIter imsg;
DBusError error;
char* ret = NULL;
dbus_error_init (&error);
msg = dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "GetNameOwner");
dbus_message_iter_init_append(msg, &imsg);
dbus_message_iter_append_basic(&imsg, DBUS_TYPE_STRING, &name);
resp = dbus_connection_send_with_reply_and_block(dbus, msg, -1, &error); //TODO: remove blocking call
if (dbus_error_is_set(&error)) {
log_error("Error while reading owner name (%s)\n", error.message);
} else {
char* resp_name;
if (! dbus_message_get_args(resp, &error,
DBUS_TYPE_STRING, &resp_name,
DBUS_TYPE_INVALID)) {
if (dbus_error_is_set(&error)) {
log_error("Error while reading owner name (%s)\n", error.message);
}
} else {
ret = strdup(resp_name);
allocfail_print(ret);
}
dbus_message_unref(resp);
}
dbus_message_unref(msg);
return ret;
}
static bool mydbus_query_players_playback_status(DBusConnection* dbus, const char* player_dbus_name) {
DBusMessage* msg;
DBusMessageIter imsg;
DBusPendingCall* resp_pending = NULL;
bool ret_playing = false;
const char* interface = "org.mpris.MediaPlayer2.Player";
const char* property = "PlaybackStatus";
msg = dbus_message_new_method_call(player_dbus_name, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "Get");
dbus_message_iter_init_append(msg, &imsg);
dbus_message_iter_append_basic(&imsg, DBUS_TYPE_STRING, &interface);
dbus_message_iter_append_basic(&imsg, DBUS_TYPE_STRING, &property);
if (! dbus_connection_send_with_reply(dbus, msg, &resp_pending, -1)) {
log_error("Out Of Memory!\n");
}
dbus_pending_call_block(resp_pending); //TODO: remove blocking call
if (dbus_pending_call_get_completed(resp_pending)) {
DBusMessage* resp;
DBusMessageIter sub, subsub;
char* playback_status;
resp = dbus_pending_call_steal_reply(resp_pending);
dbus_message_iter_init(resp, &sub);
if (dbus_message_iter_get_arg_type(&sub) == DBUS_TYPE_VARIANT) {
dbus_message_iter_recurse(&sub, &subsub);
if (! skip_if_wrong_type(&sub, &subsub, DBUS_TYPE_STRING, "PlayerStatus *Query*")) {
dbus_message_iter_get_basic(&subsub, &playback_status);
if (strcmp(playback_status, "Playing") == 0) {
ret_playing = true;
} else {
ret_playing = false;
}
}
} else {
log_error("type error in PlayerStatus *Query*");
}
dbus_message_unref(resp);
}
dbus_message_unref(msg);
return ret_playing;
}
static void mydbus_query_players(DBusConnection* dbus, char* player_dbus_name) {
DBusMessage* msg;
DBusMessageIter imsg;
DBusPendingCall* resp_pending = NULL;
const char* interface = "org.mpris.MediaPlayer2.Player";
msg = dbus_message_new_method_call(player_dbus_name, "/org/mpris/MediaPlayer2", "org.freedesktop.DBus.Properties", "GetAll");
dbus_message_iter_init_append(msg, &imsg);
dbus_message_iter_append_basic(&imsg, DBUS_TYPE_STRING, &interface);
if (! dbus_connection_send_with_reply(dbus, msg, &resp_pending, -1)) {
log_error("Out Of Memory!\n");
}
dbus_pending_call_block(resp_pending); //TODO: remove blocking call
if (dbus_pending_call_get_completed(resp_pending)) {
DBusMessage* resp;
resp = dbus_pending_call_steal_reply(resp_pending);
my_message_handler_mpris(dbus, resp, NULL);
dbus_message_unref(resp);
}
dbus_message_unref(msg);
}
static void mydbus_register_names(DBusConnection* dbus)
{
DBusMessage* msg;
DBusPendingCall* resp_pending = NULL;
msg = dbus_message_new_method_call("org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus", "ListNames");
if (! dbus_connection_send_with_reply(dbus, msg, &resp_pending, -1)) {
log_error("Out Of Memory!\n");
}
dbus_pending_call_block(resp_pending); //TODO: remove blocking call
if (dbus_pending_call_get_completed(resp_pending)) {
DBusMessage* resp;
resp = dbus_pending_call_steal_reply(resp_pending);
int current_type;
DBusMessageIter iter, iter2;
dbus_message_iter_init (resp, &iter);
if (dbus_message_iter_get_arg_type(&iter) == DBUS_TYPE_ARRAY) {
dbus_message_iter_recurse(&iter, &iter2); // Array of String
while ((current_type = dbus_message_iter_get_arg_type (&iter2)) != DBUS_TYPE_INVALID) {
if (current_type == DBUS_TYPE_STRING) {
char* name;
dbus_message_iter_get_basic(&iter2, &name);
if (strncmp(MPRIS_NAME_START, name, sizeof(MPRIS_NAME_START) - 1) == 0) { // if mpris name
char* unique_name = mydbus_get_name_owner(dbus, name);
track_info_register_player(unique_name, name);
mydbus_query_players(dbus, unique_name);
free(unique_name);
}
}
dbus_message_iter_next(&iter2);
}
}
dbus_message_unref(resp);
}
dbus_message_unref(msg);
}
static DBusConnection* mydbus_init_session()
{
DBusConnection *cbus = NULL;
DBusError error;
dbus_error_init(&error);
cbus = dbus_bus_get(DBUS_BUS_SESSION, &error);
if (dbus_error_is_set(&error)) {
log_error("Error getting Bus: %s\n", error.message);
dbus_error_free(&error);
return NULL;
}
if (cbus == NULL) {
log_error("Error getting Bus: cbus is null\n");
return NULL;
}
dbus_connection_set_exit_on_disconnect(cbus, false);
log_info("My dbus name is %s\n", dbus_bus_get_unique_name(cbus));
return cbus;
}
static void mydbus_add_matches(DBusConnection* dbus) {
DBusError error;
dbus_error_init (&error);
dbus_bus_add_match(dbus, "type='signal', interface='org.freedesktop.DBus.Properties',member='PropertiesChanged', path='/org/mpris/MediaPlayer2'", &error);
if (dbus_error_is_set(&error)) {
log_error("Error while adding match (%s)\n", error.message);
dbus_error_free(&error);
}
dbus_bus_add_match(dbus, "type='signal', interface='org.freedesktop.DBus', member='NameOwnerChanged', path='/org/freedesktop/DBus'", &error);
if (dbus_error_is_set(&error)) {
log_error("Error while adding match (%s)\n", error.message);
dbus_error_free(&error);
}
dbus_connection_add_filter(dbus, my_message_handler, NULL, dbus_free);
dbus_connection_flush(dbus);
}
void player_info_init() {
log_info("Initialising");
dbus_connection = mydbus_init_session();
if (dbus_connection == NULL) {
return;
}
mydbus_add_matches(dbus_connection);
track_info_init();
mydbus_register_names(dbus_connection);
}
int player_info_process() {
DBusDispatchStatus remains;
if (dbus_connection == NULL) {
return 0;
}
do {
dbus_connection_read_write_dispatch(dbus_connection, 500);
remains = dbus_connection_get_dispatch_status(dbus_connection);
if (remains == DBUS_DISPATCH_NEED_MEMORY) {
log_warning("Need more memory to read dbus messages\n");
}
} while (remains != DBUS_DISPATCH_COMPLETE);
return 0;
}