1 /*
2 * Copyright (c) 2022-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 #include <chrono>
16 #include <fcntl.h>
17 #ifndef WIN32
18 #include <sys/types.h>
19 #include <unistd.h>
20 #define O_BINARY 0 // which is not defined for Linux
21 #endif
22 #include <math.h>
23 #include <thread>
24 #include "helper/test_player.hpp"
25 #include "testngpp/testngpp.hpp"
26 #include "foundation/log.h"
27
28 using namespace OHOS::Media::Test;
29
30 // @fixture(tags=audio_play_fast)
FIXTURE(DataDrivenSinglePlayerTestFast)31 FIXTURE(DataDrivenSinglePlayerTestFast)
32 {
33 bool CheckTimeEquality(int32_t expectValue, int32_t currentValue)
34 {
35 MEDIA_LOG_I("expectValue : %d, currentValue : %d", expectValue, currentValue);
36 return fabs(expectValue - currentValue) < 1000; // if open debug log, should use value >= 1000
37 }
38
39 DATA_PROVIDER(myurls, 1,
40 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_LONG_48000_32.mp3")),
41 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_48000_32_SHORT.mp3")),
42 DATA_GROUP(std::string(RESOURCE_DIR "/AAC/AAC_48000_32_SHORT.aac")),
43 DATA_GROUP(std::string(RESOURCE_DIR "/FLAC/vorbis_48000_32_SHORT.flac")),
44 DATA_GROUP(std::string(RESOURCE_DIR "/M4A/MPEG-4_48000_32_SHORT.m4a")),
45 DATA_GROUP(std::string(RESOURCE_DIR "/WAV/vorbis_48000_32_SHORT.wav")),
46 DATA_GROUP(std::string("http://localhost/resource-src/media/MP3/MP3_48000_32_SHORT.mp3")));
47
48 DATA_PROVIDER(shortMusicUrls, 1,
49 DATA_GROUP(std::string(RESOURCE_DIR "/M4A/MPEG-4_48000_32_SHORT.m4a")),
50 DATA_GROUP(std::string(RESOURCE_DIR "/WAV/vorbis_48000_32_SHORT.wav")),
51 DATA_GROUP(std::string(RESOURCE_DIR "/AAC/AAC_48000_32_SHORT.aac")),
52 DATA_GROUP(std::string(RESOURCE_DIR "/FLAC/vorbis_48000_32_SHORT.flac")),
53 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_LONG_48000_32.mp3")),
54 DATA_GROUP(std::string(RESOURCE_DIR "/WAV/vorbis_48000_32_SHORT.wav")),
55 DATA_GROUP(std::string("http://localhost/resource-src/media/MP3/MP3_48000_32_SHORT.mp3")));
56
57 DATA_PROVIDER(longMusicUrls, 1,
58 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_LONG_48000_32.mp3")),
59 DATA_GROUP(std::string(RESOURCE_DIR "/M4A/MPEG-4_48000_32_LONG.m4a")),
60 DATA_GROUP(std::string("http://img.51miz.com/preview/sound/00/26/73/51miz-S267356-423D33372.mp3")),
61 DATA_GROUP(std::string("http://localhost/resource-src/media/MP3/MP3_LONG_48000_32.mp3")));
62
63 DATA_PROVIDER(durationTestMusicUrls, 2,
64 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_LONG_48000_32.mp3"), 30000),
65 DATA_GROUP(std::string(RESOURCE_DIR "/AAC/AAC_LONG_48000_32.aac"), 30000),
66 DATA_GROUP(std::string(RESOURCE_DIR "/FLAC/FLAC_48000_32_LONG.flac"), 30000),
67 DATA_GROUP(std::string(RESOURCE_DIR "/WAV/vorbis_48000_32_SHORT.wav"), 5000),
68 DATA_GROUP(std::string(RESOURCE_DIR "/M4A/MPEG-4_48000_32_LONG.m4a"), 30000));
69
70 DATA_PROVIDER(FileFdSourceTestShortMusicUrls, 2,
71 DATA_GROUP(std::string(RESOURCE_DIR "/M4A/MPEG-4_48000_32_SHORT.m4a"), 36715),
72 DATA_GROUP(std::string(RESOURCE_DIR "/WAV/vorbis_48000_32_SHORT.wav"), 524472),
73 DATA_GROUP(std::string(RESOURCE_DIR "/AAC/AAC_48000_32_SHORT.aac"), 49233),
74 DATA_GROUP(std::string(RESOURCE_DIR "/FLAC/vorbis_48000_32_SHORT.flac"), 217292),
75 DATA_GROUP(std::string(RESOURCE_DIR "/MP3/MP3_48000_32_SHORT.mp3"), 24697),
76 DATA_GROUP(std::string(RESOURCE_DIR "/OGG/OGG_48000_SHORT.ogg"), 30912),
77 DATA_GROUP(std::string(RESOURCE_DIR "/APE/MPEG-4_48000_32_SHORT.ape"), 161018));
78
79 DATA_PROVIDER(httpLinks, 1,
80 DATA_GROUP(std::string("http://img.51miz.com/preview/sound/00/26/73/51miz-S267356-423D33372.mp3")),
81 DATA_GROUP(std::string("https://img.51miz.com/preview/sound/00/26/73/51miz-S267356-423D33372.mp3")),
82 DATA_GROUP(std::string("https://music-common-ucdn-drcn.dbankcdn.com/29c9ac31326661bb9aaf1206c66e58d5/"
83 "8e0271ac3819c5d724e0b999577c0763/8e0271ac3819c5d724e0b999577c0763_c7697dac-a62c-4764-9627-d16d808abe99_1_MQ"
84 ".mp3?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=MPXW6JOQOEGGIVNZK2PN 2F20220614 2Fcn-north-2 2Fs3 2Fa"
85 "ws4_request&X-Amz-Date=20220614T014334Z&X-Amz-Expires=173000&X-Amz-SignedHeaders=host&X-Amz-Signature=912078"
86 "155a6e32a00a0faed839f17dc95b69a34db0ff1cc73b7a72964f15610b"))
87 );
88
89 DATA_PROVIDER(hlsLinks, 1,
90 DATA_GROUP(std::string("http://ngcdn001.cnr.cn/live/zgzs/index.m3u8")),
91 DATA_GROUP(std::string("http://ls-open.qingting.fm/live/20500010/64k.m3u8?"
92 "deviceid=00000000-0000-0000-0000-000000000000&format=aac")),
93 DATA_GROUP(std::string("https://live.ximalaya.com/radio-first-page-app/live/1011/64.m3u8?transcode=aac")));
94
95 // @test(data="shortMusicUrls", tags=fast)
96 PTEST((std::string url), Test single player play url music, and finished automatically)
97 {
98 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
99 ASSERT_EQ(0, player->SetSource(TestSource(url)));
100 ASSERT_EQ(0, player->Prepare());
101 ASSERT_EQ(0, player->Play());
102 while (player->IsPlaying()) {
103 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
104 }
105 }
106
107 // @test(data="longMusicUrls", tags=fast)
108 PTEST((std::string url), Test single player play url music, and finished manually)
109 {
110 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
111 ASSERT_EQ(0, player->SetSource(TestSource(url)));
112 ASSERT_EQ(0, player->Prepare());
113 ASSERT_EQ(0, player->Play());
114 ASSERT_TRUE(player->IsPlaying());
115 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
116 ASSERT_EQ(0, player->Stop());
117 }
118
119 // @test(data="durationTestMusicUrls", tags=fast)
120 PTEST((std::string url, int32_t expectDuration), Can get music duration)
121 {
122 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
123 ASSERT_EQ(0, player->SetSource(TestSource(url)));
124 ASSERT_EQ(0, player->Prepare());
125 ASSERT_EQ(0, player->Play());
126 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
127 int64_t duration;
128 ASSERT_EQ(0, player->GetDuration(duration));
129 ASSERT_TRUE(CheckTimeEquality(expectDuration, duration));
130 ASSERT_EQ(0, player->Stop());
131 }
132
133 // @test(data="longMusicUrls", tags=fast)
134 PTEST((std::string url), Can get current time)
135 {
136 int64_t currentMS {0};
137 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
138 ASSERT_EQ(0, player->SetSource(TestSource(url)));
139 ASSERT_EQ(0, player->Prepare());
140 ASSERT_EQ(0, player->Play());
141 ASSERT_TRUE(player->IsPlaying());
142 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
143 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
144 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
145 ASSERT_TRUE(CheckTimeEquality(2000, currentMS));
146 ASSERT_EQ(0, player->Stop());
147 }
148
149 // @test(data="longMusicUrls", tags=fast)
150 PTEST((std::string url), Can seek to the position specified)
151 {
152 int64_t seekPos {5000};
153 int64_t currentMS {0};
154 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
155 ASSERT_EQ(0, player->SetSource(TestSource(url)));
156 ASSERT_EQ(0, player->Prepare());
157 ASSERT_EQ(0, player->Play());
158 ASSERT_TRUE(player->IsPlaying());
159 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
160 ASSERT_EQ(0, player->Seek(seekPos));
161 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
162 EXPECT_TRUE(CheckTimeEquality(seekPos, currentMS));
163 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
164 ASSERT_EQ(0, player->Stop());
165 }
166
167 // @test(data="longMusicUrls", tags=fast)
168 PTEST((std::string url), Can seek after pause)
169 {
170 int64_t seekPos {5000};
171 int64_t currentMS {0};
172 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
173 ASSERT_EQ(0, player->SetSource(TestSource(url)));
174 ASSERT_EQ(0, player->Prepare());
175 ASSERT_EQ(0, player->Play());
176 ASSERT_TRUE(player->IsPlaying());
177 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
178 ASSERT_EQ(0, player->Pause());
179 ASSERT_EQ(0, player->Seek(seekPos));
180 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
181 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
182 ASSERT_TRUE(CheckTimeEquality(seekPos, currentMS)); // pause + seek still pause
183 ASSERT_EQ(0, player->Stop());
184 }
185
186 // @test(data="longMusicUrls", tags=fast)
187 PTEST((std::string url), Can pause,then seek,then play,final stop)
188 {
189 int64_t seekPos {5000};
190 int64_t currentMS {0};
191 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
192 ASSERT_EQ(0, player->SetSource(TestSource(url)));
193 ASSERT_EQ(0, player->Prepare());
194 ASSERT_EQ(0, player->Play());
195 ASSERT_TRUE(player->IsPlaying());
196 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
197 ASSERT_EQ(0, player->Pause());
198 ASSERT_EQ(0, player->Seek(seekPos));
199 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
200 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
201 ASSERT_TRUE(CheckTimeEquality(seekPos, currentMS)); // pause + seek still pause
202 ASSERT_EQ(0, player->Play());
203 std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // pause + seek + play still play
204 ASSERT_EQ(0, player->Stop());
205 }
206
207 // @test(data="longMusicUrls", tags=fast)
208 PTEST((std::string url), Can play after pause)
209 {
210 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
211 ASSERT_EQ(0, player->SetSource(TestSource(url)));
212 ASSERT_EQ(0, player->Prepare());
213 ASSERT_EQ(0, player->Play());
214 ASSERT_TRUE(player->IsPlaying());
215 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
216 ASSERT_EQ(0, player->Pause());
217 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
218 ASSERT_EQ(0, player->Play());
219 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
220 ASSERT_EQ(0, player->Stop());
221 }
222
223 // @test(data="shortMusicUrls", tags=fast)
224 PTEST((std::string url), Can single Loop)
225 {
226 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
227 ASSERT_EQ(0, player->SetSource(TestSource(url)));
228 ASSERT_EQ(0, player->SetSingleLoop(true));
229 ASSERT_EQ(0, player->Prepare());
230 ASSERT_EQ(0, player->Play());
231 std::this_thread::sleep_for(std::chrono::seconds(20));
232 ASSERT_EQ(0, player->Stop());
233 }
234
235 // @test(data="httpLinks", tags=http)
236 PTEST((std::string url), Test http audio play)
237 {
238 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
239 ASSERT_EQ(0, player->SetSource(TestSource(url)));
240 ASSERT_EQ(0, player->Prepare());
241 ASSERT_EQ(0, player->Play());
242 std::this_thread::sleep_for(std::chrono::seconds(20));
243 ASSERT_EQ(0, player->Seek(10000));
244 std::this_thread::sleep_for(std::chrono::seconds(10));
245 player->Stop();
246 }
247
248 // @test(data="hlsLinks", tags=http)
249 PTEST((std::string url), Test hls audio broadcast)
250 {
251 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
252 ASSERT_EQ(0, player->SetSource(TestSource(url)));
253 ASSERT_EQ(0, player->Prepare());
254 ASSERT_EQ(0, player->Play());
255 std::this_thread::sleep_for(std::chrono::seconds(20));
256 player->Stop();
257 }
258
259 // @test(data="FileFdSourceTestShortMusicUrls", tags=fast)
260 PTEST((std::string url, int32_t fileSize), Can fd play single Loop)
261 {
262 std::string uri = "fd://?offset=0&size=";
263 uri += std::to_string(fileSize);
264 int32_t fd = open(url.c_str(), O_RDONLY|O_BINARY);
265 std::string fdStr = std::to_string(fd);
266 uri.insert(5, fdStr);
267 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
268 ASSERT_EQ(0, player->SetSource(TestSource(uri)));
269 ASSERT_EQ(0, player->SetSingleLoop(true));
270 ASSERT_EQ(0, player->Prepare());
271 ASSERT_EQ(0, player->Play());
272 std::this_thread::sleep_for(std::chrono::seconds(20));
273 ASSERT_EQ(0, player->Stop());
274 }
275
276 // SUB_MEDIA_PLAYER_LOCAL_AUDIO_FUNCTION_LOOP
277 // @test(data="durationTestMusicUrls", tags=fast)
278 PTEST((std::string url, int32_t expectDuration), Seek to the location of the file near the end)
279 {
280 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
281 ASSERT_EQ(0, player->SetSource(TestSource(url)));
282 ASSERT_EQ(0, player->Prepare());
283 ASSERT_EQ(0, player->Play());
284 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
285 int64_t duration;
286 ASSERT_EQ(0, player->GetDuration(duration));
287 ASSERT_TRUE(CheckTimeEquality(expectDuration, duration));
288 int32_t count = 5;
289 while (count > 0) {
290 NZERO_LOG(player->Seek(duration - 100));
291 NZERO_LOG(player->Seek(0));
292 count--;
293 }
294 ASSERT_EQ(0, player->Reset());
295 ASSERT_EQ(0, player->Release());
296 }
297
298 // @test(data="shortMusicUrls", tags=fast)
299 PTEST((std::string url), Test play finished can play again)
300 {
301 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
302 ASSERT_EQ(0, player->SetSource(TestSource(url)));
303 ASSERT_EQ(0, player->Prepare());
304 ASSERT_EQ(0, player->Play());
305 while (player->IsPlaying()) {
306 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
307 }
308 ASSERT_EQ(0, player->Play());
309 while (player->IsPlaying()) {
310 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
311 }
312 }
313 };
314