1 /*
2 * Copyright (c) 2024 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 "dash_segment_downloader_unit_test.h"
17 #include <iostream>
18 #include "dash_segment_downloader.h"
19 #include "http_server_demo.h"
20
21 namespace OHOS {
22 namespace Media {
23 namespace Plugins {
24 namespace HttpPlugin {
25 namespace {
26 constexpr int32_t SERVERPORT = 47777;
27 // range 0-1065
28 static const std::string AUDIO_SEGMENT_URL = "http://127.0.0.1:47777/test_dash/segment_base/media-audio-und-mp4a.mp4";
29 static const std::string VIDEO_MEDIA_SEGMENT_URL_1 = "http://127.0.0.1:47777/test_dash/segment_list/video/1/seg-1.m4s";
30 static const std::string VIDEO_MEDIA_SEGMENT_URL_2 = "http://127.0.0.1:47777/test_dash/segment_list/video/1/seg-2.m4s";
31 static const std::string VIDEO_INIT_SEGMENT_URL = "http://127.0.0.1:47777/test_dash/segment_list/video/1/init.mp4";
32 }
33 using namespace testing::ext;
34
35 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server = nullptr;
SetUpTestCase(void)36 void DashSegmentDownloaderUnitTest::SetUpTestCase(void)
37 {
38 g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
39 g_server->StartServer(SERVERPORT);
40 std::cout << "start" << std::endl;
41 }
42
TearDownTestCase(void)43 void DashSegmentDownloaderUnitTest::TearDownTestCase(void)
44 {
45 g_server->StopServer();
46 g_server = nullptr;
47 }
48
SetUp(void)49 void DashSegmentDownloaderUnitTest::SetUp(void)
50 {
51 if (g_server == nullptr) {
52 g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
53 g_server->StartServer(SERVERPORT);
54 std::cout << "start server" << std::endl;
55 }
56 }
57
TearDown(void)58 void DashSegmentDownloaderUnitTest::TearDown(void) {}
59
60 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER, TestSize.Level1)
61 {
62 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
63 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
64
65 EXPECT_NE(segmentDownloader, nullptr);
66 EXPECT_EQ(segmentDownloader->GetStreamId(), 1);
67 EXPECT_EQ(segmentDownloader->GetStreamType(), MediaAVCodec::MediaType::MEDIA_TYPE_VID);
68 }
69
70 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_OPEN, TestSize.Level1)
71 {
72 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
73 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
74
75 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
76 segmentSp->url_ = AUDIO_SEGMENT_URL;
77 segmentSp->streamId_ = 1;
78 segmentSp->duration_ = 5;
79 segmentSp->bandwidth_ = 1024;
80 segmentSp->startNumberSeq_ = 1;
81 segmentSp->numberSeq_ = 1;
82 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690202(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 83 std::shared_ptr<DownloadRequest>& request) {};
84 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690302(int streamId) 85 auto doneCallback = [] (int streamId) {};
86 segmentDownloader->SetDownloadDoneCallback(doneCallback);
87 bool result = segmentDownloader->Open(segmentSp);
88 segmentDownloader->Pause();
89 segmentDownloader->Resume();
90 segmentDownloader->Close(true, true);
91 segmentDownloader = nullptr;
92 EXPECT_TRUE(result);
93 }
94
95 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_OPEN_WITH_RANGE, TestSize.Level1)
96 {
97 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
98 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
99
100 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
101 segmentSp->url_ = AUDIO_SEGMENT_URL;
102 segmentSp->streamId_ = 4;
103 segmentSp->duration_ = 5;
104 segmentSp->bandwidth_ = 1024;
105 segmentSp->startNumberSeq_ = 1;
106 segmentSp->numberSeq_ = 1;
107 segmentSp->startRangeValue_ = 0;
108 segmentSp->endRangeValue_ = 1065;
109 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690402(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 110 std::shared_ptr<DownloadRequest>& request) {};
111 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690502(int streamId) 112 auto doneCallback = [] (int streamId) {};
113 segmentDownloader->SetDownloadDoneCallback(doneCallback);
114 bool result = segmentDownloader->Open(segmentSp);
115 segmentDownloader->GetBufferSize();
116 segmentDownloader->GetRingBufferCapacity();
117 segmentDownloader->Close(true, true);
118 segmentDownloader = nullptr;
119 EXPECT_TRUE(result);
120 }
121
122 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_OPEN_WITH_INIT_SEGMENT, TestSize.Level1)
123 {
124 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
125 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
126
127 std::shared_ptr<DashInitSegment> initSeg = std::make_shared<DashInitSegment>();
128 initSeg->url_ = VIDEO_INIT_SEGMENT_URL;
129 initSeg->streamId_ = 1;
130 segmentDownloader->SetInitSegment(initSeg);
131
132 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
133 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
134 segmentSp->streamId_ = 1;
135 segmentSp->duration_ = 5;
136 segmentSp->bandwidth_ = 1024;
137 segmentSp->startNumberSeq_ = 1;
138 segmentSp->numberSeq_ = 1;
139 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690602(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 140 std::shared_ptr<DownloadRequest>& request) {};
141 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690702(int streamId) 142 auto doneCallback = [] (int streamId) {};
143 segmentDownloader->SetDownloadDoneCallback(doneCallback);
144 bool result = segmentDownloader->Open(segmentSp);
145 segmentDownloader->Close(true, true);
146 segmentDownloader = nullptr;
147
148 EXPECT_TRUE(result);
149 }
150
151 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_READ, TestSize.Level1)
152 {
153 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
154 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
155
156 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
157 segmentSp->url_ = AUDIO_SEGMENT_URL;
158 segmentSp->streamId_ = 1;
159 segmentSp->duration_ = 5;
160 segmentSp->bandwidth_ = 1024;
161 segmentSp->startNumberSeq_ = 1;
162 segmentSp->numberSeq_ = 1;
163 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690802(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 164 std::shared_ptr<DownloadRequest>& request) {};
165 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690902(int streamId) 166 auto doneCallback = [] (int streamId) {};
167 segmentDownloader->SetDownloadDoneCallback(doneCallback);
168 segmentDownloader->Open(segmentSp);
169 int repeat = 0;
170 bool status = false;
171 while (repeat++ < 1000) {
172 status = segmentDownloader->GetStartedStatus();
173 if (status) {
174 break;
175 }
176 OSAL::SleepFor(2);
177 }
178
179 unsigned char buffer[1024];
180 ReadDataInfo readDataInfo;
181 readDataInfo.streamId_ = 1;
182 readDataInfo.wantReadLength_ = 1024;
183 readDataInfo.realReadLength_ = 0;
184 readDataInfo.nextStreamId_ = 1;
185 std::atomic<bool> isInterruptNeeded = false;
186 DashReadRet result = segmentDownloader->Read(buffer, readDataInfo, isInterruptNeeded);
187 EXPECT_EQ(result, DASH_READ_OK);
188 readDataInfo.streamId_ = 2;
189 result = segmentDownloader->Read(buffer, readDataInfo, isInterruptNeeded);
190 segmentDownloader->Close(true, true);
191 segmentDownloader = nullptr;
192 EXPECT_EQ(result, DASH_READ_OK);
193 }
194
195 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_CLEAN_SEGMENT_BUFFER, TestSize.Level1)
196 {
197 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
198 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
199
200 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
201 segmentSp->url_ = AUDIO_SEGMENT_URL;
202 segmentSp->streamId_ = 1;
203 segmentSp->duration_ = 5;
204 segmentSp->bandwidth_ = 1024;
205 segmentSp->startNumberSeq_ = 1;
206 segmentSp->numberSeq_ = 1;
207 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690a02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 208 std::shared_ptr<DownloadRequest>& request) {};
209 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690b02(int streamId) 210 auto doneCallback = [] (int streamId) {};
211 segmentDownloader->SetDownloadDoneCallback(doneCallback);
212 segmentDownloader->Open(segmentSp);
213 int repeat = 0;
214 bool status = false;
215 while (repeat++ < 1000) {
216 status = segmentDownloader->GetStartedStatus();
217 if (status) {
218 break;
219 }
220 OSAL::SleepFor(2);
221 }
222
223 int64_t remainLastNumberSeq = -1;
224 segmentDownloader->CleanSegmentBuffer(false, remainLastNumberSeq);
225 segmentDownloader->Close(true, true);
226 segmentDownloader = nullptr;
227
228 EXPECT_EQ(remainLastNumberSeq, 1);
229 }
230
231 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_CLEAN_SEGMENT_BUFFER_ALL, TestSize.Level1)
232 {
233 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
234 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
235
236 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
237 segmentSp->url_ = AUDIO_SEGMENT_URL;
238 segmentSp->streamId_ = 1;
239 segmentSp->duration_ = 5;
240 segmentSp->bandwidth_ = 1024;
241 segmentSp->startNumberSeq_ = 1;
242 segmentSp->numberSeq_ = 1;
243 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690c02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 244 std::shared_ptr<DownloadRequest>& request) {};
245 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690d02(int streamId) 246 auto doneCallback = [] (int streamId) {};
247 segmentDownloader->SetDownloadDoneCallback(doneCallback);
248 segmentDownloader->Open(segmentSp);
249
250 int64_t remainLastNumberSeq = -1;
251 bool result = segmentDownloader->CleanSegmentBuffer(true, remainLastNumberSeq);
252 segmentDownloader->Close(true, true);
253 segmentDownloader = nullptr;
254
255 EXPECT_TRUE(result);
256 EXPECT_EQ(remainLastNumberSeq, 1);
257 }
258
259 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_UPDATE_STREAM_ID, TestSize.Level1)
260 {
261 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
262 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
263 segmentDownloader->UpdateStreamId(2);
264 EXPECT_EQ(segmentDownloader->GetStreamId(), 2);
265 }
266
267 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_GET_CONTENT_LENGTH, TestSize.Level1)
268 {
269 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
270 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
271 EXPECT_EQ(segmentDownloader->GetContentLength(), 0);
272 }
273
274 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_GET_IS_SEGMENT_FINISH, TestSize.Level1)
275 {
276 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
277 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
278 EXPECT_FALSE(segmentDownloader->IsSegmentFinish());
279 }
280
281 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_SEEK_TO_TIME, TestSize.Level1)
282 {
283 std::shared_ptr<DashSegmentDownloader> segmentDownloader = std::make_shared<DashSegmentDownloader>(nullptr,
284 1, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
285
286 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
287 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
288 segmentSp->streamId_ = 1;
289 segmentSp->duration_ = 5;
290 segmentSp->bandwidth_ = 1024;
291 segmentSp->startNumberSeq_ = 1;
292 segmentSp->numberSeq_ = 1;
293 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd690e02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 294 std::shared_ptr<DownloadRequest>& request) {};
295 segmentDownloader->SetStatusCallback(statusCallback);
__anon4d54cd690f02(int streamId) 296 auto doneCallback = [] (int streamId) {};
297 segmentDownloader->SetDownloadDoneCallback(doneCallback);
298 segmentDownloader->Open(segmentSp);
299
300 std::shared_ptr<DashSegment> seekSegment = std::make_shared<DashSegment>();
301 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_2;
302 segmentSp->streamId_ = 1;
303 segmentSp->duration_ = 5;
304 segmentSp->bandwidth_ = 1024;
305 segmentSp->startNumberSeq_ = 1;
306 segmentSp->numberSeq_ = 2;
307
308 int32_t streamId = -1;
309 bool result = segmentDownloader->SeekToTime(seekSegment, streamId);
310 segmentDownloader->Close(true, true);
311 segmentDownloader = nullptr;
312
313 EXPECT_FALSE(result);
314 }
315
316 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER_SUCCESS_001, TestSize.Level1)
317 {
318 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(nullptr,
319 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
320 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
321 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
322 segmentSp->streamId_ = 0;
323 segmentSp->duration_ = 5;
324 segmentSp->bandwidth_ = 1024;
325 segmentSp->startNumberSeq_ = 0;
326 segmentSp->numberSeq_ = 0;
327 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691002(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 328 std::shared_ptr<DownloadRequest>& request) {};
329 segmentDownloaderSp->SetStatusCallback(statusCallback);
__anon4d54cd691102(int streamId) 330 auto doneCallback = [] (int streamId) {};
331 segmentDownloaderSp->SetDownloadDoneCallback(doneCallback);
332 segmentDownloaderSp->Open(segmentSp);
333 OSAL::SleepFor(1000);
334 segmentDownloaderSp->Close(true, true);
335 bool status = segmentDownloaderSp->GetStartedStatus();
336 EXPECT_GE(status, false);
337 }
338
339 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER_WATERLINE_001, TestSize.Level1)
340 {
341 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(nullptr,
342 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
343 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
344 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
345 segmentSp->streamId_ = 0;
346 segmentSp->duration_ = 5;
347 segmentSp->bandwidth_ = 1024;
348 segmentSp->startNumberSeq_ = 0;
349 segmentSp->numberSeq_ = 0;
350 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691202(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 351 std::shared_ptr<DownloadRequest>& request) {};
352 segmentDownloaderSp->SetStatusCallback(statusCallback);
__anon4d54cd691302(int streamId) 353 auto doneCallback = [] (int streamId) {};
354 segmentDownloaderSp->SetDownloadDoneCallback(doneCallback);
355 segmentDownloaderSp->SetDemuxerState();
356 segmentDownloaderSp->SetCurrentBitRate(1048576);
357 unsigned char buffer[1024];
358 ReadDataInfo readDataInfo;
359 readDataInfo.streamId_ = 1;
360 readDataInfo.wantReadLength_ = 1024;
361 readDataInfo.realReadLength_ = 0;
362 readDataInfo.nextStreamId_ = 1;
363 std::atomic<bool> isInterruptNeeded = false;
364 DashReadRet result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
365 EXPECT_EQ(result, DASH_READ_AGAIN);
366 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
367 EXPECT_EQ(result, DASH_READ_AGAIN);
368 segmentDownloaderSp->Open(segmentSp);
369 OSAL::SleepFor(1000);
370 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
371 segmentDownloaderSp->Close(true, true);
372 EXPECT_GE(result, DASH_READ_SEGMENT_DOWNLOAD_FINISH);
373 }
374
375 class SourceCallbackMock : public Plugins::Callback {
376 public:
OnEvent(const Plugins::PluginEvent & event)377 void OnEvent(const Plugins::PluginEvent &event) override
378 {
379 (void)event;
380 }
381
SetSelectBitRateFlag(bool flag,uint32_t desBitRate)382 void SetSelectBitRateFlag(bool flag, uint32_t desBitRate) override
383 {
384 (void)flag;
385 (void)desBitRate;
386 }
387
CanAutoSelectBitRate()388 bool CanAutoSelectBitRate() override
389 {
390 return true;
391 }
392 };
393
394 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER_WATERLINE_002, TestSize.Level1)
395 {
396 Plugins::Callback* sourceCallback = new SourceCallbackMock();
397 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
398 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
399 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
400 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
401 segmentSp->streamId_ = 0;
402 segmentSp->duration_ = 5;
403 segmentSp->bandwidth_ = 1024;
404 segmentSp->startNumberSeq_ = 0;
405 segmentSp->numberSeq_ = 0;
406 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691402(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 407 std::shared_ptr<DownloadRequest>& request) {};
408 segmentDownloaderSp->SetStatusCallback(statusCallback);
__anon4d54cd691502(int streamId) 409 auto doneCallback = [] (int streamId) {};
410 segmentDownloaderSp->SetDownloadDoneCallback(doneCallback);
411 segmentDownloaderSp->SetDemuxerState();
412 segmentDownloaderSp->SetCurrentBitRate(1048576);
413 unsigned char buffer[1024];
414 ReadDataInfo readDataInfo;
415 readDataInfo.streamId_ = 1;
416 readDataInfo.wantReadLength_ = 1024;
417 readDataInfo.realReadLength_ = 0;
418 readDataInfo.nextStreamId_ = 1;
419 std::atomic<bool> isInterruptNeeded = false;
420 DashReadRet result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
421 EXPECT_EQ(result, DASH_READ_AGAIN);
422 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
423 EXPECT_EQ(result, DASH_READ_AGAIN);
424 segmentDownloaderSp->Open(segmentSp);
425 segmentDownloaderSp->SetAllSegmentFinished();
426 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
427 segmentDownloaderSp->Close(true, true);
428 EXPECT_EQ(result, DASH_READ_END);
429 delete sourceCallback;
430 sourceCallback = nullptr;
431 }
432
433 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER_WATERLINE_003, TestSize.Level1)
434 {
435 Plugins::Callback* sourceCallback = new SourceCallbackMock();
436 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
437 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
438 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
439 segmentSp->url_ = VIDEO_MEDIA_SEGMENT_URL_1;
440 segmentSp->streamId_ = 0;
441 segmentSp->duration_ = 5;
442 segmentSp->bandwidth_ = 1024;
443 segmentSp->startNumberSeq_ = 0;
444 segmentSp->numberSeq_ = 0;
445 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691602(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 446 std::shared_ptr<DownloadRequest>& request) {};
447 segmentDownloaderSp->SetStatusCallback(statusCallback);
__anon4d54cd691702(int streamId) 448 auto doneCallback = [] (int streamId) {};
449 segmentDownloaderSp->SetDownloadDoneCallback(doneCallback);
450 segmentDownloaderSp->SetDemuxerState();
451 segmentDownloaderSp->SetCurrentBitRate(1048576000);
452 unsigned char buffer[1024];
453 ReadDataInfo readDataInfo;
454 readDataInfo.streamId_ = 1;
455 readDataInfo.wantReadLength_ = 1024;
456 readDataInfo.realReadLength_ = 0;
457 readDataInfo.nextStreamId_ = 1;
458 std::atomic<bool> isInterruptNeeded = false;
459 DashReadRet result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
460 EXPECT_EQ(result, DASH_READ_AGAIN);
461 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
462 EXPECT_EQ(result, DASH_READ_AGAIN);
463 segmentDownloaderSp->Open(segmentSp);
464 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
465 segmentDownloaderSp->Close(true, true);
466 EXPECT_EQ(result, DASH_READ_AGAIN);
467 delete sourceCallback;
468 sourceCallback = nullptr;
469 }
470
471 HWTEST_F(DashSegmentDownloaderUnitTest, TEST_DASH_SEGMENT_DOWNLOADER_WATERLINE_004, TestSize.Level1)
472 {
473 Plugins::Callback* sourceCallback = new SourceCallbackMock();
474 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
475 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
476 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
477 segmentSp->url_ = "http://test/index.mpd";
478 segmentSp->streamId_ = 0;
479 segmentSp->duration_ = 5;
480 segmentSp->bandwidth_ = 1024;
481 segmentSp->startNumberSeq_ = 0;
482 segmentSp->numberSeq_ = 0;
483 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691802(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 484 std::shared_ptr<DownloadRequest>& request) {};
485 segmentDownloaderSp->SetStatusCallback(statusCallback);
__anon4d54cd691902(int streamId) 486 auto doneCallback = [] (int streamId) {};
487 segmentDownloaderSp->SetDownloadDoneCallback(doneCallback);
488 segmentDownloaderSp->SetDemuxerState();
489 segmentDownloaderSp->SetCurrentBitRate(1048576);
490 unsigned char buffer[1024];
491 ReadDataInfo readDataInfo;
492 readDataInfo.streamId_ = 1;
493 readDataInfo.wantReadLength_ = 1024;
494 readDataInfo.realReadLength_ = 0;
495 readDataInfo.nextStreamId_ = 1;
496 std::atomic<bool> isInterruptNeeded = false;
497 segmentDownloaderSp->Open(segmentSp);
498 DashReadRet result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
499 EXPECT_EQ(result, DASH_READ_AGAIN);
500 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
501 EXPECT_EQ(result, DASH_READ_AGAIN);
502 isInterruptNeeded.store(true);
503 result = segmentDownloaderSp->Read(buffer, readDataInfo, isInterruptNeeded);
504 segmentDownloaderSp->Close(true, true);
505 EXPECT_EQ(result, DASH_READ_INTERRUPT);
506 delete sourceCallback;
507 sourceCallback = nullptr;
508 }
509
510 HWTEST_F(DashSegmentDownloaderUnitTest, SET_DURATION_FOR_PLAYING_001, TestSize.Level1)
511 {
512 Plugins::Callback* sourceCallback = new SourceCallbackMock();
513 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
514 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
515 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691a02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 516 std::shared_ptr<DownloadRequest>& request) {};
517 segmentDownloaderSp->SetStatusCallback(statusCallback);
518 segmentDownloaderSp->SetDurationForPlaying(1);
519 segmentDownloaderSp->SetCurrentBitRate(100);
520 segmentDownloaderSp->NotifyInitSuccess();
521 EXPECT_EQ(segmentDownloaderSp->GetBufferringStatus(), true);
522 }
523
524 HWTEST_F(DashSegmentDownloaderUnitTest, IS_NEED_BUFFER_FOR_PLAYING_001, TestSize.Level1)
525 {
526 Plugins::Callback* sourceCallback = new SourceCallbackMock();
527 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
528 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
529 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691b02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 530 std::shared_ptr<DownloadRequest>& request) {};
531 segmentDownloaderSp->SetStatusCallback(statusCallback);
532 segmentDownloaderSp->SetDurationForPlaying(1);
533 segmentDownloaderSp->SetCurrentBitRate(100);
534 segmentDownloaderSp->NotifyInitSuccess();
535 OSAL::SleepFor(35000);
536 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
537 segmentSp->url_ = VIDEO_INIT_SEGMENT_URL;
538 segmentSp->streamId_ = 0;
539 segmentSp->duration_ = 5;
540 segmentSp->bandwidth_ = 1024;
541 segmentSp->startNumberSeq_ = 0;
542 segmentSp->numberSeq_ = 0;
543 segmentDownloaderSp->Open(segmentSp);
544 OSAL::SleepFor(1000);
545 EXPECT_EQ(segmentDownloaderSp->GetBufferringStatus(), false);
546 }
547
548 HWTEST_F(DashSegmentDownloaderUnitTest, IS_NEED_BUFFER_FOR_PLAYING_002, TestSize.Level1)
549 {
550 Plugins::Callback* sourceCallback = new SourceCallbackMock();
551 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
552 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
553 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691c02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 554 std::shared_ptr<DownloadRequest>& request) {};
555 segmentDownloaderSp->SetStatusCallback(statusCallback);
556 segmentDownloaderSp->SetDurationForPlaying(0.000001);
557 segmentDownloaderSp->SetCurrentBitRate(100);
558 segmentDownloaderSp->NotifyInitSuccess();
559 std::shared_ptr<DashSegment> segmentSp = std::make_shared<DashSegment>();
560 segmentSp->url_ = VIDEO_INIT_SEGMENT_URL;
561 segmentSp->streamId_ = 0;
562 segmentSp->duration_ = 5;
563 segmentSp->bandwidth_ = 1024;
564 segmentSp->startNumberSeq_ = 0;
565 segmentSp->numberSeq_ = 0;
566 segmentDownloaderSp->Open(segmentSp);
567 OSAL::SleepFor(1000);
568 EXPECT_EQ(segmentDownloaderSp->GetBufferringStatus(), false);
569 }
570
571 HWTEST_F(DashSegmentDownloaderUnitTest, NOTIFY_INIT_SUCCESS_001, TestSize.Level1)
572 {
573 Plugins::Callback* sourceCallback = new SourceCallbackMock();
574 std::shared_ptr<DashSegmentDownloader> segmentDownloaderSp = std::make_shared<DashSegmentDownloader>(sourceCallback,
575 0, MediaAVCodec::MediaType::MEDIA_TYPE_VID, 10, nullptr);
576 auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
__anon4d54cd691d02(DownloadStatus&& status, std::shared_ptr<Downloader>& downloader, std::shared_ptr<DownloadRequest>& request) 577 std::shared_ptr<DownloadRequest>& request) {};
578 segmentDownloaderSp->SetStatusCallback(statusCallback);
579 segmentDownloaderSp->SetDurationForPlaying(10);
580 segmentDownloaderSp->SetCurrentBitRate(0);
581 segmentDownloaderSp->NotifyInitSuccess();
582 EXPECT_EQ(segmentDownloaderSp->GetBufferringStatus(), false);
583 segmentDownloaderSp->SetCurrentBitRate(100);
584 segmentDownloaderSp->NotifyInitSuccess();
585 EXPECT_EQ(segmentDownloaderSp->GetBufferringStatus(), true);
586 }
587 }
588 }
589 }
590 }