RtpMultiCaster.h 2.6 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
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.
xzl committed
9 10 11 12
 */

#ifndef SRC_RTSP_RTPBROADCASTER_H_
#define SRC_RTSP_RTPBROADCASTER_H_
xiongzilaing committed
13

14

xzl committed
15
#include <mutex>
xiongzilaing committed
16
#include <memory>
xzl committed
17
#include <unordered_set>
xiongzilaing committed
18
#include <unordered_map>
xiongziliang committed
19
#include "Common/config.h"
xiongzilaing committed
20 21 22
#include "RtspMediaSource.h"
#include "Util/mini.h"
#include "Network/Socket.h"
xzl committed
23 24

using namespace std;
xiongziliang committed
25
using namespace toolkit;
xzl committed
26

xiongziliang committed
27
namespace mediakit{
xzl committed
28 29 30 31

class MultiCastAddressMaker
{
public:
32
    static MultiCastAddressMaker &Instance();
33

34 35 36 37 38 39 40
    static bool isMultiCastAddress(uint32_t iAddr){
        static uint32_t addrMin = mINI::Instance()[MultiCast::kAddrMin].as<uint32_t>();
        static uint32_t addrMax = mINI::Instance()[MultiCast::kAddrMax].as<uint32_t>();
        return iAddr >= addrMin && iAddr <= addrMax;
    }
    static string toString(uint32_t iAddr){
        iAddr = htonl(iAddr);
41
        return SockUtil::inet_ntoa((struct in_addr &)(iAddr));
42 43 44
    }
    virtual ~MultiCastAddressMaker(){}
    std::shared_ptr<uint32_t> obtain(uint32_t iTry = 10);
xzl committed
45
private:
46 47 48 49 50
    MultiCastAddressMaker(){};
    void release(uint32_t iAddr);
    uint32_t _iAddr = 0;
    recursive_mutex _mtx;
    unordered_set<uint32_t> _setBadAddr;
xzl committed
51
};
xiongziliang committed
52
class RtpMultiCaster {
xzl committed
53
public:
54 55 56 57 58 59 60
    typedef std::shared_ptr<RtpMultiCaster> Ptr;
    typedef function<void()> onDetach;
    virtual ~RtpMultiCaster();
    static Ptr get(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
    void setDetachCB(void *listener,const onDetach &cb);
    uint16_t getPort(TrackType trackType);
    string getIP();
xzl committed
61
private:
62 63 64
    static recursive_mutex g_mtx;
    static unordered_map<string , weak_ptr<RtpMultiCaster> > g_mapBroadCaster;
    static Ptr make(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
xzl committed
65

66 67 68 69 70 71
    std::shared_ptr<uint32_t> _multiAddr;
    recursive_mutex _mtx;
    unordered_map<void * , onDetach > _mapDetach;
    RtspMediaSource::RingType::RingReader::Ptr _pReader;
    Socket::Ptr _apUdpSock[2];
    struct sockaddr_in _aPeerUdpAddr[2];
xzl committed
72

73
    RtpMultiCaster(const EventPoller::Ptr &poller,const string &strLocalIp,const string &strVhost,const string &strApp,const string &strStream);
xzl committed
74 75 76

};

xiongziliang committed
77
}//namespace mediakit
xzl committed
78 79

#endif /* SRC_RTSP_RTPBROADCASTER_H_ */