RtpServer.cpp 2.94 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
 *
 * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
 *
 * 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.
 */

#if defined(ENABLE_RTPPROXY)
#include "RtpServer.h"
#include "RtpSelector.h"
namespace mediakit{

RtpServer::RtpServer() {
}

RtpServer::~RtpServer() {
xiongziliang committed
20 21
    if(_on_clearup){
        _on_clearup();
22 23 24
    }
}

25
void RtpServer::start(uint16_t local_port, const string &stream_id,  bool enable_tcp, const char *local_ip) {
26
    //创建udp服务器
xiongziliang committed
27 28 29
    Socket::Ptr udp_server = std::make_shared<Socket>(nullptr, false);
    if (!udp_server->bindUdpSock(local_port, local_ip)) {
        throw std::runtime_error(StrPrinter << "bindUdpSock on " << local_ip << ":" << local_port << " failed:" << get_uv_errmsg(true));
30 31
    }
    //设置udp socket读缓存
xiongziliang committed
32
    SockUtil::setRecvBuf(udp_server->rawFD(), 4 * 1024 * 1024);
33

xiongziliang committed
34
    TcpServer::Ptr tcp_server;
35 36 37
    if (enable_tcp) {
        try {
            //创建tcp服务器
xiongziliang committed
38 39 40
            tcp_server = std::make_shared<TcpServer>(udp_server->getPoller());
            (*tcp_server)[RtpSession::kStreamID] = stream_id;
            tcp_server->start<RtpSession>(udp_server->get_local_port(), local_ip);
41 42 43 44
        } catch (...) {
            throw;
        }
    }
xiongziliang committed
45 46 47 48 49

    RtpProcess::Ptr process;
    if (!stream_id.empty()) {
        //指定了流id,那么一个端口一个流(不管是否包含多个ssrc的多个流,绑定rtp源后,会筛选掉ip端口不匹配的流)
        process = RtpSelector::Instance().getProcess(stream_id, true);
xiongziliang committed
50 51
        udp_server->setOnRead([udp_server, process](const Buffer::Ptr &buf, struct sockaddr *addr, int) {
            process->inputRtp(udp_server, buf->data(), buf->size(), addr);
xiongziliang committed
52 53 54 55
        });
    } else {
        //未指定流id,一个端口多个流,通过ssrc来分流
        auto &ref = RtpSelector::Instance();
xiongziliang committed
56 57
        udp_server->setOnRead([&ref, udp_server](const Buffer::Ptr &buf, struct sockaddr *addr, int) {
            ref.inputRtp(udp_server, buf->data(), buf->size(), addr);
xiongziliang committed
58 59 60
        });
    }

xiongziliang committed
61
    _on_clearup = [udp_server, process, stream_id]() {
xiongziliang committed
62
        //去除循环引用
xiongziliang committed
63
        udp_server->setOnRead(nullptr);
xiongziliang committed
64 65 66 67 68
        if (process) {
            //删除rtp处理器
            RtpSelector::Instance().delProcess(stream_id, process.get());
        }
    };
xiongziliang committed
69 70 71

    _tcp_server = tcp_server;
    _udp_server = udp_server;
72 73 74 75 76 77 78
    _rtp_process = process;
}

void RtpServer::setOnDetach(const function<void()> &cb){
    if(_rtp_process){
        _rtp_process->setOnDetach(cb);
    }
79 80 81 82 83 84 85 86 87 88 89 90
}

EventPoller::Ptr RtpServer::getPoller() {
    return _udp_server->getPoller();
}

uint16_t RtpServer::getPort() {
    return _udp_server ? _udp_server->get_local_port() : 0;
}

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