2020-08-01 10:24:28 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2020-08-01 10:22:12 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2020-08-01 10:22:12 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-08-01 10:22:12 +08:00
|
|
|
|
* 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.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "CommonRtp.h"
|
|
|
|
|
|
2022-02-02 20:34:50 +08:00
|
|
|
|
using namespace mediakit;
|
|
|
|
|
|
2021-01-19 16:05:38 +08:00
|
|
|
|
CommonRtpDecoder::CommonRtpDecoder(CodecId codec, size_t max_frame_size ){
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_codec = codec;
|
2020-09-06 17:50:24 +08:00
|
|
|
|
_max_frame_size = max_frame_size;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
obtainFrame();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void CommonRtpDecoder::obtainFrame() {
|
2021-02-05 11:51:16 +08:00
|
|
|
|
_frame = FrameImp::create();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_frame->_codec_id = _codec;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool CommonRtpDecoder::inputRtp(const RtpPacket::Ptr &rtp, bool){
|
2022-05-28 10:34:54 +08:00
|
|
|
|
auto payload_size = rtp->getPayloadSize();
|
|
|
|
|
if (payload_size <= 0) {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
//无实际负载
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-05-28 10:34:54 +08:00
|
|
|
|
auto payload = rtp->getPayload();
|
2023-06-28 14:56:24 +08:00
|
|
|
|
auto stamp = rtp->getStamp();
|
2022-05-28 10:34:54 +08:00
|
|
|
|
auto seq = rtp->getSeq();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
|
2023-06-28 14:56:24 +08:00
|
|
|
|
if (_last_stamp != stamp || _frame->_buffer.size() > _max_frame_size) {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
//时间戳发生变化或者缓存超过MAX_FRAME_SIZE,则清空上帧数据
|
|
|
|
|
if (!_frame->_buffer.empty()) {
|
|
|
|
|
//有有效帧,则输出
|
|
|
|
|
RtpCodec::inputFrame(_frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//新的一帧数据
|
|
|
|
|
obtainFrame();
|
2023-06-28 14:56:24 +08:00
|
|
|
|
_frame->_dts = rtp->getStampMS();
|
|
|
|
|
_last_stamp = stamp;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_drop_flag = false;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
} else if (_last_seq != 0 && (uint16_t)(_last_seq + 1) != seq) {
|
2020-08-01 10:22:12 +08:00
|
|
|
|
//时间戳未发生变化,但是seq却不连续,说明中间rtp丢包了,那么整帧应该废弃
|
2021-01-31 19:19:24 +08:00
|
|
|
|
WarnL << "rtp丢包:" << _last_seq << " -> " << seq;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
_drop_flag = true;
|
|
|
|
|
_frame->_buffer.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!_drop_flag) {
|
2022-05-28 10:34:54 +08:00
|
|
|
|
_frame->_buffer.append((char *)payload, payload_size);
|
2020-08-01 10:22:12 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-01-31 19:19:24 +08:00
|
|
|
|
_last_seq = seq;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////
|
|
|
|
|
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool CommonRtpEncoder::inputFrame(const Frame::Ptr &frame){
|
2021-04-11 12:07:04 +08:00
|
|
|
|
auto stamp = frame->pts();
|
2020-08-01 10:22:12 +08:00
|
|
|
|
auto ptr = frame->data() + frame->prefixSize();
|
|
|
|
|
auto len = frame->size() - frame->prefixSize();
|
|
|
|
|
auto remain_size = len;
|
2023-12-09 16:23:51 +08:00
|
|
|
|
auto max_size = getRtpInfo().getMaxSize();
|
2022-07-09 22:30:43 +08:00
|
|
|
|
bool is_key = frame->keyFrame();
|
2021-01-31 18:48:28 +08:00
|
|
|
|
bool mark = false;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
while (remain_size > 0) {
|
2021-01-31 18:48:28 +08:00
|
|
|
|
size_t rtp_size;
|
2021-01-31 19:19:24 +08:00
|
|
|
|
if (remain_size > max_size) {
|
|
|
|
|
rtp_size = max_size;
|
2021-01-31 18:48:28 +08:00
|
|
|
|
} else {
|
|
|
|
|
rtp_size = remain_size;
|
|
|
|
|
mark = true;
|
|
|
|
|
}
|
2023-12-09 16:23:51 +08:00
|
|
|
|
RtpCodec::inputRtp(getRtpInfo().makeRtp(frame->getTrackType(), ptr, rtp_size, mark, stamp), is_key);
|
2020-08-01 10:22:12 +08:00
|
|
|
|
ptr += rtp_size;
|
|
|
|
|
remain_size -= rtp_size;
|
2022-07-09 22:30:43 +08:00
|
|
|
|
is_key = false;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
}
|
2021-09-27 13:12:53 +08:00
|
|
|
|
return len > 0;
|
2020-08-01 10:22:12 +08:00
|
|
|
|
}
|