ZLMediaKit/src/Rtsp/RtspSplitter.cpp

90 lines
2.6 KiB
C++
Raw Normal View History

2018-10-30 10:31:27 +08:00
/*
* MIT License
*
2019-05-08 15:40:07 +08:00
* Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
2018-10-30 10:31:27 +08:00
*
* 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.
*/
2019-01-16 14:26:06 +08:00
#include <cstdlib>
2018-10-30 10:31:27 +08:00
#include "RtspSplitter.h"
namespace mediakit{
const char *RtspSplitter::onSearchPacketTail(const char *data, int len) {
if(!_enableRecvRtp){
_isRtpPacket = false;
return HttpRequestSplitter::onSearchPacketTail(data, len);
}
if(data[0] != '$'){
//这是rtsp包
_isRtpPacket = false;
return HttpRequestSplitter::onSearchPacketTail(data, len);
}
//这是rtp包
if(len < 4){
//数据不够
return nullptr;
}
uint16_t length = (((uint8_t *)data)[2] << 8) | ((uint8_t *)data)[3];
if(len < length + 4){
//数据不够
return nullptr;
}
//返回rtp包末尾
_isRtpPacket = true;
return data + 4 + length;
}
int64_t RtspSplitter::onRecvHeader(const char *data, uint64_t len) {
if(_isRtpPacket){
onRtpPacket(data,len);
return 0;
}
_parser.Parse(data);
2018-12-17 15:21:23 +08:00
auto ret = getContentLength(_parser);
2018-10-30 10:31:27 +08:00
if(ret == 0){
onWholeRtspPacket(_parser);
2018-12-17 15:21:23 +08:00
_parser.Clear();
2018-10-30 10:31:27 +08:00
}
return ret;
}
void RtspSplitter::onRecvContent(const char *data, uint64_t len) {
_parser.setContent(string(data,len));
onWholeRtspPacket(_parser);
2018-12-17 15:21:23 +08:00
_parser.Clear();
2018-10-30 10:31:27 +08:00
}
void RtspSplitter::enableRecvRtp(bool enable) {
_enableRecvRtp = enable;
}
2018-12-17 15:21:23 +08:00
int64_t RtspSplitter::getContentLength(Parser &parser) {
return atoi(parser["Content-Length"].data());
}
2018-10-30 10:31:27 +08:00
}//namespace mediakit