-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecorder.pas
443 lines (343 loc) · 10.5 KB
/
recorder.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
unit recorder;
{$mode ObjFPC}{$H+}
interface
uses
classes, sysutils, syncobjs, intfgraphics, graphtype, fileutil, process;
type
TRecorder = class;
TCompressedFrames = record
Buffer: Pointer;
BufferSize: Integer;
CompressedSize: Integer;
end;
PCompressedFrames = ^TCompressedFrames;
TCompressedFramesArray = array of TCompressedFrames;
TCompressThread = class(TThread)
public
FRecorder: TRecorder;
FData: PByte;
FDataSize: SizeInt;
FCompressing: Boolean;
FCompressingTime: UInt64;
FCompressedFrames: PCompressedFrames;
FCompressEvent: TSimpleEvent;
FCompressBufferSize: SizeInt;
FCompressBuffer: PByte;
procedure Execute; override;
public
constructor Create(Recorder: TRecorder); reintroduce;
destructor Destroy; override;
end;
PRecorder = ^TRecorder;
PRecorderGetFrame = ^TRecorderGetFrame;
PRecorderGetTerminated = ^TRecorderGetTerminated;
TRecorderGetFrame = function(Sender: TRecorder): Pointer;
TRecorderGetTerminated = function(Sender: TRecorder): Boolean;
TRecorder = class
public
const
FPS = 15;
FRAME_COMPRESS_COUNT = 10; // Compress 10 frames at a time
protected
FFMPEGPath: String;
FFrameWidth, FFrameHeight: Integer;
FFrameSize: Integer;
FRawFrameIndex: Integer;
FRawFrames: PByte;
FRawFramesSize: SizeInt;
FRawFramesBackBuffer: PByte;
FCompressedFrames: TCompressedFramesArray;
FCompressThread: TCompressThread;
FGetFrame: TRecorderGetFrame;
FGetTerminated: TRecorderGetTerminated;
FDirectory: String;
FDebugging: Boolean;
procedure Debug(S: String; Args: array of const);
procedure Error(S: String; Args: array of const);
procedure AddFrame(const Data: PByte);
procedure GenerateVideo;
public
constructor Create(Seconds: Integer; Directory: String; Width, Height: Integer; GetFrameFunc: TRecorderGetFrame; GetTerminatedFunc: TRecorderGetTerminated);
destructor Destroy; override;
procedure Run(Debugging: Boolean = False);
property FFMPEG: String read FFMPEGPath write FFMPEGPath;
end;
implementation
uses
FPImage, FPWritePNG, ZStream, lz4;
const
ONE_MB = 1024 * 1024;
function IntToMB(const Bytes: Integer): String;
begin
Result := Format('%f', [Bytes / ONE_MB]) + 'mb';
end;
procedure Swap(var A, B: Pointer); inline;
var
T: Pointer;
begin
T := A;
A := B;
B := T;
end;
procedure ShiftUp(var CompressedFrames: TCompressedFramesArray); inline;
var
Temp: TCompressedFrames;
begin
Temp := CompressedFrames[High(CompressedFrames)];
Temp.CompressedSize := 0;
Move(CompressedFrames[0], CompressedFrames[1], High(CompressedFrames) * SizeOf(TCompressedFrames));
CompressedFrames[0] := Temp;
end;
procedure TCompressThread.Execute;
begin
try
while (not Terminated) do
begin
if (FCompressEvent.WaitFor(1000) = wrSignaled) then
with FCompressedFrames^ do
begin
FCompressing := True;
FCompressingTime := GetTickCount64();
CompressedSize := LZ4_compress_default(FData, FCompressBuffer, FDataSize, FCompressBufferSize);
if (CompressedSize <= 0) then
FRecorder.Error('Compress error: %d', [CompressedSize])
else
begin
if FRecorder.FDebugging then
FRecorder.Debug('Compressed %s to %s in %dms', [IntToMB(FCompressBufferSize), IntToMB(CompressedSize), GetTickCount64() - FCompressingTime]);
if (BufferSize < CompressedSize) then
begin
BufferSize := CompressedSize + ONE_MB;
ReAllocMem(Buffer, BufferSize);
end;
Move(FCompressBuffer^, Buffer^, CompressedSize);
end;
FCompressing := False;
end;
FCompressEvent.ResetEvent();
end;
except
on E: Exception do
begin
FRecorder.Error(E.Message, []);
raise;
end;
end;
end;
constructor TCompressThread.Create(Recorder: TRecorder);
begin
inherited Create(False);
FreeOnTerminate := True;
FRecorder := Recorder;
FDataSize := FRecorder.FRawFramesSize;
FCompressBufferSize := LZ4_compressBound(FDataSize) + ONE_MB;
FCompressBuffer := GetMem(FCompressBufferSize);
FCompressEvent := TSimpleEvent.Create();
end;
destructor TCompressThread.Destroy;
begin
if (FCompressEvent <> nil) then
FCompressEvent.Free();
FreeMem(FCompressBuffer);
inherited Destroy();
end;
procedure TRecorder.Debug(S: String; Args: array of const);
begin
try
WriteLn('[Recorder]: ' + Format(S, Args));
except
end;
end;
procedure TRecorder.Error(S: String; Args: array of const);
procedure AppendFile(const FileName, S: String);
var
Attempts: Integer;
Handle: THandle;
begin
if (Length(S) = 0) then
Exit;
Attempts := 0;
while (Attempts < 5) do
begin
Inc(Attempts);
if not FileExists(FileName) then
Handle := FileCreate(FileName, fmOpenReadWrite or fmShareDenyWrite)
else
Handle := FileOpen(FileName, fmOpenReadWrite or fmShareDenyWrite);
if (Handle > 0) then
begin
FileSeek(Handle, 0, fsFromEnd);
FileWrite(Handle, S[1], Length(S));
FileClose(Handle);
Exit;
end else
Sleep(50);
end;
end;
begin
S := Format(S, Args) + LineEnding;
try
WriteLn('[Recorder]: ' + S);
except
end;
AppendFile(FDirectory + 'log.txt', S);
end;
procedure TRecorder.AddFrame(const Data: PByte);
function GetRamUsage: Integer;
var
I: Integer;
begin
Result := MemSize(FRawFrames) + MemSize(FRawFramesBackBuffer) + MemSize(FCompressThread.FCompressBuffer);
for I := 0 to High(FCompressedFrames) do
Result += FCompressedFrames[I].BufferSize;
end;
begin
Move(Data^, FRawFrames[FRawFrameIndex * FFrameSize], FFrameSize);
Inc(FRawFrameIndex);
if (FRawFrameIndex = FRAME_COMPRESS_COUNT) then
begin
if not FCompressThread.FCompressing then
begin
if FDebugging then
Debug('Memory usage: %s', [IntToMB(GetRamUsage())]);
// Drop last frame index (shift to start)
ShiftUp(FCompressedFrames);
// Swap buffer so new frames don't get compressed
Swap(FRawFrames, FRawFramesBackBuffer);
FCompressThread.FData := FRawFramesBackBuffer;
FCompressThread.FCompressedFrames := @FCompressedFrames[0];
FCompressThread.FCompressEvent.SetEvent();
end else
Debug('Dropping frames (still compressing)', []);
FRawFrameIndex := 0;
end;
end;
procedure TRecorder.GenerateVideo;
var
ImageWriter: TFPWriterPNG;
function MakeFile(Name: String; CreateDir, CreateFile: Boolean): String;
var
I: Integer = 0;
begin
Result := FDirectory + Format(Name, [0]);
while DirectoryExists(Result) or FileExists(Result) do
begin
Inc(I);
Result := FDirectory + Format(Name, [I]);
end;
if CreateDir then ForceDirectories(Result);
if CreateFile then FileClose(FileCreate(Result));
end;
procedure Save(Image: TLazIntfImage; FrameDirectory: String; FrameIndex: Integer; Frame: PByte; FrameSize: Integer);
begin
Move(Frame^, Image.PixelData^, FrameSize);
Image.SaveToFile(FrameDirectory + IntToStr(FrameIndex) + '.png', ImageWriter);
end;
var
Image: TLazIntfImage;
RawImage: TRawImage;
I, J, FrameIndex: Integer;
Size: Integer;
Output, FileName, FrameDirectory: String;
begin
Debug('Generating video', []);
FrameIndex := 0;
FrameDirectory := IncludeTrailingPathDelimiter(MakeFile('frames_%d', True, False));
RawImage := Default(TRawImage);
RawImage.Description.Init_BPP32_B8G8R8_BIO_TTB(FFrameWidth, FFrameHeight);
RawImage.CreateData(True);
ImageWriter := TFPWriterPNG.Create();
ImageWriter.CompressionLevel := clfastest;
Image := TLazIntfImage.Create(RawImage, False);
try
for I := High(FCompressedFrames) downto 0 do
with FCompressedFrames[I] do
begin
if (CompressedSize = 0) then
Continue;
Size := LZ4_decompress_safe(Buffer, FRawFrames, CompressedSize, FRawFramesSize);
if (Size <= 0) then
begin
Error('Decompress error: %d', [Size]);
Continue;
end;
for J := 0 to (Size div FFrameSize) - 1 do
begin
Save(Image, FrameDirectory, FrameIndex, @FRawFrames[J * FFrameSize], FFrameSize);
Inc(FrameIndex);
end;
end;
FileName := MakeFile('recording_%d.mp4', False, True);
if (not RunCommand(FFMPEGPath, ['-y', '-framerate', IntToStr(FPS), '-f', 'image2' ,'-i', FrameDirectory + '%d.png', '-vf', 'pad=ceil(iw/2)*2:ceil(ih/2)*2', '-vcodec', 'libx264', '-crf', '25', '-pix_fmt', 'yuv420p', FileName], Output, [poStderrToOutPut])) then
Error(Output, []);
finally
DeleteDirectory(FrameDirectory, False);
if (ImageWriter <> nil) then
ImageWriter.Free();
if (Image <> nil) then
Image.Free();
end;
RawImage.FreeData();
end;
procedure TRecorder.Run(Debugging: Boolean);
procedure Idle(const TimeUsed: Int64);
var
IdleTime: Int64;
begin
IdleTime := (1000 div FPS) - TimeUsed;
if (IdleTime > 0) then
Sleep(IdleTime)
else
Debug('Dropping frames (GetFrame took %dms)', [TimeUsed]);
end;
var
TimeUsed: Int64;
begin
FDebugging := Debugging;
FCompressThread := TCompressThread.Create(Self);
try
while (not FGetTerminated(Self)) do
begin
TimeUsed := GetTickCount64();
AddFrame(FGetFrame(Self));
TimeUsed := GetTickCount64() - TimeUsed;
Idle(TimeUsed);
end;
except
on E: Exception do
Error(E.Message, []);
end;
FCompressThread.Terminate();
FCompressThread.WaitFor();
GenerateVideo();
end;
constructor TRecorder.Create(Seconds: Integer; Directory: String; Width, Height: Integer; GetFrameFunc: TRecorderGetFrame; GetTerminatedFunc: TRecorderGetTerminated);
begin
inherited Create();
FFMPEG := 'ffmpeg.exe';
FDirectory := IncludeTrailingPathDelimiter(ExpandFileName(Directory));
if not DirectoryExists(FDirectory) then
ForceDirectories(FDirectory);
Debug('Saving to %s', [FDirectory]);
FFrameWidth := Width;
FFrameHeight := Height;
FFrameSize := (Width * Height) * 4;
FGetFrame := GetFrameFunc;
FGetTerminated := GetTerminatedFunc;
FRawFrameIndex := 0;
FRawFramesSize := FRAME_COMPRESS_COUNT * FFrameSize;
FRawFrames := GetMem(FRawFramesSize);
FRawFramesBackBuffer := GetMem(FRawFramesSize);
SetLength(FCompressedFrames, (Seconds * FPS) div FRAME_COMPRESS_COUNT);
end;
destructor TRecorder.Destroy;
var
I: Integer;
begin
FreeMem(FRawFrames);
FreeMem(FRawFramesBackBuffer);
for I := 0 to High(FCompressedFrames) do
FreeMem(FCompressedFrames[I].Buffer);
inherited Destroy();
end;
end.