• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 SetAVMetadataCaller(AVMetadataCaller caller) override;
44     int32_t SetUrlSource(const std::string &uri, const std::map<std::string, std::string> &header) override;
45     int32_t SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage) override;
46     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
47     std::string ResolveMetadata(int32_t key) override;
48     std::unordered_map<int32_t, std::string> ResolveMetadata() override;
49     std::shared_ptr<Meta> GetAVMetadata() override;
50     std::shared_ptr<AVSharedMemory> FetchArtPicture() override;
51     std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
52     std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
53     std::shared_ptr<PixelMap> FetchScaledFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
54     void Release() override;
55     int32_t Init();
56     int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) override;
57     void SetScene(Scene scene) override;
58     int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time) override;
59     int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index) override;
60 private:
61     struct PixelMapInfo {
62         int32_t rotation = 0;
63         int32_t orientation = 0;
64         PixelFormat pixelFormat = PixelFormat::NV12;
65         bool isHdr = false;
66         int32_t width = 0;
67         int32_t height = 0;
68         int32_t outputHeight = 0;
69         int32_t primaries = 0;
70         uint8_t srcRange = 0;
71         ColorManager::ColorSpaceName colorSpaceName = ColorManager::ColorSpaceName::NONE;
72     };
73 
74     std::mutex releaseMutex_;
75 
GetLocalTime()76     static std::string GetLocalTime()
77     {
78         // time string : "year-month-day hour_minute_second.millisecond", ':' is not supported in windows file name
79         auto now = std::chrono::system_clock::now();
80         auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
81         std::time_t t = std::chrono::system_clock::to_time_t(now);
82         auto tmPtr = std::localtime(&t);
83         if (tmPtr == nullptr) {
84             return "0000-00-00 00_00_00";
85         }
86         std::tm tmInfo = *tmPtr;
87 
88         std::stringstream ss;
89         int millSecondWidth = 3;
90         ss << std::put_time(&tmInfo, "%Y-%m-%d %H_%M_%S.") << std::setfill('0')
91            << std::setw(millSecondWidth) << ms.count();
92         return ss.str();
93     }
94 
95     void FormatColorSpaceInfo(OHOS::HDI::Display::Graphic::Common::V1_0::CM_ColorSpaceInfo &colorSpaceInfo);
96     Status GetColorSpace(sptr<SurfaceBuffer> &surfaceBuffer, PixelMapInfo &pixelMapInfo);
97 
98     std::shared_ptr<IAVMetadataHelperService> avMetadataHelperService_ = nullptr;
99     int32_t rotation_ = 0;
100     bool isDump_ = false;
101     static std::chrono::milliseconds cloneTimestamp;
102     static std::chrono::milliseconds batchHandleTimestamp;
103     void ReportSceneCode(Scene scene);
104 
105     std::shared_ptr<PixelMap> CreatePixelMapYuv(const std::shared_ptr<AVBuffer> &frameBuffer,
106                                                 PixelMapInfo &pixelMapInfo);
107     std::shared_ptr<PixelMap> CreatePixelMapFromAVShareMemory(const std::shared_ptr<AVBuffer> &frameBuffer,
108                                                               PixelMapInfo &pixelMapInfo,
109                                                               InitializationOptions &options);
110     std::shared_ptr<PixelMap> CreatePixelMapFromSurfaceBuffer(sptr<SurfaceBuffer> &mySurfaceBuffer,
111                                                               PixelMapInfo &pixelMapInfo);
112     void SetPixelMapYuvInfo(sptr<SurfaceBuffer> &surfaceBuffer, std::shared_ptr<PixelMap> pixelMap,
113                             PixelMapInfo &pixelMapInfo, bool needModifyStride);
114     std::string pixelFormatToString(PixelFormat pixelFormat);
115     static void ScalePixelMapByMode(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info,
116                                     const PixelMapParams &param, int32_t scaleMode);
117     static void ScalePixelMap(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info, const PixelMapParams &param);
118     static void ScalePixelMapWithEqualRatio(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info,
119                                             const PixelMapParams &param);
120     std::shared_ptr<PixelMap> FetchFrameBase(int64_t timeUs, int32_t option,
121                                              const PixelMapParams &param, int32_t scaleMode);
122     int32_t CopySurfaceBufferToPixelMap(sptr<SurfaceBuffer> &SurfaceBuffer,
123                                         std::shared_ptr<PixelMap> pixelMap,
124                                         PixelMapInfo &pixelMapInfo);
125     int32_t SaveDataToFile(const std::string &fileName, const char *data, const size_t &totalSize);
126     void InitDumpFlag();
127     int32_t DumpPixelMap(bool isDump, std::shared_ptr<PixelMap> pixelMap, const std::string &fileName);
128     int32_t DumpAVBuffer(bool isDump, const std::shared_ptr<AVBuffer> &frameBuffer, const std::string &fileName);
129 };
130 } // namespace Media
131 } // namespace OHOS
132 #endif // AVMETADATAHELPER_IMPL_H
133