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 #include "videodec_capi_mock.h"
17 #include "avformat_capi_mock.h"
18 #include "avmemory_capi_mock.h"
19 #include "surface_capi_mock.h"
20
21 namespace OHOS {
22 namespace MediaAVCodec {
23 std::mutex VideoDecCapiMock::mutex_;
24 std::map<OH_AVCodec *, std::shared_ptr<AVCodecCallbackMock>> VideoDecCapiMock::mockCbMap_;
OnError(OH_AVCodec * codec,int32_t errorCode,void * userData)25 void VideoDecCapiMock::OnError(OH_AVCodec *codec, int32_t errorCode, void *userData)
26 {
27 (void)userData;
28 std::shared_ptr<AVCodecCallbackMock> mockCb = GetCallback(codec);
29 if (mockCb != nullptr) {
30 mockCb->OnError(errorCode);
31 }
32 }
33
OnStreamChanged(OH_AVCodec * codec,OH_AVFormat * format,void * userData)34 void VideoDecCapiMock::OnStreamChanged(OH_AVCodec *codec, OH_AVFormat *format, void *userData)
35 {
36 (void)userData;
37 std::shared_ptr<AVCodecCallbackMock> mockCb = GetCallback(codec);
38 if (mockCb != nullptr) {
39 auto formatMock = std::make_shared<AVFormatCapiMock>(format);
40 mockCb->OnStreamChanged(formatMock);
41 }
42 }
43
OnNeedInputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,void * userData)44 void VideoDecCapiMock::OnNeedInputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, void *userData)
45 {
46 (void)userData;
47 std::shared_ptr<AVCodecCallbackMock> mockCb = GetCallback(codec);
48 if (mockCb != nullptr) {
49 std::shared_ptr<AVMemoryMock> memMock = data == nullptr ? nullptr : std::make_shared<AVMemoryCapiMock>(data);
50 mockCb->OnNeedInputData(index, memMock);
51 }
52 }
53
OnNewOutputData(OH_AVCodec * codec,uint32_t index,OH_AVMemory * data,OH_AVCodecBufferAttr * attr,void * userData)54 void VideoDecCapiMock::OnNewOutputData(OH_AVCodec *codec, uint32_t index, OH_AVMemory *data, OH_AVCodecBufferAttr *attr,
55 void *userData)
56 {
57 (void)userData;
58 std::shared_ptr<AVCodecCallbackMock> mockCb = GetCallback(codec);
59 if (mockCb != nullptr) {
60 std::shared_ptr<AVMemoryMock> memMock = data == nullptr ? nullptr : std::make_shared<AVMemoryCapiMock>(data);
61 mockCb->OnNewOutputData(index, memMock, *attr);
62 }
63 }
64
GetCallback(OH_AVCodec * codec)65 std::shared_ptr<AVCodecCallbackMock> VideoDecCapiMock::GetCallback(OH_AVCodec *codec)
66 {
67 std::lock_guard<std::mutex> lock(mutex_);
68 if (mockCbMap_.find(codec) != mockCbMap_.end()) {
69 return mockCbMap_.at(codec);
70 }
71 return nullptr;
72 }
73
SetCallback(OH_AVCodec * codec,std::shared_ptr<AVCodecCallbackMock> cb)74 void VideoDecCapiMock::SetCallback(OH_AVCodec *codec, std::shared_ptr<AVCodecCallbackMock> cb)
75 {
76 std::lock_guard<std::mutex> lock(mutex_);
77 mockCbMap_[codec] = cb;
78 }
79
DelCallback(OH_AVCodec * codec)80 void VideoDecCapiMock::DelCallback(OH_AVCodec *codec)
81 {
82 auto it = mockCbMap_.find(codec);
83 if (it != mockCbMap_.end()) {
84 mockCbMap_.erase(it);
85 }
86 }
87
SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)88 int32_t VideoDecCapiMock::SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)
89 {
90 if (cb != nullptr && codec_ != nullptr) {
91 SetCallback(codec_, cb);
92 struct OH_AVCodecAsyncCallback callback;
93 callback.onError = VideoDecCapiMock::OnError;
94 callback.onStreamChanged = VideoDecCapiMock::OnStreamChanged;
95 callback.onNeedInputData = VideoDecCapiMock::OnNeedInputData;
96 callback.onNeedOutputData = VideoDecCapiMock::OnNewOutputData;
97 return OH_VideoDecoder_SetCallback(codec_, callback, NULL);
98 }
99 return AV_ERR_OPERATE_NOT_PERMIT;
100 }
101
SetOutputSurface(std::shared_ptr<SurfaceMock> surface)102 int32_t VideoDecCapiMock::SetOutputSurface(std::shared_ptr<SurfaceMock> surface)
103 {
104 if (codec_ != nullptr && surface != nullptr) {
105 auto surfaceMock = std::static_pointer_cast<SurfaceCapiMock>(surface);
106 OHNativeWindow *nativeWindow = surfaceMock->GetSurface();
107 if (nativeWindow != nullptr) {
108 return OH_VideoDecoder_SetSurface(codec_, nativeWindow);
109 }
110 }
111 return AV_ERR_OPERATE_NOT_PERMIT;
112 }
113
Configure(std::shared_ptr<FormatMock> format)114 int32_t VideoDecCapiMock::Configure(std::shared_ptr<FormatMock> format)
115 {
116 if (codec_ != nullptr && format != nullptr) {
117 auto formatMock = std::static_pointer_cast<AVFormatCapiMock>(format);
118 OH_AVFormat *avFormat = formatMock->GetFormat();
119 if (avFormat != nullptr) {
120 return OH_VideoDecoder_Configure(codec_, avFormat);
121 }
122 }
123 return AV_ERR_OPERATE_NOT_PERMIT;
124 }
125
Start()126 int32_t VideoDecCapiMock::Start()
127 {
128 if (codec_ != nullptr) {
129 return OH_VideoDecoder_Start(codec_);
130 }
131 return AV_ERR_OPERATE_NOT_PERMIT;
132 }
133
Stop()134 int32_t VideoDecCapiMock::Stop()
135 {
136 if (codec_ != nullptr) {
137 return OH_VideoDecoder_Stop(codec_);
138 }
139 return AV_ERR_OPERATE_NOT_PERMIT;
140 }
141
Flush()142 int32_t VideoDecCapiMock::Flush()
143 {
144 if (codec_ != nullptr) {
145 return OH_VideoDecoder_Flush(codec_);
146 }
147 return AV_ERR_OPERATE_NOT_PERMIT;
148 }
149
Reset()150 int32_t VideoDecCapiMock::Reset()
151 {
152 if (codec_ != nullptr) {
153 return OH_VideoDecoder_Reset(codec_);
154 }
155 return AV_ERR_OPERATE_NOT_PERMIT;
156 }
157
Release()158 int32_t VideoDecCapiMock::Release()
159 {
160 if (codec_ != nullptr) {
161 DelCallback(codec_);
162 int32_t ret = OH_VideoDecoder_Destroy(codec_);
163 codec_ = nullptr;
164 return ret;
165 }
166 return AV_ERR_OPERATE_NOT_PERMIT;
167 }
168
GetOutputDescription()169 std::shared_ptr<FormatMock> VideoDecCapiMock::GetOutputDescription()
170 {
171 if (codec_ != nullptr) {
172 OH_AVFormat *format = OH_VideoDecoder_GetOutputDescription(codec_);
173 return std::make_shared<AVFormatCapiMock>(format);
174 }
175 return nullptr;
176 }
177
SetParameter(std::shared_ptr<FormatMock> format)178 int32_t VideoDecCapiMock::SetParameter(std::shared_ptr<FormatMock> format)
179 {
180 if (codec_ != nullptr && format != nullptr) {
181 auto formatMock = std::static_pointer_cast<AVFormatCapiMock>(format);
182 return OH_VideoDecoder_SetParameter(codec_, formatMock->GetFormat());
183 }
184 return AV_ERR_OPERATE_NOT_PERMIT;
185 }
186
PushInputData(uint32_t index,OH_AVCodecBufferAttr & attr)187 int32_t VideoDecCapiMock::PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr)
188 {
189 if (codec_ != nullptr) {
190 return OH_VideoDecoder_PushInputData(codec_, index, attr);
191 }
192 return AV_ERR_OPERATE_NOT_PERMIT;
193 }
194
RenderOutputData(uint32_t index)195 int32_t VideoDecCapiMock::RenderOutputData(uint32_t index)
196 {
197 if (codec_ != nullptr) {
198 return OH_VideoDecoder_RenderOutputData(codec_, index);
199 }
200 return AV_ERR_OPERATE_NOT_PERMIT;
201 }
202
FreeOutputData(uint32_t index)203 int32_t VideoDecCapiMock::FreeOutputData(uint32_t index)
204 {
205 if (codec_ != nullptr) {
206 return OH_VideoDecoder_FreeOutputData(codec_, index);
207 }
208 return AV_ERR_OPERATE_NOT_PERMIT;
209 }
210
IsValid()211 bool VideoDecCapiMock::IsValid()
212 {
213 if (codec_ != nullptr) {
214 bool isValid = false;
215 (void)OH_VideoDecoder_IsValid(codec_, &isValid);
216 return isValid;
217 }
218 return false;
219 }
220 } // namespace MediaAVCodec
221 } // namespace OHOS