on_publish hook新增多种选项

This commit is contained in:
xiongziliang 2022-03-12 14:34:48 +08:00
parent 490656ec3e
commit 0f1120b8a6
6 changed files with 80 additions and 27 deletions

View File

@ -287,11 +287,26 @@ void installWebHook(){
if (err.empty()) { if (err.empty()) {
//推流鉴权成功 //推流鉴权成功
//兼容用户不传递enableHls、enableMP4参数 //兼容用户不传递enableHls、enableMP4参数
if (obj.isMember("enableHls")) { if (obj.isMember("enable_hls")) {
option.enable_hls = obj["enableHls"].asBool(); option.enable_hls = obj["enable_hls"].asBool();
} }
if (obj.isMember("enableMP4")) { if (obj.isMember("enable_mp4")) {
option.enable_mp4 = obj["enableMP4"].asBool(); 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); invoker(err, option);
} else { } else {

View File

@ -16,8 +16,7 @@ using namespace std;
namespace mediakit{ namespace mediakit{
bool MediaSink::addTrack(const Track::Ptr &track_in) { bool MediaSink::addTrack(const Track::Ptr &track_in) {
GET_CONFIG(bool, enabel_audio, General::kEnableAudio); if (!_enable_audio) {
if (!enabel_audio) {
//关闭音频时,加快单视频流注册速度 //关闭音频时,加快单视频流注册速度
_max_track_size = 1; _max_track_size = 1;
if (track_in->getTrackType() == TrackAudio) { if (track_in->getTrackType() == TrackAudio) {
@ -169,8 +168,7 @@ void MediaSink::emitAllTrackReady() {
void MediaSink::onAllTrackReady_l() { void MediaSink::onAllTrackReady_l() {
//是否添加静音音频 //是否添加静音音频
GET_CONFIG(bool, add_mute_audio, General::kAddMuteAudio); if (_add_mute_audio) {
if (add_mute_audio) {
addMuteAudioTrack(); addMuteAudioTrack();
} }
onAllTrackReady(); onAllTrackReady();
@ -241,8 +239,7 @@ bool MuteAudioMaker::inputFrame(const Frame::Ptr &frame) {
} }
bool MediaSink::addMuteAudioTrack() { bool MediaSink::addMuteAudioTrack() {
GET_CONFIG(bool, enabel_audio, General::kEnableAudio); if (!_enable_audio) {
if (!enabel_audio) {
return false; return false;
} }
if (_track_map.find(TrackAudio) != _track_map.end()) { if (_track_map.find(TrackAudio) != _track_map.end()) {
@ -262,4 +259,16 @@ bool MediaSink::addMuteAudioTrack() {
return true; 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 }//namespace mediakit

View File

@ -108,14 +108,17 @@ public:
/** /**
* track已经准备完成 * track已经准备完成
*/ */
bool isAllTrackReady() const { bool isAllTrackReady() const;
return _all_track_ready;
}
/** /**
* aac静音轨道 *
*/ */
bool addMuteAudioTrack(); void enableAudio(bool flag);
/**
*
*/
void enableMuteAudio(bool flag);
protected: protected:
/** /**
@ -147,8 +150,14 @@ private:
*/ */
void checkTrackIfReady(); void checkTrackIfReady();
void onAllTrackReady_l(); void onAllTrackReady_l();
/**
* aac静音轨道
*/
bool addMuteAudioTrack();
private: private:
bool _enable_audio = true;
bool _add_mute_audio = true;
bool _all_track_ready = false; bool _all_track_ready = false;
size_t _max_track_size = 2; size_t _max_track_size = 2;
std::unordered_map<int, std::pair<Track::Ptr, bool/*got frame*/> > _track_map; std::unordered_map<int, std::pair<Track::Ptr, bool/*got frame*/> > _track_map;

View File

@ -22,10 +22,15 @@ namespace toolkit {
namespace mediakit { namespace mediakit {
ProtocolOption::ProtocolOption() { ProtocolOption::ProtocolOption() {
GET_CONFIG(bool, toHls, General::kPublishToHls); GET_CONFIG(bool, s_to_hls, General::kPublishToHls);
GET_CONFIG(bool, toMP4, General::kPublishToMP4); GET_CONFIG(bool, s_to_mp4, General::kPublishToMP4);
enable_hls = toHls; GET_CONFIG(bool, s_enabel_audio, General::kEnableAudio);
enable_mp4 = toMP4; 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<MediaSinkInterface> makeRecorder(MediaSource &sender, const vector<Track::Ptr> &tracks, Recorder::type type, const string &custom_path, size_t max_second){ static std::shared_ptr<MediaSinkInterface> makeRecorder(MediaSource &sender, const vector<Track::Ptr> &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) { if (option.enable_rtsp) {
_rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleSdp>(dur_sec)); _rtsp = std::make_shared<RtspMediaSourceMuxer>(vhost, app, stream, std::make_shared<TitleSdp>(dur_sec));
} }
if (option.enable_hls) { if (option.enable_hls) {
_hls = dynamic_pointer_cast<HlsRecorder>(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream)); _hls = dynamic_pointer_cast<HlsRecorder>(Recorder::createRecorder(Recorder::type_hls, vhost, app, stream, option.hls_save_path));
} }
if (option.enable_mp4) { 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) { if (option.enable_ts) {
_ts = std::make_shared<TSMediaSourceMuxer>(vhost, app, stream); _ts = std::make_shared<TSMediaSourceMuxer>(vhost, app, stream);
@ -97,6 +100,10 @@ MultiMediaSourceMuxer::MultiMediaSourceMuxer(const string &vhost, const string &
_fmp4 = std::make_shared<FMP4MediaSourceMuxer>(vhost, app, stream); _fmp4 = std::make_shared<FMP4MediaSourceMuxer>(vhost, app, stream);
} }
#endif #endif
//音频相关设置
enableAudio(option.enable_audio);
enableMuteAudio(option.add_mute_audio);
} }
void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) { void MultiMediaSourceMuxer::setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener) {

View File

@ -39,6 +39,19 @@ public:
bool enable_ts = true; bool enable_ts = true;
//是否开启转换为http-fmp4/ws-fmp4 //是否开启转换为http-fmp4/ws-fmp4
bool enable_fmp4 = true; 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<MultiMediaSourceMuxer>{ class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSink, public std::enable_shared_from_this<MultiMediaSourceMuxer>{