• 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 <condition_variable>
22 #include <mutex>
23 #include <nocopyable.h>
24 
25 #include "buffer/avsharedmemorybase.h"
26 #include "common/status.h"
27 #include "i_avmetadatahelper_service.h"
28 #include "media_demuxer.h"
29 #include "pipeline/pipeline.h"
30 #include "video_decoder_adapter.h"
31 
32 namespace OHOS {
33 namespace Media {
34 class AVThumbnailGenerator : public NoCopyable {
35 public:
36     explicit AVThumbnailGenerator(std::shared_ptr<MediaDemuxer> &mediaDemuxer);
37     ~AVThumbnailGenerator();
38     std::shared_ptr<AVSharedMemory> FetchFrameAtTime(int64_t timeUs, int32_t option, const OutputConfiguration &param);
39     std::shared_ptr<AVBuffer> FetchFrameYuv(int64_t timeUs, int32_t option, const OutputConfiguration &param);
40     std::shared_ptr<AVSharedMemory> FetchArtPicture();
41 
42     void Reset();
43     void Destroy();
44     void OnEvent(const Event &event);
45     void OnCallback(std::shared_ptr<OHOS::Media::Pipeline::Filter> filter,
46         const OHOS::Media::Pipeline::FilterCallBackCommand cmd, OHOS::Media::Pipeline::StreamType outType);
47     void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode);
48     void OnOutputFormatChanged(const MediaAVCodec::Format &format);
49     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
50     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
51     void OnFetchedFrameBufferAvailable();
52 
53 private:
54     OutputConfiguration outputConfig_;
55     Format outputFormat_;
56     int64_t seekTime_{0};
57     std::atomic_bool hasFetchedFrame_{false};
58     std::atomic_bool stopProcessing_{false};
59     std::string trackMime_;
60     Plugins::VideoRotation rotation_ = Plugins::VideoRotation::VIDEO_ROTATION_0;
61     size_t trackIndex_{0};
62     std::shared_ptr<Meta> trackInfo_;
63     std::mutex mutex_;
64     std::condition_variable cond_;
65     std::atomic<uint32_t> bufferIndex_;
66     std::shared_ptr<AVBuffer> avBuffer_;
67     sptr<SurfaceBuffer> surfaceBuffer_;
68     std::shared_ptr<AVSharedMemoryBase> fetchedFrameAtTime_;
69     std::shared_ptr<OHOS::Media::MediaDemuxer> mediaDemuxer_;
70     std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> videoDecoder_;
71 
72     Status InitDecoder();
73     std::shared_ptr<Meta> GetVideoTrackInfo();
74     void ConvertToAVSharedMemory();
75     void ConvertP010ToNV12(
76         const sptr<SurfaceBuffer> &surfaceBuffer, uint8_t *dstNV12, int32_t strideWidth, int32_t strideHeight);
77     int32_t GetYuvDataAlignStride(const sptr<SurfaceBuffer> &surfaceBuffer);
78     Status SeekToTime(int64_t timeMs, Plugins::SeekMode option, int64_t realSeekTime);
79     int32_t width_ = 0;
80     int32_t height_ = 0;
81     double frameRate_ { 0.0 };
82     Plugins::SeekMode seekMode_ {};
83 
84     std::shared_ptr<AVBuffer> GenerateAlignmentAvBuffer();
85     std::shared_ptr<AVBuffer> GenerateAvBufferFromFCodec();
86     int32_t CopySurfaceBufferPixels(const sptr<SurfaceBuffer> &surfaceBuffer, std::shared_ptr<AVBuffer> &avBuffer);
87     void CopySurfaceBufferInfo(sptr<SurfaceBuffer> &source, sptr<SurfaceBuffer> &dst);
88     bool GetSbStaticMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &staticMetadata);
89     bool GetSbDynamicMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &dynamicMetadata);
90     bool SetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &staticMetadata);
91     bool SetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &dynamicMetadata);
92 
93     void PauseFetchFrame();
94 };
95 }  // namespace Media
96 }  // namespace OHOS
97 #endif  // AV_THUMBNAIL_GENERATOR