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);
40 FLVhttpMediaDownloader = std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE);
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);
72 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader5 =
73 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 5);
74 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader_4 =
75 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4);
76 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader_5 =
77 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5);
78 }
79
80 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_RINGBUFFER, TestSize.Level1)
81 {
82 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
83 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4);
84 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0202(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 85 std::shared_ptr<DownloadRequest>& request) {};
86 httpMediaDownloader->SetStatusCallback(statusCallback);
87 std::map<std::string, std::string> httpHeader;
88 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
89 httpMediaDownloader->GetSeekable();
90 unsigned char buff[5 * 1024 * 1024];
91 ReadDataInfo readDataInfo;
92 readDataInfo.streamId_ = 0;
93 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
94 readDataInfo.isEos_ = true;
95 httpMediaDownloader->Read(buff, readDataInfo);
96 httpMediaDownloader->SetDownloadErrorState();
97 OSAL::SleepFor(1 * 1000);
98 httpMediaDownloader->Close(true);
99 httpMediaDownloader = nullptr;
100 }
101
102 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport, TestSize.Level1)
103 {
104 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
105 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4);
106 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0302(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 107 std::shared_ptr<DownloadRequest>& request) {};
108 httpMediaDownloader->SetStatusCallback(statusCallback);
109 std::map<std::string, std::string> httpHeader;
110 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
111 OSAL::SleepFor(6 * 1000);
112 httpMediaDownloader->DownloadReport();
113 httpMediaDownloader->GetSeekable();
114 unsigned char buff[5 * 1024 * 1024];
115 ReadDataInfo readDataInfo;
116 readDataInfo.streamId_ = 0;
117 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
118 readDataInfo.isEos_ = true;
119 httpMediaDownloader->Read(buff, readDataInfo);
120 httpMediaDownloader->SetDownloadErrorState();
121 OSAL::SleepFor(1 * 1000);
122 httpMediaDownloader->Close(true);
123 httpMediaDownloader = nullptr;
124 }
125
126 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport_MP4, TestSize.Level1)
127 {
128 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
129 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4);
130 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0402(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 131 std::shared_ptr<DownloadRequest>& request) {};
132 httpMediaDownloader->SetStatusCallback(statusCallback);
133 std::map<std::string, std::string> httpHeader;
134 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
135 OSAL::SleepFor(6 * 1000);
136 httpMediaDownloader->DownloadReport();
137 httpMediaDownloader->GetSeekable();
138 unsigned char buff[5 * 1024 * 1024];
139 ReadDataInfo readDataInfo;
140 readDataInfo.streamId_ = 0;
141 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
142 readDataInfo.isEos_ = true;
143 httpMediaDownloader->Read(buff, readDataInfo);
144 httpMediaDownloader->SetDownloadErrorState();
145 OSAL::SleepFor(1 * 1000);
146 httpMediaDownloader->Close(true);
147 httpMediaDownloader = nullptr;
148 }
149
150 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_DownloadReport_MP4_default, TestSize.Level1)
151 {
152 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
153 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4);
154 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0502(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 155 std::shared_ptr<DownloadRequest>& request) {};
156 httpMediaDownloader->SetStatusCallback(statusCallback);
157 std::map<std::string, std::string> httpHeader;
158 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
159 httpMediaDownloader->GetSeekable();
160 for (int i = 0; i < 800; i++) {
161 OSAL::SleepFor(10);
162 httpMediaDownloader->DownloadReport();
163 unsigned char buff[10 * 1024];
164 ReadDataInfo readDataInfo;
165 readDataInfo.streamId_ = 0;
166 readDataInfo.wantReadLength_ = 10 * 1024;
167 readDataInfo.isEos_ = false;
168 httpMediaDownloader->Read(buff, readDataInfo);
169 if (i == 3) {
170 httpMediaDownloader->SetDemuxerState(0);
171 }
172 }
173 httpMediaDownloader->Close(true);
174 httpMediaDownloader = nullptr;
175 }
176
177 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_mp4_read_all, TestSize.Level1)
178 {
179 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
180 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4);
181 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0602(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 182 std::shared_ptr<DownloadRequest>& request) {};
183 httpMediaDownloader->SetStatusCallback(statusCallback);
184 std::map<std::string, std::string> httpHeader;
185 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
186 httpMediaDownloader->GetSeekable();
187 for (int i = 0; i < 800; i++) {
188 OSAL::SleepFor(10);
189 unsigned char buff[100 * 1024];
190 ReadDataInfo readDataInfo;
191 readDataInfo.streamId_ = 0;
192 readDataInfo.wantReadLength_ = 100 * 1024;
193 readDataInfo.isEos_ = false;
194 httpMediaDownloader->Read(buff, readDataInfo);
195 if (i == 3) {
196 httpMediaDownloader->SetDemuxerState(0);
197 }
198 }
199 httpMediaDownloader->Close(true);
200 httpMediaDownloader = nullptr;
201 }
202
203
204 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL, TestSize.Level1)
205 {
206 MP4httpMediaDownloader->GetSeekable();
207 unsigned char buff[10];
208 ReadDataInfo readDataInfo;
209 readDataInfo.streamId_ = 0;
210 readDataInfo.wantReadLength_ = 10;
211 readDataInfo.isEos_ = true;
212 MP4httpMediaDownloader->Read(buff, readDataInfo);
213 OSAL::SleepFor(1 * 1000);
214 EXPECT_GE(readDataInfo.realReadLength_, 0);
215 }
216
217 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_SEEK, TestSize.Level1)
218 {
219 MP4httpMediaDownloader->GetSeekable();
220 bool result = MP4httpMediaDownloader->SeekToPos(100);
221 EXPECT_TRUE(result);
222 result = MP4httpMediaDownloader->SeekToPos(10000000);
223 EXPECT_TRUE(result);
224 }
225
226 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV, TestSize.Level1)
227 {
228 FLVhttpMediaDownloader->GetSeekable();
229 unsigned char buff[10];
230 ReadDataInfo readDataInfo;
231 readDataInfo.streamId_ = 0;
232 readDataInfo.wantReadLength_ = 10;
233 readDataInfo.isEos_ = true;
234 FLVhttpMediaDownloader->Read(buff, readDataInfo);
235 OSAL::SleepFor(1 * 1000);
236 EXPECT_GE(readDataInfo.realReadLength_, 0);
237 }
238
239 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV_DUA, TestSize.Level1)
240 {
241 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
242 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4);
243 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0702(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 244 std::shared_ptr<DownloadRequest>& request) {};
245 httpMediaDownloader->SetStatusCallback(statusCallback);
246 std::map<std::string, std::string> httpHeader;
247 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
248 httpMediaDownloader->GetSeekable();
249 unsigned char buff[10];
250 ReadDataInfo readDataInfo;
251 readDataInfo.streamId_ = 0;
252 readDataInfo.wantReadLength_ = 10;
253 readDataInfo.isEos_ = true;
254 httpMediaDownloader->Read(buff, readDataInfo);
255 OSAL::SleepFor(1 * 1000);
256 httpMediaDownloader->Close(true);
257 httpMediaDownloader = nullptr;
258 EXPECT_GE(readDataInfo.realReadLength_, 0);
259 }
260
261 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_FLV_MAX_BUFFER, TestSize.Level1)
262 {
263 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
264 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 30);
265 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0802(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 266 std::shared_ptr<DownloadRequest>& request) {};
267 httpMediaDownloader->SetStatusCallback(statusCallback);
268 std::map<std::string, std::string> httpHeader;
269 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
270 httpMediaDownloader->GetSeekable();
271 unsigned char buff[10];
272 ReadDataInfo readDataInfo;
273 readDataInfo.streamId_ = 0;
274 readDataInfo.wantReadLength_ = 10;
275 readDataInfo.isEos_ = true;
276 httpMediaDownloader->Read(buff, readDataInfo);
277 OSAL::SleepFor(1 * 1000);
278 httpMediaDownloader->Close(true);
279 httpMediaDownloader = nullptr;
280 EXPECT_GE(readDataInfo.realReadLength_, 0);
281 }
282
283 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_MP4_DUA, TestSize.Level1)
284 {
285 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
286 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5 * 1024);
287 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0902(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 288 std::shared_ptr<DownloadRequest>& request) {};
289 httpMediaDownloader->SetStatusCallback(statusCallback);
290 std::map<std::string, std::string> httpHeader;
291 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
292 httpMediaDownloader->GetSeekable();
293 unsigned char buff[10];
294 ReadDataInfo readDataInfo;
295 readDataInfo.streamId_ = 0;
296 readDataInfo.wantReadLength_ = 10;
297 readDataInfo.isEos_ = true;
298 httpMediaDownloader->Read(buff, readDataInfo);
299 OSAL::SleepFor(1 * 1000);
300 httpMediaDownloader->Close(true);
301 httpMediaDownloader = nullptr;
302 }
303
304 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN_URL_MP4_DOWNLOADINFO, TestSize.Level1)
305 {
306 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
307 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5 * 1024);
308 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0a02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 309 std::shared_ptr<DownloadRequest>& request) {};
310 httpMediaDownloader->SetStatusCallback(statusCallback);
311 std::map<std::string, std::string> httpHeader;
312 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
313 httpMediaDownloader->GetSeekable();
314 DownloadInfo downloadInfo;
315 httpMediaDownloader->GetDownloadInfo(downloadInfo);
316 httpMediaDownloader->recordSpeedCount_ = 10;
317 httpMediaDownloader->GetDownloadInfo(downloadInfo);
318 httpMediaDownloader->OnClientErrorEvent();
319 httpMediaDownloader->SetInterruptState(true);
320 httpMediaDownloader->SetInterruptState(false);
321 httpMediaDownloader->Close(true);
322 httpMediaDownloader = nullptr;
323 }
324
325 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_SEEK_FLV, TestSize.Level1)
326 {
327 FLVhttpMediaDownloader->GetSeekable();
328 bool result = FLVhttpMediaDownloader->SeekToPos(100);
329 FLVhttpMediaDownloader->SetReadBlockingFlag(true);
330 EXPECT_TRUE(result);
331 }
332
333 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4, TestSize.Level1)
334 {
335 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
336 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5);
337 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0b02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 338 std::shared_ptr<DownloadRequest>& request) {};
339 httpMediaDownloader->SetStatusCallback(statusCallback);
340 std::map<std::string, std::string> httpHeader;
341 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
342 httpMediaDownloader->GetSeekable();
343 unsigned char buff[10];
344 ReadDataInfo readDataInfo;
345 readDataInfo.streamId_ = 0;
346 readDataInfo.wantReadLength_ = 10;
347 readDataInfo.isEos_ = true;
348 httpMediaDownloader->Read(buff, readDataInfo);
349 OSAL::SleepFor(1 * 1000);
350 httpMediaDownloader->UpdateWaterLineAbove();
351 httpMediaDownloader->SetDownloadErrorState();
352 httpMediaDownloader->SetCurrentBitRate(-1, 0);
353 httpMediaDownloader->SetCurrentBitRate(1000, 0);
354 httpMediaDownloader->UpdateWaterLineAbove();
355 httpMediaDownloader->ChangeDownloadPos(false);
356 httpMediaDownloader->Close(true);
357 httpMediaDownloader = nullptr;
358 }
359
360 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4_ERROR, TestSize.Level1)
361 {
362 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
363 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5);
364 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0c02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 365 std::shared_ptr<DownloadRequest>& request) {};
366 httpMediaDownloader->SetStatusCallback(statusCallback);
367 std::map<std::string, std::string> httpHeader;
368 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
369 httpMediaDownloader->GetSeekable();
370 unsigned char buff[10];
371 ReadDataInfo readDataInfo;
372 readDataInfo.streamId_ = 0;
373 readDataInfo.wantReadLength_ = 10;
374 readDataInfo.isEos_ = true;
375 httpMediaDownloader->downloadErrorState_ = true;
376 httpMediaDownloader->Read(buff, readDataInfo);
377 OSAL::SleepFor(1 * 1000);
378 httpMediaDownloader->CheckIsEosCacheBuffer(buff, readDataInfo);
379 httpMediaDownloader->Close(true);
380 httpMediaDownloader = nullptr;
381 }
382
383 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_FLV_ERROR, TestSize.Level1)
384 {
385 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
386 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 30);
387 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0d02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 388 std::shared_ptr<DownloadRequest>& request) {};
389 httpMediaDownloader->SetStatusCallback(statusCallback);
390 Plugins::Callback* sourceCallback = new SourceCallback();
391 httpMediaDownloader->SetCallback(sourceCallback);
392 std::map<std::string, std::string> httpHeader;
393 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
394 httpMediaDownloader->GetSeekable();
395 unsigned char buff[10];
396 ReadDataInfo readDataInfo;
397 readDataInfo.streamId_ = 0;
398 readDataInfo.wantReadLength_ = 10;
399 readDataInfo.isEos_ = true;
400 httpMediaDownloader->Read(buff, readDataInfo);
401 OSAL::SleepFor(1 * 1000);
402 httpMediaDownloader->CheckIsEosRingBuffer(buff, readDataInfo);
403 httpMediaDownloader->OnClientErrorEvent();
404 httpMediaDownloader->Close(true);
405 httpMediaDownloader = nullptr;
406 }
407
408 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_MP4_NULL, TestSize.Level1)
409 {
410 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
411 std::make_shared<HttpMediaDownloader>(MP4_NULL_SEGMENT_BASE);
412 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0e02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 413 std::shared_ptr<DownloadRequest>& request) {};
414 httpMediaDownloader->SetStatusCallback(statusCallback);
415 Plugins::Callback* sourceCallback = new SourceCallback();
416 httpMediaDownloader->SetCallback(sourceCallback);
417 std::map<std::string, std::string> httpHeader;
418 httpMediaDownloader->Open(MP4_NULL_SEGMENT_BASE, httpHeader);
419 httpMediaDownloader->GetSeekable();
420 unsigned char buff[10];
421 ReadDataInfo readDataInfo;
422 readDataInfo.streamId_ = 0;
423 readDataInfo.wantReadLength_ = 10;
424 readDataInfo.isEos_ = true;
425 httpMediaDownloader->Read(buff, readDataInfo);
426 OSAL::SleepFor(1 * 1000);
427 httpMediaDownloader->UpdateWaterLineAbove();
428 httpMediaDownloader->SetDownloadErrorState();
429 httpMediaDownloader->SetCurrentBitRate(-1, 0);
430 httpMediaDownloader->SetCurrentBitRate(1000, 0);
431 httpMediaDownloader->UpdateWaterLineAbove();
432 httpMediaDownloader->HandleCachedDuration();
433 httpMediaDownloader->Close(true);
434 httpMediaDownloader = nullptr;
435 }
436
437 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_FLC_SEEK, TestSize.Level1)
438 {
439 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
440 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4);
441 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c0f02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 442 std::shared_ptr<DownloadRequest>& request) {};
443 httpMediaDownloader->SetStatusCallback(statusCallback);
444 std::map<std::string, std::string> httpHeader;
445 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
446 httpMediaDownloader->GetSeekable();
447 unsigned char buff[5 * 1024 * 1024];
448 ReadDataInfo readDataInfo;
449 readDataInfo.streamId_ = 0;
450 readDataInfo.wantReadLength_ = 5 * 1024 * 1024;
451 readDataInfo.isEos_ = true;
452 httpMediaDownloader->downloadErrorState_ = true;
453 httpMediaDownloader->Read(buff, readDataInfo);
454 OSAL::SleepFor(1000);
455 httpMediaDownloader->SeekToPos(100 * 1024 * 1024);
456 httpMediaDownloader->Close(true);
457 httpMediaDownloader = nullptr;
458 }
459
460
461 HWTEST_F(HttpMediaDownloaderUnitTest, GetContentLength, TestSize.Level1)
462 {
463 MP4httpMediaDownloader->GetContentLength();
464 FLVhttpMediaDownloader->GetContentLength();
465 }
466
467 HWTEST_F(HttpMediaDownloaderUnitTest, GetStartedStatus, TestSize.Level1)
468 {
469 MP4httpMediaDownloader->startedPlayStatus_ = false;
470 FLVhttpMediaDownloader->startedPlayStatus_ = false;
471 EXPECT_FALSE(MP4httpMediaDownloader->GetStartedStatus());
472 EXPECT_FALSE(FLVhttpMediaDownloader->GetStartedStatus());
473 }
474
475 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_OPEN, TestSize.Level1)
476 {
477 EXPECT_TRUE(g_flvResult);
478 EXPECT_TRUE(g_mp4Result);
479 }
480
481 HWTEST_F(HttpMediaDownloaderUnitTest, TEST_PAUSE_RESUME, TestSize.Level1)
482 {
483 MP4httpMediaDownloader->Pause();
484 FLVhttpMediaDownloader->Pause();
485 EXPECT_TRUE(MP4httpMediaDownloader->isInterrupt_);
486 EXPECT_TRUE(FLVhttpMediaDownloader->isInterrupt_);
487 MP4httpMediaDownloader->Resume();
488 FLVhttpMediaDownloader->Resume();
489 EXPECT_FALSE(MP4httpMediaDownloader->isInterrupt_);
490 EXPECT_FALSE(FLVhttpMediaDownloader->isInterrupt_);
491 }
492
493 HWTEST_F(HttpMediaDownloaderUnitTest, HandleBuffering1, TestSize.Level1)
494 {
495 MP4httpMediaDownloader->isBuffering_ = false;
496 EXPECT_FALSE(MP4httpMediaDownloader->HandleBuffering());
497 }
498
499 HWTEST_F(HttpMediaDownloaderUnitTest, GET_PLAYBACK_INFO_001, TestSize.Level1)
500 {
501 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
502 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5);
503 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c1002(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 504 std::shared_ptr<DownloadRequest>& request) {};
505 httpMediaDownloader->SetStatusCallback(statusCallback);
506 std::map<std::string, std::string> httpHeader;
507 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
508 PlaybackInfo playbackInfo;
509 httpMediaDownloader->GetPlaybackInfo(playbackInfo);
510 EXPECT_EQ(playbackInfo.serverIpAddress, "");
511 EXPECT_EQ(playbackInfo.averageDownloadRate, 0);
512 EXPECT_EQ(playbackInfo.isDownloading, true);
513 EXPECT_EQ(playbackInfo.downloadRate, 0);
514 EXPECT_EQ(playbackInfo.bufferDuration, 0);
515 }
516
517 HWTEST_F(HttpMediaDownloaderUnitTest, GET_PLAYBACK_INFO_002, TestSize.Level1)
518 {
519 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
520 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5);
521 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon7c7ba23c1102(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 522 std::shared_ptr<DownloadRequest>& request) {};
523 httpMediaDownloader->SetStatusCallback(statusCallback);
524 std::map<std::string, std::string> httpHeader;
525 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
526 httpMediaDownloader->totalDownloadDuringTime_ = 1000;
527 httpMediaDownloader->totalBits_ = 1000;
528 httpMediaDownloader->isDownloadFinish_ = true;
529 httpMediaDownloader->recordData_->downloadRate = 1000;
530 httpMediaDownloader->recordData_->bufferDuring = 1;
531 PlaybackInfo playbackInfo;
532 httpMediaDownloader->GetPlaybackInfo(playbackInfo);
533 EXPECT_EQ(playbackInfo.serverIpAddress, "");
534 EXPECT_EQ(playbackInfo.averageDownloadRate, 1000);
535 EXPECT_EQ(playbackInfo.isDownloading, false);
536 EXPECT_EQ(playbackInfo.downloadRate, 1000);
537 EXPECT_EQ(playbackInfo.bufferDuration, 0);
538 }
539
540 }