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

xiongziliang committed
11 12
#ifndef ZLMEDIAKIT_RTPSENDER_H
#define ZLMEDIAKIT_RTPSENDER_H
13 14 15 16 17 18
#if defined(ENABLE_RTPPROXY)
#include "PSEncoder.h"
#include "Extension/CommonRtp.h"

namespace mediakit{

xiongziliang committed
19 20
//rtp发送客户端,支持发送GB28181协议
class RtpSender : public MediaSinkInterface, public std::enable_shared_from_this<RtpSender>{
21
public:
xiongziliang committed
22
    typedef std::shared_ptr<RtpSender> Ptr;
23

xiongziliang committed
24
    ~RtpSender() override;
25 26

    /**
xiongziliang committed
27
     * 构造函数,创建GB28181 RTP发送客户端
28 29 30
     * @param ssrc rtp的ssrc
     * @param payload_type 国标中ps-rtp的pt一般为96
     */
xiongziliang committed
31
    RtpSender(uint32_t ssrc, uint8_t payload_type = 96);
32 33 34 35 36 37 38 39

    /**
     * 开始发送ps-rtp包
     * @param dst_url 目标ip或域名
     * @param dst_port 目标端口
     * @param is_udp 是否采用udp方式发送rtp
     * @param cb 连接目标端口是否成功的回调
     */
40
    void startSend(const string &dst_url, uint16_t dst_port, bool is_udp, uint16_t src_port, const function<void(uint16_t local_port, const SockException &ex)> &cb);
41

42 43 44 45 46
    /**
     * 输入帧数据
     */
    void inputFrame(const Frame::Ptr &frame) override;

xiongziliang committed
47 48 49 50 51 52 53 54 55 56 57
    /**
     * 添加track,内部会调用Track的clone方法
     * 只会克隆sps pps这些信息 ,而不会克隆Delegate相关关系
     * @param track
     */
    virtual void addTrack(const Track::Ptr & track) override;

    /**
     * 添加所有Track完毕
     */
    virtual void addTrackCompleted() override;
58 59

    /**
xiongziliang committed
60
     * 重置track
61
     */
xiongziliang committed
62
    virtual void resetTracks() override;
63 64

private:
xiongziliang committed
65 66
    //合并写输出
    void onFlushRtpList(std::shared_ptr<List<Buffer::Ptr> > rtp_list);
67 68 69 70 71 72 73 74 75 76
    //udp/tcp连接成功回调
    void onConnect();
    //异常断开socket事件
    void onErr(const SockException &ex, bool is_connect = false);

private:
    bool _is_udp;
    bool _is_connect = false;
    string _dst_url;
    uint16_t _dst_port;
77
	uint16_t _src_port;
78 79 80
    Socket::Ptr _socket;
    EventPoller::Ptr _poller;
    Timer::Ptr _connect_timer;
xiongziliang committed
81
    MediaSinkInterface::Ptr _interface;
82 83 84 85
};

}//namespace mediakit
#endif// defined(ENABLE_RTPPROXY)
xiongziliang committed
86
#endif //ZLMEDIAKIT_RTPSENDER_H