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

11 12
#ifndef ZLMEDIAKIT_RTPSELECTOR_H
#define ZLMEDIAKIT_RTPSELECTOR_H
Gemfield committed
13

14 15
#if defined(ENABLE_RTPPROXY)
#include <stdint.h>
Gemfield committed
16 17 18
#include <mutex>
#include <unordered_map>
#include "RtpProcess.h"
19
#include "Common/MediaSource.h"
Gemfield committed
20

21
namespace mediakit{
Gemfield committed
22

23 24 25 26
class RtpSelector;
class RtpProcessHelper : public MediaSourceEvent , public std::enable_shared_from_this<RtpProcessHelper> {
public:
    typedef std::shared_ptr<RtpProcessHelper> Ptr;
27
    RtpProcessHelper(const string &stream_id, const weak_ptr<RtpSelector > &parent);
28 29 30
    ~RtpProcessHelper();
    void attachEvent();
    RtpProcess::Ptr & getProcess();
31

32 33 34 35 36
protected:
    // 通知其停止推流
    bool close(MediaSource &sender,bool force) override;
    // 观看总人数
    int totalReaderCount(MediaSource &sender) override;
37

38 39 40
private:
    weak_ptr<RtpSelector > _parent;
    RtpProcess::Ptr _process;
41
    string _stream_id;
42 43
};

Gemfield committed
44 45 46 47 48
class RtpSelector : public std::enable_shared_from_this<RtpSelector>{
public:
    RtpSelector();
    ~RtpSelector();

49
    static bool getSSRC(const char *data,size_t data_len, uint32_t &ssrc);
50 51
    static RtpSelector &Instance();

xiongziliang committed
52
    /**
53 54 55 56 57
     * 清空所有对象
     */
    void clear();

    /**
xiongziliang committed
58 59 60 61 62 63 64 65
     * 输入多个rtp流,根据ssrc分流
     * @param sock 本地socket
     * @param data 收到的数据
     * @param data_len 收到的数据长度
     * @param addr rtp流源地址
     * @param dts_out 解析出最新的dts
     * @return 是否成功
     */
66
    bool inputRtp(const Socket::Ptr &sock, const char *data, size_t data_len,
67 68
                  const struct sockaddr *addr, uint32_t *dts_out = nullptr);

xiongziliang committed
69 70 71 72 73 74
    /**
     * 获取一个rtp处理器
     * @param stream_id 流id
     * @param makeNew 不存在时是否新建
     * @return rtp处理器
     */
75
    RtpProcess::Ptr getProcess(const string &stream_id, bool makeNew);
xiongziliang committed
76 77 78 79 80 81

    /**
     * 删除rtp处理器
     * @param stream_id 流id
     * @param ptr rtp处理器指针
     */
82 83
    void delProcess(const string &stream_id, const RtpProcess *ptr);

Gemfield committed
84 85
private:
    void onManager();
86
    void createTimer();
87

Gemfield committed
88
private:
89
    Timer::Ptr _timer;
90 91
    recursive_mutex _mtx_map;
    unordered_map<string,RtpProcessHelper::Ptr> _map_rtp_process;
Gemfield committed
92 93
};

94 95 96
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)
#endif //ZLMEDIAKIT_RTPSELECTOR_H