2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2017-09-27 16:20:30 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#ifndef SRC_RTMP_RTMPPROTOCOL_H_
|
|
|
|
|
#define SRC_RTMP_RTMPPROTOCOL_H_
|
|
|
|
|
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <memory>
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include <string>
|
|
|
|
|
#include <functional>
|
|
|
|
|
#include "amf.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Rtmp.h"
|
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/logger.h"
|
|
|
|
|
#include "Util/TimeTicker.h"
|
|
|
|
|
#include "Network/Socket.h"
|
2018-01-30 11:23:57 +08:00
|
|
|
|
#include "Util/ResourcePool.h"
|
2020-11-01 03:41:35 +08:00
|
|
|
|
#include "Http/HttpRequestSplitter.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
using namespace std;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-11-01 03:41:35 +08:00
|
|
|
|
class RtmpProtocol : public HttpRequestSplitter{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
2020-03-20 11:51:24 +08:00
|
|
|
|
RtmpProtocol();
|
|
|
|
|
virtual ~RtmpProtocol();
|
2020-08-30 10:48:34 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
void onParseRtmp(const char *data, size_t size);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//作为客户端发送c0c1,等待s0s1s2并且回调
|
|
|
|
|
void startClientSession(const function<void()> &cb);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2020-11-01 03:41:35 +08:00
|
|
|
|
virtual void onSendRawData(Buffer::Ptr buffer) = 0;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
virtual void onRtmpChunk(RtmpPacket &chunk_data) = 0;
|
|
|
|
|
virtual void onStreamBegin(uint32_t stream_index){
|
|
|
|
|
_stream_index = stream_index;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
}
|
2020-08-30 10:48:34 +08:00
|
|
|
|
virtual void onStreamEof(uint32_t stream_index){};
|
|
|
|
|
virtual void onStreamDry(uint32_t stream_index){};
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2020-11-01 03:41:35 +08:00
|
|
|
|
protected:
|
|
|
|
|
//// HttpRequestSplitter override ////
|
2021-01-17 18:31:50 +08:00
|
|
|
|
size_t onRecvHeader(const char *data, size_t len) override { return 0; }
|
|
|
|
|
const char *onSearchPacketTail(const char *data, size_t len) override;
|
2020-11-01 03:41:35 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
protected:
|
2020-08-30 10:48:34 +08:00
|
|
|
|
void reset();
|
|
|
|
|
BufferRaw::Ptr obtainBuffer();
|
2021-01-17 18:31:50 +08:00
|
|
|
|
BufferRaw::Ptr obtainBuffer(const void *data, size_t len);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
private:
|
2020-11-01 03:41:35 +08:00
|
|
|
|
void handle_C1_simple(const char *data);
|
2017-05-16 11:51:18 +08:00
|
|
|
|
#ifdef ENABLE_OPENSSL
|
2020-11-01 03:41:35 +08:00
|
|
|
|
void handle_C1_complex(const char *data);
|
2020-03-20 11:51:24 +08:00
|
|
|
|
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);
|
2017-05-16 11:51:18 +08:00
|
|
|
|
#endif //ENABLE_OPENSSL
|
2017-05-16 11:45:36 +08:00
|
|
|
|
|
2021-01-17 18:31:50 +08:00
|
|
|
|
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);
|
2020-11-01 03:41:35 +08:00
|
|
|
|
void handle_chunk(RtmpPacket &chunk_data);
|
2020-08-30 10:48:34 +08:00
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
int _send_req_id = 0;
|
|
|
|
|
uint32_t _stream_index = STREAM_CONTROL;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
private:
|
2020-08-30 10:48:34 +08:00
|
|
|
|
int _now_stream_index = 0;
|
|
|
|
|
int _now_chunk_id = 0;
|
|
|
|
|
bool _data_started = false;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
////////////ChunkSize////////////
|
2020-08-30 10:48:34 +08:00
|
|
|
|
size_t _chunk_size_in = DEFAULT_CHUNK_LEN;
|
|
|
|
|
size_t _chunk_size_out = DEFAULT_CHUNK_LEN;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
////////////Acknowledgement////////////
|
2020-08-30 10:48:34 +08:00
|
|
|
|
uint32_t _bytes_sent = 0;
|
|
|
|
|
uint32_t _bytes_sent_last = 0;
|
|
|
|
|
uint32_t _windows_size = 0;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
///////////PeerBandwidth///////////
|
2020-08-30 10:48:34 +08:00
|
|
|
|
uint32_t _bandwidth = 2500000;
|
|
|
|
|
uint8_t _band_limit_type = 2;
|
2020-03-20 11:51:24 +08:00
|
|
|
|
//////////Rtmp parser//////////
|
2021-01-17 18:31:50 +08:00
|
|
|
|
function<const char * (const char *data, size_t len)> _next_step_func;
|
2020-08-30 10:48:34 +08:00
|
|
|
|
////////////Chunk////////////
|
|
|
|
|
unordered_map<int, RtmpPacket> _map_chunk_data;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
};
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#endif /* SRC_RTMP_RTMPPROTOCOL_H_ */
|