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 
23 namespace OHOS {
24 namespace Media {
25 class AVMetadataHelperImpl : public AVMetadataHelper, public NoCopyable {
26 public:
27     AVMetadataHelperImpl();
28     ~AVMetadataHelperImpl();
29 
30     int32_t SetSource(const std::string &uri, int32_t usage) override;
31     int32_t SetSource(int32_t fd, int64_t offset, int64_t size, int32_t usage) override;
32     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
33     std::string ResolveMetadata(int32_t key) override;
34     std::unordered_map<int32_t, std::string> ResolveMetadata() override;
35     std::shared_ptr<Meta> GetAVMetadata() override;
36     std::shared_ptr<AVSharedMemory> FetchArtPicture() override;
37     std::shared_ptr<PixelMap> FetchFrameAtTime(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
38     std::shared_ptr<PixelMap> FetchFrameYuv(int64_t timeUs, int32_t option, const PixelMapParams &param) override;
39     void Release() override;
40     int32_t Init();
41     int32_t SetHelperCallback(const std::shared_ptr<HelperCallback> &callback) override;
42     void SetScene(Scene scene) override;
43     int32_t GetTimeByFrameIndex(uint32_t index, uint64_t &time) override;
44     int32_t GetFrameIndexByTime(uint64_t time, uint32_t &index) override;
45 private:
46     struct PixelMapInfo {
47         int32_t rotation = 0;
48         PixelFormat pixelFormat = PixelFormat::NV12;
49         bool isHdr = false;
50     };
51 
52     std::mutex releaseMutex_;
53     std::shared_ptr<IAVMetadataHelperService> avMetadataHelperService_ = nullptr;
54     int32_t rotation_ = 0;
55     static std::chrono::milliseconds cloneTimestamp;
56     static std::chrono::milliseconds batchHandleTimestamp;
57     void ReportSceneCode(Scene scene);
58 
59     sptr<SurfaceBuffer> CopySurfaceBuffer(sptr<SurfaceBuffer> &srcSurfaceBuffer);
60     std::shared_ptr<PixelMap> CreatePixelMapYuv(const std::shared_ptr<AVBuffer> &frameBuffer,
61                                                 PixelMapInfo &pixelMapInfo);
62     std::shared_ptr<PixelMap> OnCreatePixelMapSdr(const std::shared_ptr<AVBuffer> &frameBuffer,
63                                                   PixelMapInfo &pixelMapInfo,
64                                                   InitializationOptions &options);
65     std::shared_ptr<PixelMap> OnCreatePixelMapHdr(sptr<SurfaceBuffer> &mySurfaceBuffer);
66     void SetPixelMapYuvInfo(std::shared_ptr<PixelMap> pixelMap, bool isPlanesAvailable,
67                             OH_NativeBuffer_Planes *planes);
68     void CopySurfaceBufferInfo(sptr<SurfaceBuffer> &source, sptr<SurfaceBuffer> &dst);
69     bool GetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &staticMetadata);
70     bool GetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &dynamicMetadata);
71     bool SetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &staticMetadata);
72     bool SetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &dynamicMetadata);
73     int32_t CopySurfaceBufferPixels(sptr<SurfaceBuffer> &srcSurfaceBuffer, sptr<SurfaceBuffer> &dstSurfaceBuffer);
74     static void ScalePixelMap(std::shared_ptr<PixelMap> &pixelMap, PixelMapInfo &info, const PixelMapParams &param);
75 };
76 } // namespace Media
77 } // namespace OHOS
78 #endif // AVMETADATAHELPER_IMPL_H
79