2019-03-15 12:48:34 -04:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "AviRecorder.h"
|
|
|
|
|
2020-02-05 18:57:20 -05:00
|
|
|
AviRecorder::AviRecorder(VideoCodec codec, uint32_t compressionLevel)
|
2019-03-15 12:48:34 -04:00
|
|
|
{
|
|
|
|
_recording = false;
|
|
|
|
_stopFlag = false;
|
|
|
|
_frameBuffer = nullptr;
|
|
|
|
_frameBufferLength = 0;
|
|
|
|
_sampleRate = 0;
|
2020-02-05 18:57:20 -05:00
|
|
|
_codec = codec;
|
|
|
|
_compressionLevel = compressionLevel;
|
2019-03-15 12:48:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
AviRecorder::~AviRecorder()
|
|
|
|
{
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_recording)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
StopRecording();
|
|
|
|
}
|
|
|
|
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_frameBuffer)
|
|
|
|
{
|
2020-02-05 18:57:20 -05:00
|
|
|
delete[] _frameBuffer;
|
|
|
|
_frameBuffer = nullptr;
|
2019-03-15 12:48:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-19 23:32:47 +03:00
|
|
|
bool AviRecorder::StartRecording(string filename, uint32_t width, uint32_t height, uint32_t bpp,
|
|
|
|
uint32_t audioSampleRate, double fps)
|
2019-03-15 12:48:34 -04:00
|
|
|
{
|
2020-12-19 23:32:47 +03:00
|
|
|
if (!_recording)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
_outputFile = filename;
|
|
|
|
_sampleRate = audioSampleRate;
|
|
|
|
_width = width;
|
|
|
|
_height = height;
|
2020-02-05 18:57:20 -05:00
|
|
|
_fps = fps;
|
2019-03-15 12:48:34 -04:00
|
|
|
_frameBufferLength = height * width * bpp;
|
|
|
|
_frameBuffer = new uint8_t[_frameBufferLength];
|
|
|
|
|
|
|
|
_aviWriter.reset(new AviWriter());
|
2020-12-19 23:32:47 +03:00
|
|
|
if (!_aviWriter->StartWrite(filename, _codec, width, height, bpp, (uint32_t)(_fps * 1000000), audioSampleRate,
|
|
|
|
_compressionLevel))
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
_aviWriter.reset();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-19 23:32:47 +03:00
|
|
|
_aviWriterThread = std::thread([=]()
|
|
|
|
{
|
|
|
|
while (!_stopFlag)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
_waitFrame.Wait();
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_stopFlag)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto lock = _lock.AcquireSafe();
|
|
|
|
_aviWriter->AddFrame(_frameBuffer);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
_recording = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AviRecorder::StopRecording()
|
|
|
|
{
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_recording)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
_recording = false;
|
|
|
|
|
|
|
|
_stopFlag = true;
|
|
|
|
_waitFrame.Signal();
|
|
|
|
_aviWriterThread.join();
|
|
|
|
|
|
|
|
_aviWriter->EndWrite();
|
|
|
|
_aviWriter.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 18:57:20 -05:00
|
|
|
void AviRecorder::AddFrame(void* frameBuffer, uint32_t width, uint32_t height, double fps)
|
2019-03-15 12:48:34 -04:00
|
|
|
{
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_recording)
|
|
|
|
{
|
|
|
|
if (_width != width || _height != height || _fps != fps)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
StopRecording();
|
2020-12-19 23:32:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
auto lock = _lock.AcquireSafe();
|
|
|
|
memcpy(_frameBuffer, frameBuffer, _frameBufferLength);
|
|
|
|
_waitFrame.Signal();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AviRecorder::AddSound(int16_t* soundBuffer, uint32_t sampleCount, uint32_t sampleRate)
|
|
|
|
{
|
2020-12-19 23:32:47 +03:00
|
|
|
if (_recording)
|
|
|
|
{
|
|
|
|
if (_sampleRate != sampleRate)
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
auto lock = _lock.AcquireSafe();
|
|
|
|
StopRecording();
|
2020-12-19 23:32:47 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-15 12:48:34 -04:00
|
|
|
_aviWriter->AddSound(soundBuffer, sampleCount);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AviRecorder::IsRecording()
|
|
|
|
{
|
|
|
|
return _recording;
|
2020-02-05 18:57:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
string AviRecorder::GetOutputFile()
|
|
|
|
{
|
|
|
|
return _outputFile;
|
2020-12-19 23:32:47 +03:00
|
|
|
}
|