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
83 DATA_PROVIDER(hlsLinks, 1,
84 DATA_GROUP(std::string("http://ngcdn001.cnr.cn/live/zgzs/index.m3u8")),
85 DATA_GROUP(std::string("http://ls-open.qingting.fm/live/20500010/64k.m3u8?"
86 "deviceid=00000000-0000-0000-0000-000000000000&format=aac")),
87 DATA_GROUP(std::string("https://live.ximalaya.com/radio-first-page-app/live/1011/64.m3u8?transcode=aac")));
88
89 // @test(data="shortMusicUrls", tags=fast)
90 PTEST((std::string url), Test single player play url music, and finished automatically)
91 {
92 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
93 ASSERT_EQ(0, player->SetSource(TestSource(url)));
94 ASSERT_EQ(0, player->Prepare());
95 ASSERT_EQ(0, player->Play());
96 while (player->IsPlaying()) {
97 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
98 }
99 }
100
101 // @test(data="longMusicUrls", tags=fast)
102 PTEST((std::string url), Test single player play url music, and finished manually)
103 {
104 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
105 ASSERT_EQ(0, player->SetSource(TestSource(url)));
106 ASSERT_EQ(0, player->Prepare());
107 ASSERT_EQ(0, player->Play());
108 ASSERT_TRUE(player->IsPlaying());
109 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
110 ASSERT_EQ(0, player->Stop());
111 }
112
113 // @test(data="durationTestMusicUrls", tags=fast)
114 PTEST((std::string url, int32_t expectDuration), Can get music duration)
115 {
116 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
117 ASSERT_EQ(0, player->SetSource(TestSource(url)));
118 ASSERT_EQ(0, player->Prepare());
119 ASSERT_EQ(0, player->Play());
120 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
121 int64_t duration;
122 ASSERT_EQ(0, player->GetDuration(duration));
123 ASSERT_TRUE(CheckTimeEquality(expectDuration, duration));
124 ASSERT_EQ(0, player->Stop());
125 }
126
127 // @test(data="longMusicUrls", tags=fast)
128 PTEST((std::string url), Can get current time)
129 {
130 int64_t currentMS {0};
131 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
132 ASSERT_EQ(0, player->SetSource(TestSource(url)));
133 ASSERT_EQ(0, player->Prepare());
134 ASSERT_EQ(0, player->Play());
135 ASSERT_TRUE(player->IsPlaying());
136 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
137 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
138 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
139 ASSERT_TRUE(CheckTimeEquality(2000, currentMS));
140 ASSERT_EQ(0, player->Stop());
141 }
142
143 // @test(data="longMusicUrls", tags=fast)
144 PTEST((std::string url), Can seek to the position specified)
145 {
146 int64_t seekPos {5000};
147 int64_t currentMS {0};
148 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
149 ASSERT_EQ(0, player->SetSource(TestSource(url)));
150 ASSERT_EQ(0, player->Prepare());
151 ASSERT_EQ(0, player->Play());
152 ASSERT_TRUE(player->IsPlaying());
153 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
154 ASSERT_EQ(0, player->Seek(seekPos));
155 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
156 EXPECT_TRUE(CheckTimeEquality(seekPos, currentMS));
157 std::this_thread::sleep_for(std::chrono::milliseconds(2000));
158 ASSERT_EQ(0, player->Stop());
159 }
160
161 // @test(data="longMusicUrls", tags=fast)
162 PTEST((std::string url), Can seek after pause)
163 {
164 int64_t seekPos {5000};
165 int64_t currentMS {0};
166 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
167 ASSERT_EQ(0, player->SetSource(TestSource(url)));
168 ASSERT_EQ(0, player->Prepare());
169 ASSERT_EQ(0, player->Play());
170 ASSERT_TRUE(player->IsPlaying());
171 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
172 ASSERT_EQ(0, player->Pause());
173 ASSERT_EQ(0, player->Seek(seekPos));
174 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
175 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
176 ASSERT_TRUE(CheckTimeEquality(seekPos, currentMS)); // pause + seek still pause
177 ASSERT_EQ(0, player->Stop());
178 }
179
180 // @test(data="longMusicUrls", tags=fast)
181 PTEST((std::string url), Can pause, then seek, then play, final stop)
182 {
183 int64_t seekPos {5000};
184 int64_t currentMS {0};
185 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
186 ASSERT_EQ(0, player->SetSource(TestSource(url)));
187 ASSERT_EQ(0, player->Prepare());
188 ASSERT_EQ(0, player->Play());
189 ASSERT_TRUE(player->IsPlaying());
190 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
191 ASSERT_EQ(0, player->Pause());
192 ASSERT_EQ(0, player->Seek(seekPos));
193 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
194 ASSERT_EQ(0, player->GetCurrentTime(currentMS));
195 ASSERT_TRUE(CheckTimeEquality(seekPos, currentMS)); // pause + seek still pause
196 ASSERT_EQ(0, player->Play());
197 std::this_thread::sleep_for(std::chrono::milliseconds(2000)); // pause + seek + play still play
198 ASSERT_EQ(0, player->Stop());
199 }
200
201 // @test(data="longMusicUrls", tags=fast)
202 PTEST((std::string url), Can play after pause)
203 {
204 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
205 ASSERT_EQ(0, player->SetSource(TestSource(url)));
206 ASSERT_EQ(0, player->Prepare());
207 ASSERT_EQ(0, player->Play());
208 ASSERT_TRUE(player->IsPlaying());
209 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
210 ASSERT_EQ(0, player->Pause());
211 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
212 ASSERT_EQ(0, player->Play());
213 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
214 ASSERT_EQ(0, player->Stop());
215 }
216
217 // @test(data="shortMusicUrls", tags=fast)
218 PTEST((std::string url), Can single Loop)
219 {
220 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
221 ASSERT_EQ(0, player->SetSource(TestSource(url)));
222 ASSERT_EQ(0, player->SetSingleLoop(true));
223 ASSERT_EQ(0, player->Prepare());
224 ASSERT_EQ(0, player->Play());
225 std::this_thread::sleep_for(std::chrono::seconds(20));
226 ASSERT_EQ(0, player->Stop());
227 }
228
229 // @test(data="httpLinks", tags=http)
230 PTEST((std::string url), Test http audio play)
231 {
232 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
233 ASSERT_EQ(0, player->SetSource(TestSource(url)));
234 ASSERT_EQ(0, player->Prepare());
235 ASSERT_EQ(0, player->Play());
236 std::this_thread::sleep_for(std::chrono::seconds(20));
237 ASSERT_EQ(0, player->Seek(10000));
238 std::this_thread::sleep_for(std::chrono::seconds(10));
239 player->Stop();
240 }
241
242 // @test(data="hlsLinks", tags=http)
243 PTEST((std::string url), Test hls audio broadcast)
244 {
245 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
246 ASSERT_EQ(0, player->SetSource(TestSource(url)));
247 ASSERT_EQ(0, player->Prepare());
248 ASSERT_EQ(0, player->Play());
249 std::this_thread::sleep_for(std::chrono::seconds(20));
250 player->Stop();
251 }
252
253 // @test(data="FileFdSourceTestShortMusicUrls", tags=fast)
254 PTEST((std::string url, int32_t fileSize), Can fd play single Loop)
255 {
256 std::string uri = "fd://?offset=0&size=";
257 uri += std::to_string(fileSize);
258 int32_t fd = open(url.c_str(), O_RDONLY|O_BINARY);
259 std::string fdStr = std::to_string(fd);
260 uri.insert(5, fdStr);
261 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
262 ASSERT_EQ(0, player->SetSource(TestSource(uri)));
263 ASSERT_EQ(0, player->SetSingleLoop(true));
264 ASSERT_EQ(0, player->Prepare());
265 ASSERT_EQ(0, player->Play());
266 std::this_thread::sleep_for(std::chrono::seconds(20));
267 ASSERT_EQ(0, player->Stop());
268 }
269
270 // SUB_MEDIA_PLAYER_LOCAL_AUDIO_FUNCTION_LOOP
271 // @test(data="durationTestMusicUrls", tags=fast)
272 PTEST((std::string url, int32_t expectDuration), Seek to the location of the file near the end)
273 {
274 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
275 ASSERT_EQ(0, player->SetSource(TestSource(url)));
276 ASSERT_EQ(0, player->Prepare());
277 ASSERT_EQ(0, player->Play());
278 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
279 int64_t duration;
280 ASSERT_EQ(0, player->GetDuration(duration));
281 ASSERT_TRUE(CheckTimeEquality(expectDuration, duration));
282 int32_t count = 5;
283 while (count > 0) {
284 NZERO_LOG(player->Seek(duration - 100));
285 NZERO_LOG(player->Seek(0));
286 count--;
287 }
288 ASSERT_EQ(0, player->Reset());
289 ASSERT_EQ(0, player->Release());
290 }
291
292 // @test(data="shortMusicUrls", tags=fast)
293 PTEST((std::string url), Test play finished can play again)
294 {
295 std::unique_ptr<TestPlayer> player = TestPlayer::Create();
296 ASSERT_EQ(0, player->SetSource(TestSource(url)));
297 ASSERT_EQ(0, player->Prepare());
298 ASSERT_EQ(0, player->Play());
299 while (player->IsPlaying()) {
300 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
301 }
302 ASSERT_EQ(0, player->Play());
303 while (player->IsPlaying()) {
304 std::this_thread::sleep_for(std::chrono::milliseconds(1000));
305 }
306 }
307 };
308