-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MPVPlayer.RenderSDL.pas
332 lines (259 loc) · 9.13 KB
/
MPVPlayer.RenderSDL.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
{*
* 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.RenderSDL;
// -----------------------------------------------------------------------------
{$I MPVPlayer.inc}
interface
uses
Classes, SysUtils, LCLType, sdl2lib, libMPV.Client, libMPV.Render;
// -----------------------------------------------------------------------------
type
{ TMPVPlayerRenderSDL }
TMPVPlayerRenderSDL = class;
{ TMPVPlayerRenderThread }
TMPVPlayerRenderThread = class(TThread)
private
FHandle : HWND;
FError : mpv_error;
mpvHandle : Pmpv_handle;
mpvRenderParams : array of mpv_render_param;
mpvUpdateParams : array of mpv_render_param;
mpvRenderContext : pmpv_render_context;
sdlWindow : PSDL_Window;
sdlRenderer : PSDL_Renderer;
sdlTexture : PSDL_Texture;
sdlTexSize : array[0..1] of Integer;
procedure InvalidateContext;
protected
procedure TerminatedSet; override;
public
Owner : TMPVPlayerRenderSDL;
Event : PRTLEvent;
IsRenderActive : Boolean;
ForceInvalidateContext : Boolean;
constructor Create(ACtrlHandle: HWND; AMPVHandle: Pmpv_handle; AOwner: TMPVPlayerRenderSDL);
destructor Destroy; override;
procedure Execute; override;
function InitializeRenderContext: Boolean;
procedure UnInitializeRenderContext;
end;
{ TMPVPlayerRenderSDL }
TMPVPlayerRenderSDL = class
private
FThread : TMPVPlayerRenderThread;
function GetRenderActive: Boolean;
public
constructor Create(ACtrlHandle: HWND; AMPVHandle: Pmpv_handle);
destructor Destroy; override;
procedure Render(const ForceInvalidate: Boolean = False);
property Active : Boolean read GetRenderActive;
end;
// -----------------------------------------------------------------------------
implementation
// -----------------------------------------------------------------------------
{ libmpv render wakeup_events }
// -----------------------------------------------------------------------------
procedure LIBMPV_RENDER_EVENT(Sender: Pointer); cdecl;
begin
if (Sender <> NIL) then TMPVPlayerRenderSDL(Sender).Render;
end;
// -----------------------------------------------------------------------------
{ TMPVPlayerRenderThread }
// -----------------------------------------------------------------------------
constructor TMPVPlayerRenderThread.Create(ACtrlHandle: HWND; AMPVHandle: Pmpv_handle; AOwner: TMPVPlayerRenderSDL);
begin
inherited Create(True);
FreeOnTerminate := True;
Event := RTLEventCreate;
FHandle := ACtrlHandle;
mpvHandle := AMPVHandle;
Owner := AOwner;
mpvRenderContext := NIL;
IsRenderActive := False;
ForceInvalidateContext := False;
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);
inherited TerminatedSet;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.Execute;
begin
while not Terminated do
begin
RTLEventWaitFor(Event);
if ForceInvalidateContext then
begin
ForceInvalidateContext := False;
InvalidateContext;
end
else if IsRenderActive and ((mpv_render_context_update(mpvRenderContext^) and MPV_RENDER_UPDATE_FRAME) > 0) then //while ((mpv_render_context_update(mpvRenderContext^) and MPV_RENDER_UPDATE_FRAME) <> 0) do
InvalidateContext;
RTLEventResetEvent(Event);
end;
end;
// -----------------------------------------------------------------------------
function TMPVPlayerRenderThread.InitializeRenderContext: Boolean;
begin
Result := False;
SDL2LIB_Initialize;
SDL_SetHint(SDL_HINT_NO_SIGNAL_HANDLERS, 'no');
// Initilization of video subsystem
FError := SDL_Init(SDL_INIT_VIDEO);
if FError < 0 then // 0 on success and a negative error code on failure.
Exit;
sdlWindow := SDL_CreateWindowFrom(Pointer(FHandle));
if sdlWindow = NIL then // Failed to create SDL window
begin
FError := -1;
Exit;
end;
sdlRenderer := SDL_CreateRenderer(sdlWindow, -1, 0);
if @sdlRenderer = NIL then
begin
FError := -2;
Exit;
end;
// Initialize params
SetLength(mpvRenderParams, 3);
mpvRenderParams[0]._type := MPV_RENDER_PARAM_API_TYPE;
mpvRenderParams[0].Data := PChar(MPV_RENDER_API_TYPE_SW);
mpvRenderParams[1]._type := MPV_RENDER_PARAM_ADVANCED_CONTROL;
mpvRenderParams[1].Data := @MPV_RENDER_PARAM_ADVANCED_CONTROL_ENABLED;
mpvRenderParams[2]._type := MPV_RENDER_PARAM_INVALID;
mpvRenderParams[2].Data := NIL;
FError := mpv_render_context_create(mpvRenderContext, mpvHandle^, Pmpv_render_param(@mpvRenderParams[0]));
if FError <> MPV_ERROR_SUCCESS then Exit;
SetLength(mpvUpdateParams, 5);
mpvUpdateParams[0]._type := MPV_RENDER_PARAM_SW_SIZE;
mpvUpdateParams[1]._type := MPV_RENDER_PARAM_SW_FORMAT;
mpvUpdateParams[1].Data := PChar('0bgr');
mpvUpdateParams[2]._type := MPV_RENDER_PARAM_SW_STRIDE;
mpvUpdateParams[3]._type := MPV_RENDER_PARAM_SW_POINTER;
mpvUpdateParams[4]._type := MPV_RENDER_PARAM_INVALID;
mpvUpdateParams[4].Data := NIL;
mpv_render_context_set_update_callback(mpvRenderContext^, @LIBMPV_RENDER_EVENT, Owner);
sdlTexture := NIL;
sdlTexSize[0] := 0;
sdlTexSize[1] := 0;
IsRenderActive := True;
Result := True;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.UnInitializeRenderContext;
begin
IsRenderActive := False;
if Assigned(mpvRenderContext) then
begin
mpv_render_context_set_update_callback(mpvRenderContext^, NIL, NIL);
mpv_render_context_free(mpvRenderContext^);
end;
SDL_DestroyTexture(sdlTexture);
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
SDL_Quit;
SDL2LIB_Finalize;
SetLength(mpvRenderParams, 0);
SetLength(mpvUpdateParams, 0);
UnInitialize_libMPV_Render;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderThread.InvalidateContext;
var
w, h : Integer;
pixels : Pointer;
pitch : size_t;
begin
if not Terminated and IsRenderActive then
begin
SDL_GetWindowSize(sdlWindow, @w, @h);
if not Assigned(sdlTexture) or (sdlTexSize[0] <> w) or (sdlTexSize[1] <> h) then
begin
SDL_DestroyTexture(sdlTexture);
sdlTexture := SDL_CreateTexture(sdlRenderer, SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STREAMING, w, h);
sdlTexSize[0] := w;
sdlTexSize[1] := h;
end;
SDL_LockTexture(sdlTexture, NIL, @pixels, @pitch);
mpvUpdateParams[0].Data := @sdlTexSize[0];
mpvUpdateParams[2].Data := @pitch;
mpvUpdateParams[3].Data := pixels;
FError := mpv_render_context_render(mpvRenderContext^, Pmpv_render_param(@mpvUpdateParams[0]));
SDL_UnlockTexture(sdlTexture);
SDL_RenderCopy(sdlRenderer, sdlTexture, NIL, NIL);
SDL_RenderPresent(sdlRenderer);
end;
end;
// -----------------------------------------------------------------------------
{ TMPVPlayerRenderSDL }
// -----------------------------------------------------------------------------
constructor TMPVPlayerRenderSDL.Create(ACtrlHandle: HWND; AMPVHandle: Pmpv_handle);
begin
if Initialize_libMPV_Render(hLibMPV) then
begin
FThread := TMPVPlayerRenderThread.Create(ACtrlHandle, AMPVHandle, Self);
FThread.InitializeRenderContext;
FThread.Start;
end;
end;
// -----------------------------------------------------------------------------
destructor TMPVPlayerRenderSDL.Destroy;
begin
if Assigned(FThread) then
begin
FThread.Terminate;
{$IFDEF WINDOWS}
FThread.WaitFor;
{$ENDIF}
FThread := NIL;
end;
inherited Destroy;
end;
// -----------------------------------------------------------------------------
procedure TMPVPlayerRenderSDL.Render(const ForceInvalidate: Boolean = False);
begin
if Assigned(FThread) then
begin
FThread.ForceInvalidateContext := ForceInvalidate;
RTLEventSetEvent(FThread.Event);
end;
end;
// -----------------------------------------------------------------------------
function TMPVPlayerRenderSDL.GetRenderActive: Boolean;
begin
if Assigned(FThread) then
Result := FThread.IsRenderActive
else
Result := False;
end;
// -----------------------------------------------------------------------------
end.