2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2020-05-17 18:00:37 +08:00
|
|
|
|
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2020-05-17 18:00:37 +08:00
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "HlsPlayer.h"
|
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
HlsPlayer::HlsPlayer(const EventPoller::Ptr &poller){
|
2021-01-17 18:31:50 +08:00
|
|
|
|
_segment.setOnSegment([this](const char *data, size_t len) { onPacket(data, len); });
|
2020-09-12 19:03:52 +08:00
|
|
|
|
setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HlsPlayer::~HlsPlayer() {}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::play(const string &strUrl) {
|
|
|
|
|
_m3u8_list.emplace_back(strUrl);
|
|
|
|
|
play_l();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::play_l(){
|
|
|
|
|
if (_m3u8_list.empty()) {
|
|
|
|
|
teardown_l(SockException(Err_shutdown, "所有hls url都尝试播放失败!"));
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-12-22 11:33:40 +08:00
|
|
|
|
if (alive() && _waiting_response) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
float playTimeOutSec = (*this)[Client::kTimeoutMS].as<int>() / 1000.0f;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
setMethod("GET");
|
|
|
|
|
if(!(*this)[kNetAdapter].empty()) {
|
|
|
|
|
setNetAdapter((*this)[kNetAdapter]);
|
|
|
|
|
}
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_waiting_response = true;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
sendRequest(_m3u8_list.back(), playTimeOutSec);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::teardown_l(const SockException &ex){
|
|
|
|
|
_timer.reset();
|
|
|
|
|
_timer_ts.reset();
|
|
|
|
|
_http_ts_player.reset();
|
|
|
|
|
shutdown(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::teardown() {
|
|
|
|
|
teardown_l(SockException(Err_shutdown,"teardown"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::playNextTs(bool force){
|
|
|
|
|
if (_ts_list.empty()) {
|
|
|
|
|
//播放列表为空,那么立即重新下载m3u8文件
|
|
|
|
|
_timer.reset();
|
|
|
|
|
play_l();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (!force && _http_ts_player && _http_ts_player->alive()) {
|
|
|
|
|
//播放器目前还存活,正在下载中
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto ts_duration = _ts_list.front().duration * 1000;
|
|
|
|
|
weak_ptr<HlsPlayer> weakSelf = dynamic_pointer_cast<HlsPlayer>(shared_from_this());
|
|
|
|
|
std::shared_ptr<Ticker> ticker(new Ticker);
|
|
|
|
|
|
|
|
|
|
_http_ts_player = std::make_shared<HttpTSPlayer>(getPoller(), false);
|
2020-09-12 19:03:52 +08:00
|
|
|
|
|
|
|
|
|
_http_ts_player->setOnCreateSocket([weakSelf](const EventPoller::Ptr &poller) {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (strongSelf) {
|
|
|
|
|
return strongSelf->createSocket();
|
|
|
|
|
}
|
|
|
|
|
return Socket::createSocket(poller, true);
|
|
|
|
|
});
|
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
_http_ts_player->setOnDisconnect([weakSelf, ticker, ts_duration](const SockException &err) {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto delay = ts_duration - 500 - ticker->elapsedTime();
|
|
|
|
|
if (delay <= 0) {
|
|
|
|
|
//播放这个ts切片花费了太长时间,我们立即下一个切片的播放
|
|
|
|
|
strongSelf->playNextTs(true);
|
|
|
|
|
} else {
|
|
|
|
|
//下一个切片慢点播放
|
2021-01-17 20:15:08 +08:00
|
|
|
|
strongSelf->_timer_ts.reset(new Timer(delay / 1000.0f, [weakSelf]() {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
strongSelf->playNextTs(true);
|
|
|
|
|
return false;
|
|
|
|
|
}, strongSelf->getPoller()));
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-12 19:03:52 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
_http_ts_player->setOnPacket([weakSelf](const char *data, size_t len) {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (!strongSelf) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
//收到ts包
|
|
|
|
|
strongSelf->onPacket_l(data, len);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
_http_ts_player->setMethod("GET");
|
2020-09-12 19:03:52 +08:00
|
|
|
|
if (!(*this)[kNetAdapter].empty()) {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
_http_ts_player->setNetAdapter((*this)[Client::kNetAdapter]);
|
|
|
|
|
}
|
2020-09-12 19:03:52 +08:00
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
_http_ts_player->sendRequest(_ts_list.front().url, 2 * _ts_list.front().duration);
|
|
|
|
|
_ts_list.pop_front();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::onParsed(bool is_m3u8_inner,int64_t sequence,const map<int,ts_segment> &ts_map){
|
|
|
|
|
if (!is_m3u8_inner) {
|
|
|
|
|
//这是ts播放列表
|
|
|
|
|
if (_last_sequence == sequence) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_last_sequence = sequence;
|
|
|
|
|
for (auto &pr : ts_map) {
|
|
|
|
|
auto &ts = pr.second;
|
|
|
|
|
if (_ts_url_cache.emplace(ts.url).second) {
|
|
|
|
|
//该ts未重复
|
|
|
|
|
_ts_list.emplace_back(ts);
|
|
|
|
|
//按时间排序
|
|
|
|
|
_ts_url_sort.emplace_back(ts.url);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (_ts_url_sort.size() > 2 * ts_map.size()) {
|
|
|
|
|
//去除防重列表中过多的数据
|
|
|
|
|
_ts_url_cache.erase(_ts_url_sort.front());
|
|
|
|
|
_ts_url_sort.pop_front();
|
|
|
|
|
}
|
|
|
|
|
playNextTs();
|
|
|
|
|
} else {
|
|
|
|
|
//这是m3u8列表,我们播放最高清的子hls
|
|
|
|
|
if (ts_map.empty()) {
|
|
|
|
|
teardown_l(SockException(Err_shutdown, StrPrinter << "empty sub hls list:" + getUrl()));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_timer.reset();
|
|
|
|
|
|
|
|
|
|
weak_ptr<HlsPlayer> weakSelf = dynamic_pointer_cast<HlsPlayer>(shared_from_this());
|
|
|
|
|
auto url = ts_map.rbegin()->second.url;
|
|
|
|
|
getPoller()->async([weakSelf, url]() {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (strongSelf) {
|
|
|
|
|
strongSelf->play(url);
|
|
|
|
|
}
|
|
|
|
|
}, false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
ssize_t HlsPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &headers) {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
if (status != "200" && status != "206") {
|
|
|
|
|
//失败
|
|
|
|
|
teardown_l(SockException(Err_shutdown, StrPrinter << "bad http status code:" + status));
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
auto contet_type = const_cast< HttpClient::HttpHeader &>(headers)["Content-Type"];
|
|
|
|
|
_is_m3u8 = (contet_type.find("application/vnd.apple.mpegurl") == 0);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void HlsPlayer::onResponseBody(const char *buf, size_t size, size_t recvedSize, size_t totalSize) {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
if (recvedSize == size) {
|
|
|
|
|
//刚开始
|
|
|
|
|
_m3u8.clear();
|
|
|
|
|
}
|
|
|
|
|
_m3u8.append(buf, size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::onResponseCompleted() {
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_waiting_response = false;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
if (HlsParser::parse(getUrl(), _m3u8)) {
|
|
|
|
|
playDelay();
|
|
|
|
|
if (_first) {
|
|
|
|
|
_first = false;
|
|
|
|
|
onPlayResult(SockException(Err_success, "play success"));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
teardown_l(SockException(Err_shutdown, "解析m3u8文件失败"));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-17 02:04:28 +08:00
|
|
|
|
float HlsPlayer::delaySecond() {
|
2020-05-17 18:00:37 +08:00
|
|
|
|
if (HlsParser::isM3u8() && HlsParser::getTargetDur() > 0) {
|
2021-12-17 02:04:28 +08:00
|
|
|
|
float targetOffset;
|
|
|
|
|
if (HlsParser::isLive()) {
|
|
|
|
|
// see RFC 8216, Section 4.4.3.8.
|
|
|
|
|
// 根据rfc刷新index列表的周期应该是分段时间x3, 因为根据规范播放器只处理最后3个Segment
|
|
|
|
|
targetOffset = (float) (3 * HlsParser::getTargetDur());
|
|
|
|
|
} else {
|
|
|
|
|
// 点播则一般m3u8文件不会在改变了, 没必要频繁的刷新, 所以按照总时间来进行刷新
|
|
|
|
|
targetOffset = HlsParser::getTotalDuration();
|
|
|
|
|
}
|
|
|
|
|
// 取最小值, 避免因为分段时长不规则而导致的问题
|
|
|
|
|
if (targetOffset > HlsParser::getTotalDuration()) {
|
|
|
|
|
targetOffset = HlsParser::getTotalDuration();
|
|
|
|
|
}
|
|
|
|
|
// 根据规范为一半的时间
|
2021-12-17 11:40:54 +08:00
|
|
|
|
if (targetOffset / 2 > 1.0f) {
|
2021-12-17 02:04:28 +08:00
|
|
|
|
return targetOffset / 2;
|
|
|
|
|
}
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
2021-01-17 18:31:50 +08:00
|
|
|
|
return 1.0f;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::onDisconnect(const SockException &ex) {
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_waiting_response = false;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
if (_first) {
|
|
|
|
|
//第一次失败,则播放失败
|
|
|
|
|
_first = false;
|
|
|
|
|
onPlayResult(ex);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//主动shutdown
|
|
|
|
|
if (ex.getErrCode() == Err_shutdown) {
|
|
|
|
|
if (_m3u8_list.size() <= 1) {
|
|
|
|
|
//全部url都播放失败
|
2021-12-22 11:33:40 +08:00
|
|
|
|
_timer = nullptr;
|
|
|
|
|
_timer_ts = nullptr;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
onShutdown(ex);
|
|
|
|
|
} else {
|
|
|
|
|
_m3u8_list.pop_back();
|
|
|
|
|
//还有上一级url可以重试播放
|
|
|
|
|
play_l();
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//eof等,之后播放失败,那么重试播放m3u8
|
|
|
|
|
playDelay();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HlsPlayer::onRedirectUrl(const string &url,bool temporary) {
|
|
|
|
|
_m3u8_list.emplace_back(url);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayer::playDelay(){
|
|
|
|
|
weak_ptr<HlsPlayer> weakSelf = dynamic_pointer_cast<HlsPlayer>(shared_from_this());
|
|
|
|
|
_timer.reset(new Timer(delaySecond(), [weakSelf]() {
|
|
|
|
|
auto strongSelf = weakSelf.lock();
|
|
|
|
|
if (strongSelf) {
|
|
|
|
|
strongSelf->play_l();
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}, getPoller()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void HlsPlayer::onPacket_l(const char *data, size_t len){
|
2020-05-17 18:00:37 +08:00
|
|
|
|
_segment.input(data,len);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
class HlsDemuxer : public MediaSinkInterface, public TrackSource, public std::enable_shared_from_this<HlsDemuxer> {
|
|
|
|
|
public:
|
|
|
|
|
HlsDemuxer() = default;
|
|
|
|
|
~HlsDemuxer() override { _timer = nullptr; }
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
void start(const EventPoller::Ptr &poller, TrackListener *listener);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
bool inputFrame(const Frame::Ptr &frame) override;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
bool addTrack(const Track::Ptr &track) override {
|
|
|
|
|
return _delegate.addTrack(track);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
void addTrackCompleted() override {
|
|
|
|
|
_delegate.addTrackCompleted();
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
void resetTracks() override {
|
|
|
|
|
((MediaSink &)_delegate).resetTracks();
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
vector<Track::Ptr> getTracks(bool ready = true) const override {
|
|
|
|
|
return _delegate.getTracks(ready);
|
2020-05-17 19:10:08 +08:00
|
|
|
|
}
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
private:
|
|
|
|
|
void onTick();
|
|
|
|
|
int64_t getBufferMS();
|
|
|
|
|
int64_t getPlayPosition();
|
|
|
|
|
void setPlayPosition(int64_t pos);
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int64_t _ticker_offset = 0;
|
|
|
|
|
Ticker _ticker;
|
|
|
|
|
Stamp _stamp[2];
|
|
|
|
|
Timer::Ptr _timer;
|
|
|
|
|
MediaSinkDelegate _delegate;
|
|
|
|
|
multimap<int64_t, Frame::Ptr> _frame_cache;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
void HlsDemuxer::start(const EventPoller::Ptr &poller, TrackListener *listener) {
|
|
|
|
|
_frame_cache.clear();
|
|
|
|
|
_stamp[TrackAudio].setRelativeStamp(0);
|
|
|
|
|
_stamp[TrackVideo].setRelativeStamp(0);
|
|
|
|
|
_stamp[TrackAudio].syncTo(_stamp[TrackVideo]);
|
|
|
|
|
setPlayPosition(0);
|
|
|
|
|
|
|
|
|
|
_delegate.setTrackListener(listener);
|
|
|
|
|
|
|
|
|
|
//每50毫秒执行一次
|
|
|
|
|
weak_ptr<HlsDemuxer> weak_self = shared_from_this();
|
|
|
|
|
_timer = std::make_shared<Timer>(0.05f, [weak_self]() {
|
|
|
|
|
auto strong_self = weak_self.lock();
|
|
|
|
|
if (!strong_self) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
strong_self->onTick();
|
|
|
|
|
return true;
|
|
|
|
|
}, poller);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
bool HlsDemuxer::inputFrame(const Frame::Ptr &frame) {
|
2021-12-17 14:47:17 +08:00
|
|
|
|
//为了避免track准备时间过长, 因此在没准备好之前, 直接消费掉所有的帧
|
|
|
|
|
if (!_delegate.isAllTrackReady()) {
|
|
|
|
|
_delegate.inputFrame(frame);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 23:19:13 +08:00
|
|
|
|
//计算相对时间戳
|
|
|
|
|
int64_t dts, pts;
|
|
|
|
|
_stamp[frame->getTrackType()].revise(frame->dts(), frame->pts(), dts, pts);
|
|
|
|
|
//根据时间戳缓存frame
|
|
|
|
|
_frame_cache.emplace(dts, Frame::getCacheAbleFrame(frame));
|
|
|
|
|
|
2020-09-06 17:56:45 +08:00
|
|
|
|
if (getBufferMS() > 30 * 1000) {
|
|
|
|
|
//缓存超过30秒,强制消费至15秒(减少延时或内存占用)
|
|
|
|
|
while (getBufferMS() > 15 * 1000) {
|
2021-12-01 22:09:05 +08:00
|
|
|
|
_delegate.inputFrame(_frame_cache.begin()->second);
|
2020-05-26 23:19:13 +08:00
|
|
|
|
_frame_cache.erase(_frame_cache.begin());
|
|
|
|
|
}
|
2020-09-06 17:56:45 +08:00
|
|
|
|
//接着播放缓存中最早的帧
|
|
|
|
|
setPlayPosition(_frame_cache.begin()->first);
|
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return true;
|
2020-09-06 17:56:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
int64_t HlsDemuxer::getPlayPosition() {
|
2020-09-06 17:56:45 +08:00
|
|
|
|
return _ticker.elapsedTime() + _ticker_offset;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
int64_t HlsDemuxer::getBufferMS() {
|
|
|
|
|
if (_frame_cache.empty()) {
|
2020-09-06 17:56:45 +08:00
|
|
|
|
return 0;
|
2020-05-26 23:19:13 +08:00
|
|
|
|
}
|
2020-09-06 17:56:45 +08:00
|
|
|
|
return _frame_cache.rbegin()->first - _frame_cache.begin()->first;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
void HlsDemuxer::setPlayPosition(int64_t pos) {
|
2020-09-06 17:56:45 +08:00
|
|
|
|
_ticker.resetTime();
|
|
|
|
|
_ticker_offset = pos;
|
2020-05-26 23:19:13 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
void HlsDemuxer::onTick() {
|
2020-05-26 23:19:13 +08:00
|
|
|
|
auto it = _frame_cache.begin();
|
|
|
|
|
while (it != _frame_cache.end()) {
|
2020-09-06 17:56:45 +08:00
|
|
|
|
if (it->first > getPlayPosition()) {
|
2020-05-26 23:19:13 +08:00
|
|
|
|
//这些帧还未到时间播放
|
|
|
|
|
break;
|
|
|
|
|
}
|
2020-09-12 18:53:16 +08:00
|
|
|
|
|
|
|
|
|
if (getBufferMS() < 3 * 1000) {
|
|
|
|
|
//缓存小于3秒,那么降低定时器消费速度(让剩余的数据在3秒后消费完毕)
|
|
|
|
|
//目的是为了防止定时器长时间干等后,数据瞬间消费完毕
|
|
|
|
|
setPlayPosition(_frame_cache.begin()->first);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-26 23:19:13 +08:00
|
|
|
|
//消费掉已经到期的帧
|
2021-12-01 22:09:05 +08:00
|
|
|
|
_delegate.inputFrame(it->second);
|
2020-05-26 23:19:13 +08:00
|
|
|
|
it = _frame_cache.erase(it);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-01 22:09:05 +08:00
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
HlsPlayerImp::HlsPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<HlsPlayer, PlayerBase>(poller) {}
|
|
|
|
|
|
|
|
|
|
void HlsPlayerImp::onPacket(const char *data,size_t len) {
|
|
|
|
|
if (!_decoder) {
|
|
|
|
|
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, _demuxer.get());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (_decoder && _demuxer) {
|
|
|
|
|
_decoder->input((uint8_t *) data, len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayerImp::addTrackCompleted() {
|
|
|
|
|
PlayerImp<HlsPlayer, PlayerBase>::onPlayResult(SockException(Err_success, "play hls success"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayerImp::onPlayResult(const SockException &ex) {
|
|
|
|
|
if (ex) {
|
|
|
|
|
PlayerImp<HlsPlayer, PlayerBase>::onPlayResult(ex);
|
|
|
|
|
} else {
|
|
|
|
|
auto demuxer = std::make_shared<HlsDemuxer>();
|
|
|
|
|
demuxer->start(getPoller(), this);
|
|
|
|
|
_demuxer = std::move(demuxer);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HlsPlayerImp::onShutdown(const SockException &ex) {
|
2021-12-22 11:33:40 +08:00
|
|
|
|
WarnL << ex.what();
|
2021-12-01 22:09:05 +08:00
|
|
|
|
PlayerImp<HlsPlayer, PlayerBase>::onShutdown(ex);
|
|
|
|
|
_demuxer = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
vector<Track::Ptr> HlsPlayerImp::getTracks(bool ready) const {
|
|
|
|
|
return static_pointer_cast<HlsDemuxer>(_demuxer)->getTracks(ready);
|
|
|
|
|
}
|
2020-05-17 18:00:37 +08:00
|
|
|
|
|
2021-12-17 02:04:28 +08:00
|
|
|
|
}//namespace mediakit
|