完成XThread类
该播放器会启用多个线程包括解封装线程、音视频解码线程、播放线程等等,所以必须有一个统一的线程类来进行统一的管理
代码如下,代码解析请看注释
#ifndef BOPLAY_XTHREAD_H
#define BOPLAY_XTHREAD_H
void XSleep(int ms);
class XThread {
public:
virtual void start();
virtual void stop();
virtual void main(){}
protected:
bool isExit = false;
bool isRunning = false;
private:
void threadMain();
};
#endif
#include "XThread.h"
#include <thread>
#include "XLog.h"
using namespace std;
void XSleep(int ms){
chrono::milliseconds sTime(ms);
this_thread::sleep_for(sTime);
}
void XThread::start() {
isExit = false;
thread th(&XThread::threadMain, this);
th.detach();
}
void XThread::stop() {
isExit = true;
for (int i = 0; i < 200; ++i) {
XSleep(1);
if (!isRunning){
XLOGI("停止线程成功");
return;
}
XSleep(1);
}
XLOGI("停止线程超时");
}
void XThread::threadMain() {
XLOGI("线程函数进入");
isRunning = true;
main();
isRunning = false;
XLOGI("线程函数退出");
}
解封装接口类IDemux继承XThread并实现线程主函数
#ifndef BOPLAY_IDEMUX_H
#define BOPLAY_IDEMUX_H
#include "XData.h"
#include "XThread.h"
class IDemux: public XThread {
public:
virtual bool Open(const char *url) = 0;
virtual XData Read() = 0;
int totalMs = 0;
protected:
virtual void main();
};
#endif
#include "IDemux.h"
#include "XLog.h"
void IDemux::main() {
while(!isExit){
XData xData = Read();
}
}