2020-05-18 15:31:49 +08:00
|
|
|
|
/*
|
2020-05-17 18:00:37 +08:00
|
|
|
|
* Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
|
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* 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"
|
2021-12-24 13:29:16 +08:00
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace std;
|
|
|
|
|
using namespace toolkit;
|
|
|
|
|
|
2020-05-17 18:00:37 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
2022-02-13 21:36:04 +08:00
|
|
|
|
HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller) {
|
2020-09-12 19:03:52 +08:00
|
|
|
|
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-21 16:10:27 +08:00
|
|
|
|
auto content_type = strToLower(const_cast<HttpClient::HttpHeader &>(header)["Content-Type"]);
|
2022-03-12 09:35:22 +08:00
|
|
|
|
if (content_type.find("video/mp2t") != 0 && content_type.find("video/mpeg") != 0 && content_type.find("application/octet-stream") != 0) {
|
2022-01-21 16:10:27 +08:00
|
|
|
|
WarnL << "may not a mpeg-ts video: " << content_type << ", url: " << getUrl();
|
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) {
|
2022-02-13 21:36:04 +08:00
|
|
|
|
if (_on_segment) {
|
|
|
|
|
_on_segment(buf, size);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-20 14:48:45 +08:00
|
|
|
|
void HttpTSPlayer::onResponseCompleted(const SockException &ex) {
|
2021-12-24 13:29:16 +08:00
|
|
|
|
emitOnComplete(ex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpTSPlayer::emitOnComplete(const SockException &ex) {
|
|
|
|
|
if (_on_complete) {
|
|
|
|
|
_on_complete(ex);
|
2022-01-04 16:52:51 +08:00
|
|
|
|
_on_complete = nullptr;
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 13:29:16 +08:00
|
|
|
|
void HttpTSPlayer::setOnComplete(onComplete cb) {
|
|
|
|
|
_on_complete = std::move(cb);
|
2020-05-17 18:00:37 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-24 13:29:16 +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
|