2022-01-20 22:49:47 +08:00
|
|
|
|
/*
|
2022-01-09 15:01:23 +08:00
|
|
|
|
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
|
* Created by alex on 2021/4/6.
|
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
|
|
|
|
|
*
|
|
|
|
|
|
* 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.
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
2022-01-04 15:32:59 +08:00
|
|
|
|
#include "TsPlayerImp.h"
|
2022-01-20 14:48:45 +08:00
|
|
|
|
#include "HlsPlayer.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/config.h"
|
2022-01-04 15:32:59 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
|
2022-01-04 15:32:59 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
2022-01-09 15:01:23 +08:00
|
|
|
|
TsPlayerImp::TsPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<TsPlayer, PlayerBase>(poller) {}
|
2022-01-04 15:32:59 +08:00
|
|
|
|
|
2022-02-13 21:36:04 +08:00
|
|
|
|
void TsPlayerImp::onResponseBody(const char *data, size_t len) {
|
|
|
|
|
|
TsPlayer::onResponseBody(data, len);
|
2022-01-20 14:48:45 +08:00
|
|
|
|
if (!_decoder && _demuxer) {
|
2022-01-09 15:01:23 +08:00
|
|
|
|
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, _demuxer.get());
|
2022-01-04 15:32:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-01-09 15:01:23 +08:00
|
|
|
|
if (_decoder && _demuxer) {
|
|
|
|
|
|
_decoder->input((uint8_t *) data, len);
|
2022-01-04 15:32:59 +08:00
|
|
|
|
}
|
2022-01-09 15:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TsPlayerImp::addTrackCompleted() {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
PlayerImp<TsPlayer, PlayerBase>::onPlayResult(SockException(Err_success, "play http-ts success"));
|
2022-01-09 15:01:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TsPlayerImp::onPlayResult(const SockException &ex) {
|
2022-02-11 11:04:16 +08:00
|
|
|
|
auto benchmark_mode = (*this)[Client::kBenchmarkMode].as<int>();
|
|
|
|
|
|
if (ex || benchmark_mode) {
|
2022-01-09 15:01:23 +08:00
|
|
|
|
PlayerImp<TsPlayer, PlayerBase>::onPlayResult(ex);
|
|
|
|
|
|
} else {
|
2022-01-20 14:48:45 +08:00
|
|
|
|
auto demuxer = std::make_shared<HlsDemuxer>();
|
2022-01-09 15:01:23 +08:00
|
|
|
|
demuxer->start(getPoller(), this);
|
|
|
|
|
|
_demuxer = std::move(demuxer);
|
2022-01-04 15:32:59 +08:00
|
|
|
|
}
|
2022-01-09 15:01:23 +08:00
|
|
|
|
}
|
2022-01-04 15:32:59 +08:00
|
|
|
|
|
2022-01-09 15:01:23 +08:00
|
|
|
|
void TsPlayerImp::onShutdown(const SockException &ex) {
|
2022-06-01 13:05:49 +08:00
|
|
|
|
while (_demuxer) {
|
|
|
|
|
|
try {
|
2022-10-30 23:50:20 +08:00
|
|
|
|
//shared_from_this()可能抛异常
|
2022-06-01 13:05:49 +08:00
|
|
|
|
std::weak_ptr<TsPlayerImp> weak_self = static_pointer_cast<TsPlayerImp>(shared_from_this());
|
2022-10-30 23:50:20 +08:00
|
|
|
|
if (_decoder) {
|
|
|
|
|
|
_decoder->flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
//等待所有frame flush输出后,再触发onShutdown事件
|
2022-06-01 13:05:49 +08:00
|
|
|
|
static_pointer_cast<HlsDemuxer>(_demuxer)->pushTask([weak_self, ex]() {
|
2022-10-30 23:50:20 +08:00
|
|
|
|
if (auto strong_self = weak_self.lock()) {
|
2022-06-01 13:05:49 +08:00
|
|
|
|
strong_self->_demuxer = nullptr;
|
|
|
|
|
|
strong_self->onShutdown(ex);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
return;
|
|
|
|
|
|
} catch (...) {
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
2022-05-13 23:22:00 +08:00
|
|
|
|
}
|
2022-06-01 13:05:49 +08:00
|
|
|
|
PlayerImp<TsPlayer, PlayerBase>::onShutdown(ex);
|
2022-01-09 15:01:23 +08:00
|
|
|
|
}
|
2022-01-04 15:32:59 +08:00
|
|
|
|
|
2022-02-11 11:04:16 +08:00
|
|
|
|
vector<Track::Ptr> TsPlayerImp::getTracks(bool ready) const {
|
|
|
|
|
|
if (!_demuxer) {
|
|
|
|
|
|
return vector<Track::Ptr>();
|
|
|
|
|
|
}
|
2022-01-20 14:48:45 +08:00
|
|
|
|
return static_pointer_cast<HlsDemuxer>(_demuxer)->getTracks(ready);
|
2022-01-09 15:01:23 +08:00
|
|
|
|
}
|
2022-01-04 15:32:59 +08:00
|
|
|
|
|
|
|
|
|
|
}//namespace mediakit
|