ZLMediaKit/src/Http/TsPlayer.cpp

69 lines
2.0 KiB
C++
Raw Normal View History

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 "TsPlayer.h"
namespace mediakit {
2022-01-09 15:01:23 +08:00
TsPlayer::TsPlayer(const EventPoller::Ptr &poller) : HttpTSPlayer(poller, true) {}
2022-01-04 15:32:59 +08:00
2022-01-09 15:01:23 +08:00
void TsPlayer::play(const string &strUrl) {
_ts_url.append(strUrl);
playTs();
}
2022-01-04 15:32:59 +08:00
2022-01-09 15:01:23 +08:00
void TsPlayer::teardown() {
shutdown(SockException(Err_shutdown, "teardown"));
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 TsPlayer::playTs() {
if (waitResponse()) {
//播放器目前还存活,正在下载中
return;
2022-01-04 15:32:59 +08:00
}
TraceL << "play http-ts: " << _ts_url;
2022-01-09 15:01:23 +08:00
weak_ptr <TsPlayer> weak_self = dynamic_pointer_cast<TsPlayer>(shared_from_this());
setMethod("GET");
setHeaderTimeout((*this)[Client::kTimeoutMS].as<int>());
setBodyTimeout((*this)[Client::kMediaTimeoutMS].as<int>());
sendRequest(_ts_url);
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 TsPlayer::onResponseCompleted() {
//接收完毕
shutdown(SockException(Err_success, StrPrinter << "play " << _ts_url << " completed"));
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 TsPlayer::onDisconnect(const SockException &ex) {
2022-01-19 22:53:21 +08:00
WarnL << "play " << _ts_url << " failed: " << ex.getErrCode() << " " << ex.what();
2022-01-09 15:01:23 +08:00
if (_first) {
//第一次失败,则播放失败
_first = false;
onPlayResult(ex);
return;
2022-01-04 15:32:59 +08:00
}
2022-01-09 15:01:23 +08:00
if (ex.getErrCode() == Err_shutdown) {
onShutdown(ex);
} else {
onResponseCompleted();
onShutdown(ex);
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
ssize_t TsPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &header) {
ssize_t ret = HttpTSPlayer::onResponseHeader(status, header);
if (_first) {
_first = false;
onPlayResult(SockException(Err_success, "play success"));
2022-01-04 15:32:59 +08:00
}
2022-01-09 15:01:23 +08:00
return ret;
}
2022-01-04 15:32:59 +08:00
}//namespace mediakit