• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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 
18 #include "avdemuxer.h"
19 #include "avsource.h"
20 #include "meta/format.h"
21 #include "avcodec_errors.h"
22 #include "avcodec_common.h"
23 #include "buffer/avsharedmemory.h"
24 #include "buffer/avsharedmemorybase.h"
25 #include "securec.h"
26 
27 #include "native_avcodec_base.h"
28 #include "native_avdemuxer.h"
29 #include "native_avformat.h"
30 #include "native_avsource.h"
31 #include "native_avmemory.h"
32 #include "meta/meta_key.h"
33 #include "meta/meta.h"
34 #include "av_common.h"
35 
36 #include <iostream>
37 #include <cstdio>
38 #include <string>
39 #include <fcntl.h>
40 #include <thread>
41 
42 using namespace std;
43 using namespace OHOS;
44 using namespace OHOS::MediaAVCodec;
45 using namespace OHOS::Media;
46 using namespace testing::ext;
47 
48 namespace {
49 class DemuxerNetNdkTest : public testing::Test {
50 public:
51     // SetUpTestCase: Called before all test cases
52     static void SetUpTestCase(void);
53     // TearDownTestCase: Called after all test case
54     static void TearDownTestCase(void);
55     // SetUp: Called before each test cases
56     void SetUp(void);
57     // TearDown: Called after each test cases
58     void TearDown(void);
59 
60 public:
61     int32_t fd_ = -1;
62     int64_t size;
63 };
64 static OH_AVMemory *memory = nullptr;
65 static OH_AVFormat *sourceFormat = nullptr;
66 static OH_AVFormat *trackFormat = nullptr;
67 static OH_AVSource *source = nullptr;
68 static OH_AVDemuxer *demuxer = nullptr;
69 static int32_t g_trackCount = 0;
70 static OH_AVBuffer *avBuffer = nullptr;
71 
72 static int32_t g_width = 3840;
73 static int32_t g_height = 2160;
SetUpTestCase()74 void DemuxerNetNdkTest::SetUpTestCase() {}
TearDownTestCase()75 void DemuxerNetNdkTest::TearDownTestCase() {}
SetUp()76 void DemuxerNetNdkTest::SetUp()
77 {
78     memory = OH_AVMemory_Create(g_width * g_height);
79     g_trackCount = 0;
80 }
TearDown()81 void DemuxerNetNdkTest::TearDown()
82 {
83     if (fd_ > 0) {
84         close(fd_);
85         fd_ = -1;
86     }
87     if (demuxer != nullptr) {
88         OH_AVDemuxer_Destroy(demuxer);
89         demuxer = nullptr;
90     }
91     if (memory != nullptr) {
92         OH_AVMemory_Destroy(memory);
93         memory = nullptr;
94     }
95     if (source != nullptr) {
96         OH_AVSource_Destroy(source);
97         source = nullptr;
98     }
99     if (avBuffer != nullptr) {
100         OH_AVBuffer_Destroy(avBuffer);
101         avBuffer = nullptr;
102     }
103     if (trackFormat != nullptr) {
104         OH_AVFormat_Destroy(trackFormat);
105         trackFormat = nullptr;
106     }
107     if (sourceFormat != nullptr) {
108         OH_AVFormat_Destroy(sourceFormat);
109         sourceFormat = nullptr;
110     }
111 }
112 } // namespace
113 
114 namespace {
OpenUri(const char * uri,OH_AVSource ** src,OH_AVDemuxer ** audioDemuxer)115 static void OpenUri(const char* uri, OH_AVSource **src, OH_AVDemuxer **audioDemuxer)
116 {
117     *src = OH_AVSource_CreateWithURI(const_cast<char *>(uri));
118     ASSERT_NE(*src, nullptr);
119 
120     *audioDemuxer = OH_AVDemuxer_CreateWithSource(*src);
121     ASSERT_NE(*audioDemuxer, nullptr);
122 }
123 
CheckTrackCount(OH_AVFormat ** srcFormat,OH_AVSource * src,int32_t * trackCount,int trackNum)124 static void CheckTrackCount(OH_AVFormat **srcFormat, OH_AVSource *src, int32_t *trackCount, int trackNum)
125 {
126     *srcFormat = OH_AVSource_GetSourceFormat(src);
127     ASSERT_TRUE(OH_AVFormat_GetIntValue(*srcFormat, OH_MD_KEY_TRACK_COUNT, trackCount));
128     ASSERT_EQ(trackNum, *trackCount);
129 }
130 
CheckTrackSelect(int32_t trackCount,OH_AVDemuxer * audioDemuxer)131 static void CheckTrackSelect(int32_t trackCount, OH_AVDemuxer *audioDemuxer)
132 {
133     for (int32_t index = 0; index < g_trackCount; index++) {
134         ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SelectTrackByID(demuxer, index));
135     }
136 }
137 
CountAudioFrames(OH_AVDemuxer * audioDemuxer,OH_AVMemory * mem,int32_t trackCount,int audioFrameNum,int audioKeyNum)138 static void CountAudioFrames(OH_AVDemuxer *audioDemuxer, OH_AVMemory *mem,
139                              int32_t trackCount, int audioFrameNum, int audioKeyNum)
140 {
141     int audioFrame = 0;
142     int keyCount = 0;
143     bool audioIsEnd = false;
144     OH_AVCodecBufferAttr attr;
145 
146     while (!audioIsEnd) {
147         for (int32_t index = 0; index < trackCount; index++) {
148             ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_ReadSample(audioDemuxer, index, mem, &attr));
149             if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_EOS) {
150                 audioIsEnd = true;
151                 cout << audioFrame << "    audio is end !!!!!!!!!!!!!!!" << endl;
152                 continue;
153             }
154 
155             audioFrame++;
156             if (attr.flags & OH_AVCodecBufferFlags::AVCODEC_BUFFER_FLAGS_SYNC_FRAME) {
157                 keyCount++;
158             }
159         }
160     }
161     ASSERT_EQ(audioFrame, audioFrameNum);
162     ASSERT_EQ(keyCount, audioKeyNum);
163 }
164 
165 /**
166  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0001
167  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0001
168  * @tc.desc      : function test
169  */
170 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0001, TestSize.Level2)
171 {
172     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_410_8b_1.wav";
173     OpenUri(uri, &source, &demuxer);
174     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
175     CheckTrackSelect(g_trackCount, demuxer);
176     CountAudioFrames(demuxer, memory, g_trackCount, 1, 1);
177 }
178 
179 /**
180  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0002
181  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0002
182  * @tc.desc      : function test
183  */
184 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0002, TestSize.Level2)
185 {
186     int64_t seekTime = 0;
187     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_410_8b_1.wav";
188     OpenUri(uri, &source, &demuxer);
189     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
190     CheckTrackSelect(g_trackCount, demuxer);
191     ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, seekTime, OH_AVSeekMode::SEEK_MODE_NEXT_SYNC));
192     CountAudioFrames(demuxer, memory, g_trackCount, 1, 1);
193 }
194 
195 /**
196  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0003
197  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0003
198  * @tc.desc      : function test
199  */
200 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0003, TestSize.Level2)
201 {
202     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_8K_8b_2.wav";
203     OpenUri(uri, &source, &demuxer);
204     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
205     CheckTrackSelect(g_trackCount, demuxer);
206     CountAudioFrames(demuxer, memory, g_trackCount, 20, 20);
207 }
208 
209 /**
210  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0004
211  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0004
212  * @tc.desc      : function test
213  */
214 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0004, TestSize.Level2)
215 {
216     int32_t seekTime = 2304000;
217     int32_t thousand = 1000;
218     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_8K_8b_2.wav";
219     OpenUri(uri, &source, &demuxer);
220     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
221     CheckTrackSelect(g_trackCount, demuxer);
222     ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, seekTime / thousand, OH_AVSeekMode::SEEK_MODE_PREVIOUS_SYNC));
223     CountAudioFrames(demuxer, memory, g_trackCount, 11, 11);
224 }
225 
226 /**
227  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0005
228  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0005
229  * @tc.desc      : function test
230  */
231 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0005, TestSize.Level2)
232 {
233     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_16K_8b_2.wav";
234     OpenUri(uri, &source, &demuxer);
235     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
236     CheckTrackSelect(g_trackCount, demuxer);
237     CountAudioFrames(demuxer, memory, g_trackCount, 40, 40);
238 }
239 
240 /**
241  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0006
242  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0006
243  * @tc.desc      : function test
244  */
245 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0006, TestSize.Level2)
246 {
247     int32_t seekTime = 2432000;
248     int32_t thousand = 1000;
249     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_16K_8b_2.wav";
250     OpenUri(uri, &source, &demuxer);
251     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
252     CheckTrackSelect(g_trackCount, demuxer);
253     ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, seekTime / thousand, OH_AVSeekMode::SEEK_MODE_CLOSEST_SYNC));
254     CountAudioFrames(demuxer, memory, g_trackCount, 21, 21);
255 }
256 
257 /**
258  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0007
259  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0007
260  * @tc.desc      : function test
261  */
262 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0007, TestSize.Level2)
263 {
264     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_48K_8b_1.wav";
265     OpenUri(uri, &source, &demuxer);
266     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
267     CheckTrackSelect(g_trackCount, demuxer);
268     CountAudioFrames(demuxer, memory, g_trackCount, 59, 59);
269 }
270 
271 /**
272  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0008
273  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0008
274  * @tc.desc      : function test
275  */
276 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0008, TestSize.Level2)
277 {
278     int32_t seekTime = 2816000;
279     int32_t thousand = 1000;
280     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_48K_8b_1.wav";
281     OpenUri(uri, &source, &demuxer);
282     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
283     CheckTrackSelect(g_trackCount, demuxer);
284     ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, seekTime / thousand, OH_AVSeekMode::SEEK_MODE_NEXT_SYNC));
285     CountAudioFrames(demuxer, memory, g_trackCount, 26, 26);
286 }
287 
288 /**
289  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0009
290  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0009
291  * @tc.desc      : function test
292  */
293 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0009, TestSize.Level2)
294 {
295     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_768001_8b_2.wav";
296     OpenUri(uri, &source, &demuxer);
297     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
298     CheckTrackSelect(g_trackCount, demuxer);
299     CountAudioFrames(demuxer, memory, g_trackCount, 1876, 1876);
300 }
301 
302 /**
303  * @tc.number    : DEMUXER_WAV_ALAW_NET_FUNC_0010
304  * @tc.name      : DEMUXER_WAV_ALAW_NET_FUNC_0010
305  * @tc.desc      : function test
306  */
307 HWTEST_F(DemuxerNetNdkTest, DEMUXER_WAV_ALAW_NET_FUNC_0010, TestSize.Level2)
308 {
309     int32_t seekTime = 4973326;
310     int32_t thousand = 1000;
311     const char *uri = "http://192.168.3.17:8080/share/audio/wav_alaw_768001_8b_2.wav";
312     OpenUri(uri, &source, &demuxer);
313     CheckTrackCount(&sourceFormat, source, &g_trackCount, 1);
314     CheckTrackSelect(g_trackCount, demuxer);
315     ASSERT_EQ(AV_ERR_OK, OH_AVDemuxer_SeekToTime(demuxer, seekTime / thousand, OH_AVSeekMode::SEEK_MODE_PREVIOUS_SYNC));
316     CountAudioFrames(demuxer, memory, g_trackCount, 11, 11);
317 }
318 } // namespace