2017-10-09 22:11:01 +08:00
|
|
|
|
/*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* MIT License
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*
|
2017-09-27 16:20:30 +08:00
|
|
|
|
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
|
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
2017-04-01 16:35:56 +08:00
|
|
|
|
*/
|
|
|
|
|
#include "RtmpProtocol.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Rtsp/Rtsp.h"
|
|
|
|
|
#include "Rtmp/utils.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Util/util.h"
|
|
|
|
|
#include "Util/onceToken.h"
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Thread/ThreadPool.h"
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2017-05-16 11:45:36 +08:00
|
|
|
|
#ifdef ENABLE_OPENSSL
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#include "Util/SSLBox.h"
|
2017-05-16 11:45:36 +08:00
|
|
|
|
#include <openssl/hmac.h>
|
2018-03-02 13:40:17 +08:00
|
|
|
|
#include <openssl/opensslv.h>
|
|
|
|
|
|
2017-05-16 11:45:36 +08:00
|
|
|
|
static string openssl_HMACsha256(const void *key,unsigned int key_len,
|
|
|
|
|
const void *data,unsigned int data_len){
|
2017-05-27 17:36:51 +08:00
|
|
|
|
std::shared_ptr<char> out(new char[32],[](char *ptr){delete [] ptr;});
|
2017-05-16 11:45:36 +08:00
|
|
|
|
unsigned int out_len;
|
2017-08-09 18:39:30 +08:00
|
|
|
|
|
2018-03-02 13:40:17 +08:00
|
|
|
|
#if defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10100000L)
|
|
|
|
|
//openssl 1.1.0新增api,老版本api作废
|
2017-08-09 18:39:30 +08:00
|
|
|
|
HMAC_CTX *ctx = HMAC_CTX_new();
|
|
|
|
|
HMAC_CTX_reset(ctx);
|
|
|
|
|
HMAC_Init_ex(ctx, key, key_len, EVP_sha256(), NULL);
|
|
|
|
|
HMAC_Update(ctx, (unsigned char*)data, data_len);
|
|
|
|
|
HMAC_Final(ctx, (unsigned char *)out.get(), &out_len);
|
|
|
|
|
HMAC_CTX_reset(ctx);
|
|
|
|
|
HMAC_CTX_free(ctx);
|
|
|
|
|
#else
|
2017-05-16 11:45:36 +08:00
|
|
|
|
HMAC_CTX ctx;
|
|
|
|
|
HMAC_CTX_init(&ctx);
|
|
|
|
|
HMAC_Init_ex(&ctx, key, key_len, EVP_sha256(), NULL);
|
|
|
|
|
HMAC_Update(&ctx, (unsigned char*)data, data_len);
|
2017-05-27 17:36:51 +08:00
|
|
|
|
HMAC_Final(&ctx, (unsigned char *)out.get(), &out_len);
|
2017-05-16 11:45:36 +08:00
|
|
|
|
HMAC_CTX_cleanup(&ctx);
|
2018-03-02 13:40:57 +08:00
|
|
|
|
#endif //defined(OPENSSL_VERSION_NUMBER) && (OPENSSL_VERSION_NUMBER > 0x10100000L)
|
2017-05-27 17:36:51 +08:00
|
|
|
|
return string(out.get(),out_len);
|
2017-05-16 11:45:36 +08:00
|
|
|
|
}
|
|
|
|
|
#endif //ENABLE_OPENSSL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define C1_DIGEST_SIZE 32
|
|
|
|
|
#define C1_KEY_SIZE 128
|
|
|
|
|
#define C1_SCHEMA_SIZE 764
|
|
|
|
|
#define C1_HANDSHARK_SIZE (RANDOM_LEN + 8)
|
|
|
|
|
#define C1_FPKEY_SIZE 30
|
|
|
|
|
#define S1_FMS_KEY_SIZE 36
|
|
|
|
|
#define S2_FMS_KEY_SIZE 68
|
|
|
|
|
#define C1_OFFSET_SIZE 4
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
namespace mediakit {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
RtmpProtocol::RtmpProtocol() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this](){
|
2017-04-01 16:35:56 +08:00
|
|
|
|
handle_C0C1();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
RtmpProtocol::~RtmpProtocol() {
|
2017-08-03 13:55:46 +08:00
|
|
|
|
reset();
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-08-03 13:55:46 +08:00
|
|
|
|
void RtmpProtocol::reset() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
////////////ChunkSize////////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iChunkLenIn = DEFAULT_CHUNK_LEN;
|
|
|
|
|
_iChunkLenOut = DEFAULT_CHUNK_LEN;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
////////////Acknowledgement////////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui32ByteSent = 0;
|
|
|
|
|
_ui32LastSent = 0;
|
|
|
|
|
_ui32WinSize = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
///////////PeerBandwidth///////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui32Bandwidth = 2500000;
|
|
|
|
|
_ui8LimitType = 2;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
////////////Chunk////////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_mapChunkData.clear();
|
|
|
|
|
_iNowStreamID = 0;
|
|
|
|
|
_iNowChunkID = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//////////Invoke Request//////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iReqID = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//////////Rtmp parser//////////
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strRcvBuf.clear();
|
|
|
|
|
_ui32StreamId = STREAM_CONTROL;
|
|
|
|
|
_nextHandle = [this]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
handle_C0C1();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendAcknowledgement(uint32_t ui32Size) {
|
|
|
|
|
std::string control;
|
|
|
|
|
uint32_t stream = htonl(ui32Size);
|
|
|
|
|
control.append((char *) &stream, 4);
|
|
|
|
|
sendRequest(MSG_ACK, control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendAcknowledgementSize(uint32_t ui32Size) {
|
|
|
|
|
uint32_t windowSize = htonl(ui32Size);
|
|
|
|
|
std::string set_windowSize((char *) &windowSize, 4);
|
|
|
|
|
sendRequest(MSG_WIN_SIZE, set_windowSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendPeerBandwidth(uint32_t ui32Size) {
|
|
|
|
|
uint32_t peerBandwidth = htonl(ui32Size);
|
|
|
|
|
std::string set_peerBandwidth((char *) &peerBandwidth, 4);
|
|
|
|
|
set_peerBandwidth.push_back((char) 0x02);
|
|
|
|
|
sendRequest(MSG_SET_PEER_BW, set_peerBandwidth);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendChunkSize(uint32_t ui32Size) {
|
|
|
|
|
uint32_t len = htonl(ui32Size);
|
|
|
|
|
std::string set_chunk((char *) &len, 4);
|
|
|
|
|
sendRequest(MSG_SET_CHUNK, set_chunk);
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iChunkLenOut = ui32Size;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendPingRequest(uint32_t ui32TimeStamp) {
|
|
|
|
|
sendUserControl(CONTROL_PING_REQUEST, ui32TimeStamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendPingResponse(uint32_t ui32TimeStamp) {
|
|
|
|
|
sendUserControl(CONTROL_PING_RESPONSE, ui32TimeStamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendSetBufferLength(uint32_t ui32StreamId,
|
|
|
|
|
uint32_t ui32Length) {
|
|
|
|
|
std::string control;
|
|
|
|
|
ui32StreamId = htonl(ui32StreamId);
|
|
|
|
|
control.append((char *) &ui32StreamId, 4);
|
|
|
|
|
ui32Length = htonl(ui32Length);
|
|
|
|
|
control.append((char *) &ui32Length, 4);
|
|
|
|
|
sendUserControl(CONTROL_SETBUFFER, control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendUserControl(uint16_t ui16EventType,
|
|
|
|
|
uint32_t ui32EventData) {
|
|
|
|
|
std::string control;
|
|
|
|
|
uint16_t type = htons(ui16EventType);
|
|
|
|
|
control.append((char *) &type, 2);
|
|
|
|
|
uint32_t stream = htonl(ui32EventData);
|
|
|
|
|
control.append((char *) &stream, 4);
|
|
|
|
|
sendRequest(MSG_USER_CONTROL, control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendUserControl(uint16_t ui16EventType,
|
|
|
|
|
const string& strEventData) {
|
|
|
|
|
std::string control;
|
|
|
|
|
uint16_t type = htons(ui16EventType);
|
|
|
|
|
control.append((char *) &type, 2);
|
|
|
|
|
control.append(strEventData);
|
|
|
|
|
sendRequest(MSG_USER_CONTROL, control);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendResponse(int iType, const string& str) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if(!_bDataStarted && (iType == MSG_DATA)){
|
|
|
|
|
_bDataStarted = true;
|
2017-05-13 17:25:31 +08:00
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
sendRtmp(iType, _iNowStreamID, str, 0, _bDataStarted ? CHUNK_CLIENT_REQUEST_AFTER : CHUNK_CLIENT_REQUEST_BEFORE);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendInvoke(const string& strCmd, const AMFValue& val) {
|
|
|
|
|
AMFEncoder enc;
|
2018-10-24 15:43:52 +08:00
|
|
|
|
enc << strCmd << ++_iReqID << val;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
sendRequest(MSG_CMD, enc.data());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::sendRequest(int iCmd, const string& str) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
sendRtmp(iCmd, _ui32StreamId, str, 0, CHUNK_SERVER_REQUEST);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 14:37:03 +08:00
|
|
|
|
class BufferPartial : public Buffer {
|
|
|
|
|
public:
|
|
|
|
|
BufferPartial(const Buffer::Ptr &buffer,uint32_t offset,uint32_t size){
|
|
|
|
|
_buffer = buffer;
|
|
|
|
|
_data = buffer->data() + offset;
|
|
|
|
|
_size = size;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
~BufferPartial(){}
|
|
|
|
|
|
|
|
|
|
char *data() const override {
|
|
|
|
|
return _data;
|
|
|
|
|
}
|
|
|
|
|
uint32_t size() const override{
|
|
|
|
|
return _size;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
Buffer::Ptr _buffer;
|
|
|
|
|
char *_data;
|
|
|
|
|
uint32_t _size;
|
|
|
|
|
};
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId,
|
2019-03-22 14:37:03 +08:00
|
|
|
|
const std::string& strBuf, uint32_t ui32TimeStamp, int iChunkId) {
|
|
|
|
|
sendRtmp(ui8Type,ui32StreamId,std::make_shared<BufferString>(strBuf),ui32TimeStamp,iChunkId);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-03-22 14:37:03 +08:00
|
|
|
|
void RtmpProtocol::sendRtmp(uint8_t ui8Type, uint32_t ui32StreamId,
|
|
|
|
|
const Buffer::Ptr &buf, uint32_t ui32TimeStamp, int iChunkId){
|
|
|
|
|
if (iChunkId < 2 || iChunkId > 63) {
|
|
|
|
|
auto strErr = StrPrinter << "不支持发送该类型的块流 ID:" << iChunkId << endl;
|
|
|
|
|
throw std::runtime_error(strErr);
|
|
|
|
|
}
|
2019-03-23 22:00:16 +08:00
|
|
|
|
//是否有扩展时间戳
|
2019-03-22 14:37:03 +08:00
|
|
|
|
bool bExtStamp = ui32TimeStamp >= 0xFFFFFF;
|
2018-01-30 11:23:57 +08:00
|
|
|
|
|
2019-03-23 22:00:16 +08:00
|
|
|
|
//rtmp头
|
|
|
|
|
BufferRaw::Ptr bufferHeader = obtainBuffer();
|
|
|
|
|
bufferHeader->setCapacity(sizeof(RtmpHeader));
|
|
|
|
|
bufferHeader->setSize(sizeof(RtmpHeader));
|
|
|
|
|
//对rtmp头赋值,如果使用整形赋值,在arm android上可能由于数据对齐导致总线错误的问题
|
|
|
|
|
RtmpHeader *header = (RtmpHeader*) bufferHeader->data();
|
|
|
|
|
header->flags = (iChunkId & 0x3f) | (0 << 6);
|
|
|
|
|
header->typeId = ui8Type;
|
|
|
|
|
set_be24(header->timeStamp, bExtStamp ? 0xFFFFFF : ui32TimeStamp);
|
|
|
|
|
set_be24(header->bodySize, buf->size());
|
|
|
|
|
set_le32(header->streamId, ui32StreamId);
|
|
|
|
|
//发送rtmp头
|
|
|
|
|
onSendRawData(bufferHeader);
|
2019-03-22 14:37:03 +08:00
|
|
|
|
|
2019-03-23 22:00:16 +08:00
|
|
|
|
//扩展时间戳字段
|
|
|
|
|
BufferRaw::Ptr bufferExtStamp;
|
2019-03-22 14:37:03 +08:00
|
|
|
|
if (bExtStamp) {
|
2019-03-23 22:00:16 +08:00
|
|
|
|
//生成扩展时间戳
|
|
|
|
|
bufferExtStamp = obtainBuffer();
|
|
|
|
|
bufferExtStamp->setCapacity(4);
|
|
|
|
|
bufferExtStamp->setSize(4);
|
|
|
|
|
set_be32(bufferExtStamp->data(), ui32TimeStamp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//生成一个字节的flag,标明是什么chunkId
|
|
|
|
|
BufferRaw::Ptr bufferFlags = obtainBuffer();
|
|
|
|
|
bufferFlags->setCapacity(1);
|
|
|
|
|
bufferFlags->setSize(1);
|
|
|
|
|
bufferFlags->data()[0] = (iChunkId & 0x3f) | (3 << 6);
|
|
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
uint32_t totalSize = sizeof(RtmpHeader);
|
|
|
|
|
while (offset < buf->size()) {
|
|
|
|
|
if (offset) {
|
|
|
|
|
onSendRawData(bufferFlags);
|
2019-03-22 14:37:03 +08:00
|
|
|
|
totalSize += 1;
|
|
|
|
|
}
|
|
|
|
|
if (bExtStamp) {
|
|
|
|
|
//扩展时间戳
|
2019-03-23 22:00:16 +08:00
|
|
|
|
onSendRawData(bufferExtStamp);
|
2019-03-22 14:37:03 +08:00
|
|
|
|
totalSize += 4;
|
|
|
|
|
}
|
2019-03-23 22:00:16 +08:00
|
|
|
|
size_t chunk = min(_iChunkLenOut, buf->size() - offset);
|
|
|
|
|
onSendRawData(std::make_shared<BufferPartial>(buf,offset,chunk));
|
2019-03-22 14:37:03 +08:00
|
|
|
|
totalSize += chunk;
|
2019-03-23 22:00:16 +08:00
|
|
|
|
offset += chunk;
|
2019-03-22 14:37:03 +08:00
|
|
|
|
}
|
|
|
|
|
_ui32ByteSent += totalSize;
|
|
|
|
|
if (_ui32WinSize > 0 && _ui32ByteSent - _ui32LastSent >= _ui32WinSize) {
|
|
|
|
|
_ui32LastSent = _ui32ByteSent;
|
|
|
|
|
sendAcknowledgement(_ui32ByteSent);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
2019-03-22 14:37:03 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void RtmpProtocol::onParseRtmp(const char *pcRawData, int iSize) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strRcvBuf.append(pcRawData, iSize);
|
|
|
|
|
auto cb = _nextHandle;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
cb();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////for client////
|
|
|
|
|
void RtmpProtocol::startClientSession(const function<void()> &callBack) {
|
|
|
|
|
//发送 C0C1
|
|
|
|
|
char handshake_head = HANDSHAKE_PLAINTEXT;
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer(&handshake_head, 1));
|
2017-05-17 11:52:59 +08:00
|
|
|
|
RtmpHandshake c1(0);
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer((char *) (&c1), sizeof(c1)));
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this,callBack]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//等待 S0+S1+S2
|
|
|
|
|
handle_S0S1S2(callBack);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
void RtmpProtocol::handle_S0S1S2(const function<void()> &callBack) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < 1 + 2 * C1_HANDSHARK_SIZE) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//数据不够
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf[0] != HANDSHAKE_PLAINTEXT) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
throw std::runtime_error("only plaintext[0x03] handshake supported");
|
|
|
|
|
}
|
|
|
|
|
//发送 C2
|
2018-10-24 15:43:52 +08:00
|
|
|
|
const char *pcC2 = _strRcvBuf.data() + 1;
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer(pcC2, C1_HANDSHARK_SIZE));
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strRcvBuf.erase(0, 1 + 2 * C1_HANDSHARK_SIZE);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//握手结束
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//握手结束并且开始进入解析命令模式
|
|
|
|
|
handle_rtmp();
|
|
|
|
|
};
|
|
|
|
|
callBack();
|
|
|
|
|
}
|
|
|
|
|
////for server ////
|
|
|
|
|
void RtmpProtocol::handle_C0C1() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < 1 + C1_HANDSHARK_SIZE) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data!
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf[0] != HANDSHAKE_PLAINTEXT) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
throw std::runtime_error("only plaintext[0x03] handshake supported");
|
|
|
|
|
}
|
2019-03-27 18:41:52 +08:00
|
|
|
|
if(memcmp(_strRcvBuf.data() + 5,"\x00\x00\x00\x00",4) ==0 ){
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//simple handsharke
|
|
|
|
|
handle_C1_simple();
|
|
|
|
|
}else{
|
|
|
|
|
#ifdef ENABLE_OPENSSL
|
|
|
|
|
//complex handsharke
|
|
|
|
|
handle_C1_complex();
|
|
|
|
|
#else
|
|
|
|
|
WarnL << "未打开ENABLE_OPENSSL宏,复杂握手采用简单方式处理!";
|
|
|
|
|
handle_C1_simple();
|
|
|
|
|
#endif//ENABLE_OPENSSL
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strRcvBuf.erase(0, 1 + C1_HANDSHARK_SIZE);
|
2017-05-16 11:45:36 +08:00
|
|
|
|
}
|
|
|
|
|
void RtmpProtocol::handle_C1_simple(){
|
2017-05-13 17:25:31 +08:00
|
|
|
|
//发送S0
|
2017-05-16 11:45:36 +08:00
|
|
|
|
char handshake_head = HANDSHAKE_PLAINTEXT;
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer(&handshake_head, 1));
|
2017-05-13 17:25:31 +08:00
|
|
|
|
//发送S1
|
2017-05-16 11:45:36 +08:00
|
|
|
|
RtmpHandshake s1(0);
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer((char *) &s1, C1_HANDSHARK_SIZE));
|
2017-05-13 17:25:31 +08:00
|
|
|
|
//发送S2
|
2019-03-27 18:41:52 +08:00
|
|
|
|
onSendRawData(obtainBuffer(_strRcvBuf.data() + 1, C1_HANDSHARK_SIZE));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//等待C2
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
handle_C2();
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-05-16 11:51:18 +08:00
|
|
|
|
#ifdef ENABLE_OPENSSL
|
2017-05-16 11:45:36 +08:00
|
|
|
|
void RtmpProtocol::handle_C1_complex(){
|
2017-08-09 18:39:30 +08:00
|
|
|
|
//参考自:http://blog.csdn.net/win_lin/article/details/13006803
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//skip c0,time,version
|
2018-10-24 15:43:52 +08:00
|
|
|
|
const char *c1_start = _strRcvBuf.data() + 1;
|
2017-05-16 11:45:36 +08:00
|
|
|
|
const char *schema_start = c1_start + 8;
|
|
|
|
|
char *digest_start;
|
|
|
|
|
try{
|
|
|
|
|
/* c1s1 schema0
|
|
|
|
|
time: 4bytes
|
|
|
|
|
version: 4bytes
|
|
|
|
|
key: 764bytes
|
|
|
|
|
digest: 764bytes
|
|
|
|
|
*/
|
|
|
|
|
auto digest = get_C1_digest((uint8_t *)schema_start + C1_SCHEMA_SIZE,&digest_start);
|
|
|
|
|
string c1_joined(c1_start,C1_HANDSHARK_SIZE);
|
|
|
|
|
c1_joined.erase(digest_start - c1_start , C1_DIGEST_SIZE );
|
|
|
|
|
check_C1_Digest(digest,c1_joined);
|
|
|
|
|
|
|
|
|
|
send_complex_S0S1S2(0,digest);
|
|
|
|
|
InfoL << "schema0";
|
|
|
|
|
}catch(std::exception &ex){
|
|
|
|
|
//貌似flash从来都不用schema1
|
|
|
|
|
WarnL << "try rtmp complex schema0 failed:" << ex.what();
|
|
|
|
|
try{
|
|
|
|
|
/* c1s1 schema1
|
|
|
|
|
time: 4bytes
|
|
|
|
|
version: 4bytes
|
|
|
|
|
digest: 764bytes
|
|
|
|
|
key: 764bytes
|
|
|
|
|
*/
|
|
|
|
|
auto digest = get_C1_digest((uint8_t *)schema_start,&digest_start);
|
|
|
|
|
string c1_joined(c1_start,C1_HANDSHARK_SIZE);
|
|
|
|
|
c1_joined.erase(digest_start - c1_start , C1_DIGEST_SIZE );
|
|
|
|
|
check_C1_Digest(digest,c1_joined);
|
|
|
|
|
|
|
|
|
|
send_complex_S0S1S2(1,digest);
|
|
|
|
|
InfoL << "schema1";
|
|
|
|
|
}catch(std::exception &ex){
|
|
|
|
|
WarnL << "try rtmp complex schema1 failed:" << ex.what();
|
|
|
|
|
handle_C1_simple();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#if !defined(u_int8_t)
|
|
|
|
|
#define u_int8_t unsigned char
|
|
|
|
|
#endif // !defined(u_int8_t)
|
2017-05-16 11:45:36 +08:00
|
|
|
|
|
|
|
|
|
static u_int8_t FMSKey[] = {
|
|
|
|
|
0x47, 0x65, 0x6e, 0x75, 0x69, 0x6e, 0x65, 0x20,
|
|
|
|
|
0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x46, 0x6c,
|
|
|
|
|
0x61, 0x73, 0x68, 0x20, 0x4d, 0x65, 0x64, 0x69,
|
|
|
|
|
0x61, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
|
|
|
|
|
0x20, 0x30, 0x30, 0x31, // Genuine Adobe Flash Media Server 001
|
|
|
|
|
0xf0, 0xee, 0xc2, 0x4a, 0x80, 0x68, 0xbe, 0xe8,
|
|
|
|
|
0x2e, 0x00, 0xd0, 0xd1, 0x02, 0x9e, 0x7e, 0x57,
|
|
|
|
|
0x6e, 0xec, 0x5d, 0x2d, 0x29, 0x80, 0x6f, 0xab,
|
|
|
|
|
0x93, 0xb8, 0xe6, 0x36, 0xcf, 0xeb, 0x31, 0xae
|
|
|
|
|
}; // 68
|
|
|
|
|
|
|
|
|
|
static u_int8_t FPKey[] = {
|
|
|
|
|
0x47, 0x65, 0x6E, 0x75, 0x69, 0x6E, 0x65, 0x20,
|
|
|
|
|
0x41, 0x64, 0x6F, 0x62, 0x65, 0x20, 0x46, 0x6C,
|
|
|
|
|
0x61, 0x73, 0x68, 0x20, 0x50, 0x6C, 0x61, 0x79,
|
|
|
|
|
0x65, 0x72, 0x20, 0x30, 0x30, 0x31, // Genuine Adobe Flash Player 001
|
|
|
|
|
0xF0, 0xEE, 0xC2, 0x4A, 0x80, 0x68, 0xBE, 0xE8,
|
|
|
|
|
0x2E, 0x00, 0xD0, 0xD1, 0x02, 0x9E, 0x7E, 0x57,
|
|
|
|
|
0x6E, 0xEC, 0x5D, 0x2D, 0x29, 0x80, 0x6F, 0xAB,
|
|
|
|
|
0x93, 0xB8, 0xE6, 0x36, 0xCF, 0xEB, 0x31, 0xAE
|
|
|
|
|
}; // 62
|
|
|
|
|
void RtmpProtocol::check_C1_Digest(const string &digest,const string &data){
|
|
|
|
|
auto sha256 = openssl_HMACsha256(FPKey,C1_FPKEY_SIZE,data.data(),data.size());
|
|
|
|
|
if(sha256 != digest){
|
2017-08-09 18:39:30 +08:00
|
|
|
|
throw std::runtime_error("digest mismatched");
|
2017-05-16 11:45:36 +08:00
|
|
|
|
}else{
|
|
|
|
|
InfoL << "check rtmp complex handshark success!";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
string RtmpProtocol::get_C1_digest(const uint8_t *ptr,char **digestPos){
|
|
|
|
|
/* 764bytes digest结构
|
|
|
|
|
offset: 4bytes
|
|
|
|
|
random-data: (offset)bytes
|
|
|
|
|
digest-data: 32bytes
|
|
|
|
|
random-data: (764-4-offset-32)bytes
|
|
|
|
|
*/
|
|
|
|
|
int offset = 0;
|
|
|
|
|
for(int i=0;i<C1_OFFSET_SIZE;++i){
|
|
|
|
|
offset += ptr[i];
|
|
|
|
|
}
|
|
|
|
|
offset %= (C1_SCHEMA_SIZE - C1_DIGEST_SIZE - C1_OFFSET_SIZE);
|
|
|
|
|
*digestPos = (char *)ptr + C1_OFFSET_SIZE + offset;
|
|
|
|
|
string digest(*digestPos,C1_DIGEST_SIZE);
|
|
|
|
|
//DebugL << "digest offset:" << offset << ",digest:" << hexdump(digest.data(),digest.size());
|
|
|
|
|
return digest;
|
|
|
|
|
}
|
|
|
|
|
string RtmpProtocol::get_C1_key(const uint8_t *ptr){
|
|
|
|
|
/* 764bytes key结构
|
|
|
|
|
random-data: (offset)bytes
|
|
|
|
|
key-data: 128bytes
|
|
|
|
|
random-data: (764-offset-128-4)bytes
|
|
|
|
|
offset: 4bytes
|
|
|
|
|
*/
|
|
|
|
|
int offset = 0;
|
|
|
|
|
for(int i = C1_SCHEMA_SIZE - C1_OFFSET_SIZE;i< C1_SCHEMA_SIZE;++i){
|
|
|
|
|
offset += ptr[i];
|
|
|
|
|
}
|
|
|
|
|
offset %= (C1_SCHEMA_SIZE - C1_KEY_SIZE - C1_OFFSET_SIZE);
|
|
|
|
|
string key((char *)ptr + offset,C1_KEY_SIZE);
|
|
|
|
|
//DebugL << "key offset:" << offset << ",key:" << hexdump(key.data(),key.size());
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
void RtmpProtocol::send_complex_S0S1S2(int schemeType,const string &digest){
|
2017-08-09 18:39:30 +08:00
|
|
|
|
//S1S2计算参考自:https://github.com/hitYangfei/golang/blob/master/rtmpserver.go
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//发送S0
|
|
|
|
|
char handshake_head = HANDSHAKE_PLAINTEXT;
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer(&handshake_head, 1));
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//S1
|
2017-05-17 11:52:59 +08:00
|
|
|
|
RtmpHandshake s1(0);
|
2017-05-16 11:45:36 +08:00
|
|
|
|
memcpy(s1.zero,"\x04\x05\x00\x01",4);
|
|
|
|
|
char *digestPos;
|
|
|
|
|
if(schemeType == 0){
|
|
|
|
|
/* c1s1 schema0
|
|
|
|
|
time: 4bytes
|
|
|
|
|
version: 4bytes
|
|
|
|
|
key: 764bytes
|
|
|
|
|
digest: 764bytes
|
|
|
|
|
*/
|
|
|
|
|
get_C1_digest(s1.random + C1_SCHEMA_SIZE,&digestPos);
|
|
|
|
|
}else{
|
|
|
|
|
/* c1s1 schema1
|
|
|
|
|
time: 4bytes
|
|
|
|
|
version: 4bytes
|
|
|
|
|
digest: 764bytes
|
|
|
|
|
key: 764bytes
|
|
|
|
|
*/
|
|
|
|
|
get_C1_digest(s1.random,&digestPos);
|
|
|
|
|
}
|
|
|
|
|
char *s1_start = (char *)&s1;
|
|
|
|
|
string s1_joined(s1_start,sizeof(s1));
|
|
|
|
|
s1_joined.erase(digestPos - s1_start,C1_DIGEST_SIZE);
|
|
|
|
|
string s1_digest = openssl_HMACsha256(FMSKey,S1_FMS_KEY_SIZE,s1_joined.data(),s1_joined.size());
|
|
|
|
|
memcpy(digestPos,s1_digest.data(),s1_digest.size());
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer((char *) &s1, sizeof(s1)));
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//S2
|
|
|
|
|
string s2_key = openssl_HMACsha256(FMSKey,S2_FMS_KEY_SIZE,digest.data(),digest.size());
|
|
|
|
|
RtmpHandshake s2(0);
|
|
|
|
|
s2.random_generate((char *)&s2,8);
|
|
|
|
|
string s2_digest = openssl_HMACsha256(s2_key.data(),s2_key.size(),&s2,sizeof(s2) - C1_DIGEST_SIZE);
|
|
|
|
|
memcpy((char *)&s2 + C1_HANDSHARK_SIZE - C1_DIGEST_SIZE,s2_digest.data(),C1_DIGEST_SIZE);
|
2018-09-26 23:12:03 +08:00
|
|
|
|
onSendRawData(obtainBuffer((char *)&s2, sizeof(s2)));
|
2017-05-16 11:45:36 +08:00
|
|
|
|
//等待C2
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this]() {
|
2017-05-16 11:45:36 +08:00
|
|
|
|
handle_C2();
|
|
|
|
|
};
|
|
|
|
|
}
|
2017-05-16 11:51:18 +08:00
|
|
|
|
#endif //ENABLE_OPENSSL
|
2017-04-01 16:35:56 +08:00
|
|
|
|
void RtmpProtocol::handle_C2() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < C1_HANDSHARK_SIZE) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data!
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_strRcvBuf.erase(0, C1_HANDSHARK_SIZE);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//握手结束,进入命令模式
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (!_strRcvBuf.empty()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
handle_rtmp();
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_nextHandle = [this]() {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
handle_rtmp();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::handle_rtmp() {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
while (!_strRcvBuf.empty()) {
|
|
|
|
|
uint8_t flags = _strRcvBuf[0];
|
2017-04-01 16:35:56 +08:00
|
|
|
|
int iOffset = 0;
|
|
|
|
|
static const size_t HEADER_LENGTH[] = { 12, 8, 4, 1 };
|
|
|
|
|
size_t iHeaderLen = HEADER_LENGTH[flags >> 6];
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iNowChunkID = flags & 0x3f;
|
|
|
|
|
if(_iNowChunkID >10){
|
2017-05-27 14:37:19 +08:00
|
|
|
|
int i=0;
|
|
|
|
|
i++;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
switch (_iNowChunkID) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
case 0: {
|
|
|
|
|
//0 值表示二字节形式,并且 ID 范围 64 - 319
|
|
|
|
|
//(第二个字节 + 64)。
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < 2) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iNowChunkID = 64 + (uint8_t) (_strRcvBuf[1]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
iOffset = 1;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 1: {
|
|
|
|
|
//1 值表示三字节形式,并且 ID 范围为 64 - 65599
|
|
|
|
|
//((第三个字节) * 256 + 第二个字节 + 64)。
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < 3) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iNowChunkID = 64 + ((uint8_t) (_strRcvBuf[2]) << 8) + (uint8_t) (_strRcvBuf[1]);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
iOffset = 2;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
//带有 2 值的块流 ID 被保留,用于下层协议控制消息和命令。
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < iHeaderLen + iOffset) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
RtmpHeader &header = *((RtmpHeader *) (_strRcvBuf.data() + iOffset));
|
|
|
|
|
auto &chunkData = _mapChunkData[_iNowChunkID];
|
|
|
|
|
chunkData.chunkId = _iNowChunkID;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
switch (iHeaderLen) {
|
|
|
|
|
case 12:
|
2017-05-27 10:10:07 +08:00
|
|
|
|
chunkData.hasAbsStamp = true;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
chunkData.streamId = load_le32(header.streamId);
|
|
|
|
|
case 8:
|
|
|
|
|
chunkData.bodySize = load_be24(header.bodySize);
|
|
|
|
|
chunkData.typeId = header.typeId;
|
|
|
|
|
case 4:
|
2017-05-27 09:42:08 +08:00
|
|
|
|
chunkData.deltaStamp = load_be24(header.timeStamp);
|
2017-05-27 14:37:19 +08:00
|
|
|
|
chunkData.hasExtStamp = chunkData.deltaStamp == 0xFFFFFF;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
2017-05-27 09:42:08 +08:00
|
|
|
|
|
2017-05-27 14:37:19 +08:00
|
|
|
|
if (chunkData.hasExtStamp) {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
if (_strRcvBuf.size() < iHeaderLen + iOffset + 4) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data
|
|
|
|
|
return;
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
chunkData.deltaStamp = load_be32(_strRcvBuf.data() + iOffset + iHeaderLen);
|
2017-04-01 16:35:56 +08:00
|
|
|
|
iOffset += 4;
|
|
|
|
|
}
|
2017-05-27 09:42:08 +08:00
|
|
|
|
|
|
|
|
|
if (chunkData.bodySize < chunkData.strBuf.size()) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
throw std::runtime_error("非法的bodySize");
|
|
|
|
|
}
|
2017-05-27 09:42:08 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
auto iMore = min(_iChunkLenIn, chunkData.bodySize - chunkData.strBuf.size());
|
|
|
|
|
if (_strRcvBuf.size() < iHeaderLen + iOffset + iMore) {
|
2017-04-01 16:35:56 +08:00
|
|
|
|
//need more data
|
|
|
|
|
return;
|
|
|
|
|
}
|
2017-05-27 09:42:08 +08:00
|
|
|
|
|
2018-10-24 15:43:52 +08:00
|
|
|
|
chunkData.strBuf.append(_strRcvBuf, iHeaderLen + iOffset, iMore);
|
|
|
|
|
_strRcvBuf.erase(0, iHeaderLen + iOffset + iMore);
|
2017-05-27 09:42:08 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
if (chunkData.strBuf.size() == chunkData.bodySize) {
|
2017-05-27 09:42:08 +08:00
|
|
|
|
//frame is ready
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iNowStreamID = chunkData.streamId;
|
2017-05-27 10:10:07 +08:00
|
|
|
|
chunkData.timeStamp = chunkData.deltaStamp + (chunkData.hasAbsStamp ? 0 : chunkData.timeStamp);
|
2017-05-27 14:37:19 +08:00
|
|
|
|
|
2017-05-18 09:10:22 +08:00
|
|
|
|
if(chunkData.bodySize){
|
|
|
|
|
handle_rtmpChunk(chunkData);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
chunkData.strBuf.clear();
|
2017-05-27 14:37:19 +08:00
|
|
|
|
chunkData.hasAbsStamp = false;
|
|
|
|
|
chunkData.hasExtStamp = false;
|
|
|
|
|
chunkData.deltaStamp = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void RtmpProtocol::handle_rtmpChunk(RtmpPacket& chunkData) {
|
|
|
|
|
switch (chunkData.typeId) {
|
|
|
|
|
case MSG_ACK: {
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("MSG_ACK: Not enough data");
|
|
|
|
|
}
|
|
|
|
|
//auto bytePeerRecv = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
//TraceL << "MSG_ACK:" << bytePeerRecv;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case MSG_SET_CHUNK: {
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("MSG_SET_CHUNK :Not enough data");
|
|
|
|
|
}
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_iChunkLenIn = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
TraceL << "MSG_SET_CHUNK:" << _iChunkLenIn;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case MSG_USER_CONTROL: {
|
|
|
|
|
//user control message
|
|
|
|
|
if (chunkData.strBuf.size() < 2) {
|
|
|
|
|
throw std::runtime_error("MSG_USER_CONTROL: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
uint16_t event_type = load_be16(&chunkData.strBuf[0]);
|
|
|
|
|
chunkData.strBuf.erase(0, 2);
|
|
|
|
|
switch (event_type) {
|
|
|
|
|
case CONTROL_PING_REQUEST: {
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("CONTROL_PING_REQUEST: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t timeStamp = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
//TraceL << "CONTROL_PING_REQUEST:" << timeStamp;
|
|
|
|
|
sendUserControl(CONTROL_PING_RESPONSE, timeStamp);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CONTROL_PING_RESPONSE: {
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("CONTROL_PING_RESPONSE: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
//uint32_t timeStamp = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
//TraceL << "CONTROL_PING_RESPONSE:" << timeStamp;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CONTROL_STREAM_BEGIN: {
|
|
|
|
|
//开始播放
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("CONTROL_STREAM_BEGIN: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t stramId = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
onStreamBegin(stramId);
|
|
|
|
|
TraceL << "CONTROL_STREAM_BEGIN:" << stramId;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case CONTROL_STREAM_EOF: {
|
|
|
|
|
//暂停
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("CONTROL_STREAM_EOF: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t stramId = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
onStreamEof(stramId);
|
|
|
|
|
TraceL << "CONTROL_STREAM_EOF:" << stramId;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case CONTROL_STREAM_DRY: {
|
|
|
|
|
//停止播放
|
|
|
|
|
if (chunkData.strBuf.size() < 4) {
|
|
|
|
|
throw std::runtime_error("CONTROL_STREAM_DRY: Not enough data.");
|
|
|
|
|
}
|
|
|
|
|
uint32_t stramId = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
onStreamDry(stramId);
|
|
|
|
|
TraceL << "CONTROL_STREAM_DRY:" << stramId;
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
//WarnL << "unhandled user control:" << event_type;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case MSG_WIN_SIZE: {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui32WinSize = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
TraceL << "MSG_WIN_SIZE:" << _ui32WinSize;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case MSG_SET_PEER_BW: {
|
2018-10-24 15:43:52 +08:00
|
|
|
|
_ui32Bandwidth = load_be32(&chunkData.strBuf[0]);
|
|
|
|
|
_ui8LimitType = chunkData.strBuf[4];
|
|
|
|
|
TraceL << "MSG_SET_PEER_BW:" << _ui32WinSize;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
break;
|
2019-02-28 18:03:49 +08:00
|
|
|
|
case MSG_AGGREGATE: {
|
|
|
|
|
auto ptr = (uint8_t*)chunkData.strBuf.data();
|
2019-03-04 09:44:10 +08:00
|
|
|
|
auto ptr_tail = ptr + chunkData.strBuf.length() ;
|
|
|
|
|
while(ptr + 8 + 3 < ptr_tail){
|
2019-02-28 18:03:49 +08:00
|
|
|
|
auto type = *ptr;
|
|
|
|
|
ptr += 1;
|
|
|
|
|
auto size = load_be24(ptr);
|
|
|
|
|
ptr += 3;
|
|
|
|
|
auto ts = load_be24(ptr);
|
|
|
|
|
ptr += 3;
|
|
|
|
|
ts |= (*ptr << 24);
|
|
|
|
|
ptr += 1;
|
|
|
|
|
|
|
|
|
|
//参考ffmpeg忽略了3个字节
|
|
|
|
|
/**
|
|
|
|
|
* while (next - pkt->data < pkt->size - RTMP_HEADER) {
|
|
|
|
|
type = bytestream_get_byte(&next);
|
|
|
|
|
size = bytestream_get_be24(&next);
|
|
|
|
|
cts = bytestream_get_be24(&next);
|
|
|
|
|
cts |= bytestream_get_byte(&next) << 24;
|
|
|
|
|
if (!pts)
|
|
|
|
|
pts = cts;
|
|
|
|
|
ts += cts - pts;
|
|
|
|
|
pts = cts;
|
|
|
|
|
if (size + 3 + 4 > pkt->data + pkt->size - next)
|
|
|
|
|
break;
|
|
|
|
|
bytestream_put_byte(&p, type);
|
|
|
|
|
bytestream_put_be24(&p, size);
|
|
|
|
|
bytestream_put_be24(&p, ts);
|
|
|
|
|
bytestream_put_byte(&p, ts >> 24);
|
|
|
|
|
memcpy(p, next, size + 3 + 4);
|
|
|
|
|
p += size + 3;
|
|
|
|
|
bytestream_put_be32(&p, size + RTMP_HEADER);
|
|
|
|
|
next += size + 3 + 4;
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
ptr += 3;
|
|
|
|
|
//参考FFmpeg多拷贝了4个字节
|
|
|
|
|
size += 4;
|
|
|
|
|
if(ptr + size > ptr_tail){
|
2019-03-04 09:44:10 +08:00
|
|
|
|
// ErrorL << ptr + size << " " << ptr_tail << " " << ptr_tail - ptr - size;
|
2019-02-28 18:03:49 +08:00
|
|
|
|
break;
|
|
|
|
|
}
|
2019-03-04 09:44:10 +08:00
|
|
|
|
// DebugL << (int)type << " " << size << " " << ts << " " << chunkData.timeStamp << " " << ptr_tail - ptr;
|
2019-02-28 18:03:49 +08:00
|
|
|
|
RtmpPacket sub_packet ;
|
|
|
|
|
sub_packet.strBuf.resize(size);
|
|
|
|
|
memcpy((char *)sub_packet.strBuf.data(),ptr,size);
|
|
|
|
|
sub_packet.typeId = type;
|
|
|
|
|
sub_packet.bodySize = size;
|
|
|
|
|
sub_packet.timeStamp = ts;
|
|
|
|
|
sub_packet.streamId = chunkData.streamId;
|
|
|
|
|
sub_packet.chunkId = chunkData.chunkId;
|
|
|
|
|
handle_rtmpChunk(sub_packet);
|
|
|
|
|
ptr += size;
|
|
|
|
|
}
|
2019-03-04 09:44:10 +08:00
|
|
|
|
// InfoL << ptr_tail - ptr;
|
2019-02-28 18:03:49 +08:00
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
onRtmpChunk(chunkData);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-02 14:33:57 +08:00
|
|
|
|
BufferRaw::Ptr RtmpProtocol::obtainBuffer() {
|
|
|
|
|
return std::make_shared<BufferRaw>() ;//_bufferPool.obtain();
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-26 23:12:03 +08:00
|
|
|
|
BufferRaw::Ptr RtmpProtocol::obtainBuffer(const void *data, int len) {
|
|
|
|
|
auto buffer = obtainBuffer();
|
|
|
|
|
buffer->assign((const char *)data,len);
|
|
|
|
|
return buffer;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
} /* namespace mediakit */
|