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

11
#include "HlsMediaSource.h"
mtdxc committed
12
#include "Common/config.h"
13

夏楚 committed
14 15
using namespace toolkit;

16
namespace mediakit {
17

18
HlsCookieData::HlsCookieData(const MediaInfo &info, const std::shared_ptr<SockInfo> &sock_info) {
19
    _info = info;
20
    _sock_info = sock_info;
21
    _added = std::make_shared<bool>(false);
22 23 24
    addReaderCount();
}

25 26
void HlsCookieData::addReaderCount() {
    if (!*_added) {
27
        auto src = getMediaSource();
28
        if (src) {
29 30 31
            *_added = true;
            _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller());
            auto added = _added;
32 33
            _ring_reader->setDetachCB([added]() {
                // HlsMediaSource已经销毁
34 35
                *added = false;
            });
36
        }
37 38
    }
}
39

40
HlsCookieData::~HlsCookieData() {
41
    if (*_added) {
42
        uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000;
43
        WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port()
cqm committed
44
              << ") " << "HLS播放器(" << _info.shortUrl() << ")断开,耗时(s):" << duration;
45

46
        GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
47
        uint64_t bytes = _bytes.load();
48
        if (bytes >= iFlowThreshold * 1024) {
49
            NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _info, bytes, duration, true, static_cast<SockInfo &>(*_sock_info));
50
        }
51 52
    }
}
53

54
void HlsCookieData::addByteUsage(size_t bytes) {
55
    addReaderCount();
56
    _bytes += bytes;
57
    _ticker.resetTime();
58
}
59

60 61 62
void HlsCookieData::setMediaSource(const HlsMediaSource::Ptr &src) {
    _src = src;
}
63

64 65 66
HlsMediaSource::Ptr HlsCookieData::getMediaSource() const {
    return _src.lock();
}
67

mtdxc committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
void HlsMediaSource::setIndexFile(std::string index_file)
{
    if (!_ring) {
        std::weak_ptr<HlsMediaSource> weakSelf = std::dynamic_pointer_cast<HlsMediaSource>(shared_from_this());
        auto lam = [weakSelf](int size) {
            auto strongSelf = weakSelf.lock();
            if (!strongSelf) {
                return;
            }
            strongSelf->onReaderChanged(size);
        };
        _ring = std::make_shared<RingType>(0, std::move(lam));
        regist();
    }

    //赋值m3u8索引文件内容
    std::lock_guard<std::mutex> lck(_mtx_index);
    _index_file = std::move(index_file);

    if (!_index_file.empty()) {
        _list_cb.for_each([&](const std::function<void(const std::string& str)>& cb) { cb(_index_file); });
        _list_cb.clear();
    }
}

void HlsMediaSource::getIndexFile(std::function<void(const std::string& str)> cb)
{
    std::lock_guard<std::mutex> lck(_mtx_index);
    if (!_index_file.empty()) {
        cb(_index_file);
        return;
    }
    //等待生成m3u8文件
    _list_cb.emplace_back(std::move(cb));
}

104
} // namespace mediakit