RtspSplitter.cpp 2.14 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 16 17

namespace mediakit{

18
const char *RtspSplitter::onSearchPacketTail(const char *data, size_t len) {
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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;
}

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

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

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

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

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

84 85 86 87 88

}//namespace mediakit