-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MPVPlayer.RenderGL.pas
373 lines (294 loc) · 10.6 KB
/
MPVPlayer.RenderGL.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
{*
* 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].
*}
unit MPVPlayer.RenderGL;
// -----------------------------------------------------------------------------
{$I MPVPlayer.inc}
interface
uses
Classes, SysUtils, libMPV.Client, libMPV.Render, libMPV.Render_gl, gl, glext,
OpenGLContext
{$IFDEF BGLCONTROLS}, BGRAOpenGL{$ENDIF};
// -----------------------------------------------------------------------------
type
{ TUWOpenGLControl }
TUWOpenGLControl = class(TOpenGLControl)
public
procedure Paint; override;
end;
{$IFDEF BGLCONTROLS}
TMPVPlayerDrawEvent = procedure (Sender: TObject; ABGLCanvas: TBGLCustomCanvas) of object;
{$ENDIF}
TMPVPlayerRenderGL = class;
{ TMPVPlayerRenderThread }
TMPVPlayerRenderThread = class(TThread)
private
FGL : TUWOpenGLControl;
FError : mpv_error;
mpvHandle : Pmpv_handle;
mpvRenderParams : array of mpv_render_param;
mpvUpdateParams : array of mpv_render_param;
mpvOpenGLParams : mpv_opengl_init_params;
mpvRenderContext : pmpv_render_context;
mpvfbo : mpv_opengl_fbo;
function InitializeRenderContext: Boolean;
procedure UnInitializeRenderContext;
function IsDestroyingGL: Boolean;
procedure Update_mpvfbo;
protected
procedure TerminatedSet; override;
public
Owner : TMPVPlayerRenderGL;
Event : PRTLEvent;
IsRenderActive : Boolean;
ForceInvalidateContext : Boolean;
Ready : Boolean;
{$IFDEF BGLCONTROLS}
FDrawCallback: TMPVPlayerDrawEvent;
{$ENDIF}
constructor Create(AControl: TUWOpenGLControl; AHandle: Pmpv_handle; AOwner: TMPVPlayerRenderGL {$IFDEF BGLCONTROLS}; ADrawCallback: TMPVPlayerDrawEvent = NIL{$ENDIF});
destructor Destroy; override;
procedure Execute; override;
procedure InvalidateContext(const AReportSwap: Boolean = True);
end;
{ TMPVPlayerRenderGL }
TMPVPlayerRenderGL = class
private
FThread : TMPVPlayerRenderThread;
function GetRenderActive: Boolean;
public
constructor Create(AControl: TUWOpenGLControl; AHandle: Pmpv_handle {$IFDEF BGLCONTROLS}; ADrawCallback: TMPVPlayerDrawEvent = NIL{$ENDIF});
destructor Destroy; override;
procedure Terminate;
procedure Render(const ForceInvalidate: Boolean = False);
property Active : Boolean read GetRenderActive;
end;
// -----------------------------------------------------------------------------
implementation
// -----------------------------------------------------------------------------
{ TUWOpenGLControl }
// -----------------------------------------------------------------------------
procedure TUWOpenGLControl.Paint;
begin
if Assigned(OnPaint) then
OnPaint(Self);
end;
// -----------------------------------------------------------------------------
{ libmpv render wakeup_events }
// -----------------------------------------------------------------------------
function get_proc_address_mpv(ctx: Pointer; Name: PChar): Pointer; cdecl;
begin
Result := GetProcAddress(LibGL, Name);
if Result = NIL then
Result := wglGetProcAddress(Name);
end;
// -----------------------------------------------------------------------------
procedure LIBMPV_RENDER_EVENT(Sender: Pointer); cdecl;
begin
if (Sender <> NIL) then
TMPVPlayerRenderGL(Sender).Render;
end;
// -----------------------------------------------------------------------------
{ TMPVPlayerRenderThread }
// -----------------------------------------------------------------------------
constructor TMPVPlayerRenderThread.Create(AControl: TUWOpenGLControl; AHandle: Pmpv_handle; AOwner: TMPVPlayerRenderGL {$IFDEF BGLCONTROLS}; ADrawCallback: TMPVPlayerDrawEvent = NIL{$ENDIF});
begin
inherited Create(True);
FreeOnTerminate := True;
Event := RTLEventCreate;
Owner := AOwner;
mpvHandle := AHandle;
{$IFDEF BGLCONTROLS}
FDrawCallback := ADrawCallback;
{$ENDIF}
FGL := AControl;
FGL.ReleaseContext;
IsRenderActive := False;
ForceInvalidateContext := False;
Ready := False;
mpvRenderContext := NIL;
end;
// -----------------------------------------------------------------------------
destructor TMPVPlayerRenderThread.Destroy;
begin
UnInitializeRenderContext;
RTLEventDestroy(Event);
Owner := NIL;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.TerminatedSet;
begin
IsRenderActive := False;
ForceInvalidateContext := False;
if Assigned(Event) then RTLEventSetEvent(Event);
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.Execute;
begin
if not InitializeRenderContext then
Exit;
while not Terminated do
begin
RTLEventWaitFor(Event);
if ForceInvalidateContext then
begin
ForceInvalidateContext := False;
InvalidateContext(False);
end
else if IsRenderActive and ((mpv_render_context_update(mpvRenderContext^) and MPV_RENDER_UPDATE_FRAME) > 0) then //while IsRenderActive and ((mpv_render_context_update(mpvRenderContext^) and MPV_RENDER_UPDATE_FRAME) > 0) do
InvalidateContext;
RTLEventResetEvent(Event);
end;
end;
// -----------------------------------------------------------------------------
function TMPVPlayerRenderThread.InitializeRenderContext: Boolean;
begin
Result := False;
try
mpvOpenGLParams.get_proc_address := @get_proc_address_mpv;
mpvOpenGLParams.get_proc_address_ctx := NIL;
// Initialize params
SetLength(mpvRenderParams, 4);
mpvRenderParams[0]._type := MPV_RENDER_PARAM_API_TYPE;
mpvRenderParams[0].Data := PChar(MPV_RENDER_API_TYPE_OPENGL);
mpvRenderParams[1]._type := MPV_RENDER_PARAM_OPENGL_INIT_PARAMS;
mpvRenderParams[1].Data := @mpvOpenGLParams;
mpvRenderParams[2]._type := MPV_RENDER_PARAM_ADVANCED_CONTROL;
mpvRenderParams[2].Data := @MPV_RENDER_PARAM_ADVANCED_CONTROL_ENABLED;
mpvRenderParams[3]._type := MPV_RENDER_PARAM_INVALID;
mpvRenderParams[3].Data := NIL;
FGL.MakeCurrent();
FError := mpv_render_context_create(mpvRenderContext, mpvHandle^, Pmpv_render_param(@mpvRenderParams[0]));
if FError <> MPV_ERROR_SUCCESS then Exit;
mpv_render_context_set_update_callback(mpvRenderContext^, @LIBMPV_RENDER_EVENT, Owner);
// Update params
Update_mpvfbo;
SetLength(mpvUpdateParams, 3);
mpvUpdateParams[0]._type := MPV_RENDER_PARAM_OPENGL_FBO;
mpvUpdateParams[0].Data := @mpvfbo;
mpvUpdateParams[1]._type := MPV_RENDER_PARAM_FLIP_Y;
mpvUpdateParams[1].Data := @MPV_RENDER_PARAM_ADVANCED_CONTROL_ENABLED;
mpvUpdateParams[2]._type := MPV_RENDER_PARAM_INVALID;
mpvUpdateParams[2].Data := NIL;
IsRenderActive := True;
Result := True;
finally
Ready := True;
end;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.UnInitializeRenderContext;
begin
if Assigned(mpvRenderContext) then
begin
mpv_render_context_set_update_callback(mpvRenderContext^, NIL, NIL);
mpv_render_context_free(mpvRenderContext^);
mpvRenderContext := NIL;
end;
SetLength(mpvRenderParams, 0);
SetLength(mpvUpdateParams, 0);
UnInitialize_libMPV_Render;
end;
// -----------------------------------------------------------------------------
function TMPVPlayerRenderThread.IsDestroyingGL: Boolean;
begin
Result := (csDestroying in FGL.ComponentState);
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.Update_mpvfbo;
begin
mpvfbo.internal_format := 0;
mpvfbo.fbo := 0;
mpvfbo.w := FGL.ClientWidth;
mpvfbo.h := FGL.ClientHeight;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.InvalidateContext(const AReportSwap: Boolean = True);
begin
Update_mpvfbo;
if not Terminated and IsRenderActive then
begin
if not IsDestroyingGL then
FGL.MakeCurrent();
mpv_render_context_render(mpvRenderContext^, Pmpv_render_param(@mpvUpdateParams[0]));
{$IFDEF BGLCONTROLS}
if Assigned(FDrawCallback) then
begin
BGLViewPort(FGL.ClientWidth, FGL.ClientHeight);
FDrawCallback(Self, BGLCanvas);
end;
{$ENDIF}
if IsRenderActive and not IsDestroyingGL then
begin
FGL.SwapBuffers;
if AReportSwap then
mpv_render_context_report_swap(mpvRenderContext^);
end;
end;
end;
// -----------------------------------------------------------------------------
{ TMPVPlayerRenderGL }
// -----------------------------------------------------------------------------
constructor TMPVPlayerRenderGL.Create(AControl: TUWOpenGLControl; AHandle: Pmpv_handle {$IFDEF BGLCONTROLS}; ADrawCallback: TMPVPlayerDrawEvent = NIL{$ENDIF});
begin
if Initialize_libMPV_Render(hLibMPV) then
begin
FThread := TMPVPlayerRenderThread.Create(AControl, AHandle, Self {$IFDEF BGLCONTROLS}, ADrawCallback{$ENDIF});
FThread.Start;
while not FThread.Ready do Sleep(100);
end;
end;
// -----------------------------------------------------------------------------
destructor TMPVPlayerRenderGL.Destroy;
begin
Terminate;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderGL.Terminate;
begin
if Assigned(FThread) then
begin
FThread.Terminate;
{$IFDEF WINDOWS}
FThread.WaitFor;
{$ENDIF}
FThread := NIL;
end;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderGL.Render(const ForceInvalidate: Boolean = False);
begin
if Assigned(FThread) then
begin
FThread.ForceInvalidateContext := ForceInvalidate;
RTLEventSetEvent(FThread.Event);
end;
end;
// -----------------------------------------------------------------------------
function TMPVPlayerRenderGL.GetRenderActive: Boolean;
begin
if Assigned(FThread) then
Result := FThread.IsRenderActive
else
Result := False;
end;
// -----------------------------------------------------------------------------
end.