优化hls播放速度

This commit is contained in:
xiongziliang 2020-09-12 18:53:16 +08:00
parent 28d73db9f7
commit dc192c95fc
1 changed files with 9 additions and 2 deletions

View File

@ -264,8 +264,8 @@ void HlsPlayerImp::onPlayResult(const SockException &ex) {
setPlayPosition(0); setPlayPosition(0);
weak_ptr<HlsPlayerImp> weakSelf = dynamic_pointer_cast<HlsPlayerImp>(shared_from_this()); weak_ptr<HlsPlayerImp> weakSelf = dynamic_pointer_cast<HlsPlayerImp>(shared_from_this());
//每20毫秒执行一次 //每50毫秒执行一次
_timer = std::make_shared<Timer>(0.02, [weakSelf]() { _timer = std::make_shared<Timer>(0.05, [weakSelf]() {
auto strongSelf = weakSelf.lock(); auto strongSelf = weakSelf.lock();
if (!strongSelf) { if (!strongSelf) {
return false; return false;
@ -326,6 +326,13 @@ void HlsPlayerImp::onTick() {
//这些帧还未到时间播放 //这些帧还未到时间播放
break; break;
} }
if (getBufferMS() < 3 * 1000) {
//缓存小于3秒,那么降低定时器消费速度(让剩余的数据在3秒后消费完毕)
//目的是为了防止定时器长时间干等后,数据瞬间消费完毕
setPlayPosition(_frame_cache.begin()->first);
}
//消费掉已经到期的帧 //消费掉已经到期的帧
MediaSink::inputFrame(it->second); MediaSink::inputFrame(it->second);
it = _frame_cache.erase(it); it = _frame_cache.erase(it);