RtpReceiver.cpp 6.72 KB
Newer Older
xiongziliang committed
1 2 3
/*
 * MIT License
 *
xiongziliang committed
4
 * Copyright (c) 2016-2019 xiongziliang <771730766@qq.com>
xiongziliang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * 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.
 */

#include "Common/config.h"
#include "RtpReceiver.h"

#define POP_HEAD(trackidx) \
31 32 33
        auto it = _rtp_sort_cache_map[trackidx].begin(); \
        onRtpSorted(it->second, trackidx); \
        _rtp_sort_cache_map[trackidx].erase(it);
xiongziliang committed
34

35
#define AV_RB16(x)                           \
xiongziliang committed
36 37 38
    ((((const uint8_t*)(x))[0] << 8) |          \
      ((const uint8_t*)(x))[1])

39
#define RTP_MAX_SIZE (10 * 1024)
xiongziliang committed
40 41 42 43 44 45

namespace mediakit {

RtpReceiver::RtpReceiver() {}
RtpReceiver::~RtpReceiver() {}

46
bool RtpReceiver::handleOneRtp(int track_index,SdpTrack::Ptr &track, unsigned char *rtp_raw_ptr, unsigned int rtp_raw_len) {
xiongziliang committed
47
    if(rtp_raw_len < 12){
48
        WarnL << "rtp包太小:" << rtp_raw_len;
xiongziliang committed
49 50
        return false;
    }
51 52 53 54 55 56 57 58 59 60 61

    uint8_t padding = 0;
    if (rtp_raw_ptr[0] & 0x40) {
        //获取padding大小
        padding = rtp_raw_ptr[rtp_raw_len - 1];
        //移除padding flag
        rtp_raw_ptr[0] &= ~0x40;
        //移除padding字节
        rtp_raw_len -= padding;
    }

62 63
    auto rtp_ptr = _rtp_pool.obtain();
    auto &rtp = *rtp_ptr;
xiongziliang committed
64

65
    rtp.type = track->_type;
66
    rtp.interleaved = 2 * track->_type;
67 68
    rtp.mark = rtp_raw_ptr[1] >> 7;
    rtp.PT = rtp_raw_ptr[1] & 0x7F;
69 70 71

    //序列号,内存对齐
    memcpy(&rtp.sequence, rtp_raw_ptr + 2, 2);
72
    rtp.sequence = ntohs(rtp.sequence);
73 74 75 76

    //时间戳,内存对齐
    memcpy(&rtp.timeStamp, rtp_raw_ptr + 4, 4);
    rtp.timeStamp = ntohl(rtp.timeStamp);
xiongziliang committed
77 78

    if(!track->_samplerate){
79
        //无法把时间戳转换成毫秒
xiongziliang committed
80 81
        return false;
    }
xiongziliang committed
82
    //时间戳转换成毫秒
83 84 85 86
    rtp.timeStamp = rtp.timeStamp * 1000LL / track->_samplerate;

    //ssrc,内存对齐
    memcpy(&rtp.ssrc, rtp_raw_ptr + 8, 4);
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    rtp.ssrc = ntohl(rtp.ssrc);

    if (track->_ssrc != rtp.ssrc) {
        if (track->_ssrc == 0) {
            //保存SSRC至track对象
            track->_ssrc = rtp.ssrc;
        }else{
            //ssrc错误
            WarnL << "ssrc错误:" << rtp.ssrc << " != " << track->_ssrc;
            if (_ssrc_err_count[track_index]++ > 10) {
                //ssrc切换后清除老数据
                WarnL << "ssrc更换:" << track->_ssrc << " -> " << rtp.ssrc;
                _rtp_sort_cache_map[track_index].clear();
                track->_ssrc = rtp.ssrc;
            }
            return false;
xiongziliang committed
103 104
        }
    }
105 106 107

    //ssrc匹配正确,不匹配计数清零
    _ssrc_err_count[track_index] = 0;
xiongziliang committed
108

xiongziliang committed
109
    //获取rtp中媒体数据偏移量
110 111 112 113 114
    rtp.offset 	= 12 + 4;
    int csrc     	= rtp_raw_ptr[0] & 0x0f;
    int ext      	= rtp_raw_ptr[0] & 0x10;
    rtp.offset 	+= 4 * csrc;
    if (ext && rtp_raw_len >= rtp.offset) {
xiongziliang committed
115
        /* calculate the header extension length (stored as number of 32-bit words) */
116 117
        ext = (AV_RB16(rtp_raw_ptr + rtp.offset - 2) + 1) << 2;
        rtp.offset += ext;
xiongziliang committed
118
    }
119

120
    if(rtp_raw_len + 4 <= rtp.offset){
121
        WarnL << "无有效负载的rtp包:" << rtp_raw_len << " <= " << (int)rtp.offset;
122 123 124
        return false;
    }

125 126
    if(rtp_raw_len > RTP_MAX_SIZE){
        WarnL << "超大的rtp包:" << rtp_raw_len << " > " << RTP_MAX_SIZE;
127 128 129 130
        return false;
    }

    //设置rtp负载长度
131 132
    rtp.setCapacity(rtp_raw_len + 4);
    rtp.setSize(rtp_raw_len + 4);
133
    uint8_t *payload_ptr = (uint8_t *)rtp.data();
xiongziliang committed
134
    payload_ptr[0] = '$';
135 136 137
    payload_ptr[1] = rtp.interleaved;
    payload_ptr[2] = rtp_raw_len >> 8;
    payload_ptr[3] = (rtp_raw_len & 0x00FF);
xiongziliang committed
138
    //拷贝rtp负载
139 140 141 142 143
    memcpy(payload_ptr + 4, rtp_raw_ptr, rtp_raw_len);
    //排序rtp
    sortRtp(rtp_ptr,track_index);
    return true;
}
xiongziliang committed
144

145 146
void RtpReceiver::sortRtp(const RtpPacket::Ptr &rtp,int track_index){
    if(rtp->sequence != _last_seq[track_index] + 1 && _last_seq[track_index] != 0){
xiongziliang committed
147
        //包乱序或丢包
148 149 150
        _seq_ok_count[track_index] = 0;
        _sort_started[track_index] = true;
        if(_last_seq[track_index] > rtp->sequence && _last_seq[track_index] - rtp->sequence > 0xFF){
151
            //sequence回环,清空所有排序缓存
152 153
            while (_rtp_sort_cache_map[track_index].size()) {
                POP_HEAD(track_index)
154
            }
155
            ++_seq_cycle_count[track_index];
156
        }
xiongziliang committed
157 158
    }else{
        //正确序列的包
159
        _seq_ok_count[track_index]++;
xiongziliang committed
160
    }
161 162

    _last_seq[track_index] = rtp->sequence;
xiongziliang committed
163 164

    //开始排序缓存
165 166
    if (_sort_started[track_index]) {
        _rtp_sort_cache_map[track_index].emplace(rtp->sequence, rtp);
167 168
        GET_CONFIG(uint32_t,clearCount,Rtp::kClearCount);
        GET_CONFIG(uint32_t,maxRtpCount,Rtp::kMaxRtpCount);
169
        if (_seq_ok_count[track_index] >= clearCount) {
xiongziliang committed
170
            //网络环境改善,需要清空排序缓存
171 172 173 174
            _seq_ok_count[track_index] = 0;
            _sort_started[track_index] = false;
            while (_rtp_sort_cache_map[track_index].size()) {
                POP_HEAD(track_index)
xiongziliang committed
175
            }
176
        } else if (_rtp_sort_cache_map[track_index].size() >= maxRtpCount) {
xiongziliang committed
177
            //排序缓存溢出
178
            POP_HEAD(track_index)
xiongziliang committed
179 180 181
        }
    }else{
        //正确序列
182
        onRtpSorted(rtp, track_index);
xiongziliang committed
183 184 185 186
    }
}

void RtpReceiver::clear() {
187 188 189 190 191 192 193 194
    CLEAR_ARR(_last_seq)
    CLEAR_ARR(_ssrc_err_count)
    CLEAR_ARR(_seq_ok_count)
    CLEAR_ARR(_sort_started)
    CLEAR_ARR(_seq_cycle_count)

    _rtp_sort_cache_map[0].clear();
    _rtp_sort_cache_map[1].clear();
xiongziliang committed
195 196 197
}

void RtpReceiver::setPoolSize(int size) {
198
    _rtp_pool.setSize(size);
xiongziliang committed
199 200
}

xiongziliang committed
201 202
int RtpReceiver::getJitterSize(int track_index){
    return _rtp_sort_cache_map[track_index].size();
xiongziliang committed
203 204
}

xiongziliang committed
205 206
int RtpReceiver::getCycleCount(int track_index){
    return _seq_cycle_count[track_index];
xiongziliang committed
207 208 209
}


xiongziliang committed
210
}//namespace mediakit