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_4_H
16 #define HISTREAMER_TEST_SINGLE_VIDEO_PLAYER_FAST_4_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(dataDrivenSingleVideoPlayerTestFast4)35 FIXTURE(dataDrivenSingleVideoPlayerTestFast4)
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     std::string FilePathToFd(std::string url, int32_t fileSize)
41     {
42         std::string uri = "fd://?offset=0&size=";
43         uri += std::to_string(fileSize);
44         int32_t fd = open(url.c_str(), O_RDONLY|O_BINARY);
45         std::string fdStr = std::to_string(fd);
46         uri.insert(5, fdStr); // 5 ---fd:://
47         return uri;
48     }
49 
50     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0100
51     // @test(data="myfdurl", tags=video_play_fast)
52     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, setvolume, release)
53     {
54         float leftVolume {1};
55         float rightVolume {1};
56         std::string uri = FilePathToFd(url, fileSize);
57         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
58         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
59         ASSERT_EQ(0, player->Release());
60     }
61 
62     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0200
63     // @test(data="myfdurl", tags=video_play_fast)
64     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, setvolume, release)
65     {
66         float leftVolume {1};
67         float rightVolume {1};
68         std::string uri = FilePathToFd(url, fileSize);
69         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
70         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
71         ASSERT_EQ(0, player->Prepare());
72         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
73         ASSERT_EQ(0, player->Release());
74     }
75 
76     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_PLAY_CALLBACK_0300
77     // @test(data="myfdurl", tags=video_play_fast)
78     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setvolume, release)
79     {
80         float leftVolume {1};
81         float rightVolume {1};
82         std::string uri = FilePathToFd(url, fileSize);
83         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
84         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
85         ASSERT_EQ(0, player->Prepare());
86         ASSERT_EQ(0, player->Play());
87         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
88         ASSERT_EQ(0, player->Release());
89     }
90 
91     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0400
92     // @test(data="myfdurl", tags=video_play_fast)
93     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, pause, setvolume, release)
94     {
95         float leftVolume {1};
96         float rightVolume {1};
97         std::string uri = FilePathToFd(url, fileSize);
98         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
99         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
100         ASSERT_EQ(0, player->Prepare());
101         ASSERT_EQ(0, player->Play());
102         ASSERT_EQ(0, player->Pause());
103         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
104         ASSERT_EQ(0, player->Release());
105     }
106 
107     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0500
108     // @test(data="myfdurl", tags=video_play_fast)
109     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, stop, setvolume, release)
110     {
111         float leftVolume {1};
112         float rightVolume {1};
113         std::string uri = FilePathToFd(url, fileSize);
114         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
115         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
116         ASSERT_EQ(0, player->Prepare());
117         ASSERT_EQ(0, player->Play());
118         ASSERT_EQ(0, player->Stop());
119         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
120         ASSERT_EQ(0, player->Release());
121     }
122 
123     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0600
124     // @test(data="myfdurl", tags=video_play_fast)
125     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, reset, setvolume, release)
126     {
127         float leftVolume {1};
128         float rightVolume {1};
129         std::string uri = FilePathToFd(url, fileSize);
130         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
131         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
132         ASSERT_EQ(0, player->Prepare());
133         ASSERT_EQ(0, player->Play());
134         ASSERT_EQ(0, player->Reset());
135         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
136         ASSERT_EQ(0, player->Release());
137     }
138 
139     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0700
140     // @test(data="myfdurl", tags=video_play_fast)
141     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, seek, setvolume, release)
142     {
143         float leftVolume {1};
144         float rightVolume {1};
145         int64_t seekPos {5000};
146         std::string uri = FilePathToFd(url, fileSize);
147         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
148         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
149         ASSERT_EQ(0, player->Prepare());
150         ASSERT_EQ(0, player->Play());
151         ASSERT_TRUE(player->IsPlaying());
152         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
153         ASSERT_EQ(0, player->Seek(seekPos));
154         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
155         ASSERT_EQ(0, player->Release());
156     }
157 
158     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_0900
159     // @test(data="myfdurl", tags=video_play_fast)
160     PTEST((std::string url, int32_t fileSize), Test fdsource setsourse, setvolume, release)
161     {
162         float leftVolume {1};
163         float rightVolume {1};
164         std::string uri = FilePathToFd(url, fileSize);
165         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
166         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
167         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
168         ASSERT_EQ(0, player->Release());
169     }
170 
171     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_1100
172     // @test(data="myfdurl", tags=video_play_fast)
173     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, setvolume, setvolume, setvolume, release)
174     {
175         float leftVolume {1};
176         float rightVolume {1};
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         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
183         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
184         ASSERT_EQ(0, player->SetVolume(leftVolume, rightVolume));
185         ASSERT_EQ(0, player->Release());
186     }
187 
188     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_1300
189     // @test(data="myfdurl", tags=video_play_fast)
190     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setVolume, -1, release)
191     {
192         float leftVolume {-1};
193         float rightVolume {-1};
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(3000));
200         ASSERT_NE(0, player->SetVolume(leftVolume, rightVolume));
201         ASSERT_EQ(0, player->Release());
202     }
203 
204     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_SETVOLUME_CALLBACK_1400
205     // @test(data="myfdurl", tags=video_play_fast)
206     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, play, setVolume, 2, release)
207     {
208         float leftVolume {2};
209         float rightVolume {2};
210         std::string uri = FilePathToFd(url, fileSize);
211         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
212         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
213         ASSERT_EQ(0, player->Prepare());
214         ASSERT_EQ(0, player->Play());
215         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
216         ASSERT_NE(0, player->SetVolume(leftVolume, rightVolume));
217         ASSERT_EQ(0, player->Release());
218     }
219 
220     //  SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_FUNCTION_CALLBACK_LOOP
221     // @test(data="myfdurl", tags=video_play_fast)
222     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, setsingleloop true, play, seek, durationtime
223             3 times, setsingleloop flase, release)
224     {
225         int64_t durationMs {0};
226         std::string uri = FilePathToFd(url, fileSize);
227         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
228         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
229         ASSERT_EQ(0, player->Prepare());
230         ASSERT_EQ(0, player->SetSingleLoop(true));
231         ASSERT_EQ(0, player->Play());
232         ASSERT_EQ(0, player->GetDuration(durationMs));
233         ASSERT_EQ(0, player->Seek(durationMs, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
234         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
235         ASSERT_EQ(0, player->GetDuration(durationMs));
236         ASSERT_EQ(0, player->Seek(durationMs, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
237         std::this_thread::sleep_for(std::chrono::milliseconds(5000));
238         ASSERT_EQ(0, player->GetDuration(durationMs));
239         ASSERT_EQ(0, player->Seek(durationMs, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
240         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
241         ASSERT_EQ(0, player->GetDuration(durationMs));
242         ASSERT_EQ(0, player->Seek(durationMs, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
243         std::this_thread::sleep_for(std::chrono::milliseconds(8000));
244         ASSERT_EQ(0, player->SetSingleLoop(false));
245         ASSERT_EQ(0, player->Release());
246     }
247 
248     // SUB_MULTIMEDIA_MEDIA_VIDEO_PLAYER_FUNCTION_CALLBACK_BASE
249     // @test(data="myfdurl", tags=video_play_fast)
250     PTEST((std::string url, int32_t fileSize), Test fdsource prepare, setsingleloop true, play, seek, set fd, seek
251             2 times, setsingleloop false, release)
252     {
253         int64_t durationMs {0};
254         std::unique_ptr<TestPlayer> player = TestPlayer::Create();
255         ASSERT_EQ(0, player->SetSource(TestSource(url)));
256         ASSERT_EQ(0, player->Prepare());
257         ASSERT_EQ(0, player->Play());
258         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
259         ASSERT_EQ(0, player->Pause());
260         ASSERT_EQ(0, player->Play());
261         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
262         ASSERT_EQ(0, player->Stop());
263         ASSERT_EQ(0, player->Reset());
264         std::string uri = FilePathToFd(url, fileSize);
265         ASSERT_EQ(0, player->SetSource(TestSource(uri)));
266         ASSERT_EQ(0, player->Prepare());
267         ASSERT_EQ(0, player->SetSingleLoop(true));
268         ASSERT_EQ(0, player->Play());
269         ASSERT_TRUE(player->IsPlaying());
270         std::this_thread::sleep_for(std::chrono::milliseconds(3000));
271         ASSERT_EQ(0, player->GetDuration(durationMs));
272         ASSERT_EQ(0, player->Seek(durationMs/2));
273         ASSERT_EQ(0, player->Seek(0));
274         std::this_thread::sleep_for(std::chrono::milliseconds(1000));
275         ASSERT_EQ(0, player->GetDuration(durationMs));
276         ASSERT_EQ(0, player->Seek(durationMs, OHOS::Media::PlayerSeekMode::SEEK_PREVIOUS_SYNC));
277         std::this_thread::sleep_for(std::chrono::milliseconds(8000));
278         ASSERT_EQ(0, player->SetSingleLoop(false));
279         ASSERT_EQ(0, player->Release());
280     }
281 };
282 
283 #endif //HISTREAMER_TEST_SINGLE_VIDEO_PLAYER_FAST_4_H