-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MPVPlayer.pas
2016 lines (1654 loc) · 58.8 KB
/
MPVPlayer.pas
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
{*
* URUWorks MPVPlayer
*
* Author : URUWorks
* Website : uruworks.net
*
* The contents of this file are used with permission, subject to
* the Mozilla Public License Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.mozilla.org/MPL/2.0.html
*
* Software distributed under the License is distributed on an
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the License for the specific language governing
* rights and limitations under the License.
*
* Copyright (C) 2021-2024 URUWorks, [email protected].
*
* Important for Unix/Linux needs:
* Place the following units/functions at the beginning
* - cthreads
* - XInitThreads
*}
unit MPVPlayer;
// -----------------------------------------------------------------------------
{$I MPVPlayer.inc}
interface
uses
Classes, Controls, SysUtils, LazFileUtils, ExtCtrls, Graphics, LCLType,
LResources, LazarusPackageIntf, libMPV.Client, MPVPlayer.Thread,
MPVPlayer.RenderGL, OpenGLContext, MPVPlayer.Filters
{$IFDEF LINUX}, gtk2, gdk2x{$ENDIF}
{$IFDEF BGLCONTROLS}, BGRAOpenGL{$ENDIF}
{$IFDEF SDL2}, sdl2lib, libMPV.Render, MPVPlayer.RenderSDL{$ENDIF};
// -----------------------------------------------------------------------------
type
{ TMPVPlayer Types }
TMPVPlayerRenderMode = (rmEmbedding, rmOpenGL{$IFDEF SDL2}, rmSDL2{$ENDIF});
TMPVPlayerRendeFailAction = (rfSwitchToEmbedding, rfNone);
TMPVPlayerTrackType = (ttVideo, ttAudio, ttSubtitle, ttUnknown);
TMPVPlayerVideoAspectRatio = (arDefault, ar4_3, ar16_9, ar185_1, ar235_1);
TMPVPlayerLogLevel = (llNo, llFatal, llError, llWarn, llInfo, llStatus, llV, llDebug, llTrace);
TMPVPlayerScreenshotMode = (smSubtitles, smVideo, smWindow);
TMPVPlayerNotifyEvent = procedure(ASender: TObject; AParam: Integer) of object;
TMPVPlayerEndFileEvent = procedure(ASender: TObject; AReason, AError: Integer) of object;
TMPVPlayerLogEvent = procedure(ASender: TObject; APrefix, ALevel, AText: String) of object;
TMPVPlayerGetReplyEvent = procedure(ASender: TObject; reply_userdata: Integer; error_code: mpv_error; event_property: Pmpv_event_property) of object;
TMPVPlayerSetReplyEvent = procedure(ASender: TObject; reply_userdata: Integer; error_code: mpv_error) of object;
TMPVPlayerCommandReplyEvent = procedure(ASender: TObject; reply_userdata: Integer; error_code: mpv_error; event_command: Pmpv_event_command) of object;
TMPVPlayerTrackInfo = record
Kind : TMPVPlayerTrackType;
ID : Integer;
Codec : String;
Decoder : String;
Channels : String;
Title : String;
Lang : String;
Selected : Boolean;
end;
TMPVPlayerTrackList = array of TMPVPlayerTrackInfo;
{ TMPVPlayer }
TMPVPlayer = class(TCustomPanel)
private
FMPV_HANDLE : Pmpv_handle;
FError : mpv_error;
FVersion : DWord;
FGL : TUWOpenGLControl;
FInitialized : Boolean;
FStartOptions : TStringList;
FLogLevel : TMPVPlayerLogLevel;
FMPVEvent : TMPVPlayerThreadEvent;
FTrackList : TMPVPlayerTrackList;
FAspectRatio : TMPVPlayerVideoAspectRatio;
FAutoStart : Boolean;
FAutoLoadSub : Boolean;
FKeepAspect : Boolean;
FNoAudioDisplay : Boolean;
FUseHWDec : Boolean;
FSMPTEMode : Boolean;
FRenderFail : TMPVPlayerRendeFailAction;
FStartAtPosMs : Integer;
FPausePosMs : Integer;
FFileName : String;
FMPVFileName : String;
FYTDLPFileName : String;
{$IFDEF USETIMER}
FTimer : TTimer;
FLastPos : Integer;
{$ENDIF}
FRenderMode : TMPVPlayerRenderMode;
FRenderGL : TMPVPlayerRenderGL;
{$IFDEF SDL2}
FRenderSDL : TMPVPlayerRenderSDL;
{$ENDIF}
FShowText : String;
FText : String;
FTextNode : mpv_node;
FTextNodeList : mpv_node_list;
FTextNodeKeys : array of PChar;
FTextNodeValues : array of mpv_node;
{$IFDEF ENABLE_BACKIMAGE}
FBackImage : TPicture;
{$ENDIF}
FOnStartFile: TNotifyEvent; // Notification before playback start of a file (before the file is loaded).
FOnEndFile: TMPVPlayerEndFileEvent; // Notification after playback end (after the file was unloaded).
FOnFileLoaded: TNotifyEvent; // Notification when the file has been loaded (headers were read etc.)
FOnVideoReconfig: TNotifyEvent; // Happens after video changed in some way.
FOnAudioReconfig: TNotifyEvent; // Similar to VIDEO_RECONFIG.
FOnSeek: TMPVPlayerNotifyEvent; // Happens when a seek was initiated.
FOnPlaybackRestart: TNotifyEvent; // Usually happens on start of playback and after seeking.
FOnPlay: TNotifyEvent; // Play by user
FOnStop: TNotifyEvent; // Stop by user
FOnPause: TNotifyEvent; // Pause by user
FOnTimeChanged: TMPVPlayerNotifyEvent; // Notify playback time, AParam is current position.
FOnBuffering: TMPVPlayerNotifyEvent; // Whether playback is paused because of waiting for the cache.
FOnLogMessage: TMPVPlayerLogEvent; // Receives messages enabled with mpv_request_log_messages().
FOnGetReplyEvent: TMPVPlayerGetReplyEvent; // Result data of mpv_get_property_* async.
FOnSetReplyEvent: TMPVPlayerSetReplyEvent; // Result data of mpv_set_property_* async.
FOnCommandReplyEvent: TMPVPlayerCommandReplyEvent; // Result data of the command async.
{$IFDEF BGLCONTROLS}
FOnDrawEvent: TMPVPlayerDrawEvent;
{$ENDIF}
function Initialize: Boolean;
procedure UnInitialize;
function InitializeRenderGL: Boolean;
procedure UnInitializeRenderGL;
procedure PushEvent;
procedure ReceivedEvent(Sender: TObject);
{$IFDEF SDL2}
function InitializeRenderSDL: Boolean;
procedure UnInitializeRenderSDL;
{$ENDIF}
function SetWID: Boolean;
procedure SetRenderMode(const AValue: TMPVPlayerRenderMode);
procedure SetHWDec(const AValue: Boolean);
function LogLevelToString: String;
{$IFDEF USETIMER}
procedure DoTimer(Sender: TObject);
{$ENDIF}
procedure DoOnPaint(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
{$IFDEF ENABLE_BACKIMAGE}
procedure EraseBackground(DC: HDC); override;
{$ENDIF}
function IsLibMPVAvailable: Boolean;
function mpv_command_(args: array of String; const reply_userdata: Integer = 0): mpv_error; // if reply_userdata > 0 commands are executed asynchronously
function mpv_command_node_(ANode: mpv_node; const reply_userdata: Integer = 0): mpv_error;
procedure mpv_abort_async_command_(const reply_userdata: Integer);
function mpv_set_option_string_(const AValue: String): Integer;
function mpv_get_property_string_(const APropertyName: String; const reply_userdata: Integer = 0): String;
procedure mpv_set_property_string_(const APropertyName: String; const AValue: String; const reply_userdata: Integer = 0);
function mpv_get_property_boolean(const APropertyName: String; const reply_userdata: Integer = 0): Boolean;
procedure mpv_set_property_boolean(const APropertyName: String; const AValue: Boolean; const reply_userdata: Integer = 0);
function mpv_get_property_double(const APropertyName: String; const reply_userdata: Integer = 0): Double;
procedure mpv_set_property_double(const APropertyName: String; const AValue: Double; const reply_userdata: Integer = 0);
function mpv_get_property_int64(const APropertyName: String; const reply_userdata: Integer = 0): Int64;
procedure mpv_set_property_int64(const APropertyName: String; const AValue: Int64; const reply_userdata: Integer = 0);
procedure mpv_set_pause(const AValue: Boolean);
function GetErrorString: String;
function GetVersionString: String;
function GetPlayerHandle: Pmpv_handle;
procedure Play(const AFileName: String; const AStartAtPositionMs: Integer = 0); overload;
procedure Play(const AFromMs: Integer); overload;
procedure Close(const AForce: Boolean = True);
procedure Loop(const AStartTimeMs, BFinalTimeMs: Integer; const ALoopCount: Integer = -1);
procedure Pause;
procedure Resume(const AForcePlay: Boolean = False);
procedure Stop;
function IsMediaLoaded: Boolean;
function IsPlaying: Boolean;
function IsPaused: Boolean;
function GetMediaLenInMs: Integer;
function GetMediaPosInMs: Integer;
procedure SetMediaPosInMs(const AValue: Integer);
procedure SeekInMs(const MSecs: Integer; const SeekAbsolute: Boolean = True);
procedure NextFrame(const AStep: Integer = 1);
procedure PreviousFrame(const AStep: Integer = 1);
procedure SetPlaybackRate(const AValue: Byte);
function GetAudioVolume: Byte;
procedure SetAudioVolume(const AValue: Byte);
function GetAudioMute: Boolean;
procedure SetAudioMute(const AValue: Boolean);
procedure SetTrack(const TrackType: TMPVPlayerTrackType; const ID: Integer); overload;
procedure SetTrack(const Index: Integer); overload;
procedure GetTracks;
function HasVideoTrack: Boolean;
procedure LoadTrack(const TrackType: TMPVPlayerTrackType; const AFileName: String);
procedure RemoveTrack(const TrackType: TMPVPlayerTrackType; const ID: Integer = -1);
procedure ReloadTrack(const TrackType: TMPVPlayerTrackType; const ID: Integer = -1);
procedure ShowOverlayText(const AText: String);
procedure ShowText(const AText: String; const ADuration: Integer = 1000; const ATags: String = '{\an7}');
procedure SetTextColor(const AValue: String);
procedure SetTextHAlign(const AValue: String);
procedure SetTextVAlign(const AValue: String);
procedure SetTextSize(const AValue: Int64);
procedure SetTextFont(const AValue: String);
procedure SetSubtitleColor(const AValue: String);
procedure SetSubtitleSize(const AValue: Int64);
procedure SetSubtitleFont(const AValue: String);
function GetVideoWidth: Integer;
function GetVideoHeight: Integer;
function GetVideoTotalFrames: Integer;
function GetVideoFPS: Double;
procedure ScreenshotToFile(const AFileName: String; const AScreenshotMode: TMPVPlayerScreenshotMode = smVideo); // name with full path, extension defines the format (file.png)
procedure ScreenshotToClipboard(const AScreenshotMode: TMPVPlayerScreenshotMode = smVideo);
procedure AddOption(const AValue: String);
procedure RemoveOption(const AValue: String);
procedure SetVideoAspectRatio(const AValue: TMPVPlayerVideoAspectRatio);
function CycleVideoAspectRatio: TMPVPlayerVideoAspectRatio;
procedure SetVideoFilters(const AVideoFilters: TMPVPlayerVideoFilters);
procedure ClearVideoFilters;
procedure SetAudioFilters(const AAudioFilters: TMPVPlayerAudioFilters);
procedure ClearAudioFilters;
property mpv_handle: Pmpv_handle read FMPV_HANDLE;
property Error: mpv_error read FError;
property ErrorString: String read GetErrorString;
property Version: DWord read FVersion;
property VersionString: String read GetVersionString;
property Initialized: Boolean read FInitialized;
property StartOptions: TStringList read FStartOptions;
property TrackList: TMPVPlayerTrackList read FTrackList;
property FileName: String read FFileName;
property MPVFileName: String read FMPVFileName write FMPVFileName;
property YTDLPFileName: String read FYTDLPFileName write FYTDLPFileName;
property SMPTEMode: Boolean read FSMPTEMode write FSMPTEMode;
{$IFDEF USETIMER}
property Timer: TTimer read FTimer;
{$ENDIF}
published
property Align;
property Anchors;
property AutoSize;
property BorderSpacing;
property BevelInner;
property BevelOuter;
property BevelWidth;
property BidiMode;
property BorderWidth;
property BorderStyle;
property Caption;
property ChildSizing;
property ClientHeight;
property ClientWidth;
property Color default clBlack;
property Constraints;
property DockSite;
property DragCursor;
property DragKind;
property DragMode;
property Enabled;
property Font;
property ParentBidiMode;
property ParentColor;
property ParentFont;
property ParentShowHint;
property PopupMenu;
property ShowHint;
property TabOrder;
property TabStop default True;
property UseDockManager default True;
property Visible;
property Width default 320;
property Height default 240;
property OnClick;
property OnContextPopup;
property OnDockDrop;
property OnDockOver;
property OnDblClick;
property OnDragDrop;
property OnDragOver;
property OnEndDock;
property OnEndDrag;
property OnEnter;
property OnExit;
property OnGetSiteInfo;
property OnGetDockCaption;
property OnMouseDown;
property OnMouseEnter;
property OnMouseLeave;
property OnMouseMove;
property OnMouseUp;
property OnMouseWheel;
property OnMouseWheelDown;
property OnMouseWheelUp;
property OnResize;
property OnStartDock;
property OnStartDrag;
property OnUnDock;
property AutoStartPlayback: Boolean read FAutoStart write FAutoStart;
property AutoLoadSubtitle: Boolean read FAutoLoadSub write FAutoLoadSub;
property KeepAspect: Boolean read FKeepAspect write FKeepAspect;
property AspectRatio: TMPVPlayerVideoAspectRatio read FAspectRatio write SetVideoAspectRatio;
property NoAudioDisplay: Boolean read FNoAudioDisplay write FNoAudioDisplay;
property RendererMode: TMPVPlayerRenderMode read FRenderMode write SetRenderMode;
property RenderFailAction : TMPVPlayerRendeFailAction read FRenderFail write FRenderFail;
property UseHWDec: Boolean read FUseHWDec write SetHWDec;
property LogLevel: TMPVPlayerLogLevel read FLogLevel write FLogLevel;
{$IFDEF ENABLE_BACKIMAGE}
property BackImage: TPicture read FBackImage write FBackImage;
{$ENDIF}
property OnStartFile: TNotifyEvent read FOnStartFile write FOnStartFile;
property OnEndFile: TMPVPlayerEndFileEvent read FOnEndFile write FOnEndFile;
property OnFileLoaded: TNotifyEvent read FOnFileLoaded write FOnFileLoaded;
property OnVideoReconfig: TNotifyEvent read FOnVideoReconfig write FOnVideoReconfig;
property OnAudioReconfig: TNotifyEvent read FOnAudioReconfig write FOnAudioReconfig;
property OnSeek: TMPVPlayerNotifyEvent read FOnSeek write FOnSeek;
property OnPlaybackRestart: TNotifyEvent read FOnPlaybackRestart write FOnPlaybackRestart;
property OnPlay: TNotifyEvent read FOnPlay write FOnPlay;
property OnStop: TNotifyEvent read FOnStop write FOnStop;
property OnPause: TNotifyEvent read FOnPause write FOnPause;
property OnTimeChanged: TMPVPlayerNotifyEvent read FOnTimeChanged write FOnTimeChanged;
property OnBuffering: TMPVPlayerNotifyEvent read FOnBuffering write FOnBuffering;
property OnLogMessage: TMPVPlayerLogEvent read FOnLogMessage write FOnLogMessage;
property OnGetReplyEvent: TMPVPlayerGetReplyEvent read FOnGetReplyEvent write FOnGetReplyEvent;
property OnSetReplyEvent: TMPVPlayerSetReplyEvent read FOnSetReplyEvent write FOnSetReplyEvent;
property OnCommandReplyEvent: TMPVPlayerCommandReplyEvent read FOnCommandReplyEvent write FOnCommandReplyEvent;
{$IFDEF BGLCONTROLS}
property OnDraw: TMPVPlayerDrawEvent read FOnDrawEvent write FOnDrawEvent;
{$ENDIF}
end;
procedure Register;
// -----------------------------------------------------------------------------
implementation
uses
Clipbrd;
// -----------------------------------------------------------------------------
{ libmpv wakeup_events }
// -----------------------------------------------------------------------------
procedure LIBMPV_EVENT(Sender: Pointer); cdecl;
begin
if (Sender <> NIL) then TMPVPlayer(Sender).PushEvent;
end;
// -----------------------------------------------------------------------------
{ Helpers}
// -----------------------------------------------------------------------------
function MSToTimeStamp(const Time: Integer): String; // 'hh:mm:ss.zzz'
var
Hour, Min, Secs, MSecs,
h, m, x: Integer;
begin
Hour := Trunc(Time / 3600000);
h := Time - (Hour * 3600000);
Min := Trunc(h / 60000);
m := Min * 60000;
x := h - m;
Secs := Trunc(x / 1000);
MSecs := Trunc(x - (Secs*1000));
Result := Format('%.2d:%.2d:%.2d.%.3d', [Hour, Min, Secs, MSecs]);
end;
// -----------------------------------------------------------------------------
function FramesToMS(const Frames, FPS: Single): Integer;
begin
if FPS > 0 then
Result := Round((Frames / FPS) * 1000.0)
else
Result := 0;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.IsLibMPVAvailable: Boolean;
begin
FError := IsLibMPV_Installed(FMPVFileName);
Result := (FError = MPV_ERROR_SUCCESS);
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_command_(args: array of String; const reply_userdata: Integer = 0): mpv_error;
var
pArgs: array of PChar;
i: Integer;
begin
Result := MPV_ERROR_INVALID_PARAMETER;
if High(Args) < 0 then
Exit
else if FInitialized and (mpv_command <> NIL) and (FMPV_HANDLE <> NIL) then
begin
SetLength(pArgs, (Length(Args)+1));
for i := 0 to High(Args) do
pArgs[i] := PChar(Args[i]);
pArgs[Length(Args)] := NIL;
if reply_userdata > 0 then
FError := mpv_command_async(FMPV_HANDLE^, reply_userdata, PPChar(@pArgs[0]))
else
FError := mpv_command(FMPV_HANDLE^, PPChar(@pArgs[0]));
SetLength(pArgs, 0);
end
else
FError := MPV_ERROR_UNINITIALIZED;
Result := FError;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_command_node_(ANode: mpv_node; const reply_userdata: Integer = 0): mpv_error;
var
Res: mpv_node;
begin
FError := MPV_ERROR_UNINITIALIZED;
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_command_node_async(FMPV_HANDLE^, reply_userdata, ANode)
else
FError := mpv_command_node(FMPV_HANDLE^, ANode, Res);
end;
Result := FError;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_abort_async_command_(const reply_userdata: Integer);
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
mpv_abort_async_command(FMPV_HANDLE^, reply_userdata);
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_set_option_string_(const AValue: String): Integer;
var
s1, s2: String;
i: Integer;
begin
FError := MPV_ERROR_OPTION_ERROR;
if not Assigned(mpv_set_option_string) or (FMPV_HANDLE = NIL) or AValue.IsEmpty then Exit(FError);
i := Pos('=', AValue);
if i > 0 then
begin
s1 := Copy(AValue, 1, i-1);
s2 := Copy(AValue, i+1, AValue.Length-i);
end
else
begin
s1 := AValue;
s2 := '';
end;
FError := mpv_set_option_string(FMPV_HANDLE^, PChar(s1), PChar(s2));
Result := FError;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_get_property_string_(const APropertyName: String; const reply_userdata: Integer = 0): String;
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_get_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_STRING)
else
FError := mpv_get_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_STRING, @Result);
end
else
Result := '';
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_set_property_string_(const APropertyName: String; const AValue: String; const reply_userdata: Integer = 0);
begin
if not FInitialized or (FMPV_HANDLE = NIL) then Exit;
if reply_userdata > 0 then
FError := mpv_set_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_STRING, PChar(@AValue))
else
FError := mpv_set_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_STRING, PChar(@AValue));
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_get_property_boolean(const APropertyName: String; const reply_userdata: Integer = 0): Boolean;
var
p: Integer;
begin
Result := False;
if not FInitialized or (FMPV_HANDLE = NIL) then Exit;
if reply_userdata > 0 then
begin
FError := mpv_get_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_FLAG);
Result := True;
end
else
begin
FError := mpv_get_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_FLAG, @p);
Result := Boolean(p);
end;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_set_property_boolean(const APropertyName: String; const AValue: Boolean; const reply_userdata: Integer = 0);
var
p: Integer;
begin
if not FInitialized or (FMPV_HANDLE = NIL) then Exit;
if AValue then
p := 1
else
p := 0;
if reply_userdata > 0 then
FError := mpv_set_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_FLAG, @p)
else
FError := mpv_set_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_FLAG, @p);
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_get_property_double(const APropertyName: String; const reply_userdata: Integer = 0): Double;
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_get_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_DOUBLE)
else
FError := mpv_get_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_DOUBLE, @Result);
end
else
Result := 0;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_set_property_double(const APropertyName: String; const AValue: Double; const reply_userdata: Integer = 0);
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_set_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_DOUBLE, @AValue)
else
FError := mpv_set_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_DOUBLE, @AValue);
end;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.mpv_get_property_int64(const APropertyName: String; const reply_userdata: Integer = 0): Int64;
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_get_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_INT64)
else
FError := mpv_get_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_INT64, @Result)
end
else
Result := 0;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_set_property_int64(const APropertyName: String; const AValue: Int64; const reply_userdata: Integer = 0);
begin
if FInitialized and (FMPV_HANDLE <> NIL) then
begin
if reply_userdata > 0 then
FError := mpv_set_property_async(FMPV_HANDLE^, reply_userdata, PChar(APropertyName), MPV_FORMAT_INT64, @AValue)
else
FError := mpv_set_property(FMPV_HANDLE^, PChar(APropertyName), MPV_FORMAT_INT64, @AValue);
end;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.mpv_set_pause(const AValue: Boolean);
begin
mpv_set_property_boolean('pause', AValue);
case AValue of
True : if Assigned(FOnPause) then FOnPause(Self);
False : if Assigned(FOnPlay) then FOnPlay(Self);
end;
end;
// -----------------------------------------------------------------------------
{ TMPVPlayer }
// -----------------------------------------------------------------------------
constructor TMPVPlayer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Our panel settings
DoubleBuffered := True;
Width := 320;
Height := 240;
TabStop := True;
BevelOuter := bvNone;
ParentBackground := False;
ParentColor := False;
Color := clBlack;
FullRepaint := False;
Caption := '';
// our control setup
FGL := NIL;
FMPV_HANDLE := NIL;
FVersion := 0;
FError := 0;
FInitialized := False;
FMPVEvent := NIL;
FLogLevel := llStatus;
FAutoStart := True;
FAutoLoadSub := False;
FKeepAspect := True;
FNoAudioDisplay := False;
FUseHWDec := False;
FSMPTEMode := False;
FRenderFail := rfNone;
FPausePosMs := -1;
FFileName := '';
FMPVFileName := '';
FYTDLPFileName := '';
FStartOptions := TStringList.Create;
SetLength(FTrackList, 0);
FAspectRatio := arDefault;
{$IFDEF WINDOWS}
FRenderMode := rmEmbedding;
{$ELSE}
FRenderMode := rmOpenGL;
{$ENDIF}
FRenderGL := NIL;
{$IFDEF ENABLE_BACKIMAGE}
FBackImage := TPicture.Create;
{$ENDIF}
{$IFDEF USETIMER}
FTimer := TTimer.Create(NIL);
FTimer.Enabled := False;
FTimer.Interval := 140;
FTimer.OnTimer := @DoTimer;
FLastPos := -1;
{$ENDIF}
with FStartOptions do
begin
Sorted := True;
Duplicates := dupIgnore;
Add('hwdec=no');
//Add('hwdec=auto-safe '); // enable best hw decoder. white-list ie: hwdec-codecs=h264,vc1,hevc,vp8,av1,prores
Add('vd-lavc-dr=no'); // enable direct rendering (default: auto).
Add('osc=no'); // default: yes.
Add('keep-open=always'); // don't auto close video.
Add('hr-seek=yes'); // use precise seeks whenever possible.
Add('hr-seek-framedrop=no'); // default: yes.
//Add('osd-scale-by-window=no'); // scale the OSD with the window size. default: yes.
Add('ytdl=yes'); // use YouTube downloader.
end;
FError := Load_libMPV(FMPVFileName);
end;
// -----------------------------------------------------------------------------
destructor TMPVPlayer.Destroy;
begin
UnInitialize;
{$IFDEF ENABLE_BACKIMAGE}
FBackImage.Free;
{$ENDIF}
{$IFDEF USETIMER}
FTimer.Free;
{$ENDIF}
FStartOptions.Free;
FStartOptions := NIL;
Free_libMPV;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.Initialize: Boolean;
var
sl : TStringList;
i : Integer;
begin
if FInitialized then Exit(True);
Result := False;
if not IsLibMPV_Loaded then Exit;
FMPV_HANDLE := mpv_create();
if not Assigned(FMPV_HANDLE) then
begin
FError := MPV_ERROR_UNSUPPORTED;
Exit;
end;
// Get version lib
if Assigned(mpv_client_api_version) then
FVersion := mpv_client_api_version();
sl := TStringList.Create;
try
sl.Assign(FStartOptions);
if not FAutoStart then
sl.Add('pause'); // Start the player in paused state
if not FAutoLoadSub then
sl.Add('sub=no'); // don't load subtitles
if not FKeepAspect then
sl.Add('keepaspect=no'); // always stretch the video to window size
if FNoAudioDisplay then
sl.Add('audio-display=no'); // no display cover art
for i := 0 to sl.Count-1 do
mpv_set_option_string_(sl[i]);
finally
sl.Free;
end;
if not FYTDLPFileName.IsEmpty then
mpv_set_option_string(FMPV_HANDLE^, PChar('script-opts'), PChar('ytdl_hook-ytdl_path='+FYTDLPFileName));
SetVideoAspectRatio(FAspectRatio);
// Set our window handle
if not SetWID then
begin
UnInitialize;
Exit;
end;
{$IFNDEF USETIMER}
mpv_observe_property(FMPV_HANDLE^, 0, 'playback-time', MPV_FORMAT_INT64);
{$ENDIF}
mpv_observe_property(FMPV_HANDLE^, 0, 'eof-reached', MPV_FORMAT_FLAG);
//mpv_observe_property(FMPV_HANDLE^, 0, 'paused-for-cache', MPV_FORMAT_INT64);
mpv_observe_property(FMPV_HANDLE^, 0, 'cache-buffering-state', MPV_FORMAT_INT64);
FError := mpv_initialize(FMPV_HANDLE^);
if FError <> MPV_ERROR_SUCCESS then
begin
UnInitialize;
Exit;
end;
FError := mpv_request_log_messages(FMPV_HANDLE^, PChar(LogLevelToString));
// Show text string
FShowText := '';
// Node text overlay cfg
FText := '';
SetLength(FTextNodeKeys, 4);
SetLength(FTextNodeValues, 4);
FTextNodeKeys[0] := 'name';
FTextNodeValues[0].format := MPV_FORMAT_STRING;
FTextNodeValues[0].u._string := 'osd-overlay';
FTextNodeKeys[1] := 'id';
FTextNodeValues[1].format := MPV_FORMAT_INT64;
FTextNodeValues[1].u.int64_ := 1;
FTextNodeKeys[2] := 'format';
FTextNodeValues[2].format := MPV_FORMAT_STRING;
FTextNodeKeys[3] := 'data';
FTextNodeValues[3].format := MPV_FORMAT_STRING;
FTextNodeValues[3].u._string := NIL;
FTextNodeList.num := 4;
FTextNodeList.keys := @FTextNodeKeys[0];
FTextNodeList.values := @FTextNodeValues[0];
FTextNode.format := MPV_FORMAT_NODE_MAP;
FTextNode.u.list := @FTextNodeList;
FMPVEvent := TMPVPlayerThreadEvent.Create;
FMPVEvent.OnEvent := @ReceivedEvent;
mpv_set_wakeup_callback(FMPV_HANDLE^, @LIBMPV_EVENT, Self);
if FRenderMode = rmOpenGL then
begin
if not InitializeRenderGL then
begin
UnInitializeRenderGL;
if FRenderFail = rfNone then
begin
FError := MPV_ERROR_VO_INIT_FAILED;
Exit;
end
else
FRenderMode := rmEmbedding;
end;
end
{$IFDEF SDL2}
else if FRenderMode = rmSDL2 then
begin
if not InitializeRenderSDL then
begin
UnInitializeRenderSDL;
if FRenderFail = rfNone then
begin
FError := MPV_ERROR_VO_INIT_FAILED;
Exit;
end
else
FRenderMode := rmEmbedding;
end;
end;
{$ENDIF};
FPausePosMs := -1;
FInitialized := True;
Result := True;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.UnInitialize;
begin
if not FInitialized then Exit;
{$IFDEF USETIMER}
FTimer.Enabled := False;
FLastPos := -1;
{$ENDIF}
if Assigned(mpv_unobserve_property) and Assigned(FMPV_HANDLE) then
mpv_unobserve_property(FMPV_HANDLE^, 0);
if Assigned(mpv_set_wakeup_callback) and Assigned(FMPV_HANDLE) then
mpv_set_wakeup_callback(FMPV_HANDLE^, NIL, NIL);
FShowText := '';
FText := '';
SetLength(FTextNodeKeys, 0);
SetLength(FTextNodeValues, 0);
if Assigned(FMPVEvent) then
begin
FMPVEvent.OnEvent := NIL;
FMPVEvent.Free;
FMPVEvent := NIL;
end;
if FRenderMode = rmOpenGL then
UnInitializeRenderGL
{$IFDEF SDL2}
else if FRenderMode = rmSDL2 then
UnInitializeRenderSDL
{$ENDIF};
if Assigned(mpv_terminate_destroy) and Assigned(FMPV_HANDLE) then
mpv_terminate_destroy(FMPV_HANDLE^);
FMPV_HANDLE := NIL;
SetLength(FTrackList, 0);
FFileName := '';
FPausePosMs := -1;
FInitialized := False;
end;
// -----------------------------------------------------------------------------
function TMPVPlayer.InitializeRenderGL: Boolean;
begin
FGL := TUWOpenGLControl.Create(Self);
FGL.Parent := Self;
FGL.Align := alClient;
FGL.OnClick := OnClick;
FGL.OnMouseWheelUp := OnMouseWheelUp;
FGL.OnMouseWheelDown := OnMouseWheelDown;
FGL.OnPaint := @DoOnPaint; // force to draw opengl context when paused
FRenderGL := TMPVPlayerRenderGL.Create(FGL, FMPV_HANDLE {$IFDEF BGLCONTROLS}, FOnDrawEvent{$ENDIF});
Result := FRenderGL.Active;
if Result then
begin
mpv_set_option_string_('vo=libmpv');
mpv_set_option_string_('gpu-hwdec-interop=auto');
end;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.UnInitializeRenderGL;
begin
if Assigned(FRenderGL) then
begin
FRenderGL.Free;
FRenderGL := NIL;
end;
if Assigned(FGL) then
begin
FGL.Free;
FGL := NIL;
Invalidate;
end;
end;
// -----------------------------------------------------------------------------
{$IFDEF SDL2}
function TMPVPlayer.InitializeRenderSDL: Boolean;
begin
FRenderSDL := TMPVPlayerRenderSDL.Create(Handle, FMPV_HANDLE);
Result := FRenderSDL.Active;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayer.UnInitializeRenderSDL;
begin
if Assigned(FRenderSDL) then
begin
FRenderSDL.Free;
FRenderSDL := NIL;
end;
end;
{$ENDIF}
// -----------------------------------------------------------------------------