RtpSelector.cpp 4.63 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)
xiongziliang committed
12
#include <stddef.h>
Gemfield committed
13
#include "RtpSelector.h"
xiongziliang committed
14
#include "RtpSplitter.h"
Gemfield committed
15

16 17
namespace mediakit{

Gemfield committed
18 19
INSTANCE_IMP(RtpSelector);

20 21 22 23 24
void RtpSelector::clear(){
    lock_guard<decltype(_mtx_map)> lck(_mtx_map);
    _map_rtp_process.clear();
}

xiongziliang committed
25
bool RtpSelector::inputRtp(const Socket::Ptr &sock, const char *data, int data_len,
26
                           const struct sockaddr *addr,uint32_t *dts_out) {
xiongziliang committed
27 28 29 30
    uint32_t ssrc = 0;
    if (!getSSRC(data, data_len, ssrc)) {
        WarnL << "get ssrc from rtp failed:" << data_len;
        return false;
Gemfield committed
31
    }
xiongziliang committed
32
    auto process = getProcess(printSSRC(ssrc), true);
33
    if (process) {
xiongziliang committed
34 35 36 37 38 39
        try {
            return process->inputRtp(true, sock, data, data_len, addr, dts_out);
        } catch (...) {
            delProcess(printSSRC(ssrc), process.get());
            throw;
        }
Gemfield committed
40 41 42 43
    }
    return false;
}

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

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

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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());
    }
}

83
void RtpSelector::delProcess(const string &stream_id,const RtpProcess *ptr) {
xiongziliang committed
84 85 86 87 88 89 90 91 92 93 94 95
    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
96
    }
97
    process->onDetach();
Gemfield committed
98 99 100
}

void RtpSelector::onManager() {
xiongziliang committed
101 102 103 104 105 106 107 108 109 110 111
    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
112 113
        }
    }
xiongziliang committed
114 115 116 117

    clear_list.for_each([](const RtpProcess::Ptr &process) {
        process->onDetach();
    });
Gemfield committed
118 119 120 121 122 123 124
}

RtpSelector::RtpSelector() {
}

RtpSelector::~RtpSelector() {
}
125

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

RtpProcessHelper::~RtpProcessHelper() {
}

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

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

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

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

161 162
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)