• 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_CLIENT_H
17 #define CODEC_CLIENT_H
18 
19 #include <shared_mutex>
20 #include "avcodec_log.h"
21 #include "buffer_converter.h"
22 #include "codec_buffer_circular.h"
23 #include "codec_listener_stub.h"
24 #include "i_codec_service.h"
25 #include "i_standard_codec_service.h"
26 
27 namespace OHOS {
28 namespace MediaAVCodec {
29 using namespace Media;
30 class CodecClientCallback;
31 class CodecBufferCircular;
32 class CodecClient : public MediaCodecCallback, public ICodecService, public std::enable_shared_from_this<CodecClient> {
33 public:
34     static int32_t Create(const sptr<IStandardCodecService> &ipcProxy, std::shared_ptr<ICodecService> &codec);
35     explicit CodecClient(const sptr<IStandardCodecService> &ipcProxy);
36     ~CodecClient();
37     // 业务
38     int32_t Init(AVCodecType type, bool isMimeType, const std::string &name, Media::Meta &callerInfo,
39                  API_VERSION apiVersion = API_VERSION::API_VERSION_10) override;
40     int32_t Configure(const Format &format) override;
41     int32_t Prepare() override;
42     int32_t Start() override;
43     int32_t Stop() override;
44     int32_t Flush() override;
45     int32_t Reset() override;
46     int32_t Release() override;
47     int32_t GetChannelId(int32_t &channelId) override;
48     int32_t NotifyEos() override;
49     sptr<Surface> CreateInputSurface() override;
50     int32_t SetOutputSurface(sptr<Surface> surface) override;
51     int32_t SetLowPowerPlayerMode(bool isLpp) override;
52     int32_t QueueInputBuffer(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag) override;
53     int32_t QueueInputBuffer(uint32_t index) override;
54     int32_t QueueInputParameter(uint32_t index) override;
55     int32_t GetOutputFormat(Format &format) override;
56     int32_t ReleaseOutputBuffer(uint32_t index, bool render) override;
57     int32_t RenderOutputBufferAtTime(uint32_t index, int64_t renderTimestampNs) override;
58     int32_t QueryInputBuffer(uint32_t &index, int64_t timeoutUs) override;
59     int32_t QueryOutputBuffer(uint32_t &index, int64_t timeoutUs) override;
60     std::shared_ptr<AVBuffer> GetInputBuffer(uint32_t index) override;
61     std::shared_ptr<AVBuffer> GetOutputBuffer(uint32_t index) override;
62     int32_t SetParameter(const Format &format) override;
63     int32_t SetCallback(const std::shared_ptr<AVCodecCallback> &callback) override;
64     int32_t SetCallback(const std::shared_ptr<MediaCodecCallback> &callback) override;
65     int32_t SetCallback(const std::shared_ptr<MediaCodecParameterCallback> &callback) override;
66     int32_t SetCallback(const std::shared_ptr<MediaCodecParameterWithAttrCallback> &callback) override;
67     int32_t GetInputFormat(Format &format) override;
68 #ifdef SUPPORT_DRM
69     int32_t SetDecryptConfig(const sptr<DrmStandard::IMediaKeySessionService> &keySession, const bool svpFlag) override;
70 #endif
71     int32_t SetCustomBuffer(std::shared_ptr<AVBuffer> buffer) override;
72     int32_t NotifyMemoryExchange(const bool exchangeFlag) override;
73 
74     void AVCodecServerDied();
75 
76     void OnError(AVCodecErrorType errorType, int32_t errorCode) override;
77     void OnOutputFormatChanged(const Format &format) override;
78     void OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
79     void OnOutputBufferAvailable(uint32_t index, std::shared_ptr<AVBuffer> buffer) override;
80     void OnOutputBufferBinded(std::map<uint32_t, sptr<SurfaceBuffer>> &bufferMap) override;
81     void OnOutputBufferUnbinded() override;
82 
83 private:
84     int32_t CreateListenerObject();
85     void UpdateGeneration();
86     void UpdateFormat(Format &format);
87     void SetNeedListen(const bool needListen);
88     typedef enum : uint8_t {
89         MEMORY_CALLBACK = 1,
90         BUFFER_CALLBACK,
91         INVALID_CALLBACK,
92     } CallbackMode;
93 
94     typedef enum : uint8_t {
95         CODEC_DEFAULT_MODE = 0,
96         CODEC_SURFACE_INPUT = 1 << 0,
97         CODEC_SURFACE_OUTPUT = 1 << 1,
98         CODEC_ENABLE_PARAMETER = 1 << 2,
99         CODEC_SURFACE_MODE_WITH_PARAMETER = CODEC_SURFACE_INPUT | CODEC_ENABLE_PARAMETER,
100     } CodecMode;
101 
102     bool isConfigured_ = false;
103     uint8_t callbackMode_ = INVALID_CALLBACK;
104     uint8_t codecMode_ = CODEC_DEFAULT_MODE;
105     AVCodecType type_ = AVCODEC_TYPE_NONE;
106     sptr<IStandardCodecService> codecProxy_ = nullptr;
107     sptr<CodecListenerStub> listenerStub_ = nullptr;
108     std::shared_ptr<BufferConverter> converter_ = nullptr;
109     CodecBufferCircular circular_;
110 
111     std::shared_mutex mutex_;
112     std::shared_ptr<std::recursive_mutex> syncMutex_ = nullptr;
113     std::atomic<bool> needUpdateGeneration_ = true;
114 };
115 } // namespace MediaAVCodec
116 } // namespace OHOS
117 #endif // CODEC_CLIENT_H
118