Commit 2504b05e by xiongziliang

完成rtmp复用器

parent fd65c4b6
......@@ -42,6 +42,7 @@ namespace mediakit {
class Metedata : public CodecInfo{
public:
typedef std::shared_ptr<Metedata> Ptr;
Metedata():_metedata(AMF_OBJECT){}
virtual ~Metedata(){}
const AMFValue &getMetedata() const{
......@@ -56,6 +57,8 @@ protected:
*/
class TitleMete : public Metedata{
public:
typedef std::shared_ptr<TitleMete> Ptr;
TitleMete(float dur_sec = 0,
uint64_t fileSize = 0,
const map<string,string> &header = map<string,string>()){
......@@ -86,6 +89,8 @@ public:
class VideoMete : public Metedata{
public:
typedef std::shared_ptr<VideoMete> Ptr;
VideoMete(const VideoTrack::Ptr &video,int datarate = 5000){
_metedata.set("width", video->getVideoWidth());
_metedata.set("height", video->getVideoHeight());
......@@ -118,6 +123,8 @@ private:
class AudioMete : public Metedata{
public:
typedef std::shared_ptr<AudioMete> Ptr;
AudioMete(const AudioTrack::Ptr &audio,int datarate = 160){
_metedata.set("audiodatarate", datarate);
_metedata.set("audiosamplerate", audio->getAudioSampleRate());
......
/*
* 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.
*/
#include "RtmpMuxer.h"
namespace mediakit {
void RtmpMuxer::addTrack(const Track::Ptr &track) {
//记录该Track
auto codec_id = track->getCodecId();
_track_map[codec_id] = track;
auto lam = [this,track](){
//异步生成Rtmp编码器
auto encoder = Factory::getRtmpCodecByTrack(track);
if (!encoder) {
return;
}
//根据track生产sdp
Metedata::Ptr metedate;
switch (track->getTrackType()){
case TrackVideo:{
metedate = std::make_shared<VideoMete>(dynamic_pointer_cast<VideoTrack>(track));
}
break;
case TrackAudio:{
metedate = std::make_shared<AudioMete>(dynamic_pointer_cast<AudioTrack>(track));
}
break;
default:
return;;
}
//添加其metedata
metedate->getMetedata().object_for_each([&](const std::string &key, const AMFValue &value){
_metedata.set(key,value);
});
//设置Track的代理,这样输入frame至Track时,最终数据将输出到RtmpEncoder中
track->setDelegate(encoder);
//Rtmp编码器共用同一个环形缓存
encoder->setRtmpRing(_rtmpRing);
};
if(track->ready()){
lam();
}else{
_trackReadyCallback[codec_id] = lam;
}
}
const AMFValue &RtmpMuxer::getMetedata() const {
if(!_trackReadyCallback.empty()){
//尚未就绪
static AMFValue s_amf;
return s_amf;
}
return _metedata;
}
void RtmpMuxer::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()){
//Track由未就绪状态装换成就绪状态,我们就生成sdp以及Rtmp编码器
auto it_callback = _trackReadyCallback.find(codec_id);
if(it_callback != _trackReadyCallback.end()){
it_callback->second();
_trackReadyCallback.erase(it_callback);
}
}
}
bool RtmpMuxer::inputRtmp(const RtmpPacket::Ptr &rtmp , bool key_pos) {
_rtmpRing->write(rtmp,key_pos);
return key_pos;
}
RtmpRingInterface::RingType::Ptr RtmpMuxer::getRtmpRing() const {
return _rtmpRing;
}
}
\ No newline at end of file
/*
* 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.
*/
#ifndef ZLMEDIAKIT_RTMPMUXER_H
#define ZLMEDIAKIT_RTMPMUXER_H
#include "RtmpMetedata.h"
namespace mediakit{
class RtmpMuxer{
public:
/**
* 构造函数
*/
RtmpMuxer(const TitleMete::Ptr &title = std::make_shared<TitleMete>()) : _metedata(AMF_OBJECT){
_metedata = title->getMetedata();
_rtmpRing = std::make_shared<RtmpRingInterface::RingType>();
}
virtual ~RtmpMuxer(){}
/**
* 添加音视频媒体
* @param track 媒体描述
*/
void addTrack(const Track::Ptr & track) ;
/**
* 获取完整的SDP字符串
* @return SDP字符串
*/
const AMFValue &getMetedata() const ;
/**
* 写入帧数据然后打包rtmp
* @param frame 帧数据
*/
void inputFrame(const Frame::Ptr &frame) ;
/**
* 也可以在外部打包好rtmp然后再写入
* @param rtmp rtmp包
* @param key_pos 是否为关键帧
*/
bool inputRtmp(const RtmpPacket::Ptr &rtmp, bool key_pos = true);
/**
* 获取rtmp环形缓存
* @return
*/
RtmpRingInterface::RingType::Ptr getRtmpRing() const;
private:
map<int,Track::Ptr> _track_map;
map<int,function<void()> > _trackReadyCallback;
RtmpRingInterface::RingType::Ptr _rtmpRing;
AMFValue _metedata;
};
} /* namespace mediakit */
#endif //ZLMEDIAKIT_RTMPMUXER_H
......@@ -93,7 +93,7 @@ void RtspMuxer::inputFrame(const Frame::Ptr &frame) {
bool RtspMuxer::inputRtp(const RtpPacket::Ptr &rtp, bool key_pos) {
_rtpRing->write(rtp,key_pos);
return true;
return key_pos;
}
RtpRingInterface::RingType::Ptr RtspMuxer::getRtpRing() const {
......
......@@ -45,7 +45,7 @@ public:
virtual ~RtspMuxer(){}
/**
* 添加音视频或title 媒体
* 添加音视频媒体
* @param track 媒体描述
* @param ssrc 媒体rtp ssrc
* @param mtu 媒体rtp mtu
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论