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 "codecbase.h" 21 #include "i_codec_service.h" 22 #include "nocopyable.h" 23 24 25 namespace OHOS { 26 namespace MediaAVCodec { 27 class CodecServer : public std::enable_shared_from_this<CodecServer>, public ICodecService, public NoCopyable { 28 public: 29 static std::shared_ptr<ICodecService> Create(); 30 CodecServer(); 31 virtual ~CodecServer(); 32 33 enum CodecStatus { 34 UNINITIALIZED = 0, 35 INITIALIZED, 36 CONFIGURED, 37 RUNNING, 38 FLUSHED, 39 END_OF_STREAM, 40 ERROR, 41 }; 42 43 enum CodecType { 44 CODEC_TYPE_DEFAULT = 0, 45 CODEC_TYPE_VIDEO, 46 CODEC_TYPE_AUDIO 47 }; 48 49 int32_t Init(AVCodecType type, bool isMimeType, const std::string &name) override; 50 int32_t Configure(const Format &format) override; 51 int32_t Start() override; 52 int32_t Stop() override; 53 int32_t Flush() override; 54 int32_t Reset() override; 55 int32_t Release() override; 56 int32_t NotifyEos() override; 57 sptr<Surface> CreateInputSurface() override; 58 int32_t SetOutputSurface(sptr<Surface> surface) override; 59 int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override; 60 int32_t GetOutputFormat(Format &format) override; 61 int32_t ReleaseOutputBuffer(uint32_t index, bool render) override; 62 int32_t SetParameter(const Format &format) override; 63 int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) override; 64 int32_t GetInputFormat(Format &format) override; 65 int32_t DumpInfo(int32_t fd); 66 67 void OnError(int32_t errorType, int32_t errorCode); 68 void OnOutputFormatChanged(const Format &format); 69 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer); 70 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 71 std::shared_ptr<AVSharedMemory> buffer); 72 73 private: 74 int32_t InitServer(); 75 void ExitProcessor(); 76 const std::string &GetStatusDescription(OHOS::MediaAVCodec::CodecServer::CodecStatus status); 77 CodecType GetCodecType(); 78 79 CodecStatus status_ = UNINITIALIZED; 80 81 std::shared_ptr<CodecBase> codecBase_; 82 std::shared_ptr<AVCodecCallback> codecCb_; 83 std::shared_mutex mutex_; 84 std::shared_mutex cbMutex_; 85 Format config_; 86 std::string lastErrMsg_; 87 std::string codecName_; 88 bool isFirstFrameIn_ = true; 89 bool isFirstFrameOut_ = true; 90 }; 91 92 class CodecBaseCallback : public AVCodecCallback, public NoCopyable { 93 public: 94 explicit CodecBaseCallback(const std::shared_ptr<CodecServer> &codec); 95 virtual ~CodecBaseCallback(); 96 97 void OnError(AVCodecErrorType errorType, int32_t errorCode) override; 98 void OnOutputFormatChanged(const Format &format) override; 99 void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override; 100 void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag, 101 std::shared_ptr<AVSharedMemory> buffer) override; 102 private: 103 std::shared_ptr<CodecServer> codec_ = nullptr; 104 }; 105 } // namespace MediaAVCodec 106 } // namespace OHOS 107 #endif // CODEC_SERVER_H