forked from AgoraIO/API-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYUVReader.cpp
More file actions
executable file
·59 lines (47 loc) · 1.01 KB
/
YUVReader.cpp
File metadata and controls
executable file
·59 lines (47 loc) · 1.01 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
#include "YUVReader.h"
YUVReader::YUVReader()
{
}
void YUVReader::start(IYUVCallback callback)
{
if (thread == nullptr) {
isReading = true;
this->callback = callback;
thread = new std::thread(std::bind(&YUVReader::run, this));
}
}
void YUVReader::stop()
{
isReading = false;
if (thread != nullptr) {
thread->join();
thread = nullptr;
this->callback = nullptr;
}
}
void YUVReader::run()
{
std::ifstream fileStream(FILE_PATH);
if (!fileStream) {
// failure
std::cout << "fail to open the file" << std::endl;
}
else {
// success
fileStream.seekg(0, std::ios::beg);
while (isReading) {
if (fileStream.good()) {
fileStream.read(reinterpret_cast<char*>(buffer), VIDEO_FRAME_SIZE);
if (callback != nullptr) {
callback(VIDEO_WIDTH, VIDEO_HEIGHT, buffer, VIDEO_FRAME_SIZE);
}
std::this_thread::sleep_for(std::chrono::milliseconds(VIDEO_FRAME_INTERVAL_MS));
}
else {
fileStream.clear();
fileStream.seekg(0, std::ios::beg);
}
}
}
fileStream.close();
}