author | Stepan777 <stepik-777@mail.ru> |
Sat, 11 Aug 2012 21:25:57 +0400 | |
changeset 7540 | cc6c656f70b5 |
parent 7538 | 2d6e69b392cb |
child 7671 | 43f38923bc6e |
permissions | -rw-r--r-- |
7180 | 1 |
(* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2004-2012 Andrey Korotaev <unC0Rr@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
17 |
*) |
|
18 |
||
7286 | 19 |
|
7180 | 20 |
{$INCLUDE "options.inc"} |
21 |
||
22 |
unit uVideoRec; |
|
23 |
||
7286 | 24 |
{$IFNDEF USE_VIDEO_RECORDING} |
25 |
interface |
|
26 |
implementation |
|
27 |
end. |
|
28 |
{$ELSE} |
|
29 |
||
7538 | 30 |
{$IFNDEF WIN32} |
7540 | 31 |
{$LINKLIB ../bin/libavwrapper.a} |
7180 | 32 |
{$ENDIF} |
33 |
||
34 |
interface |
|
35 |
||
36 |
var flagPrerecording: boolean = false; |
|
37 |
||
38 |
function BeginVideoRecording: Boolean; |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
39 |
function LoadNextCameraPosition(var newRealTicks, newGameTicks: LongInt): Boolean; |
7180 | 40 |
procedure EncodeFrame; |
41 |
procedure StopVideoRecording; |
|
42 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
43 |
procedure BeginPreRecording; |
7180 | 44 |
procedure StopPreRecording; |
45 |
procedure SaveCameraPosition; |
|
46 |
||
47 |
procedure freeModule; |
|
48 |
||
49 |
implementation |
|
50 |
||
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
51 |
uses uVariables, uUtils, GLunit, SDLh, SysUtils, uIO, uMisc, uTypes; |
7180 | 52 |
|
53 |
type TAddFileLogRaw = procedure (s: pchar); cdecl; |
|
54 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
55 |
procedure AVWrapper_Init( |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
56 |
AddLog: TAddFileLogRaw; |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
57 |
filename, desc, soundFile, format, vcodec, acodec: PChar; |
7540 | 58 |
width, height, framerateNum, framerateDen, vquality: LongInt); cdecl; external {$IFDEF WIN32}'libavwrapper.dll'{$ENDIF}; |
59 |
procedure AVWrapper_Close; cdecl; external {$IFDEF WIN32}'libavwrapper.dll'{$ENDIF}; |
|
60 |
procedure AVWrapper_WriteFrame( pY, pCb, pCr: PByte ); cdecl; external {$IFDEF WIN32}'libavwrapper.dll'{$ENDIF}; |
|
7180 | 61 |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
62 |
type TFrame = record |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
63 |
realTicks: LongWord; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
64 |
gameTicks: LongWord; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
65 |
CamX, CamY: LongInt; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
66 |
zoom: single; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
67 |
end; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
68 |
|
7180 | 69 |
var YCbCr_Planes: array[0..2] of PByte; |
70 |
RGB_Buffer: PByte; |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
71 |
cameraFile: File of TFrame; |
7180 | 72 |
audioFile: File; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
73 |
numPixels: LongWord; |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
74 |
startTime, numFrames, curTime, progress, maxProgress: LongWord; |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
75 |
soundFilePath: shortstring; |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
76 |
thumbnailSaved : Boolean; |
7180 | 77 |
|
78 |
function BeginVideoRecording: Boolean; |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
79 |
var filename, desc: shortstring; |
7180 | 80 |
begin |
81 |
AddFileLog('BeginVideoRecording'); |
|
82 |
||
83 |
{$IOCHECKS OFF} |
|
84 |
// open file with prerecorded camera positions |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
85 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.txtin'; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
86 |
Assign(cameraFile, filename); |
7180 | 87 |
Reset(cameraFile); |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
88 |
maxProgress:= FileSize(cameraFile); |
7180 | 89 |
if IOResult <> 0 then |
90 |
begin |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
91 |
AddFileLog('Error: Could not read from ' + filename); |
7180 | 92 |
exit(false); |
93 |
end; |
|
94 |
{$IOCHECKS ON} |
|
95 |
||
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
96 |
// store some description in output file |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
97 |
desc:= ''; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
98 |
if UserNick <> '' then |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
99 |
desc+= 'Player: ' + UserNick + #10; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
100 |
if recordFileName <> '' then |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
101 |
desc+= 'Record: ' + recordFileName + #10; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
102 |
if cMapName <> '' then |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
103 |
desc+= 'Map: ' + cMapName + #10; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
104 |
if Theme <> '' then |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
105 |
desc+= 'Theme: ' + Theme + #10; |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
106 |
desc+= 'prefix[' + RecPrefix + ']prefix'; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
107 |
desc+= #0; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
108 |
|
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
109 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + #0; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
110 |
soundFilePath:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw' + #0; |
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
111 |
cAVFormat+= #0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
112 |
cAudioCodec+= #0; |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
113 |
cVideoCodec+= #0; |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
114 |
AVWrapper_Init(@AddFileLogRaw, @filename[1], @desc[1], @soundFilePath[1], @cAVFormat[1], @cVideoCodec[1], @cAudioCodec[1], |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
115 |
cScreenWidth, cScreenHeight, cVideoFramerateNum, cVideoFramerateDen, cVideoQuality); |
7180 | 116 |
|
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
117 |
numPixels:= cScreenWidth*cScreenHeight; |
7180 | 118 |
YCbCr_Planes[0]:= GetMem(numPixels); |
119 |
YCbCr_Planes[1]:= GetMem(numPixels div 4); |
|
120 |
YCbCr_Planes[2]:= GetMem(numPixels div 4); |
|
121 |
||
122 |
if (YCbCr_Planes[0] = nil) or (YCbCr_Planes[1] = nil) or (YCbCr_Planes[2] = nil) then |
|
123 |
begin |
|
124 |
AddFileLog('Error: Could not allocate memory for video recording (YCbCr buffer).'); |
|
125 |
exit(false); |
|
126 |
end; |
|
127 |
||
128 |
RGB_Buffer:= GetMem(4*numPixels); |
|
129 |
if RGB_Buffer = nil then |
|
130 |
begin |
|
131 |
AddFileLog('Error: Could not allocate memory for video recording (RGB buffer).'); |
|
132 |
exit(false); |
|
133 |
end; |
|
134 |
||
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
135 |
curTime:= 0; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
136 |
numFrames:= 0; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
137 |
progress:= 0; |
7180 | 138 |
BeginVideoRecording:= true; |
139 |
end; |
|
140 |
||
141 |
procedure StopVideoRecording; |
|
142 |
begin |
|
143 |
AddFileLog('StopVideoRecording'); |
|
144 |
FreeMem(YCbCr_Planes[0], numPixels); |
|
145 |
FreeMem(YCbCr_Planes[1], numPixels div 4); |
|
146 |
FreeMem(YCbCr_Planes[2], numPixels div 4); |
|
147 |
FreeMem(RGB_Buffer, 4*numPixels); |
|
148 |
Close(cameraFile); |
|
149 |
AVWrapper_Close(); |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
150 |
Erase(cameraFile); |
7180 | 151 |
DeleteFile(soundFilePath); |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
152 |
SendIPC(_S'v'); // inform frontend that we finished |
7180 | 153 |
end; |
154 |
||
155 |
function pixel(x, y, color: LongInt): LongInt; |
|
156 |
begin |
|
157 |
pixel:= RGB_Buffer[(cScreenHeight-y-1)*cScreenWidth*4 + x*4 + color]; |
|
158 |
end; |
|
159 |
||
160 |
procedure EncodeFrame; |
|
161 |
var x, y, r, g, b: LongInt; |
|
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
162 |
s: shortstring; |
7180 | 163 |
begin |
164 |
// read pixels from OpenGL |
|
165 |
glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_RGBA, GL_UNSIGNED_BYTE, RGB_Buffer); |
|
166 |
||
167 |
// convert to YCbCr 4:2:0 format |
|
168 |
// Y |
|
169 |
for y := 0 to cScreenHeight-1 do |
|
170 |
for x := 0 to cScreenWidth-1 do |
|
171 |
YCbCr_Planes[0][y*cScreenWidth + x]:= Byte(16 + ((16828*pixel(x,y,0) + 33038*pixel(x,y,1) + 6416*pixel(x,y,2)) shr 16)); |
|
172 |
||
173 |
// Cb and Cr |
|
174 |
for y := 0 to cScreenHeight div 2 - 1 do |
|
175 |
for x := 0 to cScreenWidth div 2 - 1 do |
|
176 |
begin |
|
177 |
r:= pixel(2*x,2*y,0) + pixel(2*x+1,2*y,0) + pixel(2*x,2*y+1,0) + pixel(2*x+1,2*y+1,0); |
|
178 |
g:= pixel(2*x,2*y,1) + pixel(2*x+1,2*y,1) + pixel(2*x,2*y+1,1) + pixel(2*x+1,2*y+1,1); |
|
179 |
b:= pixel(2*x,2*y,2) + pixel(2*x+1,2*y,2) + pixel(2*x,2*y+1,2) + pixel(2*x+1,2*y+1,2); |
|
180 |
YCbCr_Planes[1][y*(cScreenWidth div 2) + x]:= Byte(128 + ((-2428*r - 4768*g + 7196*b) shr 16)); |
|
181 |
YCbCr_Planes[2][y*(cScreenWidth div 2) + x]:= Byte(128 + (( 7196*r - 6026*g - 1170*b) shr 16)); |
|
182 |
end; |
|
183 |
||
184 |
AVWrapper_WriteFrame(YCbCr_Planes[0], YCbCr_Planes[1], YCbCr_Planes[2]); |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
185 |
|
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
186 |
// inform frontend that we have encoded new frame |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
187 |
s[0]:= #3; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
188 |
s[1]:= 'p'; // p for progress |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
189 |
SDLNet_Write16(progress*10000 div maxProgress, @s[2]); |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
190 |
SendIPC(s); |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
191 |
inc(numFrames); |
7180 | 192 |
end; |
193 |
||
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
194 |
function LoadNextCameraPosition(var newRealTicks, newGameTicks: LongInt): Boolean; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
195 |
var frame: TFrame; |
7180 | 196 |
begin |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
197 |
// we need to skip or duplicate frames to match target framerate |
7379
aa29a2f16cc7
fix bug with desynced camera positions
Stepan777 <stepik-777@mail.ru>
parents:
7376
diff
changeset
|
198 |
while Int64(curTime)*cVideoFramerateNum <= Int64(numFrames)*cVideoFramerateDen*1000 do |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
199 |
begin |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
200 |
{$IOCHECKS OFF} |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
201 |
if eof(cameraFile) then |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
202 |
exit(false); |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
203 |
BlockRead(cameraFile, frame, 1); |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
204 |
{$IOCHECKS ON} |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
205 |
curTime:= frame.realTicks; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
206 |
WorldDx:= frame.CamX; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
207 |
WorldDy:= frame.CamY + cScreenHeight div 2; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
208 |
zoom:= frame.zoom*cScreenWidth; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
209 |
ZoomValue:= zoom; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
210 |
inc(progress); |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
211 |
newRealTicks:= frame.realTicks; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
212 |
newGameTicks:= frame.gameTicks; |
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
213 |
end; |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
214 |
LoadNextCameraPosition:= true; |
7180 | 215 |
end; |
216 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
217 |
// Callback which records sound. |
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
218 |
// This procedure may be called from different thread. |
7180 | 219 |
procedure RecordPostMix(udata: pointer; stream: PByte; len: LongInt); cdecl; |
220 |
begin |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
221 |
udata:= udata; // avoid warning |
7180 | 222 |
{$IOCHECKS OFF} |
223 |
BlockWrite(audioFile, stream^, len); |
|
224 |
{$IOCHECKS ON} |
|
225 |
end; |
|
226 |
||
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
227 |
procedure SaveThumbnail; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
228 |
var thumbpath: shortstring; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
229 |
k: LongInt; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
230 |
begin |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
231 |
thumbpath:= '/VideoTemp/' + RecPrefix; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
232 |
AddFileLog('Saving thumbnail ' + thumbpath); |
7386
e82a076df09b
Fix bug with isInLag picture displayed at end of some videos.
Stepan777 <stepik-777@mail.ru>
parents:
7379
diff
changeset
|
233 |
k:= max(max(cScreenWidth, cScreenHeight) div 400, 1); // here 400 is minimum size of thumbnail |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
234 |
MakeScreenshot(thumbpath, k); |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
235 |
thumbnailSaved:= true; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
236 |
end; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
237 |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
238 |
// copy file (free pascal doesn't have copy file function) |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
239 |
procedure CopyFile(src, dest: shortstring); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
240 |
var inF, outF: file; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
241 |
buffer: array[0..1023] of byte; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
242 |
result: LongInt; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
243 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
244 |
{$IOCHECKS OFF} |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
245 |
result:= 0; // avoid compiler hint |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
246 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
247 |
Assign(inF, src); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
248 |
Reset(inF, 1); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
249 |
if IOResult <> 0 then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
250 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
251 |
AddFileLog('Error: Could not read from ' + src); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
252 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
253 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
254 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
255 |
Assign(outF, dest); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
256 |
Rewrite(outF, 1); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
257 |
if IOResult <> 0 then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
258 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
259 |
AddFileLog('Error: Could not write to ' + dest); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
260 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
261 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
262 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
263 |
repeat |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
264 |
BlockRead(inF, buffer, 1024, result); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
265 |
BlockWrite(outF, buffer, result); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
266 |
until result < 1024; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
267 |
{$IOCHECKS ON} |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
268 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
269 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
270 |
procedure BeginPreRecording; |
7180 | 271 |
var format: word; |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
272 |
filename: shortstring; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
273 |
frequency, channels: LongInt; |
7180 | 274 |
begin |
275 |
AddFileLog('BeginPreRecording'); |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
276 |
|
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
277 |
thumbnailSaved:= false; |
7386
e82a076df09b
Fix bug with isInLag picture displayed at end of some videos.
Stepan777 <stepik-777@mail.ru>
parents:
7379
diff
changeset
|
278 |
RecPrefix:= 'hw-' + FormatDateTime('YYYY-MM-DD_HH-mm-ss-z', Now()); |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
279 |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
280 |
// If this video is recorded from demo executed directly (without frontend) |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
281 |
// then we need to copy demo so that frontend will be able to find it later. |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
282 |
if recordFileName <> '' then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
283 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
284 |
if GameType <> gmtDemo then // this is save and game demo is not recording, abort |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
285 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
286 |
CopyFile(recordFileName, UserPathPrefix + '/VideoTemp/' + RecPrefix + '.hwd'); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
287 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
288 |
|
7180 | 289 |
Mix_QuerySpec(@frequency, @format, @channels); |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
290 |
AddFileLog('sound: frequency = ' + IntToStr(frequency) + ', format = ' + IntToStr(format) + ', channels = ' + IntToStr(channels)); |
7180 | 291 |
if format <> $8010 then |
292 |
begin |
|
293 |
// TODO: support any audio format |
|
294 |
AddFileLog('Error: Unexpected audio format ' + IntToStr(format)); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
295 |
exit; |
7180 | 296 |
end; |
297 |
||
298 |
{$IOCHECKS OFF} |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
299 |
// create sound file |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
300 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw'; |
7180 | 301 |
Assign(audioFile, filename); |
302 |
Rewrite(audioFile, 1); |
|
303 |
if IOResult <> 0 then |
|
304 |
begin |
|
305 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
306 |
exit; |
7180 | 307 |
end; |
308 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
309 |
// create file with camera positions |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
310 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.txtout'; |
7180 | 311 |
Assign(cameraFile, filename); |
312 |
Rewrite(cameraFile); |
|
313 |
if IOResult <> 0 then |
|
314 |
begin |
|
315 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
316 |
exit; |
7180 | 317 |
end; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
318 |
|
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
319 |
// save audio parameters in sound file |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
320 |
BlockWrite(audioFile, frequency, 4); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
321 |
BlockWrite(audioFile, channels, 4); |
7180 | 322 |
{$IOCHECKS ON} |
323 |
||
324 |
// register callback for actual audio recording |
|
325 |
Mix_SetPostMix(@RecordPostMix, nil); |
|
326 |
||
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
327 |
startTime:= SDL_GetTicks(); |
7180 | 328 |
flagPrerecording:= true; |
329 |
end; |
|
330 |
||
331 |
procedure StopPreRecording; |
|
332 |
begin |
|
333 |
AddFileLog('StopPreRecording'); |
|
334 |
flagPrerecording:= false; |
|
335 |
||
336 |
// call SDL_LockAudio because RecordPostMix may be executing right now |
|
337 |
SDL_LockAudio(); |
|
338 |
Close(audioFile); |
|
339 |
Close(cameraFile); |
|
340 |
Mix_SetPostMix(nil, nil); |
|
341 |
SDL_UnlockAudio(); |
|
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
342 |
|
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
343 |
if not thumbnailSaved then |
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
344 |
SaveThumbnail(); |
7180 | 345 |
end; |
346 |
||
347 |
procedure SaveCameraPosition; |
|
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
348 |
var frame: TFrame; |
7180 | 349 |
begin |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
350 |
if (not thumbnailSaved) and (ScreenFade = sfNone) then |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
351 |
SaveThumbnail(); |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
352 |
|
7376
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
353 |
frame.realTicks:= SDL_GetTicks() - startTime; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
354 |
frame.gameTicks:= GameTicks; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
355 |
frame.CamX:= WorldDx; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
356 |
frame.CamY:= WorldDy - cScreenHeight div 2; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
357 |
frame.zoom:= zoom/cScreenWidth; |
48b79b3ca592
rework saving of camera positions so there is no need to know framerate during prerecording.
Stepan777 <stepik-777@mail.ru>
parents:
7369
diff
changeset
|
358 |
BlockWrite(cameraFile, frame, 1); |
7180 | 359 |
end; |
360 |
||
361 |
procedure freeModule; |
|
362 |
begin |
|
363 |
if flagPrerecording then |
|
364 |
StopPreRecording(); |
|
365 |
end; |
|
366 |
||
367 |
end. |
|
7286 | 368 |
|
369 |
{$ENDIF} // USE_VIDEO_RECORDING |