1 /* 2 * Copyright (c) 2023-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 MODULES_MEDIA_CODEC_H 17 #define MODULES_MEDIA_CODEC_H 18 19 #include <cstring> 20 #include "surface.h" 21 #include "meta/meta.h" 22 #include "buffer/avbuffer.h" 23 #include "buffer/avallocator.h" 24 #include "buffer/avbuffer_queue.h" 25 #include "buffer/avbuffer_queue_producer.h" 26 #include "buffer/avbuffer_queue_consumer.h" 27 #include "common/status.h" 28 #include "common/log.h" 29 #include "plugin/plugin_event.h" 30 #include "plugin/codec_plugin.h" 31 #include "osal/task/mutex.h" 32 33 namespace OHOS { 34 namespace Media { 35 enum class CodecState : int32_t { 36 UNINITIALIZED, 37 INITIALIZED, 38 CONFIGURED, 39 PREPARED, 40 RUNNING, 41 FLUSHED, 42 END_OF_STREAM, 43 44 INITIALIZING, // RELEASED -> INITIALIZED 45 STARTING, // INITIALIZED -> RUNNING 46 STOPPING, // RUNNING -> INITIALIZED 47 FLUSHING, // RUNNING -> FLUSHED 48 RESUMING, // FLUSHED -> RUNNING 49 RELEASING, // {ANY EXCEPT RELEASED} -> RELEASED 50 51 ERROR, 52 }; 53 54 enum class CodecErrorType : int32_t { 55 CODEC_ERROR_INTERNAL, 56 CODEC_ERROR_EXTEND_START = 0X10000, 57 }; 58 59 class CodecCallback { 60 public: 61 virtual ~CodecCallback() = default; 62 63 virtual void OnError(CodecErrorType errorType, int32_t errorCode) = 0; 64 65 virtual void OnOutputFormatChanged(const std::shared_ptr<Meta> &format) = 0; 66 }; 67 68 class MediaCodec : public Plugins::DataCallback { 69 public: 70 MediaCodec() = default; 71 72 int32_t Init(const std::string &mime, bool isEncoder); 73 74 int32_t Init(const std::string &name); 75 76 int32_t Configure(const std::shared_ptr<Meta> &meta); 77 78 int32_t SetOutputBufferQueue(const sptr<AVBufferQueueProducer> &bufferQueueProducer); 79 80 int32_t SetCodecCallback(const std::shared_ptr<CodecCallback> &codecCallback); 81 82 int32_t SetOutputSurface(sptr<Surface> surface); 83 84 int32_t Prepare(); 85 86 sptr<AVBufferQueueProducer> GetInputBufferQueue(); 87 88 sptr<Surface> GetInputSurface(); 89 90 int32_t Start(); 91 92 int32_t Stop(); 93 94 int32_t Flush(); 95 96 int32_t Reset(); 97 98 int32_t Release(); 99 100 int32_t NotifyEos(); 101 102 int32_t SetParameter(const std::shared_ptr<Meta> ¶meter); 103 104 int32_t GetOutputFormat(std::shared_ptr<Meta> ¶meter); 105 106 void ProcessInputBuffer(); 107 108 private: 109 std::shared_ptr<Plugins::CodecPlugin> CreatePlugin(Plugins::PluginType pluginType); 110 std::shared_ptr<Plugins::CodecPlugin> CreatePlugin(const std::string &mime, Plugins::PluginType pluginType); 111 Status AttachBufffer(); 112 Status HandleOutputBuffer(uint32_t eosStatus); 113 114 int32_t PrepareInputBufferQueue(); 115 116 int32_t PrepareOutputBufferQueue(); 117 118 void ClearInputBuffer(); 119 120 void OnInputBufferDone(const std::shared_ptr<AVBuffer> &inputBuffer) override; 121 122 void OnOutputBufferDone(const std::shared_ptr<AVBuffer> &outputBuffer) override; 123 124 void OnEvent(const std::shared_ptr<Plugins::PluginEvent> event) override; 125 126 std::string StateToString(CodecState state); 127 128 private: 129 std::shared_ptr<Plugins::CodecPlugin> codecPlugin_; 130 std::shared_ptr<AVBufferQueue> inputBufferQueue_; 131 sptr<AVBufferQueueProducer> inputBufferQueueProducer_; 132 sptr<AVBufferQueueConsumer> inputBufferQueueConsumer_; 133 sptr<AVBufferQueueProducer> outputBufferQueueProducer_; 134 std::shared_ptr<CodecCallback> codecCallback_; 135 AVBufferConfig outputBufferConfig_; 136 bool isEncoder_ = false; 137 bool isSurfaceMode_ = false; 138 bool isBufferMode_ = false; 139 int32_t outputBufferCapacity_ = 0; 140 141 std::atomic<CodecState> state_ = CodecState::UNINITIALIZED; 142 Mutex stateMutex_; 143 }; 144 } // namespace Media 145 } // namespace OHOS 146 #endif // MODULES_MEDIA_CODEC_H 147