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, std::string bundleName, uint64_t instanceId); 67 68 int64_t GetCurrentMillisecond(); 69 Status GetLagInfo(int32_t& lagTimes, int32_t& maxLagDuration, int32_t& avgLagDuration); 70 void ResetRenderTime(); 71 private: 72 std::shared_ptr<Media::AVBufferQueue> inputBufferQueue_; 73 sptr<Media::AVBufferQueueProducer> inputBufferQueueProducer_; 74 sptr<Media::AVBufferQueueConsumer> inputBufferQueueConsumer_; 75 76 std::shared_ptr<MediaAVCodec::AVCodecVideoDecoder> mediaCodec_; 77 std::shared_ptr<MediaAVCodec::MediaCodecCallback> callback_; 78 std::shared_ptr<AVBuffer> buffer_; 79 std::string mediaCodecName_; 80 81 std::shared_ptr<Pipeline::EventReceiver> eventReceiver_ {nullptr}; 82 83 std::mutex mutex_; 84 std::vector<std::shared_ptr<AVBuffer>> bufferVector_; 85 int32_t lagTimes_ = 0; 86 int64_t currentTime_ = 0; 87 int64_t maxLagDuration_ = 0; 88 int64_t totalLagDuration_ = 0; 89 bool isConfigured_ {false}; 90 uint64_t instanceId_ = 0; 91 int32_t appUid_ = -1; 92 int32_t appPid_ = -1; 93 std::string bundleName_; 94 }; 95 96 class VideoDecoderCallback : public OHOS::MediaAVCodec::MediaCodecCallback { 97 public: 98 explicit VideoDecoderCallback(std::shared_ptr<VideoDecoderAdapter> videoDecoder); 99 virtual ~VideoDecoderCallback(); 100 101 void OnError(MediaAVCodec::AVCodecErrorType errorType, int32_t errorCode); 102 void OnOutputFormatChanged(const MediaAVCodec::Format &format); 103 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 104 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 105 106 private: 107 std::weak_ptr<VideoDecoderAdapter> videoDecoderAdapter_; 108 }; 109 } // namespace Media 110 } // namespace OHOS 111 #endif // VIDEO_DECODER_ADAPTER_H 112