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 <string>
17 #include "gtest/gtest.h"
18 #include "AVMuxerDemo.h"
19 
20 using namespace std;
21 using namespace testing::ext;
22 using namespace OHOS;
23 using namespace OHOS::MediaAVCodec;
24 
25 
26 namespace {
27     class NativeAVMuxerNullCheckTest : public testing::Test {
28     public:
29         static void SetUpTestCase();
30         static void TearDownTestCase();
31         void SetUp() override;
32         void TearDown() override;
33     };
34 
SetUpTestCase()35     void NativeAVMuxerNullCheckTest::SetUpTestCase() {}
TearDownTestCase()36     void NativeAVMuxerNullCheckTest::TearDownTestCase() {}
SetUp()37     void NativeAVMuxerNullCheckTest::SetUp() {}
TearDown()38     void NativeAVMuxerNullCheckTest::TearDown() {}
39 
40     constexpr int64_t BITRATE = 32000;
41     constexpr int32_t CODEC_CONFIG = 100;
42     constexpr int32_t CHANNEL_COUNT = 1;
43     constexpr int32_t SAMPLE_RATE = 48000;
44     constexpr int32_t PROFILE = 0;
45     constexpr int32_t INFO_SIZE = 100;
46 }
47 
48 
49 /**
50  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_001
51  * @tc.name      : NativeSetRotation - muxer check
52  * @tc.desc      : nullptr test
53  */
54 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_001, TestSize.Level2)
55 {
56     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
57     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_M4A;
58     int32_t fd = muxerDemo->GetFdByMode(format);
59     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
60     ASSERT_NE(nullptr, handle);
61 
62     int32_t rotation = 0;
63 
64     OH_AVErrCode ret = muxerDemo->NativeSetRotation(nullptr, rotation);
65     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
66     muxerDemo->NativeDestroy(handle);
67     delete muxerDemo;
68 }
69 
70 
71 /**
72  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_002
73  * @tc.name      : OH_AVMuxer_AddTrack - muxer check
74  * @tc.desc      : nullptr test
75  */
76 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_002, TestSize.Level2)
77 {
78     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
79     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_M4A;
80     int32_t fd = muxerDemo->GetFdByMode(format);
81     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
82     ASSERT_NE(nullptr, handle);
83 
84     int32_t trackId = -1;
85 
86     uint8_t a[100];
87 
88     OH_AVFormat* trackFormat = OH_AVFormat_Create();
89     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
90     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
91     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
92     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
93     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
94     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
95 
96     OH_AVErrCode ret = muxerDemo->NativeAddTrack(nullptr, &trackId, trackFormat);
97     ASSERT_EQ(-1, trackId);
98     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
99     muxerDemo->NativeDestroy(handle);
100     OH_AVFormat_Destroy(trackFormat);
101     delete muxerDemo;
102 }
103 
104 
105 /**
106  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_003
107  * @tc.name      : OH_AVMuxer_AddTrack - trackId check
108  * @tc.desc      : nullptr test
109  */
110 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_003, TestSize.Level2)
111 {
112     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
113     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_M4A;
114     int32_t fd = muxerDemo->GetFdByMode(format);
115     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
116     ASSERT_NE(nullptr, handle);
117 
118     uint8_t a[100];
119 
120     OH_AVFormat* trackFormat = OH_AVFormat_Create();
121     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
122     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
123     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
124     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
125     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
126     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
127 
128     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, nullptr, trackFormat);
129     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
130     muxerDemo->NativeDestroy(handle);
131     OH_AVFormat_Destroy(trackFormat);
132     delete muxerDemo;
133 }
134 
135 
136 /**
137  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_004
138  * @tc.name      : OH_AVMuxer_AddTrack - trackFormat check
139  * @tc.desc      : nullptr test
140  */
141 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_004, TestSize.Level2)
142 {
143     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
144     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_M4A;
145     int32_t fd = muxerDemo->GetFdByMode(format);
146     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
147     ASSERT_NE(nullptr, handle);
148 
149     int32_t trackId = -1;
150 
151     OH_AVFormat* trackFormat = nullptr;
152 
153     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
154     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
155     muxerDemo->NativeDestroy(handle);
156     delete muxerDemo;
157 }
158 
159 
160 /**
161  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_005
162  * @tc.name      : OH_AVMuxer_Start - muxer check
163  * @tc.desc      : nullptr test
164  */
165 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_005, TestSize.Level2)
166 {
167     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
168     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
169     int32_t fd = muxerDemo->GetFdByMode(format);
170     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
171     ASSERT_NE(nullptr, handle);
172 
173     int32_t trackId = -1;
174 
175     uint8_t a[100];
176 
177     OH_AVFormat* trackFormat = OH_AVFormat_Create();
178     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
179     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
180     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
181     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
182     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
183     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
184 
185     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
186     ASSERT_EQ(0, trackId);
187 
188     ret = muxerDemo->NativeStart(nullptr);
189     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
190 
191     muxerDemo->NativeDestroy(handle);
192     OH_AVFormat_Destroy(trackFormat);
193     delete muxerDemo;
194 }
195 
196 
197 /**
198  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_006
199  * @tc.name      : OH_AVMuxer_WriteSampleBuffer - muxer check
200  * @tc.desc      : nullptr test
201  */
202 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_006, TestSize.Level2)
203 {
204     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
205     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
206     int32_t fd = muxerDemo->GetFdByMode(format);
207     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
208     ASSERT_NE(nullptr, handle);
209 
210     int32_t trackId = -1;
211 
212     uint8_t a[100];
213 
214     OH_AVFormat* trackFormat = OH_AVFormat_Create();
215     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
216     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
217     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
218     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
219     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
220     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
221 
222     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
223     ASSERT_EQ(0, trackId);
224 
225     ret = muxerDemo->NativeStart(handle);
226     ASSERT_EQ(AV_ERR_OK, ret);
227 
228     OH_AVMemory* avMemBuffer = OH_AVMemory_Create(INFO_SIZE);
229 
230     OH_AVCodecBufferAttr info;
231     info.pts = 0;
232     info.size = INFO_SIZE;
233     info.offset = 0;
234     info.flags = 0;
235 
236     ret = muxerDemo->NativeWriteSampleBuffer(nullptr, trackId, avMemBuffer, info);
237     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
238 
239     OH_AVMemory_Destroy(avMemBuffer);
240     muxerDemo->NativeDestroy(handle);
241     OH_AVFormat_Destroy(trackFormat);
242     delete muxerDemo;
243 }
244 
245 
246 /**
247  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_007
248  * @tc.name      : OH_AVMuxer_WriteSampleBuffer - sampleBuffer check
249  * @tc.desc      : nullptr test
250  */
251 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_007, TestSize.Level2)
252 {
253     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
254     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
255     int32_t fd = muxerDemo->GetFdByMode(format);
256     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
257     ASSERT_NE(nullptr, handle);
258 
259     int32_t trackId = -1;
260 
261     uint8_t a[100];
262 
263     OH_AVFormat* trackFormat = OH_AVFormat_Create();
264     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
265     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
266     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
267     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
268     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
269     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
270 
271     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
272     ASSERT_EQ(0, trackId);
273 
274     ret = muxerDemo->NativeStart(handle);
275     ASSERT_EQ(AV_ERR_OK, ret);
276 
277     OH_AVCodecBufferAttr info;
278     info.pts = 0;
279     info.size = INFO_SIZE;
280     info.offset = 0;
281     info.flags = 0;
282 
283     ret = muxerDemo->NativeWriteSampleBuffer(handle, trackId, nullptr, info);
284     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
285 
286     muxerDemo->NativeDestroy(handle);
287     OH_AVFormat_Destroy(trackFormat);
288     delete muxerDemo;
289 }
290 
291 
292 /**
293  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_008
294  * @tc.name      : OH_AVMuxer_Stop - muxer check
295  * @tc.desc      : nullptr test
296  */
297 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_008, TestSize.Level2)
298 {
299     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
300     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
301     int32_t fd = muxerDemo->GetFdByMode(format);
302     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
303     ASSERT_NE(nullptr, handle);
304 
305     int32_t trackId = -1;
306 
307     uint8_t a[100];
308 
309     OH_AVFormat* trackFormat = OH_AVFormat_Create();
310     OH_AVFormat_SetStringValue(trackFormat, OH_MD_KEY_CODEC_MIME, OH_AVCODEC_MIMETYPE_AUDIO_AAC);
311     OH_AVFormat_SetLongValue(trackFormat, OH_MD_KEY_BITRATE, BITRATE);
312     OH_AVFormat_SetBuffer(trackFormat, OH_MD_KEY_CODEC_CONFIG, a, CODEC_CONFIG);
313     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_CHANNEL_COUNT, CHANNEL_COUNT);
314     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_AUD_SAMPLE_RATE, SAMPLE_RATE);
315     OH_AVFormat_SetIntValue(trackFormat, OH_MD_KEY_PROFILE, PROFILE);
316 
317     OH_AVErrCode ret = muxerDemo->NativeAddTrack(handle, &trackId, trackFormat);
318     ASSERT_EQ(0, trackId);
319 
320     ret = muxerDemo->NativeStart(handle);
321     ASSERT_EQ(AV_ERR_OK, ret);
322 
323     ret = muxerDemo->NativeStop(nullptr);
324     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
325 
326     muxerDemo->NativeDestroy(handle);
327     OH_AVFormat_Destroy(trackFormat);
328     delete muxerDemo;
329 }
330 
331 
332 /**
333  * @tc.number    : SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_009
334  * @tc.name      : OH_AVMuxer_Destroy - muxer check
335  * @tc.desc      : nullptr test
336  */
337 HWTEST_F(NativeAVMuxerNullCheckTest, SUB_MULTIMEDIA_MEDIA_MUXER_NULL_CHECK_009, TestSize.Level2)
338 {
339     AVMuxerDemo* muxerDemo = new AVMuxerDemo();
340     OH_AVOutputFormat format = AV_OUTPUT_FORMAT_MPEG_4;
341     int32_t fd = muxerDemo->GetFdByMode(format);
342     OH_AVMuxer* handle = muxerDemo->NativeCreate(fd, format);
343     ASSERT_NE(nullptr, handle);
344 
345     OH_AVErrCode ret = muxerDemo->NativeDestroy(nullptr);
346     ASSERT_EQ(AV_ERR_INVALID_VAL, ret);
347 
348     muxerDemo->NativeDestroy(handle);
349     delete muxerDemo;
350 }