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