ZLMediaKit/src/Http/HttpTSPlayer.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

/*
2020-05-17 18:00:37 +08:00
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
*
* 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 "HttpTSPlayer.h"
2020-05-17 18:00:37 +08:00
namespace mediakit {
HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller, bool split_ts) {
2020-05-17 18:00:37 +08:00
_split_ts = split_ts;
_segment.setOnSegment([this](const char *data, size_t len) { onPacket(data, len); });
setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
2020-05-17 18:00:37 +08:00
}
2022-01-20 14:48:45 +08:00
void HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &header) {
2020-05-17 18:00:37 +08:00
if (status != "200" && status != "206") {
2022-01-20 14:48:45 +08:00
// http状态码不符合预期
throw invalid_argument("bad http status code:" + status);
2020-05-17 18:00:37 +08:00
}
2022-01-20 14:48:45 +08:00
auto content_type = const_cast<HttpClient::HttpHeader &>(header)["Content-Type"];
if (content_type.find("video/mp2t") != 0 && content_type.find("video/mpeg") != 0) {
throw invalid_argument("content type not mpeg-ts: " + content_type);
2020-05-17 18:00:37 +08:00
}
2022-01-20 14:48:45 +08:00
}
2020-05-17 18:00:37 +08:00
2022-01-20 14:48:45 +08:00
void HttpTSPlayer::onResponseBody(const char *buf, size_t size) {
2020-05-17 18:00:37 +08:00
if (_split_ts) {
2022-01-04 15:32:59 +08:00
try {
_segment.input(buf, size);
2022-01-09 15:01:23 +08:00
} catch (std::exception &ex) {
2022-01-04 15:32:59 +08:00
WarnL << ex.what();
2022-01-20 14:48:45 +08:00
// ts解析失败清空缓存数据
2022-01-04 15:32:59 +08:00
_segment.reset();
throw;
}
2020-05-17 18:00:37 +08:00
} else {
onPacket(buf, size);
}
}
2022-01-20 14:48:45 +08:00
void HttpTSPlayer::onResponseCompleted(const SockException &ex) {
emitOnComplete(ex);
}
void HttpTSPlayer::emitOnComplete(const SockException &ex) {
if (_on_complete) {
_on_complete(ex);
_on_complete = nullptr;
2020-05-17 18:00:37 +08:00
}
}
void HttpTSPlayer::onPacket(const char *data, size_t len) {
2020-05-17 18:00:37 +08:00
if (_on_segment) {
_on_segment(data, len);
}
}
void HttpTSPlayer::setOnComplete(onComplete cb) {
_on_complete = std::move(cb);
2020-05-17 18:00:37 +08:00
}
void HttpTSPlayer::setOnPacket(TSSegment::onSegment cb) {
_on_segment = std::move(cb);
2020-05-17 18:00:37 +08:00
}
2022-01-20 14:48:45 +08:00
} // namespace mediakit