ZLMediaKit/src/Http/HttpTSPlayer.cpp
夏楚 c72cf4cbcc
整理命名空间 (#1409)
* feat: remove using namespace mediakit in header files.

(cherry picked from commit d44aeb339a8a0e1f0455be82b21fe4b1b536299f)

* feat: remove using namespace mediakit in FFmpegSource.h

* feat: remove using namespace mediakit in RtpExt.h

* feat: remove using namespace mediakit in header files.

* feat: remove using namespace std in header files.

* feat: remove using namespace std in header files when zltoolkit remove std in header

* 补充命名空间

* 整理命名空间

* 整理命名空间2

* 修复macos ci

* 修复编译问题

* 修复编译问题2

* 修复编译问题3

Co-authored-by: Johnny <hellojinqiang@gmail.com>
Co-authored-by: Xiaofeng Wang <wasphin@gmail.com>
2022-02-02 20:34:50 +08:00

77 lines
2.2 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.
*
* 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.
*/
#include "HttpTSPlayer.h"
using namespace std;
using namespace toolkit;
namespace mediakit {
HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller, bool split_ts) {
_split_ts = split_ts;
_segment.setOnSegment([this](const char *data, size_t len) { onPacket(data, len); });
setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
}
void HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &header) {
if (status != "200" && status != "206") {
// http状态码不符合预期
throw invalid_argument("bad http status code:" + status);
}
auto content_type = strToLower(const_cast<HttpClient::HttpHeader &>(header)["Content-Type"]);
if (content_type.find("video/mp2t") != 0 && content_type.find("video/mpeg") != 0) {
WarnL << "may not a mpeg-ts video: " << content_type << ", url: " << getUrl();
}
}
void HttpTSPlayer::onResponseBody(const char *buf, size_t size) {
if (_split_ts) {
try {
_segment.input(buf, size);
} catch (std::exception &ex) {
WarnL << ex.what();
// ts解析失败清空缓存数据
_segment.reset();
throw;
}
} else {
onPacket(buf, size);
}
}
void HttpTSPlayer::onResponseCompleted(const SockException &ex) {
emitOnComplete(ex);
}
void HttpTSPlayer::emitOnComplete(const SockException &ex) {
if (_on_complete) {
_on_complete(ex);
_on_complete = nullptr;
}
}
void HttpTSPlayer::onPacket(const char *data, size_t len) {
if (_on_segment) {
_on_segment(data, len);
}
}
void HttpTSPlayer::setOnComplete(onComplete cb) {
_on_complete = std::move(cb);
}
void HttpTSPlayer::setOnPacket(TSSegment::onSegment cb) {
_on_segment = std::move(cb);
}
} // namespace mediakit