• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mpd_downloader_unit_test.h"
17 #include <iostream>
18 #include "dash_mpd_downloader.h"
19 #include "utils/time_utils.h"
20 #include "http_server_demo.h"
21 
22 namespace OHOS {
23 namespace Media {
24 namespace Plugins {
25 namespace HttpPlugin {
26 namespace {
27 constexpr int32_t SERVERPORT = 47777;
28 static const std::string MPD_SEGMENT_BASE = "http://127.0.0.1:47777/test_dash/segment_base/index.mpd";
29 static const std::string MPD_SEGMENT_LIST = "http://127.0.0.1:47777/test_dash/segment_list/index.mpd";
30 static const std::string MPD_SEGMENT_TEMPLATE = "http://127.0.0.1:47777/test_dash/segment_template/index.mpd";
31 static const std::string MPD_SEGMENT_LIST_TIMELINE = "http://127.0.0.1:47777/test_dash/segment_list/index_timeline.mpd";
32 static const std::string MPD_SEGMENT_TEMPLATE_ADPT = "http://127.0.0.1:47777/test_dash/segment_template/index_adpt.mpd";
33 static const std::string URL_TEMPLATE_TIMELINE = "http://127.0.0.1:47777/test_dash/segment_template/index_timeline.mpd";
34 static const std::string URL_SEGMENT_BASE_IN_PERIOD = "http://127.0.0.1:47777/test_dash/segment_base/index_period.mpd";
35 constexpr unsigned int INIT_WIDTH = 1280;
36 constexpr unsigned int INIT_HEIGHT = 720;
37 }
38 
39 using namespace testing::ext;
40 
41 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server = nullptr;
42 std::shared_ptr<DashMpdDownloader> g_mpdDownloader = nullptr;
43 
SetUpTestCase(void)44 void DashMpdDownloaderUnitTest::SetUpTestCase(void)
45 {
46     g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
47     g_server->StartServer(SERVERPORT);
48     std::cout << "start" << std::endl;
49 
50     g_mpdDownloader = std::make_shared<DashMpdDownloader>();
51     auto statusCallback = [] (DownloadStatus&& status, std::shared_ptr<Downloader>& downloader,
52                               std::shared_ptr<DownloadRequest>& request) {};
53     g_mpdDownloader->SetStatusCallback(statusCallback);
54     g_mpdDownloader->SetInitResolution(INIT_WIDTH, INIT_HEIGHT);
55     g_mpdDownloader->Open(MPD_SEGMENT_BASE);
56 }
57 
TearDownTestCase(void)58 void DashMpdDownloaderUnitTest::TearDownTestCase(void)
59 {
60     g_mpdDownloader->SetInterruptState(true);
61     g_mpdDownloader->Close(true);
62     g_mpdDownloader = nullptr;
63     g_server->StopServer();
64     g_server = nullptr;
65 }
66 
SetUp(void)67 void DashMpdDownloaderUnitTest::SetUp(void)
68 {
69     if (g_server == nullptr) {
70         g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
71         g_server->StartServer(SERVERPORT);
72         std::cout << "start server" << std::endl;
73     }
74 }
75 
TearDown(void)76 void DashMpdDownloaderUnitTest::TearDown(void) {}
77 
78 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN, TestSize.Level1)
79 {
80     std::string testUrl = MPD_SEGMENT_BASE;
81     EXPECT_EQ(testUrl, g_mpdDownloader->GetUrl());
82 }
83 
84 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_SEEKABLE, TestSize.Level1)
85 {
86     Seekable value = g_mpdDownloader->GetSeekable();
87     EXPECT_EQ(Seekable::SEEKABLE, value);
88 }
89 
90 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_DURATION, TestSize.Level1)
91 {
92     EXPECT_GE(g_mpdDownloader->GetDuration(), 0);
93 }
94 
95 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_BITRATES, TestSize.Level1)
96 {
97     std::vector<uint32_t> bitrates = g_mpdDownloader->GetBitRates();
98 
99     EXPECT_GE(bitrates.size(), 0);
100 }
101 
102 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_BITRATES_BY_HDR, TestSize.Level1)
103 {
104     std::vector<uint32_t> sdrBitrates = g_mpdDownloader->GetBitRatesByHdr(false);
105     EXPECT_GE(sdrBitrates.size(), 0);
106 
107     std::vector<uint32_t> hdrBitrates = g_mpdDownloader->GetBitRatesByHdr(true);
108     EXPECT_GE(hdrBitrates.size(), 0);
109 }
110 
111 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_STREAM_INFO, TestSize.Level1)
112 {
113     std::vector<StreamInfo> streams;
114     Status status = g_mpdDownloader->GetStreamInfo(streams);
115 
116     EXPECT_GE(streams.size(), 0);
117     EXPECT_EQ(status, Status::OK);
118 }
119 
120 HWTEST_F(DashMpdDownloaderUnitTest, TEST_IS_BITRATE_SAME, TestSize.Level1)
121 {
122     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
123     std::shared_ptr<DashStreamDescription> stream = g_mpdDownloader->GetStreamByStreamId(usingStreamId);
124 
125     EXPECT_NE(stream, nullptr);
126     EXPECT_TRUE(g_mpdDownloader->IsBitrateSame(stream->bandwidth_));
127 }
128 
129 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_BREAK_POINT_SEGMENT, TestSize.Level1)
130 {
131     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
132     std::shared_ptr<DashSegment> seg = nullptr;
133     int64_t breakPoint = 30000;
134     DashMpdGetRet getRet = g_mpdDownloader->GetBreakPointSegment(usingStreamId, breakPoint, seg);
135 
136     std::shared_ptr<DashSegment> segError = nullptr;
137     int64_t breakPointError = 300000;
138     g_mpdDownloader->GetBreakPointSegment(usingStreamId, breakPointError, segError);
139 
140     EXPECT_NE(getRet, DASH_MPD_GET_ERROR);
141     EXPECT_EQ(segError, nullptr);
142 }
143 
144 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SEEK_TO_TS, TestSize.Level1)
145 {
146     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
147     int64_t duration = g_mpdDownloader->GetDuration();
148     int64_t seekTime = duration / 2;
149     std::shared_ptr<DashSegment> seg = nullptr;
150     g_mpdDownloader->SeekToTs(usingStreamId, seekTime / MS_2_NS, seg);
151 
152     EXPECT_NE(seg, nullptr);
153 }
154 
155 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_SEGMENT, TestSize.Level1)
156 {
157     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
158     std::shared_ptr<DashSegment> seg = nullptr;
159     g_mpdDownloader->GetNextSegmentByStreamId(usingStreamId, seg);
160     EXPECT_NE(seg, nullptr);
161 }
162 
163 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_INIT_SEGMENT, TestSize.Level1)
164 {
165     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
166     std::shared_ptr<DashInitSegment> initSeg = g_mpdDownloader->GetInitSegmentByStreamId(usingStreamId);
167     EXPECT_NE(initSeg, nullptr);
168 }
169 
170 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SET_CURRENT_NUMBER_SEQ, TestSize.Level1)
171 {
172     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
173     g_mpdDownloader->SetCurrentNumberSeqByStreamId(usingStreamId, 10);
174     std::shared_ptr<DashStreamDescription> stream = g_mpdDownloader->GetStreamByStreamId(usingStreamId);
175     EXPECT_NE(stream, nullptr);
176     EXPECT_EQ(stream->currentNumberSeq_, 10);
177 }
178 
179 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_VIDEO_STREAM, TestSize.Level1)
180 {
181     int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
182     std::shared_ptr<DashStreamDescription> stream = g_mpdDownloader->GetStreamByStreamId(usingStreamId);
183     EXPECT_NE(stream, nullptr);
184 
185     uint32_t switchingBitrate = 0;
186     std::vector<uint32_t> bitrates = g_mpdDownloader->GetBitRates();
187     EXPECT_GT(bitrates.size(), 0);
188     for (auto bitrate : bitrates) {
189         if (bitrate != stream->bandwidth_) {
190             switchingBitrate = bitrate;
191             break;
192         }
193     }
194 
195     DashMpdGetRet ret = DASH_MPD_GET_ERROR;
196     DashMpdBitrateParam bitrateParam;
197     bitrateParam.bitrate_ = switchingBitrate;
198     bitrateParam.type_ = DASH_MPD_SWITCH_TYPE_SMOOTH;
199     ret = g_mpdDownloader->GetNextVideoStream(bitrateParam, bitrateParam.streamId_);
200 
201     EXPECT_NE(DASH_MPD_GET_ERROR, ret);
202 }
203 
204 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_TRACK_STREAM, TestSize.Level1)
205 {
206     std::shared_ptr<DashStreamDescription> audioStream = g_mpdDownloader->GetUsingStreamByType(
207         MediaAVCodec::MediaType::MEDIA_TYPE_AUD);
208     EXPECT_NE(audioStream, nullptr);
209 
210     DashMpdTrackParam trackParam;
211     trackParam.isEnd_ = false;
212     trackParam.type_ = MediaAVCodec::MediaType::MEDIA_TYPE_AUD;
213     trackParam.streamId_ = audioStream->streamId_;
214     trackParam.position_ = 2; // update by segment sequence, -1 means no segment downloading
215 
216     DashMpdGetRet ret = g_mpdDownloader->GetNextTrackStream(trackParam);
217     EXPECT_NE(DASH_MPD_GET_ERROR, ret);
218 }
219 
220 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SET_HDR_START, TestSize.Level1)
221 {
222     DashMpdDownloader downloader;
223     std::string testUrl = MPD_SEGMENT_LIST;
224     downloader.SetHdrStart(true);
225     downloader.Open(testUrl);
226     downloader.GetSeekable();
227     int usingStreamId = downloader.GetInUseVideoStreamId();
228     std::shared_ptr<DashStreamDescription> stream = downloader.GetStreamByStreamId(usingStreamId);
229     EXPECT_NE(stream, nullptr);
230     EXPECT_FALSE(stream->videoType_ == DASH_VIDEO_TYPE_HDR_VIVID);
231     downloader.Close(true);
232 }
233 
234 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_TIMELINE_MPD, TestSize.Level1)
235 {
236     DashMpdDownloader downloader;
237     std::string testUrl = MPD_SEGMENT_LIST_TIMELINE;
238     downloader.Open(testUrl);
239     downloader.GetSeekable();
240     int64_t duration = downloader.GetDuration();
241     EXPECT_GE(duration, 0);
242     downloader.Close(true);
243 }
244 
245 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_TEMPLATE_TIMELINE_MPD, TestSize.Level1)
246 {
247     DashMpdDownloader downloader;
248     std::string testUrl = URL_TEMPLATE_TIMELINE;
249     downloader.Open(testUrl);
250     downloader.GetSeekable();
251     int64_t duration = downloader.GetDuration();
252     EXPECT_GE(duration, 0);
253     downloader.Close(true);
254 }
255 
256 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_ADPT_MPD, TestSize.Level1)
257 {
258     DashMpdDownloader downloader;
259     std::string testUrl = MPD_SEGMENT_TEMPLATE_ADPT;
260     downloader.Open(testUrl);
261     downloader.GetSeekable();
262     int64_t duration = downloader.GetDuration();
263     EXPECT_GE(duration, 0);
264     downloader.Close(true);
265 }
266 
267 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_PERIOD_MPD, TestSize.Level1)
268 {
269     DashMpdDownloader downloader;
270     std::string testUrl = URL_SEGMENT_BASE_IN_PERIOD;
271     downloader.Open(testUrl);
272     downloader.GetSeekable();
273     int64_t duration = downloader.GetDuration();
274     EXPECT_GE(duration, 0);
275     downloader.Close(true);
276 }
277 
278 }
279 }
280 }
281 }