From f4b6a6bd40a43e3852b0280d3271fb9011c02554 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 16 Dec 2021 10:50:31 +0800 Subject: [PATCH 1/3] =?UTF-8?q?MP4:=20=E4=BF=AE=E5=A4=8Dmp4=E8=A7=A3?= =?UTF-8?q?=E5=A4=8D=E7=94=A8=E5=AE=9A=E6=97=B6=E5=99=A8=E7=B2=BE=E5=BA=A6?= =?UTF-8?q?=E5=A4=AA=E9=AB=98=E6=97=B6=E8=AF=BB=E5=8F=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=AA=E5=BF=AB=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Reader.cpp | 7 +++---- src/Record/MP4Reader.h | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Record/MP4Reader.cpp b/src/Record/MP4Reader.cpp index b5d43aee..a69ab28f 100644 --- a/src/Record/MP4Reader.cpp +++ b/src/Record/MP4Reader.cpp @@ -59,17 +59,15 @@ bool MP4Reader::readSample() { bool keyFrame = false; bool eof = false; - while (!eof) { + while (!eof && _last_dts < getCurrentStamp()) { auto frame = _demuxer->readFrame(keyFrame, eof); if (!frame) { continue; } + _last_dts = frame->dts(); if (_muxer) { _muxer->inputFrame(frame); } - if (frame->dts() > getCurrentStamp()) { - break; - } } GET_CONFIG(bool, fileRepeat, Record::kFileRepeat); @@ -129,6 +127,7 @@ uint32_t MP4Reader::getCurrentStamp() { void MP4Reader::setCurrentStamp(uint32_t new_stamp){ auto old_stamp = getCurrentStamp(); _seek_to = new_stamp; + _last_dts = new_stamp; _seek_ticker.resetTime(); if (old_stamp != new_stamp && _muxer) { //时间轴未拖动时不操作 diff --git a/src/Record/MP4Reader.h b/src/Record/MP4Reader.h index 4978e78a..29337235 100644 --- a/src/Record/MP4Reader.h +++ b/src/Record/MP4Reader.h @@ -60,6 +60,7 @@ private: bool _have_video = false; bool _paused = false; float _speed = 1.0; + uint32_t _last_dts = 0; uint32_t _seek_to; string _file_path; recursive_mutex _mtx; From 4291c3149353c352998c5dad3b4f8cb10c0d1893 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 16 Dec 2021 12:09:49 +0800 Subject: [PATCH 2/3] =?UTF-8?q?FMP4:=20http-fmp4=E7=9B=B4=E6=92=AD?= =?UTF-8?q?=E6=8F=90=E5=8D=87=E6=B5=81=E7=95=85=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4Muxer.cpp | 9 ++++----- src/Record/MP4Muxer.h | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index f4fda0ea..346d2cc8 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -319,14 +319,13 @@ bool MP4MuxerMemory::inputFrame(const Frame::Ptr &frame){ return false; } - bool key_frame = frame->keyFrame(); - if (_ticker.elapsedTime() > 50 || key_frame) { - //遇到关键帧或者超过50ms则切片 - _ticker.resetTime(); + auto key_frame = frame->keyFrame(); + auto data = _memory_file->getAndClearMemory(); + if (!data.empty()) { //flush切片 saveSegment(); //输出切片数据 - onSegmentData(_memory_file->getAndClearMemory(), frame->dts(), _key_frame); + onSegmentData(data, frame->dts(), _key_frame); _key_frame = false; } diff --git a/src/Record/MP4Muxer.h b/src/Record/MP4Muxer.h index 2d4c7a3e..6d51650f 100644 --- a/src/Record/MP4Muxer.h +++ b/src/Record/MP4Muxer.h @@ -141,7 +141,6 @@ protected: private: bool _key_frame = false; - Ticker _ticker; string _init_segment; MP4FileMemory::Ptr _memory_file; }; From 120879df0f20ad80bf1e9a6cf444626dcea17514 Mon Sep 17 00:00:00 2001 From: ziyue <1213642868@qq.com> Date: Thu, 16 Dec 2021 17:46:03 +0800 Subject: [PATCH 3/3] =?UTF-8?q?HTTP-FMP4:=20=E4=BF=AE=E5=A4=8Dhttp-fmp4?= =?UTF-8?q?=E7=9B=B4=E6=92=AD=E7=A7=92=E5=BC=80=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/FMP4/FMP4MediaSource.h | 4 +-- src/FMP4/FMP4MediaSourceMuxer.h | 2 +- src/Record/MP4Muxer.cpp | 55 +++++++++++++++++---------------- src/Record/MP4Muxer.h | 2 +- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/FMP4/FMP4MediaSource.h b/src/FMP4/FMP4MediaSource.h index 87c92831..3a7e66e6 100644 --- a/src/FMP4/FMP4MediaSource.h +++ b/src/FMP4/FMP4MediaSource.h @@ -64,9 +64,7 @@ public: */ void setInitSegment(string str) { _init_segment = std::move(str); - if (_ring) { - regist(); - } + createRing(); } /** diff --git a/src/FMP4/FMP4MediaSourceMuxer.h b/src/FMP4/FMP4MediaSourceMuxer.h index d421437e..7579934f 100644 --- a/src/FMP4/FMP4MediaSourceMuxer.h +++ b/src/FMP4/FMP4MediaSourceMuxer.h @@ -72,7 +72,7 @@ public: } protected: - void onSegmentData(const string &string, uint32_t stamp, bool key_frame) override { + void onSegmentData(string string, uint32_t stamp, bool key_frame) override { if (string.empty()) { return; } diff --git a/src/Record/MP4Muxer.cpp b/src/Record/MP4Muxer.cpp index 346d2cc8..fb54152b 100644 --- a/src/Record/MP4Muxer.cpp +++ b/src/Record/MP4Muxer.cpp @@ -9,10 +9,12 @@ */ #ifdef ENABLE_MP4 + #include "MP4Muxer.h" #include "Util/File.h" #include "Extension/H264.h" -namespace mediakit{ + +namespace mediakit { MP4Muxer::MP4Muxer() {} @@ -20,19 +22,19 @@ MP4Muxer::~MP4Muxer() { closeMP4(); } -void MP4Muxer::openMP4(const string &file){ +void MP4Muxer::openMP4(const string &file) { closeMP4(); _file_name = file; _mp4_file = std::make_shared(); _mp4_file->openFile(_file_name.data(), "wb+"); } -MP4FileIO::Writer MP4Muxer::createWriter(){ +MP4FileIO::Writer MP4Muxer::createWriter() { GET_CONFIG(bool, mp4FastStart, Record::kFastStart); return _mp4_file->createWriter(mp4FastStart ? MOV_FLAG_FASTSTART : 0, false); } -void MP4Muxer::closeMP4(){ +void MP4Muxer::closeMP4() { MP4MuxerInterface::resetTracks(); _mp4_file = nullptr; } @@ -44,15 +46,15 @@ void MP4Muxer::resetTracks() { /////////////////////////////////////////// MP4MuxerInterface ///////////////////////////////////////////// -void MP4MuxerInterface::saveSegment(){ +void MP4MuxerInterface::saveSegment() { mp4_writer_save_segment(_mov_writter.get()); } -void MP4MuxerInterface::initSegment(){ +void MP4MuxerInterface::initSegment() { mp4_writer_init_segment(_mov_writter.get()); } -bool MP4MuxerInterface::haveVideo() const{ +bool MP4MuxerInterface::haveVideo() const { return _have_video; } @@ -66,7 +68,7 @@ void MP4MuxerInterface::resetTracks() { bool MP4MuxerInterface::inputFrame(const Frame::Ptr &frame) { auto it = _codec_to_trackid.find(frame->getCodecId()); - if(it == _codec_to_trackid.end()){ + if (it == _codec_to_trackid.end()) { //该Track不存在或初始化失败 return false; } @@ -84,7 +86,6 @@ bool MP4MuxerInterface::inputFrame(const Frame::Ptr &frame) { //mp4文件时间戳需要从0开始 auto &track_info = it->second; int64_t dts_out, pts_out; - switch (frame->getCodecId()) { case CodecH264: case CodecH265: { @@ -117,7 +118,7 @@ bool MP4MuxerInterface::inputFrame(const Frame::Ptr &frame) { return true; } -static uint8_t getObject(CodecId codecId){ +static uint8_t getObject(CodecId codecId) { switch (codecId){ case CodecG711A : return MOV_OBJECT_G711a; case CodecG711U : return MOV_OBJECT_G711u; @@ -129,8 +130,8 @@ static uint8_t getObject(CodecId codecId){ } } -void MP4MuxerInterface::stampSync(){ - if(_codec_to_trackid.size() < 2){ +void MP4MuxerInterface::stampSync() { + if (_codec_to_trackid.size() < 2) { return; } @@ -143,7 +144,7 @@ void MP4MuxerInterface::stampSync(){ } } - if(audio && video){ + if (audio && video) { //音频时间戳同步于视频,因为音频时间戳被修改后不影响播放 audio->syncTo(*video); } @@ -202,7 +203,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) { audio_track->getAudioSampleRate(), audio_track->getAacCfg().data(), audio_track->getAacCfg().size()); - if(track_id < 0){ + if (track_id < 0) { WarnL << "添加AAC Track失败:" << track_id; return false; } @@ -220,7 +221,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) { struct mpeg4_avc_t avc = {0}; string sps_pps = string("\x00\x00\x00\x01", 4) + h264_track->getSps() + string("\x00\x00\x00\x01", 4) + h264_track->getPps(); - h264_annexbtomp4(&avc, sps_pps.data(), (int)sps_pps.size(), NULL, 0, NULL, NULL); + h264_annexbtomp4(&avc, sps_pps.data(), (int) sps_pps.size(), NULL, 0, NULL, NULL); uint8_t extra_data[1024]; int extra_data_size = mpeg4_avc_decoder_configuration_record_save(&avc, extra_data, sizeof(extra_data)); @@ -236,7 +237,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) { extra_data, extra_data_size); - if(track_id < 0){ + if (track_id < 0) { WarnL << "添加H264 Track失败:" << track_id; return false; } @@ -256,7 +257,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) { string vps_sps_pps = string("\x00\x00\x00\x01", 4) + h265_track->getVps() + string("\x00\x00\x00\x01", 4) + h265_track->getSps() + string("\x00\x00\x00\x01", 4) + h265_track->getPps(); - h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int)vps_sps_pps.size(), NULL, 0, NULL, NULL); + h265_annexbtomp4(&hevc, vps_sps_pps.data(), (int) vps_sps_pps.size(), NULL, 0, NULL, NULL); uint8_t extra_data[1024]; int extra_data_size = mpeg4_hevc_decoder_configuration_record_save(&hevc, extra_data, sizeof(extra_data)); @@ -271,7 +272,7 @@ bool MP4MuxerInterface::addTrack(const Track::Ptr &track) { h265_track->getVideoHeight(), extra_data, extra_data_size); - if(track_id < 0){ + if (track_id < 0) { WarnL << "添加H265 Track失败:" << track_id; return false; } @@ -298,7 +299,7 @@ MP4FileIO::Writer MP4MuxerMemory::createWriter() { return _memory_file->createWriter(MOV_FLAG_SEGMENT, true); } -const string &MP4MuxerMemory::getInitSegment(){ +const string &MP4MuxerMemory::getInitSegment() { if (_init_segment.empty()) { initSegment(); saveSegment(); @@ -307,34 +308,36 @@ const string &MP4MuxerMemory::getInitSegment(){ return _init_segment; } -void MP4MuxerMemory::resetTracks(){ +void MP4MuxerMemory::resetTracks() { MP4MuxerInterface::resetTracks(); _memory_file = std::make_shared(); _init_segment.clear(); } -bool MP4MuxerMemory::inputFrame(const Frame::Ptr &frame){ +bool MP4MuxerMemory::inputFrame(const Frame::Ptr &frame) { if (_init_segment.empty()) { //尚未生成init segment return false; } - auto key_frame = frame->keyFrame(); + bool key_frame = frame->keyFrame(); + + //flush切片 + saveSegment(); + auto data = _memory_file->getAndClearMemory(); if (!data.empty()) { - //flush切片 - saveSegment(); //输出切片数据 - onSegmentData(data, frame->dts(), _key_frame); + onSegmentData(std::move(data), frame->dts(), _key_frame); _key_frame = false; } if (key_frame) { _key_frame = true; } + return MP4MuxerInterface::inputFrame(frame); } - }//namespace mediakit #endif//#ifdef ENABLE_MP4 diff --git a/src/Record/MP4Muxer.h b/src/Record/MP4Muxer.h index 6d51650f..0705b3b8 100644 --- a/src/Record/MP4Muxer.h +++ b/src/Record/MP4Muxer.h @@ -134,7 +134,7 @@ protected: * @param stamp 切片末尾时间戳 * @param key_frame 是否有关键帧 */ - virtual void onSegmentData(const string &string, uint32_t stamp, bool key_frame) = 0; + virtual void onSegmentData(string string, uint32_t stamp, bool key_frame) = 0; protected: MP4FileIO::Writer createWriter() override;