Skip to main content

동작설명 및 구조 소개

강의 전 준비 사항

주요 소스 설명

메인 소스

#include <string>
#include <iostream>
#include "DesktopRecorder.hpp"

using namespace std;

int main()
{
DesktopRecorder recorder;

// 무한반복 하면서 입력받은 문자에 따라 녹화, 중지, 프로그램 종료를 처리한다.
while (true) {
string line;
printf("(s)tart, s(t)op, (q)uit: ");
getline(cin, line);

if (line == "s") recorder.start("test.mp4", 0, 0, 1024, 768);
if (line == "t") recorder.stop();
if (line == "q") {
recorder.terminate();
break;
}
}
}

DesktopRecorder.hpp

...
class DesktopRecorder {
public:
DesktopRecorder()
{
// Scheduler의 OnRepeat 이벤트를 처리할 핸들러를 작성한다.
// scheduler_.start()가 실행되면 이벤트 핸들러가 반복되어 실행된다.
scheduler_.setOnRepeat([&]() {
...
});

// scheduler_.add(...)를 통해서 입력된 테스크가 있을 때마다 실행된다.
scheduler_.setOnTask([&](int task, const string text, const void* data, int, int size) {
switch (task) {
case TASK_START: {
...
break;
}

case TASK_STOP: {
...
break;
}
}
});
}

// 프로그램이 종료될 때 뒷처리를 담당한다.
void terminate()
{
// 스레드를 종료하고 종료될 때까지 기다린다.
scheduler_.terminateAndWait();

// 아직 처리되지 않은 작업을 정리하고 종료처리를 한다.
do_stop();
}

void start(const string filename, int left, int top, int width, int height)
{
// 녹화시작(TASK_START) 테스크를 추가한다.
scheduler_.add(TASK_START, new Task(filename, left, top, width, height));
}

void stop()
{
// 녹화종료(TASK_STOP) 테스크를 추가한다.
scheduler_.add(TASK_STOP);
}

private:
VideoCreater* videoCreater_ = nullptr;
Scheduler scheduler_;
DesktopCapture desktopCapture_;
AudioCapture audioCapture_;
};

Scheduler를 이용해서 setOnRepeat, setOnTask 이벤트 핸들러가 같은 스레드에서 동기적으로 실행되도록 합니다. 이를 통해서 임계영역을 사용하지 않고 동기화할 수 있게됩니다.