diff --git a/3rdpart/media-server b/3rdpart/media-server index 780339e3..9732c4c2 160000 --- a/3rdpart/media-server +++ b/3rdpart/media-server @@ -1 +1 @@ -Subproject commit 780339e3f1aa28a9b2e7d03a1a84b96ee256451b +Subproject commit 9732c4c2e4c75a789bce2f3e979c012c2cce4db7 diff --git a/src/Record/MP4.cpp b/src/Record/MP4.cpp index 3926e01a..845f0a0b 100644 --- a/src/Record/MP4.cpp +++ b/src/Record/MP4.cpp @@ -13,91 +13,10 @@ #include "Util/File.h" #include "Util/logger.h" #include "Common/config.h" -#include "fmp4-writer.h" using namespace toolkit; namespace mediakit { -/////////////////////////////////////////////////mp4_writer_t///////////////////////////////////////////////// - -struct mp4_writer_t { - int is_fmp4; - union { - fmp4_writer_t *fmp4; - mov_writer_t *mov; - } u; -}; - -mp4_writer_t* mp4_writer_create(int is_fmp4, const struct mov_buffer_t *buffer, void* param, int flags){ - mp4_writer_t *mp4 = (mp4_writer_t *) malloc(sizeof(mp4_writer_t)); - mp4->is_fmp4 = is_fmp4; - if (is_fmp4) { - mp4->u.fmp4 = fmp4_writer_create(buffer, param, flags); - } else { - mp4->u.mov = mov_writer_create(buffer, param, flags); - } - return mp4; -} - -void mp4_writer_destroy(mp4_writer_t* mp4){ - if (mp4->is_fmp4) { - fmp4_writer_destroy(mp4->u.fmp4); - } else { - mov_writer_destroy(mp4->u.mov); - } - free(mp4); -} - -int mp4_writer_add_audio(mp4_writer_t* mp4, uint8_t object, int channel_count, int bits_per_sample, int sample_rate, const void* extra_data, size_t extra_data_size){ - if (mp4->is_fmp4) { - return fmp4_writer_add_audio(mp4->u.fmp4, object, channel_count, bits_per_sample, sample_rate, extra_data, extra_data_size); - } else { - return mov_writer_add_audio(mp4->u.mov, object, channel_count, bits_per_sample, sample_rate, extra_data, extra_data_size); - } -} - -int mp4_writer_add_video(mp4_writer_t* mp4, uint8_t object, int width, int height, const void* extra_data, size_t extra_data_size){ - if (mp4->is_fmp4) { - return fmp4_writer_add_video(mp4->u.fmp4, object, width, height, extra_data, extra_data_size); - } else { - return mov_writer_add_video(mp4->u.mov, object, width, height, extra_data, extra_data_size); - } -} - -int mp4_writer_add_subtitle(mp4_writer_t* mp4, uint8_t object, const void* extra_data, size_t extra_data_size){ - if (mp4->is_fmp4) { - return fmp4_writer_add_subtitle(mp4->u.fmp4, object, extra_data, extra_data_size); - } else { - return mov_writer_add_subtitle(mp4->u.mov, object, extra_data, extra_data_size); - } -} - -int mp4_writer_write(mp4_writer_t* mp4, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags){ - if (mp4->is_fmp4) { - return fmp4_writer_write(mp4->u.fmp4, track, data, bytes, pts, dts, flags); - } else { - return mov_writer_write(mp4->u.mov, track, data, bytes, pts, dts, flags); - } -} - -int mp4_writer_save_segment(mp4_writer_t* mp4){ - if (mp4->is_fmp4) { - return fmp4_writer_save_segment(mp4->u.fmp4); - } else { - return -1; - } -} - -int mp4_writer_init_segment(mp4_writer_t* mp4){ - if (mp4->is_fmp4) { - return fmp4_writer_init_segment(mp4->u.fmp4); - } else { - return -1; - } -} - -/////////////////////////////////////////////////MP4FileIO///////////////////////////////////////////////// - static struct mov_buffer_t s_io = { [](void *ctx, void *data, uint64_t bytes) { MP4FileIO *thiz = (MP4FileIO *) ctx; diff --git a/src/Record/MP4.h b/src/Record/MP4.h index a17619c1..16e91948 100644 --- a/src/Record/MP4.h +++ b/src/Record/MP4.h @@ -10,9 +10,12 @@ #ifndef ZLMEDIAKIT_MP4_H #define ZLMEDIAKIT_MP4_H + #ifdef ENABLE_MP4 + #include #include +#include "mp4-writer.h" #include "mov-writer.h" #include "mov-reader.h" #include "mpeg4-hevc.h" @@ -20,20 +23,10 @@ #include "mpeg4-aac.h" #include "mov-buffer.h" #include "mov-format.h" -using namespace std; -namespace mediakit { -//以下是fmp4/mov的通用接口,简单包装了ireader/media-server的接口 -typedef struct mp4_writer_t mp4_writer_t; -mp4_writer_t* mp4_writer_create(int is_fmp4, const struct mov_buffer_t *buffer, void* param, int flags); -void mp4_writer_destroy(mp4_writer_t* mp4); -int mp4_writer_add_audio(mp4_writer_t* mp4, uint8_t object, int channel_count, int bits_per_sample, int sample_rate, const void* extra_data, size_t extra_data_size); -int mp4_writer_add_video(mp4_writer_t* mp4, uint8_t object, int width, int height, const void* extra_data, size_t extra_data_size); -int mp4_writer_add_subtitle(mp4_writer_t* mp4, uint8_t object, const void* extra_data, size_t extra_data_size); -int mp4_writer_write(mp4_writer_t* mp4, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags); -int mp4_writer_write_l(mp4_writer_t* mp4, int track, const void* data, size_t bytes, int64_t pts, int64_t dts, int flags, int add_nalu_size); -int mp4_writer_save_segment(mp4_writer_t* mp4); -int mp4_writer_init_segment(mp4_writer_t* mp4); +using namespace std; + +namespace mediakit { //mp4文件IO的抽象接口类 class MP4FileIO : public std::enable_shared_from_this { diff --git a/src/Record/MPEG.cpp b/src/Record/MPEG.cpp index 6f8eb85e..f9c10157 100644 --- a/src/Record/MPEG.cpp +++ b/src/Record/MPEG.cpp @@ -9,121 +9,14 @@ */ #include -#include #include "MPEG.h" #if defined(ENABLE_HLS) || defined(ENABLE_RTPPROXY) -#include "mpeg-ps.h" -#include "mpeg-ts.h" -#include "mpeg-ts-proto.h" +#include "mpeg-muxer.h" namespace mediakit{ -struct mpeg_muxer_t { - int is_ps; - union { - struct { - void *ctx; - void *param; - mpeg_muxer_func_t func; - } ts; - ps_muxer_t *ps; - } u; -}; - -static void *on_mpeg_ts_alloc(void *param, size_t bytes) { - mpeg_muxer_t *mpeg = (mpeg_muxer_t *) param; - return mpeg->u.ts.func.alloc(mpeg->u.ts.param, bytes); -} - -static void on_mpeg_ts_free(void *param, void *packet) { - mpeg_muxer_t *mpeg = (mpeg_muxer_t *) param; - mpeg->u.ts.func.free(mpeg->u.ts.param, packet); -} - -static int on_mpeg_ts_write(void *param, const void *packet, size_t bytes) { - mpeg_muxer_t *mpeg = (mpeg_muxer_t *) param; - return mpeg->u.ts.func.write(mpeg->u.ts.param, 0, (void *) packet, bytes); -} - -mpeg_muxer_t *mpeg_muxer_create(int is_ps, const mpeg_muxer_func_t *func, void *param) { - mpeg_muxer_t *mpeg = (mpeg_muxer_t *) malloc(sizeof(mpeg_muxer_t)); - assert(mpeg); - mpeg->is_ps = is_ps; - if (is_ps) { - mpeg->u.ps = ps_muxer_create(func, param); - } else { - struct mpeg_ts_func_t ts_func{on_mpeg_ts_alloc, on_mpeg_ts_free, on_mpeg_ts_write}; - mpeg->u.ts.func = *func; - mpeg->u.ts.param = param; - mpeg->u.ts.ctx = mpeg_ts_create(&ts_func, mpeg); - } - return mpeg; -} - -int mpeg_muxer_destroy(mpeg_muxer_t *muxer) { - assert(muxer); - int ret = -1; - if (muxer->is_ps) { - ret = ps_muxer_destroy(muxer->u.ps); - } else { - ret = mpeg_ts_destroy(muxer->u.ts.ctx); - } - free(muxer); - return ret; -} - -int mpeg_muxer_add_stream(mpeg_muxer_t *muxer, int codecid, const void *extradata, size_t extradata_size) { - assert(muxer); - if (muxer->is_ps) { - return ps_muxer_add_stream(muxer->u.ps, codecid, extradata, extradata_size); - } - return mpeg_ts_add_stream(muxer->u.ts.ctx, codecid, extradata, extradata_size); -} - -int mpeg_muxer_input(mpeg_muxer_t *muxer, int stream, int flags, int64_t pts, int64_t dts, const void *data, size_t bytes) { - assert(muxer); - if (muxer->is_ps) { - return ps_muxer_input(muxer->u.ps, stream, flags, pts, dts, data, bytes); - } - return mpeg_ts_write(muxer->u.ts.ctx, stream, flags, pts, dts, data, bytes); -} - -int mpeg_muxer_reset(mpeg_muxer_t *muxer) { - assert(muxer); - if (muxer->is_ps) { - return -1; - } - return mpeg_ts_reset(muxer->u.ts.ctx); -} - -int mpeg_muxer_add_program(mpeg_muxer_t *muxer, uint16_t pn, const void *info, int bytes) { - assert(muxer); - if (muxer->is_ps) { - return -1; - } - return mpeg_ts_add_program(muxer->u.ts.ctx, pn, info, bytes); -} - -int mpeg_muxer_remove_program(mpeg_muxer_t *muxer, uint16_t pn) { - assert(muxer); - if (muxer->is_ps) { - return -1; - } - return mpeg_ts_remove_program(muxer->u.ts.ctx, pn); -} - -int mpeg_muxer_add_program_stream(mpeg_muxer_t *muxer, uint16_t pn, int codecid, const void *extra_data, size_t extra_data_size) { - assert(muxer); - if (muxer->is_ps) { - return -1; - } - return mpeg_ts_add_program_stream(muxer->u.ts.ctx, pn, codecid, extra_data, extra_data_size); -} - -////////////////////////////////////////////////////////////////////////////////////////// - MpegMuxer::MpegMuxer(bool is_ps) { _is_ps = is_ps; _buffer = BufferRaw::create(); diff --git a/src/Record/MPEG.h b/src/Record/MPEG.h index 1f99ae67..4b96a800 100644 --- a/src/Record/MPEG.h +++ b/src/Record/MPEG.h @@ -16,7 +16,6 @@ #include #include #include -#include "mpeg-ps.h" #include "Extension/Frame.h" #include "Extension/Track.h" #include "Util/File.h" @@ -25,20 +24,6 @@ namespace mediakit { -typedef struct mpeg_muxer_t mpeg_muxer_t; -typedef struct ps_muxer_func_t mpeg_muxer_func_t; - -mpeg_muxer_t *mpeg_muxer_create(int is_ps, const mpeg_muxer_func_t *func, void *param); -int mpeg_muxer_destroy(mpeg_muxer_t *muxer); -int mpeg_muxer_add_stream(mpeg_muxer_t *muxer, int codecid, const void *extradata, size_t extradata_size); -int mpeg_muxer_input(mpeg_muxer_t *muxer, int stream, int flags, int64_t pts, int64_t dts, const void *data, size_t bytes); -int mpeg_muxer_reset(mpeg_muxer_t *muxer); -int mpeg_muxer_add_program(mpeg_muxer_t *muxer, uint16_t pn, const void *info, int bytes); -int mpeg_muxer_remove_program(mpeg_muxer_t *muxer, uint16_t pn); -int mpeg_muxer_add_program_stream(mpeg_muxer_t *muxer, uint16_t pn, int codecid, const void *extra_data, size_t extra_data_size); - -/////////////////////////////////////////////////////////////// - //该类用于产生MPEG-TS/MPEG-PS class MpegMuxer : public MediaSinkInterface { public: @@ -80,7 +65,7 @@ private: bool _have_video = false; bool _key_pos = false; uint32_t _timestamp = 0; - mpeg_muxer_t *_context = nullptr; + struct mpeg_muxer_t *_context = nullptr; BufferRaw::Ptr _buffer; unordered_map _codec_to_trackid; FrameMerger _frame_merger{FrameMerger::h264_prefix};