From 021f09e62bb24f00142ade08fe44583a632ce98d Mon Sep 17 00:00:00 2001 From: xia-chu <771730766@qq.com> Date: Sun, 23 Nov 2025 18:56:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=B2=BE=E7=AE=80=E5=AA=92?= =?UTF-8?q?=E4=BD=93=E4=BA=8B=E4=BB=B6=E7=9B=B8=E5=85=B3=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/source/mk_media.cpp | 6 ++-- src/Common/MediaSource.cpp | 52 ++++------------------------ src/Common/MediaSource.h | 23 ------------ src/Common/MultiMediaSourceMuxer.cpp | 32 ++++++++--------- src/Common/MultiMediaSourceMuxer.h | 34 +++--------------- src/Rtp/RtpSender.cpp | 4 +-- src/Rtp/RtpSender.h | 2 +- 7 files changed, 31 insertions(+), 122 deletions(-) diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 788632b2..405995db 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -308,7 +308,7 @@ API_EXPORT void API_CALL mk_media_start_send_rtp2(mk_media ctx, const char *dst_ auto ref = *obj; std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, ptr]() { - ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, ptr](uint16_t local_port, const SockException &ex) { + ref->getChannel()->getMuxer(MediaSource::NullMediaSource())->startSendRtp( args, [cb, ptr](uint16_t local_port, const SockException &ex) { if (cb) { cb(ptr.get(), local_port, ex.getErrCode(), ex.what()); } @@ -349,7 +349,7 @@ API_EXPORT void API_CALL mk_media_start_send_rtp4(mk_media ctx, const char *dst_ std::shared_ptr ptr( user_data, user_data_free ? user_data_free : [](void *) {}); (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, ptr]() { - ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, ptr](uint16_t local_port, const SockException &ex) { + ref->getChannel()->getMuxer(MediaSource::NullMediaSource())->startSendRtp(args, [cb, ptr](uint16_t local_port, const SockException &ex) { if (cb) { cb(ptr.get(), local_port, ex.getErrCode(), ex.what()); } @@ -365,7 +365,7 @@ API_EXPORT void API_CALL mk_media_stop_send_rtp(mk_media ctx, const char *ssrc) auto ref = *obj; string ssrc_str = ssrc ? ssrc : ""; (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([ref, ssrc_str]() { - ref->getChannel()->stopSendRtp(MediaSource::NullMediaSource(), ssrc_str); + ref->getChannel()->getMuxer(MediaSource::NullMediaSource())->stopSendRtp(ssrc_str); }); } diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 33877847..f1f210bd 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -132,10 +132,10 @@ uint64_t MediaSource::getAliveSecond() const { vector MediaSource::getTracks(bool ready) const { auto listener = _listener.lock(); - if(!listener){ + if (!listener) { return vector(); } - return listener->getMediaTracks(const_cast(*this), ready); + return listener->getMuxer(const_cast(*this))->getTracks(ready); } void MediaSource::setListener(const std::weak_ptr &listener){ @@ -277,7 +277,7 @@ bool MediaSource::setupRecord(Recorder::type type, bool start, const string &cus WarnL << "未设置MediaSource的事件监听者,setupRecord失败:" << getUrl(); return false; } - return listener->setupRecord(*this, type, start, custom_path, max_second); + return listener->getMuxer(const_cast(*this))->setupRecord(type, start, custom_path, max_second); } bool MediaSource::isRecording(Recorder::type type){ @@ -285,7 +285,7 @@ bool MediaSource::isRecording(Recorder::type type){ if(!listener){ return false; } - return listener->isRecording(*this, type); + return listener->getMuxer(const_cast(*this))->isRecording(type); } void MediaSource::startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { @@ -294,7 +294,7 @@ void MediaSource::startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const cb(0, SockException(Err_other, "尚未设置事件监听器")); return; } - return listener->startSendRtp(*this, args, cb); + return listener->getMuxer(const_cast(*this))->startSendRtp(args, cb); } bool MediaSource::stopSendRtp(const string &ssrc) { @@ -302,7 +302,7 @@ bool MediaSource::stopSendRtp(const string &ssrc) { if (!listener) { return false; } - return listener->stopSendRtp(*this, ssrc); + return listener->getMuxer(const_cast(*this))->stopSendRtp(ssrc); } template @@ -830,46 +830,6 @@ std::shared_ptr MediaSourceEventInterceptor::getRtpProcess(MediaSour return listener->getRtpProcess(sender); } -bool MediaSourceEventInterceptor::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { - auto listener = _listener.lock(); - if (!listener) { - return MediaSourceEvent::setupRecord(sender, type, start, custom_path, max_second); - } - return listener->setupRecord(sender, type, start, custom_path, max_second); -} - -bool MediaSourceEventInterceptor::isRecording(MediaSource &sender, Recorder::type type) { - auto listener = _listener.lock(); - if (!listener) { - return MediaSourceEvent::isRecording(sender, type); - } - return listener->isRecording(sender, type); -} - -vector MediaSourceEventInterceptor::getMediaTracks(MediaSource &sender, bool trackReady) const { - auto listener = _listener.lock(); - if (!listener) { - return MediaSourceEvent::getMediaTracks(sender, trackReady); - } - return listener->getMediaTracks(sender, trackReady); -} - -void MediaSourceEventInterceptor::startSendRtp(MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { - auto listener = _listener.lock(); - if (!listener) { - return MediaSourceEvent::startSendRtp(sender, args, cb); - } - listener->startSendRtp(sender, args, cb); -} - -bool MediaSourceEventInterceptor::stopSendRtp(MediaSource &sender, const string &ssrc) { - auto listener = _listener.lock(); - if (!listener) { - return MediaSourceEvent::stopSendRtp(sender, ssrc); - } - return listener->stopSendRtp(sender, ssrc); -} - void MediaSourceEventInterceptor::setDelegate(const std::weak_ptr &listener) { if (listener.lock().get() == this) { throw std::invalid_argument("can not set self as a delegate"); diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 8b2655b0..9b9a78a1 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -94,17 +94,6 @@ public: // Get the current thread, this function is generally forced to overload virtual toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) { throw NotImplemented(toolkit::demangle(typeid(*this).name()) + "::getOwnerPoller not implemented"); } - // //////////////////////仅供MultiMediaSourceMuxer对象继承//////////////////////// [AUTO-TRANSLATED:6e810d1f] - // //////////////////////Only for MultiMediaSourceMuxer object inheritance//////////////////////// - // 开启或关闭录制 [AUTO-TRANSLATED:3817e390] - // Start or stop recording - virtual bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) { return false; }; - // 获取录制状态 [AUTO-TRANSLATED:a0499880] - // Get recording status - virtual bool isRecording(MediaSource &sender, Recorder::type type) { return false; } - // 获取所有track相关信息 [AUTO-TRANSLATED:2141be42] - // Get all track related information - virtual std::vector getMediaTracks(MediaSource &sender, bool trackReady = true) const { return std::vector(); }; // 获取MultiMediaSourceMuxer对象 [AUTO-TRANSLATED:2de96d44] // Get MultiMediaSourceMuxer object virtual std::shared_ptr getMuxer(MediaSource &sender) const { return nullptr; } @@ -180,13 +169,6 @@ public: bool enable_origin_recv_limit = false; }; - // 开始发送ps-rtp [AUTO-TRANSLATED:a51796fa] - // Start sending ps-rtp - virtual void startSendRtp(MediaSource &sender, const SendRtpArgs &args, const std::function cb) { cb(0, toolkit::SockException(toolkit::Err_other, "not implemented"));}; - // 停止发送ps-rtp [AUTO-TRANSLATED:952d2b35] - // Stop sending ps-rtp - virtual bool stopSendRtp(MediaSource &sender, const std::string &ssrc) {return false; } - private: toolkit::Timer::Ptr _async_close_timer; }; @@ -362,11 +344,6 @@ public: int totalReaderCount(MediaSource &sender) override; void onReaderChanged(MediaSource &sender, int size) override; void onRegist(MediaSource &sender, bool regist) override; - bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) override; - bool isRecording(MediaSource &sender, Recorder::type type) override; - std::vector getMediaTracks(MediaSource &sender, bool trackReady = true) const override; - void startSendRtp(MediaSource &sender, const SendRtpArgs &args, const std::function cb) override; - bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override; float getLossRate(MediaSource &sender, TrackType type) override; toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override; std::shared_ptr getMuxer(MediaSource &sender) const override; diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index 0395e717..5794e11b 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -135,8 +135,8 @@ private: std::multimap _cache; }; -std::shared_ptr MultiMediaSourceMuxer::makeRecorder(MediaSource &sender, Recorder::type type) { - auto recorder = Recorder::createRecorder(type, sender.getMediaTuple(), _option); +std::shared_ptr MultiMediaSourceMuxer::makeRecorder(Recorder::type type) { + auto recorder = Recorder::createRecorder(type, getMediaTuple(), _option); for (auto &track : getTracks()) { recorder->addTrack(track); } @@ -311,13 +311,13 @@ int MultiMediaSourceMuxer::totalReaderCount(MediaSource &sender) { // 此函数可能跨线程调用 [AUTO-TRANSLATED:e8c5f74d] // This function may be called across threads -bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type, bool start, const string &custom_path, size_t max_second) { +bool MultiMediaSourceMuxer::setupRecord(Recorder::type type, bool start, const string &custom_path, size_t max_second) { CHECK(getOwnerPoller(MediaSource::NullMediaSource())->isCurrentThread(), "Can only call setupRecord in it's owner poller"); onceToken token(nullptr, [&]() { if (_option.mp4_as_player && type == Recorder::type_mp4) { // 开启关闭mp4录制,触发观看人数变化相关事件 [AUTO-TRANSLATED:b63a8deb] // Turn on/off mp4 recording, trigger events related to changes in the number of viewers - onReaderChanged(sender, totalReaderCount()); + onReaderChanged(MediaSource::NullMediaSource(), totalReaderCount()); } }); switch (type) { @@ -326,7 +326,7 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type // 开始录制 [AUTO-TRANSLATED:36d99250] // Start recording _option.hls_save_path = custom_path; - auto hls = dynamic_pointer_cast(makeRecorder(sender, type)); + auto hls = dynamic_pointer_cast(makeRecorder(type)); if (hls) { // 设置HlsMediaSource的事件监听器 [AUTO-TRANSLATED:69990c92] // Set the event listener for HlsMediaSource @@ -346,7 +346,7 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type // Start recording _option.mp4_save_path = custom_path; _option.mp4_max_second = max_second; - _mp4 = makeRecorder(sender, type); + _mp4 = makeRecorder(type); } else if (!start && _mp4) { // 停止录制 [AUTO-TRANSLATED:3dee9292] // Stop recording @@ -359,7 +359,7 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type // 开始录制 [AUTO-TRANSLATED:36d99250] // Start recording _option.hls_save_path = custom_path; - auto hls = dynamic_pointer_cast(makeRecorder(sender, type)); + auto hls = dynamic_pointer_cast(makeRecorder(type)); if (hls) { // 设置HlsMediaSource的事件监听器 [AUTO-TRANSLATED:69990c92] // Set the event listener for HlsMediaSource @@ -375,7 +375,7 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type } case Recorder::type_fmp4: { if (start && !_fmp4) { - auto fmp4 = dynamic_pointer_cast(makeRecorder(sender, type)); + auto fmp4 = dynamic_pointer_cast(makeRecorder(type)); if (fmp4) { fmp4->setListener(shared_from_this()); } @@ -387,7 +387,7 @@ bool MultiMediaSourceMuxer::setupRecord(MediaSource &sender, Recorder::type type } case Recorder::type_ts: { if (start && !_ts) { - auto ts = dynamic_pointer_cast(makeRecorder(sender, type)); + auto ts = dynamic_pointer_cast(makeRecorder(type)); if (ts) { ts->setListener(shared_from_this()); } @@ -488,7 +488,7 @@ std::string MultiMediaSourceMuxer::startRecord(const std::string &file_path, uin // 此函数可能跨线程调用 [AUTO-TRANSLATED:e8c5f74d] // This function may be called across threads -bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type) { +bool MultiMediaSourceMuxer::isRecording(Recorder::type type) { switch (type) { case Recorder::type_hls: return !!_hls; case Recorder::type_mp4: return !!_mp4; @@ -499,7 +499,7 @@ bool MultiMediaSourceMuxer::isRecording(MediaSource &sender, Recorder::type type } } -void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { +void MultiMediaSourceMuxer::startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const std::function cb) { #if defined(ENABLE_RTPPROXY) createGopCacheIfNeed(1); @@ -507,7 +507,7 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE auto ssrc = args.ssrc; auto ssrc_multi_send = args.ssrc_multi_send; auto tracks = getTracks(false); - auto poller = getOwnerPoller(sender); + auto poller = getOwnerPoller(MediaSource::NullMediaSource()); auto rtp_sender = std::make_shared(poller); weak_ptr weak_self = shared_from_this(); @@ -524,7 +524,7 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE } }); - rtp_sender->startSend(sender, args, [ssrc,ssrc_multi_send, weak_self, rtp_sender, cb, tracks, ring, poller](uint16_t local_port, const SockException &ex) mutable { + rtp_sender->startSend(*this, args, [ssrc,ssrc_multi_send, weak_self, rtp_sender, cb, tracks, ring, poller](uint16_t local_port, const SockException &ex) mutable { cb(local_port, ex); auto strong_self = weak_self.lock(); if (!strong_self || ex) { @@ -556,7 +556,7 @@ void MultiMediaSourceMuxer::startSendRtp(MediaSource &sender, const MediaSourceE #endif//ENABLE_RTPPROXY } -bool MultiMediaSourceMuxer::stopSendRtp(MediaSource &sender, const string &ssrc) { +bool MultiMediaSourceMuxer::stopSendRtp(const string &ssrc) { #if defined(ENABLE_RTPPROXY) if (ssrc.empty()) { // 关闭全部 [AUTO-TRANSLATED:ffaadfda] @@ -573,10 +573,6 @@ bool MultiMediaSourceMuxer::stopSendRtp(MediaSource &sender, const string &ssrc) #endif//ENABLE_RTPPROXY } -vector MultiMediaSourceMuxer::getMediaTracks(MediaSource &sender, bool trackReady) const { - return getTracks(trackReady); -} - EventPoller::Ptr MultiMediaSourceMuxer::getOwnerPoller(MediaSource &sender) { auto listener = getDelegate(); if (!listener) { diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index cd7fbb91..df0bcedb 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -120,7 +120,7 @@ public: * [AUTO-TRANSLATED:cb1fd8a9] */ - bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) override; + bool setupRecord(Recorder::type type, bool start, const std::string &custom_path, size_t max_second); /** * 开始录制mp4 @@ -141,25 +141,13 @@ public: * [AUTO-TRANSLATED:798afa71] */ - bool isRecording(MediaSource &sender, Recorder::type type) override; + bool isRecording(Recorder::type type); /** * 开始发送ps-rtp流 - * @param dst_url 目标ip或域名 - * @param dst_port 目标端口 - * @param ssrc rtp的ssrc - * @param is_udp 是否为udp * @param cb 启动成功或失败回调 - * Start sending ps-rtp stream - * @param dst_url Target ip or domain name - * @param dst_port Target port - * @param ssrc rtp's ssrc - * @param is_udp Whether it is udp - * @param cb Start success or failure callback - - * [AUTO-TRANSLATED:620416c2] */ - void startSendRtp(MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function cb) override; + void startSendRtp(const MediaSourceEvent::SendRtpArgs &args, const std::function cb); /** * 停止ps-rtp发送 @@ -169,19 +157,7 @@ public: * [AUTO-TRANSLATED:b91e2055] */ - bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override; - - /** - * 获取所有Track - * @param trackReady 是否筛选过滤未就绪的track - * @return 所有Track - * Get all Tracks - * @param trackReady Whether to filter out unready tracks - * @return All Tracks - - * [AUTO-TRANSLATED:53755f5d] - */ - std::vector getMediaTracks(MediaSource &sender, bool trackReady = true) const override; + bool stopSendRtp(const std::string &ssrc); /** * 获取所属线程 @@ -247,7 +223,7 @@ protected: private: void createGopCacheIfNeed(size_t gop_count); - std::shared_ptr makeRecorder(MediaSource &sender, Recorder::type type); + std::shared_ptr makeRecorder(Recorder::type type); private: bool _is_enable = false; diff --git a/src/Rtp/RtpSender.cpp b/src/Rtp/RtpSender.cpp index 93a205de..fb142862 100644 --- a/src/Rtp/RtpSender.cpp +++ b/src/Rtp/RtpSender.cpp @@ -35,8 +35,8 @@ RtpSender::~RtpSender() { } } -void RtpSender::startSend(const MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const function &cb){ - auto origin_socket = sender.getOriginSock(); +void RtpSender::startSend(const MediaSourceEvent &sender, const MediaSourceEvent::SendRtpArgs &args, const function &cb){ + auto origin_socket = sender.getOriginSock(MediaSource::NullMediaSource()); _origin_socket = dynamic_pointer_cast(origin_socket); if (!_origin_socket) { auto process = dynamic_pointer_cast(origin_socket); diff --git a/src/Rtp/RtpSender.h b/src/Rtp/RtpSender.h index 5b67c51d..8ed3eb80 100644 --- a/src/Rtp/RtpSender.h +++ b/src/Rtp/RtpSender.h @@ -40,7 +40,7 @@ public: * [AUTO-TRANSLATED:c31bd9b3] */ - void startSend(const MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function &cb); + void startSend(const MediaSourceEvent &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function &cb); /** * 输入帧数据