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 VIDEO_DECODER_ADAPTER_H 17 #define VIDEO_DECODER_ADAPTER_H 18 19 #include <shared_mutex> 20 #include <vector> 21 #include "surface.h" 22 #include "avcodec_video_decoder.h" 23 #include "buffer/avbuffer.h" 24 #include "buffer/avbuffer_queue.h" 25 #include "buffer/avbuffer_queue_producer.h" 26 #include "osal/task/condition_variable.h" 27 #include "meta/format.h" 28 #include "video_sink.h" 29 30 namespace OHOS { 31 namespace Media { 32 class VideoDecoderAdapter : public std::enable_shared_from_this<VideoDecoderAdapter> { 33 public: 34 VideoDecoderAdapter(); 35 virtual ~VideoDecoderAdapter(); 36 37 Status Init(MediaAVCodec::AVCodecType type, bool isMimeType, const std::string &name); 38 Status Configure(const Format &format); 39 int32_t SetParameter(const Format &format); 40 Status Start(); 41 Status Flush(); 42 Status Stop(); 43 Status Reset(); 44 Status Release(); 45 int32_t SetCallback(const std::shared_ptr<MediaAVCodec::MediaCodecCallback> &callback); 46 47 void PrepareInputBufferQueue(); 48 sptr<AVBufferQueueProducer> GetBufferQueueProducer(); 49 sptr<AVBufferQueueConsumer> GetBufferQueueConsumer(); 50 51 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 52 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode); 53 void OnOutputFormatChanged(const MediaAVCodec::Format &format); 54 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 55 int32_t ReleaseOutputBuffer(uint32_t index, bool render); 56 int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs); 57 void AquireAvailableInputBuffer(); 58 int32_t SetOutputSurface(sptr<Surface> videoSurface); 59 int32_t GetOutputFormat(Format &format); 60 void SetEventReceiver(const std::shared_ptr<Pipeline::EventReceiver>& receiver); 61 62 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, 63 const bool svpFlag); 64 void OnDumpInfo(int32_t fd); 65 66 void SetCallingInfo(int32_t appUid, int32_t appPid, const std::string& bundleName, uint64_t instanceId); 67 void ResetRenderTime(); 68 private: 69 std::shared_ptr<Media::AVBufferQueue> inputBufferQueue_; 70 sptr<Media::AVBufferQueueProducer> inputBufferQueueProducer_; 71 sptr<Media::AVBufferQueueConsumer> inputBufferQueueConsumer_; 72 73 std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> mediaCodec_; 74 std::shared_ptr<MediaAVCodec::MediaCodecCallback> callback_; 75 std::shared_ptr<AVBuffer> buffer_; 76 std::string mediaCodecName_; 77 std::shared_ptr<Pipeline::EventReceiver> eventReceiver_ {nullptr}; 78 std::mutex mutex_; 79 std::vector<std::shared_ptr<AVBuffer>> bufferVector_; 80 int64_t currentTime_ = 0; 81 bool isConfigured_ {false}; 82 uint64_t instanceId_ = 0; 83 int32_t appUid_ = -1; 84 int32_t appPid_ = -1; 85 std::string bundleName_; 86 }; 87 88 class VideoDecoderCallback : public OHOS::MediaAVCodec::MediaCodecCallback { 89 public: 90 explicit VideoDecoderCallback(std::shared_ptr<VideoDecoderAdapter> videoDecoder); 91 virtual ~VideoDecoderCallback(); 92 93 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode); 94 void OnOutputFormatChanged(const MediaAVCodec::Format &format); 95 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 96 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 97 98 private: 99 std::weak_ptr<VideoDecoderAdapter> videoDecoderAdapter_; 100 }; 101 } // namespace Media 102 } // namespace OHOS 103 #endif // VIDEO_DECODER_ADAPTER_H 104