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 ¶m); 39 std::shared_ptr<AVBuffer> FetchFrameYuv(int64_t timeUs, int32_t option, const OutputConfiguration ¶m); 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::atomic_bool readErrorFlag_{ false }; 60 std::string trackMime_; 61 Plugins::VideoRotation rotation_ = Plugins::VideoRotation::VIDEO_ROTATION_0; 62 size_t trackIndex_{0}; 63 std::shared_ptr<Meta> trackInfo_; 64 std::mutex mutex_; 65 std::condition_variable cond_; 66 std::atomic<uint32_t> bufferIndex_; 67 std::shared_ptr<AVBuffer> avBuffer_; 68 sptr<SurfaceBuffer> surfaceBuffer_; 69 std::shared_ptr<AVSharedMemoryBase> fetchedFrameAtTime_; 70 std::shared_ptr<OHOS::Media::MediaDemuxer> mediaDemuxer_; 71 std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> videoDecoder_; 72 73 Status InitDecoder(); 74 std::shared_ptr<Meta> GetVideoTrackInfo(); 75 void ConvertToAVSharedMemory(); 76 void ConvertP010ToNV12( 77 const sptr<SurfaceBuffer> &surfaceBuffer, uint8_t *dstNV12, int32_t strideWidth, int32_t strideHeight); 78 int32_t GetYuvDataAlignStride(const sptr<SurfaceBuffer> &surfaceBuffer); 79 Status SeekToTime(int64_t timeMs, Plugins::SeekMode option, int64_t realSeekTime); 80 int32_t width_ = 0; 81 int32_t height_ = 0; 82 double frameRate_ { 0.0 }; 83 Plugins::SeekMode seekMode_ {}; 84 85 std::shared_ptr<AVBuffer> GenerateAlignmentAvBuffer(); 86 std::shared_ptr<AVBuffer> GenerateAvBufferFromFCodec(); 87 int32_t CopySurfaceBufferPixels(const sptr<SurfaceBuffer> &surfaceBuffer, std::shared_ptr<AVBuffer> &avBuffer); 88 void CopySurfaceBufferInfo(sptr<SurfaceBuffer> &source, sptr<SurfaceBuffer> &dst); 89 bool GetSbStaticMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &staticMetadata); 90 bool GetSbDynamicMetadata(const sptr<SurfaceBuffer> &buffer, std::vector<uint8_t> &dynamicMetadata); 91 bool SetSbStaticMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &staticMetadata); 92 bool SetSbDynamicMetadata(sptr<SurfaceBuffer> &buffer, const std::vector<uint8_t> &dynamicMetadata); 93 94 void HandleFetchFrameYuvRes(); 95 void HandleFetchFrameYuvFailed(); 96 void HandleFetchFrameAtTimeRes(); 97 98 void PauseFetchFrame(); 99 }; 100 } // namespace Media 101 } // namespace OHOS 102 #endif // AV_THUMBNAIL_GENERATOR