1 /*
2  * Copyright (c) 2023-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 #ifndef TEST_SINGLE_VIDEO_PLAYER_FAST_1_H
16 #define TEST_SINGLE_VIDEO_PLAYER_FAST_1_H
17 
18 #include <chrono>
19 #include <fcntl.h>
20 #ifndef WIN32
21 #include <sys/types.h>
22 #include <unistd.h>
23 #define O_BINARY 0 // which is not defined for Linux
24 #endif
25 #include <math.h>
26 #include <thread>
27 #include "foundation/log.h"
28 #include "helper/test_player.hpp"
29 #include "testngpp/testngpp.hpp"
30 
31 using namespace OHOS::Media::Test;
32 
33 // @fixture(tags=video_play_fast)
FIXTURE(dataDrivenSingleVideoPlayerTestFast1)34 FIXTURE(dataDrivenSingleVideoPlayerTestFast1)
35 {
36     DATA_PROVIDER(myurls, 1,
37                   DATA_GROUP(std::string(RESOURCE_DIR "/MP4/H264_AAC.mp4")));
38     DATA_PROVIDER(myfdurl, 2,
39                   DATA_GROUP(std::string(RESOURCE_DIR "/MP4/H264_AAC.mp4"), 1894335),
40                   DATA_GROUP(std::string(RESOURCE_DIR "/../demo_resource/video/1h264_320x240_60.3gp"), 494522));
41     static const int64_t NEXT_FRAME_TIME {8300};
42     std::string FilePathToFd(std::string url, int32_t fileSize)
43     {
44         std::string uri = "fd://?offset=0&size=";
45         uri += std::to_string(fileSize);
46         int32_t fd = open(url.c_str(), O_RDONLY|O_BINARY);
47         std::string fdStr = std::to_string(fd);
48         uri.insert(5, fdStr); // 5 ---fd:://
49         return uri;
50     }
51     bool CheckTimeEquality(int32_t expectValue, int32_t currentValue)
52     {
53         MEDIA_LOG_I("expectValue : %d, currentValue : %d", expectValue, currentValue);
54         return fabs(expectValue - currentValue) < 100; // if open debug log, should use value >= 1000
55     }
56 
57     // @test(data="myurls", tags=video_play_fast)
58     PTEST((std::string url), Test single player play url video, and finished automatically)
59     {
60         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
61         ASSERT_EQ(0, player->SetSource(TestSource(url)));
62         ASSERT_EQ(0, player->Prepare());
63         ASSERT_EQ(0, player->Play());
64         while (player->IsPlaying()) {
65             std::this_thread::sleep_for(std::chrono::milliseconds(1000));
66         }
67     }
68 
69     // @test(data="myfdurl", tags=video_play_fast)
70     PTEST((std::string url, int32_t fileSize), Test single player play fd source, and finished automatically)
71     {
72         std::string uri = FilePathToFd(url, fileSize);
73         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
74         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
75         ASSERT_EQ(0, player->Prepare());
76         ASSERT_EQ(0, player->Play());
77         while (player->IsPlaying()) {
78             std::this_thread::sleep_for(std::chrono::milliseconds(1000));
79         }
80     }
81 
82     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_fdSrc_CALLBACK_0100
83     // @test(data="myfdurl", tags=video_play_fast)
84     PTEST((std::string url, int32_t fileSize), Test single player wrong fd)
85     {
86         std::string uri = "fd://?offset=0&size=";
87         uri += std::to_string(fileSize);
88         uri.insert(5, "-1"); // 5 ---fd:://
89         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
90         ASSERT_NE(0, player->SetSource(TestSource(uri)));
91         ASSERT_NE(0, player->Prepare());
92         ASSERT_EQ(0, player->Release());
93     }
94 
95     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_fdSrc_CALLBACK_0200
96     // @test(data="myurls", tags=video_play_fast)
97     PTEST((std::string url), Test fdsource prepare, play, pause, release)
98     {
99         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
100         ASSERT_EQ(0, player->SetSource(TestSource(url)));
101         ASSERT_EQ(0, player->Prepare());
102         ASSERT_EQ(0, player->Play());
103         ASSERT_EQ(0, player->Pause());
104         ASSERT_EQ(0, player->Release());
105     }
106 
107     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_fdSrc_CALLBACK_0300
108     // @test(data="myurls", tags=video_play_fast)
109     PTEST((std::string url), Test fdsource prepare, play, pause, release)
110     {
111         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
112         ASSERT_EQ(0, player->SetSource(TestSource(url)));
113         ASSERT_EQ(0, player->Prepare());
114         ASSERT_EQ(0, player->Play());
115         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
116         ASSERT_EQ(0, player->Pause());
117         ASSERT_EQ(0, player->Release());
118     }
119 
120     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_fdSrc_CALLBACK_0400
121     // @test(data="myfdurl", tags=video_play_fast)
122     PTEST((std::string url, int32_t fileSize), Test fdsource prepare then release)
123     {
124         std::string uri = "fd://?offset=0&size=";
125         uri += std::to_string(fileSize);
126         uri.insert(5, "-123456789"); // 5 ---fd:://
127         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
128         ASSERT_NE(0, player->SetSource(TestSource(uri)));
129         ASSERT_NE(0, player->Prepare());
130         ASSERT_EQ(0, player->Release());
131     }
132 
133     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0100
134     // @test(data="myfdurl", tags=video_play_fast)
135     PTEST((std::string url, int32_t fileSize), Test fdsource prepare then release)
136     {
137         std::string uri = FilePathToFd(url, fileSize);
138         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
139         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
140         ASSERT_EQ(0, player->Prepare());
141         ASSERT_EQ(0, player->Release());
142     }
143 
144     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0200
145     // @test(data="myfdurl", tags=video_play_fast)
146     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, prepare, release)
147     {
148         std::string uri = FilePathToFd(url, fileSize);
149         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
150         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
151         ASSERT_EQ(0, player->Prepare());
152         ASSERT_EQ(0, player->Play());
153         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
154         ASSERT_NE(0, player->Prepare());
155         ASSERT_EQ(0, player->Release());
156     }
157 
158     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0300
159     // @test(data="myfdurl", tags=video_play_fast)
160     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, pause, prepare, release)
161     {
162         std::string uri = FilePathToFd(url, fileSize);
163         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
164         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
165         ASSERT_EQ(0, player->Prepare());
166         ASSERT_EQ(0, player->Play());
167         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
168         ASSERT_EQ(0, player->Pause());
169         ASSERT_NE(0, player->Prepare());
170         ASSERT_EQ(0, player->Release());
171     }
172 
173     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0400
174     // @test(data="myfdurl", tags=video_play_fast)
175     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, stop, prpeare, release)
176     {
177         std::string uri = FilePathToFd(url, fileSize);
178         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
179         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
180         ASSERT_EQ(0, player->Prepare());
181         ASSERT_EQ(0, player->Play());
182         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
183         ASSERT_EQ(0, player->Stop());
184         ASSERT_EQ(0, player->Prepare());
185         ASSERT_EQ(0, player->Play());
186         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
187         ASSERT_EQ(0, player->Release());
188     }
189 
190     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0500
191     // @test(data="myfdurl", tags=video_play_fast)
192     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, reset, setsource, prepare, release)
193     {
194         std::string uri = FilePathToFd(url, fileSize);
195         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
196         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
197         ASSERT_EQ(0, player->Prepare());
198         ASSERT_EQ(0, player->Play());
199         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
200         ASSERT_EQ(0, player->Reset());
201         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
202         ASSERT_EQ(0, player->Prepare());
203         ASSERT_EQ(0, player->Release());
204     }
205 
206     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0600/0700
207     // @test(data="myfdurl", tags=video_play_fast)
208     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, Seek, prepare, release)
209     {
210         int64_t seekPos {5000};
211         int64_t currentMS {0};
212         std::string uri = FilePathToFd(url, fileSize);
213         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
214         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
215         ASSERT_EQ(0, player->Prepare());
216         ASSERT_EQ(0, player->Play());
217         ASSERT_TRUE(player->IsPlaying());
218         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
219         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
220         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
221         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
222         ASSERT_NE(0, player->Prepare());
223         ASSERT_EQ(0, player->Release());
224     }
225 
226     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_0800
227     // @test(data="myfdurl", tags=video_play_fast)
228     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setvolume, prepare, release)
229     {
230         float leftVolume {1};
231         float rightVolume {1};
232         std::string uri = FilePathToFd(url, fileSize);
233         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
234         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
235         ASSERT_EQ(0, player->Prepare());
236         ASSERT_EQ(0, player->Play());
237         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
238         ASSERT_NE(0, player->Prepare());
239         ASSERT_EQ(0, player->Release());
240     }
241 
242     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_1000
243     // @test(data="myfdurl", tags=video_play_fast)
244     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, release)
245     {
246         std::string uri = FilePathToFd(url, fileSize);
247         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
248         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
249         ASSERT_EQ(0, player->Prepare());
250         ASSERT_EQ(0, player->Release());
251     }
252 
253     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PREPARE_CALLBACK_1200
254     // @test(data="myfdurl", tags=video_play_fast)
255     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, prepare, prepare, release)
256     {
257         std::string uri = FilePathToFd(url, fileSize);
258         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
259         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
260         ASSERT_EQ(0, player->Prepare());
261         ASSERT_NE(0, player->Prepare());
262         ASSERT_NE(0, player->Prepare());
263         ASSERT_EQ(0, player->Release());
264     }
265 
266     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0100
267     // @test(data="myfdurl", tags=video_play_fast)
268     PTEST((std::string url, int32_t fileSize), Test fdsource create, play, release)
269     {
270         std::string uri = FilePathToFd(url, fileSize);
271         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
272         ASSERT_NE(0, player->Play());
273         ASSERT_EQ(0, player->Release());
274     }
275 
276     // // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0200
277     // @test(data="myfdurl", tags=video_play_fast)
278     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, release)
279     {
280         std::string uri = FilePathToFd(url, fileSize);
281         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
282         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
283         ASSERT_EQ(0, player->Prepare());
284         ASSERT_EQ(0, player->Play());
285         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
286         ASSERT_EQ(0, player->Release());
287     }
288 
289     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0300
290     // @test(data="myfdurl", tags=video_play_fast)
291     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, pause, play, release)
292     {
293         std::string uri = FilePathToFd(url, fileSize);
294         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
295         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
296         ASSERT_EQ(0, player->Prepare());
297         ASSERT_EQ(0, player->Play());
298         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
299         ASSERT_EQ(0, player->Pause());
300         ASSERT_EQ(0, player->Play());
301         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
302         ASSERT_EQ(0, player->Release());
303     }
304 
305     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0400
306     // @test(data="myfdurl", tags=video_play_fast)
307     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, stop, play, release)
308     {
309         std::string uri = FilePathToFd(url, fileSize);
310         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
311         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
312         ASSERT_EQ(0, player->Prepare());
313         ASSERT_EQ(0, player->Play());
314         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
315         ASSERT_EQ(0, player->Stop());
316         ASSERT_NE(0, player->Play());
317         ASSERT_EQ(0, player->Release());
318     }
319 
320     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0500
321     // @test(data="myfdurl", tags=video_play_fast)
322     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, reset, play, release)
323     {
324         std::string uri = FilePathToFd(url, fileSize);
325         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
326         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
327         ASSERT_EQ(0, player->Prepare());
328         ASSERT_EQ(0, player->Play());
329         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
330         ASSERT_EQ(0, player->Reset());
331         ASSERT_NE(0, player->Play());
332         ASSERT_EQ(0, player->Release());
333     }
334 
335     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0600
336     // @test(data="myfdurl", tags=video_play_fast)
337     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, release, 600)
338     {
339         int64_t seekPos {5000};
340         int64_t currentMS {0};
341         std::string uri = FilePathToFd(url, fileSize);
342         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
343         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
344         ASSERT_EQ(0, player->Prepare());
345         ASSERT_EQ(0, player->Play());
346         ASSERT_TRUE(player->IsPlaying());
347         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
348         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
349         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
350         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
351         ASSERT_EQ(0, player->Release());
352     }
353 
354     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0800
355     // @test(data="myfdurl", tags=video_play_fast)
356     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setvolume, release)
357     {
358         float leftVolume {1};
359         float rightVolume {1};
360         std::string uri = FilePathToFd(url, fileSize);
361         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
362         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
363         ASSERT_EQ(0, player->Prepare());
364         ASSERT_EQ(0, player->Play());
365         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
366         ASSERT_EQ(0, player->Release());
367     }
368 
369     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_1000
370     // @test(data="myfdurl", tags=video_play_fast)
371     PTEST((std::string url, int32_t fileSize), Test fdsource  play, release)
372     {
373         std::string uri = FilePathToFd(url, fileSize);
374         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
375         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
376         ASSERT_NE(0, player->Play());
377         ASSERT_EQ(0, player->Release());
378     }
379 
380     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_1200
381     // @test(data="myfdurl", tags=video_play_fast)
382     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, play, play, release)
383     {
384         std::string uri = FilePathToFd(url, fileSize);
385         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
386         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
387         ASSERT_EQ(0, player->Prepare());
388         ASSERT_EQ(0, player->Play());
389         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
390         ASSERT_NE(0, player->Play());
391         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
392         ASSERT_NE(0, player->Play());
393         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
394         ASSERT_EQ(0, player->Release());
395     }
396 };
397 #endif