RtpSelector.cpp 4.93 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 19 20 21 22
void RtpSelector::clear(){
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
    _map_rtp_process.clear();
}

xiongziliang committed
23
bool RtpSelector::inputRtp(const Socket::Ptr &sock, const char *data, int data_len,
24
                           const struct sockaddr *addr,uint32_t *dts_out) {
xiongziliang committed
25 26 27 28 29
    //使用ssrc为流id
    uint32_t ssrc = 0;
    if (!getSSRC(data, data_len, ssrc)) {
        WarnL << "get ssrc from rtp failed:" << data_len;
        return false;
Gemfield committed
30
    }
31 32

    //假定指定了流id,那么通过流id来区分是否为一路流(哪怕可能同时收到多路流)
xiongziliang committed
33
    auto process = getProcess(printSSRC(ssrc), true);
34 35
    if (process) {
        return process->inputRtp(sock, data, data_len, addr, dts_out);
Gemfield committed
36 37 38 39
    }
    return false;
}

40
bool RtpSelector::getSSRC(const char *data,int data_len, uint32_t &ssrc){
41
    if (data_len < 12) {
42
        return false;
Gemfield committed
43
    }
44
    uint32_t *ssrc_ptr = (uint32_t *) (data + 8);
45 46
    ssrc = ntohl(*ssrc_ptr);
    return true;
Gemfield committed
47 48
}

49
RtpProcess::Ptr RtpSelector::getProcess(const string &stream_id,bool makeNew) {
Gemfield committed
50
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
51 52
    auto it = _map_rtp_process.find(stream_id);
    if (it == _map_rtp_process.end() && !makeNew) {
Gemfield committed
53 54
        return nullptr;
    }
55 56 57
    RtpProcessHelper::Ptr &ref = _map_rtp_process[stream_id];
    if (!ref) {
        ref = std::make_shared<RtpProcessHelper>(stream_id, shared_from_this());
58
        ref->attachEvent();
59
        createTimer();
Gemfield committed
60
    }
61
    return ref->getProcess();
Gemfield committed
62 63
}

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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());
    }
}

79
void RtpSelector::delProcess(const string &stream_id,const RtpProcess *ptr) {
xiongziliang committed
80 81 82 83 84 85 86 87 88 89 90 91
    RtpProcess::Ptr process;
    {
        lock_guard<decltype(_mtx_map)> lck(_mtx_map);
        auto it = _map_rtp_process.find(stream_id);
        if (it == _map_rtp_process.end()) {
            return;
        }
        if (it->second->getProcess().get() != ptr) {
            return;
        }
        process = it->second->getProcess();
        _map_rtp_process.erase(it);
Gemfield committed
92
    }
93
    process->onDetach();
Gemfield committed
94 95 96
}

void RtpSelector::onManager() {
xiongziliang committed
97 98 99 100 101 102 103 104 105 106 107
    List<RtpProcess::Ptr> clear_list;
    {
        lock_guard<decltype(_mtx_map)> lck(_mtx_map);
        for (auto it = _map_rtp_process.begin(); it != _map_rtp_process.end();) {
            if (it->second->getProcess()->alive()) {
                ++it;
                continue;
            }
            WarnL << "RtpProcess timeout:" << it->first;
            clear_list.emplace_back(it->second->getProcess());
            it = _map_rtp_process.erase(it);
Gemfield committed
108 109
        }
    }
xiongziliang committed
110 111 112 113

    clear_list.for_each([](const RtpProcess::Ptr &process) {
        process->onDetach();
    });
Gemfield committed
114 115 116 117 118 119 120
}

RtpSelector::RtpSelector() {
}

RtpSelector::~RtpSelector() {
}
121

122 123
RtpProcessHelper::RtpProcessHelper(const string &stream_id, const weak_ptr<RtpSelector> &parent) {
    _stream_id = stream_id;
124
    _parent = parent;
125
    _process = std::make_shared<RtpProcess>(stream_id);
126 127 128 129 130 131 132 133 134 135 136
}

RtpProcessHelper::~RtpProcessHelper() {
}

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

bool RtpProcessHelper::close(MediaSource &sender, bool force) {
    //此回调在其他线程触发
137
    if (!_process || (!force && _process->totalReaderCount())) {
138 139 140
        return false;
    }
    auto parent = _parent.lock();
141
    if (!parent) {
142 143
        return false;
    }
144
    parent->delProcess(_stream_id, _process.get());
xiongziliang committed
145
    WarnL << "close media:" << sender.getSchema() << "/" << sender.getVhost() << "/" << sender.getApp() << "/" << sender.getId() << " " << force;
146 147 148 149 150 151 152
    return true;
}

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

153 154 155 156 157 158 159 160 161 162 163 164
MediaOriginType RtpProcessHelper::getOriginType(MediaSource &sender) const{
    return MediaOriginType::rtp_push;
}

string RtpProcessHelper::getOriginUrl(MediaSource &sender) const {
    return _process ? _process->_media_info._full_url : "";
}

std::shared_ptr<SockInfo> RtpProcessHelper::getOriginSock(MediaSource &sender) const{
    return _process;
}

165 166 167 168
RtpProcess::Ptr &RtpProcessHelper::getProcess() {
    return _process;
}

169 170
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)