RtmpProtocol.h 4.11 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 13
 */

#ifndef SRC_RTMP_RTMPPROTOCOL_H_
#define SRC_RTMP_RTMPPROTOCOL_H_

xiongzilaing committed
14
#include <memory>
xzl committed
15 16 17
#include <string>
#include <functional>
#include "amf.h"
xiongzilaing committed
18 19 20 21 22
#include "Rtmp.h"
#include "Util/util.h"
#include "Util/logger.h"
#include "Util/TimeTicker.h"
#include "Network/Socket.h"
771730766@qq.com committed
23
#include "Util/ResourcePool.h"
24
#include "Http/HttpRequestSplitter.h"
xzl committed
25 26

using namespace std;
xiongziliang committed
27
using namespace toolkit;
xzl committed
28

xiongziliang committed
29
namespace mediakit {
xzl committed
30

31
class RtmpProtocol : public HttpRequestSplitter{
xzl committed
32
public:
33 34
    RtmpProtocol();
    virtual ~RtmpProtocol();
xiongziliang committed
35

36
    void onParseRtmp(const char *data, size_t size);
37 38
    //作为客户端发送c0c1,等待s0s1s2并且回调
    void startClientSession(const function<void()> &cb);
xiongziliang committed
39

xzl committed
40
protected:
41
    virtual void onSendRawData(Buffer::Ptr buffer) = 0;
xia-chu committed
42
    virtual void onRtmpChunk(RtmpPacket::Ptr chunk_data) = 0;
xiongziliang committed
43 44
    virtual void onStreamBegin(uint32_t stream_index){
        _stream_index = stream_index;
45
    }
xiongziliang committed
46 47
    virtual void onStreamEof(uint32_t stream_index){};
    virtual void onStreamDry(uint32_t stream_index){};
xzl committed
48 49

protected:
50
    //// HttpRequestSplitter override ////
51
    ssize_t onRecvHeader(const char *data, size_t len) override { return 0; }
52
    const char *onSearchPacketTail(const char *data, size_t len) override;
53 54

protected:
xiongziliang committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
    void reset();
    void sendAcknowledgement(uint32_t size);
    void sendAcknowledgementSize(uint32_t size);
    void sendPeerBandwidth(uint32_t size);
    void sendChunkSize(uint32_t size);
    void sendPingRequest(uint32_t ti = ::time(NULL));
    void sendPingResponse(uint32_t time_stamp = ::time(NULL));
    void sendSetBufferLength(uint32_t stream_index, uint32_t len);
    void sendUserControl(uint16_t event_type, uint32_t event_data);
    void sendUserControl(uint16_t event_type, const string &event_data);
    void sendInvoke(const string &cmd, const AMFValue &val);
    void sendRequest(int cmd, const string &str);
    void sendResponse(int type, const string &str);
    void sendRtmp(uint8_t type, uint32_t stream_index, const std::string &buffer, uint32_t stamp, int chunk_id);
    void sendRtmp(uint8_t type, uint32_t stream_index, const Buffer::Ptr &buffer, uint32_t stamp, int chunk_id);
70
    BufferRaw::Ptr obtainBuffer(const void *data = nullptr, size_t len = 0);
xiongziliang committed
71

xzl committed
72
private:
73
    void handle_C1_simple(const char *data);
74
#ifdef ENABLE_OPENSSL
75
    void handle_C1_complex(const char *data);
76 77 78 79
    string get_C1_digest(const uint8_t *ptr,char **digestPos);
    string get_C1_key(const uint8_t *ptr);
    void check_C1_Digest(const string &digest,const string &data);
    void send_complex_S0S1S2(int schemeType,const string &digest);
80
#endif //ENABLE_OPENSSL
81

82 83 84 85
    const char* handle_S0S1S2(const char *data, size_t len, const function<void()> &func);
    const char* handle_C0C1(const char *data, size_t len);
    const char* handle_C2(const char *data, size_t len);
    const char* handle_rtmp(const char *data, size_t len);
xia-chu committed
86
    void handle_chunk(RtmpPacket::Ptr chunk_data);
xiongziliang committed
87 88 89 90

protected:
    int _send_req_id = 0;
    uint32_t _stream_index = STREAM_CONTROL;
xzl committed
91

92
private:
xiongziliang committed
93 94 95
    int _now_stream_index = 0;
    int _now_chunk_id = 0;
    bool _data_started = false;
96
    ////////////ChunkSize////////////
xiongziliang committed
97 98
    size_t _chunk_size_in = DEFAULT_CHUNK_LEN;
    size_t _chunk_size_out = DEFAULT_CHUNK_LEN;
99
    ////////////Acknowledgement////////////
xiongziliang committed
100 101 102
    uint32_t _bytes_sent = 0;
    uint32_t _bytes_sent_last = 0;
    uint32_t _windows_size = 0;
103
    ///////////PeerBandwidth///////////
xiongziliang committed
104 105
    uint32_t _bandwidth = 2500000;
    uint8_t _band_limit_type = 2;
106
    //////////Rtmp parser//////////
107
    function<const char * (const char *data, size_t len)> _next_step_func;
xiongziliang committed
108
    ////////////Chunk////////////
xia-chu committed
109
    unordered_map<int, std::pair<RtmpPacket::Ptr/*now*/, RtmpPacket::Ptr/*last*/> > _map_chunk_data;
xzl committed
110 111
};

xiongziliang committed
112
} /* namespace mediakit */
xzl committed
113
#endif /* SRC_RTMP_RTMPPROTOCOL_H_ */