From 6b1b9f3e35bfba56b30f8cccc43d5c1135a35413 Mon Sep 17 00:00:00 2001 From: baiyfcu Date: Wed, 3 Apr 2024 14:46:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0track=E5=9C=A8=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=90=8Einput=20frame=E5=89=8D=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8Dtrack=E8=AE=A2=E9=98=85=E9=94=99?= =?UTF-8?q?=E8=BF=87=E9=85=8D=E7=BD=AE=E5=B8=A7=E6=95=B0=E6=8D=AE=EF=BC=8C?= =?UTF-8?q?MP4Demuxer=E6=8E=A5=E5=8F=A3=E6=B7=BB=E5=8A=A0virtual=E5=8F=AF?= =?UTF-8?q?=E6=B4=BE=E7=94=9F=E6=89=A9=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Demuxer.cpp | 9 ++++++++- src/Record/MP4Demuxer.h | 17 ++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp index 4d6800d2..24657b99 100644 --- a/src/Record/MP4Demuxer.cpp +++ b/src/Record/MP4Demuxer.cpp @@ -63,6 +63,8 @@ void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int hei } video->setIndex(track); _tracks.emplace(track, video); + if (_on_track_callback) + _on_track_callback(video); if (extra && bytes) { video->setExtraData((uint8_t *)extra, bytes); } @@ -75,6 +77,8 @@ void MP4Demuxer::onAudioTrack(uint32_t track, uint8_t object, int channel_count, } audio->setIndex(track); _tracks.emplace(track, audio); + if (_on_track_callback) + _on_track_callback(audio); if (extra && bytes) { audio->setExtraData((uint8_t *)extra, bytes); } @@ -100,7 +104,10 @@ struct Context { Frame::Ptr MP4Demuxer::readFrame(bool &keyFrame, bool &eof) { keyFrame = false; eof = false; - + if (!_mov_reader) { + eof = true; + return nullptr; + } static mov_reader_onread2 mov_onalloc = [](void *param, uint32_t track_id, size_t bytes, int64_t pts, int64_t dts, int flags) -> void * { Context *ctx = (Context *) param; ctx->pts = pts; diff --git a/src/Record/MP4Demuxer.h b/src/Record/MP4Demuxer.h index 889f91fc..c8b34c7b 100644 --- a/src/Record/MP4Demuxer.h +++ b/src/Record/MP4Demuxer.h @@ -20,25 +20,25 @@ class MP4Demuxer : public TrackSource { public: using Ptr = std::shared_ptr; - ~MP4Demuxer() override; + virtual ~MP4Demuxer() override; /** * 打开文件 * @param file mp4文件路径 */ - void openMP4(const std::string &file); + virtual void openMP4(const std::string &file); /** * @brief 关闭 mp4 文件 */ - void closeMP4(); + virtual void closeMP4(); /** * 移动时间轴至某处 * @param stamp_ms 预期的时间轴位置,单位毫秒 * @return 时间轴位置 */ - int64_t seekTo(int64_t stamp_ms); + virtual int64_t seekTo(int64_t stamp_ms); /** * 读取一帧数据 @@ -46,20 +46,22 @@ public: * @param eof 是否文件读取完毕 * @return 帧数据,可能为空 */ - Frame::Ptr readFrame(bool &keyFrame, bool &eof); + virtual Frame::Ptr readFrame(bool &keyFrame, bool &eof); /** * 获取所有Track信息 * @param trackReady 是否要求track为就绪状态 * @return 所有Track */ - std::vector getTracks(bool trackReady) const override; + virtual std::vector getTracks(bool trackReady) const override; /** * 获取文件长度 * @return 文件长度,单位毫秒 */ - uint64_t getDurationMS() const; + virtual uint64_t getDurationMS() const; + + virtual void setOnTrack(const std::function &callback) { _on_track_callback = callback; } private: int getAllTracks(); @@ -73,6 +75,7 @@ private: uint64_t _duration_ms = 0; std::unordered_map _tracks; toolkit::ResourcePool _buffer_pool; + std::function _on_track_callback; };