From 2f090e33cf73572b2ba4f7d99b7545777f3ad7d4 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Sat, 11 Feb 2023 15:14:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ec=20sdk=20api=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2user=5Fdata=E5=AF=BC=E8=87=B4=E7=9A=84?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/include/mk_common.h | 6 +++ api/include/mk_events_objects.h | 1 + api/include/mk_frame.h | 3 +- api/include/mk_h264_splitter.h | 1 + api/include/mk_httpclient.h | 3 +- api/include/mk_media.h | 10 +++- api/include/mk_player.h | 2 + api/include/mk_proxyplayer.h | 1 + api/include/mk_pusher.h | 2 + api/include/mk_rtp_server.h | 3 +- api/include/mk_tcp.h | 7 ++- api/include/mk_thread.h | 3 ++ api/include/mk_track.h | 1 + api/include/mk_transcode.h | 1 + api/source/mk_common.cpp | 13 +++-- api/source/mk_events_objects.cpp | 10 ++-- api/source/mk_frame.cpp | 36 +++++++------ api/source/mk_h264_splitter.cpp | 13 +++-- api/source/mk_httpclient.cpp | 21 +++++--- api/source/mk_media.cpp | 91 +++++++++++++++++++++----------- api/source/mk_player.cpp | 36 ++++++++----- api/source/mk_proxyplayer.cpp | 17 +++--- api/source/mk_pusher.cpp | 26 +++++---- api/source/mk_rtp_server.cpp | 18 +++++-- api/source/mk_tcp.cpp | 35 ++++++++---- api/source/mk_tcp_private.h | 4 +- api/source/mk_thread.cpp | 41 +++++++++----- api/source/mk_track.cpp | 9 +++- api/source/mk_transcode.cpp | 9 +++- 29 files changed, 291 insertions(+), 132 deletions(-) diff --git a/api/include/mk_common.h b/api/include/mk_common.h index 845c30c0..e8871949 100755 --- a/api/include/mk_common.h +++ b/api/include/mk_common.h @@ -47,6 +47,9 @@ extern "C" { //输出日志到回调函数(mk_events::on_mk_log) #define LOG_CALLBACK (1 << 2) +//回调user_data回调函数 +typedef void(API_CALL *on_user_data_free)(void *user_data); + typedef struct { // 线程数 int thread_num; @@ -183,6 +186,9 @@ typedef void(API_CALL *on_mk_webrtc_get_answer_sdp)(void *user_data, const char API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url); +API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type, + const char *offer, const char *url); + /** * 创建srt服务器 * @param port srt监听端口 diff --git a/api/include/mk_events_objects.h b/api/include/mk_events_objects.h index 765978a5..7641cb3e 100644 --- a/api/include/mk_events_objects.h +++ b/api/include/mk_events_objects.h @@ -120,6 +120,7 @@ typedef void(API_CALL *on_mk_media_source_send_rtp_result)(void *user_data, uint //MediaSource::startSendRtp,请参考mk_media_start_send_rtp,注意ctx参数类型不一样 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 is_udp, on_mk_media_source_send_rtp_result cb, void *user_data); +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 is_udp, on_mk_media_source_send_rtp_result cb, void *user_data, on_user_data_free user_data_free); //MediaSource::stopSendRtp,请参考mk_media_stop_send_rtp,注意ctx参数类型不一样 API_EXPORT int API_CALL mk_media_source_stop_send_rtp(const mk_media_source ctx); diff --git a/api/include/mk_frame.h b/api/include/mk_frame.h index ec14a508..a5120ebb 100644 --- a/api/include/mk_frame.h +++ b/api/include/mk_frame.h @@ -57,7 +57,8 @@ typedef void(API_CALL *on_mk_frame_data_release)(void *user_data, char *ptr); */ API_EXPORT mk_frame API_CALL mk_frame_create(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size, on_mk_frame_data_release cb, void *user_data); - +API_EXPORT mk_frame API_CALL mk_frame_create2(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size, + on_mk_frame_data_release cb, void *user_data, on_user_data_free user_data_free); /** * 减引用frame对象 * @param frame 帧对象引用 diff --git a/api/include/mk_h264_splitter.h b/api/include/mk_h264_splitter.h index 0692e321..62232414 100644 --- a/api/include/mk_h264_splitter.h +++ b/api/include/mk_h264_splitter.h @@ -36,6 +36,7 @@ typedef void(API_CALL *on_mk_h264_splitter_frame)(void *user_data, mk_h264_split * @return 分帧器对象 */ API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create(on_mk_h264_splitter_frame cb, void *user_data, int is_h265); +API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create2(on_mk_h264_splitter_frame cb, void *user_data, on_user_data_free user_data_free, int is_h265); /** * 删除h264分帧器 diff --git a/api/include/mk_httpclient.h b/api/include/mk_httpclient.h index 3aea1a35..2405e5dd 100755 --- a/api/include/mk_httpclient.h +++ b/api/include/mk_httpclient.h @@ -51,7 +51,7 @@ API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data); - +API_EXPORT void API_CALL mk_http_downloader_start2(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data, on_user_data_free user_data_free); ///////////////////////////////////////////HttpRequester///////////////////////////////////////////// typedef void *mk_http_requester; @@ -143,6 +143,7 @@ API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester c * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data); +API_EXPORT void API_CALL mk_http_requester_set_cb2(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data, on_user_data_free user_data_free); /** * 开始url请求 diff --git a/api/include/mk_media.h b/api/include/mk_media.h index a7efc0d0..5280c904 100755 --- a/api/include/mk_media.h +++ b/api/include/mk_media.h @@ -183,6 +183,7 @@ typedef void(API_CALL *on_mk_media_close)(void *user_data); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data); +API_EXPORT void API_CALL mk_media_set_on_close2(mk_media ctx, on_mk_media_close cb, void *user_data, on_user_data_free user_data_free); /** * 收到客户端的seek请求时触发该回调 @@ -213,6 +214,7 @@ typedef int(API_CALL* on_mk_media_speed)(void* user_data, float speed); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data); +API_EXPORT void API_CALL mk_media_set_on_seek2(mk_media ctx, on_mk_media_seek cb, void *user_data, on_user_data_free user_data_free); /** * 监听播放器pause请求事件 @@ -220,7 +222,8 @@ API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, * @param cb 回调指针 * @param user_data 用户数据指针 */ -API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void* user_data); +API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void *user_data); +API_EXPORT void API_CALL mk_media_set_on_pause2(mk_media ctx, on_mk_media_pause cb, void *user_data, on_user_data_free user_data_free); /** * 监听播放器pause请求事件 @@ -228,7 +231,8 @@ API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause c * @param cb 回调指针 * @param user_data 用户数据指针 */ -API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void* user_data); +API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void *user_data); +API_EXPORT void API_CALL mk_media_set_on_speed2(mk_media ctx, on_mk_media_speed cb, void *user_data, on_user_data_free user_data_free); /** * 获取总的观看人数 @@ -252,6 +256,7 @@ typedef void(API_CALL *on_mk_media_source_regist)(void *user_data, mk_media_sour * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data); +API_EXPORT void API_CALL mk_media_set_on_regist2(mk_media ctx, on_mk_media_source_regist cb, void *user_data, on_user_data_free user_data_free); /** * rtp推流成功与否的回调(第一次成功后,后面将一直重试) @@ -269,6 +274,7 @@ typedef on_mk_media_source_send_rtp_result on_mk_media_send_rtp_result; * @param user_data 回调用户指针 */ API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data); +API_EXPORT void API_CALL mk_media_start_send_rtp2(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data, on_user_data_free user_data_free); /** * 停止某路或全部ps-rtp发送,此api线程安全 diff --git a/api/include/mk_player.h b/api/include/mk_player.h index c74d9739..b57809f7 100755 --- a/api/include/mk_player.h +++ b/api/include/mk_player.h @@ -94,6 +94,7 @@ API_EXPORT void API_CALL mk_player_seekto_pos(mk_player ctx, int seek_pos); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data); +API_EXPORT void API_CALL mk_player_set_on_result2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free); /** * 设置播放被异常中断的回调 @@ -102,6 +103,7 @@ API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data); +API_EXPORT void API_CALL mk_player_set_on_shutdown2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free); ///////////////////////////获取音视频相关信息接口在播放成功回调触发后才有效/////////////////////////////// diff --git a/api/include/mk_proxyplayer.h b/api/include/mk_proxyplayer.h index 53736397..f25bf9b7 100644 --- a/api/include/mk_proxyplayer.h +++ b/api/include/mk_proxyplayer.h @@ -70,6 +70,7 @@ typedef void(API_CALL *on_mk_proxy_player_close)(void *user_data, int err, const * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_proxy_player_set_on_close(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data); +API_EXPORT void API_CALL mk_proxy_player_set_on_close2(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data, on_user_data_free user_data_free); /** * 获取总的观看人数 diff --git a/api/include/mk_pusher.h b/api/include/mk_pusher.h index 2f2883ef..fc7b067c 100644 --- a/api/include/mk_pusher.h +++ b/api/include/mk_pusher.h @@ -79,6 +79,7 @@ API_EXPORT void API_CALL mk_pusher_publish(mk_pusher ctx,const char *url); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_pusher_set_on_result(mk_pusher ctx, on_mk_push_event cb, void *user_data); +API_EXPORT void API_CALL mk_pusher_set_on_result2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free); /** * 设置推流被异常中断的回调 @@ -87,6 +88,7 @@ API_EXPORT void API_CALL mk_pusher_set_on_result(mk_pusher ctx, on_mk_push_event * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_pusher_set_on_shutdown(mk_pusher ctx, on_mk_push_event cb, void *user_data); +API_EXPORT void API_CALL mk_pusher_set_on_shutdown2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free); #ifdef __cplusplus } diff --git a/api/include/mk_rtp_server.h b/api/include/mk_rtp_server.h index 20a0c5ef..f50f438a 100644 --- a/api/include/mk_rtp_server.h +++ b/api/include/mk_rtp_server.h @@ -40,6 +40,7 @@ typedef void(API_CALL *on_mk_rtp_server_connected)(void *user_data, int err, con * @return */ API_EXPORT void API_CALL mk_rtp_server_connect(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data); +API_EXPORT void API_CALL mk_rtp_server_connect2(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data, on_user_data_free user_data_free); /** * 销毁GB28181 RTP 服务器 @@ -67,7 +68,7 @@ typedef void(API_CALL *on_mk_rtp_server_detach)(void *user_data); * @param user_data 回调函数用户数据指针 */ API_EXPORT void API_CALL mk_rtp_server_set_on_detach(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data); - +API_EXPORT void API_CALL mk_rtp_server_set_on_detach2(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data, on_user_data_free user_data_free); #ifdef __cplusplus } diff --git a/api/include/mk_tcp.h b/api/include/mk_tcp.h index 910522f9..8ba87d1f 100644 --- a/api/include/mk_tcp.h +++ b/api/include/mk_tcp.h @@ -31,6 +31,7 @@ typedef void(API_CALL *on_mk_buffer_free)(void *user_data, void *data); * @return buffer对象 */ API_EXPORT mk_buffer API_CALL mk_buffer_from_char(const char *data, size_t len, on_mk_buffer_free cb, void *user_data); +API_EXPORT mk_buffer API_CALL mk_buffer_from_char2(const char *data, size_t len, on_mk_buffer_free cb, void *user_data, on_user_data_free user_data_free); API_EXPORT mk_buffer API_CALL mk_buffer_ref(mk_buffer buffer); API_EXPORT void API_CALL mk_buffer_unref(mk_buffer buffer); API_EXPORT const char* API_CALL mk_buffer_get_data(mk_buffer buffer); @@ -141,7 +142,8 @@ typedef enum { * @param session 会话对象 * @param user_data 用户数据指针 */ -API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session,void *user_data); +API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session, void *user_data); +API_EXPORT void API_CALL mk_tcp_session_set_user_data2(mk_tcp_session session, void *user_data, on_user_data_free user_data_free); /** * 获取tcp会话对象上附着的用户数据 @@ -250,7 +252,8 @@ API_EXPORT void API_CALL mk_tcp_client_send_buffer_safe(mk_tcp_client ctx, mk_bu * @param ctx 客户端对象 * @param user_data 用户数据指针 */ -API_EXPORT void API_CALL mk_tcp_client_set_user_data(mk_tcp_client ctx,void *user_data); +API_EXPORT void API_CALL mk_tcp_client_set_user_data(mk_tcp_client ctx, void *user_data); +API_EXPORT void API_CALL mk_tcp_client_set_user_data2(mk_tcp_client ctx, void *user_data, on_user_data_free user_data_free); /** * 获取客户端对象上附着的用户数据 diff --git a/api/include/mk_thread.h b/api/include/mk_thread.h index dfd80edf..959626a3 100644 --- a/api/include/mk_thread.h +++ b/api/include/mk_thread.h @@ -87,6 +87,7 @@ typedef void (API_CALL *on_mk_async)(void *user_data); * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_async_do(mk_thread ctx, on_mk_async cb, void *user_data); +API_EXPORT void API_CALL mk_async_do2(mk_thread ctx, on_mk_async cb, void *user_data, on_user_data_free user_data_free); /** * 切换到事件线程并延时执行 @@ -96,6 +97,7 @@ API_EXPORT void API_CALL mk_async_do(mk_thread ctx, on_mk_async cb, void *user_d * @param user_data 用户数据指针 */ API_EXPORT void API_CALL mk_async_do_delay(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data); +API_EXPORT void API_CALL mk_async_do_delay2(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data, on_user_data_free user_data_free); /** * 切换到事件线程并同步执行 @@ -123,6 +125,7 @@ typedef uint64_t (API_CALL *on_mk_timer)(void *user_data); * @return 定时器对象 */ API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data); +API_EXPORT mk_timer API_CALL mk_timer_create2(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data, on_user_data_free user_data_free); /** * 销毁和取消定时器 diff --git a/api/include/mk_track.h b/api/include/mk_track.h index aa6b5f38..ff8ddf1c 100644 --- a/api/include/mk_track.h +++ b/api/include/mk_track.h @@ -80,6 +80,7 @@ API_EXPORT int API_CALL mk_track_bit_rate(mk_track track); * @param user_data frame输出回调用户指针参数 */ API_EXPORT void *API_CALL mk_track_add_delegate(mk_track track, on_mk_frame_out cb, void *user_data); +API_EXPORT void *API_CALL mk_track_add_delegate2(mk_track track, on_mk_frame_out cb, void *user_data, on_user_data_free user_data_free); /** * 取消frame输出事件监听 diff --git a/api/include/mk_transcode.h b/api/include/mk_transcode.h index 0616e05b..be97fd15 100644 --- a/api/include/mk_transcode.h +++ b/api/include/mk_transcode.h @@ -78,6 +78,7 @@ API_EXPORT void API_CALL mk_decoder_set_max_async_frame_size(mk_decoder ctx, siz * @param user_data 回调函数用户指针参数 */ API_EXPORT void API_CALL mk_decoder_set_cb(mk_decoder ctx, on_mk_decode cb, void *user_data); +API_EXPORT void API_CALL mk_decoder_set_cb2(mk_decoder ctx, on_mk_decode cb, void *user_data, on_user_data_free user_data_free); /** * 获取FFmpeg原始AVCodecContext对象 diff --git a/api/source/mk_common.cpp b/api/source/mk_common.cpp index e427c23d..0430e685 100644 --- a/api/source/mk_common.cpp +++ b/api/source/mk_common.cpp @@ -294,19 +294,24 @@ private: }; #endif -API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type, +API_EXPORT void API_CALL mk_webrtc_get_answer_sdp(void *user_data, on_mk_webrtc_get_answer_sdp cb, const char *type, + const char *offer, const char *url) { + mk_webrtc_get_answer_sdp2(user_data, nullptr, cb, type, offer, url); +} +API_EXPORT void API_CALL mk_webrtc_get_answer_sdp2(void *user_data, on_user_data_free user_data_free, on_mk_webrtc_get_answer_sdp cb, const char *type, const char *offer, const char *url) { #ifdef ENABLE_WEBRTC assert(type && offer && url && cb); auto session = std::make_shared(Socket::createSocket()); std::string offer_str = offer; + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); WebRtcPluginManager::Instance().getAnswerSdp(*session, type, WebRtcArgsUrl(url), - [offer_str, session, user_data, cb](const WebRtcInterface &exchanger) mutable { + [offer_str, session, ptr, cb](const WebRtcInterface &exchanger) mutable { try { auto sdp_answer = const_cast(exchanger).getAnswerSdp(offer_str); - cb(user_data, sdp_answer.data(), nullptr); + cb(ptr.get(), sdp_answer.data(), nullptr); } catch (std::exception &ex) { - cb(user_data, nullptr, ex.what()); + cb(ptr.get(), nullptr, ex.what()); } }); #else diff --git a/api/source/mk_events_objects.cpp b/api/source/mk_events_objects.cpp index 793ea272..7cd379cb 100644 --- a/api/source/mk_events_objects.cpp +++ b/api/source/mk_events_objects.cpp @@ -208,8 +208,11 @@ API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32 MediaSource *src = (MediaSource *)ctx; return src->seekTo(stamp); } +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 is_udp, on_mk_media_source_send_rtp_result cb, void *user_data) { + mk_media_source_start_send_rtp2(ctx, dst_url, dst_port, ssrc, is_udp, cb, user_data, nullptr); +} -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 is_udp, on_mk_media_source_send_rtp_result cb, void *user_data){ +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 is_udp, 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; @@ -219,9 +222,10 @@ API_EXPORT void API_CALL mk_media_source_start_send_rtp(const mk_media_source ct args.ssrc = ssrc; args.is_udp = is_udp; - src->startSendRtp(args, [cb, user_data](uint16_t local_port, const SockException &ex){ + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + src->startSendRtp(args, [cb, ptr](uint16_t local_port, const SockException &ex){ if (cb) { - cb(user_data, local_port, ex.getErrCode(), ex.what()); + cb(ptr.get(), local_port, ex.getErrCode(), ex.what()); } }); } diff --git a/api/source/mk_frame.cpp b/api/source/mk_frame.cpp index ff45da07..4b026577 100644 --- a/api/source/mk_frame.cpp +++ b/api/source/mk_frame.cpp @@ -27,17 +27,17 @@ public: using Ptr = std::shared_ptr; template - FrameFromPtrForC(bool cache_able, uint32_t flags, on_mk_frame_data_release cb, void *user_data, ARGS &&...args) : FrameFromPtr( + FrameFromPtrForC(bool cache_able, uint32_t flags, on_mk_frame_data_release cb, std::shared_ptr user_data, ARGS &&...args) : FrameFromPtr( std::forward(args)...) { _flags = flags; _cb = cb; - _user_data = user_data; + _user_data = std::move(user_data); _cache_able = cache_able; } ~FrameFromPtrForC() override { if (_cb) { - _cb(_user_data, _ptr); + _cb(_user_data.get(), _ptr); } } @@ -66,43 +66,47 @@ public: private: uint32_t _flags; on_mk_frame_data_release _cb; - void *_user_data; + std::shared_ptr _user_data; bool _cache_able; }; static mk_frame mk_frame_create_complex(int codec_id, uint64_t dts, uint64_t pts, uint32_t frame_flags, size_t prefix_size, - char *data, size_t size, on_mk_frame_data_release cb, void *user_data) { + char *data, size_t size, on_mk_frame_data_release cb, std::shared_ptr user_data) { switch (codec_id) { case CodecH264: - return new Frame::Ptr(new H264FrameHelper(cb, frame_flags, cb, user_data, (CodecId) codec_id, - data, size, dts, pts, prefix_size)); + return new Frame::Ptr(new H264FrameHelper( + cb, frame_flags, cb, std::move(user_data), (CodecId)codec_id, data, size, dts, pts, prefix_size)); case CodecH265: - return new Frame::Ptr(new H265FrameHelper(cb, frame_flags, cb, user_data, (CodecId) codec_id, - data, size, dts, pts, prefix_size)); + return new Frame::Ptr(new H265FrameHelper( + cb, frame_flags, cb, std::move(user_data), (CodecId)codec_id, data, size, dts, pts, prefix_size)); default: - return new Frame::Ptr(new FrameFromPtrForC(cb, frame_flags, cb, user_data, (CodecId) codec_id, data, - size, dts, pts, prefix_size)); + return new Frame::Ptr(new FrameFromPtrForC( + cb, frame_flags, cb, std::move(user_data), (CodecId)codec_id, data, size, dts, pts, prefix_size)); } } API_EXPORT mk_frame API_CALL mk_frame_create(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size, - on_mk_frame_data_release cb, void *user_data) { - + on_mk_frame_data_release cb, void *user_data) { + return mk_frame_create2(codec_id, dts, pts, data, size, cb, user_data, nullptr); +} +API_EXPORT mk_frame API_CALL mk_frame_create2(int codec_id, uint64_t dts, uint64_t pts, const char *data, size_t size, + on_mk_frame_data_release cb, void *user_data, on_user_data_free user_data_free) { + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); switch (codec_id) { case CodecH264: case CodecH265: - return mk_frame_create_complex(codec_id, dts, pts, 0, prefixSize(data, size), (char *)data, size, cb, user_data); + return mk_frame_create_complex(codec_id, dts, pts, 0, prefixSize(data, size), (char *)data, size, cb, std::move(ptr)); case CodecAAC: { int prefix = 0; if ((((uint8_t *) data)[0] == 0xFF && (((uint8_t *) data)[1] & 0xF0) == 0xF0) && size > ADTS_HEADER_LEN) { prefix = ADTS_HEADER_LEN; } - return mk_frame_create_complex(codec_id, dts, pts, 0, prefix, (char *)data, size, cb, user_data); + return mk_frame_create_complex(codec_id, dts, pts, 0, prefix, (char *)data, size, cb, std::move(ptr)); } default: - return mk_frame_create_complex(codec_id, dts, pts, 0, 0, (char *)data, size, cb, user_data); + return mk_frame_create_complex(codec_id, dts, pts, 0, 0, (char *)data, size, cb, std::move(ptr)); } } diff --git a/api/source/mk_h264_splitter.cpp b/api/source/mk_h264_splitter.cpp index c6229fdc..5f146dba 100644 --- a/api/source/mk_h264_splitter.cpp +++ b/api/source/mk_h264_splitter.cpp @@ -86,12 +86,17 @@ const char *H264Splitter::onSearchPacketTail(const char *data, size_t len) { //////////////////////////////////////////////////////////////////////////////////////////////////////// API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create(on_mk_h264_splitter_frame cb, void *user_data, int is_h265) { + return mk_h264_splitter_create2(cb, user_data, nullptr, is_h265); +} + +API_EXPORT mk_h264_splitter API_CALL mk_h264_splitter_create2(on_mk_h264_splitter_frame cb, void *user_data, on_user_data_free user_data_free, int is_h265) { assert(cb); - auto ptr = new H264Splitter(is_h265); - ptr->setOnSplitted([cb, ptr, user_data](const char *data, size_t len) { - cb(user_data, reinterpret_cast(ptr), data, len); + auto ret = new H264Splitter(is_h265); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + ret->setOnSplitted([cb, ptr, ret](const char *data, size_t len) { + cb(ptr.get(), reinterpret_cast(ret), data, len); }); - return reinterpret_cast(ptr); + return reinterpret_cast(ret); } API_EXPORT void API_CALL mk_h264_splitter_release(mk_h264_splitter ctx){ diff --git a/api/source/mk_httpclient.cpp b/api/source/mk_httpclient.cpp index e246335b..4f9b7963 100755 --- a/api/source/mk_httpclient.cpp +++ b/api/source/mk_httpclient.cpp @@ -30,17 +30,21 @@ API_EXPORT void API_CALL mk_http_downloader_release(mk_http_downloader ctx) { } API_EXPORT void API_CALL mk_http_downloader_start(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data) { + mk_http_downloader_start2(ctx, url, file, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_http_downloader_start2(mk_http_downloader ctx, const char *url, const char *file, on_mk_download_complete cb, void *user_data, on_user_data_free user_data_free) { assert(ctx && url && file); HttpDownloader::Ptr *obj = (HttpDownloader::Ptr *) ctx; - (*obj)->setOnResult([cb, user_data](const SockException &ex, const string &filePath) { + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnResult([cb, ptr](const SockException &ex, const string &filePath) { if (cb) { - cb(user_data, ex.getErrCode(), ex.what(), filePath.data()); + cb(ptr.get(), ex.getErrCode(), ex.what(), filePath.data()); } }); (*obj)->startDownload(url, file, false); } - ///////////////////////////////////////////HttpRequester///////////////////////////////////////////// API_EXPORT mk_http_requester API_CALL mk_http_requester_create(){ HttpRequester::Ptr *ret = new HttpRequester::Ptr(new HttpRequester); @@ -128,11 +132,16 @@ API_EXPORT mk_parser API_CALL mk_http_requester_get_response(mk_http_requester c return (mk_parser)&((*obj)->response()); } -API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data){ +API_EXPORT void API_CALL mk_http_requester_set_cb(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data) { + mk_http_requester_set_cb2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_http_requester_set_cb2(mk_http_requester ctx,on_mk_http_requester_complete cb, void *user_data, on_user_data_free user_data_free) { assert(ctx && cb); HttpRequester::Ptr *obj = (HttpRequester::Ptr *)ctx; - (*obj)->setOnResult([cb, user_data](const SockException &ex, const Parser &res) { - cb(user_data, ex.getErrCode(), ex.what()); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnResult([cb, ptr](const SockException &ex, const Parser &res) { + cb(ptr.get(), ex.getErrCode(), ex.what()); }); } diff --git a/api/source/mk_media.cpp b/api/source/mk_media.cpp index 7200e46e..d6e125d8 100755 --- a/api/source/mk_media.cpp +++ b/api/source/mk_media.cpp @@ -23,7 +23,8 @@ public: MediaHelper(ArgsType &&...args){ _channel = std::make_shared(std::forward(args)...); } - ~MediaHelper(){} + + ~MediaHelper() = default; void attachEvent(){ _channel->setMediaListener(shared_from_this()); @@ -33,29 +34,29 @@ public: return _channel; } - void setOnClose(on_mk_media_close cb, void *user_data){ + void setOnClose(on_mk_media_close cb, std::shared_ptr user_data){ _on_close = cb; - _on_close_data = user_data; + _on_close_data = std::move(user_data); } - void setOnSeek(on_mk_media_seek cb, void *user_data){ + void setOnSeek(on_mk_media_seek cb, std::shared_ptr user_data){ _on_seek = cb; - _on_seek_data = user_data; + _on_seek_data = std::move(user_data); } - void setOnPause(on_mk_media_pause cb, void* user_data) { + void setOnPause(on_mk_media_pause cb, std::shared_ptr user_data) { _on_pause = cb; - _on_pause_data = user_data; + _on_pause_data = std::move(user_data); } - void setOnSpeed(on_mk_media_speed cb, void* user_data) { + void setOnSpeed(on_mk_media_speed cb, std::shared_ptr user_data) { _on_speed = cb; - _on_speed_data = user_data; + _on_speed_data = std::move(user_data); } - void setOnRegist(on_mk_media_source_regist cb, void *user_data){ + void setOnRegist(on_mk_media_source_regist cb, std::shared_ptr user_data){ _on_regist = cb; - _on_regist_data = user_data; + _on_regist_data = std::move(user_data); } protected: @@ -67,7 +68,7 @@ protected: return false; } //请在回调中调用mk_media_release函数释放资源,否则MediaSource::close()操作不会生效 - _on_close(_on_close_data); + _on_close(_on_close_data.get()); WarnL << "close media: " << sender.getUrl(); return true; } @@ -76,7 +77,7 @@ protected: if (!_on_seek) { return false; } - return _on_seek(_on_seek_data, stamp); + return _on_seek(_on_seek_data.get(), stamp); } // 通知暂停或恢复 @@ -84,7 +85,7 @@ protected: if (!_on_pause) { return false; } - return _on_pause(_on_pause_data, pause); + return _on_pause(_on_pause_data.get(), pause); } //通知倍数播放 @@ -92,12 +93,12 @@ protected: if (!_on_speed) { return false; } - return _on_speed(_on_speed_data, speed); + return _on_speed(_on_speed_data.get(), speed); } void onRegist(MediaSource &sender, bool regist) override{ if (_on_regist) { - _on_regist(_on_regist_data, &sender, regist); + _on_regist(_on_regist_data.get(), &sender, regist); } } @@ -108,41 +109,66 @@ private: on_mk_media_pause _on_pause = nullptr; on_mk_media_speed _on_speed = nullptr; on_mk_media_source_regist _on_regist = nullptr; - void* _on_seek_data; - void* _on_pause_data; - void* _on_speed_data; - void *_on_close_data; - void *_on_regist_data; + std::shared_ptr _on_seek_data; + std::shared_ptr _on_pause_data; + std::shared_ptr _on_speed_data; + std::shared_ptr _on_close_data; + std::shared_ptr _on_regist_data; }; API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data){ + mk_media_set_on_close2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_set_on_close2(mk_media ctx, on_mk_media_close cb, void *user_data, on_user_data_free user_data_free) { assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; - (*obj)->setOnClose(cb, user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnClose(cb, std::move(ptr)); } API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data) { + mk_media_set_on_seek2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_set_on_seek2(mk_media ctx, on_mk_media_seek cb, void *user_data, on_user_data_free user_data_free) { assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; - (*obj)->setOnSeek(cb, user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnSeek(cb, std::move(ptr)); } API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void *user_data) { + mk_media_set_on_pause2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_set_on_pause2(mk_media ctx, on_mk_media_pause cb, void *user_data, on_user_data_free user_data_free) { assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; - (*obj)->setOnPause(cb, user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnPause(cb, std::move(ptr)); } API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void *user_data) { + mk_media_set_on_speed2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_set_on_speed2(mk_media ctx, on_mk_media_speed cb, void *user_data, on_user_data_free user_data_free){ assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; - (*obj)->setOnSpeed(cb, user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnSpeed(cb, std::move(ptr)); } API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data){ + mk_media_set_on_regist2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_set_on_regist2(mk_media ctx, on_mk_media_source_regist cb, void *user_data, on_user_data_free user_data_free) { assert(ctx); MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx; - (*obj)->setOnRegist(cb, user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->setOnRegist(cb, std::move(ptr)); } API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx){ @@ -254,7 +280,11 @@ API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void* data, int return (*obj)->getChannel()->inputAudio((const char*)data, len, dts); } -API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data){ +API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data) { + mk_media_start_send_rtp2(ctx, dst_url, dst_port, ssrc, is_udp, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_media_start_send_rtp2(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data, on_user_data_free user_data_free){ assert(ctx && dst_url && ssrc); MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx; @@ -266,10 +296,11 @@ API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_u // sender参数无用 auto ref = *obj; - (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, user_data]() { - ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, user_data](uint16_t local_port, const SockException &ex) { + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, ptr]() { + ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, ptr](uint16_t local_port, const SockException &ex) { if (cb) { - cb(user_data, local_port, ex.getErrCode(), ex.what()); + cb(ptr.get(), local_port, ex.getErrCode(), ex.what()); } }); }); diff --git a/api/source/mk_player.cpp b/api/source/mk_player.cpp index df128353..104b272d 100755 --- a/api/source/mk_player.cpp +++ b/api/source/mk_player.cpp @@ -61,7 +61,7 @@ public: if (is_shutdown) { //播放中断 if (_on_shutdown) { - _on_shutdown(_on_shutdown_data, ex.getErrCode(), ex.what(), nullptr, 0); + _on_shutdown(_on_shutdown_data.get(), ex.getErrCode(), ex.what(), nullptr, 0); } return; } @@ -74,17 +74,17 @@ public: for (auto &track : cpp_tracks) { tracks[track_count++] = (mk_track) &track; } - _on_play(_on_play_data, ex.getErrCode(), ex.what(), tracks, track_count); + _on_play(_on_play_data.get(), ex.getErrCode(), ex.what(), tracks, track_count); } } - void setOnEvent(on_mk_play_event cb, void *user_data, int type) { + void setOnEvent(on_mk_play_event cb, std::shared_ptr user_data, int type) { lock_guard lck(_mtx); - if(type == 0){ - _on_play_data = user_data; + if (type == 0) { + _on_play_data = std::move(user_data); _on_play = cb; - }else{ - _on_shutdown_data = user_data; + } else { + _on_shutdown_data = std::move(user_data); _on_shutdown = cb; } } @@ -98,8 +98,8 @@ private: on_mk_play_event _on_play = nullptr; on_mk_play_event _on_shutdown = nullptr; - void *_on_play_data = nullptr; - void *_on_shutdown_data = nullptr; + std::shared_ptr _on_play_data; + std::shared_ptr _on_shutdown_data; }; API_EXPORT mk_player API_CALL mk_player_create() { @@ -175,18 +175,28 @@ API_EXPORT void API_CALL mk_player_seekto_pos(mk_player ctx, int seek_pos) { }); } -static void mk_player_set_on_event(mk_player ctx, on_mk_play_event cb, void *user_data, int type) { +static void mk_player_set_on_event(mk_player ctx, on_mk_play_event cb, std::shared_ptr user_data, int type) { assert(ctx); MediaPlayerForC &obj = **((MediaPlayerForC::Ptr *)ctx); - obj.setOnEvent(cb,user_data, type); + obj.setOnEvent(cb, std::move(user_data), type); } API_EXPORT void API_CALL mk_player_set_on_result(mk_player ctx, on_mk_play_event cb, void *user_data) { - mk_player_set_on_event(ctx,cb,user_data,0); + mk_player_set_on_result2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_player_set_on_result2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free) { + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + mk_player_set_on_event(ctx, cb, std::move(ptr), 0); } API_EXPORT void API_CALL mk_player_set_on_shutdown(mk_player ctx, on_mk_play_event cb, void *user_data) { - mk_player_set_on_event(ctx,cb,user_data,1); + mk_player_set_on_shutdown2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_player_set_on_shutdown2(mk_player ctx, on_mk_play_event cb, void *user_data, on_user_data_free user_data_free){ + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + mk_player_set_on_event(ctx, cb, std::move(ptr), 1); } API_EXPORT float API_CALL mk_player_duration(mk_player ctx) { diff --git a/api/source/mk_proxyplayer.cpp b/api/source/mk_proxyplayer.cpp index 18dc2b9d..01171e77 100644 --- a/api/source/mk_proxyplayer.cpp +++ b/api/source/mk_proxyplayer.cpp @@ -50,13 +50,18 @@ API_EXPORT void API_CALL mk_proxy_player_play(mk_proxy_player ctx, const char *u } API_EXPORT void API_CALL mk_proxy_player_set_on_close(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data){ + mk_proxy_player_set_on_close2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_proxy_player_set_on_close2(mk_proxy_player ctx, on_mk_proxy_player_close cb, void *user_data, on_user_data_free user_data_free) { assert(ctx); - PlayerProxy::Ptr &obj = *((PlayerProxy::Ptr *) ctx); - obj->getPoller()->async([obj,cb,user_data](){ - //切换线程再操作 - obj->setOnClose([cb,user_data](const SockException &ex){ - if(cb){ - cb(user_data, ex.getErrCode(), ex.what(), ex.getCustomCode()); + PlayerProxy::Ptr &obj = *((PlayerProxy::Ptr *)ctx); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + obj->getPoller()->async([obj, cb, ptr]() { + // 切换线程再操作 + obj->setOnClose([cb, ptr](const SockException &ex) { + if (cb) { + cb(ptr.get(), ex.getErrCode(), ex.what(), ex.getCustomCode()); } }); }); diff --git a/api/source/mk_pusher.cpp b/api/source/mk_pusher.cpp index a63a7708..b44a8d89 100644 --- a/api/source/mk_pusher.cpp +++ b/api/source/mk_pusher.cpp @@ -55,23 +55,29 @@ API_EXPORT void API_CALL mk_pusher_publish(mk_pusher ctx,const char *url){ } API_EXPORT void API_CALL mk_pusher_set_on_result(mk_pusher ctx, on_mk_push_event cb, void *user_data){ + mk_pusher_set_on_result2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_pusher_set_on_result2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free) { assert(ctx && cb); MediaPusher::Ptr &obj = *((MediaPusher::Ptr *)ctx); - obj->getPoller()->async([obj,cb,user_data](){ - //切换线程再操作 - obj->setOnPublished([cb,user_data](const SockException &ex){ - cb(user_data,ex.getErrCode(),ex.what()); - }); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + obj->getPoller()->async([obj, cb, ptr]() { + // 切换线程再操作 + obj->setOnPublished([cb, ptr](const SockException &ex) { cb(ptr.get(), ex.getErrCode(), ex.what()); }); }); } API_EXPORT void API_CALL mk_pusher_set_on_shutdown(mk_pusher ctx, on_mk_push_event cb, void *user_data){ + mk_pusher_set_on_shutdown2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_pusher_set_on_shutdown2(mk_pusher ctx, on_mk_push_event cb, void *user_data, on_user_data_free user_data_free) { assert(ctx && cb); MediaPusher::Ptr &obj = *((MediaPusher::Ptr *)ctx); - obj->getPoller()->async([obj,cb,user_data](){ - //切换线程再操作 - obj->setOnShutdown([cb,user_data](const SockException &ex){ - cb(user_data,ex.getErrCode(),ex.what()); - }); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + obj->getPoller()->async([obj, cb, ptr]() { + // 切换线程再操作 + obj->setOnShutdown([cb, ptr](const SockException &ex) { cb(ptr.get(), ex.getErrCode(), ex.what()); }); }); } diff --git a/api/source/mk_rtp_server.cpp b/api/source/mk_rtp_server.cpp index 617418c6..79517263 100644 --- a/api/source/mk_rtp_server.cpp +++ b/api/source/mk_rtp_server.cpp @@ -23,11 +23,16 @@ API_EXPORT mk_rtp_server API_CALL mk_rtp_server_create(uint16_t port, int tcp_mo } API_EXPORT void API_CALL mk_rtp_server_connect(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data) { + mk_rtp_server_connect2(ctx, dst_url, dst_port, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_rtp_server_connect2(mk_rtp_server ctx, const char *dst_url, uint16_t dst_port, on_mk_rtp_server_connected cb, void *user_data, on_user_data_free user_data_free){ RtpServer::Ptr *server = (RtpServer::Ptr *)ctx; if (server) { - (*server)->connectToServer(dst_url, dst_port, [cb, user_data](const SockException &ex) { + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*server)->connectToServer(dst_url, dst_port, [cb, ptr](const SockException &ex) { if (cb) { - cb(user_data, ex.getErrCode(), ex.what(), ex.getCustomCode()); + cb(ptr.get(), ex.getErrCode(), ex.what(), ex.getCustomCode()); } }); } @@ -44,10 +49,15 @@ API_EXPORT uint16_t API_CALL mk_rtp_server_port(mk_rtp_server ctx) { } API_EXPORT void API_CALL mk_rtp_server_set_on_detach(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data) { + mk_rtp_server_set_on_detach2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_rtp_server_set_on_detach2(mk_rtp_server ctx, on_mk_rtp_server_detach cb, void *user_data, on_user_data_free user_data_free){ RtpServer::Ptr *server = (RtpServer::Ptr *) ctx; if (cb) { - (*server)->setOnDetach([cb, user_data]() { - cb(user_data); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*server)->setOnDetach([cb, ptr]() { + cb(ptr.get()); }); } else { (*server)->setOnDetach(nullptr); diff --git a/api/source/mk_tcp.cpp b/api/source/mk_tcp.cpp index 2e2dc6e1..62f58376 100644 --- a/api/source/mk_tcp.cpp +++ b/api/source/mk_tcp.cpp @@ -20,7 +20,7 @@ using namespace mediakit; class BufferForC : public Buffer { public: - BufferForC(const char *data, size_t len, on_mk_buffer_free cb, void *user_data) { + BufferForC(const char *data, size_t len, on_mk_buffer_free cb, std::shared_ptr user_data) { if (len <= 0) { len = strlen(data); } @@ -36,11 +36,11 @@ public: _data = (char *) data; _size = len; _cb = cb; - _user_data = user_data; + _user_data = std::move(user_data); } ~BufferForC() override { - _cb(_user_data, _data); + _cb(_user_data.get(), _data); } char *data() const override { @@ -55,12 +55,17 @@ private: char *_data; size_t _size; on_mk_buffer_free _cb; - void *_user_data; + std::shared_ptr _user_data; }; API_EXPORT mk_buffer API_CALL mk_buffer_from_char(const char *data, size_t len, on_mk_buffer_free cb, void *user_data) { + return mk_buffer_from_char2(data, len, cb, user_data, nullptr); +} + +API_EXPORT mk_buffer API_CALL mk_buffer_from_char2(const char *data, size_t len, on_mk_buffer_free cb, void *user_data, on_user_data_free user_data_free) { assert(data); - return new Buffer::Ptr(std::make_shared(data, len, cb, user_data)); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + return new Buffer::Ptr(std::make_shared(data, len, cb, std::move(ptr))); } API_EXPORT mk_buffer API_CALL mk_buffer_ref(mk_buffer buffer) { @@ -200,16 +205,21 @@ void stopAllTcpServer(){ CLEAR_ARR(s_tcp_server); } -API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session,void *user_data){ +API_EXPORT void API_CALL mk_tcp_session_set_user_data(mk_tcp_session session, void *user_data) { + mk_tcp_session_set_user_data2(session, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_tcp_session_set_user_data2(mk_tcp_session session, void *user_data, on_user_data_free user_data_free) { assert(session); SessionForC *obj = (SessionForC *)session; - obj->_user_data = user_data; + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + obj->_user_data = std::move(ptr); } API_EXPORT void* API_CALL mk_tcp_session_get_user_data(mk_tcp_session session){ assert(session); SessionForC *obj = (SessionForC *)session; - return obj->_user_data; + return obj->_user_data.get(); } API_EXPORT void API_CALL mk_tcp_server_events_listen(const mk_tcp_session_events *events){ @@ -364,13 +374,18 @@ API_EXPORT void API_CALL mk_tcp_client_send_safe(mk_tcp_client ctx, const char * } API_EXPORT void API_CALL mk_tcp_client_set_user_data(mk_tcp_client ctx,void *user_data){ + mk_tcp_client_set_user_data2(ctx, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_tcp_client_set_user_data2(mk_tcp_client ctx, void *user_data, on_user_data_free user_data_free) { assert(ctx); TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; - (*client)->_user_data = user_data; + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + (*client)->_user_data = std::move(ptr); } API_EXPORT void* API_CALL mk_tcp_client_get_user_data(mk_tcp_client ctx){ assert(ctx); TcpClientForC::Ptr *client = (TcpClientForC::Ptr *)ctx; - return (*client)->_user_data; + return (*client)->_user_data.get(); } diff --git a/api/source/mk_tcp_private.h b/api/source/mk_tcp_private.h index 7bd3a939..241e88b6 100644 --- a/api/source/mk_tcp_private.h +++ b/api/source/mk_tcp_private.h @@ -25,7 +25,7 @@ public: void onManager() override; void onConnect(const toolkit::SockException &ex) override; void setClient(mk_tcp_client client); - void *_user_data; + std::shared_ptr _user_data; private: mk_tcp_client_events _events; mk_tcp_client _client; @@ -38,7 +38,7 @@ public: void onRecv(const toolkit::Buffer::Ptr &buffer) override ; void onError(const toolkit::SockException &err) override; void onManager() override; - void *_user_data; + std::shared_ptr _user_data; uint16_t _local_port; }; diff --git a/api/source/mk_thread.cpp b/api/source/mk_thread.cpp index ae803ef0..d783ff5e 100644 --- a/api/source/mk_thread.cpp +++ b/api/source/mk_thread.cpp @@ -44,11 +44,23 @@ API_EXPORT void API_CALL mk_async_do(mk_thread ctx,on_mk_async cb, void *user_da }); } +API_EXPORT void API_CALL mk_async_do2(mk_thread ctx, on_mk_async cb, void *user_data, on_user_data_free user_data_free){ + assert(ctx && cb); + EventPoller *poller = (EventPoller *)ctx; + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + poller->async([cb, ptr]() { cb(ptr.get()); }); +} + API_EXPORT void API_CALL mk_async_do_delay(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data) { + mk_async_do_delay2(ctx, ms, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_async_do_delay2(mk_thread ctx, size_t ms, on_mk_async cb, void *user_data, on_user_data_free user_data_free){ assert(ctx && cb && ms); - EventPoller *poller = (EventPoller *) ctx; - poller->doDelayTask(ms, [cb, user_data]() { - cb(user_data); + EventPoller *poller = (EventPoller *)ctx; + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + poller->doDelayTask(ms, [cb, ptr]() { + cb(ptr.get()); return 0; }); } @@ -56,28 +68,26 @@ API_EXPORT void API_CALL mk_async_do_delay(mk_thread ctx, size_t ms, on_mk_async API_EXPORT void API_CALL mk_sync_do(mk_thread ctx,on_mk_async cb, void *user_data){ assert(ctx && cb); EventPoller *poller = (EventPoller *)ctx; - poller->sync([cb,user_data](){ - cb(user_data); - }); + poller->sync([cb, user_data]() { cb(user_data); }); } class TimerForC : public std::enable_shared_from_this{ public: using Ptr = std::shared_ptr; - TimerForC(on_mk_timer cb, void *user_data){ + TimerForC(on_mk_timer cb, std::shared_ptr user_data) { _cb = cb; - _user_data = user_data; + _user_data = std::move(user_data); } - ~TimerForC(){} + ~TimerForC() = default; uint64_t operator()(){ lock_guard lck(_mxt); if(!_cb){ return 0; } - return _cb(_user_data); + return _cb(_user_data.get()); } void cancel(){ @@ -98,15 +108,20 @@ public: } private: on_mk_timer _cb = nullptr; - void *_user_data = nullptr; + std::shared_ptr _user_data; recursive_mutex _mxt; EventPoller::DelayTask::Ptr _task; }; -API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx,uint64_t delay_ms,on_mk_timer cb, void *user_data){ +API_EXPORT mk_timer API_CALL mk_timer_create(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data) { + return mk_timer_create2(ctx, delay_ms, cb, user_data, nullptr); +} + +API_EXPORT mk_timer API_CALL mk_timer_create2(mk_thread ctx, uint64_t delay_ms, on_mk_timer cb, void *user_data, on_user_data_free user_data_free){ assert(ctx && cb); EventPoller *poller = (EventPoller *)ctx; - TimerForC::Ptr *ret = new TimerForC::Ptr(new TimerForC(cb, user_data)); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + TimerForC::Ptr *ret = new TimerForC::Ptr(new TimerForC(cb, ptr)); (*ret)->start(delay_ms,*poller); return ret; } diff --git a/api/source/mk_track.cpp b/api/source/mk_track.cpp index 3190b936..578a5258 100644 --- a/api/source/mk_track.cpp +++ b/api/source/mk_track.cpp @@ -114,9 +114,14 @@ API_EXPORT int API_CALL mk_track_bit_rate(mk_track track) { } API_EXPORT void *API_CALL mk_track_add_delegate(mk_track track, on_mk_frame_out cb, void *user_data) { + return mk_track_add_delegate2(track, cb, user_data, nullptr); +} + +API_EXPORT void *API_CALL mk_track_add_delegate2(mk_track track, on_mk_frame_out cb, void *user_data, on_user_data_free user_data_free){ assert(track && cb); - return (*((Track::Ptr *) track))->addDelegate([cb, user_data](const Frame::Ptr &frame) { - cb(user_data, (mk_frame) &frame); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + return (*((Track::Ptr *) track))->addDelegate([cb, ptr](const Frame::Ptr &frame) { + cb(ptr.get(), (mk_frame) &frame); return true; }); } diff --git a/api/source/mk_transcode.cpp b/api/source/mk_transcode.cpp index 83981354..294db8eb 100644 --- a/api/source/mk_transcode.cpp +++ b/api/source/mk_transcode.cpp @@ -57,9 +57,14 @@ API_EXPORT void API_CALL mk_decoder_set_max_async_frame_size(mk_decoder ctx, siz } API_EXPORT void API_CALL mk_decoder_set_cb(mk_decoder ctx, on_mk_decode cb, void *user_data) { + mk_decoder_set_cb2(ctx, cb, user_data, nullptr); +} + +API_EXPORT void API_CALL mk_decoder_set_cb2(mk_decoder ctx, on_mk_decode cb, void *user_data, on_user_data_free user_data_free){ assert(ctx && cb); - ((FFmpegDecoder *) ctx)->setOnDecode([cb, user_data](const FFmpegFrame::Ptr &pix_frame) { - cb(user_data, (mk_frame_pix) &pix_frame); + std::shared_ptr ptr(user_data, user_data_free ? user_data_free : [](void *) {}); + ((FFmpegDecoder *) ctx)->setOnDecode([cb, ptr](const FFmpegFrame::Ptr &pix_frame) { + cb(ptr.get(), (mk_frame_pix) &pix_frame); }); }