diff --git a/conf/config.ini b/conf/config.ini index 105a76ad..c6a7e545 100644 --- a/conf/config.ini +++ b/conf/config.ini @@ -40,6 +40,11 @@ modify_stamp=2 enable_audio=1 #添加acc静音音频,在关闭音频时,此开关无效 add_mute_audio=1 +#无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close) +#此配置置1时,此流如果无人观看,将不触发on_none_reader hook回调, +#而是将直接关闭流 +auto_close=0 + #推流断开后可以在超时时间内重新连接上继续推流,这样播放器会接着播放。 #置0关闭此特性(推流断开会导致立即断开播放器) #此参数不应大于播放器超时时间;单位毫秒 diff --git a/src/Common/MediaSource.cpp b/src/Common/MediaSource.cpp index 9b494f16..fc1d64f4 100644 --- a/src/Common/MediaSource.cpp +++ b/src/Common/MediaSource.cpp @@ -56,6 +56,7 @@ ProtocolOption::ProtocolOption() { GET_CONFIG(int, s_modify_stamp, Protocol::kModifyStamp); GET_CONFIG(bool, s_enabel_audio, Protocol::kEnableAudio); GET_CONFIG(bool, s_add_mute_audio, Protocol::kAddMuteAudio); + GET_CONFIG(bool, s_auto_close, Protocol::kAutoClose); GET_CONFIG(uint32_t, s_continue_push_ms, Protocol::kContinuePushMS); GET_CONFIG(bool, s_enable_hls, Protocol::kEnableHls); @@ -81,6 +82,7 @@ ProtocolOption::ProtocolOption() { modify_stamp = s_modify_stamp; enable_audio = s_enabel_audio; add_mute_audio = s_add_mute_audio; + auto_close = s_auto_close; continue_push_ms = s_continue_push_ms; enable_hls = s_enable_hls; @@ -658,8 +660,15 @@ void MediaSourceEvent::onReaderChanged(MediaSource &sender, int size){ } if (!is_mp4_vod) { - //直播时触发无人观看事件,让开发者自行选择是否关闭 - NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastStreamNoneReader, *strong_sender); + auto muxer = strong_sender->getMuxer(); + if (muxer && muxer->getOption().auto_close) { + // 此流被标记为无人观看自动关闭流 + WarnL << "Auto cloe stream when none reader: " << strong_sender->getUrl(); + strong_sender->close(false); + } else { + // 直播时触发无人观看事件,让开发者自行选择是否关闭 + NoticeCenter::Instance().emitEvent(Broadcast::kBroadcastStreamNoneReader, *strong_sender); + } } else { //这个是mp4点播,我们自动关闭 WarnL << "MP4点播无人观看,自动关闭:" << strong_sender->getUrl(); diff --git a/src/Common/MediaSource.h b/src/Common/MediaSource.h index 8d2c7658..1329b196 100644 --- a/src/Common/MediaSource.h +++ b/src/Common/MediaSource.h @@ -151,6 +151,11 @@ public: bool enable_audio; //添加静音音频,在关闭音频时,此开关无效 bool add_mute_audio; + // 无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close) + // 此配置置1时,此流如果无人观看,将不触发on_none_reader hook回调, + // 而是将直接关闭流 + bool auto_close; + //断连续推延时,单位毫秒,默认采用配置文件 uint32_t continue_push_ms; @@ -199,6 +204,7 @@ public: GET_OPT_VALUE(modify_stamp); GET_OPT_VALUE(enable_audio); GET_OPT_VALUE(add_mute_audio); + GET_OPT_VALUE(auto_close); GET_OPT_VALUE(continue_push_ms); GET_OPT_VALUE(enable_hls); diff --git a/src/Common/config.cpp b/src/Common/config.cpp index 2e6b9421..52c79091 100644 --- a/src/Common/config.cpp +++ b/src/Common/config.cpp @@ -99,6 +99,7 @@ namespace Protocol { const string kModifyStamp = PROTOCOL_FIELD "modify_stamp"; const string kEnableAudio = PROTOCOL_FIELD "enable_audio"; const string kAddMuteAudio = PROTOCOL_FIELD "add_mute_audio"; +const string kAutoClose = PROTOCOL_FIELD "auto_close"; const string kContinuePushMS = PROTOCOL_FIELD "continue_push_ms"; const string kEnableHls = PROTOCOL_FIELD "enable_hls"; @@ -126,6 +127,7 @@ static onceToken token([]() { mINI::Instance()[kEnableAudio] = 1; mINI::Instance()[kAddMuteAudio] = 1; mINI::Instance()[kContinuePushMS] = 15000; + mINI::Instance()[kAutoClose] = 0; mINI::Instance()[kEnableHls] = 1; mINI::Instance()[kEnableHlsFmp4] = 0; diff --git a/src/Common/config.h b/src/Common/config.h index 9f043ecb..baafac2d 100644 --- a/src/Common/config.h +++ b/src/Common/config.h @@ -190,6 +190,10 @@ extern const std::string kModifyStamp; extern const std::string kEnableAudio; //添加静音音频,在关闭音频时,此开关无效 extern const std::string kAddMuteAudio; +// 无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close) +// 此配置置1时,此流如果无人观看,将不触发on_none_reader hook回调, +// 而是将直接关闭流 +extern const std::string kAutoClose; //断连续推延时,单位毫秒,默认采用配置文件 extern const std::string kContinuePushMS;