RtpMultiCaster.h 1.97 KB
Newer Older
xiongziliang committed
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
xiongziliang committed
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
xiongziliang committed
5
 *
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

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

xiongziliang committed
25
namespace mediakit{
xzl committed
26

27
class MultiCastAddressMaker {
xzl committed
28
public:
29 30 31 32 33 34
    ~MultiCastAddressMaker() {}
    static MultiCastAddressMaker& Instance();
    static bool isMultiCastAddress(uint32_t addr);
    static string toString(uint32_t addr);

    std::shared_ptr<uint32_t> obtain(uint32_t max_try = 10);
35

xzl committed
36
private:
37 38 39 40 41
    MultiCastAddressMaker() {};
    void release(uint32_t addr);

private:
    uint32_t _addr = 0;
42
    recursive_mutex _mtx;
43
    unordered_set<uint32_t> _used_addr;
xzl committed
44
};
45

xiongziliang committed
46
class RtpMultiCaster {
xzl committed
47
public:
48 49
    typedef std::shared_ptr<RtpMultiCaster> Ptr;
    typedef function<void()> onDetach;
50 51 52
    ~RtpMultiCaster();

    static Ptr get(SocketHelper &helper, const string &local_ip, const string &vhost, const string &app, const string &stream);
53
    void setDetachCB(void *listener,const onDetach &cb);
xzl committed
54

55 56
    string getMultiCasterIP();
    uint16_t getMultiCasterPort(TrackType trackType);
xzl committed
57

58 59
private:
    RtpMultiCaster(SocketHelper &helper, const string &local_ip, const string &vhost, const string &app, const string &stream);
xzl committed
60

61 62 63 64 65 66
private:
    recursive_mutex _mtx;
    Socket::Ptr _udp_sock[2];
    std::shared_ptr<uint32_t> _multicast_ip;
    unordered_map<void * , onDetach > _detach_map;
    RtspMediaSource::RingType::RingReader::Ptr _rtp_reader;
xzl committed
67 68
};

xiongziliang committed
69
}//namespace mediakit
xzl committed
70
#endif /* SRC_RTSP_RTPBROADCASTER_H_ */