1 /*
2  * Copyright (C) 2023 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 AVMETADATAHELPER_HST_IMPL_H
17 #define AVMETADATAHELPER_HST_IMPL_H
18 
19 #include <condition_variable>
20 #include <mutex>
21 
22 #include "avmetadata_collector.h"
23 #include "av_thumbnail_generator.h"
24 #include "buffer/avsharedmemorybase.h"
25 #include "common/status.h"
26 #include "i_avmetadatahelper_engine.h"
27 #include "i_avmetadatahelper_service.h"
28 #include "media_demuxer.h"
29 #include "nocopyable.h"
30 
31 namespace OHOS {
32 namespace Media {
33 class TimeAndIndexConversion;
34 class AVMetadataHelperImpl : public IAVMetadataHelperEngine,
35                              public std::enable_shared_from_this<AVMetadataHelperImpl>,
36                              public NoCopyable {
37 public:
38     AVMetadataHelperImpl();
39     ~AVMetadataHelperImpl();
40 
41     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
42     int32_t SetSource(const std::string &uri, int32_t usage) override;
43     int32_t SetSource(const std::shared_ptr<IMediaDataSource> &dataSrc) override;
44     std::string ResolveMetadata(int32_t key) override;
45     std::unordered_map<int32_t, std::string> ResolveMetadata() override;
46     std::shared_ptr<Meta> GetAVMetadata() override;
47     std::shared_ptr<AVSharedMemory> FetchFrameAtTime(
48         int64_t timeUs, int32_t option, const OutputConfiguration &param) override;
49     std::shared_ptr<AVBuffer> FetchFrameYuv(
50         int64_t timeUs, int32_t option, const OutputConfiguration &param) override;
51     std::shared_ptr<AVSharedMemory> FetchArtPicture() 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 SetInterruptState(bool isInterruptNeeded) override;
55 
56 private:
57     std::shared_ptr<OHOS::Media::MediaDemuxer> mediaDemuxer_;
58     std::shared_ptr<AVMetaDataCollector> metadataCollector_;
59     std::shared_ptr<AVThumbnailGenerator> thumbnailGenerator_;
60     std::unordered_map<int32_t, std::string> collectedMeta_;
61     std::shared_ptr<AVSharedMemory> collectedArtPicture_;
62     std::shared_ptr<AVSharedMemoryBase> fetchedFrameAtTime_;
63     std::shared_ptr<OHOS::Media::TimeAndIndexConversion> conversion_ { nullptr };
64     std::atomic_bool stopProcessing_{ false };
65     std::atomic_bool isForFrameConvert_{ false };
66 
67     Status SetSourceInternel(const std::string &uri, bool isForFrameConvert);
68     Status SetSourceInternel(const std::shared_ptr<IMediaDataSource> &dataSrc);
69     Status SetSourceForFrameConvert(const std::string &uri);
70     Status InitMetadataCollector();
71     Status InitThumbnailGenerator();
72     int32_t GetTimeForFrameConvert(uint32_t index, uint64_t &time);
73     int32_t GetIndexForFrameConvert(uint64_t time, uint32_t &index);
74 
75     void Reset();
76     void Destroy();
77     std::string groupId_;
78     std::atomic<bool> isInterruptNeeded_ = false;
79 };
80 }  // namespace Media
81 }  // namespace OHOS
82 #endif  // AVMETADATAHELPER_HST_IMPL_H