1 /* 2 * Copyright (C) 2021 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 #ifndef AVMETADATAHELPER_IMPL_H 16 #define AVMETADATAHELPER_IMPL_H 17 18 #include "avmetadatahelper.h" 19 #include "nocopyable.h" 20 #include "i_avmetadatahelper_service.h" 21 #include "surface_buffer.h" 22 #include <cerrno> 23 #include <climits> 24 #include <cmath> 25 #include <cstdint> 26 #include <cstdlib> 27 #include <string> 28 #include <fstream> 29 #include <sstream> 30 #include <chrono> 31 32 #include "color_space.h" 33 #include "v1_0/cm_color_space.h" 34 35 namespace OHOS { 36 namespace Media { 37 class AVMetadataHelperImpl : public AVMetadataHelper, public NoCopyable { 38 public: 39 AVMetadataHelperImpl(); 40 ~AVMetadataHelperImpl(); 41 42 int32_t SetSource(const std::string &uri, int32_t usage) override; 43 int32_t SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage) override; 44 int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override; 45 std::string ResolveMetadata(int32_t key) override; 46 std::unordered_map<int32_t, std::string> ResolveMetadata() override; 47 std::shared_ptr<Meta> GetAVMetadata() override; 48 std::shared_ptr<AVSharedMemory> FetchArtPicture() override; 49 std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams ¶m) override; 50 std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams ¶m) override; 51 void Release() override; 52 int32_t Init(); 53 int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) override; 54 void SetScene(Scene scene) override; 55 int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time) override; 56 int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index) override; 57 private: 58 struct PixelMapInfo { 59 int32_t rotation = 0; 60 PixelFormat pixelFormat = PixelFormat::NV12; 61 bool isHdr = false; 62 int32_t width = 0; 63 int32_t height = 0; 64 int32_t outputHeight = 0; 65 int32_t primaries = 0; 66 uint8_t srcRange = 0; 67 ColorManager::ColorSpaceName colorSpaceName = ColorManager::ColorSpaceName::NONE; 68 }; 69 70 std::mutex releaseMutex_; 71 GetLocalTime()72 static std::string GetLocalTime() 73 { 74 // time string : "year-month-day hour_minute_second.millisecond", ':' is not supported in windows file name 75 auto now = std::chrono::system_clock::now(); 76 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000; 77 std::time_t t = std::chrono::system_clock::to_time_t(now); 78 auto tmPtr = std::localtime(&t); 79 if (tmPtr == nullptr) { 80 return "0000-00-00 00_00_00"; 81 } 82 std::tm tmInfo = *tmPtr; 83 84 std::stringstream ss; 85 int millSecondWidth = 3; 86 ss << std::put_time(&tmInfo, "%Y-%m-%d %H_%M_%S.") << std::setfill('0') 87 << std::setw(millSecondWidth) << ms.count(); 88 return ss.str(); 89 } 90 91 void FormatColorSpaceInfo(OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo &colorSpaceInfo); 92 Status GetColorSpace(sptr<SurfaceBuffer> &surfaceBuffer, PixelMapInfo &pixelMapInfo); 93 94 std::shared_ptr<IAVMetadataHelperService> avMetadataHelperService_ = nullptr; 95 int32_t rotation_ = 0; 96 bool isDump_ = false; 97 static std::chrono::milliseconds cloneTimestamp; 98 static std::chrono::milliseconds batchHandleTimestamp; 99 void ReportSceneCode(Scene scene); 100 101 std::shared_ptr<PixelMap> CreatePixelMapYuv(const std::shared_ptr<AVBuffer> &frameBuffer, 102 PixelMapInfo &pixelMapInfo); 103 std::shared_ptr<PixelMap> CreatePixelMapFromAVShareMemory(const std::shared_ptr<AVBuffer> &frameBuffer, 104 PixelMapInfo &pixelMapInfo, 105 InitializationOptions &options); 106 std::shared_ptr<PixelMap> CreatePixelMapFromSurfaceBuffer(sptr<SurfaceBuffer> &mySurfaceBuffer, 107 PixelMapInfo &pixelMapInfo); 108 void SetPixelMapYuvInfo(sptr<SurfaceBuffer> &surfaceBuffer, std::shared_ptr<PixelMap> pixelMap, 109 PixelMapInfo &pixelMapInfo, bool needModifyStride); 110 std::string pixelFormatToString(PixelFormat pixelFormat); 111 static void ScalePixelMap(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info, const PixelMapParams ¶m); 112 int32_t CopySurfaceBufferToPixelMap(sptr<SurfaceBuffer> &SurfaceBuffer, 113 std::shared_ptr<PixelMap> pixelMap, 114 PixelMapInfo &pixelMapInfo); 115 int32_t SaveDataToFile(const std::string &fileName, const char *data, const size_t &totalSize); 116 void InitDumpFlag(); 117 int32_t DumpPixelMap(bool isDump, std::shared_ptr<PixelMap> pixelMap, const std::string &fileName); 118 int32_t DumpAVBuffer(bool isDump, const std::shared_ptr<AVBuffer> &frameBuffer, const std::string &fileName); 119 }; 120 } // namespace Media 121 } // namespace OHOS 122 #endif // AVMETADATAHELPER_IMPL_H 123