2022-06-11 12:56:03 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright ( c ) 2016 - present The ZLMediaKit project authors . All Rights Reserved .
2018-02-02 18:19:35 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit ( https : //github.com/ZLMediaKit/ZLMediaKit).
2018-02-02 18:19:35 +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-02-02 18:19:35 +08:00
*/
# ifndef ZLMEDIAKIT_MEDIASOURCE_H
# define ZLMEDIAKIT_MEDIASOURCE_H
# include <string>
2022-01-10 16:37:50 +08:00
# include <atomic>
2018-02-02 18:19:35 +08:00
# include <memory>
# include <functional>
2024-06-28 19:35:03 +08:00
# include "Util/mini.h"
2020-09-27 11:32:49 +08:00
# include "Network/Socket.h"
2019-06-28 16:48:02 +08:00
# include "Extension/Track.h"
2020-04-05 09:26:29 +08:00
# include "Record/Recorder.h"
2018-02-02 18:19:35 +08:00
2022-10-31 17:53:20 +08:00
namespace toolkit {
class Session ;
} // namespace toolkit
2019-05-27 22:32:07 +08:00
2018-10-24 17:17:55 +08:00
namespace mediakit {
2018-02-02 18:19:35 +08:00
2020-09-27 11:32:49 +08:00
enum class MediaOriginType : uint8_t {
unknown = 0 ,
rtmp_push ,
rtsp_push ,
rtp_push ,
pull ,
ffmpeg_pull ,
mp4_vod ,
2021-04-07 18:17:49 +08:00
device_chn ,
rtc_push ,
2022-06-03 15:02:07 +08:00
srt_push
2020-09-27 11:32:49 +08:00
} ;
2022-02-02 20:34:50 +08:00
std : : string getOriginTypeString ( MediaOriginType type ) ;
2020-09-27 11:32:49 +08:00
2019-05-27 18:39:43 +08:00
class MediaSource ;
2024-06-09 10:52:10 +08:00
class RtpProcess ;
2023-07-08 21:35:09 +08:00
class MultiMediaSourceMuxer ;
2022-04-03 18:25:36 +08:00
class MediaSourceEvent {
2018-02-02 18:19:35 +08:00
public :
2020-03-23 10:21:17 +08:00
friend class MediaSource ;
2022-08-28 17:25:56 +08:00
class NotImplemented : public std : : runtime_error {
public :
template < typename . . . T >
NotImplemented ( T & & . . . args ) : std : : runtime_error ( std : : forward < T > ( args ) . . . ) { }
} ;
2022-11-12 21:59:48 +08:00
virtual ~ MediaSourceEvent ( ) = default ;
2019-12-03 16:10:02 +08:00
2024-09-19 14:53:50 +08:00
// 获取媒体源类型 [AUTO-TRANSLATED:34290a69]
// Get media source type
2020-09-27 11:32:49 +08:00
virtual MediaOriginType getOriginType ( MediaSource & sender ) const { return MediaOriginType : : unknown ; }
2024-09-19 14:53:50 +08:00
// 获取媒体源url或者文件路径 [AUTO-TRANSLATED:fa34d795]
// Get media source url or file path
2022-02-02 20:34:50 +08:00
virtual std : : string getOriginUrl ( MediaSource & sender ) const ;
2024-09-19 14:53:50 +08:00
// 获取媒体源客户端相关信息 [AUTO-TRANSLATED:037ef910]
// Get media source client related information
2022-02-02 20:34:50 +08:00
virtual std : : shared_ptr < toolkit : : SockInfo > getOriginSock ( MediaSource & sender ) const { return nullptr ; }
2020-09-27 11:32:49 +08:00
2024-09-19 14:53:50 +08:00
// 通知拖动进度条 [AUTO-TRANSLATED:561b17f7]
// Notify drag progress bar
2020-09-06 17:52:07 +08:00
virtual bool seekTo ( MediaSource & sender , uint32_t stamp ) { return false ; }
2024-09-19 14:53:50 +08:00
// 通知暂停或恢复 [AUTO-TRANSLATED:ee3c219f]
// Notify pause or resume
2021-08-12 16:07:31 +08:00
virtual bool pause ( MediaSource & sender , bool pause ) { return false ; }
2024-09-19 14:53:50 +08:00
// 通知倍数 [AUTO-TRANSLATED:8f1dab15]
// Notify multiple times
2021-08-12 16:07:31 +08:00
virtual bool speed ( MediaSource & sender , float speed ) { return false ; }
2024-09-19 14:53:50 +08:00
// 通知其停止产生流 [AUTO-TRANSLATED:62c9022c]
// Notify it to stop generating streams
2022-09-18 20:36:47 +08:00
virtual bool close ( MediaSource & sender ) { return false ; }
2024-09-19 14:53:50 +08:00
// 获取观看总人数,此函数一般强制重载 [AUTO-TRANSLATED:1da20a10]
// Get the total number of viewers, this function is generally forced to overload
2022-08-28 17:25:56 +08:00
virtual int totalReaderCount ( MediaSource & sender ) { throw NotImplemented ( toolkit : : demangle ( typeid ( * this ) . name ( ) ) + " ::totalReaderCount not implemented " ) ; }
2024-09-19 14:53:50 +08:00
// 通知观看人数变化 [AUTO-TRANSLATED:bad89528]
// Notify the change in the number of viewers
2020-09-12 19:09:56 +08:00
virtual void onReaderChanged ( MediaSource & sender , int size ) ;
2024-09-19 14:53:50 +08:00
// 流注册或注销事件 [AUTO-TRANSLATED:2cac8178]
// Stream registration or deregistration event
2022-09-07 11:06:39 +08:00
virtual void onRegist ( MediaSource & sender , bool regist ) { }
2024-09-19 14:53:50 +08:00
// 获取丢包率 [AUTO-TRANSLATED:ec61b378]
// Get packet loss rate
2022-09-03 15:53:01 +08:00
virtual float getLossRate ( MediaSource & sender , TrackType type ) { return - 1 ; }
2024-09-19 14:53:50 +08:00
// 获取所在线程, 此函数一般强制重载 [AUTO-TRANSLATED:71c99afb]
// Get the current thread, this function is generally forced to overload
2022-08-28 17:25:56 +08:00
virtual toolkit : : EventPoller : : Ptr getOwnerPoller ( MediaSource & sender ) { throw NotImplemented ( toolkit : : demangle ( typeid ( * this ) . name ( ) ) + " ::getOwnerPoller not implemented " ) ; }
2022-06-11 04:31:06 +00:00
2024-09-19 14:53:50 +08:00
// //////////////////////仅供MultiMediaSourceMuxer对象继承//////////////////////// [AUTO-TRANSLATED:6e810d1f]
// //////////////////////Only for MultiMediaSourceMuxer object inheritance////////////////////////
// 开启或关闭录制 [AUTO-TRANSLATED:3817e390]
// Start or stop recording
2022-02-02 20:34:50 +08:00
virtual bool setupRecord ( MediaSource & sender , Recorder : : type type , bool start , const std : : string & custom_path , size_t max_second ) { return false ; } ;
2024-09-19 14:53:50 +08:00
// 获取录制状态 [AUTO-TRANSLATED:a0499880]
// Get recording status
2022-09-07 11:06:39 +08:00
virtual bool isRecording ( MediaSource & sender , Recorder : : type type ) { return false ; }
2024-09-19 14:53:50 +08:00
// 获取所有track相关信息 [AUTO-TRANSLATED:2141be42]
// Get all track related information
2022-02-02 20:34:50 +08:00
virtual std : : vector < Track : : Ptr > getMediaTracks ( MediaSource & sender , bool trackReady = true ) const { return std : : vector < Track : : Ptr > ( ) ; } ;
2024-09-19 14:53:50 +08:00
// 获取MultiMediaSourceMuxer对象 [AUTO-TRANSLATED:2de96d44]
// Get MultiMediaSourceMuxer object
2024-06-09 10:52:10 +08:00
virtual std : : shared_ptr < MultiMediaSourceMuxer > getMuxer ( MediaSource & sender ) const { return nullptr ; }
2024-09-19 14:53:50 +08:00
// 获取RtpProcess对象 [AUTO-TRANSLATED:c6b7da43]
// Get RtpProcess object
2024-06-09 10:52:10 +08:00
virtual std : : shared_ptr < RtpProcess > getRtpProcess ( MediaSource & sender ) const { return nullptr ; }
2022-04-03 18:25:36 +08:00
class SendRtpArgs {
public :
2024-07-28 22:55:33 +08:00
enum DataType {
kRtpES = 0 , // 发送ES流
kRtpPS = 1 , // 发送PS流
kRtpTS = 2 // 发送TS流
} ;
enum ConType {
kTcpActive = 0 , // tcp主动模式, tcp客户端主动连接对方并发送rtp
kUdpActive = 1 , // udp主动模式, 主动发送数据给对方
kTcpPassive = 2 , // tcp被动模式, tcp服务器, 等待对方连接并回复rtp
kUdpPassive = 3 // udp被动方式, 等待对方发送nat打洞包, 然后回复rtp至打洞包源地址
} ;
2024-09-19 14:53:50 +08:00
// rtp类型 [AUTO-TRANSLATED:acca40ab]
// Rtp type
2024-07-28 22:55:33 +08:00
DataType data_type = kRtpPS ;
2024-09-19 14:53:50 +08:00
// 连接类型 [AUTO-TRANSLATED:8ad5c881]
// Connection type
2024-07-28 22:55:33 +08:00
ConType con_type = kUdpActive ;
2024-09-19 14:53:50 +08:00
// 发送es流时指定是否只发送纯音频流 [AUTO-TRANSLATED:470c761e]
// Specify whether to send only pure audio stream when sending es stream
2023-11-02 12:51:08 +08:00
bool only_audio = false ;
2022-04-03 18:25:36 +08:00
// rtp payload type
uint8_t pt = 96 ;
2024-09-19 14:53:50 +08:00
// 是否支持同ssrc多服务器发送 [AUTO-TRANSLATED:9d817af2]
// Whether to support multiple servers sending with the same ssrc
2023-11-07 23:38:58 +08:00
bool ssrc_multi_send = false ;
2024-09-19 14:53:50 +08:00
// 指定rtp ssrc [AUTO-TRANSLATED:7366c6f9]
// Specify rtp ssrc
2022-04-03 18:25:36 +08:00
std : : string ssrc ;
2024-09-19 14:53:50 +08:00
// 指定本地发送端口 [AUTO-TRANSLATED:f5d92f40]
// Specify local sending port
2022-04-03 18:25:36 +08:00
uint16_t src_port = 0 ;
2024-09-19 14:53:50 +08:00
// 发送目标端口 [AUTO-TRANSLATED:096b5574]
// Send target port
2022-04-03 18:25:36 +08:00
uint16_t dst_port ;
2024-09-19 14:53:50 +08:00
// 发送目标主机地址, 可以是ip或域名 [AUTO-TRANSLATED:2c872f2e]
// Send target host address, can be ip or domain name
2022-04-03 18:25:36 +08:00
std : : string dst_url ;
2022-08-20 12:48:27 +08:00
2024-09-19 14:53:50 +08:00
// udp发送时, 是否开启rr rtcp接收超时判断 [AUTO-TRANSLATED:784982bd]
// When sending udp, whether to enable rr rtcp receive timeout judgment
2022-08-20 12:48:27 +08:00
bool udp_rtcp_timeout = false ;
2024-09-19 14:53:50 +08:00
// passive被动、tcp主动模式超时时间 [AUTO-TRANSLATED:8886d475]
// Passive passive, tcp active mode timeout time
2024-07-28 22:55:33 +08:00
uint32_t close_delay_ms = 0 ;
2024-09-19 14:53:50 +08:00
// udp 发送时, rr rtcp包接收超时时间, 单位毫秒 [AUTO-TRANSLATED:9f0d91d9]
// When sending udp, rr rtcp packet receive timeout time, in milliseconds
2022-08-20 12:48:27 +08:00
uint32_t rtcp_timeout_ms = 30 * 1000 ;
2024-09-19 14:53:50 +08:00
// udp 发送时, 发送sr rtcp包间隔, 单位毫秒 [AUTO-TRANSLATED:c87bfed4]
// When sending udp, send sr rtcp packet interval, in milliseconds
2022-08-20 12:48:27 +08:00
uint32_t rtcp_send_interval_ms = 5 * 1000 ;
2023-01-07 22:36:30 +08:00
2024-09-19 14:53:50 +08:00
// 发送rtp同时接收, 一般用于双向语言对讲, 如果不为空,说明开启接收 [AUTO-TRANSLATED:f4c18084]
// Send rtp while receiving, generally used for two-way language intercom, if not empty, it means receiving is enabled
2023-01-07 22:36:30 +08:00
std : : string recv_stream_id ;
2024-08-22 19:50:29 +08:00
std : : string recv_stream_app ;
std : : string recv_stream_vhost ;
2022-04-03 18:25:36 +08:00
} ;
2024-09-19 14:53:50 +08:00
// 开始发送ps-rtp [AUTO-TRANSLATED:a51796fa]
// Start sending ps-rtp
2022-04-03 18:25:36 +08:00
virtual void startSendRtp ( MediaSource & sender , const SendRtpArgs & args , const std : : function < void ( uint16_t , const toolkit : : SockException & ) > cb ) { cb ( 0 , toolkit : : SockException ( toolkit : : Err_other , " not implemented " ) ) ; } ;
2024-09-19 14:53:50 +08:00
// 停止发送ps-rtp [AUTO-TRANSLATED:952d2b35]
// Stop sending ps-rtp
2022-02-02 20:34:50 +08:00
virtual bool stopSendRtp ( MediaSource & sender , const std : : string & ssrc ) { return false ; }
2020-09-06 17:52:07 +08:00
2020-03-23 10:21:17 +08:00
private :
2022-02-02 20:34:50 +08:00
toolkit : : Timer : : Ptr _async_close_timer ;
2018-02-02 18:19:35 +08:00
} ;
2019-05-27 18:39:43 +08:00
2024-03-23 11:46:30 -03:00
template < typename MAP , typename KEY , typename TYPE >
static void getArgsValue ( const MAP & allArgs , const KEY & key , TYPE & value ) {
auto val = ( ( MAP & ) allArgs ) [ key ] ;
if ( ! val . empty ( ) ) {
value = ( TYPE ) val ;
}
}
2024-06-28 19:35:03 +08:00
template < typename KEY , typename TYPE >
static void getArgsValue ( const toolkit : : mINI & allArgs , const KEY & key , TYPE & value ) {
auto it = allArgs . find ( key ) ;
if ( it ! = allArgs . end ( ) ) {
value = ( TYPE ) it - > second ;
}
}
2022-11-12 23:54:35 +08:00
class ProtocolOption {
public :
ProtocolOption ( ) ;
2023-06-03 17:34:34 +08:00
enum {
kModifyStampOff = 0 , // 采用源视频流绝对时间戳,不做任何改变
kModifyStampSystem = 1 , // 采用zlmediakit接收数据时的系统时间戳(有平滑处理)
kModifyStampRelative = 2 // 采用源视频流时间戳相对时间戳(增长量),有做时间戳跳跃和回退矫正
} ;
2024-09-19 14:53:50 +08:00
// 时间戳类型 [AUTO-TRANSLATED:7d2779e1]
// Timestamp type
2023-06-03 17:34:34 +08:00
int modify_stamp ;
2024-09-19 14:53:50 +08:00
// 转协议是否开启音频 [AUTO-TRANSLATED:220dddfa]
// Whether to enable audio for protocol conversion
2022-11-12 23:54:35 +08:00
bool enable_audio ;
2024-09-19 14:53:50 +08:00
// 添加静音音频,在关闭音频时,此开关无效 [AUTO-TRANSLATED:47c0ec8e]
// Add mute audio, this switch is invalid when audio is closed
2022-11-12 23:54:35 +08:00
bool add_mute_audio ;
2024-09-19 14:53:50 +08:00
// 无人观看时,是否直接关闭(而不是通过on_none_reader hook返回close) [AUTO-TRANSLATED:dba7ab70]
// Whether to close directly when no one is watching (instead of returning close through the on_none_reader hook)
// 此配置置1时, 此流如果无人观看, 将不触发on_none_reader hook回调, [AUTO-TRANSLATED:a5ead314]
// When this configuration is set to 1, if no one is watching this stream, it will not trigger the on_none_reader hook callback,
// 而是将直接关闭流 [AUTO-TRANSLATED:06887d49]
// but will directly close the stream
2023-07-08 21:36:34 +08:00
bool auto_close ;
2024-09-19 14:53:50 +08:00
// 断连续推延时,单位毫秒,默认采用配置文件 [AUTO-TRANSLATED:7a15b12f]
// Delay in milliseconds for continuous pushing, default is using the configuration file
2022-11-12 23:54:35 +08:00
uint32_t continue_push_ms ;
2024-09-19 14:53:50 +08:00
// 平滑发送定时器间隔, 单位毫秒, 置0则关闭; 开启后影响cpu性能同时增加内存 [AUTO-TRANSLATED:ad4e306a]
// Smooth sending timer interval, in milliseconds, set to 0 to close; enabling it will affect cpu performance and increase memory at the same time
// 该配置开启后可以解决一些流发送不平滑导致zlmediakit转发也不平滑的问题 [AUTO-TRANSLATED:0f2b1657]
// This configuration can solve some problems where the stream is not sent smoothly, resulting in zlmediakit forwarding not being smooth
2023-12-02 10:20:06 +08:00
uint32_t paced_sender_ms ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为hls(mpegts) [AUTO-TRANSLATED:bfc1167a]
// Whether to enable conversion to hls(mpegts)
2022-11-12 23:54:35 +08:00
bool enable_hls ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为hls(fmp4) [AUTO-TRANSLATED:20548673]
// Whether to enable conversion to hls(fmp4)
2023-07-02 12:02:33 +08:00
bool enable_hls_fmp4 ;
2024-09-19 14:53:50 +08:00
// 是否开启MP4录制 [AUTO-TRANSLATED:0157b014]
// Whether to enable MP4 recording
2022-11-12 23:54:35 +08:00
bool enable_mp4 ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为rtsp/webrtc [AUTO-TRANSLATED:0711cb18]
// Whether to enable conversion to rtsp/webrtc
2022-11-12 23:54:35 +08:00
bool enable_rtsp ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为rtmp/flv [AUTO-TRANSLATED:d4774119]
// Whether to enable conversion to rtmp/flv
2022-11-12 23:54:35 +08:00
bool enable_rtmp ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为http-ts/ws-ts [AUTO-TRANSLATED:51acc798]
// Whether to enable conversion to http-ts/ws-ts
2022-11-12 23:54:35 +08:00
bool enable_ts ;
2024-09-19 14:53:50 +08:00
// 是否开启转换为http-fmp4/ws-fmp4 [AUTO-TRANSLATED:8c96e1e4]
// Whether to enable conversion to http-fmp4/ws-fmp4
2022-11-12 23:54:35 +08:00
bool enable_fmp4 ;
2024-09-19 14:53:50 +08:00
// hls协议是否按需生成, 如果hls.segNum配置为0(意味着hls录制), 那么hls将一直生成(不管此开关) [AUTO-TRANSLATED:4653b411]
// Whether to generate hls protocol on demand, if hls.segNum is configured to 0 (meaning hls recording), then hls will always be generated (regardless of this switch)
2022-11-12 23:54:35 +08:00
bool hls_demand ;
2024-09-19 14:53:50 +08:00
// rtsp[s]协议是否按需生成 [AUTO-TRANSLATED:1c3237b0]
// Whether to generate rtsp[s] protocol on demand
2022-11-12 23:54:35 +08:00
bool rtsp_demand ;
2024-09-19 14:53:50 +08:00
// rtmp[s]、http[s]-flv、ws[s]-flv协议是否按需生成 [AUTO-TRANSLATED:09ed2c30]
// Whether to generate rtmp[s]、http[s]-flv、ws[s]-flv protocol on demand
2022-11-12 23:54:35 +08:00
bool rtmp_demand ;
2024-09-19 14:53:50 +08:00
// http[s]-ts协议是否按需生成 [AUTO-TRANSLATED:a0129db3]
// Whether to generate http[s]-ts protocol on demand
2022-11-12 23:54:35 +08:00
bool ts_demand ;
2024-09-19 14:53:50 +08:00
// http[s]-fmp4、ws[s]-fmp4协议是否按需生成 [AUTO-TRANSLATED:828d25c7]
// Whether to generate http[s]-fmp4、ws[s]-fmp4 protocol on demand
2022-11-12 23:54:35 +08:00
bool fmp4_demand ;
2024-09-19 14:53:50 +08:00
// 是否将mp4录制当做观看者 [AUTO-TRANSLATED:ba351230]
// Whether to treat mp4 recording as a viewer
2022-11-12 23:54:35 +08:00
bool mp4_as_player ;
2024-09-19 14:53:50 +08:00
// mp4切片大小, 单位秒 [AUTO-TRANSLATED:c3fb8ec1]
// MP4 slice size, in seconds
2022-11-12 23:54:35 +08:00
size_t mp4_max_second ;
2024-09-19 14:53:50 +08:00
// mp4录制保存路径 [AUTO-TRANSLATED:6d860f27]
// MP4 recording save path
2022-11-12 23:54:35 +08:00
std : : string mp4_save_path ;
2024-09-19 14:53:50 +08:00
// hls录制保存路径 [AUTO-TRANSLATED:cfa90719]
// HLS recording save path
2022-11-12 23:54:35 +08:00
std : : string hls_save_path ;
2024-09-19 14:53:50 +08:00
// 支持通过on_publish返回值替换stream_id [AUTO-TRANSLATED:2c4e4997]
// Support replacing stream_id through the return value of on_publish
2023-07-08 21:32:04 +08:00
std : : string stream_replace ;
2024-09-19 14:53:50 +08:00
// 最大track数 [AUTO-TRANSLATED:2565fd37]
// Maximum number of tracks
2023-12-09 22:34:22 +08:00
size_t max_track = 2 ;
2022-11-12 23:54:35 +08:00
template < typename MAP >
ProtocolOption ( const MAP & allArgs ) : ProtocolOption ( ) {
2023-11-24 10:44:08 +08:00
load ( allArgs ) ;
}
template < typename MAP >
void load ( const MAP & allArgs ) {
2022-11-12 23:54:35 +08:00
# define GET_OPT_VALUE(key) getArgsValue(allArgs, #key, key)
GET_OPT_VALUE ( modify_stamp ) ;
GET_OPT_VALUE ( enable_audio ) ;
GET_OPT_VALUE ( add_mute_audio ) ;
2023-07-08 21:36:34 +08:00
GET_OPT_VALUE ( auto_close ) ;
2022-11-12 23:54:35 +08:00
GET_OPT_VALUE ( continue_push_ms ) ;
2023-12-02 10:20:06 +08:00
GET_OPT_VALUE ( paced_sender_ms ) ;
2022-11-12 23:54:35 +08:00
GET_OPT_VALUE ( enable_hls ) ;
2023-07-02 12:02:33 +08:00
GET_OPT_VALUE ( enable_hls_fmp4 ) ;
2022-11-12 23:54:35 +08:00
GET_OPT_VALUE ( enable_mp4 ) ;
GET_OPT_VALUE ( enable_rtsp ) ;
GET_OPT_VALUE ( enable_rtmp ) ;
GET_OPT_VALUE ( enable_ts ) ;
GET_OPT_VALUE ( enable_fmp4 ) ;
GET_OPT_VALUE ( hls_demand ) ;
GET_OPT_VALUE ( rtsp_demand ) ;
GET_OPT_VALUE ( rtmp_demand ) ;
GET_OPT_VALUE ( ts_demand ) ;
GET_OPT_VALUE ( fmp4_demand ) ;
GET_OPT_VALUE ( mp4_max_second ) ;
GET_OPT_VALUE ( mp4_as_player ) ;
GET_OPT_VALUE ( mp4_save_path ) ;
GET_OPT_VALUE ( hls_save_path ) ;
2023-07-08 21:32:04 +08:00
GET_OPT_VALUE ( stream_replace ) ;
2023-12-09 22:34:22 +08:00
GET_OPT_VALUE ( max_track ) ;
2022-11-12 23:54:35 +08:00
}
} ;
2024-09-19 14:53:50 +08:00
// 该对象用于拦截感兴趣的MediaSourceEvent事件 [AUTO-TRANSLATED:fd6d0559]
// This object is used to intercept interesting MediaSourceEvent events
2022-10-31 17:53:20 +08:00
class MediaSourceEventInterceptor : public MediaSourceEvent {
2020-09-06 17:52:07 +08:00
public :
2020-10-24 23:31:58 +08:00
void setDelegate ( const std : : weak_ptr < MediaSourceEvent > & listener ) ;
std : : shared_ptr < MediaSourceEvent > getDelegate ( ) const ;
2020-09-27 11:32:49 +08:00
MediaOriginType getOriginType ( MediaSource & sender ) const override ;
2022-02-02 20:34:50 +08:00
std : : string getOriginUrl ( MediaSource & sender ) const override ;
std : : shared_ptr < toolkit : : SockInfo > getOriginSock ( MediaSource & sender ) const override ;
2020-09-27 11:32:49 +08:00
2020-09-06 17:52:07 +08:00
bool seekTo ( MediaSource & sender , uint32_t stamp ) override ;
2021-08-12 16:07:31 +08:00
bool pause ( MediaSource & sender , bool pause ) override ;
bool speed ( MediaSource & sender , float speed ) override ;
2022-09-18 20:36:47 +08:00
bool close ( MediaSource & sender ) override ;
2020-09-06 17:52:07 +08:00
int totalReaderCount ( MediaSource & sender ) override ;
2020-09-12 19:09:56 +08:00
void onReaderChanged ( MediaSource & sender , int size ) override ;
2020-09-06 17:54:52 +08:00
void onRegist ( MediaSource & sender , bool regist ) override ;
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-09-06 17:52:07 +08:00
bool isRecording ( MediaSource & sender , Recorder : : type type ) override ;
2022-02-02 20:34:50 +08:00
std : : vector < Track : : Ptr > getMediaTracks ( MediaSource & sender , bool trackReady = true ) const override ;
2022-04-03 18:25:36 +08:00
void startSendRtp ( MediaSource & sender , const SendRtpArgs & args , const std : : function < void ( uint16_t , const toolkit : : SockException & ) > cb ) override ;
2022-02-02 20:34:50 +08:00
bool stopSendRtp ( MediaSource & sender , const std : : string & ssrc ) override ;
2022-09-03 15:53:01 +08:00
float getLossRate ( MediaSource & sender , TrackType type ) override ;
2022-06-11 12:56:03 +08:00
toolkit : : EventPoller : : Ptr getOwnerPoller ( MediaSource & sender ) override ;
2024-06-09 10:52:10 +08:00
std : : shared_ptr < MultiMediaSourceMuxer > getMuxer ( MediaSource & sender ) const override ;
std : : shared_ptr < RtpProcess > getRtpProcess ( MediaSource & sender ) const override ;
2022-06-11 12:56:03 +08:00
2020-10-24 23:31:58 +08:00
private :
2020-09-06 17:52:07 +08:00
std : : weak_ptr < MediaSourceEvent > _listener ;
} ;
2019-12-29 10:49:04 +08:00
/**
* 解 析 url获取媒体相关信息
2024-09-19 14:53:50 +08:00
* Parse the url to get media information
* [ AUTO - TRANSLATED : 3 b3cfde7 ]
2019-12-29 10:49:04 +08:00
*/
2023-05-25 16:23:24 +08:00
class MediaInfo : public MediaTuple {
2018-02-02 18:19:35 +08:00
public :
2022-10-31 17:53:20 +08:00
MediaInfo ( ) = default ;
2022-02-02 20:34:50 +08:00
MediaInfo ( const std : : string & url ) { parse ( url ) ; }
void parse ( const std : : string & url ) ;
2023-05-25 16:23:24 +08:00
std : : string getUrl ( ) const { return schema + " :// " + shortUrl ( ) ; }
2022-10-31 17:53:20 +08:00
2018-02-02 18:19:35 +08:00
public :
2023-05-25 16:23:24 +08:00
uint16_t port = 0 ;
std : : string full_url ;
std : : string schema ;
std : : string host ;
2018-02-02 18:19:35 +08:00
} ;
2023-06-17 10:28:01 +08:00
bool equalMediaTuple ( const MediaTuple & a , const MediaTuple & b ) ;
2019-12-03 16:10:02 +08:00
/**
* 媒 体 源 , 任 何 rtsp / rtmp的直播流都源自该对象
2024-09-19 14:53:50 +08:00
* Media source , any rtsp / rtmp live stream originates from this object
* [ AUTO - TRANSLATED : 658077 ad ]
2019-12-03 16:10:02 +08:00
*/
2022-02-02 20:34:50 +08:00
class MediaSource : public TrackSource , public std : : enable_shared_from_this < MediaSource > {
2018-02-02 18:19:35 +08:00
public :
2022-08-23 14:11:32 +08:00
static MediaSource & NullMediaSource ( ) ;
2021-06-16 10:14:24 +08:00
using Ptr = std : : shared_ptr < MediaSource > ;
2018-02-02 18:19:35 +08:00
2023-05-25 16:23:24 +08:00
MediaSource ( const std : : string & schema , const MediaTuple & tuple ) ;
2021-07-20 13:15:57 +08:00
virtual ~ MediaSource ( ) ;
2019-12-03 16:10:02 +08:00
2024-09-19 14:53:50 +08:00
// //////////////获取MediaSource相关信息//////////////// [AUTO-TRANSLATED:4a520f1f]
// //////////////Get MediaSource information////////////////
2020-09-06 17:54:52 +08:00
2024-09-19 14:53:50 +08:00
// 获取协议类型 [AUTO-TRANSLATED:d6b50c14]
// Get protocol type
2023-06-17 10:28:01 +08:00
const std : : string & getSchema ( ) const {
return _schema ;
}
2019-12-03 16:10:02 +08:00
2023-05-25 16:23:24 +08:00
const MediaTuple & getMediaTuple ( ) const {
return _tuple ;
}
2023-06-17 10:28:01 +08:00
std : : string getUrl ( ) const { return _schema + " :// " + _tuple . shortUrl ( ) ; }
2022-10-31 17:53:20 +08:00
2024-09-19 14:53:50 +08:00
// 获取对象所有权 [AUTO-TRANSLATED:84fb43cd]
// Get object ownership
2022-01-10 16:37:50 +08:00
std : : shared_ptr < void > getOwnership ( ) ;
2024-09-19 14:53:50 +08:00
// 获取所有Track [AUTO-TRANSLATED:59f1c570]
// Get all Tracks
2022-02-02 20:34:50 +08:00
std : : vector < Track : : Ptr > getTracks ( bool ready = true ) const override ;
2019-12-29 10:49:04 +08:00
2024-09-19 14:53:50 +08:00
// 获取流当前时间戳 [AUTO-TRANSLATED:f65f560a]
// Get the current timestamp of the stream
2020-09-06 17:54:52 +08:00
virtual uint32_t getTimeStamp ( TrackType type ) { return 0 ; } ;
2024-09-19 14:53:50 +08:00
// 设置时间戳 [AUTO-TRANSLATED:2bfce32f]
// Set timestamp
2020-09-06 17:54:52 +08:00
virtual void setTimeStamp ( uint32_t stamp ) { } ;
2024-09-19 14:53:50 +08:00
// 获取数据速率, 单位bytes/s [AUTO-TRANSLATED:c70465c1]
// Get data rate, unit bytes/s
2020-12-05 12:22:17 +08:00
int getBytesSpeed ( TrackType type = TrackInvalid ) ;
2024-09-19 14:53:50 +08:00
// 获取流创建GMT unix时间戳, 单位秒 [AUTO-TRANSLATED:0bbe145e]
// Get the stream creation GMT unix timestamp, unit seconds
2022-10-31 17:53:20 +08:00
uint64_t getCreateStamp ( ) const { return _create_stamp ; }
2024-09-19 14:53:50 +08:00
// 获取流上线时间,单位秒 [AUTO-TRANSLATED:a087d56a]
// Get the stream online time, unit seconds
2020-10-01 18:57:15 +08:00
uint64_t getAliveSecond ( ) const ;
2020-10-01 11:02:00 +08:00
2024-09-19 14:53:50 +08:00
// //////////////MediaSourceEvent相关接口实现//////////////// [AUTO-TRANSLATED:aa63d949]
// //////////////MediaSourceEvent related interface implementation////////////////
2020-09-06 17:54:52 +08:00
2024-09-19 14:53:50 +08:00
// 设置监听者 [AUTO-TRANSLATED:b9b90b57]
// Set listener
2020-10-24 23:31:58 +08:00
virtual void setListener ( const std : : weak_ptr < MediaSourceEvent > & listener ) ;
2024-09-19 14:53:50 +08:00
// 获取监听者 [AUTO-TRANSLATED:5c9cbb82]
// Get listener
2023-07-08 21:35:09 +08:00
std : : weak_ptr < MediaSourceEvent > getListener ( ) const ;
2019-12-29 10:49:04 +08:00
2024-09-19 14:53:50 +08:00
// 本协议获取观看者个数,可能返回本协议的观看人数,也可能返回总人数 [AUTO-TRANSLATED:0874fa7c]
// This protocol gets the number of viewers, it may return the number of viewers of this protocol, or it may return the total number of viewers
2019-12-03 16:10:02 +08:00
virtual int readerCount ( ) = 0 ;
2024-09-19 14:53:50 +08:00
// 观看者个数,包括(hls/rtsp/rtmp) [AUTO-TRANSLATED:6604020f]
// Number of viewers, including (hls/rtsp/rtmp)
2019-12-28 16:48:11 +08:00
virtual int totalReaderCount ( ) ;
2024-09-19 14:53:50 +08:00
// 获取播放器列表 [AUTO-TRANSLATED:e7691d2b]
// Get the player list
2023-09-02 12:06:35 +08:00
virtual void getPlayerList ( const std : : function < void ( const std : : list < toolkit : : Any > & info_list ) > & cb ,
const std : : function < toolkit : : Any ( toolkit : : Any & & info ) > & on_change ) {
2022-08-30 21:05:19 +08:00
assert ( cb ) ;
2023-09-02 12:06:35 +08:00
cb ( std : : list < toolkit : : Any > ( ) ) ;
2022-08-30 21:05:19 +08:00
}
2019-12-29 10:49:04 +08:00
2023-09-02 12:53:56 +08:00
virtual bool broadcastMessage ( const toolkit : : Any & data ) { return false ; }
2024-09-19 14:53:50 +08:00
// 获取媒体源类型 [AUTO-TRANSLATED:34290a69]
// Get the media source type
2020-09-27 11:32:49 +08:00
MediaOriginType getOriginType ( ) const ;
2024-09-19 14:53:50 +08:00
// 获取媒体源url或者文件路径 [AUTO-TRANSLATED:fa34d795]
// Get the media source url or file path
2022-02-02 20:34:50 +08:00
std : : string getOriginUrl ( ) const ;
2024-09-19 14:53:50 +08:00
// 获取媒体源客户端相关信息 [AUTO-TRANSLATED:037ef910]
// Get the media source client information
2022-02-02 20:34:50 +08:00
std : : shared_ptr < toolkit : : SockInfo > getOriginSock ( ) const ;
2020-09-27 11:32:49 +08:00
2024-09-19 14:53:50 +08:00
// 拖动进度条 [AUTO-TRANSLATED:65ee8a83]
// Drag the progress bar
2020-09-06 17:52:07 +08:00
bool seekTo ( uint32_t stamp ) ;
2024-09-19 14:53:50 +08:00
// 暂停 [AUTO-TRANSLATED:ffd21ae7]
// Pause
2021-08-12 16:07:31 +08:00
bool pause ( bool pause ) ;
2024-09-19 14:53:50 +08:00
// 倍数播放 [AUTO-TRANSLATED:a5e3c1c9]
// Playback speed
2021-08-09 18:28:43 +08:00
bool speed ( float speed ) ;
2024-09-19 14:53:50 +08:00
// 关闭该流 [AUTO-TRANSLATED:b3867b98]
// Close the stream
2019-12-03 16:10:02 +08:00
bool close ( bool force ) ;
2024-09-19 14:53:50 +08:00
// 该流观看人数变化 [AUTO-TRANSLATED:8e583993]
// The number of viewers of this stream changes
2020-09-12 19:09:56 +08:00
void onReaderChanged ( int size ) ;
2024-09-19 14:53:50 +08:00
// 开启或关闭录制 [AUTO-TRANSLATED:3817e390]
// Turn recording on or off
2022-02-02 20:34:50 +08:00
bool setupRecord ( Recorder : : type type , bool start , const std : : string & custom_path , size_t max_second ) ;
2024-09-19 14:53:50 +08:00
// 获取录制状态 [AUTO-TRANSLATED:a0499880]
// Get recording status
2020-09-06 17:52:33 +08:00
bool isRecording ( Recorder : : type type ) ;
2024-09-19 14:53:50 +08:00
// 开始发送ps-rtp [AUTO-TRANSLATED:a51796fa]
// Start sending ps-rtp
2022-04-03 18:25:36 +08:00
void startSendRtp ( const MediaSourceEvent : : SendRtpArgs & args , const std : : function < void ( uint16_t , const toolkit : : SockException & ) > cb ) ;
2024-09-19 14:53:50 +08:00
// 停止发送ps-rtp [AUTO-TRANSLATED:952d2b35]
// Stop sending ps-rtp
2022-02-02 20:34:50 +08:00
bool stopSendRtp ( const std : : string & ssrc ) ;
2024-09-19 14:53:50 +08:00
// 获取丢包率 [AUTO-TRANSLATED:ec61b378]
// Get packet loss rate
2022-09-03 15:53:01 +08:00
float getLossRate ( mediakit : : TrackType type ) ;
2024-09-19 14:53:50 +08:00
// 获取所在线程 [AUTO-TRANSLATED:75662eb8]
// Get the thread where it is running
2022-06-11 12:56:03 +08:00
toolkit : : EventPoller : : Ptr getOwnerPoller ( ) ;
2024-09-19 14:53:50 +08:00
// 获取MultiMediaSourceMuxer对象 [AUTO-TRANSLATED:2de96d44]
// Get the MultiMediaSourceMuxer object
2024-06-09 10:52:10 +08:00
std : : shared_ptr < MultiMediaSourceMuxer > getMuxer ( ) const ;
2024-09-19 14:53:50 +08:00
// 获取RtpProcess对象 [AUTO-TRANSLATED:c6b7da43]
// Get the RtpProcess object
2024-06-09 10:52:10 +08:00
std : : shared_ptr < RtpProcess > getRtpProcess ( ) const ;
2019-11-18 12:07:11 +08:00
2024-09-19 14:53:50 +08:00
// //////////////static方法, 查找或生成MediaSource//////////////// [AUTO-TRANSLATED:c3950036]
// //////////////static methods, find or generate MediaSource////////////////
2020-09-06 17:54:52 +08:00
2024-09-19 14:53:50 +08:00
// 同步查找流 [AUTO-TRANSLATED:97048f1e]
// Synchronously find the stream
2022-02-02 20:34:50 +08:00
static Ptr find ( const std : : string & schema , const std : : string & vhost , const std : : string & app , const std : : string & id , bool from_mp4 = false ) ;
2022-09-07 11:06:39 +08:00
static Ptr find ( const MediaInfo & info , bool from_mp4 = false ) {
2023-05-25 16:23:24 +08:00
return find ( info . schema , info . vhost , info . app , info . stream , from_mp4 ) ;
2022-09-07 11:06:39 +08:00
}
2020-09-06 18:19:54 +08:00
2024-09-19 14:53:50 +08:00
// 忽略schema, 同步查找流, 可能返回rtmp/rtsp/hls类型 [AUTO-TRANSLATED:8c061cac]
// Ignore schema, synchronously find the stream, may return rtmp/rtsp/hls type
2022-02-02 20:34:50 +08:00
static Ptr find ( const std : : string & vhost , const std : : string & app , const std : : string & stream_id , bool from_mp4 = false ) ;
2020-09-06 18:19:54 +08:00
2024-09-19 14:53:50 +08:00
// 异步查找流 [AUTO-TRANSLATED:4decf738]
// Asynchronously find the stream
2022-02-02 20:34:50 +08:00
static void findAsync ( const MediaInfo & info , const std : : shared_ptr < toolkit : : Session > & session , const std : : function < void ( const Ptr & src ) > & cb ) ;
2024-09-19 14:53:50 +08:00
// 遍历所有流 [AUTO-TRANSLATED:a39b2399]
// Traverse all streams
2022-02-02 20:34:50 +08:00
static void for_each_media ( const std : : function < void ( const Ptr & src ) > & cb , const std : : string & schema = " " , const std : : string & vhost = " " , const std : : string & app = " " , const std : : string & stream = " " ) ;
2024-09-19 14:53:50 +08:00
// 从mp4文件生成MediaSource [AUTO-TRANSLATED:7df9762e]
// Generate MediaSource from mp4 file
2022-02-02 20:34:50 +08:00
static MediaSource : : Ptr createFromMP4 ( const std : : string & schema , const std : : string & vhost , const std : : string & app , const std : : string & stream , const std : : string & file_path = " " , bool check_app = true ) ;
2020-05-26 10:11:58 +08:00
2018-08-31 14:13:00 +08:00
protected :
2024-09-19 14:53:50 +08:00
// 媒体注册 [AUTO-TRANSLATED:dbf5c730]
// Media registration
2020-09-06 17:52:07 +08:00
void regist ( ) ;
2020-05-26 10:11:58 +08:00
private :
2024-09-19 14:53:50 +08:00
// 媒体注销 [AUTO-TRANSLATED:06a0630a]
// Media unregistration
2020-09-06 17:52:07 +08:00
bool unregist ( ) ;
2024-09-19 14:53:50 +08:00
// 触发媒体事件 [AUTO-TRANSLATED:0c2f9ae6]
// Trigger media events
2020-09-06 17:52:07 +08:00
void emitEvent ( bool regist ) ;
2020-10-01 11:02:00 +08:00
protected :
2022-08-30 21:05:19 +08:00
toolkit : : BytesSpeed _speed [ TrackMax ] ;
2023-05-25 16:23:24 +08:00
MediaTuple _tuple ;
2020-10-01 11:02:00 +08:00
2018-02-02 18:19:35 +08:00
private :
2022-02-02 20:34:50 +08:00
std : : atomic_flag _owned { false } ;
2020-10-01 18:57:15 +08:00
time_t _create_stamp ;
2022-02-02 20:34:50 +08:00
toolkit : : Ticker _ticker ;
std : : string _schema ;
2020-09-06 17:52:07 +08:00
std : : weak_ptr < MediaSourceEvent > _listener ;
2024-09-19 14:53:50 +08:00
// 对象个数统计 [AUTO-TRANSLATED:f4a012d0]
// Object count statistics
2022-02-02 20:34:50 +08:00
toolkit : : ObjectStatistic < MediaSource > _statistic ;
2018-02-02 18:19:35 +08:00
} ;
2018-10-24 17:17:55 +08:00
} /* namespace mediakit */
2022-07-28 16:43:03 +08:00
# endif //ZLMEDIAKIT_MEDIASOURCE_H