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 HISTREAMER_TEST_SINGLE_VIDEO_PLAYER_FAST_3_H
16 #define HISTREAMER_TEST_SINGLE_VIDEO_PLAYER_FAST_3_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 #include "test_single_video_player_fast_1.h"
31 
32 using namespace OHOS::Media::Test;
33 
34 // @fixture(tags=video_play_fast)
FIXTURE(dataDrivenSingleVideoPlayerTestFast3)35 FIXTURE(dataDrivenSingleVideoPlayerTestFast3)
36 {
37     DATA_PROVIDER(myfdurl, 2,
38                   DATA_GROUP(std::string(RESOURCE_DIR "/MP4/H264_AAC.mp4"), 1894335),
39                   DATA_GROUP(std::string(RESOURCE_DIR "/../demo_resource/video/1h264_320x240_60.3gp"), 494522));
40     static const int64_t NEXT_FRAME_TIME {8300};
41     static const int64_t PREV_FRAME_TIME {4166};
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     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0100
57     // @test(data="myfdurl", tags=video_play_fast)
58     PTEST((std::string url, int32_t fileSize), Test fdsource create, reset)
59     {
60         std::string uri = FilePathToFd(url, fileSize);
61         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
62         ASSERT_EQ(0, player->Release());
63     }
64 
65     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0200
66     // @test(data="myfdurl", tags=video_play_fast)
67     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, release)
68     {
69         std::string uri = FilePathToFd(url, fileSize);
70         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
71         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
72         ASSERT_EQ(0, player->Prepare());
73         ASSERT_EQ(0, player->Release());
74     }
75 
76     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0300
77     // @test(data="myfdurl", tags=video_play_fast)
78     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, release)
79     {
80         std::string uri = FilePathToFd(url, fileSize);
81         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
82         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
83         ASSERT_EQ(0, player->Prepare());
84         ASSERT_EQ(0, player->Play());
85         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
86         ASSERT_EQ(0, player->Release());
87     }
88 
89     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0400
90     // @test(data="myfdurl", tags=video_play_fast)
91     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, pause, release)
92     {
93         std::string uri = FilePathToFd(url, fileSize);
94         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
95         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
96         ASSERT_EQ(0, player->Prepare());
97         ASSERT_EQ(0, player->Play());
98         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
99         ASSERT_EQ(0, player->Pause());
100         ASSERT_EQ(0, player->Release());
101     }
102 
103     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0500
104     // @test(data="myfdurl", tags=video_play_fast)
105     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, stop, release)
106     {
107         std::string uri = FilePathToFd(url, fileSize);
108         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
109         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
110         ASSERT_EQ(0, player->Prepare());
111         ASSERT_EQ(0, player->Play());
112         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
113         ASSERT_EQ(0, player->Stop());
114         ASSERT_EQ(0, player->Release());
115     }
116 
117     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0700
118     // @test(data="myfdurl", tags=video_play_fast)
119     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, release, 700)
120     {
121         int64_t seekPos {5000};
122         int64_t currentMS {0};
123         std::string uri = FilePathToFd(url, fileSize);
124         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
125         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
126         ASSERT_EQ(0, player->Prepare());
127         ASSERT_EQ(0, player->Play());
128         ASSERT_TRUE(player->IsPlaying());
129         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
130         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
131         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
132         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
133         ASSERT_EQ(0, player->Release());
134     }
135 
136     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_0800
137     // @test(data="myfdurl", tags=video_play_fast)
138     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setvolume, release)
139     {
140         float leftVolume {1};
141         float rightVolume {1};
142         std::string uri = FilePathToFd(url, fileSize);
143         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
144         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
145         ASSERT_EQ(0, player->Prepare());
146         ASSERT_EQ(0, player->Play());
147         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
148         ASSERT_EQ(0, player->Release());
149     }
150 
151     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_1000
152     // @test(data="myfdurl", tags=video_play_fast)
153     PTEST((std::string url, int32_t fileSize), Test fdsource setSource, release)
154     {
155         std::string uri = FilePathToFd(url, fileSize);
156         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
157         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
158         ASSERT_EQ(0, player->Release());
159     }
160 
161     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_RELEASE_CALLBACK_1200
162     // @test(data="myfdurl", tags=video_play_fast)
163     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, reset, release)
164     {
165         std::string uri = FilePathToFd(url, fileSize);
166         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
167         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
168         ASSERT_EQ(0, player->Prepare());
169         ASSERT_EQ(0, player->Reset());
170         ASSERT_EQ(0, player->Release());
171     }
172 
173     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0100
174     // @test(data="myfdurl", tags=video_play_fast)
175     PTEST((std::string url, int32_t fileSize), Test fdsource create, seek, release)
176     {
177         int64_t seekPos {5000};
178         int64_t currentMS {0};
179         std::string uri = FilePathToFd(url, fileSize);
180         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
181         ASSERT_NE(0, player->Seek(seekPos));
182         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
183         ASSERT_EQ(0, player->Release());
184     }
185 
186     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0200
187     // @test(data="myfdurl", tags=video_play_fast)
188     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, seek, release)
189     {
190         int64_t seekPos {5000};
191         int64_t currentMS {0};
192         std::string uri = FilePathToFd(url, fileSize);
193         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
194         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
195         ASSERT_EQ(0, player->Prepare());
196         ASSERT_EQ(0, player->Seek(seekPos));
197         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
198         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
199         ASSERT_EQ(0, player->Release());
200     }
201 
202     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0300
203     // @test(data="myfdurl", tags=video_play_fast)
204     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, release, 300)
205     {
206         int64_t seekPos {5000};
207         int64_t currentMS {0};
208         std::string uri = FilePathToFd(url, fileSize);
209         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
210         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
211         ASSERT_EQ(0, player->Prepare());
212         ASSERT_EQ(0, player->Play());
213         ASSERT_TRUE(player->IsPlaying());
214         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
215         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
216         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
217         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
218         ASSERT_EQ(0, player->Release());
219     }
220 
221     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0400
222     // @test(data="myfdurl", tags=video_play_fast)
223     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, pause, seek, release)
224     {
225         int64_t seekPos {5000};
226         int64_t currentMS {0};
227         std::string uri = FilePathToFd(url, fileSize);
228         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
229         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
230         ASSERT_EQ(0, player->Prepare());
231         ASSERT_EQ(0, player->Play());
232         ASSERT_TRUE(player->IsPlaying());
233         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
234         ASSERT_EQ(0, player->Pause());
235         ASSERT_EQ(0, player->Seek(seekPos));
236         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
237         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
238         ASSERT_EQ(0, player->Release());
239     }
240 
241     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0500
242     // @test(data="myfdurl", tags=video_play_fast)
243     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, stop, seek, release)
244     {
245         int64_t seekPos {5000};
246         int64_t currentMS {0};
247         std::string uri = FilePathToFd(url, fileSize);
248         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
249         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
250         ASSERT_EQ(0, player->Prepare());
251         ASSERT_EQ(0, player->Play());
252         ASSERT_TRUE(player->IsPlaying());
253         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
254         ASSERT_EQ(0, player->Stop());
255         ASSERT_NE(0, player->Seek(seekPos));
256         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
257         ASSERT_EQ(0, player->Release());
258     }
259 
260     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0600
261     // @test(data="myfdurl", tags=video_play_fast)
262     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, reset, seek, release)
263     {
264         int64_t seekPos {5000};
265         int64_t currentMS {0};
266         std::string uri = FilePathToFd(url, fileSize);
267         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
268         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
269         ASSERT_EQ(0, player->Prepare());
270         ASSERT_EQ(0, player->Play());
271         ASSERT_TRUE(player->IsPlaying());
272         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
273         ASSERT_EQ(0, player->Reset());
274         ASSERT_NE(0, player->Seek(seekPos));
275         ASSERT_EQ(0, player->Release());
276     }
277 
278     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0700
279     // @test(data="myfdurl", tags=video_play_fast)
280     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setvolume, seek, release)
281     {
282         float leftVolume {1};
283         float rightVolume {1};
284         int64_t  seekPos {5000};
285         std::string uri = FilePathToFd(url, fileSize);
286         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
287         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
288         ASSERT_EQ(0, player->Prepare());
289         ASSERT_EQ(0, player->Play());
290         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
291         ASSERT_EQ(0, player->Seek(seekPos));
292         ASSERT_EQ(0, player->Release());
293     }
294 
295     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_0900
296     // @test(data="myfdurl", tags=video_play_fast)
297     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, release, 900)
298     {
299         int64_t seekPos {5000};
300         int64_t currentMS {0};
301         std::string uri = FilePathToFd(url, fileSize);
302         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
303         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
304         ASSERT_NE(0, player->Seek(seekPos));
305         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
306         ASSERT_EQ(0, player->Release());
307     }
308 
309     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_1100
310     // @test(data="myfdurl", tags=video_play_fast)
311     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, seek, seek, release)
312     {
313         int64_t seekPos {5000};
314         int64_t currentMS {0};
315         std::string uri = FilePathToFd(url, fileSize);
316         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
317         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
318         ASSERT_EQ(0, player->Prepare());
319         ASSERT_EQ(0, player->Play());
320         ASSERT_TRUE(player->IsPlaying());
321         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
322         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
323         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
324         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
325         seekPos = 5000;
326         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
327         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
328         EXPECT_TRUE(CheckTimeEquality(PREV_FRAME_TIME, currentMS));
329         seekPos = 5000;
330         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_NEXT_SYNC));
331         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
332         EXPECT_TRUE(CheckTimeEquality(NEXT_FRAME_TIME, currentMS));
333         ASSERT_EQ(0, player->Release());
334     }
335 
336     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_1300
337     // @test(data="myfdurl", tags=video_play_fast)
338     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, -1, release)
339     {
340         int64_t seekPos {-1};
341         int64_t currentMS {0};
342         std::string uri = FilePathToFd(url, fileSize);
343         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
344         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
345         ASSERT_EQ(0, player->Prepare());
346         ASSERT_EQ(0, player->Play());
347         ASSERT_TRUE(player->IsPlaying());
348         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
349         ASSERT_EQ(0, player->Seek(seekPos));
350         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
351         ASSERT_EQ(0, player->Release());
352     }
353 
354     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SEEK_CALLBACK_1400
355     // @test(data="myfdurl", tags=video_play_fast)
356     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, durationTime + 1000, release)
357     {
358         int64_t seekPos {0};
359         int64_t currentMS {0};
360         int64_t durationMs {0};
361         int64_t realSeekTime {8300};
362         std::string uri = FilePathToFd(url, fileSize);
363         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
364         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
365         ASSERT_EQ(0, player->Prepare());
366         ASSERT_EQ(0, player->Play());
367         ASSERT_TRUE(player->IsPlaying());
368         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
369         ASSERT_EQ(0, player->GetDuration(durationMs));
370         seekPos = durationMs + 1000;
371         ASSERT_EQ(0, player->Seek(seekPos, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
372         ASSERT_EQ(0, player->GetCurrentTime(currentMS));
373         EXPECT_TRUE(CheckTimeEquality(realSeekTime, currentMS));
374         ASSERT_EQ(0, player->Release());
375     }
376 };
377 
378 #endif //HISTREAMER_TEST_SINGLE_VIDEO_PLAYER_FAST_3_H