teleport/client/tp-player/thr_play.cpp

197 lines
5.7 KiB
C++
Raw Normal View History

2019-09-09 18:39:08 +00:00
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
2019-09-02 21:40:52 +00:00
#include "thr_play.h"
2019-10-31 17:45:17 +00:00
#include "thr_data.h"
2019-11-03 19:34:11 +00:00
#include "mainwindow.h"
2019-09-02 21:40:52 +00:00
#include "record_format.h"
2019-10-31 17:45:17 +00:00
#include "util.h"
2019-09-02 21:40:52 +00:00
2019-10-31 17:45:17 +00:00
/*
*
* - 线线
* + 线500100020ms500
* - 线UI
* + if( * ( - ) >= ( - ))
* + 33
*/
2019-11-03 19:34:11 +00:00
ThrPlay::ThrPlay(MainWindow* mainwnd) {
m_mainwnd = mainwnd;
2019-09-02 21:40:52 +00:00
m_need_stop = false;
m_need_pause = false;
m_speed = 1;
m_skip = false;
m_start_ms = 0;
2019-09-16 05:53:27 +00:00
}
2019-10-31 17:45:17 +00:00
ThrPlay::~ThrPlay() {
2019-09-16 05:53:27 +00:00
stop();
2019-09-02 21:40:52 +00:00
}
2019-10-31 17:45:17 +00:00
void ThrPlay::stop() {
2019-09-16 05:53:27 +00:00
if(!isRunning())
return;
// warning: never call stop() inside thread::run() loop.
2019-09-02 21:40:52 +00:00
m_need_stop = true;
2019-09-16 05:53:27 +00:00
wait();
qDebug() << "play-thread end.";
2019-10-31 17:45:17 +00:00
// if(m_thr_data) {
// m_thr_data->stop();
// qDebug("delete thrData.");
// //m_thr_download->wait();
// delete m_thr_data;
// m_thr_data = nullptr;
// }
2019-09-02 21:40:52 +00:00
}
2019-10-31 17:45:17 +00:00
void ThrPlay::_notify_message(const QString& msg) {
UpdateData* _msg = new UpdateData(TYPE_MESSAGE);
2019-09-16 05:53:27 +00:00
_msg->message(msg);
emit signal_update_data(_msg);
}
2019-10-31 17:45:17 +00:00
void ThrPlay::_notify_error(const QString& msg) {
UpdateData* _msg = new UpdateData(TYPE_ERROR);
_msg->message(msg);
emit signal_update_data(_msg);
2019-09-16 05:53:27 +00:00
}
void ThrPlay::resume(bool relocate, uint32_t start_ms) {
if(relocate) {
m_start_ms = start_ms;
m_first_run = true;
}
m_need_pause = false;
}
2019-10-31 17:45:17 +00:00
void ThrPlay::run() {
2019-09-16 05:53:27 +00:00
2019-11-03 19:34:11 +00:00
ThrData* thr_data = m_mainwnd->get_thr_data();
m_first_run = true;
uint32_t last_time_ms = 0;
uint32_t last_pass_ms = 0;
UpdateData* dat = nullptr;
2019-11-03 19:34:11 +00:00
for(;;) {
if(m_need_stop)
break;
2019-09-16 05:53:27 +00:00
2019-11-03 19:34:11 +00:00
// 1. 从ThrData的待播放队列中取出一个数据
dat = thr_data->get_data();
2019-11-03 19:34:11 +00:00
if(dat == nullptr) {
msleep(20);
continue;
2019-09-16 05:53:27 +00:00
}
if(m_first_run) {
m_first_run = false;
2019-11-03 19:34:11 +00:00
_notify_message("");
2019-09-17 16:51:22 +00:00
}
if(m_start_ms > 0) {
if(dat->get_time() < m_start_ms) {
emit signal_update_data(dat);
continue;
}
last_time_ms = m_start_ms;
m_start_ms = 0;
}
2019-11-03 19:34:11 +00:00
// 2. 根据数据包的信息,等待到播放时间点
uint32_t need_wait_ms = 0;
uint32_t this_time_ms = dat->get_time();
uint32_t this_pass_ms = last_time_ms;
if(this_time_ms > 0) {
2019-11-05 19:15:23 +00:00
if(this_time_ms >= last_time_ms)
need_wait_ms = this_time_ms - last_time_ms;
else
need_wait_ms = 0;
if(need_wait_ms > 0) {
uint32_t time_wait = 0;
// 如果设置了跳过无操作区间将超过1秒的等待时间压缩至1秒。
if(m_skip) {
if(need_wait_ms > 1000)
need_wait_ms = 1000;
2019-09-17 16:51:22 +00:00
}
for(;;) {
time_wait = need_wait_ms > 10 ? 10 : need_wait_ms;
msleep(time_wait);
if(m_need_pause) {
while(m_need_pause) {
msleep(50);
if(m_need_stop)
break;
}
}
if(m_need_stop)
break;
if(m_start_ms > 0) {
// if(dat) {
// delete dat;
// dat = nullptr;
// }
break;
}
time_wait *= m_speed;
// 如果已经在等待长时间无操作区间内用户设置了跳过无操作区间则将超过0.5秒的等待时间压缩至0.5秒。
if(m_skip) {
if(need_wait_ms > 500)
need_wait_ms = 500;
}
this_pass_ms += time_wait;
if(this_pass_ms - last_pass_ms > 100) {
UpdateData* _passed_ms = new UpdateData(TYPE_PLAYED_MS);
_passed_ms->played_ms(this_pass_ms);
emit signal_update_data(_passed_ms);
last_pass_ms = this_pass_ms;
}
if(need_wait_ms <= time_wait)
break;
else
need_wait_ms -= time_wait;
2019-09-17 16:51:22 +00:00
}
if(m_need_stop)
2019-09-17 16:51:22 +00:00
break;
// if(m_start_ms > 0) {
// if(dat) {
// delete dat;
// dat = nullptr;
// }
// break;
// }
}
2019-09-17 16:51:22 +00:00
}
2019-11-05 19:15:23 +00:00
last_time_ms = this_time_ms;
// 3. 将数据包发送给主UI界面进行显示
if(dat->data_type() == TYPE_END) {
_notify_message(LOCAL8BIT("播放结束"));
}
emit signal_update_data(dat);
2019-09-17 16:51:22 +00:00
}
if(dat != nullptr)
delete dat;
2019-09-02 21:40:52 +00:00
}