hunting/test/hal/tool/src/CameraHalMock.cpp
2024-06-19 21:50:36 +08:00

149 lines
5.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 "CameraHalMock.h"
#include "IHalCpp.h"
#include "ILog.h"
#include "MediaBase.h"
#include "StatusCode.h"
#include <memory>
CameraHalTest::CameraHalTest(const CameraType &cameraType)
: mCameraType(cameraType), mReadH264File(nullptr), mReadG711aFile(nullptr), mTaskRuning(false)
{
}
void CameraHalTest::Init(void)
{
CameraHal::Init();
if (nullptr == mReadH264File) {
ReadVideoFileCallback videCallback = [](const void *stream, const unsigned int length, void *context) -> void {
((CameraHalTest *)context)->ReadDataFromH264File(stream, length);
};
mReadH264File = ICreateMediaBase(MEDIA_HANDLE_TYPE_READ_H264);
ISetReadVideoCallback(mReadH264File, videCallback, this);
}
if (nullptr == mReadG711aFile) {
ReadAudioFileCallback audioCallback = [](const void *stream, const unsigned int length, void *context) -> void {
((CameraHalTest *)context)->ReadDataFromG711aFile(stream, length);
};
mReadG711aFile = ICreateMediaBase(MEDIA_HANDLE_TYPE_READ_G711A);
ISetReadVideoCallback(mReadG711aFile, audioCallback, this);
}
}
void CameraHalTest::UnInit(void)
{
CameraHal::UnInit();
mTaskRuning = false;
mCv.notify_one();
if (mTaskTimerThread.joinable()) {
mTaskTimerThread.join();
}
if (mReadH264File) {
ISetReadVideoCallback(mReadH264File, nullptr, nullptr);
IStopReadFile(mReadH264File);
IMediaBaseFree(mReadH264File);
mReadH264File = nullptr;
}
if (mReadG711aFile) {
ISetReadAudioCallback(mReadG711aFile, nullptr, nullptr);
IStopReadFile(mReadG711aFile);
IMediaBaseFree(mReadG711aFile);
mReadG711aFile = nullptr;
}
}
void CameraHalTest::SetCameraMonitor(std::shared_ptr<VCameraHalMonitor> &monitor)
{
LogInfo("CameraHalTest::SetCameraMonitor.\n");
mMonitor = monitor;
SetCameraMonitorTrace(monitor);
if (nullptr != mFastBootEvent) {
monitor->ReportEvent(*(mFastBootEvent.get()));
}
}
void CameraHalTest::SetCameraMonitorTrace(std::shared_ptr<VCameraHalMonitor> &monitor)
{
LogWarning("SetCameraMonitorTrace.\n");
}
StatusCode CameraHalTest::StartSingleTask(const CameraTaskParam &param)
{
LogInfo("CameraHalTest::StartSingleTask\n");
auto taskTimerThread = [=](std::shared_ptr<CameraHalTest> cameraHal) {
LogInfo("MockReportMediaStream start.\n");
cameraHal->MockReportMediaStream();
};
std::shared_ptr<CameraHalTest> cameraHal = std::dynamic_pointer_cast<CameraHalTest>(CameraHal::shared_from_this());
mTaskTimerThread = std::thread(taskTimerThread, cameraHal);
StatusCode code = StartSingleTaskTrace(param);
if (StatusCodeEqual(code, "STATUS_CODE_VIRTUAL_FUNCTION")) {
return CameraHal::StartSingleTask(param);
}
return code;
}
StatusCode CameraHalTest::StopTask(void)
{
mTaskRuning = false;
if (nullptr != mReadH264File) {
IStopReadFile(mReadH264File);
}
if (nullptr != mReadG711aFile) {
IStopReadFile(mReadG711aFile);
}
mCv.notify_one();
return CameraHal::StopTask();
}
StatusCode CameraHalTest::StartSingleTaskTrace(const CameraTaskParam &param)
{
LogInfo("CameraHalTest::StartSingleTaskTrace\n");
return CreateStatusCode(STATUS_CODE_VIRTUAL_FUNCTION);
}
void CameraHalTest::MockReportMediaStream(void)
{
mTaskRuning = true;
if (nullptr != mReadH264File) {
IStartReadFile(mReadH264File, TEST_SOURCE_PATH "/support_test/video.h264");
}
if (nullptr != mReadG711aFile) {
IStartReadFile(mReadG711aFile, TEST_SOURCE_PATH "/support_test/audio.g711a");
}
while (mTaskRuning) {
std::unique_lock<std::mutex> lock(mMutex);
mCv.wait(lock, [&] {
return !mTaskRuning;
});
/**
* @brief If the recording time is over, you need to stop the recording timer here.
*/
mTaskRuning = false;
}
}
void CameraHalTest::ReadDataFromH264File(const void *stream, const unsigned int length)
{
GetVideoStream(stream, length, 0);
}
void CameraHalTest::ReadDataFromG711aFile(const void *stream, const unsigned int length)
{
GetAudioStream(stream, length, 0);
}
CameraHalMock::CameraHalMock(const CameraType &cameraType) : CameraHalTest(cameraType)
{
}
void CameraHalMock::MockReportCameraEvent(const CameraReportEvent &event)
{
auto monitor = mMonitor.lock();
if (mMonitor.expired()) {
LogWarning("monitor is nullptr.\n");
mFastBootEvent = std::make_shared<CameraReportEvent>(event.mFileName, event.mCameraType);
return;
}
monitor->ReportEvent(event);
}