c sdk新增mpeg-ps/ts打包接口 (#2620)

This commit is contained in:
xiangshengjye 2023-07-06 14:11:21 +08:00 committed by GitHub
parent 6b9889a883
commit 7c6b612a4d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 118 additions and 0 deletions

View File

@ -166,6 +166,59 @@ API_EXPORT void API_CALL mk_frame_merger_input(mk_frame_merger ctx, mk_frame fra
*/
API_EXPORT void API_CALL mk_frame_merger_flush(mk_frame_merger ctx);
//////////////////////////////////////////////////////////////////////
typedef struct mk_mpeg_muxer_t *mk_mpeg_muxer;
/**
* mpeg-ps/ts
* @param user_data
* @param muxer
* @param frame
* @param size
* @param timestamp
* @param key_pos
*/
typedef void(API_CALL *on_mk_mpeg_muxer_frame)(void *user_data, mk_mpeg_muxer muxer, const char *frame, size_t size, uint64_t timestamp, int key_pos);
/**
* mpeg-ps/ts
* @param cb
* @param user_data
* @param is_ps ps
* @return
*/
API_EXPORT mk_mpeg_muxer API_CALL mk_mpeg_muxer_create(on_mk_mpeg_muxer_frame cb, void *user_data, int is_ps);
/**
* mpeg-ps/ts
* @param ctx
*/
API_EXPORT void API_CALL mk_mpeg_muxer_release(mk_mpeg_muxer ctx);
/**
* track
* @param ctx mk_mpeg_muxer对象
* @param track mk_track对象
*/
API_EXPORT void API_CALL mk_mpeg_muxer_init_track(mk_mpeg_muxer ctx, void* track);
/**
* track完毕后调用此函数
* track()ZLMediaKit不知道后续是否还要添加track3
* Track类型便(3)
* @param ctx
*/
API_EXPORT void API_CALL mk_mpeg_muxer_init_complete(mk_mpeg_muxer ctx);
/**
* frame对象
* @param ctx mk_mpeg_muxer对象
* @param frame
* @return 10
*/
API_EXPORT int API_CALL mk_mpeg_muxer_input_frame(mk_mpeg_muxer ctx, mk_frame frame);
#ifdef __cplusplus
}
#endif

View File

@ -9,10 +9,12 @@
*/
#include "mk_frame.h"
#include "mk_track.h"
#include "Extension/Frame.h"
#include "Extension/H264.h"
#include "Extension/H265.h"
#include "Extension/AAC.h"
#include "Record/MPEG.h"
using namespace mediakit;
@ -204,3 +206,66 @@ API_EXPORT void API_CALL mk_frame_merger_input(mk_frame_merger ctx, mk_frame fra
cb(user_data, dts, pts, (mk_buffer)(&buffer), have_key_frame);
});
}
//////////////////////////////////////////////////////////////////////
class MpegMuxerForC : public MpegMuxer {
public:
using onMuxer = std::function<void(const char *frame, size_t size, uint64_t timestamp, int key_pos)>;
MpegMuxerForC(bool is_ps) : MpegMuxer(is_ps) {
_cb = nullptr;
}
~MpegMuxerForC() { MpegMuxer::flush(); };
void setOnMuxer(onMuxer cb) {
_cb = std::move(cb);
}
private:
void onWrite(std::shared_ptr<toolkit::Buffer> buffer, uint64_t timestamp, bool key_pos) override {
if (_cb) {
if (!buffer) {
_cb(nullptr, 0, timestamp, key_pos);
} else {
_cb(buffer->data(), buffer->size(), timestamp, key_pos);
}
}
}
private:
onMuxer _cb;
};
API_EXPORT mk_mpeg_muxer API_CALL mk_mpeg_muxer_create(on_mk_mpeg_muxer_frame cb, void *user_data, int is_ps){
assert(cb);
auto ret = new MpegMuxerForC(is_ps);
std::shared_ptr<void> ptr(user_data, [](void *) {});
ret->setOnMuxer([cb, ptr, ret](const char *frame, size_t size, uint64_t timestamp, int key_pos) {
cb(ptr.get(), reinterpret_cast<mk_mpeg_muxer>(ret), frame, size, timestamp, key_pos);
});
return reinterpret_cast<mk_mpeg_muxer>(ret);
}
API_EXPORT void API_CALL mk_mpeg_muxer_release(mk_mpeg_muxer ctx){
assert(ctx);
auto ptr = reinterpret_cast<MpegMuxerForC *>(ctx);
delete ptr;
}
API_EXPORT void API_CALL mk_mpeg_muxer_init_track(mk_mpeg_muxer ctx, void* track) {
assert(ctx && track);
auto ptr = reinterpret_cast<MpegMuxerForC *>(ctx);
ptr->addTrack(*((Track::Ptr *) track));
}
API_EXPORT void API_CALL mk_mpeg_muxer_init_complete(mk_mpeg_muxer ctx) {
assert(ctx);
auto ptr = reinterpret_cast<MpegMuxerForC *>(ctx);
ptr->addTrackCompleted();
}
API_EXPORT int API_CALL mk_mpeg_muxer_input_frame(mk_mpeg_muxer ctx, mk_frame frame){
assert(ctx && frame);
auto ptr = reinterpret_cast<MpegMuxerForC *>(ctx);
return ptr->inputFrame(*((Frame::Ptr *) frame));
}