1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_AVSESSION_UTILS_H 17 #define OHOS_AVSESSION_UTILS_H 18 19 #include <cstdio> 20 #include <fstream> 21 #include <string> 22 #include <vector> 23 24 #include "avsession_log.h" 25 #include "directory_ex.h" 26 27 namespace OHOS::AVSession { 28 class AVSessionUtils { 29 public: 30 static constexpr const int32_t MAX_FILE_SIZE = 200 * 1024; 31 WriteImageToFile(const std::shared_ptr<AVSessionPixelMap> & innerPixelMap,const std::string & fileName)32 static void WriteImageToFile(const std::shared_ptr<AVSessionPixelMap>& innerPixelMap, const std::string& fileName) 33 { 34 if (innerPixelMap == nullptr) { 35 SLOGE("innerPixelMap is nullptr"); 36 return; 37 } 38 39 char realPath[PATH_MAX] = { 0x00 }; 40 if (realpath(AVSessionUtils::GetCachePathName(), realPath) == nullptr) { 41 SLOGE("check path failed %{public}s", AVSessionUtils::GetCachePathName()); 42 return; 43 } 44 45 size_t imgBufferSize = innerPixelMap->GetInnerImgBuffer().size(); 46 SLOGI("imgBufferSize=%{public}zu", imgBufferSize); 47 if (imgBufferSize > MAX_FILE_SIZE) { 48 SLOGE("error, dataSize larger than %{public}d", MAX_FILE_SIZE); 49 return; 50 } 51 52 std::ofstream ofile(fileName.c_str(), std::ios::binary | std::ios::out | std::ios::trunc); 53 if (!ofile.is_open()) { 54 SLOGE("open file error, fileName=%{public}s", fileName.c_str()); 55 return; 56 } 57 58 ofile.write((char*)&imgBufferSize, sizeof(size_t)); 59 ofile.write((char*)(&(innerPixelMap->GetInnerImgBuffer()[0])), imgBufferSize); 60 ofile.close(); 61 } 62 ReadImageFromFile(std::shared_ptr<AVSessionPixelMap> & innerPixelMap,const std::string & fileName)63 static void ReadImageFromFile(std::shared_ptr<AVSessionPixelMap>& innerPixelMap, const std::string& fileName) 64 { 65 if (innerPixelMap == nullptr) { 66 SLOGE("innerPixelMap is nullptr"); 67 return; 68 } 69 70 char realPath[PATH_MAX] = { 0x00 }; 71 if (realpath(AVSessionUtils::GetCachePathName(), realPath) == nullptr) { 72 SLOGE("check path failed %{public}s", AVSessionUtils::GetCachePathName()); 73 return; 74 } 75 76 std::ifstream ifile(fileName.c_str(), std::ios::binary | std::ios::in); 77 if (!ifile.is_open()) { 78 SLOGE("open file error, fileName=%{public}s", fileName.c_str()); 79 return; 80 } 81 82 size_t imgBufferSize; 83 ifile.read((char*)&imgBufferSize, sizeof(size_t)); 84 SLOGI("imgBufferSize=%{public}zu", imgBufferSize); 85 if (imgBufferSize > MAX_FILE_SIZE) { 86 SLOGE("error, dataSize larger than %{public}d", MAX_FILE_SIZE); 87 ifile.close(); 88 return; 89 } 90 std::vector<std::uint8_t> imgBuffer(imgBufferSize); 91 ifile.read((char*)&imgBuffer[0], imgBufferSize); 92 innerPixelMap->SetInnerImgBuffer(imgBuffer); 93 ifile.close(); 94 } 95 DeleteFile(const std::string & filePath)96 static void DeleteFile(const std::string& filePath) 97 { 98 if (OHOS::RemoveFile(filePath)) { 99 SLOGI("remove .image.dat file success filePath=%{public}s", filePath.c_str()); 100 } else { 101 SLOGE("remove .image.dat file fail filePath=%{public}s", filePath.c_str()); 102 } 103 } 104 DeleteCacheFiles(const std::string & path)105 static void DeleteCacheFiles(const std::string& path) 106 { 107 std::vector<std::string> fileList; 108 OHOS::GetDirFiles(path, fileList); 109 for (const auto& file : fileList) { 110 if (file.find(AVSessionUtils::GetFileSuffix()) != std::string::npos) { 111 DeleteFile(file); 112 } 113 } 114 } 115 GetCachePathName()116 static const char* GetCachePathName() 117 { 118 return CACHE_PATH_NAME; 119 } 120 GetFileSuffix()121 static const char* GetFileSuffix() 122 { 123 return FILE_SUFFIX; 124 } 125 126 private: 127 static constexpr const char* CACHE_PATH_NAME = "/data/service/el1/public/av_session/cache/"; 128 static constexpr const char* FILE_SUFFIX = ".image.dat"; 129 }; 130 } // namespace OHOS::AVSession 131 #endif // OHOS_AVSESSION_UTILS_H