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_CLIENT_H 17 #define CODEC_CLIENT_H 18 19 #include <shared_mutex> 20 #include "avcodec_log.h" 21 #include "buffer_converter.h" 22 #include "codec_listener_stub.h" 23 #include "i_codec_service.h" 24 #include "i_standard_codec_service.h" 25 26 namespace OHOS { 27 namespace MediaAVCodec { 28 class CodecClientCallback; 29 class CodecClient : public MediaCodecCallback, 30 public AVCodecCallback, 31 public MediaCodecParameterCallback, 32 public MediaCodecParameterWithAttrCallback, 33 public ICodecService, 34 public std::enable_shared_from_this<CodecClient> { 35 public: 36 static int32_t Create(const sptr<IStandardCodecService> &ipcProxy, std::shared_ptr<ICodecService> &codec); 37 explicit CodecClient(const sptr<IStandardCodecService> &ipcProxy); 38 ~CodecClient(); 39 // 业务 40 int32_t Init(AVCodecType type, bool isMimeType, const std::string &name, 41 Media::Meta &callerInfo, API_VERSION apiVersion = API_VERSION::API_VERSION_10) override; 42 int32_t Configure(const Format &format) override; 43 int32_t Prepare() override; 44 int32_t Start() override; 45 int32_t Stop() override; 46 int32_t Flush() override; 47 int32_t Reset() override; 48 int32_t Release() override; 49 int32_t NotifyEos() override; 50 sptr<Surface> CreateInputSurface() override; 51 int32_t SetOutputSurface(sptr<Surface> surface) override; 52 int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 53 int32_t QueueInputBuffer(uint32_t index) override; 54 int32_t QueueInputParameter(uint32_t index) override; 55 int32_t GetOutputFormat(Format &format) override; 56 int32_t ReleaseOutputBuffer(uint32_t index, bool render) override; 57 int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs) override; 58 int32_t SetParameter(const Format &format) override; 59 int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) override; 60 int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override; 61 int32_t SetCallback(const std::shared_ptr<MediaCodecParameterCallback> &callback) override; 62 int32_t SetCallback(const std::shared_ptr<MediaCodecParameterWithAttrCallback> &callback) override; 63 int32_t GetInputFormat(Format &format) override; 64 #ifdef SUPPORT_DRM 65 int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, const bool svpFlag) override; 66 #endif 67 int32_t SetCustomBuffer(std::shared_ptr<AVBuffer> buffer) override; 68 69 void AVCodecServerDied(); 70 71 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 72 void OnOutputFormatChanged(const Format &format) override; 73 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override; 74 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 75 std::shared_ptr<AVSharedMemory> buffer) override; 76 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 77 void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override; 78 void OnInputParameterAvailable(uint32_t index, std::shared_ptr<Format> parameter) override; 79 void OnInputParameterWithAttrAvailable(uint32_t index, std::shared_ptr<Format> attribute, 80 std::shared_ptr<Format> parameter) override; 81 82 private: 83 int32_t CreateListenerObject(); 84 void UpdateGeneration(); 85 void UpdateFormat(Format &format); 86 void SetNeedListen(const bool needListen); 87 void InitLabel(AVCodecType type); 88 typedef enum : uint32_t { 89 MEMORY_CALLBACK = 1, 90 BUFFER_CALLBACK, 91 INVALID_CALLBACK, 92 } CallbackMode; 93 94 typedef enum : uint32_t { 95 CODEC_BUFFER_MODE = 0, 96 CODEC_SURFACE_MODE = 1, 97 CODEC_SET_PARAMETER_CALLBACK = 1 << 1, 98 CODEC_SURFACE_MODE_WITH_SETPARAMETER = CODEC_SURFACE_MODE | CODEC_SET_PARAMETER_CALLBACK, 99 } CodecMode; 100 101 bool hasOnceConfigured_ = false; 102 uint32_t callbackMode_ = INVALID_CALLBACK; 103 uint32_t codecMode_ = CODEC_BUFFER_MODE; 104 AVCodecType type_ = AVCODEC_TYPE_NONE; 105 sptr<IStandardCodecService> codecProxy_ = nullptr; 106 sptr<CodecListenerStub> listenerStub_ = nullptr; 107 std::shared_ptr<AVCodecCallback> callback_ = nullptr; 108 std::shared_ptr<MediaCodecCallback> videoCallback_ = nullptr; 109 std::shared_ptr<MediaCodecParameterCallback> paramCallback_ = nullptr; 110 std::shared_ptr<MediaCodecParameterWithAttrCallback> paramWithAttrCallback_ = nullptr; 111 std::shared_ptr<BufferConverter> converter_ = nullptr; 112 113 std::shared_mutex mutex_; 114 std::shared_ptr<std::recursive_mutex> syncMutex_ = nullptr; 115 std::atomic<bool> needUpdateGeneration_ = true; 116 117 const OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_FRAMEWORK, "CodecClient"}; 118 uint64_t uid_ = 0; 119 std::string tag_ = ""; 120 }; 121 } // namespace MediaAVCodec 122 } // namespace OHOS 123 #endif // CODEC_CLIENT_H 124