ZLMediaKit/src/Common/MultiMediaSourceMuxer.h

184 lines
5.2 KiB
C++
Raw Normal View History

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
*
* 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
#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:
Listener() = default;
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());
~MultiMediaSourceMuxer() override = default;
2018-10-25 16:49:43 +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
/**
* Track就绪事件监听器
* @param listener
*/
void setTrackListener(const std::weak_ptr<Listener> &listener);
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
*/
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
*/
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
*/
std::vector<Track::Ptr> getMediaTracks(MediaSource &sender, bool trackReady = true) const override;
2020-04-05 09:26:29 +08:00
/**
* 线
*/
toolkit::EventPoller::Ptr getOwnerPoller(MediaSource &sender) override;
2023-05-25 16:23:24 +08:00
const MediaTuple& getMediaTuple() const {
return _tuple;
}
std::string shortUrl() const;
2022-08-27 10:53:47 +08:00
protected:
/////////////////////////////////MediaSink override/////////////////////////////////
/**
* track已经准备好ready()true
* sps pps等相关信息了
* @param track
*/
bool onTrackReady(const Track::Ptr & track) override;
2020-09-06 17:52:07 +08:00
/**
* Track已经准备好
*/
2020-09-06 17:52:07 +08:00
void onAllTrackReady() override;
/**
* Track输出frameonAllTrackReady触发后才会调用此方法
* @param frame
*/
bool onTrackFrame(const Frame::Ptr &frame) override;
private:
void createGopCacheIfNeed();
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-05-25 16:23:24 +08:00
MediaTuple _tuple;
ProtocolOption _option;
toolkit::Ticker _last_check;
2020-09-06 17:52:07 +08:00
Stamp _stamp[2];
std::weak_ptr<Listener> _track_listener;
std::unordered_map<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;
2021-01-23 09:44:37 +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