RtpSelector.cpp 3.84 KB
Newer Older
Gemfield committed
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
 */
Gemfield committed
10

11
#if defined(ENABLE_RTPPROXY)
Gemfield committed
12 13
#include "RtpSelector.h"

14 15
namespace mediakit{

Gemfield committed
16 17
INSTANCE_IMP(RtpSelector);

18
bool RtpSelector::inputRtp(const Socket::Ptr &sock, const char *data, int data_len,const struct sockaddr *addr,uint32_t *dts_out) {
19 20
    uint32_t ssrc = 0;
    if(!getSSRC(data,data_len,ssrc)){
Gemfield committed
21 22 23 24 25
        WarnL << "get ssrc from rtp failed:" << data_len;
        return false;
    }
    auto process = getProcess(ssrc, true);
    if(process){
26
        return process->inputRtp(sock, data,data_len, addr,dts_out);
Gemfield committed
27 28 29 30
    }
    return false;
}

31
bool RtpSelector::getSSRC(const char *data,int data_len, uint32_t &ssrc){
Gemfield committed
32
    if(data_len < 12){
33
        return false;
Gemfield committed
34 35
    }
    uint32_t *ssrc_ptr = (uint32_t *)(data + 8);
36 37
    ssrc = ntohl(*ssrc_ptr);
    return true;
Gemfield committed
38 39 40 41 42 43 44 45
}

RtpProcess::Ptr RtpSelector::getProcess(uint32_t ssrc,bool makeNew) {
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
    auto it = _map_rtp_process.find(ssrc);
    if(it == _map_rtp_process.end() && !makeNew){
        return nullptr;
    }
46
    RtpProcessHelper::Ptr &ref = _map_rtp_process[ssrc];
Gemfield committed
47
    if(!ref){
48 49
        ref = std::make_shared<RtpProcessHelper>(ssrc,shared_from_this());
        ref->attachEvent();
50
        createTimer();
Gemfield committed
51
    }
52
    return ref->getProcess();
Gemfield committed
53 54
}

55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
void RtpSelector::createTimer() {
    if (!_timer) {
        //创建超时管理定时器
        weak_ptr<RtpSelector> weakSelf = shared_from_this();
        _timer = std::make_shared<Timer>(3.0, [weakSelf] {
            auto strongSelf = weakSelf.lock();
            if (!strongSelf) {
                return false;
            }
            strongSelf->onManager();
            return true;
        }, EventPollerPool::Instance().getPoller());
    }
}

Gemfield committed
70 71 72 73 74 75 76
void RtpSelector::delProcess(uint32_t ssrc,const RtpProcess *ptr) {
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
    auto it = _map_rtp_process.find(ssrc);
    if(it == _map_rtp_process.end()){
        return;
    }

77
    if(it->second->getProcess().get() != ptr){
Gemfield committed
78 79 80 81 82 83 84 85 86
        return;
    }

    _map_rtp_process.erase(it);
}

void RtpSelector::onManager() {
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
    for (auto it = _map_rtp_process.begin(); it != _map_rtp_process.end();) {
87
        if (it->second->getProcess()->alive()) {
Gemfield committed
88 89 90 91 92 93 94 95 96 97 98 99 100
            ++it;
            continue;
        }
        WarnL << "RtpProcess timeout:" << printSSRC(it->first);
        it = _map_rtp_process.erase(it);
    }
}

RtpSelector::RtpSelector() {
}

RtpSelector::~RtpSelector() {
}
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
RtpProcessHelper::RtpProcessHelper(uint32_t ssrc, const weak_ptr<RtpSelector> &parent) {
    _ssrc = ssrc;
    _parent = parent;
    _process = std::make_shared<RtpProcess>(_ssrc);
}

RtpProcessHelper::~RtpProcessHelper() {
}

void RtpProcessHelper::attachEvent() {
    _process->setListener(shared_from_this());
}

bool RtpProcessHelper::close(MediaSource &sender, bool force) {
    //此回调在其他线程触发
    if(!_process || (!force && _process->totalReaderCount())){
        return false;
    }
    auto parent = _parent.lock();
    if(!parent){
        return false;
    }
    parent->delProcess(_ssrc,_process.get());
xiongziliang committed
125
    WarnL << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
126 127 128 129 130 131 132 133 134 135 136
    return true;
}

int RtpProcessHelper::totalReaderCount(MediaSource &sender) {
    return _process ? _process->totalReaderCount() : sender.totalReaderCount();
}

RtpProcess::Ptr &RtpProcessHelper::getProcess() {
    return _process;
}

137 138
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)