HlsMediaSource.cpp 2.14 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3 4 5
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
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"
12 13 14

namespace mediakit{

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

void HlsCookieData::addReaderCount(){
23
    if(!*_added){
24 25 26
        auto src = dynamic_pointer_cast<HlsMediaSource>(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid));
        if(src){
            src->modifyReaderCount(true);
27
            *_added = true;
28
            _src = src;
29 30 31 32 33 34
            _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller());
            auto added = _added;
            _ring_reader->setDetachCB([added](){
                //HlsMediaSource已经销毁
                *added = false;
            });
35
        }
36 37
    }
}
38

39
HlsCookieData::~HlsCookieData() {
40
    if (*_added) {
41
        auto src = _src.lock();
42
        if (src) {
43 44
            src->modifyReaderCount(false);
        }
45
        uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000;
46
        WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port() << ") "
47 48 49
              << "HLS播放器(" << _info._vhost << "/" << _info._app << "/" << _info._streamid
              << ")断开,耗时(s):" << duration;

50 51
        GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
        if (_bytes > iFlowThreshold * 1024) {
52
            NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _info, _bytes, duration, true, static_cast<SockInfo&>(*_sock_info));
53
        }
54 55
    }
}
56

57
void HlsCookieData::addByteUsage(uint64_t bytes) {
58
    addReaderCount();
59
    _bytes += bytes;
60
    _ticker.resetTime();
61
}
62 63 64


}//namespace mediakit
65