diff --git a/api/include/mk_frame.h b/api/include/mk_frame.h index 40ba4a4d..5df8a617 100644 --- a/api/include/mk_frame.h +++ b/api/include/mk_frame.h @@ -117,6 +117,55 @@ API_EXPORT uint64_t API_CALL mk_frame_get_pts(mk_frame frame); */ API_EXPORT uint32_t API_CALL mk_frame_get_flags(mk_frame frame); +////////////////////////////////////////////////////////////////////// + +typedef struct mk_buffer_t *mk_buffer; +typedef struct mk_frame_merger_t *mk_frame_merger; + +/** + * 创建帧合并器 + * @param type 起始头类型,0: none, 1: h264_prefix/AnnexB(0x 00 00 00 01), 2: mp4_nal_size(avcC) + * @return 帧合并器 + */ +API_EXPORT mk_frame_merger API_CALL mk_frame_merger_create(int type); + +/** + * 销毁帧合并器 + * @param ctx 对象指针 + */ +API_EXPORT void API_CALL mk_frame_merger_release(mk_frame_merger ctx); + +/** + * 清空merger对象缓冲,方便复用 + * @param ctx 对象指针 + */ +API_EXPORT void API_CALL mk_frame_merger_clear(mk_frame_merger ctx); + +/** + * 合并帧回调函数 + * @param user_data 用户数据指针 + * @param dts 解码时间戳 + * @param pts 显示时间戳 + * @param buffer 合并后数据buffer对象 + * @param have_key_frame 合并后数据中是否包含关键帧 + */ +typedef void(API_CALL *on_mk_frame_merger)(void *user_data, uint64_t dts, uint64_t pts, mk_buffer buffer, int have_key_frame); + +/** + * 输入frame到merger对象并合并 + * @param ctx 对象指针 + * @param frame 帧数据 + * @param cb 帧合并回调函数 + * @param user_data 帧合并回调函数用户数据指针 + */ +API_EXPORT void API_CALL mk_frame_merger_input(mk_frame_merger ctx, mk_frame frame, on_mk_frame_merger cb, void *user_data); + +/** + * 强制flush merger对象缓冲,调用此api前需要确保先调用mk_frame_merger_input函数并且回调参数有效 + * @param ctx 对象指针 + */ +API_EXPORT void API_CALL mk_frame_merger_flush(mk_frame_merger ctx); + #ifdef __cplusplus } #endif diff --git a/api/source/mk_frame.cpp b/api/source/mk_frame.cpp index 8c2e4ff6..c136d76f 100644 --- a/api/source/mk_frame.cpp +++ b/api/source/mk_frame.cpp @@ -178,3 +178,29 @@ API_EXPORT uint32_t API_CALL mk_frame_get_flags(mk_frame frame) { } return ret; } + +API_EXPORT mk_frame_merger API_CALL mk_frame_merger_create(int type) { + return reinterpret_cast(new FrameMerger(type)); +} + +API_EXPORT void API_CALL mk_frame_merger_release(mk_frame_merger ctx) { + assert(ctx); + delete reinterpret_cast(ctx); +} + +API_EXPORT void API_CALL mk_frame_merger_clear(mk_frame_merger ctx) { + assert(ctx); + reinterpret_cast(ctx)->clear(); +} + +API_EXPORT void API_CALL mk_frame_merger_flush(mk_frame_merger ctx) { + assert(ctx); + reinterpret_cast(ctx)->flush(); +} + +API_EXPORT void API_CALL mk_frame_merger_input(mk_frame_merger ctx, mk_frame frame, on_mk_frame_merger cb, void *user_data) { + assert(ctx && frame && cb); + reinterpret_cast(ctx)->inputFrame(*((Frame::Ptr *) frame), [cb, user_data](uint64_t dts, uint64_t pts, const toolkit::Buffer::Ptr &buffer, bool have_key_frame) { + cb(user_data, dts, pts, (mk_buffer)(&buffer), have_key_frame); + }); +}