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
|
{* UltraStar Deluxe - Karaoke Game
*
* UltraStar Deluxe is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* $URL$
* $Id$
*}
unit UMediaPlugin;
interface
{$IFDEF FPC}
{$MODE DELPHI }
{$PACKENUM 4} (* use 4-byte enums *)
{$PACKRECORDS C} (* C/C++-compatible record packing *)
{$ELSE}
{$MINENUMSIZE 4} (* use 4-byte enums *)
{$ENDIF}
uses
UMusic,
ctypes;
type
PFileStream = Pointer;
PThread = Pointer;
PMutex = Pointer;
PCond = Pointer;
PMediaPluginCore = ^TMediaPluginCore;
TMediaPluginCore = record
version: cint;
log: procedure(level: cint; msg: PChar; context: PChar); cdecl;
ticksMillis: function(): cuint32; cdecl;
fileOpen: function(utf8Filename: PAnsiChar; mode: cint): PFileStream; cdecl;
fileClose: procedure(stream: PFileStream); cdecl;
fileRead: function(stream: PFileStream; buf: PCuint8; size: cint): cint64; cdecl;
fileWrite: function(stream: PFileStream; buf: PCuint8; size: cint): cint64; cdecl;
fileSeek: function(stream: PFileStream; pos: cint64; whence: cint): cint64; cdecl;
fileSize: function(stream: PFileStream): cint64; cdecl;
threadCreate: function(func: Pointer; data: Pointer): PThread; cdecl;
threadCurrentID: function(): cuint32; cdecl;
threadGetID: function(thread: PThread): cuint32; cdecl;
threadWait: procedure(thread: PThread; status: PCint); cdecl;
threadSleep: procedure(millisecs: cuint32); cdecl;
mutexCreate: function(): PMutex; cdecl;
mutexDestroy: procedure(mutex: PMutex); cdecl;
mutexLock: function(mutex: PMutex): cint; cdecl;
mutexUnlock: function(mutex: PMutex): cint; cdecl;
condCreate: function(): PCond; cdecl;
condDestroy: procedure(cond: PCond); cdecl;
condSignal: function(cond: PCond): cint; cdecl;
condBroadcast: function(cond: PCond): cint; cdecl;
condWait: function(cond: PCond; mutex: PMutex): cint; cdecl;
condWaitTimeout: function(cond: PCond; mutex: PMutex; ms: cuint32): cint; cdecl;
end;
PAudioDecodeStream = Pointer;
PAudioConvertStream = Pointer;
PVideoDecodeStream = Pointer;
PCAudioFormatInfo = ^TCAudioFormatInfo;
TCAudioFormatInfo = record
sampleRate: double;
channels: cuint8;
format: cint;
end;
PAudioDecoderInfo = ^TAudioDecoderInfo;
TAudioDecoderInfo = record
priority: cint;
open: function(filename: PAnsiChar): PAudioDecodeStream; cdecl;
close: procedure(stream: PAudioDecodeStream); cdecl;
getLength: function(stream: PAudioDecodeStream): double; cdecl;
getAudioFormatInfo: procedure(stream: PAudioDecodeStream; var info: TCAudioFormatInfo); cdecl;
getPosition: function(stream: PAudioDecodeStream): double; cdecl;
setPosition: procedure(stream: PAudioDecodeStream; time: double); cdecl;
getLoop: function(stream: PAudioDecodeStream): cbool; cdecl;
setLoop: procedure(stream: PAudioDecodeStream; enabled: cbool); cdecl;
isEOF: function(stream: PAudioDecodeStream): cbool; cdecl;
isError: function(stream: PAudioDecodeStream): cbool; cdecl;
readData: function(stream: PAudioDecodeStream; buffer: PCUint8; bufferSize: cint): cint; cdecl;
end;
PAudioConverterInfo = ^TAudioConverterInfo;
TAudioConverterInfo = record
priority: cint;
open: function(inputFormat: PCAudioFormatInfo; outputFormat: PCAudioFormatInfo): PAudioConvertStream; cdecl;
close: procedure(stream: PAudioConvertStream); cdecl;
convert: function(stream: PAudioConvertStream; input, output: PCuint8; numSamples: PCint): cint; cdecl;
getOutputBufferSize: function(stream: PAudioConvertStream; inputSize: cint): cint; cdecl;
getRatio: function(stream: PAudioConvertStream): double; cdecl;
end;
PVideoDecoderInfo = ^TVideoDecoderInfo;
TVideoDecoderInfo = record
priority: cint;
open: function(filename: PAnsiChar): PVideoDecodeStream; cdecl;
close: procedure(stream: PVideoDecodeStream); cdecl;
setLoop: procedure(stream: PVideoDecodeStream; enable: cbool); cdecl;
getLoop: function(stream: PVideoDecodeStream): cbool; cdecl;
setPosition: procedure(stream: PVideoDecodeStream; time: double); cdecl;
getPosition: function(stream: PVideoDecodeStream): double; cdecl;
getFrameWidth: function(stream: PVideoDecodeStream): cint; cdecl;
getFrameHeight: function(stream: PVideoDecodeStream): cint; cdecl;
getFrameAspect: function(stream: PVideoDecodeStream): double; cdecl;
getFrame: function (stream: PVideoDecodeStream; time: clongdouble): PCuint8; cdecl;
end;
PMediaPluginInfo = ^TMediaPluginInfo;
TMediaPluginInfo = record
version: cint;
name: PAnsiChar;
initialize: function(): cbool; cdecl;
finalize: function(): cbool; cdecl;
audioDecoder: PAudioDecoderInfo;
audioConverter: PAudioConverterInfo;
videoDecoder: PVideoDecoderInfo;
end;
TPluginRegisterFunc = function(core: PMediaPluginCore): PMediaPluginInfo; cdecl;
procedure LoadMediaPlugins();
function MediaPluginCore: PMediaPluginCore;
procedure AudioFormatInfoToCStruct(
const Info: TAudioFormatInfo; var CInfo: TCAudioFormatInfo);
implementation
uses
SysUtils,
SDL,
moduleloader,
UFilesystem,
UPath,
UPathUtils,
ULog,
UAudioDecoderPlugin,
UVideoDecoderPlugin;
var
MediaPluginCore_Instance: TMediaPluginCore;
const
DebugLogLevels: array[0 .. 5] of integer = (
LOG_LEVEL_DEBUG,
LOG_LEVEL_INFO,
LOG_LEVEL_STATUS,
LOG_LEVEL_WARN,
LOG_LEVEL_ERROR,
LOG_LEVEL_CRITICAL
);
function MediaPluginCore: PMediaPluginCore;
begin
Result := @MediaPluginCore_Instance;
end;
procedure AudioFormatInfoToCStruct(
const Info: TAudioFormatInfo; var CInfo: TCAudioFormatInfo);
begin
CInfo.sampleRate := Info.SampleRate;
CInfo.channels := Info.Channels;
CInfo.format := Ord(Info.Format);
end;
{* Misc *}
procedure Core_log(level: cint; msg: PChar; context: PChar); cdecl;
begin
Log.LogMsg(msg, context, DebugLogLevels[level]);
end;
function Core_ticksMillis(): cuint32; cdecl;
begin
Result := SDL_GetTicks();
end;
{* File *}
function Core_fileOpen(utf8Filename: PChar; mode: cint): PFileStream; cdecl;
begin
end;
procedure Core_fileClose(stream: PFileStream); cdecl;
begin
end;
function Core_fileRead(stream: PFileStream; buf: PCuint8; size: cint): cint64; cdecl;
begin
end;
function Core_fileWrite(stream: PFileStream; buf: PCuint8; size: cint): cint64; cdecl;
begin
end;
function Core_fileSeek(stream: PFileStream; pos: cint64; whence: cint): cint64; cdecl;
begin
end;
function Core_fileSize(stream: PFileStream): cint64; cdecl;
begin
end;
{* Thread *}
function Core_threadCreate(func: Pointer; data: Pointer): PThread; cdecl;
begin
Result := SDL_CreateThread(func, data);
end;
function Core_threadCurrentID(): cuint32; cdecl;
begin
Result := SDL_ThreadID();
end;
function Core_threadGetID(thread: PThread): cuint32; cdecl;
begin
Result := SDL_GetThreadID(PSDL_Thread(thread));
end;
procedure Core_threadWait(thread: PThread; status: PCint); cdecl;
begin
SDL_WaitThread(PSDL_Thread(thread), status^);
end;
procedure Core_threadSleep(millisecs: cuint32); cdecl;
begin
SDL_Delay(millisecs);
end;
{* Mutex *}
function Core_mutexCreate(): PMutex; cdecl;
begin
Result := PMutex(SDL_CreateMutex());
end;
procedure Core_mutexDestroy(mutex: PMutex); cdecl;
begin
SDL_DestroyMutex(PSDL_Mutex(mutex));
end;
function Core_mutexLock(mutex: PMutex): cint; cdecl;
begin
Result := SDL_mutexP(PSDL_Mutex(mutex));
end;
function Core_mutexUnlock(mutex: PMutex): cint; cdecl;
begin
Result := SDL_mutexV(PSDL_Mutex(mutex));
end;
{* Condition *}
function Core_condCreate(): PCond; cdecl;
begin
Result := PCond(SDL_CreateCond());
end;
procedure Core_condDestroy(cond: PCond); cdecl;
begin
SDL_DestroyCond(PSDL_Cond(cond));
end;
function Core_condSignal(cond: PCond): cint; cdecl;
begin
Result := SDL_CondSignal(PSDL_Cond(cond));
end;
function Core_condBroadcast(cond: PCond): cint; cdecl;
begin
Result := SDL_CondBroadcast(PSDL_Cond(cond));
end;
function Core_condWait(cond: PCond; mutex: PMutex): cint; cdecl;
begin
Result := SDL_CondWait(PSDL_Cond(cond), PSDL_Mutex(mutex));
end;
function Core_condWaitTimeout(cond: PCond; mutex: PMutex; ms: cuint32): cint; cdecl;
begin
Result := SDL_CondWaitTimeout(PSDL_Cond(cond), PSDL_Mutex(mutex), ms);
end;
procedure InitializeMediaPluginCore;
begin
with MediaPluginCore_Instance do
begin
version := 0;
log := Core_log;
ticksMillis := Core_ticksMillis;
fileOpen := Core_fileOpen;
fileClose := Core_fileClose;
fileRead := Core_fileRead;
fileWrite := Core_fileWrite;
fileSeek := Core_fileSeek;
fileSize := Core_fileSize;
threadCreate := Core_threadCreate;
threadCurrentID := Core_threadCurrentID;
threadGetID := Core_threadGetID;
threadWait := Core_threadWait;
threadSleep := Core_threadSleep;
mutexCreate := Core_mutexCreate;
mutexDestroy := Core_mutexDestroy;
mutexLock := Core_mutexLock;
mutexUnlock := Core_mutexUnlock;
condCreate := Core_condCreate;
condDestroy := Core_condDestroy;
condSignal := Core_condSignal;
condBroadcast := Core_condBroadcast;
condWait := Core_condWait;
condWaitTimeout := Core_condWaitTimeout;
end;
end;
procedure LoadMediaPlugins();
var
LibPath: IPath;
Iter: IFileIterator;
FileInfo: TFileInfo;
ModuleFile: IPath;
Module: TModuleHandle;
RegisterFunc: TPluginRegisterFunc;
PluginInfo: PMediaPluginInfo;
begin
LibPath := MediaPluginPath.Append('*.dll');
Iter := FileSystem.FileFind(LibPath, faAnyFile);
while (Iter.HasNext) do
begin
FileInfo := Iter.Next();
ModuleFile := MediaPluginPath.Append(FileInfo.Name);
if (not LoadModule(Module, PChar(ModuleFile.ToNative))) then
begin
Log.LogInfo('Failed to load media plugin: "' + FileInfo.Name.ToNative + '"',
'LoadMediaPlugins');
Continue;
end;
RegisterFunc := GetModuleSymbol(Module, 'Plugin_register');
if (@RegisterFunc = nil) then
begin
Log.LogError('Invalid media plugin: "' + FileInfo.Name.ToNative + '"',
'LoadMediaPlugins');
UnloadModule(Module);
Continue;
end;
PluginInfo := RegisterFunc(MediaPluginCore);
if (PluginInfo = nil) then
begin
Log.LogError('Invalid media plugin info: "' + FileInfo.Name.ToNative + '"',
'LoadMediaPlugins');
UnloadModule(Module);
Continue;
end;
if (not PluginInfo.initialize()) then
begin
Log.LogError('Failed to initialize media plugin: "' + PluginInfo.name + '"',
'LoadMediaPlugins');
UnloadModule(Module);
Continue;
end;
Log.LogStatus('Loaded media plugin: "' + PluginInfo.name + '"',
'LoadMediaPlugins');
// register modules
if (PluginInfo.audioDecoder <> nil) then
MediaManager.Add(TAudioDecoderPlugin.Create(PluginInfo));
if (PluginInfo.videoDecoder <> nil) then
MediaManager.Add(TVideoDecoderPlugin.Create(PluginInfo));
//if (PluginInfo.audioConverter <> nil) then
// MediaManager.Add();
end;
end;
initialization
InitializeMediaPluginCore;
end.
|