hunting/middleware/MediaManager/src/RecordMp4.cpp
2024-07-20 15:12:27 +08:00

110 lines
4.0 KiB
C++

/*
* Copyright (c) 2023 Fancy Code.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this mFileAudio 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 "RecordMp4.h"
#include "ILog.h"
#include "IMediaManager.h"
#include "MediaBase.h"
#include "StatusCode.h"
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <mutex>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
RecordMp4::RecordMp4(std::shared_ptr<VMediaTask> &recordTask)
: mRecordMp4Object(nullptr), mRecordTask(recordTask), mIsRecordingFinished(OUTPUT_FILE_STATUS_END)
{
}
StatusCode RecordMp4::Init(void)
{
mRecordMp4Object = ICreateMediaBase(MEDIA_HANDLE_TYPE_COMBINE_MP4);
if (nullptr == mRecordMp4Object) {
LogError("mRecordMp4Object is null.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
std::string videoPath = mRecordTask->GetTargetNameForSaving();
std::string thumbnailPath = mRecordTask->GetThumbnailNameForSaving(videoPath);
const unsigned int duration_ms = mRecordTask->GetVideoDuration_ms();
OutputFileInfo fileInfo = {.mDuration_ms = duration_ms, .mFinished = &mIsRecordingFinished};
if (OUTPUT_FILE_NAME_MAX >= videoPath.size()) {
memcpy(fileInfo.mFileName, videoPath.c_str(), videoPath.size());
}
else {
LogError("VideoPath is too long.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
if (OUTPUT_FILE_NAME_MAX >= thumbnailPath.size()) {
memcpy(fileInfo.mThumbnailFileName, thumbnailPath.c_str(), thumbnailPath.size());
}
else {
LogError("ThumbnailPath is too long.\n");
return CreateStatusCode(STATUS_CODE_NOT_OK);
}
StatusCode code = IOpenOutputFile(mRecordMp4Object, &fileInfo);
if (!IsCodeOK(code)) {
LogError("OpenOutputFile failed.\n");
ICloseOutputFile(mRecordMp4Object);
IMediaBaseFree(mRecordMp4Object);
mRecordMp4Object = nullptr;
}
return code;
}
StatusCode RecordMp4::UnInit(void)
{
StopHandleStream();
return CreateStatusCode(STATUS_CODE_OK);
}
void RecordMp4::StopHandleStream(void)
{
std::lock_guard<std::mutex> locker(mMutex);
if (mRecordMp4Object) {
OutputFileInfo finalFile = IGetOutputFileInfo(mRecordMp4Object);
MediaTaskResponse response(finalFile.mFileName, finalFile.mDuration_ms);
mTaskResponse.push_back(response);
ICloseOutputFile(mRecordMp4Object);
IMediaBaseFree(mRecordMp4Object);
mRecordMp4Object = nullptr;
}
// std::string videoPath = mRecordTask->GetTargetNameForSaving();
}
void RecordMp4::GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
{
std::lock_guard<std::mutex> locker(mMutex);
if (mRecordMp4Object) {
StreamInfo info = {.mType = STREAM_TYPE_VIDEO_H264, .mTimeStamp_us = timeStamp};
IGetStreamData(mRecordMp4Object, stream, length, info);
}
}
void RecordMp4::GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp)
{
std::lock_guard<std::mutex> locker(mMutex);
if (mRecordMp4Object) {
StreamInfo info = {.mType = STREAM_TYPE_AUDIO_G711A, .mTimeStamp_us = timeStamp};
IGetStreamData(mRecordMp4Object, stream, length, info);
}
}
StatusCode RecordMp4::GetAllFiles(std::vector<MediaTaskResponse> &files)
{
files = std::move(mTaskResponse);
mTaskResponse.clear();
return CreateStatusCode(STATUS_CODE_OK);
}
bool RecordMp4::HandleFinished(void)
{
return mIsRecordingFinished == OUTPUT_FILE_STATUS_FINISHED ? true : false;
}