hunting/application/MissionManager/src/MediaTask.cpp
2024-07-25 19:43:22 +08:00

161 lines
5.7 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "MediaTask.h"
#include "DataProcessing.h"
#include "IIpcConfig.h"
#include "ILog.h"
#include "IMediaManager.h"
#include <cctype>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <sys/stat.h>
#include <vector>
MediaTask::MediaTask(const MediaTaskType &type, const InternalStateEvent &bindEvent,
const std::weak_ptr<VMediaTaskIniator> &iniator, const unsigned long &serialNumber,
const std::string &savePath)
: mType(type), mBindEvent(bindEvent), mIniator(iniator), mSerialNumber(serialNumber), mSavePath(savePath),
mCreateTime_s(0)
{
mResponseData.reset();
mTargetName.clear();
}
unsigned int MediaTask::GetTaskTimeOutMs(void)
{
return MEDIA_TASK_TIMEOUT_MS;
}
const MediaTaskType MediaTask::GetTaskType(void)
{
return mType;
}
std::string MediaTask::GetTargetNameForSaving(void)
{
if (!mTargetName.empty()) {
return mTargetName;
}
auto now = std::chrono::system_clock::now();
mCreateTime_s = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
const std::string fileType = MediaTaskType::TAKE_VIDEO == mType ? ".mp4" : ".jpeg";
return CreateFileName(mSerialNumber, mSavePath, fileType);
}
std::string MediaTask::GetThumbnailNameForSaving(const std::string &targetName)
{
return MediaTask::GetThumbnailNameByTargetName(targetName);
}
int MediaTask::GetVideoDuration_ms(void)
{
return DEFAULT_VIDEO_DURATION_MS;
}
void MediaTask::Response(const std::vector<MediaTaskResponse> &response)
{
LogInfo("Response handle.\n");
auto iniator = mIniator.lock();
if (mIniator.expired()) {
LogWarning("mIniator is expired.\n");
return;
}
MediaTaskInfo info = {
.mResponse = response,
.mSerialNumber = mSerialNumber,
.mCreateTime_s = mCreateTime_s,
};
iniator->TaskResponse(info);
}
std::string MediaTask::GetThumbnailNameByTargetName(const std::string &targetName)
{
// const std::string fileType = MediaTaskType::TAKE_VIDEO == mType ? ".mp4" : ".jpeg";
std::string thumbnailName = targetName;
size_t dot_pos = thumbnailName.find_last_of('.');
if (dot_pos != std::string::npos) {
std::string extension = thumbnailName.substr(dot_pos);
if (extension == ".mp4" || extension == ".jpeg") {
thumbnailName.replace(dot_pos, extension.length(), "-thumbnail.jpeg");
LogInfo("GetThumbnailNameForSaving: %s\n", thumbnailName.c_str());
return thumbnailName;
}
}
LogError("TargetName is not a mp4 file.\n");
std::string unknowFile = "unknow";
return unknowFile;
}
long long MediaTask::GetCreateTime_s(void)
{
auto now = std::chrono::system_clock::now();
// time_t t_now = std::chrono::system_clock::to_time_t(now);
// struct tm tm_now = *std::localtime(&t_now);
return std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
}
int MediaTask::GetFileSize_KB(const char *filePath)
{
struct stat fileInfo;
if (stat(filePath, &fileInfo) != 0) {
LogError("stat failed.\n");
return FILE_SIZE_ERROR;
}
return static_cast<double>(fileInfo.st_size) / 1024.0;
}
MediaTaskType MediaTask::WorkModeConvert(const WorkMode &mode)
{
switch (mode) {
case WorkMode::MODE_PIC:
return MediaTaskType::TAKE_PICTURE;
case WorkMode::MODE_VIDEO:
return MediaTaskType::TAKE_VIDEO;
case WorkMode::MODE_PIC_VIDEO:
return MediaTaskType::TAKE_PICTURE_AND_VIDEO;
default:
LogWarning("unknow work mode.\n");
return MediaTaskType::TAKE_PICTURE_AND_VIDEO;
}
}
std::string MediaTask::GetFileNameBySeriaNumber(const unsigned long &serialNumber, const std::string &sourceFile,
const std::string &savePaht)
{
std::string fileType = "";
size_t dot_pos = sourceFile.find_last_of('.');
if (dot_pos != std::string::npos) {
std::string extension = sourceFile.substr(dot_pos);
fileType = extension;
}
return CreateFileName(serialNumber, savePaht, fileType);
}
std::string MediaTask::CreateFileName(const unsigned long &serialNumber, const std::string &savePaht,
const std::string &fileType)
{
auto now = std::chrono::system_clock::now();
time_t t_now = std::chrono::system_clock::to_time_t(now);
struct tm tm_now = *std::localtime(&t_now);
/**
* @brief When the sequence number is 0, it indicates an invalid sequence number and is replaced by "???".
*
*/
const std::string serialNumberString = 0 == serialNumber ? "???" : std::to_string(serialNumber);
int hour = tm_now.tm_hour;
int minute = tm_now.tm_min;
int second = tm_now.tm_sec;
std::ostringstream pathStream;
pathStream << savePaht << "xak47-" << serialNumberString << "-" << std::setw(2) << std::setfill('0') << hour
<< std::setw(2) << std::setfill('0') << minute << std::setw(2) << std::setfill('0') << second
<< fileType;
return pathStream.str();
}