This commit is contained in:
Fancy code 2024-07-23 14:48:26 +08:00
parent 281a1aa60f
commit 7cfcd56f87
3 changed files with 41 additions and 18 deletions

View File

@ -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();

View File

@ -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.

View File

@ -58,7 +58,13 @@ void FfmpegThumbnail::Init(const ThumbnailInfo &thumbnailInfo)
mTargetWidth = thumbnailInfo.mTargetWidth;
mTargetHeight = thumbnailInfo.mTargetHeight;
mDecoder = std::make_shared<FfmpegDecoder>(mDecodecId, mSrouceWidth, mSrouceHeight);
if (!mDecoder) {
LogError("mDecoder = nullptr.\n");
}
mEncoder = std::make_shared<FfmpegEncoder>(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;
}