mirror of
https://github.com/ZLMediaKit/ZLMediaKit.git
synced 2026-07-07 04:08:13 +08:00
整理代码
This commit is contained in:
102
src/Rtmp/Rtmp.h
102
src/Rtmp/Rtmp.h
@@ -23,9 +23,6 @@
|
||||
|
||||
using namespace toolkit;
|
||||
|
||||
#define PORT 1935
|
||||
#define DEFAULT_CHUNK_LEN 128
|
||||
|
||||
#if !defined(_WIN32)
|
||||
#define PACKED __attribute__((packed))
|
||||
#else
|
||||
@@ -33,6 +30,7 @@ using namespace toolkit;
|
||||
#endif //!defined(_WIN32)
|
||||
|
||||
|
||||
#define DEFAULT_CHUNK_LEN 128
|
||||
#define HANDSHAKE_PLAINTEXT 0x03
|
||||
#define RANDOM_LEN (1536 - 8)
|
||||
|
||||
@@ -91,22 +89,24 @@ class RtmpHandshake {
|
||||
public:
|
||||
RtmpHandshake(uint32_t _time, uint8_t *_random = nullptr) {
|
||||
_time = htonl(_time);
|
||||
memcpy(timeStamp, &_time, 4);
|
||||
memcpy(time_stamp, &_time, 4);
|
||||
if (!_random) {
|
||||
random_generate((char *) random, sizeof(random));
|
||||
} else {
|
||||
memcpy(random, _random, sizeof(random));
|
||||
}
|
||||
}
|
||||
uint8_t timeStamp[4];
|
||||
|
||||
uint8_t time_stamp[4];
|
||||
uint8_t zero[4] = {0};
|
||||
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 };
|
||||
|
||||
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)];
|
||||
}
|
||||
@@ -116,10 +116,10 @@ public:
|
||||
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 */
|
||||
uint8_t time_stamp[3];
|
||||
uint8_t body_size[3];
|
||||
uint8_t type_id;
|
||||
uint8_t stream_index[4]; /* Note, this is little-endian while others are BE */
|
||||
}PACKED;
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -129,21 +129,23 @@ public:
|
||||
class RtmpPacket : public Buffer{
|
||||
public:
|
||||
typedef std::shared_ptr<RtmpPacket> Ptr;
|
||||
uint8_t typeId;
|
||||
uint32_t bodySize = 0;
|
||||
uint32_t timeStamp = 0;
|
||||
bool hasAbsStamp = false;
|
||||
uint32_t tsField = 0;
|
||||
uint32_t streamId;
|
||||
uint32_t chunkId;
|
||||
std::string strBuf;
|
||||
uint8_t type_id;
|
||||
uint32_t body_size = 0;
|
||||
uint32_t time_stamp = 0;
|
||||
bool is_abs_stamp = false;
|
||||
uint32_t ts_field = 0;
|
||||
uint32_t stream_index;
|
||||
uint32_t chunk_id;
|
||||
std::string buffer;
|
||||
|
||||
public:
|
||||
char *data() const override{
|
||||
return (char*)strBuf.data();
|
||||
return (char*)buffer.data();
|
||||
}
|
||||
uint32_t size() const override {
|
||||
return strBuf.size();
|
||||
};
|
||||
return buffer.size();
|
||||
}
|
||||
|
||||
public:
|
||||
RtmpPacket() = default;
|
||||
RtmpPacket(const RtmpPacket &that) = delete;
|
||||
@@ -151,58 +153,64 @@ public:
|
||||
RtmpPacket &operator=(RtmpPacket &&that) = delete;
|
||||
|
||||
RtmpPacket(RtmpPacket &&that){
|
||||
typeId = that.typeId;
|
||||
bodySize = that.bodySize;
|
||||
timeStamp = that.timeStamp;
|
||||
hasAbsStamp = that.hasAbsStamp;
|
||||
tsField = that.tsField;
|
||||
streamId = that.streamId;
|
||||
chunkId = that.chunkId;
|
||||
strBuf = std::move(that.strBuf);
|
||||
type_id = that.type_id;
|
||||
body_size = that.body_size;
|
||||
time_stamp = that.time_stamp;
|
||||
is_abs_stamp = that.is_abs_stamp;
|
||||
ts_field = that.ts_field;
|
||||
stream_index = that.stream_index;
|
||||
chunk_id = that.chunk_id;
|
||||
buffer = std::move(that.buffer);
|
||||
}
|
||||
|
||||
bool isVideoKeyFrame() const {
|
||||
return typeId == MSG_VIDEO && (uint8_t) strBuf[0] >> 4 == FLV_KEY_FRAME && (uint8_t) strBuf[1] == 1;
|
||||
return type_id == MSG_VIDEO && (uint8_t) buffer[0] >> 4 == FLV_KEY_FRAME && (uint8_t) buffer[1] == 1;
|
||||
}
|
||||
|
||||
bool isCfgFrame() const {
|
||||
switch (typeId){
|
||||
case MSG_VIDEO : return strBuf[1] == 0;
|
||||
switch (type_id){
|
||||
case MSG_VIDEO : return buffer[1] == 0;
|
||||
case MSG_AUDIO : {
|
||||
switch (getMediaType()){
|
||||
case FLV_CODEC_AAC : return strBuf[1] == 0;
|
||||
case FLV_CODEC_AAC : return buffer[1] == 0;
|
||||
default : return false;
|
||||
}
|
||||
}
|
||||
default : return false;
|
||||
}
|
||||
}
|
||||
|
||||
int getMediaType() const {
|
||||
switch (typeId) {
|
||||
case MSG_VIDEO : return (uint8_t) strBuf[0] & 0x0F;
|
||||
case MSG_AUDIO : return (uint8_t) strBuf[0] >> 4;
|
||||
switch (type_id) {
|
||||
case MSG_VIDEO : return (uint8_t) buffer[0] & 0x0F;
|
||||
case MSG_AUDIO : return (uint8_t) buffer[0] >> 4;
|
||||
default : return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int getAudioSampleRate() const {
|
||||
if (typeId != MSG_AUDIO) {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
int flvSampleRate = ((uint8_t) strBuf[0] & 0x0C) >> 2;
|
||||
int flvSampleRate = ((uint8_t) buffer[0] & 0x0C) >> 2;
|
||||
const static int sampleRate[] = { 5512, 11025, 22050, 44100 };
|
||||
return sampleRate[flvSampleRate];
|
||||
}
|
||||
|
||||
int getAudioSampleBit() const {
|
||||
if (typeId != MSG_AUDIO) {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
int flvSampleBit = ((uint8_t) strBuf[0] & 0x02) >> 1;
|
||||
int flvSampleBit = ((uint8_t) buffer[0] & 0x02) >> 1;
|
||||
const static int sampleBit[] = { 8, 16 };
|
||||
return sampleBit[flvSampleBit];
|
||||
}
|
||||
|
||||
int getAudioChannel() const {
|
||||
if (typeId != MSG_AUDIO) {
|
||||
if (type_id != MSG_AUDIO) {
|
||||
return 0;
|
||||
}
|
||||
int flvStereoOrMono = (uint8_t) strBuf[0] & 0x01;
|
||||
int flvStereoOrMono = (uint8_t) buffer[0] & 0x01;
|
||||
const static int channel[] = { 1, 2 };
|
||||
return channel[flvStereoOrMono];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user