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