RtpSplitter.cpp 2.31 KB
Newer Older
Gemfield committed
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
 */
Gemfield committed
10

11
#if defined(ENABLE_RTPPROXY)
12
#include <string.h>
Gemfield committed
13
#include "RtpSplitter.h"
14
namespace mediakit{
Gemfield committed
15

16 17
static const char kEHOME_MAGIC[] = "\x01\x00\x01\x00";
static const int  kEHOME_OFFSET = 256;
Gemfield committed
18

19
RtpSplitter::RtpSplitter() {}
20
RtpSplitter::~RtpSplitter() {}
Gemfield committed
21

22
ssize_t RtpSplitter::onRecvHeader(const char *data,size_t len){
23 24 25 26
    //忽略偏移量
    data += _offset;
    len -= _offset;

27
    if (_is_ehome && len > 12 && data[12] == '\r') {
28 29 30 31 32 33 34 35 36
        //这是ehome,移除第12个字节
        memmove((char *) data + 1, data, 12);
        data += 1;
        len -= 1;
    }
    onRtpPacket(data, len);
    return 0;
}

37
static bool isEhome(const char *data, size_t len){
38 39 40 41 42 43
    if (len < 4) {
        return false;
    }
    return memcmp(data, kEHOME_MAGIC, sizeof(kEHOME_MAGIC) - 1) == 0;
}

44
const char *RtpSplitter::onSearchPacketTail(const char *data, size_t len) {
xiongziliang committed
45 46 47 48
    if (len < 4) {
        //数据不够
        return nullptr;
    }
49 50 51 52 53 54 55 56 57

    if (isEhome(data, len)) {
        //是ehome协议
        if (len < kEHOME_OFFSET + 4) {
            //数据不够
            return nullptr;
        }
        //忽略ehome私有头后是rtsp样式的rtp,多4个字节,
        _offset = kEHOME_OFFSET + 4;
58
        _is_ehome = true;
59 60 61 62
        //忽略ehome私有头
        return onSearchPacketTail_l(data + kEHOME_OFFSET + 2, len - kEHOME_OFFSET - 2);
    }

63 64
    if (data[0] == '$') {
        //可能是4个字节的rtp头
xiongziliang committed
65
        _offset = 4;
66 67 68
        return onSearchPacketTail_l(data + 2, len - 2);
    }
    //两个字节的rtp头
xiongziliang committed
69
    _offset = 2;
70 71 72
    return onSearchPacketTail_l(data, len);
}

73
const char *RtpSplitter::onSearchPacketTail_l(const char *data, size_t len) {
Gemfield committed
74
    //这是rtp包
75
    uint16_t length = (((uint8_t *) data)[0] << 8) | ((uint8_t *) data)[1];
76
    if (len < (size_t)(length + 2)) {
Gemfield committed
77 78 79 80 81 82 83
        //数据不够
        return nullptr;
    }
    //返回rtp包末尾
    return data + 2 + length;
}

84 85
}//namespace mediakit
#endif//defined(ENABLE_RTPPROXY)