HlsMediaSource.cpp 2.2 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 string &sessionIdentifier, const string &peer_ip, uint16_t peer_port) {
16
    _info = info;
17
    _sessionIdentifier = sessionIdentifier;
18 19
    _peer_ip = peer_ip;
    _peer_port = peer_port;
20
    _added = std::make_shared<bool>(false);
21 22 23 24
    addReaderCount();
}

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

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

52 53
        GET_CONFIG(uint32_t, iFlowThreshold, General::kFlowThreshold);
        if (_bytes > iFlowThreshold * 1024) {
54
            NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastFlowReport, _info, _bytes, duration, true, _sessionIdentifier, _peer_ip, _peer_port);
55
        }
56 57
    }
}
58

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


}//namespace mediakit
67