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 CODEC_SERVER_H 17 #define CODEC_SERVER_H 18 19 #include <shared_mutex> 20 #include <unordered_map> 21 #include "avcodec_sysevent.h" 22 #include "codecbase.h" 23 #include "i_codec_service.h" 24 #include "nocopyable.h" 25 #include "codec_drm_decrypt.h" 26 27 28 namespace OHOS { 29 namespace MediaAVCodec { 30 class CodecServer : public std::enable_shared_from_this<CodecServer>, public ICodecService, public NoCopyable { 31 public: 32 static std::shared_ptr<ICodecService> Create(); 33 CodecServer(); 34 virtual ~CodecServer(); 35 36 enum CodecStatus { 37 UNINITIALIZED = 0, 38 INITIALIZED, 39 CONFIGURED, 40 RUNNING, 41 FLUSHED, 42 END_OF_STREAM, 43 ERROR, 44 }; 45 46 enum CodecType { 47 CODEC_TYPE_DEFAULT = 0, 48 CODEC_TYPE_VIDEO, 49 CODEC_TYPE_AUDIO 50 }; 51 52 typedef struct { 53 std::shared_ptr<AVBuffer> inBuf; 54 std::shared_ptr<AVBuffer> outBuf; 55 } DrmDecryptVideoBuf; 56 57 int32_t Init(AVCodecType type, bool isMimeType, const std::string &name, 58 API_VERSION apiVersion = API_VERSION::API_VERSION_10) override; 59 int32_t Configure(const Format &format) override; 60 int32_t Start() override; 61 int32_t Stop() override; 62 int32_t Flush() override; 63 int32_t Reset() override; 64 int32_t Release() override; 65 int32_t NotifyEos() override; 66 sptr<Surface> CreateInputSurface() override; 67 int32_t SetInputSurface(sptr<Surface> surface); 68 int32_t SetOutputSurface(sptr<Surface> surface) override; 69 int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 70 int32_t QueueInputBuffer(uint32_t index) override; 71 int32_t GetOutputFormat(Format &format) override; 72 int32_t ReleaseOutputBuffer(uint32_t index, bool render) override; 73 int32_t SetParameter(const Format &format) override; 74 int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) override; 75 int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override; 76 int32_t GetInputFormat(Format &format) override; 77 #ifdef SUPPORT_DRM 78 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, 79 const bool svpFlag) override; 80 #endif 81 int32_t DumpInfo(int32_t fd); 82 int32_t SetClientInfo(int32_t clientPid, int32_t clientUid); 83 84 void OnError(int32_t errorType, int32_t errorCode); 85 void OnOutputFormatChanged(const Format &format); 86 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer); 87 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 88 std::shared_ptr<AVSharedMemory> buffer); 89 90 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 91 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer); 92 93 int32_t Configure(const std::shared_ptr<Media::Meta> &meta) override; 94 int32_t SetParameter(const std::shared_ptr<Media::Meta> ¶meter) override; 95 int32_t GetOutputFormat(std::shared_ptr<Media::Meta> ¶meter) override; 96 97 int32_t SetOutputBufferQueue(const sptr<Media::AVBufferQueueProducer> &bufferQueueProducer) override; 98 int32_t Prepare() override; 99 sptr<Media::AVBufferQueueProducer> GetInputBufferQueue() override; 100 void ProcessInputBuffer() override; 101 bool GetStatus() override; 102 private: 103 int32_t InitServer(); 104 void ExitProcessor(); 105 const std::string &GetStatusDescription(OHOS::MediaAVCodec::CodecServer::CodecStatus status); 106 void StatusChanged(CodecStatus newStatus); 107 CodecType GetCodecType(); 108 int32_t GetCodecDfxInfo(CodecDfxInfo &codecDfxInfo); 109 void DrmVideoCencDecrypt(uint32_t index); 110 void SetFreeStatus(bool isFree); 111 int32_t QueueInputBufferIn(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag); 112 113 CodecStatus status_ = UNINITIALIZED; 114 115 std::shared_ptr<CodecBase> codecBase_; 116 std::shared_ptr<AVCodecCallback> codecCb_; 117 std::shared_ptr<MediaCodecCallback> videoCb_; 118 std::shared_mutex mutex_; 119 std::shared_mutex cbMutex_; 120 Format config_; 121 std::string lastErrMsg_; 122 std::string codecName_; 123 AVCodecType codecType_ = AVCODEC_TYPE_NONE; 124 bool isStarted_ = false; 125 uint32_t clientPid_ = 0; 126 uint32_t clientUid_ = 0; 127 bool isSurfaceMode_ = false; 128 std::shared_ptr<CodecDrmDecrypt> drmDecryptor_ = nullptr; 129 std::unordered_map<uint32_t, DrmDecryptVideoBuf> decryptVideoBufs_; 130 std::shared_mutex freeMutex_; 131 bool isFree_ = false; 132 }; 133 134 class CodecBaseCallback : public AVCodecCallback, public NoCopyable { 135 public: 136 explicit CodecBaseCallback(const std::shared_ptr<CodecServer> &codec); 137 virtual ~CodecBaseCallback(); 138 139 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 140 void OnOutputFormatChanged(const Format &format) override; 141 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override; 142 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 143 std::shared_ptr<AVSharedMemory> buffer) override; 144 145 private: 146 std::shared_ptr<CodecServer> codec_ = nullptr; 147 }; 148 149 class VCodecBaseCallback : public MediaCodecCallback, public NoCopyable { 150 public: 151 explicit VCodecBaseCallback(const std::shared_ptr<CodecServer> &codec); 152 virtual ~VCodecBaseCallback(); 153 154 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 155 void OnOutputFormatChanged(const Format &format) override; 156 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 157 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 158 159 private: 160 std::shared_ptr<CodecServer> codec_ = nullptr; 161 }; 162 } // namespace MediaAVCodec 163 } // namespace OHOS 164 #endif // CODEC_SERVER_H