ZLMediaKit/src/Rtsp/RtspMuxer.cpp

113 lines
3.2 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
2020-04-04 20:30:09 +08:00
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
*
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
2020-04-04 20:30:09 +08:00
*
* 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.
*/
2018-10-24 09:45:57 +08:00
2018-10-24 18:09:54 +08:00
#include "RtspMuxer.h"
2018-10-30 14:59:42 +08:00
#include "Extension/Factory.h"
2018-10-24 09:45:57 +08:00
using namespace std;
using namespace toolkit;
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-10-24 09:45:57 +08:00
void RtspMuxer::onRtp(RtpPacket::Ptr in, bool is_key) {
2021-09-29 00:04:36 +08:00
if (_live) {
if (_rtp_stamp[in->type] != in->getHeader()->stamp) {
//rtp时间戳变化才计算ntp节省cpu资源
int64_t stamp_ms = in->getStamp() * uint64_t(1000) / in->sample_rate;
int64_t stamp_ms_inc;
//求rtp时间戳增量
_stamp[in->type].revise(stamp_ms, stamp_ms, stamp_ms_inc, stamp_ms_inc);
_rtp_stamp[in->type] = in->getHeader()->stamp;
_ntp_stamp[in->type] = stamp_ms_inc + _ntp_stamp_start;
}
2021-09-29 00:04:36 +08:00
//rtp拦截入口此处统一赋值ntp
in->ntp_stamp = _ntp_stamp[in->type];
} else {
2021-09-29 00:25:34 +08:00
//点播情况下设置ntp时间戳为rtp时间戳加基准ntp时间戳
in->ntp_stamp = _ntp_stamp_start + (in->getStamp() * uint64_t(1000) / in->sample_rate);
2021-09-29 00:04:36 +08:00
}
_rtpRing->write(std::move(in), is_key);
}
2021-07-14 21:41:26 +08:00
RtspMuxer::RtspMuxer(const TitleSdp::Ptr &title) {
if (!title) {
_sdp = std::make_shared<TitleSdp>()->getSdp();
2021-07-14 21:41:26 +08:00
} else {
2021-09-29 00:04:36 +08:00
_live = title->getDuration() == 0;
_sdp = title->getSdp();
2018-10-26 17:14:39 +08:00
}
2019-12-26 09:43:44 +08:00
_rtpRing = std::make_shared<RtpRing::RingType>();
_rtpInterceptor = std::make_shared<RtpRing::RingType>();
2021-07-16 15:54:43 +08:00
_rtpInterceptor->setDelegate(std::make_shared<RingDelegateHelper>([this](RtpPacket::Ptr in, bool is_key) {
onRtp(std::move(in), is_key);
}));
_ntp_stamp_start = getCurrentMillisecond(true);
}
2018-10-25 13:43:41 +08:00
bool RtspMuxer::addTrack(const Track::Ptr &track) {
2019-08-30 11:17:27 +08:00
//根据track生成sdp
2019-06-28 16:12:39 +08:00
Sdp::Ptr sdp = track->getSdp();
if (!sdp) {
return false;
}
2019-12-03 12:32:57 +08:00
auto &encoder = _encoder[track->getTrackType()];
encoder = Factory::getRtpEncoderBySdp(sdp);
if (!encoder) {
return false;
2018-10-25 11:53:45 +08:00
}
2019-12-03 12:32:57 +08:00
//设置rtp输出环形缓存
encoder->setRtpRing(_rtpInterceptor);
2019-12-03 12:32:57 +08:00
//添加其sdp
_sdp.append(sdp->getSdp());
trySyncTrack();
return true;
}
void RtspMuxer::trySyncTrack() {
if (_encoder[TrackAudio] && _encoder[TrackVideo]) {
//音频时间戳同步于视频,因为音频时间戳被修改后不影响播放
_stamp[TrackAudio].syncTo(_stamp[TrackVideo]);
}
2018-10-25 11:53:45 +08:00
}
bool RtspMuxer::inputFrame(const Frame::Ptr &frame) {
2019-12-03 12:32:57 +08:00
auto &encoder = _encoder[frame->getTrackType()];
return encoder ? encoder->inputFrame(frame) : false;
2019-12-03 12:32:57 +08:00
}
2022-10-16 19:49:56 +08:00
void RtspMuxer::flush() {
for (auto &encoder : _encoder) {
if (encoder) {
encoder->flush();
}
}
}
2019-12-03 12:32:57 +08:00
string RtspMuxer::getSdp() {
2018-10-25 13:43:41 +08:00
return _sdp;
2018-10-25 11:53:45 +08:00
}
2019-12-26 09:43:44 +08:00
RtpRing::RingType::Ptr RtspMuxer::getRtpRing() const {
2018-10-25 11:53:45 +08:00
return _rtpRing;
}
2019-12-03 12:32:57 +08:00
void RtspMuxer::resetTracks() {
_sdp.clear();
2021-07-14 21:41:26 +08:00
for (auto &encoder : _encoder) {
2019-12-03 12:32:57 +08:00
encoder = nullptr;
}
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */