ZLMediaKit/src/Rtp/RtpSplitter.cpp

85 lines
2.3 KiB
C++
Raw Normal View History

2019-12-05 19:20:12 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
2019-12-06 11:54:10 +08:00
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2019-12-06 11:54:10 +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.
2019-12-06 11:54:10 +08:00
*/
2019-12-05 19:20:12 +08:00
2019-12-06 11:54:10 +08:00
#if defined(ENABLE_RTPPROXY)
2020-10-24 23:32:23 +08:00
#include <string.h>
2019-12-05 19:20:12 +08:00
#include "RtpSplitter.h"
2019-12-06 11:54:10 +08:00
namespace mediakit{
2019-12-05 19:20:12 +08:00
2020-10-24 23:32:23 +08:00
static const char kEHOME_MAGIC[] = "\x01\x00\x01\x00";
static const int kEHOME_OFFSET = 256;
2019-12-05 19:20:12 +08:00
2020-10-24 23:32:23 +08:00
RtpSplitter::RtpSplitter() {}
2020-08-15 09:48:27 +08:00
RtpSplitter::~RtpSplitter() {}
2019-12-05 19:20:12 +08:00
size_t RtpSplitter::onRecvHeader(const char *data,size_t len){
2020-10-24 23:32:23 +08:00
//忽略偏移量
data += _offset;
len -= _offset;
2020-11-07 17:46:37 +08:00
if (_is_ehome && len > 12 && data[12] == '\r') {
2020-10-24 23:32:23 +08:00
//这是ehome,移除第12个字节
memmove((char *) data + 1, data, 12);
data += 1;
len -= 1;
}
onRtpPacket(data, len);
return 0;
}
static bool isEhome(const char *data, size_t len){
2020-10-24 23:32:23 +08:00
if (len < 4) {
return false;
}
return memcmp(data, kEHOME_MAGIC, sizeof(kEHOME_MAGIC) - 1) == 0;
}
const char *RtpSplitter::onSearchPacketTail(const char *data, size_t len) {
2020-10-18 21:39:22 +08:00
if (len < 4) {
//数据不够
return nullptr;
}
2020-10-24 23:32:23 +08:00
if (isEhome(data, len)) {
//是ehome协议
if (len < kEHOME_OFFSET + 4) {
//数据不够
return nullptr;
}
//忽略ehome私有头后是rtsp样式的rtp多4个字节
_offset = kEHOME_OFFSET + 4;
2020-11-07 17:46:37 +08:00
_is_ehome = true;
2020-10-24 23:32:23 +08:00
//忽略ehome私有头
return onSearchPacketTail_l(data + kEHOME_OFFSET + 2, len - kEHOME_OFFSET - 2);
}
2020-08-15 09:48:27 +08:00
if (data[0] == '$') {
//可能是4个字节的rtp头
2020-10-18 21:39:22 +08:00
_offset = 4;
2020-08-15 09:48:27 +08:00
return onSearchPacketTail_l(data + 2, len - 2);
}
//两个字节的rtp头
2020-10-18 21:39:22 +08:00
_offset = 2;
2020-08-15 09:48:27 +08:00
return onSearchPacketTail_l(data, len);
}
const char *RtpSplitter::onSearchPacketTail_l(const char *data, size_t len) {
2019-12-05 19:20:12 +08:00
//这是rtp包
2020-08-15 09:48:27 +08:00
uint16_t length = (((uint8_t *) data)[0] << 8) | ((uint8_t *) data)[1];
if (len < length + 2) {
2019-12-05 19:20:12 +08:00
//数据不够
return nullptr;
}
//返回rtp包末尾
return data + 2 + length;
}
2019-12-06 11:54:10 +08:00
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)