ZLMediaKit/src/RtspMuxer/RtspMuxer.cpp

109 lines
3.4 KiB
C++
Raw Normal View History

2018-10-25 10:00:17 +08:00
/*
* MIT License
*
* Copyright (c) 2016 xiongziliang <771730766@qq.com>
*
* 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.
*/
2018-10-24 09:45:57 +08:00
2018-10-24 18:09:54 +08:00
#include "RtspMuxer.h"
2018-10-24 09:45:57 +08:00
#include "Common/Factory.h"
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-10-24 09:45:57 +08:00
2018-10-25 23:17:30 +08:00
void RtspMuxer::addTrack(const Track::Ptr &track_in, uint32_t ssrc, int mtu) {
//克隆对象防止在setDelegate时错误覆盖
auto track = track_in->clone();
2018-10-25 11:53:45 +08:00
auto codec_id = track->getCodecId();
_track_map[codec_id] = track;
2018-10-25 13:43:41 +08:00
2018-10-25 11:53:45 +08:00
auto lam = [this,ssrc,mtu,track](){
2018-10-25 13:43:41 +08:00
//异步生成rtp编码器
//根据track生产sdp
2018-10-24 09:45:57 +08:00
Sdp::Ptr sdp = Factory::getSdpByTrack(track);
2018-10-25 11:53:45 +08:00
if (!sdp) {
return;
}
2018-10-25 13:43:41 +08:00
// 根据sdp生成rtp编码器
2018-10-25 11:53:45 +08:00
auto encoder = sdp->createRtpEncoder(ssrc ? ssrc : ((uint64_t) sdp.get()) & 0xFFFFFFFF, mtu);
if (!encoder) {
return;
}
2018-10-25 13:43:41 +08:00
//添加其sdp
_sdp.append(sdp->getSdp());
2018-10-25 11:53:45 +08:00
//设置Track的代理这样输入frame至Track时最终数据将输出到RtpEncoder中
track->setDelegate(encoder);
//rtp编码器共用同一个环形缓存
encoder->setRtpRing(_rtpRing);
};
if(track->ready()){
lam();
}else{
_trackReadyCallback[codec_id] = lam;
}
}
string RtspMuxer::getSdp() {
if(!_trackReadyCallback.empty()){
//尚未就绪
return "";
}
2018-10-25 13:43:41 +08:00
return _sdp;
2018-10-25 11:53:45 +08:00
}
void RtspMuxer::inputFrame(const Frame::Ptr &frame) {
auto codec_id = frame->getCodecId();
auto it = _track_map.find(codec_id);
if (it == _track_map.end()) {
return;
}
//Track是否准备好
auto ready = it->second->ready();
//inputFrame可能使Track变成就绪状态
it->second->inputFrame(frame);
if(!ready && it->second->ready()){
2018-10-25 13:43:41 +08:00
//Track由未就绪状态装换成就绪状态我们就生成sdp以及rtp编码器
2018-10-25 11:53:45 +08:00
auto it_callback = _trackReadyCallback.find(codec_id);
if(it_callback != _trackReadyCallback.end()){
it_callback->second();
_trackReadyCallback.erase(it_callback);
2018-10-24 09:45:57 +08:00
}
}
2018-10-25 15:45:38 +08:00
if(!_inited && _trackReadyCallback.empty()){
_inited = true;
onInited();
}
2018-10-24 09:45:57 +08:00
}
2018-10-25 11:53:45 +08:00
bool RtspMuxer::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
_rtpRing->write(rtp,key_pos);
2018-10-25 15:30:44 +08:00
return key_pos;
2018-10-25 11:53:45 +08:00
}
RtpRingInterface::RingType::Ptr RtspMuxer::getRtpRing() const {
return _rtpRing;
}
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */