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 namespace OHOS {
33 namespace Media {
34 class AVMetadataHelperImpl : public AVMetadataHelper, public NoCopyable {
35 public:
36     AVMetadataHelperImpl();
37     ~AVMetadataHelperImpl();
38 
39     int32_t SetSource(const std::string &uri, int32_t usage) override;
40     int32_t SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage) override;
41     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
42     std::string ResolveMetadata(int32_t key) override;
43     std::unordered_map<int32_t, std::string> ResolveMetadata() override;
44     std::shared_ptr<Meta> GetAVMetadata() override;
45     std::shared_ptr<AVSharedMemory> FetchArtPicture() override;
46     std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
47     std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
48     void Release() override;
49     int32_t Init();
50     int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) override;
51     void SetScene(Scene scene) override;
52     int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time) override;
53     int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index) override;
54     void SetIsNapiInstance(bool isNapiInstance) override;
55 private:
56     struct PixelMapInfo {
57         int32_t rotation = 0;
58         PixelFormat pixelFormat = PixelFormat::NV12;
59         bool isHdr = false;
60         int32_t outputHeight = 0;
61     };
62 
GetLocalTime()63     static std::string GetLocalTime()
64     {
65         // time string : "year-month-day hour_minute_second.millisecond", ':' is not supported in windows file name
66         auto now = std::chrono::system_clock::now();
67         auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
68         std::time_t t = std::chrono::system_clock::to_time_t(now);
69         std::tm tmInfo = *std::localtime(&t);
70 
71         std::stringstream ss;
72         int millSecondWidth = 3;
73         ss << std::put_time(&tmInfo, "%Y-%m-%d %H_%M_%S.") << std::setfill('0')
74            << std::setw(millSecondWidth) << ms.count();
75         return ss.str();
76     }
77     std::shared_ptr<IAVMetadataHelperService> avMetadataHelperService_ = nullptr;
78     int32_t rotation_ = 0;
79     bool isDump_ = false;
80     static std::chrono::milliseconds cloneTimestamp;
81     static std::chrono::milliseconds batchHandleTimestamp;
82     void ReportSceneCode(Scene scene);
83 
84     sptr<SurfaceBuffer> CopySurfaceBuffer(sptr<SurfaceBuffer> &srcSurfaceBuffer);
85     std::shared_ptr<PixelMap> CreatePixelMapYuv(const std::shared_ptr<AVBuffer> &frameBuffer,
86                                                 PixelMapInfo &pixelMapInfo);
87     std::shared_ptr<PixelMap> CreatePixelMapFromAVShareMemory(const std::shared_ptr<AVBuffer> &frameBuffer,
88                                                               PixelMapInfo &pixelMapInfo,
89                                                               InitializationOptions &options);
90     std::shared_ptr<PixelMap> CreatePixelMapFromSurfaceBuffer(sptr<SurfaceBuffer> &mySurfaceBuffer,
91                                                               PixelMapInfo &pixelMapInfo);
92     void SetPixelMapYuvInfo(sptr<SurfaceBuffer> &surfaceBuffer, std::shared_ptr<PixelMap> pixelMap,
93                             PixelMapInfo &pixelMapInfo);
94     std::string pixelFormatToString(PixelFormat pixelFormat);
95     void CopySurfaceBufferInfo(sptr<SurfaceBuffer> &source, sptr<SurfaceBuffer> &dst);
96     bool GetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &staticMetadata);
97     bool GetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &dynamicMetadata);
98     bool SetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &staticMetadata);
99     bool SetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &dynamicMetadata);
100     static void ScalePixelMap(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info, const PixelMapParams &param);
101     int32_t CopySurfaceBufferToPixelMap(sptr<SurfaceBuffer> &SurfaceBuffer,
102                                         std::shared_ptr<PixelMap> pixelMap,
103                                         PixelMapInfo &pixelMapInfo);
104     int32_t SaveDataToFile(const std::string &fileName, const char *data, const size_t &totalSize);
105     void InitDumpFlag();
106     int32_t DumpPixelMap(bool isDump, std::shared_ptr<PixelMap> pixelMap, const std::string &fileName);
107     int32_t DumpAVBuffer(bool isDump, const std::shared_ptr<AVBuffer> &frameBuffer, const std::string &fileName);
108 };
109 } // namespace Media
110 } // namespace OHOS
111 #endif // AVMETADATAHELPER_IMPL_H
112