1 /*
2 * Copyright (c) 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
16 #include "http_media_downloader_unit_test.h"
17 #include "http_server_demo.h"
18 #include "source_callback.h"
19
20 #define LOCAL true
21 namespace OHOS::Media::Plugins::HttpPlugin {
22 using namespace std;
23 using namespace testing::ext;
24
25 const std::string MP4_SEGMENT_BASE = "http://127.0.0.1:46666/dewu.mp4";
26 const std::string MP4_NULL_SEGMENT_BASE = "http://127.0.0.1:46666/dewuNull.mp4";
27 const std::string FLV_SEGMENT_BASE = "http://127.0.0.1:46666/h264.flv";
28
29 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server;
30 std::shared_ptr<HttpMediaDownloader> MP4httpMediaDownloader;
31 std::shared_ptr<HttpMediaDownloader> FLVhttpMediaDownloader;
32 bool g_flvResult = false;
33 bool g_mp4Result = false;
34
SetUpTestCase(void)35 void HttpMediaDownloaderUnitTest::SetUpTestCase(void)
36 {
37 g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
38 g_server->StartServer();
39 MP4httpMediaDownloader = std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, MAX_CACHE_BUFFER_SIZE, nullptr);
40 FLVhttpMediaDownloader = std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, MAX_CACHE_BUFFER_SIZE, nullptr);
41 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
42 std::shared_ptr<DownloadRequest>& request) {};
43 MP4httpMediaDownloader->SetStatusCallback(statusCallback);
44 FLVhttpMediaDownloader->SetStatusCallback(statusCallback);
45 std::map<std::string, std::string> httpHeader;
46 g_flvResult = FLVhttpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
47 g_mp4Result = MP4httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
48 }
49
TearDownTestCase(void)50 void HttpMediaDownloaderUnitTest::TearDownTestCase(void)
51 {
52 MP4httpMediaDownloader->Close(true);
53 FLVhttpMediaDownloader->Close(true);
54 MP4httpMediaDownloader = nullptr;
55 FLVhttpMediaDownloader = nullptr;
56 g_server->StopServer();
57 g_server = nullptr;
58 }
59
SetUp()60 void HttpMediaDownloaderUnitTest::SetUp()
61 {
62 }
63
TearDown()64 void HttpMediaDownloaderUnitTest::TearDown()
65 {
66 }
67
68 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_INIT, TestSize.Level1)
69 {
70 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader4 =
71 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
72 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader5 =
73 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 5, nullptr);
74 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader_4 =
75 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
76 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader_5 =
77 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
78 EXPECT_TRUE(httpMediaDownloader4);
79 }
80
81 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_RINGBUFFER, TestSize.Level1)
82 {
83 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
84 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
85 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440202(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 86 std::shared_ptr<DownloadRequest>& request) {};
87 httpMediaDownloader->SetStatusCallback(statusCallback);
88 std::map<std::string, std::string> httpHeader;
89 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
90 httpMediaDownloader->GetSeekable();
91 unsigned char buff[5 * 1024 * 1024];
92 ReadDataInfo readDataInfo;
93 readDataInfo.streamId_ = 0;
94 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
95 readDataInfo.isEos_ = true;
96 httpMediaDownloader->Read(buff, readDataInfo);
97 httpMediaDownloader->SetDownloadErrorState();
98 OSAL::SleepFor(1 * 1000);
99 httpMediaDownloader->Close(true);
100 httpMediaDownloader = nullptr;
101 EXPECT_GE(readDataInfo.realReadLength_, 0);
102 }
103
104 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport, TestSize.Level1)
105 {
106 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
107 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
108 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440302(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 109 std::shared_ptr<DownloadRequest>& request) {};
110 httpMediaDownloader->SetStatusCallback(statusCallback);
111 std::map<std::string, std::string> httpHeader;
112 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
113 OSAL::SleepFor(6 * 1000);
114 httpMediaDownloader->DownloadReport();
115 httpMediaDownloader->GetSeekable();
116 unsigned char buff[5 * 1024 * 1024];
117 ReadDataInfo readDataInfo;
118 readDataInfo.streamId_ = 0;
119 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
120 readDataInfo.isEos_ = true;
121 httpMediaDownloader->Read(buff, readDataInfo);
122 httpMediaDownloader->SetDownloadErrorState();
123 OSAL::SleepFor(1 * 1000);
124 httpMediaDownloader->Close(true);
125 httpMediaDownloader = nullptr;
126 EXPECT_GE(readDataInfo.realReadLength_, 0);
127 }
128
129 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport_MP4, TestSize.Level1)
130 {
131 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
132 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
133 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440402(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 134 std::shared_ptr<DownloadRequest>& request) {};
135 httpMediaDownloader->SetStatusCallback(statusCallback);
136 std::map<std::string, std::string> httpHeader;
137 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
138 OSAL::SleepFor(6 * 1000);
139 httpMediaDownloader->DownloadReport();
140 httpMediaDownloader->GetSeekable();
141 unsigned char buff[5 * 1024 * 1024];
142 ReadDataInfo readDataInfo;
143 readDataInfo.streamId_ = 0;
144 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
145 readDataInfo.isEos_ = true;
146 httpMediaDownloader->Read(buff, readDataInfo);
147 httpMediaDownloader->SetDownloadErrorState();
148 OSAL::SleepFor(1 * 1000);
149 httpMediaDownloader->Close(true);
150 httpMediaDownloader = nullptr;
151 EXPECT_GE(readDataInfo.realReadLength_, 0);
152 }
153
154 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport_MP4_default, TestSize.Level1)
155 {
156 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
157 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
158 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440502(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 159 std::shared_ptr<DownloadRequest>& request) {};
160 httpMediaDownloader->SetStatusCallback(statusCallback);
161 std::map<std::string, std::string> httpHeader;
162 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
163 httpMediaDownloader->GetSeekable();
164 ReadDataInfo readDataInfo;
165 for (int i = 0; i < 800; i++) {
166 OSAL::SleepFor(10);
167 httpMediaDownloader->DownloadReport();
168 unsigned char buff[10 * 1024];
169 readDataInfo.streamId_ = 0;
170 readDataInfo.wantReadLength_ = 10 * 1024;
171 readDataInfo.isEos_ = false;
172 httpMediaDownloader->Read(buff, readDataInfo);
173 if (i == 3) {
174 httpMediaDownloader->SetDemuxerState(0);
175 }
176 }
177 httpMediaDownloader->Close(true);
178 httpMediaDownloader = nullptr;
179 EXPECT_GE(readDataInfo.realReadLength_, 0);
180 }
181
182 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_mp4_read_all, TestSize.Level1)
183 {
184 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
185 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
186 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440602(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 187 std::shared_ptr<DownloadRequest>& request) {};
188 httpMediaDownloader->SetStatusCallback(statusCallback);
189 std::map<std::string, std::string> httpHeader;
190 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
191 httpMediaDownloader->GetSeekable();
192 ReadDataInfo readDataInfo;
193 for (int i = 0; i < 800; i++) {
194 OSAL::SleepFor(10);
195 unsigned char buff[100 * 1024];
196 readDataInfo.streamId_ = 0;
197 readDataInfo.wantReadLength_ = 100 * 1024;
198 readDataInfo.isEos_ = false;
199 httpMediaDownloader->Read(buff, readDataInfo);
200 if (i == 3) {
201 httpMediaDownloader->SetDemuxerState(0);
202 }
203 }
204 httpMediaDownloader->Close(true);
205 httpMediaDownloader = nullptr;
206 EXPECT_GE(readDataInfo.realReadLength_, 0);
207 }
208
209
210 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL, TestSize.Level1)
211 {
212 MP4httpMediaDownloader->GetSeekable();
213 unsigned char buff[10];
214 ReadDataInfo readDataInfo;
215 readDataInfo.streamId_ = 0;
216 readDataInfo.wantReadLength_ = 10;
217 readDataInfo.isEos_ = true;
218 MP4httpMediaDownloader->Read(buff, readDataInfo);
219 OSAL::SleepFor(1 * 1000);
220 EXPECT_GE(readDataInfo.realReadLength_, 0);
221 }
222
223 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_SEEK, TestSize.Level1)
224 {
225 MP4httpMediaDownloader->GetSeekable();
226 bool isSeekHit = false;
227 bool result = MP4httpMediaDownloader->SeekToPos(100, isSeekHit);
228 EXPECT_TRUE(result);
229 result = MP4httpMediaDownloader->SeekToPos(10000000, isSeekHit);
230 EXPECT_TRUE(result);
231 }
232
233 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV, TestSize.Level1)
234 {
235 FLVhttpMediaDownloader->GetSeekable();
236 unsigned char buff[10];
237 ReadDataInfo readDataInfo;
238 readDataInfo.streamId_ = 0;
239 readDataInfo.wantReadLength_ = 10;
240 readDataInfo.isEos_ = true;
241 FLVhttpMediaDownloader->Read(buff, readDataInfo);
242 OSAL::SleepFor(1 * 1000);
243 EXPECT_GE(readDataInfo.realReadLength_, 0);
244 }
245
246 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV_DUA, TestSize.Level1)
247 {
248 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
249 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
250 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440702(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 251 std::shared_ptr<DownloadRequest>& request) {};
252 httpMediaDownloader->SetStatusCallback(statusCallback);
253 std::map<std::string, std::string> httpHeader;
254 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
255 httpMediaDownloader->GetSeekable();
256 unsigned char buff[10];
257 ReadDataInfo readDataInfo;
258 readDataInfo.streamId_ = 0;
259 readDataInfo.wantReadLength_ = 10;
260 readDataInfo.isEos_ = true;
261 httpMediaDownloader->Read(buff, readDataInfo);
262 OSAL::SleepFor(1 * 1000);
263 httpMediaDownloader->Close(true);
264 httpMediaDownloader = nullptr;
265 EXPECT_GE(readDataInfo.realReadLength_, 0);
266 }
267
268 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV_MAX_BUFFER, TestSize.Level1)
269 {
270 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
271 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 30, nullptr);
272 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440802(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 273 std::shared_ptr<DownloadRequest>& request) {};
274 httpMediaDownloader->SetStatusCallback(statusCallback);
275 std::map<std::string, std::string> httpHeader;
276 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
277 httpMediaDownloader->GetSeekable();
278 unsigned char buff[10];
279 ReadDataInfo readDataInfo;
280 readDataInfo.streamId_ = 0;
281 readDataInfo.wantReadLength_ = 10;
282 readDataInfo.isEos_ = true;
283 httpMediaDownloader->Read(buff, readDataInfo);
284 OSAL::SleepFor(1 * 1000);
285 httpMediaDownloader->Close(true);
286 httpMediaDownloader = nullptr;
287 EXPECT_GE(readDataInfo.realReadLength_, 0);
288 }
289
290 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_MP4_DUA, TestSize.Level1)
291 {
292 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
293 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5 * 1024, nullptr);
294 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440902(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 295 std::shared_ptr<DownloadRequest>& request) {};
296 httpMediaDownloader->SetStatusCallback(statusCallback);
297 std::map<std::string, std::string> httpHeader;
298 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
299 httpMediaDownloader->GetSeekable();
300 unsigned char buff[10];
301 ReadDataInfo readDataInfo;
302 readDataInfo.streamId_ = 0;
303 readDataInfo.wantReadLength_ = 10;
304 readDataInfo.isEos_ = true;
305 httpMediaDownloader->Read(buff, readDataInfo);
306 OSAL::SleepFor(1 * 1000);
307 httpMediaDownloader->Close(true);
308 httpMediaDownloader = nullptr;
309 EXPECT_GE(readDataInfo.realReadLength_, 0);
310 }
311
312 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_MP4_DOWNLOADINFO, TestSize.Level1)
313 {
314 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
315 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5 * 1024, nullptr);
316 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440a02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 317 std::shared_ptr<DownloadRequest>& request) {};
318 httpMediaDownloader->SetStatusCallback(statusCallback);
319 std::map<std::string, std::string> httpHeader;
320 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
321 httpMediaDownloader->GetSeekable();
322 EXPECT_TRUE(httpMediaDownloader);
323 DownloadInfo downloadInfo;
324 httpMediaDownloader->GetDownloadInfo(downloadInfo);
325 httpMediaDownloader->recordSpeedCount_ = 10;
326 httpMediaDownloader->GetDownloadInfo(downloadInfo);
327 httpMediaDownloader->OnClientErrorEvent();
328 httpMediaDownloader->SetInterruptState(true);
329 httpMediaDownloader->SetInterruptState(false);
330 httpMediaDownloader->Close(true);
331 httpMediaDownloader = nullptr;
332 }
333
334 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_SEEK_FLV, TestSize.Level1)
335 {
336 FLVhttpMediaDownloader->GetSeekable();
337 bool isSeekHit = false;
338 bool result = FLVhttpMediaDownloader->SeekToPos(100, isSeekHit);
339 FLVhttpMediaDownloader->SetReadBlockingFlag(true);
340 EXPECT_TRUE(result);
341 }
342
343 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4, TestSize.Level1)
344 {
345 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
346 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
347 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440b02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 348 std::shared_ptr<DownloadRequest>& request) {};
349 httpMediaDownloader->SetStatusCallback(statusCallback);
350 std::map<std::string, std::string> httpHeader;
351 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
352 httpMediaDownloader->GetSeekable();
353 unsigned char buff[10];
354 ReadDataInfo readDataInfo;
355 readDataInfo.streamId_ = 0;
356 readDataInfo.wantReadLength_ = 10;
357 readDataInfo.isEos_ = true;
358 httpMediaDownloader->Read(buff, readDataInfo);
359 OSAL::SleepFor(1 * 1000);
360 httpMediaDownloader->UpdateWaterLineAbove();
361 httpMediaDownloader->SetDownloadErrorState();
362 httpMediaDownloader->SetCurrentBitRate(-1, 0);
363 httpMediaDownloader->SetCurrentBitRate(1000, 0);
364 httpMediaDownloader->UpdateWaterLineAbove();
365 httpMediaDownloader->ChangeDownloadPos(false);
366 httpMediaDownloader->Close(true);
367 httpMediaDownloader = nullptr;
368 EXPECT_GE(readDataInfo.realReadLength_, 0);
369 }
370
371 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4_ERROR, TestSize.Level1)
372 {
373 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
374 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
375 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440c02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 376 std::shared_ptr<DownloadRequest>& request) {};
377 httpMediaDownloader->SetStatusCallback(statusCallback);
378 std::map<std::string, std::string> httpHeader;
379 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
380 httpMediaDownloader->GetSeekable();
381 unsigned char buff[10];
382 ReadDataInfo readDataInfo;
383 readDataInfo.streamId_ = 0;
384 readDataInfo.wantReadLength_ = 10;
385 readDataInfo.isEos_ = true;
386 httpMediaDownloader->downloadErrorState_ = true;
387 httpMediaDownloader->Read(buff, readDataInfo);
388 OSAL::SleepFor(1 * 1000);
389 httpMediaDownloader->CheckIsEosCacheBuffer(buff, readDataInfo);
390 httpMediaDownloader->Close(true);
391 httpMediaDownloader = nullptr;
392 EXPECT_GE(readDataInfo.realReadLength_, 0);
393 }
394
395 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_FLV_ERROR, TestSize.Level1)
396 {
397 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
398 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 30, nullptr);
399 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440d02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 400 std::shared_ptr<DownloadRequest>& request) {};
401 httpMediaDownloader->SetStatusCallback(statusCallback);
402 Plugins::Callback* sourceCallback = new SourceCallback();
403 httpMediaDownloader->SetCallback(sourceCallback);
404 std::map<std::string, std::string> httpHeader;
405 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
406 httpMediaDownloader->GetSeekable();
407 unsigned char buff[10];
408 ReadDataInfo readDataInfo;
409 readDataInfo.streamId_ = 0;
410 readDataInfo.wantReadLength_ = 10;
411 readDataInfo.isEos_ = true;
412 httpMediaDownloader->Read(buff, readDataInfo);
413 OSAL::SleepFor(1 * 1000);
414 httpMediaDownloader->CheckIsEosRingBuffer(buff, readDataInfo);
415 httpMediaDownloader->OnClientErrorEvent();
416 httpMediaDownloader->Close(true);
417 httpMediaDownloader = nullptr;
418 EXPECT_GE(readDataInfo.realReadLength_, 0);
419 }
420
421 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4_NULL, TestSize.Level1)
422 {
423 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
424 std::make_shared<HttpMediaDownloader>(MP4_NULL_SEGMENT_BASE, MAX_CACHE_BUFFER_SIZE, nullptr);
425 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440e02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 426 std::shared_ptr<DownloadRequest>& request) {};
427 httpMediaDownloader->SetStatusCallback(statusCallback);
428 Plugins::Callback* sourceCallback = new SourceCallback();
429 httpMediaDownloader->SetCallback(sourceCallback);
430 std::map<std::string, std::string> httpHeader;
431 httpMediaDownloader->Open(MP4_NULL_SEGMENT_BASE, httpHeader);
432 httpMediaDownloader->GetSeekable();
433 unsigned char buff[10];
434 ReadDataInfo readDataInfo;
435 readDataInfo.streamId_ = 0;
436 readDataInfo.wantReadLength_ = 10;
437 readDataInfo.isEos_ = true;
438 httpMediaDownloader->Read(buff, readDataInfo);
439 OSAL::SleepFor(1 * 1000);
440 httpMediaDownloader->UpdateWaterLineAbove();
441 httpMediaDownloader->SetDownloadErrorState();
442 httpMediaDownloader->SetCurrentBitRate(-1, 0);
443 httpMediaDownloader->SetCurrentBitRate(1000, 0);
444 httpMediaDownloader->UpdateWaterLineAbove();
445 httpMediaDownloader->HandleCachedDuration();
446 httpMediaDownloader->Close(true);
447 httpMediaDownloader = nullptr;
448 EXPECT_GE(readDataInfo.realReadLength_, 0);
449 }
450
451 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_FLC_SEEK, TestSize.Level1)
452 {
453 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
454 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
455 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf440f02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 456 std::shared_ptr<DownloadRequest>& request) {};
457 httpMediaDownloader->SetStatusCallback(statusCallback);
458 std::map<std::string, std::string> httpHeader;
459 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
460 httpMediaDownloader->GetSeekable();
461 unsigned char buff[5 * 1024 * 1024];
462 ReadDataInfo readDataInfo;
463 readDataInfo.streamId_ = 0;
464 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
465 readDataInfo.isEos_ = true;
466 httpMediaDownloader->downloadErrorState_ = true;
467 httpMediaDownloader->Read(buff, readDataInfo);
468 OSAL::SleepFor(1000);
469 bool isSeekHit = false;
470 httpMediaDownloader->SeekToPos(100 * 1024 * 1024, isSeekHit);
471 httpMediaDownloader->Close(true);
472 httpMediaDownloader = nullptr;
473 EXPECT_GE(readDataInfo.realReadLength_, 0);
474 }
475
476
477 HWTEST_F(HttpMediaDownloaderUnitTest, GetContentLength, TestSize.Level1)
478 {
479 EXPECT_GE(MP4httpMediaDownloader->GetContentLength(), 0);
480 EXPECT_GE(FLVhttpMediaDownloader->GetContentLength(), 0);
481 }
482
483 HWTEST_F(HttpMediaDownloaderUnitTest, GetStartedStatus, TestSize.Level1)
484 {
485 MP4httpMediaDownloader->startedPlayStatus_ = false;
486 FLVhttpMediaDownloader->startedPlayStatus_ = false;
487 EXPECT_FALSE(MP4httpMediaDownloader->GetStartedStatus());
488 EXPECT_FALSE(FLVhttpMediaDownloader->GetStartedStatus());
489 }
490
491 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN, TestSize.Level1)
492 {
493 EXPECT_TRUE(g_flvResult);
494 EXPECT_TRUE(g_mp4Result);
495 }
496
497 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_PAUSE_RESUME, TestSize.Level1)
498 {
499 MP4httpMediaDownloader->Pause();
500 FLVhttpMediaDownloader->Pause();
501 EXPECT_TRUE(MP4httpMediaDownloader->isInterrupt_);
502 EXPECT_TRUE(FLVhttpMediaDownloader->isInterrupt_);
503 MP4httpMediaDownloader->Resume();
504 FLVhttpMediaDownloader->Resume();
505 EXPECT_FALSE(MP4httpMediaDownloader->isInterrupt_);
506 EXPECT_FALSE(FLVhttpMediaDownloader->isInterrupt_);
507 }
508
509 HWTEST_F(HttpMediaDownloaderUnitTest, HandleBuffering1, TestSize.Level1)
510 {
511 MP4httpMediaDownloader->isBuffering_ = false;
512 EXPECT_FALSE(MP4httpMediaDownloader->HandleBuffering());
513 }
514
515 HWTEST_F(HttpMediaDownloaderUnitTest, GET_PLAYBACK_INFO_001, TestSize.Level1)
516 {
517 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
518 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
519 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441002(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 520 std::shared_ptr<DownloadRequest>& request) {};
521 httpMediaDownloader->SetStatusCallback(statusCallback);
522 std::map<std::string, std::string> httpHeader;
523 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
524 PlaybackInfo playbackInfo;
525 httpMediaDownloader->GetPlaybackInfo(playbackInfo);
526 EXPECT_EQ(playbackInfo.serverIpAddress, "");
527 EXPECT_EQ(playbackInfo.averageDownloadRate, 0);
528 EXPECT_EQ(playbackInfo.isDownloading, true);
529 EXPECT_EQ(playbackInfo.downloadRate, 0);
530 EXPECT_EQ(playbackInfo.bufferDuration, 0);
531 }
532
533 HWTEST_F(HttpMediaDownloaderUnitTest, GET_PLAYBACK_INFO_002, TestSize.Level1)
534 {
535 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
536 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
537 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441102(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 538 std::shared_ptr<DownloadRequest>& request) {};
539 httpMediaDownloader->SetStatusCallback(statusCallback);
540 std::map<std::string, std::string> httpHeader;
541 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
542 httpMediaDownloader->totalDownloadDuringTime_ = 1000;
543 httpMediaDownloader->totalBits_ = 1000;
544 httpMediaDownloader->isDownloadFinish_ = true;
545 httpMediaDownloader->recordData_->downloadRate = 1000;
546 httpMediaDownloader->recordData_->bufferDuring = 1;
547 PlaybackInfo playbackInfo;
548 httpMediaDownloader->GetPlaybackInfo(playbackInfo);
549 EXPECT_EQ(playbackInfo.serverIpAddress, "");
550 EXPECT_EQ(playbackInfo.averageDownloadRate, 1000);
551 EXPECT_EQ(playbackInfo.isDownloading, false);
552 EXPECT_EQ(playbackInfo.downloadRate, 1000);
553 EXPECT_EQ(playbackInfo.bufferDuration, 0);
554 }
555
556 HWTEST_F(HttpMediaDownloaderUnitTest, HANDLE_WATER_LINE_001, TestSize.Level1)
557 {
558 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
559 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
560 Plugins::Callback* sourceCallback = new SourceCallback();
561 httpMediaDownloader->callback_ = sourceCallback;
562 httpMediaDownloader->canWrite_ = false;
563 httpMediaDownloader->waterLineAbove_ = 0;
564 httpMediaDownloader->readOffset_ = 0;
565 httpMediaDownloader->initCacheSize_ = 5000;
566 httpMediaDownloader->HandleWaterline();
567 EXPECT_EQ(httpMediaDownloader->initCacheSize_, -1);
568 EXPECT_EQ(httpMediaDownloader->isBuffering_, false);
569 }
570
571 HWTEST_F(HttpMediaDownloaderUnitTest, CACHE_BUFFER_FULL_LOOP_001, TestSize.Level1)
572 {
573 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
574 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
575 Plugins::Callback* sourceCallback = new SourceCallback();
576 httpMediaDownloader->callback_ = sourceCallback;
577 httpMediaDownloader->isHitSeeking_ = true;
578 httpMediaDownloader->initCacheSize_ = 1;
579 EXPECT_EQ(httpMediaDownloader->CacheBufferFullLoop(), true);
580 httpMediaDownloader->isHitSeeking_ = false;
581 EXPECT_EQ(httpMediaDownloader->CacheBufferFullLoop(), false);
582 }
583
584 HWTEST_F(HttpMediaDownloaderUnitTest, SET_INITIAL_BUFFER_SIZE_001, TestSize.Level1)
585 {
586 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
587 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
588 std::map<std::string, std::string> httpHeader;
589 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441202(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 590 std::shared_ptr<DownloadRequest>& request) {};
591 httpMediaDownloader->SetStatusCallback(statusCallback);
592 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
593 Plugins::Callback* sourceCallback = new SourceCallback();
594 httpMediaDownloader->callback_ = sourceCallback;
595 httpMediaDownloader->isBuffering_ = false;
596 EXPECT_EQ(httpMediaDownloader->SetInitialBufferSize(0, 0), false);
597 EXPECT_EQ(httpMediaDownloader->SetInitialBufferSize(0, 20000000), true);
598 }
599
600 HWTEST_F(HttpMediaDownloaderUnitTest, SET_PLAY_STRATEGY_001, TestSize.Level1)
601 {
602 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
603 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
604 std::map<std::string, std::string> httpHeader;
605 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441302(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 606 std::shared_ptr<DownloadRequest>& request) {};
607 httpMediaDownloader->SetStatusCallback(statusCallback);
608 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
609 Plugins::Callback* sourceCallback = new SourceCallback();
610 httpMediaDownloader->callback_ = sourceCallback;
611 httpMediaDownloader->SetPlayStrategy(nullptr);
612 EXPECT_EQ(httpMediaDownloader->waterlineForPlaying_, 0);
613 std::shared_ptr<PlayStrategy> playStrategy = std::make_shared<PlayStrategy>();
614 playStrategy->bufferDurationForPlaying = 5;
615 httpMediaDownloader->SetPlayStrategy(playStrategy);
616 EXPECT_NE(httpMediaDownloader->waterlineForPlaying_, 0);
617 }
618
619 HWTEST_F(HttpMediaDownloaderUnitTest, IS_NEED_BUFFER_FOR_PLAYING_001, TestSize.Level1)
620 {
621 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
622 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
623 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441402(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 624 std::shared_ptr<DownloadRequest>& request) {};
625 httpMediaDownloader->SetStatusCallback(statusCallback);
626 Plugins::Callback* sourceCallback = new SourceCallback();
627 httpMediaDownloader->callback_ = sourceCallback;
628 EXPECT_EQ(httpMediaDownloader->IsNeedBufferForPlaying(), false);
629 httpMediaDownloader->bufferDurationForPlaying_ = 5;
630 httpMediaDownloader->isDemuxerInitSuccess_ = true;
631 httpMediaDownloader->isBuffering_ = true;
632 httpMediaDownloader->bufferingTime_ = static_cast<size_t>(httpMediaDownloader->
633 steadyClock_.ElapsedMilliseconds()) - 100 * 1000;
634 EXPECT_EQ(httpMediaDownloader->IsNeedBufferForPlaying(), false);
635 httpMediaDownloader->bufferingTime_ = 0;
636 httpMediaDownloader->waterlineForPlaying_ = 0;
637 EXPECT_EQ(httpMediaDownloader->IsNeedBufferForPlaying(), false);
638 }
639
640 HWTEST_F(HttpMediaDownloaderUnitTest, NOTIFY_INIT_SUCCESS_001, TestSize.Level1)
641 {
642 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
643 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
644 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6712cf441502(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 645 std::shared_ptr<DownloadRequest>& request) {};
646 httpMediaDownloader->SetStatusCallback(statusCallback);
647 Plugins::Callback* sourceCallback = new SourceCallback();
648 httpMediaDownloader->callback_ = sourceCallback;
649 httpMediaDownloader->isBuffering_ = false;
650 httpMediaDownloader->NotifyInitSuccess();
651 EXPECT_EQ(httpMediaDownloader->isBuffering_, false);
652 httpMediaDownloader->bufferDurationForPlaying_ = 5;
653 httpMediaDownloader->currentBitRate_ = 1000000;
654 httpMediaDownloader->NotifyInitSuccess();
655 EXPECT_EQ(httpMediaDownloader->isBuffering_, true);
656 }
657
658 }