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