• 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 #include "gtest/gtest.h"
17 #include <gmock/gmock.h>
18 #define private public
19 #include "avcodec_video_decoder_impl.h"
20 #include "media_codec_decoder_adapter_impl.h"
21 #undef private
22 #include "avsharedmemory.h"
23 #include "avsharedmemorybase.h"
24 #include "foundation/graphic/graphic_surface/interfaces/inner_api/surface/window.h"
25 #include "native_window.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace std;
30 using testing::ext::TestSize;
31 using namespace OHOS::MediaAVCodec;
32 
33 namespace OHOS {
34 namespace MediaAVCodec {
35 class AVCodecVideoDecoderImplMock : public AVCodecVideoDecoderImpl {
36 public:
37     MOCK_METHOD0(Prepare, int32_t());
38 };
39 }
40 namespace NWeb {
41 class DecoderCallbackImplTest : public testing::Test {};
42 
43 class DecoderCallbackAdapterMock : public DecoderCallbackAdapter {
44 public:
45     DecoderCallbackAdapterMock() = default;
46 
47     ~DecoderCallbackAdapterMock() override = default;
48 
OnError(ErrorType errorType,int32_t errorCode)49     void OnError(ErrorType errorType, int32_t errorCode) override {}
50 
OnStreamChanged(int32_t width,int32_t height,double frameRate)51     void OnStreamChanged(int32_t width, int32_t height, double frameRate) override {}
52 
OnNeedInputData(uint32_t index,std::shared_ptr<OhosBufferAdapter> buffer)53     void OnNeedInputData(uint32_t index, std::shared_ptr<OhosBufferAdapter> buffer) override {}
54 
OnNeedOutputData(uint32_t index,std::shared_ptr<BufferInfoAdapter> info,BufferFlag flag)55     void OnNeedOutputData(uint32_t index, std::shared_ptr<BufferInfoAdapter> info, BufferFlag flag) override {}
56 };
57 
58 class DecoderFormatAdapterMock : public DecoderFormatAdapter {
59 public:
60     DecoderFormatAdapterMock() = default;
61 
GetWidth()62     int32_t GetWidth() override
63     {
64         return width;
65     }
66 
GetHeight()67     int32_t GetHeight() override
68     {
69         return height;
70     }
71 
GetFrameRate()72     double GetFrameRate() override
73     {
74         return frameRate;
75     }
76 
SetWidth(int32_t w)77     void SetWidth(int32_t w) override
78     {
79         width = w;
80     }
81 
SetHeight(int32_t h)82     void SetHeight(int32_t h) override
83     {
84         height = h;
85     }
86 
SetFrameRate(double fr)87     void SetFrameRate(double fr) override
88     {
89         frameRate = fr;
90     }
91 
92     int32_t width;
93     int32_t height;
94     double frameRate;
95 };
96 
97 class MediaCodecDecoderAdapterImplTest : public testing::Test {
98 public:
99     static void SetUpTestCase(void);
100     static void TearDownTestCase(void);
101     void SetUp();
102     void TearDown();
103 
104 protected:
105     std::shared_ptr<DecoderFormatAdapterMock> format_ = nullptr;
106     std::shared_ptr<MediaCodecDecoderAdapterImpl> mediaCodecDecoderAdapterImpl_ = nullptr;
107 };
108 
SetUpTestCase(void)109 void MediaCodecDecoderAdapterImplTest::SetUpTestCase(void) {}
110 
TearDownTestCase(void)111 void MediaCodecDecoderAdapterImplTest::TearDownTestCase(void) {}
112 
SetUp()113 void MediaCodecDecoderAdapterImplTest::SetUp()
114 {
115     EXPECT_EQ(mediaCodecDecoderAdapterImpl_, nullptr);
116     mediaCodecDecoderAdapterImpl_ = std::make_unique<MediaCodecDecoderAdapterImpl>();
117     EXPECT_NE(mediaCodecDecoderAdapterImpl_, nullptr);
118     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->decoder_, nullptr);
119     EXPECT_EQ(format_, nullptr);
120     format_ = std::make_unique<DecoderFormatAdapterMock>();
121     EXPECT_NE(format_, nullptr);
122     format_->width = 200;
123     format_->height = 300;
124     format_->frameRate = 20;
125 }
126 
TearDown(void)127 void MediaCodecDecoderAdapterImplTest::TearDown(void) {}
128 
129 /**
130  * @tc.name: MediaCodecDecoderAdapterImpl_CreateVideoDecoderByName_001.
131  * @tc.desc: test of MediaCodecDecoderAdapterImpl::CreateVideoDecoderByName() CreateVideoDecoderByName()
132  * @tc.type: FUNC.
133  * @tc.require:
134  */
135 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_CreateVideoDecoderByName_001, TestSize.Level1)
136 {
137     std::string mimetype_ = "testmimeType";
138     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByMime(mimetype_), DecoderAdapterCode::DECODER_ERROR);
139     std::string name_ = "testname";
140     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByName(name_), DecoderAdapterCode::DECODER_ERROR);
141     mimetype_ = "video/avc";
142     DecoderAdapterCode ret = mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByMime(mimetype_);
143     EXPECT_EQ(ret, DecoderAdapterCode::DECODER_OK);
144 }
145 
146 /**
147  * @tc.name: MediaCodecDecoderAdapterImpl_InvalidValueTest_002.
148  * @tc.desc: test of InvalidValueScene in MediaCodecDecoderAdapterImpl
149  * @tc.type: FUNC.
150  * @tc.require:
151  */
152 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_InvalidValueTest_002, TestSize.Level1)
153 {
154     uint32_t index_ = 0;
155     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->QueueInputBufferDec(index_, 0, 0, 0, BufferFlag::CODEC_BUFFER_FLAG_NONE),
156         DecoderAdapterCode::DECODER_ERROR);
157     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), DecoderAdapterCode::DECODER_ERROR);
158     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
159     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
160     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetParameterDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
161     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetOutputSurface(nullptr), DecoderAdapterCode::DECODER_ERROR);
162     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_ERROR);
163     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StartDecoder(), DecoderAdapterCode::DECODER_ERROR);
164     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StopDecoder(), DecoderAdapterCode::DECODER_ERROR);
165     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->FlushDecoder(), DecoderAdapterCode::DECODER_ERROR);
166     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ResetDecoder(), DecoderAdapterCode::DECODER_ERROR);
167     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseDecoder(), DecoderAdapterCode::DECODER_ERROR);
168     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
169     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
170 }
171 
172 /**
173  * @tc.name: MediaCodecDecoderAdapterImpl_NormalTest_003.
174  * @tc.desc: test of NormalScene in MediaCodecDecoderAdapterImpl
175  * @tc.type: FUNC.
176  * @tc.require:
177  */
178 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_NormalTest_003, TestSize.Level1)
179 {
180     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_ERROR);
181     AVCodecVideoDecoderImplMock *mock = new AVCodecVideoDecoderImplMock();
182     mediaCodecDecoderAdapterImpl_->decoder_.reset(mock);
183     uint32_t index_ = 1;
184     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->QueueInputBufferDec(index_, 0, 0, 0, BufferFlag::CODEC_BUFFER_FLAG_NONE),
185         DecoderAdapterCode::DECODER_ERROR);
186     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetOutputFormatDec(format_), DecoderAdapterCode::DECODER_ERROR);
187     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
188     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
189     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetParameterDecoder(format_), DecoderAdapterCode::DECODER_ERROR);
190     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetOutputSurface(nullptr), DecoderAdapterCode::DECODER_ERROR);
191     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
192     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
193     EXPECT_CALL(*mock, Prepare())
194         .Times(1)
195         .WillRepeatedly(::testing::Return(0));
196     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->PrepareDecoder(), DecoderAdapterCode::DECODER_OK);
197     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StartDecoder(), DecoderAdapterCode::DECODER_ERROR);
198     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->StopDecoder(), DecoderAdapterCode::DECODER_ERROR);
199     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->FlushDecoder(), DecoderAdapterCode::DECODER_ERROR);
200     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ResetDecoder(), DecoderAdapterCode::DECODER_ERROR);
201     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseDecoder(), DecoderAdapterCode::DECODER_ERROR);
202     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ReleaseOutputBufferDec(index_, true), DecoderAdapterCode::DECODER_ERROR);
203 }
204 
205 /**
206  * @tc.name: MediaCodecDecoderAdapterImpl_SetCallbackDec_004.
207  * @tc.desc: test of MediaCodecDecoderAdapterImpl::SetCallbackDec
208  * @tc.type: FUNC.
209  * @tc.require:
210  */
211 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_SetCallbackDec_004, TestSize.Level1)
212 {
213     std::shared_ptr<DecoderCallbackAdapter> callback = nullptr;
214     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
215     callback = std::make_shared<DecoderCallbackAdapterMock>();
216     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->SetCallbackDec(callback), DecoderAdapterCode::DECODER_ERROR);
217 }
218 
219 /**
220  * @tc.name: MediaCodecDecoderAdapterImpl_GetTypeOrFlag_005.
221  * @tc.desc: test of MediaCodecDecoderAdapterImpl::GetErrorType() GetBufferFlag() GetAVBufferFlag()
222  * @tc.type: FUNC.
223  * @tc.require:
224  */
225 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_GetTypeOrFlag_005, TestSize.Level1)
226 {
227     EXPECT_EQ(
228         mediaCodecDecoderAdapterImpl_->GetErrorType(OHOS::MediaAVCodec::AVCodecErrorType::AVCODEC_ERROR_EXTEND_START),
229         ErrorType::CODEC_ERROR_EXTEND_START);
230     EXPECT_EQ(
231         mediaCodecDecoderAdapterImpl_->GetBufferFlag(OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS),
232         BufferFlag::CODEC_BUFFER_FLAG_EOS);
233     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetAVBufferFlag(BufferFlag::CODEC_BUFFER_FLAG_EOS),
234         OHOS::MediaAVCodec::AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS);
235     AVCodecErrorType testAVCodecErrorType = static_cast<AVCodecErrorType>(100);
236     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetErrorType(testAVCodecErrorType), ErrorType::CODEC_ERROR_INTERNAL);
237     AVCodecBufferFlag testAVCodecBufferFlag = static_cast<AVCodecBufferFlag>(100);
238     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetBufferFlag(testAVCodecBufferFlag), BufferFlag::CODEC_BUFFER_FLAG_NONE);
239     BufferFlag testBufferFlag = static_cast<BufferFlag>(100);
240     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->GetAVBufferFlag(testBufferFlag),
241         AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE);
242     std::string codecName = "video/avc";
243     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->CreateVideoDecoderByName(codecName), DecoderAdapterCode::DECODER_OK);
244     EXPECT_EQ(mediaCodecDecoderAdapterImpl_->ConfigureDecoder(format_), DecoderAdapterCode::DECODER_OK);
245 }
246 
247 /**
248  * @tc.name: MediaCodecDecoderAdapterImpl_OnError_006.
249  * @tc.desc: test of MediaCodecDecoderAdapterImpl::GetErrorType() GetBufferFlag() GetAVBufferFlag()
250  * @tc.type: FUNC.
251  * @tc.require:
252  */
253 HWTEST_F(MediaCodecDecoderAdapterImplTest, MediaCodecDecoderAdapterImpl_OnError_006, TestSize.Level1)
254 {
255     std::shared_ptr<DecoderCallbackAdapter> cb = nullptr;
256     std::shared_ptr<DecoderCallbackImpl> decoderCallbackImpl = std::make_shared<DecoderCallbackImpl>(cb);
257     EXPECT_NE(decoderCallbackImpl, nullptr);
258     std::shared_ptr<DecoderCallbackAdapter> callback = std::make_shared<DecoderCallbackAdapterMock>();
259     EXPECT_NE(callback, nullptr);
260     decoderCallbackImpl->cb_ = callback;
261     decoderCallbackImpl->OnError(OHOS::MediaAVCodec::AVCodecErrorType::AVCODEC_ERROR_EXTEND_START, 1);
262     MediaAVCodec::Format fomat;
263     decoderCallbackImpl->OnOutputFormatChanged(fomat);
264     decoderCallbackImpl->OnInputBufferAvailable(1, nullptr);
265     std::shared_ptr<Media::AVSharedMemory> memory = std::make_shared<Media::AVSharedMemoryBase>(1, 1.0, "test");
266     decoderCallbackImpl->OnInputBufferAvailable(1, memory);
267     AVCodecBufferInfo info;
268     decoderCallbackImpl->OnOutputBufferAvailable(1, info, AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS, nullptr);
269 }
270 }
271 } // namespace OHOS::NWeb
272