From 373620cfc6289d504bcf5a01cef62a5f3abda0be Mon Sep 17 00:00:00 2001 From: KkemChen Date: Thu, 23 May 2024 10:30:11 +0800 Subject: [PATCH] feat: add broadcast event for player count change (#3562) --- conf/config.ini | 2 ++ src/Common/MediaSource.cpp | 4 ++++ src/Common/config.cpp | 3 +++ src/Common/config.h | 6 ++++++ 4 files changed, 15 insertions(+) diff --git a/conf/config.ini b/conf/config.ini index 89800db2..95d3fa37 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -126,6 +126,8 @@ wait_track_ready_ms=10000 wait_add_track_ms=3000 #如果track未就绪,我们先缓存帧数据,但是有最大个数限制,防止内存溢出 unready_frame_cache=100 +#是否启用观看人数变化事件广播,置1则启用,置0则关闭 +broadcast_player_count_changed=0 [hls] #hls写文件的buf大小,调整参数可以提高文件io性能 diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 8de8202a..039b156a 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -652,6 +652,10 @@ MediaSource::Ptr MediaSource::createFromMP4(const string &schema, const string & /////////////////////////////////////MediaSourceEvent////////////////////////////////////// void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ + GET_CONFIG(bool, enable, General::kBroadcastPlayerCountChanged); + if (enable) { + NOTICE_EMIT(BroadcastPlayerCountChangedArgs, Broadcast::kBroadcastPlayerCountChanged, sender.getMediaTuple(), sender.totalReaderCount()); + } if (size || sender.totalReaderCount()) { //还有人观看该视频,不触发关闭事件 _async_close_timer = nullptr; diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 8b7031ae..f2ec0354 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -64,6 +64,7 @@ const string kBroadcastRtcSctpFailed = "kBroadcastRtcSctpFailed"; const string kBroadcastRtcSctpClosed = "kBroadcastRtcSctpClosed"; const string kBroadcastRtcSctpSend = "kBroadcastRtcSctpSend"; const string kBroadcastRtcSctpReceived = "kBroadcastRtcSctpReceived"; +const string kBroadcastPlayerCountChanged = "kBroadcastPlayerCountChanged"; } // namespace Broadcast @@ -82,6 +83,7 @@ const string kEnableFFmpegLog = GENERAL_FIELD "enable_ffmpeg_log"; const string kWaitTrackReadyMS = GENERAL_FIELD "wait_track_ready_ms"; const string kWaitAddTrackMS = GENERAL_FIELD "wait_add_track_ms"; const string kUnreadyFrameCache = GENERAL_FIELD "unready_frame_cache"; +const string kBroadcastPlayerCountChanged = GENERAL_FIELD "broadcast_player_count_changed"; static onceToken token([]() { mINI::Instance()[kFlowThreshold] = 1024; @@ -96,6 +98,7 @@ static onceToken token([]() { mINI::Instance()[kWaitTrackReadyMS] = 10000; mINI::Instance()[kWaitAddTrackMS] = 3000; mINI::Instance()[kUnreadyFrameCache] = 100; + mINI::Instance()[kBroadcastPlayerCountChanged] = 0; }); } // namespace General diff --git a/src/Common/config.h b/src/Common/config.h index b10fe685..4b6423a5 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -124,6 +124,10 @@ extern const std::string kBroadcastRtcSctpSend; extern const std::string kBroadcastRtcSctpReceived; #define BroadcastRtcSctpReceivedArgs WebRtcTransport& sender, uint16_t &streamId, uint32_t &ppid, const uint8_t *&msg, size_t &len +// 观看人数变化广播 +extern const std::string kBroadcastPlayerCountChanged; +#define BroadcastPlayerCountChangedArgs const MediaTuple& args, const int& count + #define ReloadConfigTag ((void *)(0xFF)) #define RELOAD_KEY(arg, key) \ do { \ @@ -196,6 +200,8 @@ extern const std::string kWaitTrackReadyMS; extern const std::string kWaitAddTrackMS; // 如果track未就绪,我们先缓存帧数据,但是有最大个数限制(100帧时大约4秒),防止内存溢出 extern const std::string kUnreadyFrameCache; +// 是否启用观看人数变化事件广播,置1则启用,置0则关闭 +extern const std::string kBroadcastPlayerCountChanged; } // namespace General namespace Protocol {