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