-
Notifications
You must be signed in to change notification settings - Fork 2
/
player.c
1252 lines (1109 loc) · 34.3 KB
/
player.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
//////////////////////////////////////////////////////////////////////////////
/// ///
/// This file is part of the VDR mpv plugin and licensed under AGPLv3 ///
/// ///
/// See the README file for copyright information ///
/// ///
//////////////////////////////////////////////////////////////////////////////
#include <locale.h>
#include <string>
#include <vector>
#include <vdr/plugin.h>
#include "player.h"
#include "config.h"
#include "osd.h"
#ifdef USE_XRANDR
#include <X11/extensions/Xrandr.h>
#endif
#ifdef USE_DRM
#include <xf86drmMode.h>
#endif
using std::vector;
#define MPV_OBSERVE_TIME_POS 1
#define MPV_OBSERVE_DISC_MENU 2
#define MPV_OBSERVE_FPS 3
#define MPV_OBSERVE_FILENAME 4
#define MPV_OBSERVE_LENGTH 5
#define MPV_OBSERVE_CHAPTERS 6
#define MPV_OBSERVE_CHAPTER 7
#define MPV_OBSERVE_PAUSE 8
#define MPV_OBSERVE_SPEED 9
#define MPV_OBSERVE_MEDIA_TITLE 10
#define MPV_OBSERVE_LIST_POS 11
#define MPV_OBSERVE_LIST_COUNT 12
#define MPV_OBSERVE_VIA_NET 13
#define MPV_OBSERVE_TRACK_LIST 14
volatile int cMpvPlayer::running = 0;
cMpvPlayer *cMpvPlayer::PlayerHandle = NULL;
std::string LocaleSave;
int drm_ctx = 0;
const char *drm_dev = NULL;
Display *Dpy = NULL;
xcb_connection_t *Connect = NULL;
xcb_window_t VideoWindow = 0;
xcb_pixmap_t pixmap = XCB_NONE;
xcb_cursor_t cursor = XCB_NONE;
int is_softhddevice = 0;
#ifdef __cplusplus
extern "C"
{
#endif
extern void FeedKeyPress(const char *, const char *, int, int, const char *);
extern void RemoteStart();
extern void RemoteStop();
#ifdef __cplusplus
}
#endif
// check mpv errors and send them to log
static inline void check_error(int status)
{
if (status < 0)
{
esyslog("[mpv] API error: %s\n", mpv_error_string(status));
}
}
void set_deinterlace(mpv_handle *h)
{
if (strstr(MpvPluginConfig->HwDec.c_str(),"vaapi"))
{
check_error(mpv_set_option_string(h, "vf", "vavpp=deint=auto"));
}
else if (strstr(MpvPluginConfig->HwDec.c_str(),"vdpau"))
{
check_error(mpv_set_option_string(h, "vf", "vdpaupp=deint=yes:deint-mode=temporal-spatial"));
}
else if (strstr(MpvPluginConfig->HwDec.c_str(),"cuda"))
{
check_error(mpv_set_option_string(h, "vd-lavc-o", "deint=adaptive"));
}
else
{
check_error(mpv_set_option_string(h, "deinterlace", "yes"));
}
}
void *cMpvPlayer::XEventThread(void *handle)
{
XEvent event;
KeySym keysym;
const char *keynam;
char buf[64];
char letter[64];
int letter_len;
static Time clicktime;
static bool toggle;
cMpvPlayer *Player = (cMpvPlayer*) handle;
while (Player->PlayerIsRunning())
{
if(Dpy && Connect && VideoWindow) {
XWindowEvent(Dpy, VideoWindow, KeyPressMask|ButtonPressMask|StructureNotifyMask|SubstructureNotifyMask, &event);
switch (event.type) {
case ButtonPress:
if (is_softhddevice) {
if (event.xbutton.button == 1) {
Time difftime = event.xbutton.time - clicktime;
if (difftime < 500) {
check_error(mpv_set_option_string(Player->hMpv, "fullscreen", toggle ? "yes" : "no"));
toggle = !toggle;
}
clicktime = event.xbutton.time;
}
else if (event.xbutton.button == 2) {
FeedKeyPress("XKeySym", "Ok", 0, 0, NULL);
}
else if (event.xbutton.button == 3) {
FeedKeyPress("XKeySym", "Menu", 0, 0, NULL);
}
if (event.xbutton.button == 4) {
FeedKeyPress("XKeySym", "Volume+", 0, 0, NULL);
}
if (event.xbutton.button == 5) {
FeedKeyPress("XKeySym", "Volume-", 0, 0, NULL);
}
} else {
check_error(mpv_set_option_string(Player->hMpv, "fullscreen", toggle ? "yes" : "no"));
toggle = !toggle;
}
break;
case ButtonRelease:
break;
case KeyPress:
letter_len =
XLookupString(&event.xkey, letter, sizeof(letter) - 1, &keysym, NULL);
if (letter_len < 0) {
letter_len = 0;
}
letter[letter_len] = '\0';
if (keysym == NoSymbol) {
dsyslog("video/event: No symbol for %d\n", event.xkey.keycode);
break;
}
keynam = XKeysymToString(keysym);
// check for key modifiers (Alt/Ctrl)
if (event.xkey.state & (Mod1Mask | ControlMask)) {
if (event.xkey.state & Mod1Mask) {
strcpy(buf, "Alt+");
} else {
buf[0] = '\0';
}
if (event.xkey.state & ControlMask) {
strcat(buf, "Ctrl+");
}
strncat(buf, keynam, sizeof(buf) - 10);
keynam = buf;
}
FeedKeyPress("XKeySym", keynam, 0, 0, letter);
break;
case ConfigureNotify:
Player->windowWidth = event.xconfigure.width;
Player->windowHeight = event.xconfigure.height;
Player->windowX = event.xconfigure.x;
Player->windowY = event.xconfigure.y;
Player->OsdClose();
break;
default:
break;
}
}
usleep(1000);
}
#ifdef DEBUG
dsyslog("[mpv] XEvent thread ended\n");
#endif
return handle;
}
void *cMpvPlayer::ObserverThread(void *handle)
{
cMpvPlayer *Player = (cMpvPlayer*) handle;
struct mpv_event_log_message *msg;
// set properties which should be observed
mpv_observe_property(Player->hMpv, MPV_OBSERVE_TIME_POS, "time-pos", MPV_FORMAT_DOUBLE);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_DISC_MENU, "disc-menu-active", MPV_FORMAT_FLAG);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_FPS, "container-fps", MPV_FORMAT_DOUBLE);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_FILENAME, "filename", MPV_FORMAT_STRING);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_LENGTH, "duration", MPV_FORMAT_DOUBLE);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_CHAPTERS, "chapters", MPV_FORMAT_INT64);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_CHAPTER, "chapter", MPV_FORMAT_INT64);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_PAUSE, "pause", MPV_FORMAT_FLAG);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_SPEED, "speed", MPV_FORMAT_DOUBLE);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_MEDIA_TITLE, "media-title", MPV_FORMAT_STRING);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_LIST_POS, "playlist-pos-1", MPV_FORMAT_INT64);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_LIST_COUNT, "playlist-count", MPV_FORMAT_INT64);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_VIA_NET, "demuxer-via-network", MPV_FORMAT_FLAG);
mpv_observe_property(Player->hMpv, MPV_OBSERVE_TRACK_LIST, "track-list", MPV_FORMAT_NODE);
while (Player->PlayerIsRunning())
{
mpv_event *event = mpv_wait_event(Player->hMpv, 5);
switch (event->event_id)
{
case MPV_EVENT_SHUTDOWN :
Player->running = 0;
break;
case MPV_EVENT_PROPERTY_CHANGE :
Player->HandlePropertyChange(event);
break;
case MPV_EVENT_PLAYBACK_RESTART :
Player->ChangeFrameRate(Player->CurrentFps()); // switching directly after the fps event causes black screen
Player->PlayerIdle = 0;
if (MpvPluginConfig->UseDeinterlace && !Player->Image()) set_deinterlace(Player->hMpv);
break;
case MPV_EVENT_LOG_MESSAGE :
msg = (struct mpv_event_log_message *)event->data;
// without DEBUG log to error since we only request error messages from mpv in this case
#ifdef DEBUG
dsyslog("[mpv]: %s\n", msg->text);
#else
esyslog("[mpv]: %s\n", msg->text);
#endif
break;
#if MPV_CLIENT_API_VERSION < MPV_MAKE_VERSION(2,0)
case MPV_EVENT_TRACKS_CHANGED :
Player->HandleTracksChange();
break;
#endif
case MPV_EVENT_VIDEO_RECONFIG :
if(!drm_ctx) {
Player->PlayerGetWindow("- mpv", &Connect, VideoWindow, Player->windowWidth, Player->windowHeight, Player->windowX, Player->windowY);
Player->PlayerHideCursor();
}
#ifdef USE_DRM
else
Player->PlayerGetDRM();
#endif
break;
case MPV_EVENT_IDLE :
if (Player->PlayerIdle != -1)
Player->PlayerIdle = 1;
break;
case MPV_EVENT_NONE :
case MPV_EVENT_END_FILE :
#if MPV_CLIENT_API_VERSION < MPV_MAKE_VERSION(2,0)
case MPV_EVENT_PAUSE :
case MPV_EVENT_UNPAUSE :
case MPV_EVENT_TRACK_SWITCHED :
case MPV_EVENT_SCRIPT_INPUT_DISPATCH :
case MPV_EVENT_METADATA_UPDATE :
case MPV_EVENT_CHAPTER_CHANGE :
#endif
case MPV_EVENT_FILE_LOADED :
case MPV_EVENT_GET_PROPERTY_REPLY :
case MPV_EVENT_SET_PROPERTY_REPLY :
case MPV_EVENT_COMMAND_REPLY :
case MPV_EVENT_START_FILE :
case MPV_EVENT_TICK :
case MPV_EVENT_CLIENT_MESSAGE :
case MPV_EVENT_AUDIO_RECONFIG :
case MPV_EVENT_SEEK :
default :
dsyslog("[mpv]: event: %d %s\n", event->event_id, mpv_event_name(event->event_id));
break;
}
}
#ifdef DEBUG
dsyslog("[mpv] Observer thread ended\n");
#endif
return handle;
}
cMpvPlayer::cMpvPlayer(string Filename, bool Shuffle)
:cPlayer(pmExtern_THIS_SHOULD_BE_AVOIDED)
{
PlayerHandle = this;
PlayFilename = Filename;
PlayShuffle = Shuffle;
running = 0;
OriginalFps = -1;
PlayerRecord = 0;
ObserverThreadHandle = 0;
XEventThreadHandle = 0;
}
cMpvPlayer::~cMpvPlayer()
{
#ifdef DEBUG
dsyslog("[mpv]%s: end\n", __FUNCTION__);
#endif
Detach();
PlayerHandle = NULL;
}
void cMpvPlayer::Activate(bool on)
{
if (on)
PlayerStart();
}
void cMpvPlayer::SetAudioTrack(eTrackType Type, const tTrackId *TrackId)
{
SetAudio(TrackId->id);
}
void cMpvPlayer::SetSubtitleTrack(eTrackType Type, const tTrackId *TrackId)
{
if (Type == ttNone)
{
check_error(mpv_set_option_string(hMpv, "sub-forced-only", "yes"));
return;
}
check_error(mpv_set_option_string(hMpv, "sub-forced-only", "no"));
SetSubtitle(TrackId->id);
}
bool cMpvPlayer::GetReplayMode(bool &Play, bool &Forward, int &Speed)
{
Speed = CurrentPlaybackSpeed();
if (Speed == 1)
Speed = -1;
Forward = true;
Play = !IsPaused();
return true;
}
bool cMpvPlayer::GetIndex(int& Current, int& Total, bool SnapToIFrame __attribute__((unused)))
{
Total = TotalPlayTime() * FramesPerSecond();
Current = CurrentPlayTime() * FramesPerSecond();
return true;
}
double cMpvPlayer::FramesPerSecond()
{
return CurrentFps();
}
void cMpvPlayer::PlayerGetWindow(string need, xcb_connection_t **connect, xcb_window_t &window, int &width, int &height, int &x, int &y)
{
int screen_nr;
int i;
int len;
xcb_screen_iterator_t screen_iter;
xcb_screen_t *screen;
xcb_query_tree_cookie_t cookie;
xcb_query_tree_reply_t *reply;
xcb_window_t *child;
xcb_window_t parent = 0;
if (!Dpy)
Dpy = XOpenDisplay(MpvPluginConfig->X11Display.c_str());
if (!Dpy) return;
if (!*connect)
*connect = XGetXCBConnection(Dpy);
if (!*connect) return;
if(!window)
{
screen_nr = DefaultScreen(Dpy);
//get root screen
screen_iter = xcb_setup_roots_iterator(xcb_get_setup(*connect));
for (i = 0; i < screen_nr; ++i)
{
xcb_screen_next(&screen_iter);
}
screen = screen_iter.data;
//query child of root
cookie = xcb_query_tree(*connect,screen->root);
reply = xcb_query_tree_reply(*connect, cookie, 0);
len = xcb_query_tree_children_length(reply);
if (len)
{
xcb_get_property_cookie_t procookie;
xcb_get_property_reply_t *proreply;
xcb_atom_t property = XCB_ATOM_WM_NAME;
//get children of root
child = xcb_query_tree_children(reply);
xcb_query_tree_cookie_t c_cookie;
xcb_query_tree_reply_t *c_reply;
xcb_window_t *c_child = NULL;
int c_len;
for (i = 0; i < len; i++) {
//query child of child
c_cookie = xcb_query_tree(*connect,child[i]);
c_reply = xcb_query_tree_reply(*connect, c_cookie, 0);
c_len = xcb_query_tree_children_length(c_reply);
if (c_len) {
//get children of child
c_child = xcb_query_tree_children(c_reply);
}
for (int o = 0; o < (c_len ? c_len : 1); o++){
//get child property
procookie = xcb_get_property(*connect, 0, c_len ? c_child[o] : child[i], property, XCB_GET_PROPERTY_TYPE_ANY, 0, 1000);
if (proreply = xcb_get_property_reply(*connect, procookie, NULL)) {
if (xcb_get_property_value_length(proreply) > 0) {
string name = (char*)xcb_get_property_value(proreply);
if (name.find(need) != string::npos) {
if (!c_len) window = child[i];
else {
window = c_child[o];
parent = child[i];
}
break;
}
}
}
free(proreply);
}
free(c_reply);
}
}
free(reply);
}
if (window) {
//get geometry
xcb_get_geometry_cookie_t geocookie;
xcb_get_geometry_reply_t *georeply;
geocookie = xcb_get_geometry(*connect, window);
georeply = xcb_get_geometry_reply(*connect, geocookie, NULL);
if (georeply) {
width = georeply->width;
height = georeply->height;
x = georeply->x;
y = georeply->y;
free(georeply);
}
}
if (parent) {
//get geometry
xcb_get_geometry_cookie_t geocookie;
xcb_get_geometry_reply_t *georeply;
geocookie = xcb_get_geometry(*connect, parent);
georeply = xcb_get_geometry_reply(*connect, geocookie, NULL);
if (georeply) {
x += georeply->x;
y += georeply->y;
free(georeply);
}
}
}
void cMpvPlayer::PlayerHideCursor()
{
uint32_t values[4];
if (VideoWindow && Connect) {
pixmap = xcb_generate_id(Connect);
xcb_create_pixmap(Connect, 1, pixmap, VideoWindow, 1, 1);
cursor = xcb_generate_id(Connect);
xcb_create_cursor(Connect, cursor, pixmap, pixmap, 0, 0, 0, 0, 0, 0, 1, 1);
values[0] = cursor;
xcb_change_window_attributes(Connect, VideoWindow, XCB_CW_CURSOR, values);
}
if (VideoWindow) {
XSelectInput (Dpy, VideoWindow, KeyPressMask|ButtonPressMask|StructureNotifyMask|SubstructureNotifyMask);
XMapWindow (Dpy, VideoWindow);
}
XFlush(Dpy);
}
#ifdef USE_DRM
int cMpvPlayer::PlayerTryDRM()
{
int fd;
if (strcmp(MpvPluginConfig->DRMdev.c_str(),"")) {
drm_dev = MpvPluginConfig->DRMdev.c_str();
return 1;
}
//card1 mean external card, card0 internal. First try external card
fd = open("/dev/dri/card1", O_RDWR);
if (fd < 0) {
fd = open("/dev/dri/card0", O_RDWR);
if (fd < 0) return 0;
else drm_dev = "/dev/dri/card0";
} else drm_dev = "/dev/dri/card1";
close(fd);
return 1;
}
void cMpvPlayer::PlayerGetDRM()
{
int fd, i;
drmModeRes *resources;
drmModeConnector *connector;
drmModeModeInfo mode;
drmModeEncoder *encoder;
drmModeCrtc *crtc;
fd = open(drm_dev, O_RDWR);
if (fd < 0) return;
resources = drmModeGetResources(fd);
if (resources != NULL) {
for(i = 0; i < resources->count_connectors; ++i) {
connector = drmModeGetConnector(fd, resources->connectors[i]);
if(connector != NULL) {
if(connector->connection == DRM_MODE_CONNECTED && connector->count_modes > 0)
break;
drmModeFreeConnector(connector);
}
}
if(i < resources->count_connectors) {
encoder = drmModeGetEncoder(fd, connector->encoder_id);
crtc = drmModeGetCrtc(fd, encoder->crtc_id);
mode = crtc->mode;
windowWidth = mode.hdisplay;
windowHeight = mode.vdisplay;
drmModeFreeCrtc(crtc);
drmModeFreeEncoder(encoder);
drmModeFreeConnector(connector);
} else
esyslog("No active connector found\n");
esyslog("windowWidth %d windowHeight %d\n",windowWidth,windowHeight);
drmModeFreeResources(resources);
close(fd);
}
}
#endif
void cMpvPlayer::PlayerStart()
{
PlayerPaused = 0;
PlayerIdle = 0;
PlayerSpeed = 1;
PlayerDiscNav = 0;
isImage = 0;
SwitchOsdToMpv();
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// we are cheating here with mpv since it checks for LC_NUMERIC=C at startup
// this can cause unforseen issues with mpv
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! WARNING !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
LocaleSave = setlocale(LC_NUMERIC, NULL);
#ifdef DEBUG
dsyslog ("get locale %s\n", LocaleSave.c_str());
#endif
setlocale(LC_NUMERIC, "C");
hMpv = mpv_create();
if (!hMpv)
{
esyslog("[mpv] failed to create context\n");
cControl::Shutdown();
}
int64_t osdlevel = 0;
string config_dir = PLGRESDIR;
check_error(mpv_set_option_string(hMpv, "vo", MpvPluginConfig->VideoOut.c_str()));
check_error(mpv_set_option_string(hMpv, "hwdec", MpvPluginConfig->HwDec.c_str()));
check_error(mpv_set_option_string(hMpv, "gpu-context", MpvPluginConfig->GpuCtx.c_str()));
check_error(mpv_set_option_string(hMpv, "hwdec-codecs", "all"));
#ifdef USE_DRM
if (!strcmp(MpvPluginConfig->GpuCtx.c_str(),"drm") || !strcmp(MpvPluginConfig->VideoOut.c_str(),"drm"))
{
drm_ctx = 1;
if (!PlayerTryDRM()) return;
check_error(mpv_set_option_string(hMpv, "drm-device", drm_dev));
}
#endif
//window geometry with x11, drm-mode with drm
if (!drm_ctx) //x11
{
if (strcmp(MpvPluginConfig->Geometry.c_str(),""))
{
check_error(mpv_set_option_string(hMpv, "geometry", MpvPluginConfig->Geometry.c_str()));
} else if (windowWidth && windowHeight) {
char geo[25];
sprintf(geo, "%dx%d+%d+%d", windowWidth, windowHeight, windowX, windowY);
check_error(mpv_set_option_string(hMpv, "geometry", geo));
}
if (!MpvPluginConfig->Windowed)
{
check_error(mpv_set_option_string(hMpv, "fullscreen", "yes"));
}
}
#ifdef USE_DRM
else //drm
{
if (strcmp(MpvPluginConfig->Geometry.c_str(),""))
{
check_error(mpv_set_option_string(hMpv, "drm-mode", MpvPluginConfig->Geometry.c_str()));
}
}
#endif
if (MpvPluginConfig->UseDeinterlace)
{
set_deinterlace(hMpv);
}
check_error(mpv_set_option_string(hMpv, "audio-device", MpvPluginConfig->AudioOut.c_str()));
check_error(mpv_set_option_string(hMpv, "slang", MpvPluginConfig->Languages.c_str()));
check_error(mpv_set_option_string(hMpv, "alang", MpvPluginConfig->Languages.c_str()));
check_error(mpv_set_option_string(hMpv, "cache", "no")); // video stutters if enabled
check_error(mpv_set_option_string(hMpv, "sub-visibility", MpvPluginConfig->ShowSubtitles ? "yes" : "no"));
check_error(mpv_set_option_string(hMpv, "sub-forced-only", "yes"));
check_error(mpv_set_option_string(hMpv, "sub-auto", "all"));
check_error(mpv_set_option_string(hMpv, "hr-seek", "yes"));
check_error(mpv_set_option_string(hMpv, "write-filename-in-watch-later-config", "yes"));
check_error(mpv_set_option_string(hMpv, "config-dir", config_dir.c_str()));
check_error(mpv_set_option_string(hMpv, "config", "yes"));
check_error(mpv_set_option_string(hMpv, "ontop", "yes"));
check_error(mpv_set_option_string(hMpv, "cursor-autohide", "always"));
check_error(mpv_set_option_string(hMpv, "input-cursor", "no"));
check_error(mpv_set_option_string(hMpv, "stop-playback-on-init-failure", "no"));
check_error(mpv_set_option_string(hMpv, "idle", MpvPluginConfig->ExitAtEnd ? "once" : "yes"));
check_error(mpv_set_option_string(hMpv, "force-window", "immediate"));
check_error(mpv_set_option_string(hMpv, "image-display-duration", "inf"));
check_error(mpv_set_option(hMpv, "osd-level", MPV_FORMAT_INT64, &osdlevel));
#ifdef DEBUG
check_error(mpv_set_option_string(hMpv, "log-file", "/var/log/mpv"));
#endif
if (MpvPluginConfig->UsePassthrough)
{
check_error(mpv_set_option_string(hMpv, "audio-spdif", "ac3,dts"));
if (MpvPluginConfig->UseDtsHdPassthrough)
{
check_error(mpv_set_option_string(hMpv, "ad-lavc-downmix", "no"));
check_error(mpv_set_option_string(hMpv, "audio-channels", "7.1,5.1,stereo"));
check_error(mpv_set_option_string(hMpv, "audio-spdif", "ac3,dts,dts-hd,truehd,eac3"));
}
}
else
{
int64_t StartVolume = cDevice::CurrentVolume() / 2.55;
if (MpvPluginConfig->SoftVol)
check_error(mpv_set_option(hMpv, "volume", MPV_FORMAT_INT64, &StartVolume));
else
mpv_set_property_string(hMpv, "ao-volume", std::to_string(StartVolume).c_str());
if (MpvPluginConfig->StereoDownmix)
{
check_error(mpv_set_option_string(hMpv, "ad-lavc-downmix", "yes"));
check_error(mpv_set_option_string(hMpv, "audio-channels", "stereo"));
}
else
check_error(mpv_set_option_string(hMpv, "audio-channels", "7.1,5.1,stereo"));
}
if (PlayShuffle && IsPlaylist(PlayFilename))
check_error(mpv_set_option_string(hMpv, "shuffle", "yes"));
if (MpvPluginConfig->NoScripts)
check_error(mpv_set_option_string(hMpv, "load-scripts", "no"));
#ifdef DEBUG
mpv_request_log_messages(hMpv, "info");
#else
mpv_request_log_messages(hMpv, "error");
#endif
if (mpv_initialize(hMpv) < 0)
{
esyslog("[mpv] failed to initialize\n");
return;
}
running = 1;
isyslog("[mpv] playing %s\n", PlayFilename.c_str());
if (!IsPlaylist(PlayFilename))
{
const char *cmd[] = {"loadfile", PlayFilename.c_str(), NULL};
mpv_command(hMpv, cmd);
}
else
{
const char *cmd[] = {"loadlist", PlayFilename.c_str(), NULL};
mpv_command(hMpv, cmd);
}
// start thread to observe and react on mpv events
pthread_create(&ObserverThreadHandle, NULL, ObserverThread, this);
if (!drm_ctx)
{
pthread_create(&XEventThreadHandle, NULL, XEventThread, this);
RemoteStart();
}
if (cPluginManager::GetPlugin("softhddevice"))
{
is_softhddevice = 1;
}
}
void cMpvPlayer::HandlePropertyChange(mpv_event *event)
{
mpv_event_property *property = (mpv_event_property *) event->data;
if (!property->data)
return;
// don't log on time-pos change since this floods the log
if (event->reply_userdata != MPV_OBSERVE_TIME_POS
&& event->reply_userdata != MPV_OBSERVE_LENGTH)
{
dsyslog("[mpv]: property %s \n", property->name);
}
switch (event->reply_userdata)
{
case MPV_OBSERVE_TIME_POS :
PlayerCurrent = (int)*(double*)property->data;
break;
case MPV_OBSERVE_DISC_MENU :
PlayerDiscNav = (int)*(int64_t*)property->data;
break;
case MPV_OBSERVE_FPS :
PlayerFps = (int)*(double*)property->data;
break;
case MPV_OBSERVE_FILENAME :
PlayerFilename = *(char**)property->data;
break;
case MPV_OBSERVE_LENGTH :
PlayerTotal = (int)*(double*)property->data;
break;
case MPV_OBSERVE_CHAPTERS :
PlayerNumChapters = (int)*(int64_t*)property->data;
mpv_node Node;
mpv_get_property(hMpv, "chapter-list", MPV_FORMAT_NODE, &Node);
ChapterTitles.clear();
PlayerChapters.clear();
if (Node.format == MPV_FORMAT_NODE_ARRAY)
{
for (int i=0; i<Node.u.list->num; i++)
{
ChapterTitles.push_back (Node.u.list->values[i].u.list->values[0].u.string);
PlayerChapters.push_back (Node.u.list->values[i].u.list->values[1].u.double_);
}
mpv_free_node_contents(&Node);
}
break;
case MPV_OBSERVE_CHAPTER :
PlayerChapter = (int)*(int64_t*)property->data;
break;
#if MPV_CLIENT_API_VERSION >= MPV_MAKE_VERSION(2,0)
case MPV_OBSERVE_TRACK_LIST :
HandleTracksChange();
break;
#endif
case MPV_OBSERVE_PAUSE :
PlayerPaused = (int)*(int64_t*)property->data;
break;
case MPV_OBSERVE_SPEED :
PlayerSpeed = (int)*(double*)property->data;
break;
case MPV_OBSERVE_VIA_NET :
isNetwork = (int)*(int64_t*)property->data;
break;
case MPV_OBSERVE_MEDIA_TITLE :
mediaTitle = *(char**)property->data;
break;
case MPV_OBSERVE_LIST_POS :
ListCurrent = (int)*(int64_t*)property->data;
break;
case MPV_OBSERVE_LIST_COUNT :
ListTotal = (int)*(int64_t*)property->data;
mpv_node Node1;
if (mpv_get_property(hMpv, "playlist", MPV_FORMAT_NODE, &Node1) >= 0)
{
ListTitles.clear();
ListFilenames.clear();
if (Node1.format == MPV_FORMAT_NODE_ARRAY)
{
for (int i=0; i<Node1.u.list->num; i++)
{
ListFilenames.push_back (Node1.u.list->values[i].u.list->values[0].u.string);
for (int a=1;a<Node1.u.list->values[i].u.list->num;a++)
{
if(Node1.u.list->values[i].u.list->values[a].format == MPV_FORMAT_STRING)
{
ListTitles.push_back (Node1.u.list->values[i].u.list->values[a].u.string);
break;
}
}
// push filename if no title
std::string title = Node1.u.list->values[i].u.list->values[0].u.string;
if ((int)ListTitles.size() < i + 1) ListTitles.push_back (title.substr(title.find_last_of("/") + 1));
// dsyslog("%d %s ---- %s\n",i,ListFilename(i+1).c_str(),ListTitle(i+1).c_str());
}
mpv_free_node_contents(&Node1);
}
}
break;
}
}
void cMpvPlayer::HandleTracksChange()
{
mpv_node Node;
mpv_get_property(hMpv, "track-list", MPV_FORMAT_NODE, &Node);
if (Node.format != MPV_FORMAT_NODE_ARRAY)
return;
// loop though available tracks
for (int i=0; i<Node.u.list->num; i++)
{
int TrackId = 0;
string TrackType;
string TrackLanguage = "undefined";
string TrackTitle = "";
for (int j=0; j<Node.u.list->values[i].u.list->num; j++)
{
if (strcmp(Node.u.list->values[i].u.list->keys[j], "id") == 0)
TrackId = Node.u.list->values[i].u.list->values[j].u.int64;
if (strcmp(Node.u.list->values[i].u.list->keys[j], "type") == 0)
TrackType = Node.u.list->values[i].u.list->values[j].u.string;
if (strcmp(Node.u.list->values[i].u.list->keys[j], "lang") == 0)
TrackLanguage = Node.u.list->values[i].u.list->values[j].u.string;
if (strcmp(Node.u.list->values[i].u.list->keys[j], "title") == 0)
TrackTitle = Node.u.list->values[i].u.list->values[j].u.string;
if (strcmp(Node.u.list->values[i].u.list->keys[j], "image") == 0)
{
isImage = Node.u.list->values[i].u.list->values[j].u.flag;
if (isImage) check_error(mpv_set_option_string(hMpv, "deinterlace", "no"));
}
}
if (TrackType == "audio")
{
eTrackType type = ttAudio;
DeviceSetAvailableTrack(type, i, TrackId, TrackLanguage.c_str(), TrackTitle.c_str());
}
else if (TrackType == "sub")
{
eTrackType type = ttSubtitle;
DeviceSetAvailableTrack(type, 0, 0, "Off");
DeviceSetAvailableTrack(type, i, TrackId, TrackLanguage.c_str(), TrackTitle.c_str());
}
}
mpv_free_node_contents(&Node);
}
void cMpvPlayer::OsdClose()
{
#ifdef DEBUG
dsyslog("[mpv] %s\n", __FUNCTION__);
#endif
SendCommand ("overlay-remove 1");
}
void cMpvPlayer::Shutdown()
{
RemoteStop();
if(!drm_ctx) {
if (XEventThreadHandle) {
void *res;
int s;
pthread_cancel(XEventThreadHandle);
while(res != PTHREAD_CANCELED) {
s= pthread_join(XEventThreadHandle, &res);
esyslog("cansel %d\n",s);
if (s) break;
}
}
}
mediaTitle = "";
running = 0;
MpvPluginConfig->TitleOverride = "";
ChapterTitles.clear();
PlayerChapters.clear();
ListTitles.clear();
ListFilenames.clear();
if (ObserverThreadHandle)
pthread_cancel(ObserverThreadHandle);
VideoWindow = 0;
if (!drm_ctx) {
if (cursor != XCB_NONE) {
xcb_free_cursor(Connect, cursor);
cursor = XCB_NONE;
}
if (pixmap != XCB_NONE) {
xcb_free_pixmap(Connect, pixmap);
pixmap = XCB_NONE;
}
if (Connect) {
xcb_flush(Connect);
Connect = NULL;
}
if (Dpy) {
XFlush(Dpy);
Dpy = NULL;
}
}
#if MPV_CLIENT_API_VERSION >= MPV_MAKE_VERSION(1,29)
mpv_destroy(hMpv);
#else
mpv_detach_destroy(hMpv);
#endif
hMpv = NULL;
cOsdProvider::Shutdown();
// set back locale
setlocale(LC_NUMERIC, LocaleSave.c_str());
if (MpvPluginConfig->RefreshRate)
{
ChangeFrameRate(OriginalFps);
OriginalFps = -1;
}
Setup.CurrentVolume = cDevice::CurrentVolume();
Setup.Save();
}
void cMpvPlayer::SwitchOsdToMpv()
{
#ifdef DEBUG
dsyslog("[mpv] %s\n", __FUNCTION__);
#endif
cOsdProvider::Shutdown();
new cMpvOsdProvider(this);
}
bool cMpvPlayer::IsPlaylist(string File)
{
for (unsigned int i=0;i<MpvPluginConfig->PlaylistExtensions.size();i++)
{
if (File.substr(File.find_last_of(".") + 1) == MpvPluginConfig->PlaylistExtensions[i])
return true;
}
return false;
}
void cMpvPlayer::ChangeFrameRate(int TargetRate)
{
if (!MpvPluginConfig->RefreshRate)
return;
int RefreshRate = 0;
#ifdef USE_XRANDR
if (!drm_ctx)
{
Display *Dpl;
XRRScreenConfiguration *CurrInfo;
if (TargetRate == 25)
TargetRate = 50; // fix DVD audio and since this is doubled it's ok
if (TargetRate == 23)
TargetRate = 24;
Dpl = XOpenDisplay(MpvPluginConfig->X11Display.c_str());
if (Dpl)
{
short *Rates;
int NumberOfRates;
SizeID CurrentSizeId;
Rotation CurrentRotation;
int RateFound = 0;
CurrInfo = XRRGetScreenInfo(Dpl, DefaultRootWindow(Dpl));