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