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