author | koda |
Wed, 06 Mar 2013 12:12:02 +0100 | |
changeset 8666 | 1652c1d9adc8 |
parent 8370 | 0c79946e96f8 |
child 8697 | 9624836094de |
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} |
8666
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
31 |
{$linklib avcodec} |
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
32 |
{$linklib avformat} |
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
33 |
{$linklib avutil} |
7180 | 34 |
{$ENDIF} |
35 |
||
36 |
interface |
|
37 |
||
38 |
var flagPrerecording: boolean = false; |
|
39 |
||
40 |
function BeginVideoRecording: Boolean; |
|
7671 | 41 |
function LoadNextCameraPosition(out newRealTicks, newGameTicks: LongInt): Boolean; |
7180 | 42 |
procedure EncodeFrame; |
43 |
procedure StopVideoRecording; |
|
44 |
||
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
45 |
procedure BeginPreRecording; |
7180 | 46 |
procedure StopPreRecording; |
47 |
procedure SaveCameraPosition; |
|
48 |
||
7850 | 49 |
procedure initModule; |
7180 | 50 |
procedure freeModule; |
51 |
||
52 |
implementation |
|
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
53 |
uses uVariables, uUtils, GLunit, SDLh, SysUtils, uIO, uMisc, uTypes; |
7180 | 54 |
|
55 |
type TAddFileLogRaw = procedure (s: pchar); cdecl; |
|
8666
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
56 |
const AvwrapperLibName = 'libavwrapper'; |
7180 | 57 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
58 |
procedure AVWrapper_Init( |
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
59 |
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
|
60 |
filename, desc, soundFile, format, vcodec, acodec: PChar; |
8666
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
61 |
width, height, framerateNum, framerateDen, vquality: LongInt); cdecl; external AvwrapperLibName; |
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
62 |
procedure AVWrapper_Close; cdecl; external AvwrapperLibName; |
1652c1d9adc8
rework ffmpeg/libav/videorec linking and their cmake discovery
koda
parents:
8370
diff
changeset
|
63 |
procedure AVWrapper_WriteFrame( pY, pCb, pCr: PByte ); cdecl; external AvwrapperLibName; |
7180 | 64 |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
gameTicks: LongWord; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
68 |
CamX, CamY: LongInt; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
69 |
zoom: single; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
70 |
end; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
71 |
|
7180 | 72 |
var YCbCr_Planes: array[0..2] of PByte; |
73 |
RGB_Buffer: PByte; |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
74 |
cameraFile: File of TFrame; |
7180 | 75 |
audioFile: File; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
76 |
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
|
77 |
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
|
78 |
soundFilePath: shortstring; |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
79 |
thumbnailSaved : Boolean; |
7180 | 80 |
|
81 |
function BeginVideoRecording: Boolean; |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
82 |
var filename, desc: shortstring; |
7180 | 83 |
begin |
84 |
AddFileLog('BeginVideoRecording'); |
|
85 |
||
86 |
{$IOCHECKS OFF} |
|
87 |
// 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
|
88 |
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
|
89 |
Assign(cameraFile, filename); |
7180 | 90 |
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
|
91 |
maxProgress:= FileSize(cameraFile); |
7180 | 92 |
if IOResult <> 0 then |
93 |
begin |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
94 |
AddFileLog('Error: Could not read from ' + filename); |
7180 | 95 |
exit(false); |
96 |
end; |
|
97 |
{$IOCHECKS ON} |
|
98 |
||
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
|
99 |
// store some description in output file |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
100 |
desc:= ''; |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
101 |
if UserNick <> '' then |
7804 | 102 |
desc:= desc + 'Player: ' + UserNick + #10; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
103 |
if recordFileName <> '' then |
7804 | 104 |
desc:= desc + 'Record: ' + recordFileName + #10; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
105 |
if cMapName <> '' then |
7804 | 106 |
desc:= desc + 'Map: ' + cMapName + #10; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
107 |
if Theme <> '' then |
7804 | 108 |
desc:= desc + 'Theme: ' + Theme + #10; |
109 |
desc:= desc + 'prefix[' + RecPrefix + ']prefix'; |
|
110 |
||
111 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix; |
|
112 |
soundFilePath:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw'; |
|
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
113 |
|
7804 | 114 |
AVWrapper_Init(@AddFileLogRaw |
115 |
, PChar(ansistring(filename)) |
|
116 |
, PChar(ansistring(desc)) |
|
117 |
, PChar(ansistring(soundFilePath)) |
|
118 |
, PChar(ansistring(cAVFormat)) |
|
119 |
, PChar(ansistring(cVideoCodec)) |
|
120 |
, PChar(ansistring(cAudioCodec)) |
|
121 |
, cScreenWidth, cScreenHeight, cVideoFramerateNum, cVideoFramerateDen, cVideoQuality); |
|
7180 | 122 |
|
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
|
123 |
numPixels:= cScreenWidth*cScreenHeight; |
7180 | 124 |
YCbCr_Planes[0]:= GetMem(numPixels); |
125 |
YCbCr_Planes[1]:= GetMem(numPixels div 4); |
|
126 |
YCbCr_Planes[2]:= GetMem(numPixels div 4); |
|
127 |
||
128 |
if (YCbCr_Planes[0] = nil) or (YCbCr_Planes[1] = nil) or (YCbCr_Planes[2] = nil) then |
|
129 |
begin |
|
130 |
AddFileLog('Error: Could not allocate memory for video recording (YCbCr buffer).'); |
|
131 |
exit(false); |
|
132 |
end; |
|
133 |
||
134 |
RGB_Buffer:= GetMem(4*numPixels); |
|
135 |
if RGB_Buffer = nil then |
|
136 |
begin |
|
137 |
AddFileLog('Error: Could not allocate memory for video recording (RGB buffer).'); |
|
138 |
exit(false); |
|
139 |
end; |
|
140 |
||
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
|
141 |
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
|
142 |
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
|
143 |
progress:= 0; |
7180 | 144 |
BeginVideoRecording:= true; |
145 |
end; |
|
146 |
||
147 |
procedure StopVideoRecording; |
|
148 |
begin |
|
149 |
AddFileLog('StopVideoRecording'); |
|
150 |
FreeMem(YCbCr_Planes[0], numPixels); |
|
151 |
FreeMem(YCbCr_Planes[1], numPixels div 4); |
|
152 |
FreeMem(YCbCr_Planes[2], numPixels div 4); |
|
153 |
FreeMem(RGB_Buffer, 4*numPixels); |
|
154 |
Close(cameraFile); |
|
155 |
AVWrapper_Close(); |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
156 |
Erase(cameraFile); |
7180 | 157 |
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
|
158 |
SendIPC(_S'v'); // inform frontend that we finished |
7180 | 159 |
end; |
160 |
||
161 |
function pixel(x, y, color: LongInt): LongInt; |
|
162 |
begin |
|
163 |
pixel:= RGB_Buffer[(cScreenHeight-y-1)*cScreenWidth*4 + x*4 + color]; |
|
164 |
end; |
|
165 |
||
166 |
procedure EncodeFrame; |
|
167 |
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
|
168 |
s: shortstring; |
7180 | 169 |
begin |
170 |
// read pixels from OpenGL |
|
171 |
glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_RGBA, GL_UNSIGNED_BYTE, RGB_Buffer); |
|
172 |
||
173 |
// convert to YCbCr 4:2:0 format |
|
174 |
// Y |
|
175 |
for y := 0 to cScreenHeight-1 do |
|
176 |
for x := 0 to cScreenWidth-1 do |
|
177 |
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)); |
|
178 |
||
179 |
// Cb and Cr |
|
180 |
for y := 0 to cScreenHeight div 2 - 1 do |
|
181 |
for x := 0 to cScreenWidth div 2 - 1 do |
|
182 |
begin |
|
183 |
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); |
|
184 |
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); |
|
185 |
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); |
|
186 |
YCbCr_Planes[1][y*(cScreenWidth div 2) + x]:= Byte(128 + ((-2428*r - 4768*g + 7196*b) shr 16)); |
|
187 |
YCbCr_Planes[2][y*(cScreenWidth div 2) + x]:= Byte(128 + (( 7196*r - 6026*g - 1170*b) shr 16)); |
|
188 |
end; |
|
189 |
||
190 |
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
|
191 |
|
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
|
192 |
// 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
|
193 |
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
|
194 |
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
|
195 |
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
|
196 |
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
|
197 |
inc(numFrames); |
7180 | 198 |
end; |
199 |
||
7671 | 200 |
function LoadNextCameraPosition(out newRealTicks, newGameTicks: LongInt): Boolean; |
8370 | 201 |
var frame: TFrame = (realTicks: 0; gameTicks: 0; CamX: 0; CamY: 0; zoom: 0); |
7180 | 202 |
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
|
203 |
// 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
|
204 |
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
|
205 |
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
|
206 |
{$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
|
207 |
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
|
208 |
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
|
209 |
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
|
210 |
{$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
|
211 |
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
|
212 |
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
|
213 |
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
|
214 |
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
|
215 |
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
|
216 |
inc(progress); |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
217 |
newRealTicks:= frame.realTicks; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
218 |
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
|
219 |
end; |
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
220 |
LoadNextCameraPosition:= true; |
7180 | 221 |
end; |
222 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
223 |
// 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
|
224 |
// This procedure may be called from different thread. |
7180 | 225 |
procedure RecordPostMix(udata: pointer; stream: PByte; len: LongInt); cdecl; |
226 |
begin |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
227 |
udata:= udata; // avoid warning |
7180 | 228 |
{$IOCHECKS OFF} |
229 |
BlockWrite(audioFile, stream^, len); |
|
230 |
{$IOCHECKS ON} |
|
231 |
end; |
|
232 |
||
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
233 |
procedure SaveThumbnail; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
234 |
var thumbpath: shortstring; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
235 |
k: LongInt; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
236 |
begin |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
237 |
thumbpath:= '/VideoTemp/' + RecPrefix; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
MakeScreenshot(thumbpath, k); |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
241 |
thumbnailSaved:= true; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
242 |
end; |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
243 |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
244 |
// 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
|
245 |
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
|
246 |
var inF, outF: file; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
247 |
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
|
248 |
result: LongInt; |
8370 | 249 |
i: integer; |
7392
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 |
{$IOCHECKS OFF} |
8370 | 252 |
result:= 0; // avoid compiler hint and warning |
253 |
for i:= 0 to 1023 do |
|
254 |
buffer[i]:= 0; |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
255 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
256 |
Assign(inF, src); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
257 |
Reset(inF, 1); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
258 |
if IOResult <> 0 then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
259 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
260 |
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
|
261 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
262 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
263 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
264 |
Assign(outF, dest); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
265 |
Rewrite(outF, 1); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
266 |
if IOResult <> 0 then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
267 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
268 |
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
|
269 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
270 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
271 |
|
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
272 |
repeat |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
273 |
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
|
274 |
BlockWrite(outF, buffer, result); |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
275 |
until result < 1024; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
276 |
{$IOCHECKS ON} |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
277 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
278 |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
279 |
procedure BeginPreRecording; |
7180 | 280 |
var format: word; |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
281 |
filename: shortstring; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
282 |
frequency, channels: LongInt; |
7180 | 283 |
begin |
284 |
AddFileLog('BeginPreRecording'); |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
285 |
|
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
286 |
thumbnailSaved:= false; |
7386
e82a076df09b
Fix bug with isInLag picture displayed at end of some videos.
Stepan777 <stepik-777@mail.ru>
parents:
7379
diff
changeset
|
287 |
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
|
288 |
|
7392
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
289 |
// 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
|
290 |
// 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
|
291 |
if recordFileName <> '' then |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
292 |
begin |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
293 |
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
|
294 |
exit; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
295 |
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
|
296 |
end; |
bc3306c59a08
Correctly distinguish between game and real ticks while recording video,
Stepan777 <stepik-777@mail.ru>
parents:
7386
diff
changeset
|
297 |
|
7180 | 298 |
Mix_QuerySpec(@frequency, @format, @channels); |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
299 |
AddFileLog('sound: frequency = ' + IntToStr(frequency) + ', format = ' + IntToStr(format) + ', channels = ' + IntToStr(channels)); |
7180 | 300 |
if format <> $8010 then |
301 |
begin |
|
302 |
// TODO: support any audio format |
|
303 |
AddFileLog('Error: Unexpected audio format ' + IntToStr(format)); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
304 |
exit; |
7180 | 305 |
end; |
306 |
||
307 |
{$IOCHECKS OFF} |
|
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
308 |
// create sound file |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
309 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw'; |
7180 | 310 |
Assign(audioFile, filename); |
311 |
Rewrite(audioFile, 1); |
|
312 |
if IOResult <> 0 then |
|
313 |
begin |
|
314 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
315 |
exit; |
7180 | 316 |
end; |
317 |
||
7235
baa69bd025d9
1. Implement new page in frontend with options for video recording.
Stepan777 <stepik-777@mail.ru>
parents:
7198
diff
changeset
|
318 |
// create file with camera positions |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
319 |
filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.txtout'; |
7180 | 320 |
Assign(cameraFile, filename); |
321 |
Rewrite(cameraFile); |
|
322 |
if IOResult <> 0 then |
|
323 |
begin |
|
324 |
AddFileLog('Error: Could not write to ' + filename); |
|
7198
5debd5fe526e
1. Add IFDEFs for video recording
Stepan777 <stepik-777@mail.ru>
parents:
7180
diff
changeset
|
325 |
exit; |
7180 | 326 |
end; |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
327 |
|
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
328 |
// save audio parameters in sound file |
7280
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
329 |
BlockWrite(audioFile, frequency, 4); |
fd707afbc3a2
pagevideos is now much better that before:
Stepan777 <stepik-777@mail.ru>
parents:
7235
diff
changeset
|
330 |
BlockWrite(audioFile, channels, 4); |
7180 | 331 |
{$IOCHECKS ON} |
332 |
||
333 |
// register callback for actual audio recording |
|
334 |
Mix_SetPostMix(@RecordPostMix, nil); |
|
335 |
||
7369
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
336 |
startTime:= SDL_GetTicks(); |
7180 | 337 |
flagPrerecording:= true; |
338 |
end; |
|
339 |
||
340 |
procedure StopPreRecording; |
|
341 |
begin |
|
342 |
AddFileLog('StopPreRecording'); |
|
343 |
flagPrerecording:= false; |
|
344 |
||
345 |
// call SDL_LockAudio because RecordPostMix may be executing right now |
|
346 |
SDL_LockAudio(); |
|
347 |
Close(audioFile); |
|
348 |
Close(cameraFile); |
|
349 |
Mix_SetPostMix(nil, nil); |
|
350 |
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
|
351 |
|
46921fbe76d3
Move SDL_GetTicks() to another place, maybe this will fix sound desync.
Stepan777 <stepik-777@mail.ru>
parents:
7363
diff
changeset
|
352 |
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
|
353 |
SaveThumbnail(); |
7180 | 354 |
end; |
355 |
||
356 |
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
|
357 |
var frame: TFrame; |
7180 | 358 |
begin |
7306
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
359 |
if (not thumbnailSaved) and (ScreenFade = sfNone) then |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
360 |
SaveThumbnail(); |
3cff5c769509
Here they come - thumbnails.
Stepan777 <stepik-777@mail.ru>
parents:
7286
diff
changeset
|
361 |
|
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
|
362 |
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
|
363 |
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
|
364 |
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
|
365 |
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
|
366 |
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
|
367 |
BlockWrite(cameraFile, frame, 1); |
7180 | 368 |
end; |
369 |
||
7850 | 370 |
procedure initModule; |
371 |
begin |
|
372 |
end; |
|
373 |
||
7180 | 374 |
procedure freeModule; |
375 |
begin |
|
376 |
if flagPrerecording then |
|
377 |
StopPreRecording(); |
|
378 |
end; |
|
379 |
||
380 |
end. |
|
7286 | 381 |
|
382 |
{$ENDIF} // USE_VIDEO_RECORDING |