HlsMediaSource.cpp 2.01 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
        auto src = dynamic_pointer_cast<HlsMediaSource>(MediaSource::find(HLS_SCHEMA,_info._vhost,_info._app,_info._streamid));
        if(src){
26 27 28 29 30 31 32
            *_added = true;
            _ring_reader = src->getRing()->attach(EventPollerPool::Instance().getPoller());
            auto added = _added;
            _ring_reader->setDetachCB([added](){
                //HlsMediaSource已经销毁
                *added = false;
            });
33
        }
34 35
    }
}
36

37
HlsCookieData::~HlsCookieData() {
38
    if (*_added) {
39
        uint64_t duration = (_ticker.createdTime() - _ticker.elapsedTime()) / 1000;
40
        WarnL << _sock_info->getIdentifier() << "(" << _sock_info->get_peer_ip() << ":" << _sock_info->get_peer_port() << ") "
41 42 43
              << "HLS播放器(" << _info._vhost << "/" << _info._app << "/" << _info._streamid
              << ")断开,耗时(s):" << duration;

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

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


}//namespace mediakit
60