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
|
|
|
|
*
|
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit).
|
|
|
|
|
*
|
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-09-27 16:20:30 +08:00
|
|
|
|
*/
|
2020-04-04 20:30:09 +08:00
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#ifndef __rtmp_h
|
|
|
|
|
#define __rtmp_h
|
|
|
|
|
|
|
|
|
|
#include <memory>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include <string>
|
2019-01-16 14:26:06 +08:00
|
|
|
|
#include <cstdlib>
|
2017-04-25 11:35:41 +08:00
|
|
|
|
#include "Util/util.h"
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#include "Util/logger.h"
|
2019-03-22 14:37:03 +08:00
|
|
|
|
#include "Network/Buffer.h"
|
2017-08-10 14:18:59 +08:00
|
|
|
|
#include "Network/sockutil.h"
|
2019-06-28 17:25:53 +08:00
|
|
|
|
#include "amf.h"
|
|
|
|
|
#include "Extension/Track.h"
|
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
using namespace toolkit;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#define PORT 1935
|
|
|
|
|
#define DEFAULT_CHUNK_LEN 128
|
2017-08-09 18:39:30 +08:00
|
|
|
|
|
|
|
|
|
#if !defined(_WIN32)
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#define PACKED __attribute__((packed))
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#else
|
|
|
|
|
#define PACKED
|
|
|
|
|
#endif //!defined(_WIN32)
|
|
|
|
|
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
#define HANDSHAKE_PLAINTEXT 0x03
|
|
|
|
|
#define RANDOM_LEN (1536 - 8)
|
|
|
|
|
|
|
|
|
|
#define MSG_SET_CHUNK 1 /*Set Chunk Size (1)*/
|
|
|
|
|
#define MSG_ABORT 2 /*Abort Message (2)*/
|
|
|
|
|
#define MSG_ACK 3 /*Acknowledgement (3)*/
|
|
|
|
|
#define MSG_USER_CONTROL 4 /*User Control Messages (4)*/
|
|
|
|
|
#define MSG_WIN_SIZE 5 /*Window Acknowledgement Size (5)*/
|
|
|
|
|
#define MSG_SET_PEER_BW 6 /*Set Peer Bandwidth (6)*/
|
|
|
|
|
#define MSG_AUDIO 8 /*Audio Message (8)*/
|
|
|
|
|
#define MSG_VIDEO 9 /*Video Message (9)*/
|
|
|
|
|
#define MSG_DATA 18 /*Data Message (18, 15) AMF0*/
|
|
|
|
|
#define MSG_DATA3 15 /*Data Message (18, 15) AMF3*/
|
|
|
|
|
#define MSG_CMD 20 /*Command Message AMF0 */
|
|
|
|
|
#define MSG_CMD3 17 /*Command Message AMF3 */
|
|
|
|
|
#define MSG_OBJECT3 16 /*Shared Object Message (19, 16) AMF3*/
|
|
|
|
|
#define MSG_OBJECT 19 /*Shared Object Message (19, 16) AMF0*/
|
|
|
|
|
#define MSG_AGGREGATE 22 /*Aggregate Message (22)*/
|
|
|
|
|
|
|
|
|
|
#define CONTROL_STREAM_BEGIN 0
|
|
|
|
|
#define CONTROL_STREAM_EOF 1
|
|
|
|
|
#define CONTROL_STREAM_DRY 2
|
|
|
|
|
#define CONTROL_SETBUFFER 3
|
|
|
|
|
#define CONTROL_STREAM_ISRECORDED 4
|
|
|
|
|
#define CONTROL_PING_REQUEST 6
|
|
|
|
|
#define CONTROL_PING_RESPONSE 7
|
|
|
|
|
|
|
|
|
|
#define STREAM_CONTROL 0
|
|
|
|
|
#define STREAM_MEDIA 1
|
|
|
|
|
|
|
|
|
|
#define CHUNK_SERVER_REQUEST 2 /*服务器像客户端发出请求时的chunkID*/
|
|
|
|
|
#define CHUNK_CLIENT_REQUEST_BEFORE 3 /*客户端在createStream前,向服务器发出请求的chunkID*/
|
|
|
|
|
#define CHUNK_CLIENT_REQUEST_AFTER 4 /*客户端在createStream后,向服务器发出请求的chunkID*/
|
2017-05-27 11:26:49 +08:00
|
|
|
|
#define CHUNK_AUDIO 6 /*音频chunkID*/
|
|
|
|
|
#define CHUNK_VIDEO 7 /*视频chunkID*/
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
|
|
|
|
#define FLV_KEY_FRAME 1
|
|
|
|
|
#define FLV_INTER_FRAME 2
|
|
|
|
|
|
2020-04-04 22:54:49 +08:00
|
|
|
|
#define FLV_CODEC_AAC 10
|
|
|
|
|
#define FLV_CODEC_H264 7
|
|
|
|
|
#define FLV_CODEC_H265 12
|
2020-04-17 17:47:10 +08:00
|
|
|
|
#define FLV_CODEC_G711A 7
|
|
|
|
|
#define FLV_CODEC_G711U 8
|
|
|
|
|
|
2020-04-04 22:54:49 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
namespace mediakit {
|
2017-08-09 18:39:30 +08:00
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#pragma pack(push, 1)
|
|
|
|
|
#endif // defined(_WIN32)
|
|
|
|
|
|
2017-04-01 16:35:56 +08:00
|
|
|
|
class RtmpHandshake {
|
|
|
|
|
public:
|
|
|
|
|
RtmpHandshake(uint32_t _time, uint8_t *_random = nullptr) {
|
|
|
|
|
_time = htonl(_time);
|
|
|
|
|
memcpy(timeStamp, &_time, 4);
|
|
|
|
|
if (!_random) {
|
|
|
|
|
random_generate((char *) random, sizeof(random));
|
|
|
|
|
} else {
|
|
|
|
|
memcpy(random, _random, sizeof(random));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
uint8_t timeStamp[4];
|
2017-05-13 17:25:31 +08:00
|
|
|
|
uint8_t zero[4] = {0};
|
2017-04-01 16:35:56 +08:00
|
|
|
|
uint8_t random[RANDOM_LEN];
|
|
|
|
|
void random_generate(char* bytes, int size) {
|
|
|
|
|
static char cdata[] = { 0x73, 0x69, 0x6d, 0x70, 0x6c, 0x65, 0x2d, 0x72,
|
|
|
|
|
0x74, 0x6d, 0x70, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
|
|
|
|
0x2d, 0x77, 0x69, 0x6e, 0x6c, 0x69, 0x6e, 0x2d, 0x77, 0x69,
|
|
|
|
|
0x6e, 0x74, 0x65, 0x72, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
|
|
|
|
|
0x40, 0x31, 0x32, 0x36, 0x2e, 0x63, 0x6f, 0x6d };
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
|
bytes[i] = cdata[rand() % (sizeof(cdata) - 1)];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}PACKED;
|
|
|
|
|
|
|
|
|
|
class RtmpHeader {
|
|
|
|
|
public:
|
|
|
|
|
uint8_t flags;
|
|
|
|
|
uint8_t timeStamp[3];
|
|
|
|
|
uint8_t bodySize[3];
|
|
|
|
|
uint8_t typeId;
|
|
|
|
|
uint8_t streamId[4]; /* Note, this is little-endian while others are BE */
|
|
|
|
|
}PACKED;
|
|
|
|
|
|
2017-08-09 18:39:30 +08:00
|
|
|
|
#if defined(_WIN32)
|
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
#endif // defined(_WIN32)
|
2017-04-01 16:35:56 +08:00
|
|
|
|
|
2019-03-22 14:37:03 +08:00
|
|
|
|
class RtmpPacket : public Buffer{
|
2017-04-01 16:35:56 +08:00
|
|
|
|
public:
|
|
|
|
|
typedef std::shared_ptr<RtmpPacket> Ptr;
|
|
|
|
|
uint8_t typeId;
|
|
|
|
|
uint32_t bodySize = 0;
|
|
|
|
|
uint32_t timeStamp = 0;
|
2017-05-27 10:10:07 +08:00
|
|
|
|
bool hasAbsStamp = false;
|
2017-05-27 14:37:19 +08:00
|
|
|
|
bool hasExtStamp = false;
|
2017-05-27 09:42:08 +08:00
|
|
|
|
uint32_t deltaStamp = 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
uint32_t streamId;
|
|
|
|
|
uint32_t chunkId;
|
|
|
|
|
std::string strBuf;
|
2019-03-22 14:37:03 +08:00
|
|
|
|
public:
|
|
|
|
|
char *data() const override{
|
|
|
|
|
return (char*)strBuf.data();
|
|
|
|
|
}
|
|
|
|
|
uint32_t size() const override {
|
|
|
|
|
return strBuf.size();
|
|
|
|
|
};
|
2019-02-28 18:20:17 +08:00
|
|
|
|
public:
|
|
|
|
|
RtmpPacket() = default;
|
2019-09-24 15:21:20 +08:00
|
|
|
|
RtmpPacket(const RtmpPacket &that) = delete;
|
|
|
|
|
RtmpPacket &operator=(const RtmpPacket &that) = delete;
|
|
|
|
|
RtmpPacket &operator=(RtmpPacket &&that) = delete;
|
2019-02-28 18:20:17 +08:00
|
|
|
|
|
|
|
|
|
RtmpPacket(RtmpPacket &&that){
|
|
|
|
|
typeId = that.typeId;
|
|
|
|
|
bodySize = that.bodySize;
|
|
|
|
|
timeStamp = that.timeStamp;
|
|
|
|
|
hasAbsStamp = that.hasAbsStamp;
|
|
|
|
|
hasExtStamp = that.hasExtStamp;
|
|
|
|
|
deltaStamp = that.deltaStamp;
|
|
|
|
|
streamId = that.streamId;
|
|
|
|
|
chunkId = that.chunkId;
|
|
|
|
|
strBuf = std::move(that.strBuf);
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
bool isVideoKeyFrame() const {
|
2020-04-18 22:13:11 +08:00
|
|
|
|
return typeId == MSG_VIDEO && (uint8_t) strBuf[0] >> 4 == FLV_KEY_FRAME && (uint8_t) strBuf[1] == 1;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
bool isCfgFrame() const {
|
2020-04-18 22:13:11 +08:00
|
|
|
|
switch (typeId){
|
|
|
|
|
case MSG_VIDEO : return strBuf[1] == 0;
|
|
|
|
|
case MSG_AUDIO : {
|
|
|
|
|
switch (getMediaType()){
|
|
|
|
|
case FLV_CODEC_AAC : return strBuf[1] == 0;
|
|
|
|
|
default : return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default : return false;
|
|
|
|
|
}
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
int getMediaType() const {
|
|
|
|
|
switch (typeId) {
|
2020-04-18 22:13:11 +08:00
|
|
|
|
case MSG_VIDEO : return (uint8_t) strBuf[0] & 0x0F;
|
|
|
|
|
case MSG_AUDIO : return (uint8_t) strBuf[0] >> 4;
|
|
|
|
|
default : return 0;
|
2017-04-01 16:35:56 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int getAudioSampleRate() const {
|
|
|
|
|
if (typeId != MSG_AUDIO) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int flvSampleRate = ((uint8_t) strBuf[0] & 0x0C) >> 2;
|
|
|
|
|
const static int sampleRate[] = { 5512, 11025, 22050, 44100 };
|
|
|
|
|
return sampleRate[flvSampleRate];
|
|
|
|
|
}
|
|
|
|
|
int getAudioSampleBit() const {
|
|
|
|
|
if (typeId != MSG_AUDIO) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int flvSampleBit = ((uint8_t) strBuf[0] & 0x02) >> 1;
|
|
|
|
|
const static int sampleBit[] = { 8, 16 };
|
|
|
|
|
return sampleBit[flvSampleBit];
|
|
|
|
|
}
|
|
|
|
|
int getAudioChannel() const {
|
|
|
|
|
if (typeId != MSG_AUDIO) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int flvStereoOrMono = (uint8_t) strBuf[0] & 0x01;
|
|
|
|
|
const static int channel[] = { 1, 2 };
|
|
|
|
|
return channel[flvStereoOrMono];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-06-28 17:25:53 +08:00
|
|
|
|
/**
|
2019-09-23 16:47:20 +08:00
|
|
|
|
* rtmp metadata基类,用于描述rtmp格式信息
|
2019-06-28 17:25:53 +08:00
|
|
|
|
*/
|
2019-09-23 16:47:20 +08:00
|
|
|
|
class Metadata : public CodecInfo{
|
2019-06-28 17:25:53 +08:00
|
|
|
|
public:
|
2019-09-23 16:47:20 +08:00
|
|
|
|
typedef std::shared_ptr<Metadata> Ptr;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
Metadata():_metadata(AMF_OBJECT){}
|
|
|
|
|
virtual ~Metadata(){}
|
|
|
|
|
const AMFValue &getMetadata() const{
|
|
|
|
|
return _metadata;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
}
|
|
|
|
|
protected:
|
2019-09-23 16:47:20 +08:00
|
|
|
|
AMFValue _metadata;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
2019-09-23 16:47:20 +08:00
|
|
|
|
* metadata中除音视频外的其他描述部分
|
2019-06-28 17:25:53 +08:00
|
|
|
|
*/
|
2019-09-23 16:47:20 +08:00
|
|
|
|
class TitleMeta : public Metadata{
|
2019-06-28 17:25:53 +08:00
|
|
|
|
public:
|
2019-09-23 16:47:20 +08:00
|
|
|
|
typedef std::shared_ptr<TitleMeta> Ptr;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
TitleMeta(float dur_sec = 0,
|
2019-06-28 17:25:53 +08:00
|
|
|
|
uint64_t fileSize = 0,
|
|
|
|
|
const map<string,string> &header = map<string,string>()){
|
2019-09-23 16:47:20 +08:00
|
|
|
|
_metadata.set("duration", dur_sec);
|
|
|
|
|
_metadata.set("fileSize", 0);
|
2020-04-04 19:55:11 +08:00
|
|
|
|
_metadata.set("server",SERVER_NAME);
|
2019-06-28 17:25:53 +08:00
|
|
|
|
for (auto &pr : header){
|
2019-09-23 16:47:20 +08:00
|
|
|
|
_metadata.set(pr.first, pr.second);
|
2019-06-28 17:25:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override{
|
|
|
|
|
return CodecInvalid;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
class VideoMeta : public Metadata{
|
2019-06-28 17:25:53 +08:00
|
|
|
|
public:
|
2019-09-23 16:47:20 +08:00
|
|
|
|
typedef std::shared_ptr<VideoMeta> Ptr;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
VideoMeta(const VideoTrack::Ptr &video,int datarate = 5000);
|
|
|
|
|
virtual ~VideoMeta(){}
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override{
|
|
|
|
|
return _codecId;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
CodecId _codecId;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
class AudioMeta : public Metadata{
|
2019-06-28 17:25:53 +08:00
|
|
|
|
public:
|
2019-09-23 16:47:20 +08:00
|
|
|
|
typedef std::shared_ptr<AudioMeta> Ptr;
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
AudioMeta(const AudioTrack::Ptr &audio,int datarate = 160);
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-09-23 16:47:20 +08:00
|
|
|
|
virtual ~AudioMeta(){}
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
|
|
|
|
CodecId getCodecId() const override{
|
|
|
|
|
return _codecId;
|
|
|
|
|
}
|
|
|
|
|
private:
|
|
|
|
|
CodecId _codecId;
|
|
|
|
|
};
|
|
|
|
|
|
2020-04-18 22:13:11 +08:00
|
|
|
|
//根据音频track获取flags
|
|
|
|
|
uint8_t getAudioRtmpFlags(const Track::Ptr &track);
|
2019-06-28 17:25:53 +08:00
|
|
|
|
|
2019-03-27 18:41:52 +08:00
|
|
|
|
}//namespace mediakit
|
2020-05-11 22:33:10 +08:00
|
|
|
|
#endif//__rtmp_h
|