ZLMediaKit/src/Http/TsplayerImp.cpp
PioLing 7b1f8fedac
Some checks failed
Android / build (push) Has been cancelled
CodeQL / Analyze (cpp) (push) Has been cancelled
CodeQL / Analyze (javascript) (push) Has been cancelled
Docker / build (push) Has been cancelled
Linux / build (push) Has been cancelled
macOS / build (push) Has been cancelled
Windows / build (push) Has been cancelled
Add network traffic statistics (#4239)
Co-authored-by: xiongguangjie <xiong_panda@163.com>
Co-authored-by: xia-chu <771730766@qq.com>
2025-05-02 16:23:25 +08:00

87 lines
2.9 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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/ZLMediaKit/ZLMediaKit).
*
* Use of this source code is governed by MIT-like 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 "TsPlayerImp.h"
#include "HlsPlayer.h"
#include "Common/config.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
TsPlayerImp::TsPlayerImp(const EventPoller::Ptr &poller) : PlayerImp<TsPlayer, PlayerBase>(poller) {}
void TsPlayerImp::onResponseBody(const char *data, size_t len) {
TsPlayer::onResponseBody(data, len);
if (!_decoder && _demuxer) {
_decoder = DecoderImp::createDecoder(DecoderImp::decoder_ts, _demuxer.get());
}
if (_decoder && _demuxer) {
_decoder->input((uint8_t *) data, len);
}
}
void TsPlayerImp::addTrackCompleted() {
PlayerImp<TsPlayer, PlayerBase>::onPlayResult(SockException(Err_success, "play http-ts success"));
}
void TsPlayerImp::onPlayResult(const SockException &ex) {
auto benchmark_mode = (*this)[Client::kBenchmarkMode].as<int>();
if (ex || benchmark_mode) {
PlayerImp<TsPlayer, PlayerBase>::onPlayResult(ex);
} else {
auto demuxer = std::make_shared<HlsDemuxer>();
demuxer->start(getPoller(), this);
_demuxer = std::move(demuxer);
}
}
void TsPlayerImp::onShutdown(const SockException &ex) {
while (_demuxer) {
try {
// shared_from_this()可能抛异常 [AUTO-TRANSLATED:6af9bd3c]
// shared_from_this() may throw an exception
std::weak_ptr<TsPlayerImp> weak_self = static_pointer_cast<TsPlayerImp>(shared_from_this());
if (_decoder) {
_decoder->flush();
}
// 等待所有frame flush输出后再触发onShutdown事件 [AUTO-TRANSLATED:93982eb3]
// Wait for all frame flush output before triggering the onShutdown event
static_pointer_cast<HlsDemuxer>(_demuxer)->pushTask([weak_self, ex]() {
if (auto strong_self = weak_self.lock()) {
strong_self->_demuxer = nullptr;
strong_self->onShutdown(ex);
}
});
return;
} catch (...) {
break;
}
}
PlayerImp<TsPlayer, PlayerBase>::onShutdown(ex);
}
vector<Track::Ptr> TsPlayerImp::getTracks(bool ready) const {
if (!_demuxer) {
return vector<Track::Ptr>();
}
return static_pointer_cast<HlsDemuxer>(_demuxer)->getTracks(ready);
}
size_t TsPlayerImp::getRecvSpeed() {
return TcpClient::getRecvSpeed();
}
size_t TsPlayerImp::getRecvTotalBytes() {
return TcpClient::getRecvTotalBytes();
}
}//namespace mediakit