ZLMediaKit/src/Common/MultiMediaSourceMuxer.h

266 lines
7.9 KiB
C++
Raw Normal View History

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
#include "Common/Stamp.h"
#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-12-02 14:43:06 +08:00
namespace mediakit {
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>;
using RingType = toolkit::RingBuffer<Frame::Ptr>;
class Listener {
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());
2018-10-25 16:49:43 +08:00
/**
*
* @param listener
* 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
/**
* Track就绪事件监听器
* @param listener
* Set Track ready event listener
* @param listener Event listener
* [AUTO-TRANSLATED:64262ac5]
*/
void setTrackListener(const std::weak_ptr<Listener> &listener);
2018-10-26 14:12:16 +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
/**
* ()
* 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
* 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
* 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
* 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
* 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
*/
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
* 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
* 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
* Stop ps-rtp sending
* @return Whether it is successful
* [AUTO-TRANSLATED:b91e2055]
2020-09-06 17:56:05 +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
* 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
*/
std::vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const override;
2020-04-05 09:26:29 +08:00
/**
* 线
* Get the thread it belongs to
* [AUTO-TRANSLATED:a4dc847e]
*/
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
/**
*
* Get this object
* [AUTO-TRANSLATED:5e119bb3]
*/
std::shared_ptr<MultiMediaSourceMuxer> getMuxer(MediaSource &sender) const override;
const ProtocolOption &getOption() const;
const MediaTuple &getMediaTuple() const;
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;
protected:
/////////////////////////////////MediaSink override/////////////////////////////////
/**
* track已经准备好ready()true
* sps pps等相关信息了
* @param track
* 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]
*/
bool onTrackReady(const Track::Ptr & track) override;
2020-09-06 17:52:07 +08:00
/**
* Track已经准备好
* All Tracks are ready,
* [AUTO-TRANSLATED:c54d02e2]
*/
2020-09-06 17:52:07 +08:00
void onAllTrackReady() override;
/**
* Track输出frameonAllTrackReady触发后才会调用此方法
* @param frame
* A certain Track outputs a frame, this method will be called after onAllTrackReady is triggered
* @param frame
* [AUTO-TRANSLATED:debbd247]
*/
bool onTrackFrame(const Frame::Ptr &frame) override;
2023-12-02 10:20:06 +08:00
bool onTrackFrame_l(const Frame::Ptr &frame);
private:
void createGopCacheIfNeed();
std::shared_ptr<MediaSinkInterface> makeRecorder(MediaSource &sender, Recorder::type type);
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;
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;
ProtocolOption _option;
toolkit::Ticker _last_check;
2023-12-09 22:34:22 +08:00
std::unordered_map<int, Stamp> _stamps;
std::weak_ptr<Listener> _track_listener;
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;
MediaSinkInterface::Ptr _mp4;
2022-12-02 14:43:06 +08:00
HlsRecorder::Ptr _hls;
HlsFMP4Recorder::Ptr _hls_fmp4;
toolkit::EventPoller::Ptr _poller;
RingType::Ptr _ring;
// 对象个数统计 [AUTO-TRANSLATED:3b43e8c2]
// Object count statistics
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