2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2021-01-17 18:31:50 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* Use of this source code is governed by MIT license that can be found in the
|
|
|
|
|
* LICENSE file in the root of the source tree. All contributing project authors
|
|
|
|
|
* may be found in the AUTHORS file in the root of the source tree.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*/
|
2018-10-24 17:17:55 +08:00
|
|
|
|
|
2018-10-25 16:46:00 +08:00
|
|
|
|
#ifndef ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
|
|
|
|
|
#define ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
|
2020-09-12 19:46:58 +08:00
|
|
|
|
|
|
|
|
|
#include "Common/Stamp.h"
|
2020-10-24 23:33:13 +08:00
|
|
|
|
#include "Rtp/RtpSender.h"
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "Record/Recorder.h"
|
2019-12-29 10:49:04 +08:00
|
|
|
|
#include "Record/HlsRecorder.h"
|
2020-09-12 19:46:58 +08:00
|
|
|
|
#include "Record/HlsMediaSource.h"
|
|
|
|
|
#include "Rtsp/RtspMediaSourceMuxer.h"
|
|
|
|
|
#include "Rtmp/RtmpMediaSourceMuxer.h"
|
2020-09-20 00:21:46 +08:00
|
|
|
|
#include "TS/TSMediaSourceMuxer.h"
|
2020-09-20 19:45:37 +08:00
|
|
|
|
#include "FMP4/FMP4MediaSourceMuxer.h"
|
2020-09-06 17:56:05 +08:00
|
|
|
|
|
2022-03-12 13:19:21 +08:00
|
|
|
|
namespace mediakit {
|
|
|
|
|
|
|
|
|
|
class ProtocolOption {
|
|
|
|
|
public:
|
|
|
|
|
ProtocolOption();
|
|
|
|
|
|
|
|
|
|
//是否开启转换为hls
|
|
|
|
|
bool enable_hls = false;
|
|
|
|
|
//是否开启MP4录制
|
|
|
|
|
bool enable_mp4 = false;
|
2022-08-16 11:47:24 +08:00
|
|
|
|
//是否将mp4录制当做观看者
|
|
|
|
|
bool mp4_as_player = false;
|
2022-03-12 13:19:21 +08:00
|
|
|
|
//是否开启转换为rtsp/webrtc
|
|
|
|
|
bool enable_rtsp = true;
|
|
|
|
|
//是否开启转换为rtmp/flv
|
|
|
|
|
bool enable_rtmp = true;
|
|
|
|
|
//是否开启转换为http-ts/ws-ts
|
|
|
|
|
bool enable_ts = true;
|
|
|
|
|
//是否开启转换为http-fmp4/ws-fmp4
|
|
|
|
|
bool enable_fmp4 = true;
|
2022-03-12 14:34:48 +08:00
|
|
|
|
|
|
|
|
|
//转协议是否开启音频
|
|
|
|
|
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;
|
2022-06-04 11:06:35 +08:00
|
|
|
|
|
|
|
|
|
//断连续推延时,单位毫秒,默认采用配置文件
|
|
|
|
|
uint32_t continue_push_ms;
|
2022-03-12 13:19:21 +08:00
|
|
|
|
};
|
2018-10-24 17:17:55 +08:00
|
|
|
|
|
2021-07-20 11:51:26 +08:00
|
|
|
|
class MultiMediaSourceMuxer : public MediaSourceEventInterceptor, public MediaSink, public std::enable_shared_from_this<MultiMediaSourceMuxer>{
|
2018-10-24 17:17:55 +08:00
|
|
|
|
public:
|
2021-07-20 11:51:26 +08:00
|
|
|
|
typedef std::shared_ptr<MultiMediaSourceMuxer> Ptr;
|
|
|
|
|
|
2019-12-26 11:53:19 +08:00
|
|
|
|
class Listener{
|
|
|
|
|
public:
|
|
|
|
|
Listener() = default;
|
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
|
virtual void onAllTrackReady() = 0;
|
|
|
|
|
};
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
2022-03-12 13:19:21 +08:00
|
|
|
|
MultiMediaSourceMuxer(const std::string &vhost, const std::string &app, const std::string &stream, float dur_sec = 0.0,const ProtocolOption &option = ProtocolOption());
|
2021-07-20 11:51:26 +08:00
|
|
|
|
~MultiMediaSourceMuxer() override = default;
|
2019-10-11 16:51:10 +08:00
|
|
|
|
|
2018-10-25 16:49:43 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置事件监听器
|
2020-08-08 12:20:13 +08:00
|
|
|
|
* @param listener 监听器
|
2018-10-25 16:49:43 +08:00
|
|
|
|
*/
|
2020-04-05 09:26:29 +08:00
|
|
|
|
void setMediaListener(const std::weak_ptr<MediaSourceEvent> &listener);
|
2018-10-26 14:12:16 +08:00
|
|
|
|
|
2020-08-08 12:20:13 +08:00
|
|
|
|
/**
|
|
|
|
|
* 随着Track就绪事件监听器
|
|
|
|
|
* @param listener 事件监听器
|
|
|
|
|
*/
|
2021-07-20 11:51:26 +08:00
|
|
|
|
void setTrackListener(const std::weak_ptr<Listener> &listener);
|
2020-08-08 12:20:13 +08:00
|
|
|
|
|
2018-10-26 14:12:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 返回总的消费者个数
|
|
|
|
|
*/
|
2020-04-05 09:26:29 +08:00
|
|
|
|
int totalReaderCount() const;
|
|
|
|
|
|
2020-09-06 17:52:07 +08:00
|
|
|
|
/**
|
|
|
|
|
* 判断是否生效(是否正在转其他协议)
|
|
|
|
|
*/
|
|
|
|
|
bool isEnabled();
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置MediaSource时间戳
|
|
|
|
|
* @param stamp 时间戳
|
|
|
|
|
*/
|
|
|
|
|
void setTimeStamp(uint32_t stamp);
|
|
|
|
|
|
2021-07-20 13:15:57 +08:00
|
|
|
|
/**
|
|
|
|
|
* 重置track
|
|
|
|
|
*/
|
|
|
|
|
void resetTracks() override;
|
|
|
|
|
|
2020-09-06 17:54:52 +08:00
|
|
|
|
/////////////////////////////////MediaSourceEvent override/////////////////////////////////
|
2020-09-06 17:52:07 +08:00
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 观看总人数
|
|
|
|
|
* @param sender 事件发送者
|
|
|
|
|
* @return 观看总人数
|
|
|
|
|
*/
|
|
|
|
|
int totalReaderCount(MediaSource &sender) override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置录制状态
|
|
|
|
|
* @param type 录制类型
|
|
|
|
|
* @param start 开始或停止
|
|
|
|
|
* @param custom_path 开启录制时,指定自定义路径
|
|
|
|
|
* @return 是否设置成功
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool setupRecord(MediaSource &sender, Recorder::type type, bool start, const std::string &custom_path, size_t max_second) override;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取录制状态
|
|
|
|
|
* @param type 录制类型
|
|
|
|
|
* @return 录制状态
|
|
|
|
|
*/
|
|
|
|
|
bool isRecording(MediaSource &sender, Recorder::type type) override;
|
|
|
|
|
|
2020-09-06 17:56:05 +08:00
|
|
|
|
/**
|
|
|
|
|
* 开始发送ps-rtp流
|
|
|
|
|
* @param dst_url 目标ip或域名
|
|
|
|
|
* @param dst_port 目标端口
|
|
|
|
|
* @param ssrc rtp的ssrc
|
|
|
|
|
* @param is_udp 是否为udp
|
|
|
|
|
* @param cb 启动成功或失败回调
|
|
|
|
|
*/
|
2022-04-03 18:25:36 +08:00
|
|
|
|
void startSendRtp(MediaSource &sender, const MediaSourceEvent::SendRtpArgs &args, const std::function<void(uint16_t, const toolkit::SockException &)> cb) override;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 停止ps-rtp发送
|
|
|
|
|
* @return 是否成功
|
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
bool stopSendRtp(MediaSource &sender, const std::string &ssrc) override;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
/**
|
2021-07-20 13:15:57 +08:00
|
|
|
|
* 获取所有Track
|
|
|
|
|
* @param trackReady 是否筛选过滤未就绪的track
|
|
|
|
|
* @return 所有Track
|
2020-04-05 09:26:29 +08:00
|
|
|
|
*/
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const override;
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
2021-07-20 11:51:26 +08:00
|
|
|
|
protected:
|
|
|
|
|
/////////////////////////////////MediaSink override/////////////////////////////////
|
2020-04-29 11:59:45 +08:00
|
|
|
|
|
2021-07-20 11:51:26 +08:00
|
|
|
|
/**
|
|
|
|
|
* 某track已经准备好,其ready()状态返回true,
|
|
|
|
|
* 此时代表可以获取其例如sps pps等相关信息了
|
|
|
|
|
* @param track
|
|
|
|
|
*/
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool onTrackReady(const Track::Ptr & track) override;
|
2020-09-06 17:52:07 +08:00
|
|
|
|
|
2020-04-29 11:59:45 +08:00
|
|
|
|
/**
|
2021-07-20 11:51:26 +08:00
|
|
|
|
* 所有Track已经准备好,
|
2020-04-29 11:59:45 +08:00
|
|
|
|
*/
|
2020-09-06 17:52:07 +08:00
|
|
|
|
void onAllTrackReady() override;
|
2020-08-08 12:20:13 +08:00
|
|
|
|
|
2021-07-20 11:51:26 +08:00
|
|
|
|
/**
|
|
|
|
|
* 某Track输出frame,在onAllTrackReady触发后才会调用此方法
|
|
|
|
|
* @param frame
|
|
|
|
|
*/
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool onTrackFrame(const Frame::Ptr &frame) override;
|
2021-07-20 11:51:26 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
private:
|
2020-11-15 00:59:31 +08:00
|
|
|
|
bool _is_enable = false;
|
2022-08-16 11:47:24 +08:00
|
|
|
|
ProtocolOption _option;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _last_check;
|
2020-09-06 17:52:07 +08:00
|
|
|
|
Stamp _stamp[2];
|
2021-07-20 11:51:26 +08:00
|
|
|
|
std::weak_ptr<Listener> _track_listener;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::function<std::string()> _get_origin_url;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#if defined(ENABLE_RTPPROXY)
|
2022-02-02 20:34:50 +08:00
|
|
|
|
std::unordered_map<std::string, RtpSender::Ptr> _rtp_sender;
|
2020-09-06 17:56:05 +08:00
|
|
|
|
#endif //ENABLE_RTPPROXY
|
2021-07-20 11:51:26 +08:00
|
|
|
|
|
|
|
|
|
#if defined(ENABLE_MP4)
|
|
|
|
|
FMP4MediaSourceMuxer::Ptr _fmp4;
|
|
|
|
|
#endif
|
|
|
|
|
RtmpMediaSourceMuxer::Ptr _rtmp;
|
|
|
|
|
RtspMediaSourceMuxer::Ptr _rtsp;
|
|
|
|
|
TSMediaSourceMuxer::Ptr _ts;
|
|
|
|
|
MediaSinkInterface::Ptr _mp4;
|
|
|
|
|
HlsRecorder::Ptr _hls;
|
|
|
|
|
|
2021-01-23 09:44:37 +08:00
|
|
|
|
//对象个数统计
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::ObjectStatistic<MultiMediaSourceMuxer> _statistic;
|
2018-10-24 17:17:55 +08:00
|
|
|
|
};
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
}//namespace mediakit
|
2018-10-25 16:46:00 +08:00
|
|
|
|
#endif //ZLMEDIAKIT_MULTIMEDIASOURCEMUXER_H
|