From 095834fe57493c7f711b3748685a91b2a64ab248 Mon Sep 17 00:00:00 2001 From: xiongziliang <771730766@qq.com> Date: Fri, 3 Apr 2020 20:46:55 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99MP4=E7=82=B9=E6=92=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Record/MP4.cpp | 140 ++++++++++++++++++++ src/Record/MP4.h | 65 +++++++++ src/Record/MP4Demuxer.cpp | 269 ++++++++++++++++++++++++++++++++++++++ src/Record/MP4Demuxer.h | 59 +++++++++ 4 files changed, 533 insertions(+) create mode 100644 src/Record/MP4.cpp create mode 100644 src/Record/MP4.h create mode 100644 src/Record/MP4Demuxer.cpp create mode 100644 src/Record/MP4Demuxer.h diff --git a/src/Record/MP4.cpp b/src/Record/MP4.cpp new file mode 100644 index 00000000..5b42d9c1 --- /dev/null +++ b/src/Record/MP4.cpp @@ -0,0 +1,140 @@ +/* + * MIT License + * + * Copyright (c) 2016-2020 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifdef ENABLE_MP4 +#include "MP4.h" +#include "Util/File.h" +#include "Util/logger.h" +#include "Common/config.h" +using namespace toolkit; +namespace mediakit { + +static struct mov_buffer_t s_io = { + [](void* ctx, void* data, uint64_t bytes) { + MP4File *thiz = (MP4File *)ctx; + return thiz->onRead(data,bytes); + }, + [](void* ctx, const void* data, uint64_t bytes){ + MP4File *thiz = (MP4File *)ctx; + return thiz->onWrite(data,bytes); + }, + [](void* ctx, uint64_t offset) { + MP4File *thiz = (MP4File *)ctx; + return thiz->onSeek(offset); + }, + [](void* ctx){ + MP4File *thiz = (MP4File *)ctx; + return thiz->onTell(); + } +}; + +MP4File::Writer MP4File::createWriter(){ + GET_CONFIG(bool, mp4FastStart, Record::kFastStart); + Writer writer; + writer.reset(mov_writer_create(&s_io,this,mp4FastStart ? MOV_FLAG_FASTSTART : 0),[](mov_writer_t *ptr){ + if(ptr){ + mov_writer_destroy(ptr); + } + }); + if(!writer){ + throw std::runtime_error("写入mp4文件失败!"); + } + return writer; +} + +MP4File::Reader MP4File::createReader(){ + Reader reader; + reader.reset(mov_reader_create(&s_io,this),[](mov_reader_t *ptr){ + if(ptr){ + mov_reader_destroy(ptr); + } + }); + if(!reader){ + throw std::runtime_error("读取mp4文件失败!"); + } + return reader; +} + +#if defined(_WIN32) || defined(_WIN64) + #define fseek64 _fseeki64 +#define ftell64 _ftelli64 +#else +#define fseek64 fseek +#define ftell64 ftell +#endif + +void MP4File::openFile(const char *file,const char *mode) { + //创建文件 + auto fp = File::createfile_file(file,mode); + if(!fp){ + throw std::runtime_error(string("打开文件失败:") + file); + } + + GET_CONFIG(uint32_t,mp4BufSize,Record::kFileBufSize); + + //新建文件io缓存 + std::shared_ptr file_buf(new char[mp4BufSize],[](char *ptr){ + if(ptr){ + delete [] ptr; + } + }); + + if(file_buf){ + //设置文件io缓存 + setvbuf(fp, file_buf.get(), _IOFBF, mp4BufSize); + } + + //创建智能指针 + _file.reset(fp,[file_buf](FILE *fp) { + fclose(fp); + }); +} + +void MP4File::closeFile() { + _file = nullptr; +} + +int MP4File::onRead(void *data, uint64_t bytes) { + if (bytes == fread(data, 1, bytes, _file.get())){ + return 0; + } + return 0 != ferror(_file.get()) ? ferror(_file.get()) : -1 /*EOF*/; +} + +int MP4File::onWrite(const void *data, uint64_t bytes) { + return bytes == fwrite(data, 1, bytes, _file.get()) ? 0 : ferror(_file.get()); +} + +int MP4File::onSeek(uint64_t offset) { + return fseek64(_file.get(), offset, SEEK_SET); +} + +uint64_t MP4File::onTell() { + return ftell64(_file.get()); +} + +}//namespace mediakit +#endif //NABLE_MP4RECORD diff --git a/src/Record/MP4.h b/src/Record/MP4.h new file mode 100644 index 00000000..2af03b94 --- /dev/null +++ b/src/Record/MP4.h @@ -0,0 +1,65 @@ +/* + * MIT License + * + * Copyright (c) 2016-2020 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef ZLMEDIAKIT_MP4_H +#define ZLMEDIAKIT_MP4_H +#ifdef ENABLE_MP4 +#include +#include +#include "mov-writer.h" +#include "mov-reader.h" +#include "mpeg4-hevc.h" +#include "mpeg4-avc.h" +#include "mpeg4-aac.h" +#include "mov-buffer.h" +#include "mov-format.h" +using namespace std; +namespace mediakit { + +class MP4File { +public: + friend struct mov_buffer_t; + typedef std::shared_ptr Writer; + typedef std::shared_ptr Reader; + MP4File() = default; + virtual ~MP4File() = default; + + Writer createWriter(); + Reader createReader(); + void openFile(const char *file,const char *mode); + void closeFile(); + + int onRead(void* data, uint64_t bytes); + int onWrite(const void* data, uint64_t bytes); + int onSeek( uint64_t offset); + uint64_t onTell(); +private: + std::shared_ptr _file; +}; + +}//namespace mediakit +#endif //NABLE_MP4RECORD +#endif //ZLMEDIAKIT_MP4_H diff --git a/src/Record/MP4Demuxer.cpp b/src/Record/MP4Demuxer.cpp new file mode 100644 index 00000000..352ff876 --- /dev/null +++ b/src/Record/MP4Demuxer.cpp @@ -0,0 +1,269 @@ +/* + * MIT License + * + * Copyright (c) 2016-2020 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifdef ENABLE_MP4 +#include "MP4Demuxer.h" +#include "Util/logger.h" +#include "Extension/H265.h" +#include "Extension/H264.h" +#include "Extension/AAC.h" +using namespace toolkit; +namespace mediakit { + +MP4Demuxer::MP4Demuxer(const char *file) { + openFile(file,"rb+"); + _mov_reader = createReader(); + getAllTracks(); + _duration_ms = mov_reader_getduration(_mov_reader.get()); +} + +MP4Demuxer::~MP4Demuxer() { + _mov_reader = nullptr; + closeFile(); +} + +int MP4Demuxer::getAllTracks() { + static mov_reader_trackinfo_t s_on_track = { + [](void *param, uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) { + //onvideo + MP4Demuxer *thiz = (MP4Demuxer *)param; + thiz->onVideoTrack(track,object,width,height,extra,bytes); + }, + [](void *param, uint32_t track, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) { + //onaudio + MP4Demuxer *thiz = (MP4Demuxer *)param; + thiz->onAudioTrack(track,object,channel_count,bit_per_sample,sample_rate,extra,bytes); + }, + [](void *param, uint32_t track, uint8_t object, const void *extra, size_t bytes) { + //onsubtitle, do nothing + } + }; + return mov_reader_getinfo(_mov_reader.get(),&s_on_track,this); +} + +#define SWITCH_CASE(obj_id) case obj_id : return #obj_id +static const char *getObjectName(int obj_id) { + switch (obj_id) { + SWITCH_CASE(MOV_OBJECT_TEXT); + SWITCH_CASE(MOV_OBJECT_MP4V); + SWITCH_CASE(MOV_OBJECT_H264); + SWITCH_CASE(MOV_OBJECT_HEVC); + SWITCH_CASE(MOV_OBJECT_AAC); + SWITCH_CASE(MOV_OBJECT_MP2V); + SWITCH_CASE(MOV_OBJECT_AAC_MAIN); + SWITCH_CASE(MOV_OBJECT_AAC_LOW); + SWITCH_CASE(MOV_OBJECT_AAC_SSR); + SWITCH_CASE(MOV_OBJECT_MP3); + SWITCH_CASE(MOV_OBJECT_MP1V); + SWITCH_CASE(MOV_OBJECT_MP1A); + SWITCH_CASE(MOV_OBJECT_JPEG); + SWITCH_CASE(MOV_OBJECT_PNG); + SWITCH_CASE(MOV_OBJECT_JPEG2000); + SWITCH_CASE(MOV_OBJECT_G719); + SWITCH_CASE(MOV_OBJECT_OPUS); + SWITCH_CASE(MOV_OBJECT_G711a); + SWITCH_CASE(MOV_OBJECT_G711u); + SWITCH_CASE(MOV_OBJECT_AV1); + default: + return "unknown mp4 object"; + } +} + + +void MP4Demuxer::onVideoTrack(uint32_t track, uint8_t object, int width, int height, const void *extra, size_t bytes) { + switch (object) { + case MOV_OBJECT_H264: { + auto video = std::make_shared(); + _track_to_codec.emplace(track,video); + + struct mpeg4_avc_t avc = {0}; + if (mpeg4_avc_decoder_configuration_record_load((uint8_t *) extra, bytes, &avc) > 0) { + uint8_t config[1024] = {0}; + int size = mpeg4_avc_to_nalu(&avc, config, sizeof(config)); + if (size > 0) { + video->inputFrame(std::make_shared((char *)config, size, 0, 0)); + } + } + } + break; + case MOV_OBJECT_HEVC: { + auto video = std::make_shared(); + _track_to_codec.emplace(track,video); + + struct mpeg4_hevc_t hevc = {0}; + if (mpeg4_hevc_decoder_configuration_record_load((uint8_t *) extra, bytes, &hevc) > 0) { + uint8_t config[1024] = {0}; + int size = mpeg4_hevc_to_nalu(&hevc, config, sizeof(config)); + if (size > 0) { + video->inputFrame(std::make_shared((char *) config, size, 0, 0)); + } + } + } + break; + default: + WarnL << "不支持该编码类型的MP4,已忽略:" << getObjectName(object); + break; + } +} + +void MP4Demuxer::onAudioTrack(uint32_t track_id, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void *extra, size_t bytes) { + switch(object){ + case MOV_OBJECT_AAC:{ + auto audio = std::make_shared(bytes > 0 ? string((char *)extra,bytes) : ""); + _track_to_codec.emplace(track_id, audio); + } + break; + default: + WarnL << "不支持该编码类型的MP4,已忽略:" << getObjectName(object); + break; + } +} + +int64_t MP4Demuxer::seekTo(int64_t stamp_ms) { + if(0 != mov_reader_seek(_mov_reader.get(),&stamp_ms)){ + return -1; + } + return stamp_ms; +} + +struct Context{ + MP4Demuxer *thiz; + int flags; + int64_t pts; + int64_t dts; + uint32_t track_id; + BufferRaw::Ptr buffer; +}; + +Frame::Ptr MP4Demuxer::readFrame(bool seekKeyFrame, bool *eof) { + static mov_reader_onread mov_reader_onread = [](void *param, uint32_t track_id, const void *buffer, size_t bytes, int64_t pts, int64_t dts, int flags) { + Context *ctx = (Context *) param; + ctx->pts = pts; + ctx->dts = dts; + ctx->flags = flags; + ctx->track_id = track_id; + }; + + static mov_onalloc mov_onalloc = [](void *param, int bytes) -> void * { + Context *ctx = (Context *) param; + ctx->buffer = ctx->thiz->_buffer_pool.obtain(); + ctx->buffer->setCapacity(bytes + 1); + ctx->buffer->setSize(bytes); + return ctx->buffer->data(); + }; + + Context ctx = {this, 0}; + auto ret = mov_reader_read2(_mov_reader.get(), mov_onalloc, mov_reader_onread, &ctx); + switch (ret) { + case 0 : { + if(eof){ + *eof = true; + } + WarnL << "读取mp4文件完毕"; + } + break; + + case 1 : { + if (seekKeyFrame && !(ctx.flags & MOV_AV_FLAG_KEYFREAME)) { + //请求key帧,但是这个帧不是 + return nullptr; + } + return makeFrame(ctx.track_id,ctx.buffer, ctx.pts, ctx.dts); + } + break; + + default: + WarnL << "读取mp4文件数据失败:" << ret; + break; + } + return nullptr; +} + +template +class FrameWrapper : public Parent{ +public: + ~FrameWrapper() = default; + FrameWrapper(const Buffer::Ptr &buf, int64_t pts, int64_t dts, int prefix) : Parent(buf->data(), buf->size(), dts, pts, prefix){ + _buf = buf; + } + bool cacheAble() const override { + return true; + } +private: + Buffer::Ptr _buf; +}; + +Frame::Ptr MP4Demuxer::makeFrame(uint32_t track_id, const Buffer::Ptr &buf, int64_t pts, int64_t dts) { + auto it = _track_to_codec.find(track_id); + if (it == _track_to_codec.end()) { + return nullptr; + } + auto numBytes = buf->size(); + auto pBytes = buf->data(); + auto codec = it->second->getCodecId(); + switch (codec) { + case CodecH264 : + case CodecH265 : { + uint32_t iOffset = 0; + while (iOffset < numBytes) { + uint32_t iFrameLen; + memcpy(&iFrameLen, pBytes + iOffset, 4); + iFrameLen = ntohl(iFrameLen); + if (iFrameLen + iOffset + 4 > numBytes) { + return nullptr; + } + memcpy(pBytes + iOffset, "\x0\x0\x0\x1", 4); + iOffset += (iFrameLen + 4); + } + if (codec == CodecH264) { + return std::make_shared >(buf, pts, dts,4); + } + return std::make_shared >(buf, pts, dts,4); + } + case CodecAAC : + return std::make_shared > (buf, pts, dts, 0); + default: + return nullptr; + } +} + +vector MP4Demuxer::getTracks(bool trackReady) const { + vector ret; + for (auto &pr : _track_to_codec) { + if(trackReady && !pr.second->ready()){ + continue; + } + ret.push_back(pr.second); + } + return std::move(ret); +} + +uint64_t MP4Demuxer::getDurationMS() const { + return _duration_ms; +} + +}//namespace mediakit +#endif// ENABLE_MP4 \ No newline at end of file diff --git a/src/Record/MP4Demuxer.h b/src/Record/MP4Demuxer.h new file mode 100644 index 00000000..e9159142 --- /dev/null +++ b/src/Record/MP4Demuxer.h @@ -0,0 +1,59 @@ +/* + * MIT License + * + * Copyright (c) 2016-2020 xiongziliang <771730766@qq.com> + * + * This file is part of ZLMediaKit(https://github.com/xiongziliang/ZLMediaKit). + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef ZLMEDIAKIT_MP4DEMUXER_H +#define ZLMEDIAKIT_MP4DEMUXER_H +#ifdef ENABLE_MP4 +#include "MP4.h" +#include "Extension/Track.h" +#include "Util/ResourcePool.h" +namespace mediakit { + +class MP4Demuxer : public MP4File, public TrackSource{ +public: + typedef std::shared_ptr Ptr; + MP4Demuxer(const char *file); + ~MP4Demuxer() override; + int64_t seekTo(int64_t stamp_ms); + Frame::Ptr readFrame(bool seekKeyFrame = false, bool *eof = nullptr); + vector getTracks(bool trackReady) const override ; + uint64_t getDurationMS() const; +private: + int getAllTracks(); + void onVideoTrack(uint32_t track_id, uint8_t object, int width, int height, const void* extra, size_t bytes); + void onAudioTrack(uint32_t track_id, uint8_t object, int channel_count, int bit_per_sample, int sample_rate, const void* extra, size_t bytes); + Frame::Ptr makeFrame(uint32_t track_id, const Buffer::Ptr &buf,int64_t pts, int64_t dts); +private: + MP4File::Reader _mov_reader; + uint64_t _duration_ms = 0; + map _track_to_codec; + ResourcePool _buffer_pool; +}; + + +}//namespace mediakit +#endif//ENABLE_MP4 +#endif //ZLMEDIAKIT_MP4DEMUXER_H