• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023-2025 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 <atomic>
20 #include <shared_mutex>
21 #include <unordered_map>
22 #include "avcodec_sysevent.h"
23 #include "codecbase.h"
24 #include "i_codec_service.h"
25 #include "nocopyable.h"
26 #include "codec_drm_decrypt.h"
27 #include "temporal_scalability.h"
28 #include "task_thread.h"
29 #include "codec_param_checker.h"
30 #include "lock_free_queue.h"
31 #include "post_processing.h"
32 #include "instance_info.h"
33 #include "adaptive_framerate_controller.h"
34 
35 namespace OHOS {
36 namespace MediaAVCodec {
37 class CodecServer : public std::enable_shared_from_this<CodecServer>,
38                     public ICodecService,
39                     public NoCopyable {
40 public:
41     static std::shared_ptr<ICodecService> Create(int32_t instanceId = INVALID_INSTANCE_ID);
42     CodecServer();
43     virtual ~CodecServer();
44 
45     enum CodecStatus {
46         UNINITIALIZED = 0,
47         INITIALIZED,
48         CONFIGURED,
49         RUNNING,
50         FLUSHED,
51         END_OF_STREAM,
52         ERROR,
53     };
54 
55     typedef struct {
56         std::shared_ptr<AVBuffer> inBuf;
57         std::shared_ptr<AVBuffer> outBuf;
58     } DrmDecryptVideoBuf;
59 
60     int32_t Init(AVCodecType type, bool isMimeType, const std::string &name,
61                  Meta &callerInfo, API_VERSION apiVersion = API_VERSION::API_VERSION_10) override;
62     int32_t Configure(const Format &format) override;
63     int32_t Start() override;
64     int32_t Stop() override;
65     int32_t Flush() override;
66     int32_t Reset() override;
67     int32_t Release() override;
68     int32_t GetChannelId(int32_t &channelId) override;
69     int32_t NotifyEos() override;
70     sptr<Surface> CreateInputSurface() override;
71     int32_t SetInputSurface(sptr<Surface> surface);
72     int32_t SetOutputSurface(sptr<Surface> surface) override;
73     int32_t SetLowPowerPlayerMode(bool isLpp) override;
74     int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
75     int32_t QueueInputBuffer(uint32_t index) override;
76     int32_t QueueInputParameter(uint32_t index) override;
77     int32_t GetOutputFormat(Format &format) override;
78     int32_t ReleaseOutputBuffer(uint32_t index, bool render) override;
79     int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs) override;
80     int32_t SetParameter(const Format &format) override;
81     int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) override;
82     int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override;
83     int32_t SetCallback(const std::shared_ptr<MediaCodecParameterCallback> &callback) override;
84     int32_t SetCallback(const std::shared_ptr<MediaCodecParameterWithAttrCallback> &callback) override;
85     int32_t GetInputFormat(Format &format) override;
86     int32_t ChangePlugin(const std::string &mime, bool isEncoder, const std::shared_ptr<Meta> &meta) override;
87     int32_t SetCodecCallback(const std::shared_ptr<MediaCodecCallback> &codecCallback) override;
88     void SetDumpInfo(bool isDump, uint64_t instanceId) override;
89 #ifdef SUPPORT_DRM
90     int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
91         const bool svpFlag) override;
92 #endif
93     int32_t SetCustomBuffer(std::shared_ptr<AVBuffer> buffer) override;
94     int32_t DumpInfo(int32_t fd);
95     void SetCallerInfo(const Meta &callerInfo);
96     std::shared_ptr<Media::Meta> GetCallerInfo();
97 
98     void OnError(int32_t errorType, int32_t errorCode);
99     void OnOutputFormatChanged(const Format &format);
100     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer);
101     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
102                                  std::shared_ptr<AVSharedMemory> buffer);
103 
104     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
105     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer);
106 
107     void OnOutputBufferBinded(std::map<uint32_t, sptr<SurfaceBuffer>> &bufferMap);
108     void OnOutputBufferUnbinded();
109 
110     int32_t Configure(const std::shared_ptr<Media::Meta> &meta) override;
111     int32_t SetParameter(const std::shared_ptr<Media::Meta> &parameter) override;
112     int32_t GetOutputFormat(std::shared_ptr<Media::Meta> &parameter) override;
113 
114     int32_t SetOutputBufferQueue(const sptr<Media::AVBufferQueueProducer> &bufferQueueProducer) override;
115     int32_t Prepare() override;
116     sptr<Media::AVBufferQueueProducer> GetInputBufferQueue() override;
117     sptr<Media::AVBufferQueueConsumer> GetInputBufferQueueConsumer() override;
118     sptr<Media::AVBufferQueueProducer> GetOutputBufferQueueProducer() override;
119     void ProcessInputBufferInner(bool isTriggeredByOutPort, bool isFlushed, uint32_t &bufferStatus) override;
120     void ProcessInputBuffer() override;
121     bool CheckRunning() override;
122     int32_t NotifyMemoryExchange(const bool exchangeFlag) override;
123 
124     // post processing callback
125     void PostProcessingOnError(int32_t errorCode);
126     void PostProcessingOnOutputBufferAvailable(uint32_t index, [[maybe_unused]] int32_t flag);
127     void PostProcessingOnOutputFormatChanged(const Format &format);
128 
129 #ifdef SUPPORT_DRM
130     int32_t SetAudioDecryptionConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession,
131         const bool svpFlag) override;
132 #endif
133 
134     int32_t NotifyMemoryRecycle();
135     int32_t NotifyMemoryWriteBack();
136     int32_t NotifySuspend();
137     int32_t NotifyResume();
138 
139 private:
140     int32_t InitByName(Meta &callerInfo, API_VERSION apiVersion);
141     int32_t InitByMime(Meta &callerInfo, API_VERSION apiVersion);
142     int32_t InitServer(int32_t instanceId = INVALID_INSTANCE_ID);
143     int32_t CodecScenarioInit(Format &config);
144     void StartInputParamTask();
145     const std::string &GetStatusDescription(OHOS::MediaAVCodec::CodecServer::CodecStatus status);
146     void StatusChanged(CodecStatus newStatus);
147     int32_t GetCodecDfxInfo(CodecDfxInfo &codecDfxInfo);
148     int32_t DrmVideoCencDecrypt(uint32_t index);
149     int32_t CheckDrmSvpConsistency(const sptr<DrmStandard::IMediaKeySessionService> &keySession, bool svpFlag);
150     void SetFreeStatus(bool isFree);
151     int32_t QueueInputBufferIn(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag);
152     int32_t ReleaseOutputBufferOfCodec(uint32_t index, bool render);
153     void OnInstanceMemoryUpdateEvent(std::shared_ptr<Media::Meta> meta = nullptr);
154     void OnInstanceMemoryResetEvent(std::shared_ptr<Media::Meta> meta = nullptr);
155     void InitFramerateCalculator(Meta &callerInfo);
156 
157     CodecStatus status_ = UNINITIALIZED;
158 
159     std::shared_ptr<CodecBase> codecBase_;
160     std::shared_ptr<AVCodecCallback> codecCb_;
161     std::shared_ptr<MediaCodecCallback> videoCb_;
162     std::shared_mutex mutex_;
163     std::shared_mutex cbMutex_;
164     std::string lastErrMsg_;
165     std::string codecMime_;
166     std::string codecName_;
167     AVCodecType codecType_ = AVCODEC_TYPE_NONE;
168     int32_t instanceId_ = INVALID_INSTANCE_ID;
169     CallerInfo caller_, forwardCaller_;
170     bool isSurfaceMode_ = false;
171     uint64_t surfaceId_ = -1;
172     int32_t pushBlankBufferOnShutdown_ = false;
173     bool isModeConfirmed_ = false;
174     bool isCreateSurface_ = false;
175     bool isSetParameterCb_ = false;
176     bool isLpp_ = false;
177     std::shared_ptr<TemporalScalability> temporalScalability_ = nullptr;
178     std::shared_ptr<CodecDrmDecrypt> drmDecryptor_ = nullptr;
179     std::unordered_map<uint32_t, DrmDecryptVideoBuf> decryptVideoBufs_;
180     std::shared_mutex freeMutex_;
181     bool isFree_ = false;
182     std::shared_ptr<TaskThread> inputParamTask_ = nullptr;
183     CodecScenario scenario_ = CodecScenario::CODEC_SCENARIO_ENC_NORMAL;
184 
185     // post processing, video decoder and surface mode only
186     int32_t SetCallbackForPostProcessing();
187     void ClearCallbackForPostProcessing();
188     int32_t CreatePostProcessing(const Format& format);
189     int32_t SetOutputSurfaceForPostProcessing(sptr<Surface> surface);
190     int32_t PreparePostProcessing();
191     int32_t StartPostProcessing();
192     int32_t StopPostProcessing();
193     int32_t FlushPostProcessing();
194     int32_t ResetPostProcessing();
195     int32_t ReleasePostProcessing();
196     int32_t GetPostProcessingOutputFormat(Format& format);
197     int32_t ReleaseOutputBufferOfPostProcessing(uint32_t index, bool render);
198     int32_t PushDecodedBufferInfo(uint32_t index, std::shared_ptr<AVBuffer> buffer);
199     int32_t StartPostProcessingTask();
200     void PostProcessingTask();
201     void DeactivatePostProcessingQueue();
202     void CleanPostProcessingResource();
203     using PostProcessingType = PostProcessing::DynamicPostProcessing;
204     std::unique_ptr<PostProcessingType> postProcessing_{nullptr};
205     void* postProcessingUserData_{nullptr};
206     PostProcessing::Callback postProcessingCallback_;
207     struct DecodedBufferInfo {
208         uint32_t index;
209         std::shared_ptr<AVBuffer> buffer;
210     };
211     using DecodedBufferInfoQueue = LockFreeQueue<std::shared_ptr<DecodedBufferInfo>, 20>;       // 20: QueueSize
212     using PostProcessingBufferInfoQueue = LockFreeQueue<std::shared_ptr<DecodedBufferInfo>, 8>; // 8: QueueSize
213     std::shared_ptr<DecodedBufferInfoQueue> decodedBufferInfoQueue_{nullptr};
214     std::shared_ptr<PostProcessingBufferInfoQueue> postProcessingInputBufferInfoQueue_{nullptr};
215     std::unique_ptr<TaskThread> postProcessingTask_{nullptr};
216     Format outputFormatChanged_;
217     std::shared_ptr<AVCodecCallback> shareBufCallback_ = nullptr;
218     std::shared_ptr<MediaCodecCallback> avBufCallback_ = nullptr;
219     std::shared_ptr<FramerateCalculator> framerateCalculator_ = nullptr;
220 };
221 
222 class CodecBaseCallback : public AVCodecCallback, public NoCopyable {
223 public:
224     explicit CodecBaseCallback(const std::shared_ptr<CodecServer> &codec);
225     virtual ~CodecBaseCallback();
226 
227     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
228     void OnOutputFormatChanged(const Format &format) override;
229     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer) override;
230     void OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
231                                  std::shared_ptr<AVSharedMemory> buffer) override;
232 
233 private:
234     std::shared_ptr<CodecServer> codec_ = nullptr;
235 };
236 
237 class VCodecBaseCallback : public MediaCodecCallback, public NoCopyable {
238 public:
239     explicit VCodecBaseCallback(const std::shared_ptr<CodecServer> &codec);
240     virtual ~VCodecBaseCallback();
241 
242     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
243     void OnOutputFormatChanged(const Format &format) override;
244     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
245     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
246     void OnOutputBufferBinded(std::map<uint32_t, sptr<SurfaceBuffer>> &bufferMap) override;
247     void OnOutputBufferUnbinded() override;
248 
249 private:
250     std::shared_ptr<CodecServer> codec_ = nullptr;
251 };
252 } // namespace MediaAVCodec
253 } // namespace OHOS
254 #endif // CODEC_SERVER_H