RtspSplitter.cpp 2.19 KB
Newer Older
1
/*
xiongziliang committed
2
 * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
3
 *
4
 * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
5
 *
xiongziliang committed
6 7 8
 * 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.
9 10
 */

11
#include <cstdlib>
12
#include "RtspSplitter.h"
13 14
#include "Util/logger.h"
#include "Util/util.h"
15

夏楚 committed
16 17 18
using namespace std;
using namespace toolkit;

19 20
namespace mediakit{

21
const char *RtspSplitter::onSearchPacketTail(const char *data, size_t len) {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
    auto ret = onSearchPacketTail_l(data, len);
    if(ret){
        return ret;
    }

    if (len > 256 * 1024) {
        //rtp大于256KB
        ret = (char *) memchr(data, '$', len);
        if (!ret) {
            WarnL << "rtp缓存溢出:" << hexdump(data, 1024);
            reset();
        }
    }
    return ret;
}

38
const char *RtspSplitter::onSearchPacketTail_l(const char *data, size_t len) {
xiongziliang committed
39
    if(!_enableRecvRtp || data[0] != '$'){
40 41 42 43 44 45 46 47 48 49
        //这是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];
50
    if(len < (size_t)(length + 4)){
51 52 53 54 55 56 57 58
        //数据不够
        return nullptr;
    }
    //返回rtp包末尾
    _isRtpPacket = true;
    return data + 4 + length;
}

59
ssize_t RtspSplitter::onRecvHeader(const char *data, size_t len) {
60 61 62 63 64
    if(_isRtpPacket){
        onRtpPacket(data,len);
        return 0;
    }
    _parser.Parse(data);
xiongziliang committed
65
    auto ret = getContentLength(_parser);
66 67
    if(ret == 0){
        onWholeRtspPacket(_parser);
xiongziliang committed
68
        _parser.Clear();
69 70 71 72
    }
    return ret;
}

73
void RtspSplitter::onRecvContent(const char *data, size_t len) {
74 75
    _parser.setContent(string(data,len));
    onWholeRtspPacket(_parser);
xiongziliang committed
76
    _parser.Clear();
77 78 79 80 81 82
}

void RtspSplitter::enableRecvRtp(bool enable) {
    _enableRecvRtp = enable;
}

83
ssize_t RtspSplitter::getContentLength(Parser &parser) {
xiongziliang committed
84 85 86
    return atoi(parser["Content-Length"].data());
}

87 88 89 90 91

}//namespace mediakit