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

11 12 13 14
#ifndef ZLMEDIAKIT_RTPPROCESS_H
#define ZLMEDIAKIT_RTPPROCESS_H

#if defined(ENABLE_RTPPROXY)
Gemfield committed
15 16

#include "Rtsp/RtpReceiver.h"
xiongziliang committed
17
#include "Decoder.h"
Gemfield committed
18
#include "Common/Device.h"
19
#include "Common/Stamp.h"
20
#include "Http/HttpRequestSplitter.h"
21
#include "Extension/CommonRtp.h"
Gemfield committed
22 23
using namespace mediakit;

24 25
namespace mediakit{

26
class RtpProcess : public HttpRequestSplitter, public RtpReceiver, public SockInfo, public MediaSinkInterface, public std::enable_shared_from_this<RtpProcess>{
Gemfield committed
27 28
public:
    typedef std::shared_ptr<RtpProcess> Ptr;
29
    RtpProcess(const string &stream_id);
Gemfield committed
30
    ~RtpProcess();
xiongziliang committed
31 32 33 34 35 36 37 38 39 40

    /**
     * 输入rtp
     * @param sock 本地监听的socket
     * @param data rtp数据指针
     * @param data_len rtp数据长度
     * @param addr 数据源地址
     * @param dts_out 解析出最新的dts
     * @return 是否解析成功
     */
41
    bool inputRtp(const Socket::Ptr &sock, const char *data,int data_len, const struct sockaddr *addr , uint32_t *dts_out = nullptr);
xiongziliang committed
42 43 44 45

    /**
     * 是否超时,用于超时移除对象
     */
Gemfield committed
46
    bool alive();
47

48 49 50 51 52 53 54 55 56 57
    /**
     * 超时时被RtpSelector移除时触发
     */
    void onDetach();

    /**
     * 设置onDetach事件回调
     */
    void setOnDetach(const function<void()> &cb);

xiongziliang committed
58
    /// SockInfo override
xiongziliang committed
59
    string get_local_ip() override;
60
    uint16_t get_local_port() override;
xiongziliang committed
61
    string get_peer_ip() override;
62 63 64
    uint16_t get_peer_port() override;
    string getIdentifier() const override;

65 66
    int totalReaderCount();
    void setListener(const std::weak_ptr<MediaSourceEvent> &listener);
67

Gemfield committed
68 69
protected:
    void onRtpSorted(const RtpPacket::Ptr &rtp, int track_index) override ;
70 71 72
    void inputFrame(const Frame::Ptr &frame) override;
    void addTrack(const Track::Ptr & track) override;
    void resetTracks() override {};
73

74 75 76
    const char *onSearchPacketTail(const char *data,int len) override;
    int64_t onRecvHeader(const char *data,uint64_t len) override { return 0; };

77 78
private:
    void emitOnPublish();
79
    void onRtpDecode(const uint8_t *packet, int bytes, uint32_t timestamp);
80

Gemfield committed
81
private:
82
    std::shared_ptr<CommonRtpDecoder> _rtp_decoder;
Gemfield committed
83 84 85 86 87 88 89
    std::shared_ptr<FILE> _save_file_rtp;
    std::shared_ptr<FILE> _save_file_ps;
    std::shared_ptr<FILE> _save_file_video;
    struct sockaddr *_addr = nullptr;
    uint16_t _sequence = 0;
    MultiMediaSourceMuxer::Ptr _muxer;
    Ticker _last_rtp_time;
90
    uint32_t _dts = 0;
91
    DecoderImp::Ptr _decoder;
92 93
    std::weak_ptr<MediaSourceEvent> _listener;
    MediaInfo _media_info;
xiongziliang committed
94
    uint64_t _total_bytes = 0;
95
    Socket::Ptr _sock;
96
    function<void()> _on_detach;
Gemfield committed
97 98
};

99 100 101
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)
#endif //ZLMEDIAKIT_RTPPROCESS_H