HttpTSPlayer.cpp 1.76 KB
Newer Older
1
/*
2 3
 * Copyright (c) 2020 The ZLMediaKit project authors. All Rights Reserved.
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5 6 7 8 9 10 11
 *
 * 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"
12

夏楚 committed
13 14 15
using namespace std;
using namespace toolkit;

16 17
namespace mediakit {

18
HttpTSPlayer::HttpTSPlayer(const EventPoller::Ptr &poller) {
19
    setPoller(poller ? poller : EventPollerPool::Instance().getPoller());
20 21
}

22
void HttpTSPlayer::onResponseHeader(const string &status, const HttpClient::HttpHeader &header) {
23
    if (status != "200" && status != "206") {
24 25
        // http状态码不符合预期
        throw invalid_argument("bad http status code:" + status);
26 27
    }

28
    auto content_type = strToLower(const_cast<HttpClient::HttpHeader &>(header)["Content-Type"]);
29
    if (content_type.find("video/mp2t") != 0 && content_type.find("video/mpeg") != 0 && content_type.find("application/octet-stream") != 0) {
30
        WarnL << "may not a mpeg-ts video: " << content_type << ", url: " << getUrl();
31
    }
32
}
33

34
void HttpTSPlayer::onResponseBody(const char *buf, size_t size) {
35 36
    if (_on_segment) {
        _on_segment(buf, size);
37 38 39
    }
}

40
void HttpTSPlayer::onResponseCompleted(const SockException &ex) {
41 42 43 44 45 46
    emitOnComplete(ex);
}

void HttpTSPlayer::emitOnComplete(const SockException &ex) {
    if (_on_complete) {
        _on_complete(ex);
47
        _on_complete = nullptr;
48 49 50
    }
}

51 52
void HttpTSPlayer::setOnComplete(onComplete cb) {
    _on_complete = std::move(cb);
53 54
}

55 56
void HttpTSPlayer::setOnPacket(TSSegment::onSegment cb) {
    _on_segment = std::move(cb);
57 58
}

59
} // namespace mediakit