From 7cfcd56f8754df64a36f81d0fb1df119abb5ed8a Mon Sep 17 00:00:00 2001 From: Fancy code <258828110.@qq.com> Date: Tue, 23 Jul 2024 14:48:26 +0800 Subject: [PATCH] Backup. --- .../MissionManager/src/TestMissionState.cpp | 2 +- utils/MediaBase/src/FfmpegOutputStream.cpp | 4 +- utils/MediaBase/src/FfmpegThumbnail.cpp | 53 +++++++++++++------ 3 files changed, 41 insertions(+), 18 deletions(-) diff --git a/application/MissionManager/src/TestMissionState.cpp b/application/MissionManager/src/TestMissionState.cpp index 3944399..0348b58 100644 --- a/application/MissionManager/src/TestMissionState.cpp +++ b/application/MissionManager/src/TestMissionState.cpp @@ -89,7 +89,7 @@ bool TestMissionState::HoldDownResetKey(const KeyEventData &data) constexpr int CLOSE_WATCH_DOG_PRESSING_TIME_MS = 1000 * 3; if (mFormatKeyHoldTime > data.mHoldTime && data.mHoldTime >= CLOSE_WATCH_DOG_PRESSING_TIME_MS && mResetKeyHoldTime == 0) { - LogInfo("Close watch dog.\n"); + LogWarning("Close watch dog.\n"); constexpr int KEEP_BLINKING_FAST_MS = 1000 * 3; LedsHandle::ControlDeviceStatusLed(DeviceStatus::CLOSE_WATCHDOG, KEEP_BLINKING_FAST_MS, BLINKING_FAST_MS); CloseWatchDog(); diff --git a/utils/MediaBase/src/FfmpegOutputStream.cpp b/utils/MediaBase/src/FfmpegOutputStream.cpp index 3df3b78..06fd463 100644 --- a/utils/MediaBase/src/FfmpegOutputStream.cpp +++ b/utils/MediaBase/src/FfmpegOutputStream.cpp @@ -155,12 +155,12 @@ bool FfmpegOutputStream::CheckStreamHeader(const void *data, const size_t &size) if ((0x00 == pData[i]) && (0x00 == pData[i + 1]) && (0x00 == pData[i + 2]) && (0x01 == pData[i + 3]) && (0x5 == (pData[i + 4] & 0x1F))) { uint8_t *extradata = (uint8_t *)av_mallocz(i + 1); - if (!extradata) { + mH264Data2Jpeg = (char *)malloc(size + 1); + if (!extradata && !mH264Data2Jpeg) { LogError("Could not allocate extradata\n"); return false; } LogInfo("Found extradata\n"); - mH264Data2Jpeg = (char *)malloc(size + 1); memcpy(mH264Data2Jpeg, data, size); /** * @brief Find the first I-frame and decode it ->encode it into a JPEG image file. diff --git a/utils/MediaBase/src/FfmpegThumbnail.cpp b/utils/MediaBase/src/FfmpegThumbnail.cpp index a4f32cc..9a7bd1a 100644 --- a/utils/MediaBase/src/FfmpegThumbnail.cpp +++ b/utils/MediaBase/src/FfmpegThumbnail.cpp @@ -58,7 +58,13 @@ void FfmpegThumbnail::Init(const ThumbnailInfo &thumbnailInfo) mTargetWidth = thumbnailInfo.mTargetWidth; mTargetHeight = thumbnailInfo.mTargetHeight; mDecoder = std::make_shared(mDecodecId, mSrouceWidth, mSrouceHeight); + if (!mDecoder) { + LogError("mDecoder = nullptr.\n"); + } mEncoder = std::make_shared(mEncodecId, mTargetWidth, mTargetHeight); + if (!mEncoder) { + LogError("mEncoder = nullptr.\n"); + } } void FfmpegThumbnail::UnInit(void) { @@ -130,6 +136,10 @@ void FfmpegThumbnail::GetDecodeDataCallback(AVFrame *frame) { LogInfo("Decode frame->width = %d, frame->height=%d\n", frame->width, frame->height); AVFrame *thumbnailFrame = av_frame_alloc(); + if (!thumbnailFrame) { + LogError("thumbnailFrame = nullptr.\n"); + return; + } thumbnailFrame->format = AV_PIX_FMT_YUV420P; thumbnailFrame->width = mTargetWidth; thumbnailFrame->height = mTargetHeight; @@ -137,6 +147,10 @@ void FfmpegThumbnail::GetDecodeDataCallback(AVFrame *frame) int jpegBufSize = av_image_get_buffer_size(AV_PIX_FMT_YUV420P, mSrouceWidth, mSrouceHeight, 1); LogInfo("jpegBufSize: %d\n", jpegBufSize); uint8_t *jpegBuf = (uint8_t *)av_malloc(jpegBufSize); + if (!jpegBuf) { + LogError("jpegBuf = nullptr.\n"); + goto END; + } av_image_fill_arrays( thumbnailFrame->data, thumbnailFrame->linesize, jpegBuf, AV_PIX_FMT_YUV420P, frame->width, frame->height, 1); @@ -150,14 +164,25 @@ void FfmpegThumbnail::GetDecodeDataCallback(AVFrame *frame) nullptr, nullptr, nullptr); + if (!mSwsCtx) { + LogError("mSwsCtx = nullptr.\n"); + goto END; + } + system("echo 1 > /proc/sys/vm/drop_caches"); // Perform pixel format conversion. sws_scale(mSwsCtx, frame->data, frame->linesize, 0, frame->height, thumbnailFrame->data, thumbnailFrame->linesize); + return; if (mEncoder) { mEncoder->EncodeData(thumbnailFrame, mStream, mEncodeCallback); } - av_frame_free(&thumbnailFrame); - av_free(jpegBuf); +END: + if (thumbnailFrame) { + av_frame_free(&thumbnailFrame); + } + if (jpegBuf) { + av_free(jpegBuf); + } return; } void FfmpegThumbnail::GetEncodeDataCallback(AVPacket *pkt, const std::string &fileName) @@ -167,21 +192,19 @@ void FfmpegThumbnail::GetEncodeDataCallback(AVPacket *pkt, const std::string &fi bool FfmpegThumbnail::SaveThumbnailFile(const std::string &fileName, const void *data, const size_t &size) { FILE *file = nullptr; - LogInfo("SaveThumbnailFile:%s\n", fileName.c_str()); - file = fopen(fileName.c_str(), "a+"); - - if (file) { - fwrite(data, 1, size, file); - fflush(file); - } - else { + if (!data) { + LogError("SaveThumbnailFile:%s failed, data is nullptr.\n", fileName.c_str()); return false; } - - if (file) { - fclose(file); - file = nullptr; + LogInfo("SaveThumbnailFile:%s, size = %u\n", fileName.c_str(), size); + file = fopen(fileName.c_str(), "a+"); + if (!file) { + LogError("fopen failed.\n"); + return false; } - fx_system_v2("sync"); + fwrite(data, 1, size, file); + fflush(file); + fclose(file); + // system("sync"); return true; } \ No newline at end of file