This repository was archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathAviRecorder.cpp
More file actions
108 lines (93 loc) · 2.14 KB
/
AviRecorder.cpp
File metadata and controls
108 lines (93 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include "stdafx.h"
#include "AviRecorder.h"
AviRecorder::AviRecorder(VideoCodec codec, uint32_t compressionLevel)
{
_recording = false;
_stopFlag = false;
_frameBuffer = nullptr;
_frameBufferLength = 0;
_sampleRate = 0;
_codec = codec;
_compressionLevel = compressionLevel;
}
AviRecorder::~AviRecorder()
{
if(_recording) {
StopRecording();
}
if(_frameBuffer) {
delete[] _frameBuffer;
_frameBuffer = nullptr;
}
}
bool AviRecorder::StartRecording(string filename, uint32_t width, uint32_t height, uint32_t bpp, uint32_t audioSampleRate, double fps)
{
if(!_recording) {
_outputFile = filename;
_sampleRate = audioSampleRate;
_width = width;
_height = height;
_fps = fps;
_frameBufferLength = height * width * bpp;
_frameBuffer = new uint8_t[_frameBufferLength];
_aviWriter.reset(new AviWriter());
if(!_aviWriter->StartWrite(filename, _codec, width, height, bpp, (uint32_t)(_fps * 1000000), audioSampleRate, _compressionLevel)) {
_aviWriter.reset();
return false;
}
_aviWriterThread = std::thread([=]() {
while(!_stopFlag) {
_waitFrame.Wait();
if(_stopFlag) {
break;
}
auto lock = _lock.AcquireSafe();
_aviWriter->AddFrame(_frameBuffer);
}
});
_recording = true;
}
return true;
}
void AviRecorder::StopRecording()
{
if(_recording) {
_recording = false;
_stopFlag = true;
_waitFrame.Signal();
_aviWriterThread.join();
_aviWriter->EndWrite();
_aviWriter.reset();
}
}
void AviRecorder::AddFrame(void* frameBuffer, uint32_t width, uint32_t height, double fps)
{
if(_recording) {
if(_width != width || _height != height || _fps != fps) {
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;
}
string AviRecorder::GetOutputFile()
{
return _outputFile;
}