diff --git a/src/RtmpMuxer/RtmpMetedata.h b/src/RtmpMuxer/RtmpMetedata.h index 86bd094b..ea0b8114 100644 --- a/src/RtmpMuxer/RtmpMetedata.h +++ b/src/RtmpMuxer/RtmpMetedata.h @@ -42,6 +42,7 @@ namespace mediakit { class Metedata : public CodecInfo{ public: typedef std::shared_ptr 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 Ptr; + TitleMete(float dur_sec = 0, uint64_t fileSize = 0, const map &header = map()){ @@ -86,6 +89,8 @@ public: class VideoMete : public Metedata{ public: + typedef std::shared_ptr 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 Ptr; + AudioMete(const AudioTrack::Ptr &audio,int datarate = 160){ _metedata.set("audiodatarate", datarate); _metedata.set("audiosamplerate", audio->getAudioSampleRate()); diff --git a/src/RtmpMuxer/RtmpMuxer.cpp b/src/RtmpMuxer/RtmpMuxer.cpp new file mode 100644 index 00000000..4d1a61e4 --- /dev/null +++ b/src/RtmpMuxer/RtmpMuxer.cpp @@ -0,0 +1,115 @@ +/* +* 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(dynamic_pointer_cast(track)); + } + break; + case TrackAudio:{ + metedate = std::make_shared(dynamic_pointer_cast(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 diff --git a/src/RtmpMuxer/RtmpMuxer.h b/src/RtmpMuxer/RtmpMuxer.h new file mode 100644 index 00000000..d0f2ae71 --- /dev/null +++ b/src/RtmpMuxer/RtmpMuxer.h @@ -0,0 +1,117 @@ +/* +* 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()) : _metedata(AMF_OBJECT){ + _metedata = title->getMetedata(); + _rtmpRing = std::make_shared(); + } + 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 _track_map; + map > _trackReadyCallback; + RtmpRingInterface::RingType::Ptr _rtmpRing; + AMFValue _metedata; +}; + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} /* namespace mediakit */ + +#endif //ZLMEDIAKIT_RTMPMUXER_H diff --git a/src/RtspMuxer/RtspMuxer.cpp b/src/RtspMuxer/RtspMuxer.cpp index 6bf7f3e7..62418df6 100644 --- a/src/RtspMuxer/RtspMuxer.cpp +++ b/src/RtspMuxer/RtspMuxer.cpp @@ -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 { diff --git a/src/RtspMuxer/RtspMuxer.h b/src/RtspMuxer/RtspMuxer.h index 89185753..5cc1ece4 100644 --- a/src/RtspMuxer/RtspMuxer.h +++ b/src/RtspMuxer/RtspMuxer.h @@ -45,7 +45,7 @@ public: virtual ~RtspMuxer(){} /** - * 添加音视频或title 媒体 + * 添加音视频媒体 * @param track 媒体描述 * @param ssrc 媒体rtp ssrc * @param mtu 媒体rtp mtu