Mesen-X/Core/AviRecorder.cpp

117 lines
2.6 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "AviRecorder.h"
#include "MessageManager.h"
2017-09-29 22:09:00 -04:00
#include "Console.h"
#include "EmulationSettings.h"
AviRecorder::AviRecorder(shared_ptr<Console> console)
{
_console = console;
_recording = false;
_stopFlag = false;
_frameBuffer = nullptr;
_frameBufferLength = 0;
_sampleRate = 0;
}
AviRecorder::~AviRecorder()
{
if(_recording) {
StopRecording();
}
if(_frameBuffer) {
delete[] _frameBuffer;
_frameBuffer = nullptr;
}
}
2017-09-29 22:09:00 -04:00
uint32_t AviRecorder::GetFps()
{
if(_console->GetModel() == NesModel::NTSC) {
return _console->GetSettings()->CheckFlag(EmulationFlags::IntegerFpsMode) ? 60000000 : 60098812;
2017-09-29 22:09:00 -04:00
} else {
return _console->GetSettings()->CheckFlag(EmulationFlags::IntegerFpsMode) ? 50000000 : 50006978;
2017-09-29 22:09:00 -04:00
}
}
bool AviRecorder::StartRecording(string filename, VideoCodec codec, uint32_t width, uint32_t height, uint32_t bpp, uint32_t audioSampleRate, uint32_t compressionLevel)
{
if(!_recording) {
_outputFile = filename;
_sampleRate = audioSampleRate;
2017-04-28 19:54:58 -04:00
_width = width;
_height = height;
2017-09-29 22:09:00 -04:00
_fps = GetFps();
_frameBufferLength = height * width * bpp;
_frameBuffer = new uint8_t[_frameBufferLength];
_aviWriter.reset(new AviWriter());
2017-09-29 22:09:00 -04:00
if(!_aviWriter->StartWrite(filename, codec, width, height, bpp, _fps, audioSampleRate, compressionLevel)) {
_aviWriter.reset();
return false;
}
_aviWriterThread = std::thread([=]() {
while(!_stopFlag) {
_waitFrame.Wait();
if(_stopFlag) {
break;
}
auto lock = _lock.AcquireSafe();
_aviWriter->AddFrame(_frameBuffer);
}
});
MessageManager::DisplayMessage("VideoRecorder", "VideoRecorderStarted", _outputFile);
_recording = true;
}
return true;
}
void AviRecorder::StopRecording()
{
if(_recording) {
_recording = false;
_stopFlag = true;
_waitFrame.Signal();
_aviWriterThread.join();
_aviWriter->EndWrite();
_aviWriter.reset();
MessageManager::DisplayMessage("VideoRecorder", "VideoRecorderStopped", _outputFile);
}
}
2017-04-28 19:54:58 -04:00
void AviRecorder::AddFrame(void* frameBuffer, uint32_t width, uint32_t height)
{
if(_recording) {
2017-09-29 22:09:00 -04:00
if(_width != width || _height != height || _fps != GetFps()) {
2017-04-28 19:54:58 -04:00
StopRecording();
} else {
auto lock = _lock.AcquireSafe();
memcpy(_frameBuffer, frameBuffer, _frameBufferLength);
_waitFrame.Signal();
}
}
}
void AviRecorder::AddSound(int16_t* soundBuffer, uint32_t sampleCount, uint32_t sampleRate)
{
if(_recording) {
if(_sampleRate != sampleRate) {
auto lock = _lock.AcquireSafe();
StopRecording();
} else {
_aviWriter->AddSound(soundBuffer, sampleCount);
}
}
}
bool AviRecorder::IsRecording()
{
return _recording;
}