• 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 MEDIA_CODEC_ADAPTER_H
17 #define MEDIA_CODEC_ADAPTER_H
18 
19 #include <condition_variable>
20 #include <cstdint>
21 #include <memory>
22 #include <mutex>
23 #include <queue>
24 #include <string>
25 
26 #include "graphic_adapter.h"
27 
28 namespace OHOS::NWeb {
29 
30 enum class CodecCodeAdapter : int32_t { OK = 0, ERROR = 1, RETRY = 2 };
31 
32 class CapabilityDataAdapter {
33 public:
34     CapabilityDataAdapter() = default;
35 
36     virtual ~CapabilityDataAdapter() = default;
37 
38     virtual int32_t GetMaxWidth() = 0;
39 
40     virtual int32_t GetMaxHeight() = 0;
41 
42     virtual int32_t GetMaxframeRate() = 0;
43 };
44 
45 class CodecFormatAdapter {
46 public:
47     CodecFormatAdapter() = default;
48 
49     virtual ~CodecFormatAdapter() = default;
50 
51     virtual int32_t GetWidth() = 0;
52 
53     virtual int32_t GetHeight() = 0;
54 };
55 
56 enum class ErrorType : int32_t {
57     CODEC_ERROR_INTERNAL,
58     CODEC_ERROR_EXTEND_START = 0X10000,
59 };
60 
61 class BufferInfoAdapter {
62 public:
63     BufferInfoAdapter() = default;
64 
65     virtual ~BufferInfoAdapter() = default;
66 
67     virtual int64_t GetPresentationTimeUs() = 0;
68 
69     virtual int32_t GetSize() = 0;
70 
71     virtual int32_t GetOffset() = 0;
72 };
73 
74 enum class BufferFlag : uint32_t {
75     CODEC_BUFFER_FLAG_NONE = 0,
76     CODEC_BUFFER_FLAG_EOS = 1 << 0,
77     CODEC_BUFFER_FLAG_SYNC_FRAME = 1 << 1,
78     CODEC_BUFFER_FLAG_PARTIAL_FRAME = 1 << 2,
79     CODEC_BUFFER_FLAG_CODEC_DATA = 1 << 3,
80 };
81 
82 class OhosBufferAdapter {
83 public:
84     OhosBufferAdapter() = default;
85 
86     virtual ~OhosBufferAdapter() = default;
87 
88     virtual uint8_t* GetAddr() = 0;
89 
90     virtual uint32_t GetBufferSize() = 0;
91 };
92 
93 class CodecConfigParaAdapter {
94 public:
95     CodecConfigParaAdapter() = default;
96 
97     virtual ~CodecConfigParaAdapter() = default;
98 
99     virtual int32_t GetWidth() = 0;
100 
101     virtual int32_t GetHeight() = 0;
102 
103     virtual int64_t GetBitRate() = 0;
104 
105     virtual double GetFrameRate() = 0;
106 };
107 
108 class CodecCallbackAdapter {
109 public:
110     CodecCallbackAdapter() = default;
111 
112     virtual ~CodecCallbackAdapter() = default;
113 
114     virtual void OnError(ErrorType errorType, int32_t errorCode) = 0;
115 
116     virtual void OnStreamChanged(const std::shared_ptr<CodecFormatAdapter> format) = 0;
117 
118     virtual void OnNeedInputData(uint32_t index, std::shared_ptr<OhosBufferAdapter> buffer) = 0;
119 
120     virtual void OnNeedOutputData(uint32_t index, std::shared_ptr<BufferInfoAdapter> info, BufferFlag flag,
121         std::shared_ptr<OhosBufferAdapter> buffer) = 0;
122 };
123 
124 class MediaCodecAdapter {
125 public:
126     MediaCodecAdapter() = default;
127 
128     virtual ~MediaCodecAdapter() = default;
129 
130     virtual CodecCodeAdapter CreateVideoCodecByMime(const std::string mimetype) = 0;
131 
132     virtual CodecCodeAdapter CreateVideoCodecByName(const std::string name) = 0;
133 
134     virtual CodecCodeAdapter SetCodecCallback(const std::shared_ptr<CodecCallbackAdapter> callback) = 0;
135 
136     virtual CodecCodeAdapter Configure(const std::shared_ptr<CodecConfigParaAdapter> config) = 0;
137 
138     virtual CodecCodeAdapter Prepare() = 0;
139 
140     virtual CodecCodeAdapter Start() = 0;
141 
142     virtual CodecCodeAdapter Stop() = 0;
143 
144     virtual CodecCodeAdapter Reset() = 0;
145 
146     virtual CodecCodeAdapter Release() = 0;
147 
148     virtual std::shared_ptr<ProducerSurfaceAdapter> CreateInputSurface() = 0;
149 
150     virtual CodecCodeAdapter ReleaseOutputBuffer(uint32_t index, bool isRender) = 0;
151 
152     virtual CodecCodeAdapter RequestKeyFrameSoon() = 0;
153 };
154 
155 class MediaCodecListAdapter {
156 public:
157     MediaCodecListAdapter() = default;
158 
159     virtual ~MediaCodecListAdapter() = default;
160 
161     virtual std::shared_ptr<CapabilityDataAdapter> GetCodecCapability(const std::string& mime, const bool isCodec) = 0;
162 };
163 
164 } // namespace OHOS::NWeb
165 
166 #endif