2018-10-25 10:00:17 +08:00
|
|
|
|
/*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Copyright (c) 2016-present The ZLMediaKit project authors. All Rights Reserved.
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* This file is part of ZLMediaKit(https://github.com/ZLMediaKit/ZLMediaKit).
|
2018-10-25 10:00:17 +08:00
|
|
|
|
*
|
2023-12-09 16:23:51 +08:00
|
|
|
|
* Use of this source code is governed by MIT-like license that can be found in the
|
2020-04-04 20:30:09 +08:00
|
|
|
|
* 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"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
#include "Common/MediaSource.h"
|
|
|
|
|
#include "Common/MediaSink.h"
|
2019-12-04 10:45:38 +08:00
|
|
|
|
#include "Record/Recorder.h"
|
2022-12-02 14:43:06 +08:00
|
|
|
|
#include "Rtp/RtpSender.h"
|
|
|
|
|
#include "Record/HlsRecorder.h"
|
|
|
|
|
#include "Record/HlsMediaSource.h"
|
|
|
|
|
#include "Rtsp/RtspMediaSourceMuxer.h"
|
|
|
|
|
#include "Rtmp/RtmpMediaSourceMuxer.h"
|
|
|
|
|
#include "TS/TSMediaSourceMuxer.h"
|
|
|
|
|
#include "FMP4/FMP4MediaSourceMuxer.h"
|
2022-11-29 11:07:13 +08:00
|
|
|
|
|
2022-12-02 14:43:06 +08:00
|
|
|
|
namespace mediakit {
|
2022-03-12 13:19:21 +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:
|
2022-12-02 14:43:06 +08:00
|
|
|
|
using Ptr = std::shared_ptr<MultiMediaSourceMuxer>;
|
2023-04-17 12:19:24 +08:00
|
|
|
|
using RingType = toolkit::RingBuffer<Frame::Ptr>;
|
2021-07-20 11:51:26 +08:00
|
|
|
|
|
2022-11-05 20:47:33 +08:00
|
|
|
|
class Listener {
|
2019-12-26 11:53:19 +08:00
|
|
|
|
public:
|
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
|
virtual void onAllTrackReady() = 0;
|
|
|
|
|
};
|
2020-04-05 09:26:29 +08:00
|
|
|
|
|
2023-05-25 16:23:24 +08:00
|
|
|
|
MultiMediaSourceMuxer(const MediaTuple& tuple, float dur_sec = 0.0,const ProtocolOption &option = ProtocolOption());
|
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 监听器
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Set event listener
|
|
|
|
|
* @param listener Listener
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:d829419b]
|
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
|
|
|
|
/**
|
2023-04-17 12:19:24 +08:00
|
|
|
|
* 设置Track就绪事件监听器
|
2020-08-08 12:20:13 +08:00
|
|
|
|
* @param listener 事件监听器
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Set Track ready event listener
|
|
|
|
|
* @param listener Event listener
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:64262ac5]
|
2020-08-08 12:20:13 +08:00
|
|
|
|
*/
|
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
|
|
|
|
/**
|
|
|
|
|
* 返回总的消费者个数
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Return the total number of consumers
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:5eaac131]
|
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
|
|
|
|
/**
|
|
|
|
|
* 判断是否生效(是否正在转其他协议)
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Determine whether it is effective (whether it is being converted to another protocol)
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:ca92165c]
|
2020-09-06 17:52:07 +08:00
|
|
|
|
*/
|
|
|
|
|
bool isEnabled();
|
|
|
|
|
|
2020-04-05 09:26:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 设置MediaSource时间戳
|
|
|
|
|
* @param stamp 时间戳
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Set MediaSource timestamp
|
|
|
|
|
* @param stamp Timestamp
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:a75cc2fa]
|
2020-04-05 09:26:29 +08:00
|
|
|
|
*/
|
|
|
|
|
void setTimeStamp(uint32_t stamp);
|
|
|
|
|
|
2021-07-20 13:15:57 +08:00
|
|
|
|
/**
|
|
|
|
|
* 重置track
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Reset track
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:95dc0b4f]
|
2021-07-20 13:15:57 +08:00
|
|
|
|
*/
|
|
|
|
|
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 观看总人数
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Total number of viewers
|
|
|
|
|
* @param sender Event sender
|
|
|
|
|
* @return Total number of viewers
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:f4d7146c]
|
2020-04-05 09:26:29 +08:00
|
|
|
|
*/
|
|
|
|
|
int totalReaderCount(MediaSource &sender) override;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置录制状态
|
|
|
|
|
* @param type 录制类型
|
|
|
|
|
* @param start 开始或停止
|
|
|
|
|
* @param custom_path 开启录制时,指定自定义路径
|
|
|
|
|
* @return 是否设置成功
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Set recording status
|
|
|
|
|
* @param type Recording type
|
|
|
|
|
* @param start Start or stop
|
|
|
|
|
* @param custom_path Specify a custom path when recording is enabled
|
|
|
|
|
* @return Whether the setting is successful
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:cb1fd8a9]
|
2020-04-05 09:26:29 +08:00
|
|
|
|
*/
|
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 录制状态
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Get recording status
|
|
|
|
|
* @param type Recording type
|
|
|
|
|
* @return Recording status
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:798afa71]
|
2020-04-05 09:26:29 +08:00
|
|
|
|
*/
|
|
|
|
|
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 启动成功或失败回调
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Start sending ps-rtp stream
|
|
|
|
|
* @param dst_url Target ip or domain name
|
|
|
|
|
* @param dst_port Target port
|
|
|
|
|
* @param ssrc rtp's ssrc
|
|
|
|
|
* @param is_udp Whether it is udp
|
|
|
|
|
* @param cb Start success or failure callback
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:620416c2]
|
2020-09-06 17:56:05 +08:00
|
|
|
|
*/
|
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 是否成功
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Stop ps-rtp sending
|
|
|
|
|
* @return Whether it is successful
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:b91e2055]
|
2020-09-06 17:56:05 +08:00
|
|
|
|
*/
|
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
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Get all Tracks
|
|
|
|
|
* @param trackReady Whether to filter out unready tracks
|
|
|
|
|
* @return All Tracks
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:53755f5d]
|
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
|
|
|
|
|
2022-08-28 17:25:56 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取所属线程
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Get the thread it belongs to
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:a4dc847e]
|
2022-08-28 17:25:56 +08:00
|
|
|
|
*/
|
|
|
|
|
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
|
|
|
|
|
|
2023-07-08 21:35:09 +08:00
|
|
|
|
/**
|
|
|
|
|
* 获取本对象
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* Get this object
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:5e119bb3]
|
2023-07-08 21:35:09 +08:00
|
|
|
|
*/
|
2024-06-09 10:52:10 +08:00
|
|
|
|
std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &sender) const override;
|
2023-07-08 21:35:09 +08:00
|
|
|
|
|
|
|
|
|
const ProtocolOption &getOption() const;
|
|
|
|
|
const MediaTuple &getMediaTuple() const;
|
2022-11-05 20:47:33 +08:00
|
|
|
|
std::string shortUrl() const;
|
2022-08-27 10:53:47 +08:00
|
|
|
|
|
2024-04-21 11:31:43 +08:00
|
|
|
|
void forEachRtpSender(const std::function<void(const std::string &ssrc)> &cb) const;
|
|
|
|
|
|
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
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* A certain track is ready, its ready() status returns true,
|
|
|
|
|
* This means that you can get information such as sps pps, etc.
|
|
|
|
|
* @param track
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:05659d48]
|
2021-07-20 11:51:26 +08:00
|
|
|
|
*/
|
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已经准备好,
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* All Tracks are ready,
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:c54d02e2]
|
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
|
2024-09-19 14:53:50 +08:00
|
|
|
|
* A certain Track outputs a frame, this method will be called after onAllTrackReady is triggered
|
|
|
|
|
* @param frame
|
|
|
|
|
|
|
|
|
|
* [AUTO-TRANSLATED:debbd247]
|
2021-07-20 11:51:26 +08:00
|
|
|
|
*/
|
2021-09-27 13:12:53 +08:00
|
|
|
|
bool onTrackFrame(const Frame::Ptr &frame) override;
|
2023-12-02 10:20:06 +08:00
|
|
|
|
bool onTrackFrame_l(const Frame::Ptr &frame);
|
2021-07-20 11:51:26 +08:00
|
|
|
|
|
2023-04-17 12:19:24 +08:00
|
|
|
|
private:
|
|
|
|
|
void createGopCacheIfNeed();
|
2024-10-18 22:22:03 +08:00
|
|
|
|
std::shared_ptr<MediaSinkInterface> makeRecorder(MediaSource &sender, Recorder::type type);
|
2023-04-17 12:19:24 +08:00
|
|
|
|
|
2018-10-24 17:17:55 +08:00
|
|
|
|
private:
|
2020-11-15 00:59:31 +08:00
|
|
|
|
bool _is_enable = false;
|
2022-10-31 17:53:20 +08:00
|
|
|
|
bool _create_in_poller = false;
|
2023-04-17 12:19:24 +08:00
|
|
|
|
bool _video_key_pos = false;
|
2023-12-09 22:34:22 +08:00
|
|
|
|
float _dur_sec;
|
2023-12-02 10:20:06 +08:00
|
|
|
|
std::shared_ptr<class FramePacedSender> _paced_sender;
|
2023-05-25 16:23:24 +08:00
|
|
|
|
MediaTuple _tuple;
|
2022-08-16 11:47:24 +08:00
|
|
|
|
ProtocolOption _option;
|
2022-02-02 20:34:50 +08:00
|
|
|
|
toolkit::Ticker _last_check;
|
2023-12-09 22:34:22 +08:00
|
|
|
|
std::unordered_map<int, Stamp> _stamps;
|
2021-07-20 11:51:26 +08:00
|
|
|
|
std::weak_ptr<Listener> _track_listener;
|
2023-11-07 23:38:58 +08:00
|
|
|
|
std::unordered_multimap<std::string, RingType::RingReader::Ptr> _rtp_sender;
|
2022-12-02 14:43:06 +08:00
|
|
|
|
FMP4MediaSourceMuxer::Ptr _fmp4;
|
|
|
|
|
RtmpMediaSourceMuxer::Ptr _rtmp;
|
|
|
|
|
RtspMediaSourceMuxer::Ptr _rtsp;
|
|
|
|
|
TSMediaSourceMuxer::Ptr _ts;
|
2021-07-20 11:51:26 +08:00
|
|
|
|
MediaSinkInterface::Ptr _mp4;
|
2022-12-02 14:43:06 +08:00
|
|
|
|
HlsRecorder::Ptr _hls;
|
2023-07-02 12:02:33 +08:00
|
|
|
|
HlsFMP4Recorder::Ptr _hls_fmp4;
|
2022-08-28 17:25:56 +08:00
|
|
|
|
toolkit::EventPoller::Ptr _poller;
|
2023-04-17 12:19:24 +08:00
|
|
|
|
RingType::Ptr _ring;
|
2021-07-20 11:51:26 +08:00
|
|
|
|
|
2024-09-19 14:53:50 +08:00
|
|
|
|
// 对象个数统计 [AUTO-TRANSLATED:3b43e8c2]
|
|
|
|
|
// Object count statistics
|
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
|