/* * 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 "CameraHal.h" #include "IHalCpp.h" #include "ILog.h" #include "LinuxApi.h" #include "StatusCode.h" #include CameraHal::CameraHal() : mTaskRuning(false), mAudioStreamCallback(nullptr), mVideoStreamCallback(nullptr), mVideoFile(nullptr), mAudioFile(nullptr), mTaskType(CameraTaskType::END), mFastBootSaveFile(false) { } void CameraHal::Init(void) { } void CameraHal::UnInit(void) { } void CameraHal::SetCameraMonitor(std::shared_ptr &monitor) { std::lock_guard locker(mMutex); LogInfo("ssssssssssssssssssssssssssssssssssssSetCameraMonitor.\n"); mCameraMonitor = monitor; if (nullptr != mFastBootEvent) { monitor->ReportEvent(*(mFastBootEvent.get())); } } StatusCode CameraHal::StartSingleTask(const CameraTaskParam ¶m) { LogInfo("StartSingleTask.\n"); // mTaskType = param.mTaskType; mTaskRuning = true; fx_system_v2("rm -f " SD_CARD_MOUNT_PATH "/chip.g711a"); fx_system_v2("rm -f " SD_CARD_MOUNT_PATH "/chip.h264"); mAudioFile = fopen(SD_CARD_MOUNT_PATH "/chip.g711a", "a+"); mVideoFile = fopen(SD_CARD_MOUNT_PATH "/chip.h264", "a+"); return CreateStatusCode(STATUS_CODE_OK); } StatusCode CameraHal::StopTask(void) { mTaskRuning = false; if (mAudioFile) { fclose(mAudioFile); mAudioFile = nullptr; } if (mVideoFile) { fclose(mVideoFile); mVideoFile = nullptr; } // mTaskType = CameraTaskType::END; fx_system_v2("sync"); return CreateStatusCode(STATUS_CODE_OK); } StatusCode CameraHal::SetAudioStreamCallback(AudioStreamCallback callback) { mAudioStreamCallback = callback; return CreateStatusCode(STATUS_CODE_OK); } StatusCode CameraHal::SetVideoStreamCallback(VideoStreamCallback callback) { mVideoStreamCallback = callback; return CreateStatusCode(STATUS_CODE_OK); } StatusCode CameraHal::SetJpegEncodeCallback(JpegEncodeCallback callback) { mJpegEncodeCallback = callback; return CreateStatusCode(STATUS_CODE_OK); } void CameraHal::GetAudioStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) { if (mTaskRuning && nullptr != mAudioStreamCallback) { SaveChipStream(ChipStreamType::AUDIO, stream, length); mAudioStreamCallback(stream, length, timeStamp); } } void CameraHal::GetVideoStream(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) { if (mTaskRuning && nullptr != mVideoStreamCallback) { SaveChipStream(ChipStreamType::VIDEO, stream, length); mVideoStreamCallback(stream, length, timeStamp); } } void CameraHal::GetJpegData(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) { if (mTaskRuning && nullptr != mJpegEncodeCallback) { mJpegEncodeCallback(stream, length, timeStamp); } else { FastStartGetJpegData(stream, length, timeStamp); } } void CameraHal::SaveChipStream(const ChipStreamType &streamType, const void *stream, const unsigned int &length) { FILE *file = nullptr; file = streamType == ChipStreamType::AUDIO ? mAudioFile : mVideoFile; if (file) { fwrite(stream, 1, length, file); fflush(file); } } void CameraHal::FastStartGetJpegData(const void *stream, const unsigned int &length, const unsigned long long &timeStamp) { std::lock_guard locker(mMutex); if (mFastBootSaveFile) { return; } std::string savedFile = SaveJpeg(stream, length); if (savedFile.empty()) { LogError("save jpeg failed.\n"); return; } mFastBootSaveFile = true; auto monitor = mCameraMonitor.lock(); if (mCameraMonitor.expired()) { LogWarning("SdCardHal: monitor is expired.\n"); mFastBootEvent = std::make_shared(savedFile, CameraType::MAIN_CAMERA); return; } CameraReportEvent report(savedFile, CameraType::MAIN_CAMERA); monitor->ReportEvent(report); } std::string CameraHal::SaveJpeg(const void *data, unsigned int dataLength) { const std::string SAVE_JPEG_FAILED = ""; const std::string SaveJpegPath = "/tmp/fastboot.jpg"; FILE *fp = nullptr; fp = fopen(SaveJpegPath.c_str(), "wb"); if (fp) { fwrite(data, 1, dataLength, fp); fflush(fp); fclose(fp); LogInfo("save fastboot jpeg success.\n"); return SaveJpegPath; } else { LogInfo("open file error\n"); return SAVE_JPEG_FAILED; } }