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,
__anon6bdde4660202(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,
__anon6bdde4660302(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,
__anon6bdde4660402(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,
__anon6bdde4660502(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,
__anon6bdde4660602(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,
__anon6bdde4660702(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,
__anon6bdde4660802(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,
__anon6bdde4660902(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,
__anon6bdde4660a02(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,
__anon6bdde4660b02(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,
__anon6bdde4660c02(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,
__anon6bdde4660d02(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,
__anon6bdde4660e02(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,
__anon6bdde4660f02(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,
__anon6bdde4661002(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,
__anon6bdde4661102(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,
__anon6bdde4661202(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,
__anon6bdde4661302(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,
__anon6bdde4661402(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,
__anon6bdde4661502(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 HWTEST_F(HttpMediaDownloaderUnitTest, SET_PLAY_STRATEGY_002, TestSize.Level1)
659 {
660 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
661 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 5, nullptr);
662 std::map<std::string, std::string> httpHeader;
663 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661602(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 664 std::shared_ptr<DownloadRequest>& request) {};
665 httpMediaDownloader->SetStatusCallback(statusCallback);
666 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
667 Plugins::Callback* sourceCallback = new SourceCallback();
668 httpMediaDownloader->callback_ = sourceCallback;
669
670 MediaStreamList mediaStreams;
671 std::shared_ptr<PlayMediaStream> mediaStreamA = std::make_shared<PlayMediaStream>();
672 mediaStreamA->width = 480;
673 mediaStreamA->height = 360;
674 mediaStreamA->bitrate = 3200;
675 mediaStreams.push_back(mediaStreamA);
676 std::shared_ptr<PlayMediaStream> mediaStreamB = std::make_shared<PlayMediaStream>();
677 mediaStreamB->width = 640;
678 mediaStreamB->height = 480;
679 mediaStreamB->bitrate = 4800;
680 mediaStreams.push_back(mediaStreamB);
681 std::shared_ptr<PlayMediaStream> mediaStreamC = std::make_shared<PlayMediaStream>();
682 mediaStreamC->width = 640;
683 mediaStreamC->height = 480;
684 mediaStreamC->bitrate = 4000;
685 mediaStreams.push_back(mediaStreamC);
686 std::shared_ptr<PlayMediaStream> mediaStreamD = std::make_shared<PlayMediaStream>();
687 mediaStreamD->width = 1280;
688 mediaStreamD->height = 720;
689 mediaStreamD->bitrate = 8000;
690 mediaStreams.push_back(mediaStreamD);
691 std::shared_ptr<PlayMediaStream> mediaStreamE = std::make_shared<PlayMediaStream>();
692 mediaStreamE->width = 1280;
693 mediaStreamE->height = 720;
694 mediaStreamE->bitrate = 4000;
695 mediaStreams.push_back(mediaStreamE);
696
697 httpMediaDownloader->SetMediaStreams(mediaStreams);
698 std::shared_ptr<PlayStrategy> playStrategy = std::make_shared<PlayStrategy>();
699 playStrategy->width = 1280;
700 playStrategy->height = 720;
701 httpMediaDownloader->SetPlayStrategy(playStrategy);
702 EXPECT_EQ(httpMediaDownloader->defaultStream_->width, 1280);
703 EXPECT_EQ(httpMediaDownloader->defaultStream_->height, 720);
704 EXPECT_EQ(httpMediaDownloader->defaultStream_->bitrate, 4000);
705 }
706
707 HWTEST_F(HttpMediaDownloaderUnitTest, SELECT_BIT_RATE_001, TestSize.Level1)
708 {
709 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
710 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 5, nullptr);
711 std::map<std::string, std::string> httpHeader;
712 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661702(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 713 std::shared_ptr<DownloadRequest>& request) {};
714 httpMediaDownloader->SetStatusCallback(statusCallback);
715 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
716 Plugins::Callback* sourceCallback = new SourceCallback();
717 httpMediaDownloader->callback_ = sourceCallback;
718 MediaStreamList mediaStreams;
719 std::shared_ptr<PlayMediaStream> mediaStreamA = std::make_shared<PlayMediaStream>();
720 mediaStreamA->width = 480;
721 mediaStreamA->height = 360;
722 mediaStreamA->bitrate = 3200;
723 mediaStreamA->url = FLV_SEGMENT_BASE;
724 mediaStreams.push_back(mediaStreamA);
725 std::shared_ptr<PlayMediaStream> mediaStreamB = std::make_shared<PlayMediaStream>();
726 mediaStreamB->width = 640;
727 mediaStreamB->height = 480;
728 mediaStreamB->bitrate = 4800;
729 mediaStreamB->url = FLV_SEGMENT_BASE;
730 mediaStreams.push_back(mediaStreamB);
731 std::shared_ptr<PlayMediaStream> mediaStreamC = std::make_shared<PlayMediaStream>();
732 mediaStreamC->width = 640;
733 mediaStreamC->height = 480;
734 mediaStreamC->bitrate = 4000;
735 mediaStreamC->url = FLV_SEGMENT_BASE;
736 mediaStreams.push_back(mediaStreamC);
737 std::sort(mediaStreams.begin(), mediaStreams.end(),
__anon6bdde4661802(const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) 738 [](const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) {
739 return (streamA->bitrate < streamB->bitrate) ||
740 (streamA->bitrate == streamB->bitrate &&
741 streamA->width * streamA->height < streamB->width * streamB->height);
742 });
743 httpMediaDownloader->SetMediaStreams(mediaStreams);
744 httpMediaDownloader->SelectBitRate(4000);
745 EXPECT_EQ(httpMediaDownloader->defaultStream_->width, 640);
746 EXPECT_EQ(httpMediaDownloader->defaultStream_->height, 480);
747 EXPECT_EQ(httpMediaDownloader->defaultStream_->bitrate, 4000);
748 }
749
750 HWTEST_F(HttpMediaDownloaderUnitTest, CHECK_AUTO_SELECT_BITRATE_001, TestSize.Level1)
751 {
752 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
753 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 5, nullptr);
754 std::map<std::string, std::string> httpHeader;
755 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661902(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 756 std::shared_ptr<DownloadRequest>& request) {};
757 httpMediaDownloader->SetStatusCallback(statusCallback);
758 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
759 Plugins::Callback* sourceCallback = new SourceCallback();
760 httpMediaDownloader->callback_ = sourceCallback;
761 MediaStreamList mediaStreams;
762 std::shared_ptr<PlayMediaStream> mediaStreamA = std::make_shared<PlayMediaStream>();
763 mediaStreamA->width = 480;
764 mediaStreamA->height = 360;
765 mediaStreamA->bitrate = 3200;
766 mediaStreamA->url = FLV_SEGMENT_BASE;
767 mediaStreams.push_back(mediaStreamA);
768 std::shared_ptr<PlayMediaStream> mediaStreamB = std::make_shared<PlayMediaStream>();
769 mediaStreamB->width = 640;
770 mediaStreamB->height = 480;
771 mediaStreamB->bitrate = 4800;
772 mediaStreamB->url = FLV_SEGMENT_BASE;
773 mediaStreams.push_back(mediaStreamB);
774 std::shared_ptr<PlayMediaStream> mediaStreamC = std::make_shared<PlayMediaStream>();
775 mediaStreamC->width = 640;
776 mediaStreamC->height = 480;
777 mediaStreamC->bitrate = 4000;
778 mediaStreamC->url = FLV_SEGMENT_BASE;
779 mediaStreams.push_back(mediaStreamC);
780 std::sort(mediaStreams.begin(), mediaStreams.end(),
__anon6bdde4661a02(const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) 781 [](const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) {
782 return (streamA->bitrate < streamB->bitrate) ||
783 (streamA->bitrate == streamB->bitrate &&
784 streamA->width * streamA->height < streamB->width * streamB->height);
785 });
786 httpMediaDownloader->SetMediaStreams(mediaStreams);
787 httpMediaDownloader->downloadSpeeds_.push_back(5100);
788 EXPECT_EQ(httpMediaDownloader->CheckAutoSelectBitrate(), true);
789 httpMediaDownloader->downloadSpeeds_.push_back(3200);
790 EXPECT_EQ(httpMediaDownloader->CheckAutoSelectBitrate(), false);
791 }
792
793 HWTEST_F(HttpMediaDownloaderUnitTest, GetReadTimeOut, TestSize.Level1)
794 {
795 bool ret = MP4httpMediaDownloader->GetReadTimeOut(false);
796 ret = MP4httpMediaDownloader->GetReadTimeOut(true);
797 EXPECT_FALSE(ret);
798 }
799
800 HWTEST_F(HttpMediaDownloaderUnitTest, StopBufferring, TestSize.Level1)
801 {
802 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
803 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
804 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661b02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 805 std::shared_ptr<DownloadRequest>& request) {};
806 httpMediaDownloader->SetStatusCallback(statusCallback);
807 std::map<std::string, std::string> httpHeader;
808 httpMediaDownloader->Open(MP4_SEGMENT_BASE, httpHeader);
809 httpMediaDownloader->StopBufferring(false);
810 EXPECT_GE(httpMediaDownloader->StopBufferring(true), Status::OK);
811 }
812
813
814 HWTEST_F(HttpMediaDownloaderUnitTest, ClearHasReadBuffer, TestSize.Level1)
815 {
816 MP4httpMediaDownloader->isFirstFrameArrived_ = true;
817 MP4httpMediaDownloader->isNeedClearHasRead_ = true;
818 EXPECT_FALSE(MP4httpMediaDownloader->ClearHasReadBuffer());
819 }
820
821 HWTEST_F(HttpMediaDownloaderUnitTest, RestartAndClearBuffer, TestSize.Level1)
822 {
823 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader1 =
824 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 4, nullptr);
825 auto statusCallback1 = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661c02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 826 std::shared_ptr<DownloadRequest>& request) {};
827 httpMediaDownloader1->SetStatusCallback(statusCallback1);
828 std::map<std::string, std::string> httpHeader1;
829 httpMediaDownloader1->Open(FLV_SEGMENT_BASE, httpHeader1);
830 httpMediaDownloader1->RestartAndClearBuffer();
831
832 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader2 =
833 std::make_shared<HttpMediaDownloader>(MP4_SEGMENT_BASE, 4, nullptr);
834 auto statusCallback2 = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661d02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 835 std::shared_ptr<DownloadRequest>& request) {};
836 httpMediaDownloader2->SetStatusCallback(statusCallback2);
837 std::map<std::string, std::string> httpHeader2;
838 httpMediaDownloader2->Open(MP4_SEGMENT_BASE, httpHeader2);
839 httpMediaDownloader2->RestartAndClearBuffer();
840 EXPECT_TRUE(httpMediaDownloader1->isRingBuffer_);
841 }
842
843 HWTEST_F(HttpMediaDownloaderUnitTest, ClearCacheBuffer, TestSize.Level1)
844 {
845 FLVhttpMediaDownloader->ClearCacheBuffer();
846 MP4httpMediaDownloader->ClearCacheBuffer();
847 EXPECT_TRUE(FLVhttpMediaDownloader->isRingBuffer_);
848 }
849
850 HWTEST_F(HttpMediaDownloaderUnitTest, IsFlvLive, TestSize.Level1)
851 {
852 bool ret = FLVhttpMediaDownloader->IsFlvLive();
853 EXPECT_FALSE(ret);
854 }
855
856 HWTEST_F(HttpMediaDownloaderUnitTest, GetPlayable, TestSize.Level1)
857 {
858 MP4httpMediaDownloader->isBuffering_ = true;
859 EXPECT_FALSE(MP4httpMediaDownloader->GetPlayable());
860
861 MP4httpMediaDownloader->isBuffering_ = false;
862 MP4httpMediaDownloader->isFirstFrameArrived_ = true;
863 MP4httpMediaDownloader->GetPlayable();
864
865 MP4httpMediaDownloader->isBuffering_ = false;
866 MP4httpMediaDownloader->isFirstFrameArrived_ = false;
867 MP4httpMediaDownloader->GetPlayable();
868 MP4httpMediaDownloader->SetAppUid(1);
869 }
870
871 HWTEST_F(HttpMediaDownloaderUnitTest, GetCacheDuration, TestSize.Level1)
872 {
873 float num = MP4httpMediaDownloader->GetCacheDuration(1);
874 num = MP4httpMediaDownloader->GetCacheDuration(0);
875 EXPECT_EQ(num, 5.0);
876 }
877
878 HWTEST_F(HttpMediaDownloaderUnitTest, IsAutoSelectConditionOk_1, TestSize.Level1)
879 {
880 FLVhttpMediaDownloader->downloadSpeeds_.clear();
881
882 EXPECT_FALSE(FLVhttpMediaDownloader->IsAutoSelectConditionOk());
883 EXPECT_FALSE(FLVhttpMediaDownloader->CheckAutoSelectBitrate());
884
885 FLVhttpMediaDownloader->ClearBuffer();
886 MP4httpMediaDownloader->ClearBuffer();
887 }
888
889 HWTEST_F(HttpMediaDownloaderUnitTest, IsAutoSelectConditionOk_2, TestSize.Level1)
890 {
891 std::shared_ptr<HttpMediaDownloader> httpMediaDownloader =
892 std::make_shared<HttpMediaDownloader>(FLV_SEGMENT_BASE, 5, nullptr);
893 std::map<std::string, std::string> httpHeader;
894 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon6bdde4661e02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 895 std::shared_ptr<DownloadRequest>& request) {};
896 httpMediaDownloader->SetStatusCallback(statusCallback);
897 httpMediaDownloader->Open(FLV_SEGMENT_BASE, httpHeader);
898 Plugins::Callback* sourceCallback = new SourceCallback();
899 httpMediaDownloader->callback_ = sourceCallback;
900 MediaStreamList mediaStreams;
901 std::shared_ptr<PlayMediaStream> mediaStreamA = std::make_shared<PlayMediaStream>();
902 mediaStreamA->width = 480;
903 mediaStreamA->height = 360;
904 mediaStreamA->bitrate = 3200;
905 mediaStreamA->url = FLV_SEGMENT_BASE;
906 mediaStreams.push_back(mediaStreamA);
907 std::shared_ptr<PlayMediaStream> mediaStreamB = std::make_shared<PlayMediaStream>();
908 mediaStreamB->width = 640;
909 mediaStreamB->height = 480;
910 mediaStreamB->bitrate = 4800;
911 mediaStreamB->url = FLV_SEGMENT_BASE;
912 mediaStreams.push_back(mediaStreamB);
913 std::shared_ptr<PlayMediaStream> mediaStreamC = std::make_shared<PlayMediaStream>();
914 mediaStreamC->width = 640;
915 mediaStreamC->height = 480;
916 mediaStreamC->bitrate = 4000;
917 mediaStreamC->url = FLV_SEGMENT_BASE;
918 mediaStreams.push_back(mediaStreamC);
919 std::sort(mediaStreams.begin(), mediaStreams.end(),
__anon6bdde4661f02(const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) 920 [](const std::shared_ptr<PlayMediaStream>& streamA, const std::shared_ptr<PlayMediaStream>& streamB) {
921 return (streamA->bitrate < streamB->bitrate) ||
922 (streamA->bitrate == streamB->bitrate &&
923 streamA->width * streamA->height < streamB->width * streamB->height);
924 });
925 httpMediaDownloader->SetMediaStreams(mediaStreams);
926 httpMediaDownloader->SelectBitRate(4000);
927
928 EXPECT_FALSE(httpMediaDownloader->IsAutoSelectConditionOk());
929 httpMediaDownloader->isSelectingBitrate_ = true;
930 EXPECT_FALSE(httpMediaDownloader->IsAutoSelectConditionOk());
931 httpMediaDownloader->defaultStream_ = nullptr;
932
933 EXPECT_FALSE(httpMediaDownloader->IsAutoSelectConditionOk());
934 }
935
936 HWTEST_F(HttpMediaDownloaderUnitTest, IsAutoSelectConditionOk, TestSize.Level1)
937 {
938 FLVhttpMediaDownloader->isAutoSelectBitrate_.store(false);
939 EXPECT_FALSE(FLVhttpMediaDownloader->IsAutoSelectConditionOk());
940
941 FLVhttpMediaDownloader->isAutoSelectBitrate_ = true;
942 FLVhttpMediaDownloader->isRingBuffer_ = false;
943 EXPECT_FALSE(FLVhttpMediaDownloader->IsAutoSelectConditionOk());
944
945 FLVhttpMediaDownloader->isAutoSelectBitrate_ = true;
946 FLVhttpMediaDownloader->isRingBuffer_ = true;
947 FLVhttpMediaDownloader->isSelectingBitrate_ = true;
948 EXPECT_FALSE(FLVhttpMediaDownloader->IsAutoSelectConditionOk());
949
950 FLVhttpMediaDownloader->isAutoSelectBitrate_ = true;
951 FLVhttpMediaDownloader->isRingBuffer_ = true;
952 FLVhttpMediaDownloader->isSelectingBitrate_ = false;
953 FLVhttpMediaDownloader->downloadSpeeds_.clear();
954 EXPECT_FALSE(FLVhttpMediaDownloader->IsAutoSelectConditionOk());
955 }
956 }