2019-12-24 13:56:53 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright ( c ) 2016 - present The ZLMediaKit project authors . All Rights Reserved .
2019-12-23 18:50:46 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit ( https : //github.com/ZLMediaKit/ZLMediaKit).
2019-12-23 18:50:46 +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 .
2019-12-23 18:50:46 +08:00
*/
# include <assert.h>
2019-12-27 10:10:31 +08:00
# include "mk_events_objects.h"
2019-12-23 18:50:46 +08:00
# include "Common/config.h"
# include "Record/MP4Recorder.h"
# include "Http/HttpSession.h"
2019-12-25 14:25:26 +08:00
# include "Http/HttpBody.h"
2022-04-16 15:57:02 +08:00
2019-12-25 14:25:26 +08:00
# include "Http/HttpClient.h"
2019-12-23 18:50:46 +08:00
# include "Rtsp/RtspSession.h"
2024-03-22 20:41:14 +08:00
# ifdef ENABLE_WEBRTC
2024-03-02 16:52:51 +08:00
# include "webrtc/WebRtcTransport.h"
2024-03-22 20:41:14 +08:00
# endif
2022-02-02 20:34:50 +08:00
using namespace toolkit ;
2019-12-23 18:50:46 +08:00
using namespace mediakit ;
2020-09-15 19:09:26 +08:00
///////////////////////////////////////////RecordInfo/////////////////////////////////////////////
2024-05-10 14:27:23 +08:00
API_EXPORT uint64_t API_CALL mk_record_info_get_start_time ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > start_time ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT float API_CALL mk_record_info_get_time_len ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > time_len ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT size_t API_CALL mk_record_info_get_file_size ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > file_size ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_file_path ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > file_path . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_file_name ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > file_name . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_folder ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > folder . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_url ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > url . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_vhost ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > vhost . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_app ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > app . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-05-10 14:27:23 +08:00
API_EXPORT const char * API_CALL mk_record_info_get_stream ( const mk_record_info ctx ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
2020-09-15 19:09:26 +08:00
RecordInfo * info = ( RecordInfo * ) ctx ;
2020-09-20 11:40:42 +08:00
return info - > stream . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
///////////////////////////////////////////Parser/////////////////////////////////////////////
API_EXPORT const char * API_CALL mk_parser_get_method ( const mk_parser ctx ) {
assert ( ctx ) ;
Parser * parser = ( Parser * ) ctx ;
2023-06-10 11:04:52 +08:00
return parser - > method ( ) . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_parser_get_url ( const mk_parser ctx ) {
assert ( ctx ) ;
Parser * parser = ( Parser * ) ctx ;
2023-06-10 11:04:52 +08:00
return parser - > url ( ) . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_parser_get_url_params ( const mk_parser ctx ) {
assert ( ctx ) ;
Parser * parser = ( Parser * ) ctx ;
2023-06-10 11:04:52 +08:00
return parser - > params ( ) . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_parser_get_url_param ( const mk_parser ctx , const char * key ) {
assert ( ctx & & key ) ;
Parser * parser = ( Parser * ) ctx ;
return parser - > getUrlArgs ( ) [ key ] . c_str ( ) ;
}
API_EXPORT const char * API_CALL mk_parser_get_tail ( const mk_parser ctx ) {
assert ( ctx ) ;
Parser * parser = ( Parser * ) ctx ;
2023-06-10 11:04:52 +08:00
return parser - > protocol ( ) . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_parser_get_header ( const mk_parser ctx , const char * key ) {
assert ( ctx & & key ) ;
Parser * parser = ( Parser * ) ctx ;
2020-04-20 18:13:45 +08:00
return parser - > getHeader ( ) [ key ] . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2021-01-17 18:31:50 +08:00
API_EXPORT const char * API_CALL mk_parser_get_content ( const mk_parser ctx , size_t * length ) {
2019-12-23 18:50:46 +08:00
assert ( ctx ) ;
Parser * parser = ( Parser * ) ctx ;
2019-12-25 14:25:26 +08:00
if ( length ) {
2023-06-10 11:04:52 +08:00
* length = parser - > content ( ) . size ( ) ;
2019-12-25 14:25:26 +08:00
}
2023-06-10 11:04:52 +08:00
return parser - > content ( ) . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2024-07-05 20:56:23 +08:00
API_EXPORT void API_CALL mk_parser_headers_for_each ( const mk_parser ctx , on_mk_parser_header_cb cb , void * user_data ) {
assert ( ctx & & cb ) ;
Parser * parser = ( Parser * ) ctx ;
for ( auto it = parser - > getHeader ( ) . begin ( ) ; it ! = parser - > getHeader ( ) . end ( ) ; + + it ) {
cb ( user_data , it - > first . c_str ( ) , it - > second . c_str ( ) ) ;
}
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////MediaInfo/////////////////////////////////////////////
API_EXPORT const char * API_CALL mk_media_info_get_params ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2024-03-30 14:41:20 +08:00
return info - > params . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2019-12-29 10:49:04 +08:00
2019-12-23 18:50:46 +08:00
API_EXPORT const char * API_CALL mk_media_info_get_schema ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > schema . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2020-04-22 09:51:04 +08:00
2019-12-23 18:50:46 +08:00
API_EXPORT const char * API_CALL mk_media_info_get_vhost ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > vhost . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2020-04-22 09:51:04 +08:00
API_EXPORT const char * API_CALL mk_media_info_get_host ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > host . c_str ( ) ;
2020-04-22 09:51:04 +08:00
}
API_EXPORT uint16_t API_CALL mk_media_info_get_port ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > port ;
2020-04-22 09:51:04 +08:00
}
2019-12-23 18:50:46 +08:00
API_EXPORT const char * API_CALL mk_media_info_get_app ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > app . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
2020-04-22 09:51:04 +08:00
2019-12-23 18:50:46 +08:00
API_EXPORT const char * API_CALL mk_media_info_get_stream ( const mk_media_info ctx ) {
assert ( ctx ) ;
MediaInfo * info = ( MediaInfo * ) ctx ;
2023-05-25 16:23:24 +08:00
return info - > stream . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
///////////////////////////////////////////MediaSource/////////////////////////////////////////////
API_EXPORT const char * API_CALL mk_media_source_get_schema ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > getSchema ( ) . c_str ( ) ;
}
API_EXPORT const char * API_CALL mk_media_source_get_vhost ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2023-06-17 10:28:01 +08:00
return src - > getMediaTuple ( ) . vhost . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_media_source_get_app ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2023-06-17 10:28:01 +08:00
return src - > getMediaTuple ( ) . app . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT const char * API_CALL mk_media_source_get_stream ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2023-06-17 10:28:01 +08:00
return src - > getMediaTuple ( ) . stream . c_str ( ) ;
2019-12-23 18:50:46 +08:00
}
API_EXPORT int API_CALL mk_media_source_get_reader_count ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > readerCount ( ) ;
}
2019-12-28 16:57:35 +08:00
API_EXPORT int API_CALL mk_media_source_get_total_reader_count ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > totalReaderCount ( ) ;
}
2023-06-10 09:34:00 +08:00
API_EXPORT int API_CALL mk_media_source_get_track_count ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > getTracks ( false ) . size ( ) ;
}
API_EXPORT mk_track API_CALL mk_media_source_get_track ( const mk_media_source ctx , int index ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
auto tracks = src - > getTracks ( false ) ;
if ( index < 0 & & index > = tracks . size ( ) ) {
return nullptr ;
}
2023-06-17 09:14:51 +08:00
return ( mk_track ) new Track : : Ptr ( std : : move ( tracks [ index ] ) ) ;
2023-06-10 09:34:00 +08:00
}
2024-06-15 15:55:39 +08:00
API_EXPORT float API_CALL mk_media_source_get_track_loss ( const mk_media_source ctx , const mk_track track ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2024-09-19 14:53:50 +08:00
// rtp推流只有一个统计器, 但是可能有多个track, 如果短时间多次获取间隔丢包率, 第二次会获取为-1 [AUTO-TRANSLATED:b30fec2c]
// RTP streaming has only one statistics object, but there may be multiple tracks. If the packet loss rate is obtained multiple times in a short period, the second time will be obtained as -1
2024-06-15 15:55:39 +08:00
return src - > getLossRate ( ( * ( ( Track : : Ptr * ) track ) ) - > getTrackType ( ) ) ;
}
2023-09-02 12:58:20 +08:00
API_EXPORT int API_CALL mk_media_source_broadcast_msg ( const mk_media_source ctx , const char * msg , size_t len ) {
assert ( ctx & & msg & & len ) ;
MediaSource * src = ( MediaSource * ) ctx ;
Any any ;
Buffer : : Ptr buffer = std : : make_shared < BufferLikeString > ( std : : string ( msg , len ) ) ;
any . set ( std : : move ( buffer ) ) ;
2023-09-02 13:06:42 +08:00
return src - > broadcastMessage ( any ) ;
2023-09-02 12:58:20 +08:00
}
2024-04-05 22:09:40 +08:00
API_EXPORT const char * API_CALL mk_media_source_get_origin_url ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return _strdup ( src - > getOriginUrl ( ) . c_str ( ) ) ;
}
API_EXPORT int API_CALL mk_media_source_get_origin_type ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return static_cast < int > ( src - > getOriginType ( ) ) ;
}
2024-06-15 15:55:39 +08:00
API_EXPORT const char * API_CALL mk_media_source_get_origin_type_str ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return _strdup ( getOriginTypeString ( src - > getOriginType ( ) ) . c_str ( ) ) ;
}
2024-04-05 22:09:40 +08:00
API_EXPORT uint64_t API_CALL mk_media_source_get_create_stamp ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > getCreateStamp ( ) ;
}
API_EXPORT int API_CALL mk_media_source_is_recording ( const mk_media_source ctx , int type ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > isRecording ( ( Recorder : : type ) type ) ;
}
2024-06-15 15:55:39 +08:00
API_EXPORT int API_CALL mk_media_source_get_bytes_speed ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > getBytesSpeed ( ) ;
}
API_EXPORT uint64_t API_CALL mk_media_source_get_alive_second ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > getAliveSecond ( ) ;
}
2019-12-23 18:50:46 +08:00
API_EXPORT int API_CALL mk_media_source_close ( const mk_media_source ctx , int force ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > close ( force ) ;
}
API_EXPORT int API_CALL mk_media_source_seek_to ( const mk_media_source ctx , uint32_t stamp ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
return src - > seekTo ( stamp ) ;
}
2024-07-28 22:55:33 +08:00
API_EXPORT void API_CALL mk_media_source_start_send_rtp ( const mk_media_source ctx , const char * dst_url , uint16_t dst_port , const char * ssrc , int con_type , on_mk_media_source_send_rtp_result cb , void * user_data ) {
mk_media_source_start_send_rtp2 ( ctx , dst_url , dst_port , ssrc , con_type , cb , user_data , nullptr ) ;
2023-02-11 15:14:18 +08:00
}
2019-12-23 18:50:46 +08:00
2024-07-28 22:55:33 +08:00
API_EXPORT void API_CALL mk_media_source_start_send_rtp2 ( const mk_media_source ctx , const char * dst_url , uint16_t dst_port , const char * ssrc , int con_type , on_mk_media_source_send_rtp_result cb , void * user_data , on_user_data_free user_data_free ) {
2020-11-21 22:13:08 +08:00
assert ( ctx & & dst_url & & ssrc ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2022-04-03 18:25:36 +08:00
MediaSourceEvent : : SendRtpArgs args ;
args . dst_url = dst_url ;
args . dst_port = dst_port ;
args . ssrc = ssrc ;
2024-08-22 21:12:06 +08:00
args . close_delay_ms = 30 * 1000 ;
2024-07-28 22:55:33 +08:00
args . con_type = ( mediakit : : MediaSourceEvent : : SendRtpArgs : : ConType ) con_type ;
2022-04-03 18:25:36 +08:00
2023-02-11 15:14:18 +08:00
std : : shared_ptr < void > ptr ( user_data , user_data_free ? user_data_free : [ ] ( void * ) { } ) ;
2024-08-22 21:12:06 +08:00
src - > getOwnerPoller ( ) - > async ( [ = ] ( ) mutable {
src - > startSendRtp ( args , [ cb , ptr ] ( uint16_t local_port , const SockException & ex ) {
if ( cb ) {
cb ( ptr . get ( ) , local_port , ex . getErrCode ( ) , ex . what ( ) ) ;
}
} ) ;
} ) ;
}
API_EXPORT void API_CALL mk_media_source_start_send_rtp3 ( const mk_media_source ctx , const char * dst_url , uint16_t dst_port , const char * ssrc , int con_type , mk_ini options , on_mk_media_source_send_rtp_result cb , void * user_data ) {
mk_media_source_start_send_rtp4 ( ctx , dst_url , dst_port , ssrc , con_type , options , cb , user_data , nullptr ) ;
}
API_EXPORT void API_CALL mk_media_source_start_send_rtp4 ( const mk_media_source ctx , const char * dst_url , uint16_t dst_port , const char * ssrc , int con_type , mk_ini options , on_mk_media_source_send_rtp_result cb , void * user_data , on_user_data_free user_data_free ) {
assert ( ctx & & dst_url & & ssrc ) ;
MediaSource * src = ( MediaSource * ) ctx ;
MediaSourceEvent : : SendRtpArgs args ;
args . dst_url = dst_url ;
args . dst_port = dst_port ;
args . ssrc = ssrc ;
args . con_type = ( mediakit : : MediaSourceEvent : : SendRtpArgs : : ConType ) con_type ;
auto ini_ptr = ( mINI * ) options ;
args . src_port = ( * ini_ptr ) [ " src_port " ] . empty ( ) ? 0 : ( * ini_ptr ) [ " src_port " ] . as < int > ( ) ;
args . ssrc_multi_send = ( * ini_ptr ) [ " ssrc_multi_send " ] . empty ( ) ? false : ( * ini_ptr ) [ " ssrc_multi_send " ] . as < bool > ( ) ;
args . pt = ( * ini_ptr ) [ " pt " ] . empty ( ) ? 96 : ( * ini_ptr ) [ " pt " ] . as < int > ( ) ;
args . data_type = ( * ini_ptr ) [ " data_type " ] . empty ( ) ? MediaSourceEvent : : SendRtpArgs : : DataType : : kRtpPS : ( MediaSourceEvent : : SendRtpArgs : : DataType ) ( * ini_ptr ) [ " data_type " ] . as < int > ( ) ;
args . only_audio = ( * ini_ptr ) [ " only_audio " ] . empty ( ) ? false : ( * ini_ptr ) [ " only_audio " ] . as < bool > ( ) ;
args . udp_rtcp_timeout = ( * ini_ptr ) [ " udp_rtcp_timeout " ] . empty ( ) ? false : ( * ini_ptr ) [ " udp_rtcp_timeout " ] . as < bool > ( ) ;
args . recv_stream_id = ( * ini_ptr ) [ " recv_stream_id " ] ;
args . recv_stream_app = src - > getMediaTuple ( ) . app . c_str ( ) ;
args . recv_stream_vhost = src - > getMediaTuple ( ) . vhost . c_str ( ) ;
args . close_delay_ms = ( * ini_ptr ) [ " close_delay_ms " ] . empty ( ) ? 0 : ( * ini_ptr ) [ " close_delay_ms " ] . as < int > ( ) ;
args . rtcp_timeout_ms = ( * ini_ptr ) [ " rtcp_timeout_ms " ] . empty ( ) ? 30000 : ( * ini_ptr ) [ " rtcp_timeout_ms " ] . as < int > ( ) ;
args . rtcp_send_interval_ms = ( * ini_ptr ) [ " rtcp_send_interval_ms " ] . empty ( ) ? 5000 : ( * ini_ptr ) [ " rtcp_send_interval_ms " ] . as < int > ( ) ;
std : : shared_ptr < void > ptr (
user_data , user_data_free ? user_data_free : [ ] ( void * ) { } ) ;
src - > getOwnerPoller ( ) - > async ( [ = ] ( ) mutable {
src - > startSendRtp ( args , [ cb , ptr ] ( uint16_t local_port , const SockException & ex ) {
if ( cb ) {
cb ( ptr . get ( ) , local_port , ex . getErrCode ( ) , ex . what ( ) ) ;
}
} ) ;
2020-11-21 22:13:08 +08:00
} ) ;
}
API_EXPORT int API_CALL mk_media_source_stop_send_rtp ( const mk_media_source ctx ) {
assert ( ctx ) ;
MediaSource * src = ( MediaSource * ) ctx ;
2020-12-27 18:41:53 +08:00
return src - > stopSendRtp ( " " ) ;
2020-11-21 22:13:08 +08:00
}
2019-12-23 18:50:46 +08:00
API_EXPORT void API_CALL mk_media_source_find ( const char * schema ,
const char * vhost ,
const char * app ,
const char * stream ,
2022-07-24 08:51:11 +08:00
int from_mp4 ,
2019-12-23 18:50:46 +08:00
void * user_data ,
on_mk_media_source_find_cb cb ) {
assert ( schema & & vhost & & app & & stream & & cb ) ;
2022-07-24 08:51:11 +08:00
auto src = MediaSource : : find ( schema , vhost , app , stream , from_mp4 ) ;
2023-02-26 21:45:14 +08:00
cb ( user_data , ( mk_media_source ) src . get ( ) ) ;
2019-12-23 18:50:46 +08:00
}
2024-03-30 14:59:28 +08:00
API_EXPORT mk_media_source API_CALL mk_media_source_find2 ( const char * schema ,
const char * vhost ,
const char * app ,
const char * stream ,
int from_mp4 ) {
2023-05-25 16:57:03 +08:00
assert ( schema & & vhost & & app & & stream ) ;
auto src = MediaSource : : find ( schema , vhost , app , stream , from_mp4 ) ;
return ( mk_media_source ) src . get ( ) ;
}
2021-06-30 21:06:29 +08:00
API_EXPORT void API_CALL mk_media_source_for_each ( void * user_data , on_mk_media_source_find_cb cb , const char * schema ,
const char * vhost , const char * app , const char * stream ) {
2019-12-23 18:50:46 +08:00
assert ( cb ) ;
2021-06-30 21:06:29 +08:00
MediaSource : : for_each_media ( [ & ] ( const MediaSource : : Ptr & src ) {
2023-02-26 21:45:14 +08:00
cb ( user_data , ( mk_media_source ) src . get ( ) ) ;
2021-06-30 21:06:29 +08:00
} , schema ? schema : " " , vhost ? vhost : " " , app ? app : " " , stream ? stream : " " ) ;
2019-12-23 18:50:46 +08:00
}
2019-12-25 14:25:26 +08:00
///////////////////////////////////////////HttpBody/////////////////////////////////////////////
2023-02-11 11:55:06 +08:00
2021-01-17 18:31:50 +08:00
API_EXPORT mk_http_body API_CALL mk_http_body_from_string ( const char * str , size_t len ) {
2019-12-25 14:25:26 +08:00
assert ( str ) ;
if ( ! len ) {
len = strlen ( str ) ;
}
2023-02-26 21:45:14 +08:00
return ( mk_http_body ) new HttpBody : : Ptr ( new HttpStringBody ( std : : string ( str , len ) ) ) ;
2019-12-25 14:25:26 +08:00
}
2023-02-11 11:55:06 +08:00
API_EXPORT mk_http_body API_CALL mk_http_body_from_buffer ( mk_buffer buffer ) {
assert ( buffer ) ;
2023-02-26 21:45:14 +08:00
return ( mk_http_body ) new HttpBody : : Ptr ( new HttpBufferBody ( * ( ( Buffer : : Ptr * ) buffer ) ) ) ;
2023-02-11 11:55:06 +08:00
}
2019-12-25 14:25:26 +08:00
API_EXPORT mk_http_body API_CALL mk_http_body_from_file ( const char * file_path ) {
assert ( file_path ) ;
2023-02-26 21:45:14 +08:00
return ( mk_http_body ) new HttpBody : : Ptr ( new HttpFileBody ( file_path ) ) ;
2019-12-25 14:25:26 +08:00
}
template < typename C = StrCaseMap >
static C get_http_header ( const char * response_header [ ] ) {
C header ;
2019-12-23 18:50:46 +08:00
for ( int i = 0 ; response_header [ i ] ! = NULL ; ) {
auto key = response_header [ i ] ;
auto value = response_header [ i + 1 ] ;
if ( key & & value ) {
i + = 2 ;
2019-12-28 13:39:25 +08:00
header . emplace ( key , value ) ;
2019-12-23 18:50:46 +08:00
continue ;
}
break ;
}
2020-09-21 14:32:56 +08:00
return header ;
2019-12-23 18:50:46 +08:00
}
2019-12-25 14:25:26 +08:00
API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form ( const char * key_val [ ] , const char * file_path ) {
assert ( key_val & & file_path ) ;
2023-02-26 21:45:14 +08:00
return ( mk_http_body ) new HttpBody : : Ptr ( new HttpMultiFormBody ( get_http_header < HttpArgs > ( key_val ) , file_path ) ) ;
2019-12-25 14:25:26 +08:00
}
API_EXPORT void API_CALL mk_http_body_release ( mk_http_body ctx ) {
assert ( ctx ) ;
HttpBody : : Ptr * ptr = ( HttpBody : : Ptr * ) ctx ;
delete ptr ;
}
///////////////////////////////////////////HttpResponseInvoker/////////////////////////////////////////////
API_EXPORT void API_CALL mk_http_response_invoker_do_string ( const mk_http_response_invoker ctx ,
2021-01-02 21:24:06 +08:00
int response_code ,
2019-12-25 14:25:26 +08:00
const char * * response_header ,
const char * response_content ) {
2019-12-23 18:50:46 +08:00
assert ( ctx & & response_code & & response_header & & response_content ) ;
auto header = get_http_header ( response_header ) ;
HttpSession : : HttpResponseInvoker * invoker = ( HttpSession : : HttpResponseInvoker * ) ctx ;
( * invoker ) ( response_code , header , response_content ) ;
}
API_EXPORT void API_CALL mk_http_response_invoker_do_file ( const mk_http_response_invoker ctx ,
const mk_parser request_parser ,
const char * response_header [ ] ,
const char * response_file_path ) {
assert ( ctx & & request_parser & & response_header & & response_file_path ) ;
auto header = get_http_header ( response_header ) ;
HttpSession : : HttpResponseInvoker * invoker = ( HttpSession : : HttpResponseInvoker * ) ctx ;
2020-04-20 18:13:45 +08:00
( * invoker ) . responseFile ( ( ( Parser * ) ( request_parser ) ) - > getHeader ( ) , header , response_file_path ) ;
2019-12-23 18:50:46 +08:00
}
2019-12-25 14:25:26 +08:00
API_EXPORT void API_CALL mk_http_response_invoker_do ( const mk_http_response_invoker ctx ,
2021-01-02 21:24:06 +08:00
int response_code ,
2019-12-25 14:25:26 +08:00
const char * * response_header ,
const mk_http_body response_body ) {
assert ( ctx & & response_code & & response_header & & response_body ) ;
auto header = get_http_header ( response_header ) ;
HttpSession : : HttpResponseInvoker * invoker = ( HttpSession : : HttpResponseInvoker * ) ctx ;
HttpBody : : Ptr * body = ( HttpBody : : Ptr * ) response_body ;
( * invoker ) ( response_code , header , * body ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone ( const mk_http_response_invoker ctx ) {
assert ( ctx ) ;
HttpSession : : HttpResponseInvoker * invoker = ( HttpSession : : HttpResponseInvoker * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_http_response_invoker ) new HttpSession : : HttpResponseInvoker ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
API_EXPORT void API_CALL mk_http_response_invoker_clone_release ( const mk_http_response_invoker ctx ) {
assert ( ctx ) ;
HttpSession : : HttpResponseInvoker * invoker = ( HttpSession : : HttpResponseInvoker * ) ctx ;
delete invoker ;
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////HttpAccessPathInvoker/////////////////////////////////////////////
API_EXPORT void API_CALL mk_http_access_path_invoker_do ( const mk_http_access_path_invoker ctx ,
const char * err_msg ,
const char * access_path ,
int cookie_life_second ) {
assert ( ctx ) ;
2019-12-24 14:24:32 +08:00
HttpSession : : HttpAccessPathInvoker * invoker = ( HttpSession : : HttpAccessPathInvoker * ) ctx ;
( * invoker ) ( err_msg ? err_msg : " " ,
2019-12-23 18:50:46 +08:00
access_path ? access_path : " " ,
cookie_life_second ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_http_access_path_invoker API_CALL mk_http_access_path_invoker_clone ( const mk_http_access_path_invoker ctx ) {
assert ( ctx ) ;
HttpSession : : HttpAccessPathInvoker * invoker = ( HttpSession : : HttpAccessPathInvoker * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_http_access_path_invoker ) new HttpSession : : HttpAccessPathInvoker ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
API_EXPORT void API_CALL mk_http_access_path_invoker_clone_release ( const mk_http_access_path_invoker ctx ) {
assert ( ctx ) ;
HttpSession : : HttpAccessPathInvoker * invoker = ( HttpSession : : HttpAccessPathInvoker * ) ctx ;
delete invoker ;
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////RtspSession::onGetRealm/////////////////////////////////////////////
API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_do ( const mk_rtsp_get_realm_invoker ctx ,
const char * realm ) {
assert ( ctx ) ;
RtspSession : : onGetRealm * invoker = ( RtspSession : : onGetRealm * ) ctx ;
( * invoker ) ( realm ? realm : " " ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_rtsp_get_realm_invoker API_CALL mk_rtsp_get_realm_invoker_clone ( const mk_rtsp_get_realm_invoker ctx ) {
assert ( ctx ) ;
RtspSession : : onGetRealm * invoker = ( RtspSession : : onGetRealm * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_rtsp_get_realm_invoker ) new RtspSession : : onGetRealm ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_clone_release ( const mk_rtsp_get_realm_invoker ctx ) {
assert ( ctx ) ;
RtspSession : : onGetRealm * invoker = ( RtspSession : : onGetRealm * ) ctx ;
delete invoker ;
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////RtspSession::onAuth/////////////////////////////////////////////
API_EXPORT void API_CALL mk_rtsp_auth_invoker_do ( const mk_rtsp_auth_invoker ctx ,
int encrypted ,
const char * pwd_or_md5 ) {
assert ( ctx ) ;
RtspSession : : onAuth * invoker = ( RtspSession : : onAuth * ) ctx ;
( * invoker ) ( encrypted , pwd_or_md5 ? pwd_or_md5 : " " ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_rtsp_auth_invoker API_CALL mk_rtsp_auth_invoker_clone ( const mk_rtsp_auth_invoker ctx ) {
assert ( ctx ) ;
RtspSession : : onAuth * invoker = ( RtspSession : : onAuth * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_rtsp_auth_invoker ) new RtspSession : : onAuth ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
API_EXPORT void API_CALL mk_rtsp_auth_invoker_clone_release ( const mk_rtsp_auth_invoker ctx ) {
assert ( ctx ) ;
RtspSession : : onAuth * invoker = ( RtspSession : : onAuth * ) ctx ;
delete invoker ;
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////Broadcast::PublishAuthInvoker/////////////////////////////////////////////
API_EXPORT void API_CALL mk_publish_auth_invoker_do ( const mk_publish_auth_invoker ctx ,
const char * err_msg ,
int enable_hls ,
int enable_mp4 ) {
assert ( ctx ) ;
Broadcast : : PublishAuthInvoker * invoker = ( Broadcast : : PublishAuthInvoker * ) ctx ;
2022-03-12 13:19:21 +08:00
ProtocolOption option ;
option . enable_hls = enable_hls ;
option . enable_mp4 = enable_mp4 ;
( * invoker ) ( err_msg ? err_msg : " " , option ) ;
2019-12-23 18:50:46 +08:00
}
2023-11-12 21:14:46 +08:00
API_EXPORT void API_CALL mk_publish_auth_invoker_do2 ( const mk_publish_auth_invoker ctx , const char * err_msg , mk_ini ini ) {
assert ( ctx ) ;
Broadcast : : PublishAuthInvoker * invoker = ( Broadcast : : PublishAuthInvoker * ) ctx ;
ProtocolOption option ( ini ? * ( ( mINI * ) ini ) : mINI { } ) ;
( * invoker ) ( err_msg ? err_msg : " " , option ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_publish_auth_invoker API_CALL mk_publish_auth_invoker_clone ( const mk_publish_auth_invoker ctx ) {
assert ( ctx ) ;
Broadcast : : PublishAuthInvoker * invoker = ( Broadcast : : PublishAuthInvoker * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_publish_auth_invoker ) new Broadcast : : PublishAuthInvoker ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
API_EXPORT void API_CALL mk_publish_auth_invoker_clone_release ( const mk_publish_auth_invoker ctx ) {
assert ( ctx ) ;
Broadcast : : PublishAuthInvoker * invoker = ( Broadcast : : PublishAuthInvoker * ) ctx ;
delete invoker ;
}
2019-12-23 18:50:46 +08:00
///////////////////////////////////////////Broadcast::AuthInvoker/////////////////////////////////////////////
API_EXPORT void API_CALL mk_auth_invoker_do ( const mk_auth_invoker ctx , const char * err_msg ) {
assert ( ctx ) ;
Broadcast : : AuthInvoker * invoker = ( Broadcast : : AuthInvoker * ) ctx ;
( * invoker ) ( err_msg ? err_msg : " " ) ;
}
2019-12-24 14:24:32 +08:00
API_EXPORT mk_auth_invoker API_CALL mk_auth_invoker_clone ( const mk_auth_invoker ctx ) {
assert ( ctx ) ;
Broadcast : : AuthInvoker * invoker = ( Broadcast : : AuthInvoker * ) ctx ;
2023-02-26 21:45:14 +08:00
return ( mk_auth_invoker ) new Broadcast : : AuthInvoker ( * invoker ) ;
2019-12-24 14:24:32 +08:00
}
2020-02-29 10:27:30 +08:00
API_EXPORT void API_CALL mk_auth_invoker_clone_release ( const mk_auth_invoker ctx ) {
2019-12-24 14:24:32 +08:00
assert ( ctx ) ;
Broadcast : : AuthInvoker * invoker = ( Broadcast : : AuthInvoker * ) ctx ;
delete invoker ;
2024-03-02 16:52:51 +08:00
}
///////////////////////////////////////////WebRtcTransport/////////////////////////////////////////////
2024-05-06 11:07:23 +08:00
API_EXPORT void API_CALL mk_rtc_send_datachannel ( const mk_rtc_transport ctx , uint16_t streamId , uint32_t ppid , const char * msg , size_t len ) {
2024-03-02 16:52:51 +08:00
# ifdef ENABLE_WEBRTC
assert ( ctx & & msg ) ;
WebRtcTransport * transport = ( WebRtcTransport * ) ctx ;
std : : string msg_str ( msg , len ) ;
std : : weak_ptr < WebRtcTransport > weak_trans = transport - > shared_from_this ( ) ;
transport - > getPoller ( ) - > async ( [ streamId , ppid , msg_str , weak_trans ] ( ) {
2024-09-19 14:53:50 +08:00
// 切换线程后再操作 [AUTO-TRANSLATED:12d77fca]
// Operate after switching threads
2024-03-02 16:52:51 +08:00
if ( auto trans = weak_trans . lock ( ) ) {
trans - > sendDatachannel ( streamId , ppid , msg_str . c_str ( ) , msg_str . size ( ) ) ;
}
} ) ;
# else
WarnL < < " 未启用webrtc功能, 编译时请开启ENABLE_WEBRTC " ;
# endif
}