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_inner_mock.h"
17 #include "avformat_inner_mock.h"
18 #include "avmemory_inner_mock.h"
19 #include "surface_inner_mock.h"
20
21 namespace OHOS {
22 namespace MediaAVCodec {
VideoDecCallbackMock(std::shared_ptr<AVCodecCallbackMock> cb,std::weak_ptr<AVCodecVideoDecoder> vd)23 VideoDecCallbackMock::VideoDecCallbackMock(std::shared_ptr<AVCodecCallbackMock> cb,
24 std::weak_ptr<AVCodecVideoDecoder> vd)
25 : mockCb_(cb), videoDec_(vd)
26 {
27 }
28
OnError(AVCodecErrorType errorType,int32_t errorCode)29 void VideoDecCallbackMock::OnError(AVCodecErrorType errorType, int32_t errorCode)
30 {
31 (void)errorType;
32 if (mockCb_ != nullptr) {
33 mockCb_->OnError(errorCode);
34 }
35 }
36
OnOutputFormatChanged(const Format & format)37 void VideoDecCallbackMock::OnOutputFormatChanged(const Format &format)
38 {
39 if (mockCb_ != nullptr) {
40 auto formatMock = std::make_shared<AVFormatInnerMock>(format);
41 mockCb_->OnStreamChanged(formatMock);
42 }
43 }
44
OnInputBufferAvailable(uint32_t index,std::shared_ptr<AVSharedMemory> buffer)45 void VideoDecCallbackMock::OnInputBufferAvailable(uint32_t index, std::shared_ptr<AVSharedMemory> buffer)
46 {
47 auto videoDec = videoDec_.lock();
48 if (mockCb_ != nullptr && videoDec != nullptr) {
49 if (buffer != nullptr) {
50 std::shared_ptr<AVMemoryMock> memMock =
51 buffer == nullptr ? nullptr : std::make_shared<AVMemoryInnerMock>(buffer);
52 mockCb_->OnNeedInputData(index, memMock);
53 }
54 }
55 }
56
OnOutputBufferAvailable(uint32_t index,AVCodecBufferInfo info,AVCodecBufferFlag flag,std::shared_ptr<AVSharedMemory> buffer)57 void VideoDecCallbackMock::OnOutputBufferAvailable(uint32_t index, AVCodecBufferInfo info, AVCodecBufferFlag flag,
58 std::shared_ptr<AVSharedMemory> buffer)
59 {
60 if (mockCb_ != nullptr) {
61 struct OH_AVCodecBufferAttr bufferInfo;
62 bufferInfo.pts = info.presentationTimeUs;
63 bufferInfo.size = info.size;
64 bufferInfo.offset = info.offset;
65 bufferInfo.flags = flag;
66 std::shared_ptr<AVMemoryMock> memMock =
67 buffer == nullptr ? nullptr : std::make_shared<AVMemoryInnerMock>(buffer);
68 return mockCb_->OnNewOutputData(index, memMock, bufferInfo);
69 }
70 }
71
SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)72 int32_t VideoDecInnerMock::SetCallback(std::shared_ptr<AVCodecCallbackMock> cb)
73 {
74 if (cb != nullptr) {
75 auto callback = std::make_shared<VideoDecCallbackMock>(cb, videoDec_);
76 if (videoDec_ != nullptr) {
77 return videoDec_->SetCallback(callback);
78 }
79 }
80 return AV_ERR_UNKNOWN;
81 }
82
SetOutputSurface(std::shared_ptr<SurfaceMock> surface)83 int32_t VideoDecInnerMock::SetOutputSurface(std::shared_ptr<SurfaceMock> surface)
84 {
85 if (surface != nullptr) {
86 auto decSurface = std::static_pointer_cast<SurfaceInnerMock>(surface);
87 sptr<Surface> nativeSurface = decSurface->GetSurface();
88 if (videoDec_ != nullptr && nativeSurface != nullptr) {
89 return videoDec_->SetOutputSurface(nativeSurface);
90 }
91 }
92 return AV_ERR_UNKNOWN;
93 }
94
Configure(std::shared_ptr<FormatMock> format)95 int32_t VideoDecInnerMock::Configure(std::shared_ptr<FormatMock> format)
96 {
97 if (videoDec_ != nullptr && format != nullptr) {
98 auto fmt = std::static_pointer_cast<AVFormatInnerMock>(format);
99 return videoDec_->Configure(fmt->GetFormat());
100 }
101 return AV_ERR_UNKNOWN;
102 }
103
Start()104 int32_t VideoDecInnerMock::Start()
105 {
106 if (videoDec_ != nullptr) {
107 return videoDec_->Start();
108 }
109 return AV_ERR_UNKNOWN;
110 }
111
Stop()112 int32_t VideoDecInnerMock::Stop()
113 {
114 if (videoDec_ != nullptr) {
115 return videoDec_->Stop();
116 }
117 return AV_ERR_UNKNOWN;
118 }
119
Flush()120 int32_t VideoDecInnerMock::Flush()
121 {
122 if (videoDec_ != nullptr) {
123 return videoDec_->Flush();
124 }
125 return AV_ERR_UNKNOWN;
126 }
127
Reset()128 int32_t VideoDecInnerMock::Reset()
129 {
130 if (videoDec_ != nullptr) {
131 return videoDec_->Reset();
132 }
133 return AV_ERR_UNKNOWN;
134 }
135
Release()136 int32_t VideoDecInnerMock::Release()
137 {
138 if (videoDec_ != nullptr) {
139 return videoDec_->Release();
140 }
141 return AV_ERR_UNKNOWN;
142 }
143
GetOutputDescription()144 std::shared_ptr<FormatMock> VideoDecInnerMock::GetOutputDescription()
145 {
146 if (videoDec_ != nullptr) {
147 Format format;
148 (void)videoDec_->GetOutputFormat(format);
149 return std::make_shared<AVFormatInnerMock>(format);
150 }
151 return nullptr;
152 }
153
SetParameter(std::shared_ptr<FormatMock> format)154 int32_t VideoDecInnerMock::SetParameter(std::shared_ptr<FormatMock> format)
155 {
156 if (videoDec_ != nullptr && format != nullptr) {
157 auto fmt = std::static_pointer_cast<AVFormatInnerMock>(format);
158 return videoDec_->SetParameter(fmt->GetFormat());
159 }
160 return AV_ERR_UNKNOWN;
161 }
162
PushInputData(uint32_t index,OH_AVCodecBufferAttr & attr)163 int32_t VideoDecInnerMock::PushInputData(uint32_t index, OH_AVCodecBufferAttr &attr)
164 {
165 if (videoDec_ != nullptr) {
166 AVCodecBufferInfo info;
167 info.presentationTimeUs = attr.pts;
168 info.size = attr.size;
169 info.offset = attr.offset;
170 AVCodecBufferFlag flags = static_cast<AVCodecBufferFlag>(attr.flags);
171 return videoDec_->QueueInputBuffer(index, info, flags);
172 }
173 return AV_ERR_UNKNOWN;
174 }
175
RenderOutputData(uint32_t index)176 int32_t VideoDecInnerMock::RenderOutputData(uint32_t index)
177 {
178 if (videoDec_ != nullptr) {
179 return videoDec_->ReleaseOutputBuffer(index, true);
180 }
181 return AV_ERR_UNKNOWN;
182 }
183
FreeOutputData(uint32_t index)184 int32_t VideoDecInnerMock::FreeOutputData(uint32_t index)
185 {
186 if (videoDec_ != nullptr) {
187 return videoDec_->ReleaseOutputBuffer(index, false);
188 }
189 return AV_ERR_UNKNOWN;
190 }
191
IsValid()192 bool VideoDecInnerMock::IsValid()
193 {
194 return true;
195 }
196 } // namespace MediaAVCodec
197 } // namespace OHOS