From 0f1120b8a60ddd14c75b546d62dc18f05b0ba080 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Sat, 12 Mar 2022 14:34:48 +0800 Subject: [PATCH] =?UTF-8?q?on=5Fpublish=20hook=E6=96=B0=E5=A2=9E=E5=A4=9A?= =?UTF-8?q?=E7=A7=8D=E9=80=89=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/WebApi.cpp | 4 ++-- server/WebHook.cpp | 23 +++++++++++++++++++---- src/Common/MediaSink.cpp | 21 +++++++++++++++------ src/Common/MediaSink.h | 23 ++++++++++++++++------- src/Common/MultiMediaSourceMuxer.cpp | 23 +++++++++++++++-------- src/Common/MultiMediaSourceMuxer.h | 13 +++++++++++++ 6 files changed, 80 insertions(+), 27 deletions(-) diff --git a/server/WebApi.cpp b/server/WebApi.cpp index cb58304e..6992d4db 100755 --- a/server/WebApi.cpp +++ b/server/WebApi.cpp @@ -907,8 +907,8 @@ void installWebApi() { CHECK_SECRET(); CHECK_ARGS("vhost","app","stream","url"); ProtocolOption option; - option.enable_hls = allArgs["enable_hls"]; - option.enable_mp4 = allArgs["enable_mp4"]; + option.enable_hls = allArgs["enable_hls"]; + option.enable_mp4 = allArgs["enable_mp4"]; addStreamProxy(allArgs["vhost"], allArgs["app"], diff --git a/server/WebHook.cpp b/server/WebHook.cpp index abead83f..e8dc521f 100755 --- a/server/WebHook.cpp +++ b/server/WebHook.cpp @@ -287,11 +287,26 @@ void installWebHook(){ if (err.empty()) { //推流鉴权成功 //兼容用户不传递enableHls、enableMP4参数 - if (obj.isMember("enableHls")) { - option.enable_hls = obj["enableHls"].asBool(); + if (obj.isMember("enable_hls")) { + option.enable_hls = obj["enable_hls"].asBool(); } - if (obj.isMember("enableMP4")) { - option.enable_mp4 = obj["enableMP4"].asBool(); + if (obj.isMember("enable_mp4")) { + option.enable_mp4 = obj["enable_mp4"].asBool(); + } + if (obj.isMember("enable_audio")) { + option.enable_audio = obj["enable_audio"].asBool(); + } + if (obj.isMember("add_mute_audio")) { + option.add_mute_audio = obj["add_mute_audio"].asBool(); + } + if (obj.isMember("mp4_save_path")) { + option.mp4_save_path = obj["mp4_save_path"].asString(); + } + if (obj.isMember("mp4_max_second")) { + option.mp4_max_second = obj["mp4_max_second"].asUInt(); + } + if (obj.isMember("hls_save_path")) { + option.hls_save_path = obj["hls_save_path"].asString(); } invoker(err, option); } else { diff --git a/src/Common/MediaSink.cpp b/src/Common/MediaSink.cpp index c11d266e..1f8050ab 100644 --- a/src/Common/MediaSink.cpp +++ b/src/Common/MediaSink.cpp @@ -16,8 +16,7 @@ using namespace std; namespace mediakit{ bool MediaSink::addTrack(const Track::Ptr &track_in) { - GET_CONFIG(bool, enabel_audio, General::kEnableAudio); - if (!enabel_audio) { + if (!_enable_audio) { //关闭音频时,加快单视频流注册速度 _max_track_size = 1; if (track_in->getTrackType() == TrackAudio) { @@ -169,8 +168,7 @@ void MediaSink::emitAllTrackReady() { void MediaSink::onAllTrackReady_l() { //是否添加静音音频 - GET_CONFIG(bool, add_mute_audio, General::kAddMuteAudio); - if (add_mute_audio) { + if (_add_mute_audio) { addMuteAudioTrack(); } onAllTrackReady(); @@ -241,8 +239,7 @@ bool MuteAudioMaker::inputFrame(const Frame::Ptr &frame) { } bool MediaSink::addMuteAudioTrack() { - GET_CONFIG(bool, enabel_audio, General::kEnableAudio); - if (!enabel_audio) { + if (!_enable_audio) { return false; } if (_track_map.find(TrackAudio) != _track_map.end()) { @@ -262,4 +259,16 @@ bool MediaSink::addMuteAudioTrack() { return true; } +bool MediaSink::isAllTrackReady() const { + return _all_track_ready; +} + +void MediaSink::enableAudio(bool flag) { + _enable_audio = flag; +} + +void MediaSink::enableMuteAudio(bool flag) { + _add_mute_audio = flag; +} + }//namespace mediakit diff --git a/src/Common/MediaSink.h b/src/Common/MediaSink.h index ee6d404a..449f666e 100644 --- a/src/Common/MediaSink.h +++ b/src/Common/MediaSink.h @@ -104,18 +104,21 @@ public: * @param trackReady 是否获取已经准备好的Track */ std::vector getTracks(bool trackReady = true) const override; - + /** * 返回是否所有track已经准备完成 */ - bool isAllTrackReady() const { - return _all_track_ready; - } - + bool isAllTrackReady() const; + /** - * 添加aac静音轨道 + * 设置是否开启音频 */ - bool addMuteAudioTrack(); + void enableAudio(bool flag); + + /** + * 设置是否开启添加静音音频 + */ + void enableMuteAudio(bool flag); protected: /** @@ -147,8 +150,14 @@ private: */ void checkTrackIfReady(); void onAllTrackReady_l(); + /** + * 添加aac静音轨道 + */ + bool addMuteAudioTrack(); private: + bool _enable_audio = true; + bool _add_mute_audio = true; bool _all_track_ready = false; size_t _max_track_size = 2; std::unordered_map > _track_map; diff --git a/src/Common/MultiMediaSourceMuxer.cpp b/src/Common/MultiMediaSourceMuxer.cpp index fac4684f..33902a78 100644 --- a/src/Common/MultiMediaSourceMuxer.cpp +++ b/src/Common/MultiMediaSourceMuxer.cpp @@ -22,10 +22,15 @@ namespace toolkit { namespace mediakit { ProtocolOption::ProtocolOption() { - GET_CONFIG(bool, toHls, General::kPublishToHls); - GET_CONFIG(bool, toMP4, General::kPublishToMP4); - enable_hls = toHls; - enable_mp4 = toMP4; + GET_CONFIG(bool, s_to_hls, General::kPublishToHls); + GET_CONFIG(bool, s_to_mp4, General::kPublishToMP4); + GET_CONFIG(bool, s_enabel_audio, General::kEnableAudio); + GET_CONFIG(bool, s_add_mute_audio, General::kAddMuteAudio); + + enable_hls = s_to_hls; + enable_mp4 = s_to_mp4; + enable_audio = s_enabel_audio; + add_mute_audio = s_add_mute_audio; } static std::shared_ptr makeRecorder(MediaSource &sender, const vector &tracks, Recorder::type type, const string &custom_path, size_t max_second){ @@ -81,13 +86,11 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string & if (option.enable_rtsp) { _rtsp = std::make_shared(vhost, app, stream, std::make_shared(dur_sec)); } - if (option.enable_hls) { - _hls = dynamic_pointer_cast(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream)); + _hls = dynamic_pointer_cast(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream, option.hls_save_path)); } - if (option.enable_mp4) { - _mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream); + _mp4 = Recorder::createRecorder(Recorder::type_mp4, vhost, app, stream, option.mp4_save_path, option.mp4_max_second); } if (option.enable_ts) { _ts = std::make_shared(vhost, app, stream); @@ -97,6 +100,10 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string & _fmp4 = std::make_shared(vhost, app, stream); } #endif + + //音频相关设置 + enableAudio(option.enable_audio); + enableMuteAudio(option.add_mute_audio); } void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr &listener) { diff --git a/src/Common/MultiMediaSourceMuxer.h b/src/Common/MultiMediaSourceMuxer.h index e76fdd75..8e640730 100644 --- a/src/Common/MultiMediaSourceMuxer.h +++ b/src/Common/MultiMediaSourceMuxer.h @@ -39,6 +39,19 @@ public: bool enable_ts = true; //是否开启转换为http-fmp4/ws-fmp4 bool enable_fmp4 = true; + + //转协议是否开启音频 + bool enable_audio = true; + //添加静音音频,在关闭音频时,此开关无效 + bool add_mute_audio = true; + + //mp4录制保存路径 + std::string mp4_save_path; + //mp4切片大小,单位秒 + size_t mp4_max_second = 0; + + //hls录制保存路径 + std::string hls_save_path; }; class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSink, public std::enable_shared_from_this{