hedgewars/uVideoRec.pas
changeset 7306 3cff5c769509
parent 7286 068adc6948e3
child 7363 09af8bc9181f
equal deleted inserted replaced
7303:998128081b86 7306:3cff5c769509
    49 
    49 
    50 procedure freeModule;
    50 procedure freeModule;
    51 
    51 
    52 implementation
    52 implementation
    53 
    53 
    54 uses uVariables, uUtils, GLunit, SDLh, SysUtils, uIO;
    54 uses uVariables, uUtils, GLunit, SDLh, SysUtils, uIO, uMisc, uTypes;
    55 
    55 
    56 {$IFDEF WIN32}
    56 {$IFDEF WIN32}
    57 const AVWrapperLibName = 'libavwrapper.dll';
    57 const AVWrapperLibName = 'libavwrapper.dll';
    58 {$ENDIF}
    58 {$ENDIF}
    59 
    59 
    86     cameraFile: File of TFrame;
    86     cameraFile: File of TFrame;
    87     audioFile: File;
    87     audioFile: File;
    88     numPixels: LongWord;
    88     numPixels: LongWord;
    89     startTime, numFrames: LongWord;
    89     startTime, numFrames: LongWord;
    90     cameraFilePath, soundFilePath: shortstring;
    90     cameraFilePath, soundFilePath: shortstring;
       
    91     thumbnailSaved : Boolean;
    91 
    92 
    92 function BeginVideoRecording: Boolean;
    93 function BeginVideoRecording: Boolean;
    93 var filename, desc: shortstring;
    94 var filename, desc: shortstring;
    94 begin
    95 begin
    95     AddFileLog('BeginVideoRecording');
    96     AddFileLog('BeginVideoRecording');
    96 
    97 
    97     numPixels:= cScreenWidth*cScreenHeight;
    98     numPixels:= cScreenWidth*cScreenHeight;
    98 
    99 
    99 {$IOCHECKS OFF}
   100 {$IOCHECKS OFF}
   100     // open file with prerecorded camera positions
   101     // open file with prerecorded camera positions
   101     cameraFilePath:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + '.txtin';
   102     cameraFilePath:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.txtin';
   102     Assign(cameraFile, cameraFilePath);
   103     Assign(cameraFile, cameraFilePath);
   103     Reset(cameraFile);
   104     Reset(cameraFile);
   104     if IOResult <> 0 then
   105     if IOResult <> 0 then
   105     begin
   106     begin
   106         AddFileLog('Error: Could not read from ' + cameraFilePath);
   107         AddFileLog('Error: Could not read from ' + cameraFilePath);
   115         desc+= 'Record: ' + recordFileName + #10;
   116         desc+= 'Record: ' + recordFileName + #10;
   116     if cMapName <> '' then
   117     if cMapName <> '' then
   117         desc+= 'Map: ' + cMapName + #10;
   118         desc+= 'Map: ' + cMapName + #10;
   118     if Theme <> '' then
   119     if Theme <> '' then
   119         desc+= 'Theme: ' + Theme + #10;
   120         desc+= 'Theme: ' + Theme + #10;
       
   121     desc+= 'prefix[' + RecPrefix + ']prefix';
   120     desc+= #0;
   122     desc+= #0;
   121 
   123 
   122     filename:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + #0;
   124     filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + #0;
   123     soundFilePath:= UserPathPrefix + '/VideoTemp/' + cRecPrefix + '.sw' + #0;
   125     soundFilePath:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw' + #0;
   124     cAVFormat+= #0;
   126     cAVFormat+= #0;
   125     cAudioCodec+= #0;
   127     cAudioCodec+= #0;
   126     cVideoCodec+= #0;
   128     cVideoCodec+= #0;
   127     cVideoPreset+= #0;
   129     cVideoPreset+= #0;
   128     AVWrapper_Init(@AddFileLogRaw, @filename[1], @desc[1], @soundFilePath[1], @cAVFormat[1], @cVideoCodec[1], @cAudioCodec[1], @cVideoPreset[1],
   130     AVWrapper_Init(@AddFileLogRaw, @filename[1], @desc[1], @soundFilePath[1], @cAVFormat[1], @cVideoCodec[1], @cAudioCodec[1], @cVideoPreset[1],
   220 {$IOCHECKS OFF}
   222 {$IOCHECKS OFF}
   221     BlockWrite(audioFile, stream^, len);
   223     BlockWrite(audioFile, stream^, len);
   222 {$IOCHECKS ON}
   224 {$IOCHECKS ON}
   223 end;
   225 end;
   224 
   226 
       
   227 procedure SaveThumbnail;
       
   228 var thumbpath: shortstring;
       
   229     k: LongInt;
       
   230 begin
       
   231     thumbpath:= '/VideoTemp/' + RecPrefix;
       
   232     AddFileLog('Saving thumbnail ' + thumbpath);
       
   233     if cScreenWidth > cScreenHeight then
       
   234         k:= cScreenWidth div 400  // here 400 is minimum size of thumbnail
       
   235     else
       
   236         k:= cScreenHeight div 400;
       
   237     if k = 0 then
       
   238         k:= 1;
       
   239     MakeScreenshot(thumbpath, k);
       
   240     thumbnailSaved:= true;
       
   241 end;
       
   242 
   225 procedure BeginPreRecording;
   243 procedure BeginPreRecording;
   226 var format: word;
   244 var format: word;
   227     filePrefix, filename: shortstring;
   245     filename: shortstring;
   228     frequency, channels: LongInt;
   246     frequency, channels: LongInt;
   229 begin
   247 begin
   230     AddFileLog('BeginPreRecording');
   248     AddFileLog('BeginPreRecording');
   231 
   249 
   232     numFrames:= 0;
   250     numFrames:= 0;
   233     startTime:= SDL_GetTicks();
   251     startTime:= SDL_GetTicks();
   234 
   252 
   235     filePrefix:= FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now());
   253     RecPrefix:= FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now());
       
   254 
       
   255     thumbnailSaved:= false;
       
   256     if (not (gameState in [gsLandGen, gsStart])) and (ScreenFade = sfNone) then
       
   257         SaveThumbnail();
   236 
   258 
   237     Mix_QuerySpec(@frequency, @format, @channels);
   259     Mix_QuerySpec(@frequency, @format, @channels);
   238     AddFileLog('sound: frequency = ' + IntToStr(frequency) + ', format = ' + IntToStr(format) + ', channels = ' + IntToStr(channels));
   260     AddFileLog('sound: frequency = ' + IntToStr(frequency) + ', format = ' + IntToStr(format) + ', channels = ' + IntToStr(channels));
   239     if format <> $8010 then
   261     if format <> $8010 then
   240     begin
   262     begin
   243         exit;
   265         exit;
   244     end;
   266     end;
   245 
   267 
   246 {$IOCHECKS OFF}
   268 {$IOCHECKS OFF}
   247     // create sound file
   269     // create sound file
   248     filename:= UserPathPrefix + '/VideoTemp/' + filePrefix + '.sw';
   270     filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.sw';
   249     Assign(audioFile, filename);
   271     Assign(audioFile, filename);
   250     Rewrite(audioFile, 1);
   272     Rewrite(audioFile, 1);
   251     if IOResult <> 0 then
   273     if IOResult <> 0 then
   252     begin
   274     begin
   253         AddFileLog('Error: Could not write to ' + filename);
   275         AddFileLog('Error: Could not write to ' + filename);
   254         exit;
   276         exit;
   255     end;
   277     end;
   256 
   278 
   257     // create file with camera positions
   279     // create file with camera positions
   258     filename:= UserPathPrefix + '/VideoTemp/' + filePrefix + '.txtout';
   280     filename:= UserPathPrefix + '/VideoTemp/' + RecPrefix + '.txtout';
   259     Assign(cameraFile, filename);
   281     Assign(cameraFile, filename);
   260     Rewrite(cameraFile);
   282     Rewrite(cameraFile);
   261     if IOResult <> 0 then
   283     if IOResult <> 0 then
   262     begin
   284     begin
   263         AddFileLog('Error: Could not write to ' + filename);
   285         AddFileLog('Error: Could not write to ' + filename);
   283     SDL_LockAudio();
   305     SDL_LockAudio();
   284     Close(audioFile);
   306     Close(audioFile);
   285     Close(cameraFile);
   307     Close(cameraFile);
   286     Mix_SetPostMix(nil, nil);
   308     Mix_SetPostMix(nil, nil);
   287     SDL_UnlockAudio();
   309     SDL_UnlockAudio();
       
   310 
       
   311     if (not thumbnailSaved) then
       
   312         SaveThumbnail();
   288 end;
   313 end;
   289 
   314 
   290 procedure SaveCameraPosition;
   315 procedure SaveCameraPosition;
   291 var curTime: LongInt;
   316 var curTime: LongInt;
   292     frame: TFrame;
   317     frame: TFrame;
   293 begin
   318 begin
       
   319     if (not thumbnailSaved) and (ScreenFade = sfNone) then
       
   320         SaveThumbnail();
       
   321 
   294     curTime:= SDL_GetTicks();
   322     curTime:= SDL_GetTicks();
   295     while Int64(curTime - startTime)*cVideoFramerateNum > Int64(numFrames)*cVideoFramerateDen*1000 do
   323     while Int64(curTime - startTime)*cVideoFramerateNum > Int64(numFrames)*cVideoFramerateDen*1000 do
   296     begin
   324     begin
   297         frame.ticks:= GameTicks;
   325         frame.ticks:= GameTicks;
   298         frame.CamX:= WorldDx;
   326         frame.CamX:= WorldDx;