diff --git a/server/FFmpegSource.cpp b/server/FFmpegSource.cpp index cea3cdc2..21e87b41 100644 --- a/server/FFmpegSource.cpp +++ b/server/FFmpegSource.cpp @@ -245,6 +245,18 @@ bool FFmpegSource::close(MediaSource &sender, bool force) { return true; } +MediaOriginType FFmpegSource::getOriginType(MediaSource &sender) const{ + return MediaOriginType::ffmpeg_pull; +} + +string FFmpegSource::getOriginUrl(MediaSource &sender) const{ + return _src_url; +} + +std::shared_ptr FFmpegSource::getOriginSock(MediaSource &sender) const { + return nullptr; +} + void FFmpegSource::onGetMediaSource(const MediaSource::Ptr &src) { auto listener = src->getListener(); if (listener.lock().get() != this) { diff --git a/server/FFmpegSource.h b/server/FFmpegSource.h index 02fc288c..1b9c1e9f 100644 --- a/server/FFmpegSource.h +++ b/server/FFmpegSource.h @@ -58,8 +58,15 @@ private: void startTimer(int timeout_ms); void onGetMediaSource(const MediaSource::Ptr &src); - //MediaSourceEvent override + ///////MediaSourceEvent override/////// + // 关闭 bool close(MediaSource &sender,bool force) override; + // 获取媒体源类型 + MediaOriginType getOriginType(MediaSource &sender) const override; + //获取媒体源url或者文件路径 + string getOriginUrl(MediaSource &sender) const override; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock(MediaSource &sender) const override; private: Process _process; diff --git a/server/WebApi.cpp b/server/WebApi.cpp index 8a81deff..4fe3eef8 100644 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -397,6 +397,20 @@ void installWebApi() { item["stream"] = media->getId(); item["readerCount"] = media->readerCount(); item["totalReaderCount"] = media->totalReaderCount(); + item["originType"] = (int) media->getOriginType(); + item["originTypeStr"] = getOriginTypeString(media->getOriginType()); + item["originUrl"] = media->getOriginUrl(); + auto originSock = media->getOriginSock(); + if (originSock) { + item["originSock"]["local_ip"] = originSock->get_local_ip(); + item["originSock"]["local_port"] = originSock->get_local_port(); + item["originSock"]["peer_ip"] = originSock->get_peer_ip(); + item["originSock"]["peer_port"] = originSock->get_peer_port(); + item["originSock"]["identifier"] = originSock->getIdentifier(); + } else { + item["originSock"] = Json::nullValue; + } + for(auto &track : media->getTracks()){ Value obj; auto codec_type = track->getTrackType(); diff --git a/src/Common/Device.cpp b/src/Common/Device.cpp index 0b08289d..50aa4407 100644 --- a/src/Common/Device.cpp +++ b/src/Common/Device.cpp @@ -166,5 +166,9 @@ void DevChannel::initAudio(const AudioInfo &info) { } } +MediaOriginType DevChannel::getOriginType(MediaSource &sender) const { + return MediaOriginType::device_chn; +} + } /* namespace mediakit */ diff --git a/src/Common/Device.h b/src/Common/Device.h index 5f726f79..694570e7 100644 --- a/src/Common/Device.h +++ b/src/Common/Device.h @@ -128,6 +128,9 @@ public: void inputPCM(char *pcData, int iDataLen, uint32_t uiStamp); #endif //ENABLE_FAAC +private: + MediaOriginType getOriginType(MediaSource &sender) const override; + private: #ifdef ENABLE_X264 diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 2b7b80bb..6f5f564e 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -19,6 +19,20 @@ namespace mediakit { recursive_mutex s_media_source_mtx; MediaSource::SchemaVhostAppStreamMap s_media_source_map; +string getOriginTypeString(MediaOriginType type){ +#define SWITCH_CASE(type) case type : return #type + switch (type) { + SWITCH_CASE(MediaOriginType::unknown); + SWITCH_CASE(MediaOriginType::rtmp_push); + SWITCH_CASE(MediaOriginType::rtsp_push); + SWITCH_CASE(MediaOriginType::rtp_push); + SWITCH_CASE(MediaOriginType::pull); + SWITCH_CASE(MediaOriginType::ffmpeg_pull); + SWITCH_CASE(MediaOriginType::mp4_vod); + SWITCH_CASE(MediaOriginType::device_chn); + } +} + MediaSource::MediaSource(const string &schema, const string &vhost, const string &app, const string &stream_id){ GET_CONFIG(bool, enableVhost, General::kEnableVhost); if (!enableVhost) { @@ -76,6 +90,30 @@ int MediaSource::totalReaderCount(){ return listener->totalReaderCount(*this); } +MediaOriginType MediaSource::getOriginType() const { + auto listener = _listener.lock(); + if (!listener) { + return MediaOriginType::unknown; + } + return listener->getOriginType(const_cast(*this)); +} + +string MediaSource::getOriginUrl() const { + auto listener = _listener.lock(); + if (!listener) { + return ""; + } + return listener->getOriginUrl(const_cast(*this)); +} + +std::shared_ptr MediaSource::getOriginSock() const { + auto listener = _listener.lock(); + if (!listener) { + return nullptr; + } + return listener->getOriginSock(const_cast(*this)); +} + bool MediaSource::seekTo(uint32_t stamp) { auto listener = _listener.lock(); if(!listener){ @@ -365,6 +403,7 @@ bool MediaSource::unregist() { /////////////////////////////////////MediaInfo////////////////////////////////////// void MediaInfo::parse(const string &url_in){ + _full_url = url_in; string url = url_in; auto pos = url.find("?"); if (pos != string::npos) { @@ -486,6 +525,30 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ }, nullptr); } +MediaOriginType MediaSourceEventInterceptor::getOriginType(MediaSource &sender) const { + auto listener = _listener.lock(); + if (!listener) { + return MediaOriginType::unknown; + } + return listener->getOriginType(sender); +} + +string MediaSourceEventInterceptor::getOriginUrl(MediaSource &sender) const { + auto listener = _listener.lock(); + if (!listener) { + return ""; + } + return listener->getOriginUrl(sender); +} + +std::shared_ptr MediaSourceEventInterceptor::getOriginSock(MediaSource &sender) const { + auto listener = _listener.lock(); + if (!listener) { + return nullptr; + } + return listener->getOriginSock(sender); +} + bool MediaSourceEventInterceptor::seekTo(MediaSource &sender, uint32_t stamp) { auto listener = _listener.lock(); if (!listener) { diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index f92df7c2..039cf127 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -22,6 +22,7 @@ #include "Util/TimeTicker.h" #include "Util/NoticeCenter.h" #include "Util/List.h" +#include "Network/Socket.h" #include "Rtsp/Rtsp.h" #include "Rtmp/Rtmp.h" #include "Extension/Track.h" @@ -36,6 +37,19 @@ namespace toolkit{ namespace mediakit { +enum class MediaOriginType : uint8_t { + unknown = 0, + rtmp_push , + rtsp_push, + rtp_push, + pull, + ffmpeg_pull, + mp4_vod, + device_chn +}; + +string getOriginTypeString(MediaOriginType type); + class MediaSource; class MediaSourceEvent{ public: @@ -43,6 +57,13 @@ public: MediaSourceEvent(){}; virtual ~MediaSourceEvent(){}; + // 获取媒体源类型 + virtual MediaOriginType getOriginType(MediaSource &sender) const { return MediaOriginType::unknown; } + // 获取媒体源url或者文件路径 + virtual string getOriginUrl(MediaSource &sender) const { return ""; } + // 获取媒体源客户端相关信息 + virtual std::shared_ptr getOriginSock(MediaSource &sender) const { return nullptr; } + // 通知拖动进度条 virtual bool seekTo(MediaSource &sender, uint32_t stamp) { return false; } // 通知其停止产生流 @@ -76,6 +97,10 @@ public: MediaSourceEventInterceptor(){} ~MediaSourceEventInterceptor() override {} + MediaOriginType getOriginType(MediaSource &sender) const override; + string getOriginUrl(MediaSource &sender) const override; + std::shared_ptr getOriginSock(MediaSource &sender) const override; + bool seekTo(MediaSource &sender, uint32_t stamp) override; bool close(MediaSource &sender, bool force) override; int totalReaderCount(MediaSource &sender) override; @@ -102,6 +127,7 @@ public: void parse(const string &url); public: + string _full_url; string _schema; string _host; string _port; @@ -156,6 +182,13 @@ public: // 观看者个数,包括(hls/rtsp/rtmp) virtual int totalReaderCount(); + // 获取媒体源类型 + MediaOriginType getOriginType() const; + // 获取媒体源url或者文件路径 + string getOriginUrl() const; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock() const; + // 拖动进度条 bool seekTo(uint32_t stamp); // 关闭该流 diff --git a/src/Player/PlayerBase.h b/src/Player/PlayerBase.h index e38d723e..749a86aa 100644 --- a/src/Player/PlayerBase.h +++ b/src/Player/PlayerBase.h @@ -194,6 +194,11 @@ public: } return Parent::getTracks(trackReady); } + + std::shared_ptr getSockInfo() const{ + return dynamic_pointer_cast(_delegate); + } + protected: void onShutdown(const SockException &ex) override { if (_shutdownCB) { diff --git a/src/Player/PlayerProxy.cpp b/src/Player/PlayerProxy.cpp index e63b5a6a..78b8d5d6 100644 --- a/src/Player/PlayerProxy.cpp +++ b/src/Player/PlayerProxy.cpp @@ -112,6 +112,7 @@ void PlayerProxy::play(const string &strUrlTmp) { } }); MediaPlayer::play(strUrlTmp); + _pull_url = strUrlTmp; MediaSource::Ptr mediaSource; if(dynamic_pointer_cast(_delegate)){ @@ -180,6 +181,18 @@ int PlayerProxy::totalReaderCount(MediaSource &sender) { return totalReaderCount(); } +MediaOriginType PlayerProxy::getOriginType(MediaSource &sender) const{ + return MediaOriginType::pull; +} + +string PlayerProxy::getOriginUrl(MediaSource &sender) const{ + return _pull_url; +} + +std::shared_ptr PlayerProxy::getOriginSock(MediaSource &sender) const{ + return getSockInfo(); +} + class MuteAudioMaker : public FrameDispatcher{ public: typedef std::shared_ptr Ptr; diff --git a/src/Player/PlayerProxy.h b/src/Player/PlayerProxy.h index 35089ee5..dbb87001 100644 --- a/src/Player/PlayerProxy.h +++ b/src/Player/PlayerProxy.h @@ -59,6 +59,10 @@ private: //MediaSourceEvent override bool close(MediaSource &sender,bool force) override; int totalReaderCount(MediaSource &sender) override; + MediaOriginType getOriginType(MediaSource &sender) const override; + string getOriginUrl(MediaSource &sender) const override; + std::shared_ptr getOriginSock(MediaSource &sender) const override; + void rePlay(const string &strUrl,int iFailedCnt); void onPlaySuccess(); @@ -69,6 +73,7 @@ private: string _vhost; string _app; string _stream_id; + string _pull_url; Timer::Ptr _timer; function _on_close; function _on_play; diff --git a/src/Pusher/PusherBase.h b/src/Pusher/PusherBase.h index e68e93e1..6a19fa48 100644 --- a/src/Pusher/PusherBase.h +++ b/src/Pusher/PusherBase.h @@ -109,6 +109,11 @@ public: } _shutdownCB = cb; } + + std::shared_ptr getSockInfo() const{ + return dynamic_pointer_cast(_delegate); + } + protected: PusherBase::Event _shutdownCB; PusherBase::Event _publishCB; diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index ea58e0a3..12d3eb63 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -17,24 +17,24 @@ namespace mediakit { MP4Reader::MP4Reader(const string &strVhost,const string &strApp, const string &strId,const string &filePath ) { _poller = WorkThreadPool::Instance().getPoller(); - auto strFileName = filePath; - if(strFileName.empty()){ + _file_path = filePath; + if(_file_path.empty()){ GET_CONFIG(string,recordPath,Record::kFilePath); GET_CONFIG(bool,enableVhost,General::kEnableVhost); if(enableVhost){ - strFileName = strVhost + "/" + strApp + "/" + strId; + _file_path = strVhost + "/" + strApp + "/" + strId; }else{ - strFileName = strApp + "/" + strId; + _file_path = strApp + "/" + strId; } - strFileName = File::absolutePath(strFileName,recordPath); + _file_path = File::absolutePath(_file_path,recordPath); } _demuxer = std::make_shared(); - _demuxer->openMP4(strFileName); + _demuxer->openMP4(_file_path); _mediaMuxer.reset(new MultiMediaSourceMuxer(strVhost, strApp, strId, _demuxer->getDurationMS() / 1000.0, true, true, false, false)); auto tracks = _demuxer->getTracks(false); if(tracks.empty()){ - throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << strFileName); + throw std::runtime_error(StrPrinter << "该mp4文件没有有效的track:" << _file_path); } for(auto &track : tracks){ _mediaMuxer->addTrack(track); @@ -153,5 +153,13 @@ int MP4Reader::totalReaderCount(MediaSource &sender) { return _mediaMuxer ? _mediaMuxer->totalReaderCount() : sender.readerCount(); } +MediaOriginType MP4Reader::getOriginType(MediaSource &sender) const { + return MediaOriginType::mp4_vod; +} + +string MP4Reader::getOriginUrl(MediaSource &sender) const { + return _file_path; +} + } /* namespace mediakit */ #endif //ENABLE_MP4 \ No newline at end of file diff --git a/src/Record/MP4Reader.h b/src/Record/MP4Reader.h index b8348752..de374de5 100644 --- a/src/Record/MP4Reader.h +++ b/src/Record/MP4Reader.h @@ -41,6 +41,8 @@ private: bool seekTo(MediaSource &sender,uint32_t ui32Stamp) override; bool close(MediaSource &sender,bool force) override; int totalReaderCount(MediaSource &sender) override; + MediaOriginType getOriginType(MediaSource &sender) const override; + string getOriginUrl(MediaSource &sender) const override; bool readSample(); uint32_t getCurrentStamp(); @@ -50,6 +52,7 @@ private: private: bool _have_video = false; uint32_t _seek_to; + string _file_path; recursive_mutex _mtx; Ticker _seek_ticker; Timer::Ptr _timer; diff --git a/src/Rtmp/RtmpSession.cpp b/src/Rtmp/RtmpSession.cpp index 91ea29e2..f992385c 100644 --- a/src/Rtmp/RtmpSession.cpp +++ b/src/Rtmp/RtmpSession.cpp @@ -543,6 +543,18 @@ int RtmpSession::totalReaderCount(MediaSource &sender) { return _publisher_src ? _publisher_src->totalReaderCount() : sender.readerCount(); } +MediaOriginType RtmpSession::getOriginType(MediaSource &sender) const{ + return MediaOriginType::rtmp_push; +} + +string RtmpSession::getOriginUrl(MediaSource &sender) const { + return _media_info._full_url; +} + +std::shared_ptr RtmpSession::getOriginSock(MediaSource &sender) const { + return const_cast(this)->shared_from_this(); +} + void RtmpSession::setSocketFlags(){ GET_CONFIG(int, merge_write_ms, General::kMergeWriteMS); if (merge_write_ms > 0) { diff --git a/src/Rtmp/RtmpSession.h b/src/Rtmp/RtmpSession.h index 860422f3..239a7781 100644 --- a/src/Rtmp/RtmpSession.h +++ b/src/Rtmp/RtmpSession.h @@ -69,9 +69,17 @@ private: sendResponse(MSG_CMD, invoke.data()); } - //MediaSourceEvent override - bool close(MediaSource &sender,bool force) override ; + ///////MediaSourceEvent override/////// + // 关闭 + bool close(MediaSource &sender, bool force) override; + // 播放总人数 int totalReaderCount(MediaSource &sender) override; + // 获取媒体源类型 + MediaOriginType getOriginType(MediaSource &sender) const override; + // 获取媒体源url或者文件路径 + string getOriginUrl(MediaSource &sender) const override; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock(MediaSource &sender) const override; void setSocketFlags(); string getStreamId(const string &str); diff --git a/src/Rtp/RtpProcess.h b/src/Rtp/RtpProcess.h index b9530021..7715eda0 100644 --- a/src/Rtp/RtpProcess.h +++ b/src/Rtp/RtpProcess.h @@ -26,6 +26,7 @@ namespace mediakit{ class RtpProcess : public HttpRequestSplitter, public RtpReceiver, public SockInfo, public MediaSinkInterface, public std::enable_shared_from_this{ public: typedef std::shared_ptr Ptr; + friend class RtpProcessHelper; RtpProcess(const string &stream_id); ~RtpProcess(); diff --git a/src/Rtp/RtpSelector.cpp b/src/Rtp/RtpSelector.cpp index 283dd98f..0ab553f2 100644 --- a/src/Rtp/RtpSelector.cpp +++ b/src/Rtp/RtpSelector.cpp @@ -145,6 +145,18 @@ int RtpProcessHelper::totalReaderCount(MediaSource &sender) { return _process ? _process->totalReaderCount() : sender.totalReaderCount(); } +MediaOriginType RtpProcessHelper::getOriginType(MediaSource &sender) const{ + return MediaOriginType::rtp_push; +} + +string RtpProcessHelper::getOriginUrl(MediaSource &sender) const { + return _process ? _process->_media_info._full_url : ""; +} + +std::shared_ptr RtpProcessHelper::getOriginSock(MediaSource &sender) const{ + return _process; +} + RtpProcess::Ptr &RtpProcessHelper::getProcess() { return _process; } diff --git a/src/Rtp/RtpSelector.h b/src/Rtp/RtpSelector.h index 83f8bd2e..33a01256 100644 --- a/src/Rtp/RtpSelector.h +++ b/src/Rtp/RtpSelector.h @@ -34,6 +34,12 @@ protected: bool close(MediaSource &sender,bool force) override; // 观看总人数 int totalReaderCount(MediaSource &sender) override; + // 获取媒体源类型 + MediaOriginType getOriginType(MediaSource &sender) const override; + // 获取媒体源url或者文件路径 + string getOriginUrl(MediaSource &sender) const override; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock(MediaSource &sender) const override; private: weak_ptr _parent; diff --git a/src/Rtsp/RtspSession.cpp b/src/Rtsp/RtspSession.cpp index cdc44293..e010bd3c 100644 --- a/src/Rtsp/RtspSession.cpp +++ b/src/Rtsp/RtspSession.cpp @@ -1105,6 +1105,18 @@ int RtspSession::totalReaderCount(MediaSource &sender) { return _push_src ? _push_src->totalReaderCount() : sender.readerCount(); } +MediaOriginType RtspSession::getOriginType(MediaSource &sender) const{ + return MediaOriginType::rtsp_push; +} + +string RtspSession::getOriginUrl(MediaSource &sender) const { + return _media_info._full_url; +} + +std::shared_ptr RtspSession::getOriginSock(MediaSource &sender) const { + return const_cast(this)->shared_from_this(); +} + inline void RtspSession::onSendRtpPacket(const RtpPacket::Ptr &pkt){ #if RTSP_SERVER_SEND_RTCP int track_index = getTrackIndexByTrackType(pkt->type); diff --git a/src/Rtsp/RtspSession.h b/src/Rtsp/RtspSession.h index 47e9d5da..f681e874 100644 --- a/src/Rtsp/RtspSession.h +++ b/src/Rtsp/RtspSession.h @@ -74,11 +74,22 @@ protected: void onRtpPacket(const char *data, uint64_t len) override; //从rtsp头中获取Content长度 int64_t getContentLength(Parser &parser) override; + ////RtpReceiver override//// void onRtpSorted(const RtpPacket::Ptr &rtp, int track_idx) override; - ////MediaSourceEvent override//// - bool close(MediaSource &sender, bool force) override ; + + ///////MediaSourceEvent override/////// + // 关闭 + bool close(MediaSource &sender, bool force) override; + // 播放总人数 int totalReaderCount(MediaSource &sender) override; + // 获取媒体源类型 + MediaOriginType getOriginType(MediaSource &sender) const override; + // 获取媒体源url或者文件路径 + string getOriginUrl(MediaSource &sender) const override; + // 获取媒体源客户端相关信息 + std::shared_ptr getOriginSock(MediaSource &sender) const override; + /////TcpSession override//// int send(const Buffer::Ptr &pkt) override; //收到RTCP包回调