平滑动播放hls

This commit is contained in:
xiongziliang 2020-05-26 23:19:13 +08:00
parent 53ca43fb7d
commit a7bcfd566b
2 changed files with 57 additions and 0 deletions

View File

@ -256,12 +256,62 @@ void HlsPlayerImp::onAllTrackReady() {
void HlsPlayerImp::onPlayResult(const SockException &ex) { void HlsPlayerImp::onPlayResult(const SockException &ex) {
if(ex){ if(ex){
PlayerImp<HlsPlayer, PlayerBase>::onPlayResult(ex); PlayerImp<HlsPlayer, PlayerBase>::onPlayResult(ex);
}else{
_stamp[TrackAudio].syncTo(_stamp[TrackVideo]);
_ticker.resetTime();
weak_ptr<HlsPlayerImp> weakSelf = dynamic_pointer_cast<HlsPlayerImp>(shared_from_this());
//每50毫秒执行一次
_timer = std::make_shared<Timer>(0.05, [weakSelf]() {
auto strongSelf = weakSelf.lock();
if (!strongSelf) {
return false;
}
strongSelf->onTick();
return true;
}, getPoller());
} }
} }
void HlsPlayerImp::onShutdown(const SockException &ex) {
PlayerImp<HlsPlayer, PlayerBase>::onShutdown(ex);
_timer = nullptr;
}
vector<Track::Ptr> HlsPlayerImp::getTracks(bool trackReady) const { vector<Track::Ptr> HlsPlayerImp::getTracks(bool trackReady) const {
return MediaSink::getTracks(trackReady); return MediaSink::getTracks(trackReady);
} }
void HlsPlayerImp::inputFrame(const Frame::Ptr &frame) {
//计算相对时间戳
int64_t dts, pts;
_stamp[frame->getTrackType()].revise(frame->dts(), frame->pts(), dts, pts);
//根据时间戳缓存frame
_frame_cache.emplace(dts, Frame::getCacheAbleFrame(frame));
while (!_frame_cache.empty()) {
if (_frame_cache.rbegin()->first - _frame_cache.begin()->first > 30 * 1000) {
//缓存超过30秒强制消费掉
MediaSink::inputFrame(_frame_cache.begin()->second);
_frame_cache.erase(_frame_cache.begin());
continue;
}
//缓存小于30秒
break;
}
}
void HlsPlayerImp::onTick() {
auto it = _frame_cache.begin();
while (it != _frame_cache.end()) {
if (it->first > _ticker.elapsedTime()) {
//这些帧还未到时间播放
break;
}
//消费掉已经到期的帧
MediaSink::inputFrame(it->second);
it = _frame_cache.erase(it);
}
}
}//namespace mediakit }//namespace mediakit

View File

@ -135,9 +135,16 @@ private:
void onAllTrackReady() override; void onAllTrackReady() override;
void onPlayResult(const SockException &ex) override; void onPlayResult(const SockException &ex) override;
vector<Track::Ptr> getTracks(bool trackReady = true) const override; vector<Track::Ptr> getTracks(bool trackReady = true) const override;
void inputFrame(const Frame::Ptr &frame) override;
void onShutdown(const SockException &ex) override;
void onTick();
private: private:
TSSegment::onSegment _on_ts; TSSegment::onSegment _on_ts;
DecoderImp::Ptr _decoder; DecoderImp::Ptr _decoder;
multimap<int64_t, Frame::Ptr> _frame_cache;
Timer::Ptr _timer;
Ticker _ticker;
Stamp _stamp[2];
}; };
}//namespace mediakit }//namespace mediakit