-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cpp
3049 lines (2851 loc) · 105 KB
/
player.cpp
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 "player.h"
#include <fcntl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <unistd.h>
#include <thread>
/**
* Converts bool to const char*
*
*@param b (type: bool)
*@return "true" if b is true or "false" otherwise
*/
inline const char *const bool_to_string(bool b) { return b ? "true" : "false"; }
#ifdef HAVE_DBUS
void Player::update_position_thread() {
if (get_is_playing()) {
double current_pos = get_position(); // get current pos
Helper::get_instance().log("Current pos: " + std::to_string(current_pos) +
", " + get_position_str());
}
send_info_to_clients();
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // wait 1 sec
}
#endif
void Player::server_thread() {
// Create a socket
int serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == -1) {
Helper::get_instance().log("SOCKET: Failed to create socket");
return;
}
// Set the socket to non-blocking mode
if (fcntl(serverSocket, F_SETFL, O_NONBLOCK) == -1) {
Helper::get_instance().log(
"SOCKET: Failed to set socket to non-blocking mode");
close(serverSocket);
return;
}
// Bind the socket to a specific IP address and port
sockaddr_in serverAddress{};
serverAddress.sin_family = AF_INET;
serverAddress.sin_addr.s_addr =
INADDR_ANY; // Listen on all network interfaces
serverAddress.sin_port = htons(4308); // Port number
bool isBinded = false;
while (!isBinded) {
if (bind(serverSocket, reinterpret_cast<sockaddr *>(&serverAddress),
sizeof(serverAddress)) == -1) {
Helper::get_instance().log(
"SOCKET: Failed to bind socket, trying again after 10 seconds...");
// close(serverSocket);
std::this_thread::sleep_for(
std::chrono::seconds(10)); // Wait for 10 seconds
} else {
isBinded = true; // Socket is successfully bound, exit the loop
}
}
// Listen for incoming connections
if (listen(serverSocket, 10) == -1) {
Helper::get_instance().log("SOCKET: Failed to listen on socket");
close(serverSocket);
return;
}
Helper::get_instance().log(
"SOCKET: Server started. Listening for connections...");
// Accept incoming connections and handle them
while (serverRunning) {
sockaddr_in clientAddress{};
socklen_t clientAddressLength = sizeof(clientAddress);
// Accept a client connection
int clientSocket =
accept(serverSocket, reinterpret_cast<sockaddr *>(&clientAddress),
&clientAddressLength);
if (clientSocket == -1) {
// Check if the error is due to non-blocking socket and no connection is
// pending
if (errno == EWOULDBLOCK || errno == EAGAIN) {
// Sleep for a short duration to avoid busy looping
usleep(1000); // 1 millisecond
continue;
} else {
Helper::get_instance().log(
"SOCKET: Failed to accept client connection");
close(serverSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
return;
}
}
// Handle the client request
clients.push_back(clientSocket);
while (serverRunning) {
// Set up the timeout for recv()
struct timeval timeout;
timeout.tv_sec = 2; // Set the timeout value in seconds
timeout.tv_usec = 0;
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(clientSocket, &readSet);
int selectResult =
select(clientSocket + 1, &readSet, nullptr, nullptr, &timeout);
if (selectResult == -1) {
Helper::get_instance().log("SOCKET: Select failed");
close(clientSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
break;
} else if (selectResult == 0) {
Helper::get_instance().log(
"SOCKET: Timeout occurred. Closing the client connection.");
close(clientSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
break;
}
char received[2048] = {0};
std::string receivedStr;
ssize_t bytesRead =
recv(clientSocket, &received, sizeof(received) - 1, 0);
received[bytesRead] = '\0';
receivedStr = std::string(received);
memset(&(received[0]), 0, 2048);
if (bytesRead == -1) {
Helper::get_instance().log("SOCKET: Failed to read from client socket");
close(clientSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
break;
} else if (bytesRead == 0) {
// Client disconnected
Helper::get_instance().log("SOCKET: Client disconnected");
close(clientSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
break;
} else {
// Process the received byte
// int received = ntohl(buffer);
// received = ntohl(received);
int operation_code = Helper::get_instance().getOPCode(receivedStr);
if (operation_code == 400 || operation_code == 40)
operation_code =
4; // At startup in some reason receives "400" instead of "4"
if (operation_code != 0)
Helper::get_instance().log("Received: " + receivedStr);
switch (operation_code) {
case 0: {
// std::cout << "Received byte: 0 (Testing connection)" <<
// std::endl;
break;
}
case 1: {
Helper::get_instance().log("SOCKET: Received byte: 1 (Previous)");
send_previous();
break;
}
case 2: {
Helper::get_instance().log("SOCKET: Received byte: 2 (PlayPause)");
send_play_pause();
break;
}
case 3: {
Helper::get_instance().log("SOCKET: Received byte: 3 (Next)");
send_next();
break;
}
case 4: {
Helper::get_instance().log("SOCKET: Received byte: 4 (Get)");
send_info_to_clients();
break;
}
case 5: {
Helper::get_instance().log(
"SOCKET: Received byte: 5 (Toggle Shuffle)");
set_shuffle(!get_shuffle());
break;
}
case 6: {
Helper::get_instance().log(
"SOCKET: Received byte: 6 (Toggle Repeat)");
int current_loop_status = get_repeat(); // get current loop status
if (current_loop_status + 1 == 3) { // if it last status
set_repeat(0); // go to 0 status
} else {
set_repeat(current_loop_status + 1); // go to next status
}
break;
}
case 7: {
Helper::get_instance().log("SOCKET: Received byte: 7 (Set position)");
std::string digits = receivedStr.substr(3);
int newPos;
try {
newPos = std::stoi(digits);
} catch (std::invalid_argument) {
Helper::get_instance().log(
"Error while setting position! Can't cast \"" + digits +
"\" to int.");
}
Helper::get_instance().log("Fetched position " + digits);
set_position(newPos);
break;
}
case 8: { // Get players. Need to send status 8, count of players and
// player:id pairs.
Helper::get_instance().log("SOCKET: Received byte: 8 (Get players)");
auto players = get_players();
uint64_t selected = get_current_player_index();
std::string result = "8||" + std::to_string(selected);
for (const auto &player : players) {
if (player.first == "Local")
result += "||" + player.first + "||Local";
else
result += "||" + player.first + "||" + player.second;
}
for (int client : clients) {
ssize_t bytesSent = send(client, result.c_str(), result.size(), 0);
if (bytesSent == -1) {
Helper::get_instance().log(
"Failed to send message to the client " +
std::to_string(client));
} else {
Helper::get_instance().log("Sent " + std::to_string(bytesSent) +
" bytes to the client " +
std::to_string(client));
}
}
break;
}
case 9: { // change player. Desired input format: "9||playerIndex"
Helper::get_instance().log("SOCKET: Received byte: 9 (Set player)");
// Find the position of "9||" in the input string
std::string playerID = receivedStr.substr(3);
uint64_t index;
try {
index = std::stoi(playerID);
} catch (std::invalid_argument) {
Helper::get_instance().log(
"Error while setting player! Can't cast \"" + playerID +
"\" to int.");
}
select_player(index);
if (m_players[index].first == "Local") {
notify_observers_player_choosed(true);
} else
notify_observers_player_choosed(false);
break;
}
case 10: {
// get list of output devices: devicename||sinkid
Helper::get_instance().log(
"SOCKET: Received byte: 10 (Get output devices)");
auto devices = get_output_devices();
uint64_t selected = get_current_device_sink_index();
std::string result = "9||" + std::to_string(selected);
for (const auto &device : devices) {
result +=
"||" + device.first + "||" + std::to_string(device.second);
}
for (int client : clients) {
ssize_t bytesSent = send(client, result.c_str(), result.size(), 0);
if (bytesSent == -1) {
Helper::get_instance().log(
"Failed to send message to the client " +
std::to_string(client));
} else {
Helper::get_instance().log("Sent " + std::to_string(bytesSent) +
" bytes to the client " +
std::to_string(client));
}
}
break;
}
case 11: { // change output device. Desired input format:
// "11||sinkIndex"
Helper::get_instance().log(
"SOCKET: Received byte: 11 (Set output device)");
std::string deviceID = receivedStr.substr(4);
uint64_t index;
try {
index = std::stoi(deviceID);
} catch (std::invalid_argument) {
Helper::get_instance().log(
"Error while setting output device! Can't cast \"" + deviceID +
"\" to int.");
}
set_output_device(index);
break;
}
case 12: { // change volume. Desired input format: "12||newVolume"
Helper::get_instance().log("SOCKET: Received byte: 12 (Set volume)");
std::string volume = receivedStr.substr(4);
double newVolume;
try {
newVolume = std::stod(volume);
} catch (std::invalid_argument) {
Helper::get_instance().log(
"Error while setting volume! Can't cast \"" + volume +
"\" to double.");
}
set_volume(newVolume);
break;
}
default: {
Helper::get_instance().log("SOCKET: Received unknown byte: " +
std::to_string(operation_code));
break;
}
}
}
}
close(clientSocket);
clients.erase(std::remove(clients.begin(), clients.end(), clientSocket),
clients.end());
}
// Close the server socket
close(serverSocket);
}
void Player::send_info_to_clients() {
if (!clients.empty()) {
std::string current_info = "";
auto metadata = get_metadata();
for (const auto &data : metadata) {
if (data.first == "mpris:artUrl") {
if (data.second != "")
current_info += "art||" + data.second + "||";
else
current_info += "art||-||";
} else if (data.first == "xesam:artist") {
if (data.second != "")
current_info += "artist||" + data.second + "||";
else
current_info += "artist||-||";
} else if (data.first == "xesam:title") {
if (data.second != "")
current_info += "title||" + data.second + "||";
else
current_info += "title||-||";
}
}
std::string length = get_song_length_str();
if (length == "")
length = "0:00";
std::string position = get_position_str();
if (position == "")
position = "0:00";
current_info += "length||" + length + "||";
current_info += "pos||" + position + "||";
current_info += "playing||" + std::to_string(get_is_playing()) + "||";
current_info += "shuffle||" + std::to_string(get_shuffle()) + "||";
current_info += "repeat||" + std::to_string(get_repeat()) + "||";
current_info += "volume||" + std::to_string(get_volume()) + "||";
for (int client : clients) {
ssize_t bytesSent =
send(client, current_info.c_str(), current_info.size(), 0);
if (bytesSent == -1) {
Helper::get_instance().log("Failed to send message to the client " +
std::to_string(client));
} else {
Helper::get_instance().log("Sent " + std::to_string(bytesSent) +
" bytes to the client " +
std::to_string(client));
}
}
}
}
Player::Player(bool with_gui) {
// init neccessary variables
m_with_gui = with_gui;
m_song_title = "";
m_song_artist = "";
m_song_length_str = "";
m_song_pos = 0;
m_song_length = 0;
m_is_shuffle = false;
m_is_playing = false;
m_song_volume = 0;
#ifdef HAVE_DBUS
// create dbus connection
m_dbus_conn = sdbus::createSessionBusConnection();
if (m_dbus_conn) // check connection and print info about connection on
// success
Helper::get_instance().log("Connected to D-Bus as \"" +
m_dbus_conn->getUniqueName() + "\".");
#endif
#ifdef HAVE_DBUS
start_server();
#endif
// get current players
get_players();
// if players size is not null
if (m_players.size() != 0) {
// then select first accessible player
if (select_player(0)) {
if (m_players[m_selected_player_id].first == "Local") {
Helper::get_instance().log("Selected local player.");
} else {
Helper::get_instance().log(
"Selected player: " + m_players[m_selected_player_id].first +
" at " + m_players[m_selected_player_id].second);
std::this_thread::sleep_for(
std::chrono::milliseconds(500)); // wait 0.5 sec
get_song_data(); // get song data from dbus if player is not local
}
};
} else {
// while(m_players.size() <= 0 || !serverRunning) {
// //no players found
// std::this_thread::sleep_for(
// std::chrono::milliseconds(5000)); // wait 5 sec
// get_players();
// }
if (m_players.size() > 0 && select_player(0)) {
if (m_players[m_selected_player_id].first == "Local") {
Helper::get_instance().log("Selected local player.");
} else {
Helper::get_instance().log(
"Selected player: " + m_players[m_selected_player_id].first +
" at " + m_players[m_selected_player_id].second);
std::this_thread::sleep_for(
std::chrono::milliseconds(500)); // wait 0.5 sec
get_song_data(); // get song data from dbus if player is not local
}
}
}
#ifdef SUPPORT_AUDIO_OUTPUT
// if we can use audio output then initialize SDL
if(m_with_gui) {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
std::cerr << "SDL initialization failed: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
if (Mix_OpenAudio(44100, AUDIO_S16SYS, 2, 4096) < 0) {
std::cerr << "Mix_OpenAudio failed: " << Mix_GetError() << std::endl;
exit(EXIT_FAILURE);
}
}
#endif
}
Player::~Player() {
#ifdef HAVE_DBUS
// release proxy
m_proxy_signal.reset();
#endif
#ifdef SUPPORT_AUDIO_OUTPUT
// free music from mix
Mix_FreeMusic(m_current_music);
// close audio output device
Mix_CloseAudio();
// quit from SDL
SDL_Quit();
#endif
}
std::vector<std::pair<std::string, std::string>> Player::get_players() {
m_players.clear(); // clear m_players vector
#ifdef SUPPORT_AUDIO_OUTPUT
// if we can play local audio, then add local player
if (m_with_gui)
m_players.push_back(std::make_pair("Local", ""));
#endif
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't get players. Aborting.");
// if we cant get list of players via DBus then just return local player if
// added
return m_players;
}
std::vector<std::string> result_of_call; // vector of all ListNames of Dbus
try {
// creating proxy for getting ListNames
auto proxy = sdbus::createProxy(*m_dbus_conn.get(), "org.freedesktop.DBus",
"/org/freedesktop/DBus");
proxy->callMethod("ListNames")
.onInterface("org.freedesktop.DBus")
.storeResultsTo(result_of_call); // and storing inside vector
} catch (const sdbus::Error &e) {
// if error - print it
Helper::get_instance().log(
std::string("Error while getting all DBus services: ") + e.what());
return m_players;
}
Helper::get_instance().log("Started checking for players");
if (result_of_call.empty()) { // if vector empty - stop parsing
Helper::get_instance().log("Error getting reply.");
return m_players;
} else {
Helper::get_instance().log("Got reply from DBus");
int num_names = 0;
for (const auto &name : result_of_call) { // check every name
if (strstr(name.c_str(), "org.mpris.MediaPlayer2.") ==
name.c_str()) { // if it implements org.mpris.MediaPlayer2
Helper::get_instance().log("Found media player: " +
name); // print that we found media player
std::string identity; // string for saving name of this player
try {
auto proxy =
sdbus::createProxy(*m_dbus_conn.get(), name,
"/org/mpris/MediaPlayer2"); // create new proxy
sdbus::Variant v_identity;
proxy->callMethod("Get")
.onInterface("org.freedesktop.DBus.Properties")
.withArguments("org.mpris.MediaPlayer2",
"Identity") // get identity of new player
.storeResultsTo(
v_identity); // save it into identity variant variable
identity =
v_identity.get<std::string>(); // parse std::string from variable
} catch (const sdbus::Error &e) {
Helper::get_instance().log(
std::string("Error while getting Identity: ") +
e.what()); // if error while getting player name
m_players.push_back(std::make_pair(
"Player", name)); // then just add interface with "Player" name
return m_players;
}
Helper::get_instance().log("Identity: " +
identity); // print identity of player
m_players.push_back(std::make_pair(
identity, name)); // add name and interface to m_players vector
}
}
}
// if no players found and local not accessible
if (m_players.empty()) {
Helper::get_instance().log("No media players found.");
return {};
}
#endif
return m_players;
}
void Player::print_players() {
for (auto &player : m_players) {
Helper::get_instance().log(player.first + ": " + player.second);
}
}
void Player::print_players_names() {
for (auto &player : m_players) {
Helper::get_instance().log(player.first);
}
}
bool Player::select_player(unsigned int new_id) {
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't get players. Aborting.");
return false;
}
#endif
get_players(); // get list of currently accessible players
if (new_id < 0 || new_id > m_players.size()) { // if new_id out of bounds
Helper::get_instance().log("This player does not exists!");
return false; // cancel operation
}
m_selected_player_id = new_id; // set new player
#ifdef SUPPORT_AUDIO_OUTPUT
if (m_players[m_selected_player_id].first == "Local") { // if it is local
// then just say that all methods and properties are supported
m_play_pause_method = true;
m_pause_method = true;
m_play_method = true;
m_next_method = true;
m_previous_method = true;
m_setpos_method = true;
m_is_shuffle_prop = true;
m_is_pos_prop = true;
m_is_volume_prop = true;
m_is_playback_status_prop = true;
m_is_metadata_prop = true;
m_is_repeat_prop = true;
#ifdef HAVE_DBUS
// if local player, we must stop listening signals from dbus
stop_listening_signals();
#endif
return true;
} else {
// if we switched from local player to DBus, we need to pause audio on local
// player
pause_audio();
}
#endif
#ifdef HAVE_DBUS
std::string
xml_introspect; // string, which will contain xml of all interfaces,
// properties and methods of out player
try {
auto proxy = sdbus::createProxy(*m_dbus_conn.get(),
m_players[m_selected_player_id].second,
"/org/mpris/MediaPlayer2");
proxy
->callMethod("Introspect") // call introspect method
.onInterface("org.freedesktop.DBus.Introspectable")
.storeResultsTo(xml_introspect); // and store info in xml_introspect
} catch (const sdbus::Error &e) {
Helper::get_instance().log(
std::string("Error while trying to get all functions for player: ") +
e.what());
}
// start parsing and checking
pugi::xml_document doc;
pugi::xml_parse_result result = doc.load_string(xml_introspect.c_str());
if (!result) {
Helper::get_instance().log(std::string("Failed to parse XML: ") +
result.description());
return false;
}
// Find the root node
pugi::xml_node root = doc.document_element();
if (!root) {
Helper::get_instance().log("Error: no root element");
return 1;
}
pugi::xml_node iface_node =
doc.select_node(
(std::string(
"/node/interface[@name='org.mpris.MediaPlayer2.Player']")
.c_str()))
.node();
// Searching for play_pause method...
Helper::get_instance().log("Searching for PlayPause method... ", false);
pugi::xml_node methodNode =
iface_node.find_child_by_attribute("method", "name", "PlayPause");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_play_pause_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_play_pause_method = false;
}
// Searching for pause method...
Helper::get_instance().log("Searching for Pause method... ", false);
methodNode = iface_node.find_child_by_attribute("method", "name", "Pause");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_pause_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_pause_method = false;
}
// Searching for Play method
Helper::get_instance().log("Searching for Play method... ", false);
methodNode = iface_node.find_child_by_attribute("method", "name", "Play");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_play_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_play_method = false;
}
// Searching for Next method
Helper::get_instance().log("Searching for Next method... ", false);
methodNode = iface_node.find_child_by_attribute("method", "name", "Next");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_next_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_next_method = false;
}
// Searching for Previous method
Helper::get_instance().log("Searching for Previous method... ", false);
methodNode = iface_node.find_child_by_attribute("method", "name", "Previous");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_previous_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_previous_method = false;
}
// Searching for SetPosition method
Helper::get_instance().log("Searching for SetPosition method... ", false);
methodNode =
iface_node.find_child_by_attribute("method", "name", "SetPosition");
if (methodNode) {
// Method found
Helper::get_instance().log("found.", true, false);
m_setpos_method = true;
} else {
// Method not found
Helper::get_instance().log("not found.", true, false);
m_setpos_method = false;
}
// Searching for Shuffle property
Helper::get_instance().log("Searching for Shuffle property... ", false);
pugi::xml_node propertyNode =
iface_node.find_child_by_attribute("property", "name", "Shuffle");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_shuffle_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_shuffle_prop = false;
}
// Searching for Position property
Helper::get_instance().log("Searching for Position property... ", false);
propertyNode =
iface_node.find_child_by_attribute("property", "name", "Position");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_pos_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_pos_prop = false;
}
// Searching for Volume property
Helper::get_instance().log("Searching for Volume property... ", false);
propertyNode =
iface_node.find_child_by_attribute("property", "name", "Volume");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_volume_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_volume_prop = false;
}
// Searching for PlaybackStatus property
Helper::get_instance().log("Searching for PlaybackStatus property... ",
false);
propertyNode =
iface_node.find_child_by_attribute("property", "name", "PlaybackStatus");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_playback_status_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_playback_status_prop = false;
}
// Searching for Metadata property
Helper::get_instance().log("Searching for Metadata property... ", false);
propertyNode =
iface_node.find_child_by_attribute("property", "name", "Metadata");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_metadata_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_metadata_prop = false;
}
// Searching for LoopStatus property
Helper::get_instance().log("Searching for LoopStatus property... ", false);
propertyNode =
iface_node.find_child_by_attribute("property", "name", "LoopStatus");
if (propertyNode) {
// Property found
Helper::get_instance().log("found.", true, false);
m_is_repeat_prop = true;
} else {
// Property not found
Helper::get_instance().log("not found.", true, false);
m_is_repeat_prop = false;
}
get_song_data();
start_listening_signals();
#endif
return true;
}
bool Player::send_play_pause() {
if (m_selected_player_id < 0 || m_selected_player_id > m_players.size()) {
Helper::get_instance().log("Player not selected, can't continue.");
return false;
}
#ifdef SUPPORT_AUDIO_OUTPUT
if (m_players[m_selected_player_id].first == "Local") { // if local player
if (Mix_PlayingMusic() && Mix_PausedMusic()) { // if music opened and paused
play_audio(); // just play
} else if (Mix_PlayingMusic() &&
!Mix_PausedMusic()) { // if opened and playing
pause_audio(); // just pause
} else { // in any other variant
Helper::get_instance().log("Starting playing");
play_audio(); // just play
}
return true; // success
}
#endif
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't send PlayPause. Aborting.");
return false;
}
if (!m_play_pause_method) {
Helper::get_instance().log(
"This player does not compatible with PlayPause method!");
return false;
}
try {
auto proxy = sdbus::createProxy(*m_dbus_conn.get(),
m_players[m_selected_player_id].second,
"/org/mpris/MediaPlayer2");
proxy
->callMethod("PlayPause") // call PlayPause method
.onInterface("org.mpris.MediaPlayer2.Player")
.dontExpectReply();
return true;
} catch (const sdbus::Error &e) {
Helper::get_instance().log(std::string(
std::string("Error while trying call PlayPause method: ") + e.what()));
return false;
}
#endif
return false;
}
bool Player::send_pause() {
if (m_selected_player_id < 0 || m_selected_player_id > m_players.size()) {
Helper::get_instance().log("Player not selected, can't continue.");
return false;
}
#ifdef SUPPORT_AUDIO_OUTPUT
if (m_players[m_selected_player_id].first == "Local") {
pause_audio(); // if local player then just pause audio
return true;
}
#endif
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't send Pause. Aborting.");
return false;
}
if (!m_pause_method) {
Helper::get_instance().log(
"This player does not compatible with Pause method!");
return false;
}
try {
auto proxy = sdbus::createProxy(*m_dbus_conn.get(),
m_players[m_selected_player_id].second,
"/org/mpris/MediaPlayer2");
proxy
->callMethod("Pause") // call Pause method to DBus
.onInterface("org.mpris.MediaPlayer2.Player")
.dontExpectReply();
return true;
} catch (const sdbus::Error &e) {
Helper::get_instance().log("Error while trying call Pause method: ");
return false;
}
#endif
return false;
}
bool Player::send_play() {
if (m_selected_player_id < 0 || m_selected_player_id > m_players.size()) {
Helper::get_instance().log("Player not selected, can't continue.");
return false;
}
#ifdef SUPPORT_AUDIO_OUTPUT
if (m_players[m_selected_player_id].first == "Local") {
play_audio(); // if local player then just play
return true;
}
#endif
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't send Play. Aborting.");
return false;
}
if (!m_play_method) {
Helper::get_instance().log(
"This player does not compatible with Play method!");
return false;
}
try {
auto proxy = sdbus::createProxy(*m_dbus_conn.get(),
m_players[m_selected_player_id].second,
"/org/mpris/MediaPlayer2");
proxy
->callMethod("Play") // call Play method to DBus
.onInterface("org.mpris.MediaPlayer2.Player")
.dontExpectReply();
return true;
} catch (const sdbus::Error &e) {
Helper::get_instance().log(
std::string("Error while trying call Play method: ") + e.what());
return false;
}
#endif
return false;
}
bool Player::send_next() {
if (m_selected_player_id < 0 || m_selected_player_id > m_players.size()) {
Helper::get_instance().log("Player not selected, can't continue.");
return false;
}
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't send Next. Aborting.");
return false;
}
if (!m_next_method) {
Helper::get_instance().log(
"This player does not compatible with Next method!");
return false;
}
try {
auto proxy = sdbus::createProxy(*m_dbus_conn.get(),
m_players[m_selected_player_id].second,
"/org/mpris/MediaPlayer2");
proxy
->callMethod("Next") // call Next method to DBus
.onInterface("org.mpris.MediaPlayer2.Player")
.dontExpectReply();
return true;
} catch (const sdbus::Error &e) {
Helper::get_instance().log(
std::string("Error while trying call Next method: ") + e.what());
return false;
}
#endif
return false;
}
bool Player::send_previous() {
if (m_selected_player_id < 0 || m_selected_player_id > m_players.size()) {
Helper::get_instance().log("Player not selected, can't continue.");
return false;
}
#ifdef HAVE_DBUS
if (!m_dbus_conn) {
Helper::get_instance().log(
"Not connected to DBus, can't send Previous. Aborting.");
return false;