From 1ff490d119a6149eb0d4d2cd1742e1577cf72f01 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Thu, 25 Oct 2018 16:46:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90MediaSource=E7=94=9F=E6=88=90?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ZLToolKit | 2 +- src/Common/MultiMediaSourceMuxer.h | 68 +++++++++++++++++++ src/Rtmp/RtmpMediaSource.h | 6 +- .../RtmpMediaSourceMuxer.h} | 42 +++++++++++- src/RtmpMuxer/RtmpMuxer.h | 10 ++- src/Rtsp/RtspMediaSource.h | 7 +- .../RtspMediaSourceMuxer.h} | 37 +++++++--- src/RtspMuxer/RtspMuxer.h | 10 ++- 8 files changed, 161 insertions(+), 21 deletions(-) create mode 100644 src/Common/MultiMediaSourceMuxer.h rename src/{Device/MediaSourceMaker.cpp => RtmpMuxer/RtmpMediaSourceMuxer.h} (54%) rename src/{Device/MediaSourceMaker.h => RtspMuxer/RtspMediaSourceMuxer.h} (58%) diff --git a/ZLToolKit b/ZLToolKit index 6e69a082..3a9c9164 160000 --- a/ZLToolKit +++ b/ZLToolKit @@ -1 +1 @@ -Subproject commit 6e69a082a2f6e6161785b00c7421bf1a811ed34a +Subproject commit 3a9c916454897ad02c6ee4cba89f5e370718cd91 diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h new file mode 100644 index 00000000..1c443739 --- /dev/null +++ b/src/Common/MultiMediaSourceMuxer.h @@ -0,0 +1,68 @@ +/* + * 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_MULTIMEDIASOURCEMUXER_H +#define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H + +#include "RtspMuxer/RtspMediaSourceMuxer.h" +#include "RtmpMuxer/RtmpMediaSourceMuxer.h" + +class MultiMediaSourceMuxer { +public: + MultiMediaSourceMuxer(const string &vhost, + const string &strApp, + const string &strId, + float dur_sec = 0.0){ + _rtmp = std::make_shared(vhost,strApp,strId,std::make_shared(dur_sec)); + _rtsp = std::make_shared(vhost,strApp,strId,std::make_shared(dur_sec)); + } + virtual ~MultiMediaSourceMuxer(){} + + + /** + * 添加音视频媒体 + * @param track 媒体描述 + */ + void addTrack(const Track::Ptr & track,int mtu = 1400) { + _rtmp->addTrack(track); + _rtsp->addTrack(track,0,mtu); + } + + /** + * 写入帧数据然后打包rtmp + * @param frame 帧数据 + */ + void inputFrame(const Frame::Ptr &frame) { + _rtmp->inputFrame(frame); + _rtsp->inputFrame(frame); + } +private: + RtmpMediaSourceMuxer::Ptr _rtmp; + RtspMediaSourceMuxer::Ptr _rtsp; +}; + + +#endif //ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H diff --git a/src/Rtmp/RtmpMediaSource.h b/src/Rtmp/RtmpMediaSource.h index 842fd734..0db2e733 100644 --- a/src/Rtmp/RtmpMediaSource.h +++ b/src/Rtmp/RtmpMediaSource.h @@ -48,7 +48,7 @@ using namespace toolkit; namespace mediakit { -class RtmpMediaSource: public MediaSource { +class RtmpMediaSource: public MediaSource ,public RingDelegate { public: typedef std::shared_ptr Ptr; typedef RingBuffer RingType; @@ -107,6 +107,10 @@ private: lock_guard lock(_mtxMap); return _iCfgFrameSize != -1 && _iCfgFrameSize == _mapCfgFrame.size(); } + + void onWrite(const RtmpPacket::Ptr &pkt,bool isKey = true) override { + onGetMedia(pkt); + } protected: AMFValue _metadata; unordered_map _mapCfgFrame; diff --git a/src/Device/MediaSourceMaker.cpp b/src/RtmpMuxer/RtmpMediaSourceMuxer.h similarity index 54% rename from src/Device/MediaSourceMaker.cpp rename to src/RtmpMuxer/RtmpMediaSourceMuxer.h index a95fe470..55b47e02 100644 --- a/src/Device/MediaSourceMaker.cpp +++ b/src/RtmpMuxer/RtmpMediaSourceMuxer.h @@ -24,7 +24,43 @@ * SOFTWARE. */ -#include "MediaSourceMaker.h" -namespace mediakit{ +#ifndef ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H +#define ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H -} \ No newline at end of file +#include "RtmpMuxer/RtmpMuxer.h" +#include "Rtmp/RtmpMediaSource.h" + +namespace mediakit { + +class RtmpMediaSourceMuxer : public RtmpMuxer { +public: + typedef std::shared_ptr Ptr; + + RtmpMediaSourceMuxer(const string &vhost, + const string &strApp, + const string &strId, + const TitleMete::Ptr &title = nullptr) : RtmpMuxer(title){ + _mediaSouce = std::make_shared(vhost,strApp,strId); + getRtmpRing()->setDelegate(_mediaSouce); + } + virtual ~RtmpMediaSourceMuxer(){} + + void setListener(const std::weak_ptr &listener){ + _mediaSouce->setListener(listener); + } +private: + void onInited() override { + _mediaSouce->onGetMetaData(getMetedata()); + } +private: + RtmpMediaSource::Ptr _mediaSouce; +}; + + + + + + + +}//namespace mediakit +#endif //ZLMEDIAKIT_RTMPMEDIASOURCEMUXER_H diff --git a/src/RtmpMuxer/RtmpMuxer.h b/src/RtmpMuxer/RtmpMuxer.h index 770aa06b..c4271746 100644 --- a/src/RtmpMuxer/RtmpMuxer.h +++ b/src/RtmpMuxer/RtmpMuxer.h @@ -33,11 +33,17 @@ namespace mediakit{ class RtmpMuxer{ public: + typedef std::shared_ptr Ptr; + /** * 构造函数 */ - RtmpMuxer(const TitleMete::Ptr &title = std::make_shared()) : _metedata(AMF_OBJECT){ - _metedata = title->getMetedata(); + RtmpMuxer(const TitleMete::Ptr &title = nullptr) : _metedata(AMF_OBJECT){ + if(!title){ + _metedata = std::make_shared()->getMetedata(); + }else{ + _metedata = title->getMetedata(); + } _rtmpRing = std::make_shared(); } virtual ~RtmpMuxer(){} diff --git a/src/Rtsp/RtspMediaSource.h b/src/Rtsp/RtspMediaSource.h index 5d4494cb..db5ee444 100644 --- a/src/Rtsp/RtspMediaSource.h +++ b/src/Rtsp/RtspMediaSource.h @@ -49,7 +49,7 @@ using namespace toolkit; namespace mediakit { -class RtspMediaSource: public MediaSource { +class RtspMediaSource: public MediaSource , public RingDelegate { public: typedef ResourcePool PoolType; typedef std::shared_ptr Ptr; @@ -93,6 +93,11 @@ public: trackRef.type = rtppt->type; _pRing->write(rtppt,keyPos); } + +private: + void onWrite(const RtpPacket::Ptr &rtppt, bool keyPos) override { + onGetRTP(rtppt,keyPos); + } protected: unordered_map _mapTracks; string _strSdp; //媒体描述信息 diff --git a/src/Device/MediaSourceMaker.h b/src/RtspMuxer/RtspMediaSourceMuxer.h similarity index 58% rename from src/Device/MediaSourceMaker.h rename to src/RtspMuxer/RtspMediaSourceMuxer.h index 3265589b..11138465 100644 --- a/src/Device/MediaSourceMaker.h +++ b/src/RtspMuxer/RtspMediaSourceMuxer.h @@ -24,23 +24,38 @@ * SOFTWARE. */ -#ifndef ZLMEDIAKIT_MEDIASOURCEMAKER_H -#define ZLMEDIAKIT_MEDIASOURCEMAKER_H +#ifndef ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H +#define ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H -#include "Player/Track.h" +#include "RtspMuxer/RtspMuxer.h" #include "Rtsp/RtspMediaSource.h" -#include "Rtmp/RtmpMediaSource.h" namespace mediakit { -class MediaSourceMaker { +class RtspMediaSourceMuxer : public RtspMuxer { public: - MediaSourceMaker() {} - virtual ~MediaSourceMaker() {} + typedef std::shared_ptr Ptr; + + RtspMediaSourceMuxer(const string &vhost, + const string &strApp, + const string &strId, + const TitleSdp::Ptr &title = nullptr) : RtspMuxer(title){ + _mediaSouce = std::make_shared(vhost,strApp,strId); + getRtpRing()->setDelegate(_mediaSouce); + } + virtual ~RtspMediaSourceMuxer(){} + + void setListener(const std::weak_ptr &listener){ + _mediaSouce->setListener(listener); + } private: - RtspMediaSource::Ptr _rtspSrc; - RtmpMediaSource::Ptr _rtmpSrc; + void onInited() override { + _mediaSouce->onGetSDP(getSdp()); + } +private: + RtspMediaSource::Ptr _mediaSouce; }; -} //namespace mediakit -#endif //ZLMEDIAKIT_MEDIASOURCEMAKER_H + +}//namespace mediakit +#endif //ZLMEDIAKIT_RTSPMEDIASOURCEMUXER_H diff --git a/src/RtspMuxer/RtspMuxer.h b/src/RtspMuxer/RtspMuxer.h index 05d90003..acdada97 100644 --- a/src/RtspMuxer/RtspMuxer.h +++ b/src/RtspMuxer/RtspMuxer.h @@ -35,11 +35,17 @@ namespace mediakit{ */ class RtspMuxer{ public: + typedef std::shared_ptr Ptr; + /** * 构造函数 */ - RtspMuxer(const TitleSdp::Ptr &title = std::make_shared()){ - _sdp = title->getSdp(); + RtspMuxer(const TitleSdp::Ptr &title = nullptr){ + if(!title){ + _sdp = std::make_shared()->getSdp(); + } else{ + _sdp = title->getSdp(); + } _rtpRing = std::make_shared(); } virtual ~RtspMuxer(){}