• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AV_THUMBNAIL_GENERATOR
17 #define AV_THUMBNAIL_GENERATOR
18 
19 #include <unordered_map>
20 #include <set>
21 #include <deque>
22 #include <condition_variable>
23 #include <mutex>
24 #include <nocopyable.h>
25 
26 #include "buffer/avsharedmemorybase.h"
27 #include "common/status.h"
28 #include "i_avmetadatahelper_service.h"
29 #include "media_demuxer.h"
30 #include "pipeline/pipeline.h"
31 #include "video_decoder_adapter.h"
32 #include "buffer/avbuffer.h"
33 
34 namespace OHOS {
35 namespace Media {
36 class AVThumbnailGenerator : public NoCopyable, public std::enable_shared_from_this<AVThumbnailGenerator> {
37 public:
38     explicit AVThumbnailGenerator(std::shared_ptr<MediaDemuxer> &mediaDemuxer, int32_t appUid, int32_t appPid,
39         uint32_t appTokenId, uint64_t appFullTokenId);
40     ~AVThumbnailGenerator();
41     std::shared_ptr<AVSharedMemory> FetchFrameAtTime(int64_t timeUs, int32_t option, const OutputConfiguration &param);
42     std::shared_ptr<AVBuffer> FetchFrameYuv(int64_t timeUs, int32_t option, const OutputConfiguration &param);
43     std::shared_ptr<AVSharedMemory> FetchArtPicture();
44 
45     void Reset();
46     void Destroy();
47     void OnEvent(const Event &event);
48     void OnCallback(std::shared_ptr<OHOS::Media::Pipeline::Filter> filter,
49         const OHOS::Media::Pipeline::FilterCallBackCommand cmd, OHOS::Media::Pipeline::StreamType outType);
50     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
51     void OnOutputFormatChanged(const MediaAVCodec::Format &format);
52     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
53     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
54     void OnFetchedFrameBufferAvailable();
55     int32_t Init();
56     void SetClientBundleName(std::string appName);
57     void AcquireAvailableInputBuffer();
58 
59 private:
60     OutputConfiguration outputConfig_;
61     Format outputFormat_;
62     int64_t seekTime_{0};
63     std::atomic_bool hasFetchedFrame_{false};
64     std::atomic_bool stopProcessing_{false};
65     std::atomic_bool readErrorFlag_{ false };
66     std::atomic_bool isBufferAvailable_{ false };
67     std::string trackMime_;
68     Plugins::VideoRotation rotation_ = Plugins::VideoRotation::VIDEO_ROTATION_0;
69     Plugins::VideoOrientationType orientation_ = Plugins::VideoOrientationType::ROTATE_NONE ;
70     size_t trackIndex_{0};
71     std::shared_ptr<Meta> trackInfo_;
72     std::mutex mutex_;
73     std::mutex queueMutex_;
74     std::mutex dtsQueMutex_;
75     FileType fileType_ = FileType::UNKNOW;
76     std::deque<int64_t> inputBufferDtsQue_;
77     std::condition_variable cond_;
78     std::condition_variable bufferAvailableCond_;
79     std::atomic<uint32_t> bufferIndex_;
80     std::shared_ptr<AVBuffer> avBuffer_;
81     sptr<SurfaceBuffer> surfaceBuffer_;
82     std::shared_ptr<AVSharedMemoryBase> fetchedFrameAtTime_;
83     std::shared_ptr<OHOS::Media::MediaDemuxer> mediaDemuxer_;
84     std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> videoDecoder_;
85     std::shared_ptr<Media::AVBufferQueue> inputBufferQueue_;
86     sptr<Media::AVBufferQueueProducer> inputBufferQueueProducer_;
87     sptr<Media::AVBufferQueueConsumer> inputBufferQueueConsumer_;
88     std::unique_ptr<Task> readTask_;
89     std::vector<std::shared_ptr<AVBuffer>> bufferVector_;
90 
91     Status InitDecoder();
92     std::shared_ptr<Meta> GetVideoTrackInfo();
93     void SetDemuxerOutputBufferPts(std::shared_ptr<AVBuffer> &outputBuffer);
94     void GetInputBufferDts(std::shared_ptr<AVBuffer> &inputBuffer);
95     void SetDecoderOutputBufferPts(std::shared_ptr<AVBuffer> &outputBuffer);
96     void ConvertToAVSharedMemory();
97     void ConvertP010ToNV12(
98         const sptr<SurfaceBuffer> &surfaceBuffer, uint8_t *dstNV12, int32_t strideWidth, int32_t strideHeight);
99     int32_t GetYuvDataAlignStride(const sptr<SurfaceBuffer> &surfaceBuffer);
100     Status SeekToTime(int64_t timeMs, Plugins::SeekMode option, int64_t realSeekTime);
101     int32_t width_ = 0;
102     int32_t height_ = 0;
103     int32_t appUid_{0};
104     int32_t appPid_{0};
105     uint32_t appTokenId_{0};
106     uint64_t appFullTokenId_{0};
107     std::string appName_;
108     double frameRate_ { 0.0 };
109     Plugins::SeekMode seekMode_ {};
110     int64_t duration_ = 0;
111 
112     std::shared_ptr<AVBuffer> GenerateAlignmentAvBuffer();
113     std::shared_ptr<AVBuffer> GenerateAvBufferFromFCodec();
114     void CopySurfaceBufferInfo(sptr<SurfaceBuffer> &source, sptr<SurfaceBuffer> &dst);
115     bool GetSbStaticMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &staticMetadata);
116     bool GetSbDynamicMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &dynamicMetadata);
117     bool SetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &staticMetadata);
118     bool SetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &dynamicMetadata);
119 
120     void HandleFetchFrameYuvRes();
121     void HandleFetchFrameYuvFailed();
122     void HandleFetchFrameAtTimeRes();
123 
124     void PauseFetchFrame();
125     void InitMediaInfoFromGlobalMeta();
126     int64_t ReadLoop();
127     void FlushBufferQueue();
128     int64_t StopTask();
129 };
130 }  // namespace Media
131 }  // namespace OHOS
132 #endif  // AV_THUMBNAIL_GENERATOR