hunting/application/MissionManager/src/MediaTask.cpp
2024-07-20 17:24:14 +08:00

114 lines
4.1 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 "ILog.h"
#include "IMediaManager.h"
#include <cctype>
#include <chrono>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#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;
}
std::string MediaTask::GetTargetNameForSaving(void)
{
if (!mTargetName.empty()) {
return mTargetName;
}
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);
mCreateTime_s = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
int hour = tm_now.tm_hour;
int minute = tm_now.tm_min;
int second = tm_now.tm_sec;
std::ostringstream pathStream;
pathStream << mSavePath << "xak47-" << mSerialNumber << "-" << std::setw(2) << std::setfill('0') << hour
<< std::setw(2) << std::setfill('0') << minute << std::setw(2) << std::setfill('0') << second << ".mp4";
mTargetName = pathStream.str();
LogInfo("GetTargetNameForSaving: %s\n", pathStream.str().c_str());
return pathStream.str();
}
std::string MediaTask::GetThumbnailNameForSaving(const std::string &targetName)
{
return MediaTask::GetThumbnailNameByTargetName(targetName);
// 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") {
// 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;
}
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)
{
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") {
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;
}