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_GET_BREAK_POINT_SEGMENT, TestSize.Level1)
121 {
122 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
123 std::shared_ptr<DashSegment> seg = nullptr;
124 int64_t breakPoint = 30000;
125 DashMpdGetRet getRet = g_mpdDownloader->GetBreakPointSegment(usingStreamId, breakPoint, seg);
126
127 std::shared_ptr<DashSegment> segError = nullptr;
128 int64_t breakPointError = 300000;
129 g_mpdDownloader->GetBreakPointSegment(usingStreamId, breakPointError, segError);
130
131 EXPECT_NE(getRet, DASH_MPD_GET_ERROR);
132 EXPECT_EQ(segError, nullptr);
133 }
134
135 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SEEK_TO_TS, TestSize.Level1)
136 {
137 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
138 int64_t duration = g_mpdDownloader->GetDuration();
139 int64_t seekTime = duration / 2;
140 std::shared_ptr<DashSegment> seg = nullptr;
141 g_mpdDownloader->SeekToTs(usingStreamId, seekTime / MS_2_NS, seg);
142
143 EXPECT_NE(seg, nullptr);
144 }
145
146 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_SEGMENT, TestSize.Level1)
147 {
148 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
149 std::shared_ptr<DashSegment> seg = nullptr;
150 g_mpdDownloader->GetNextSegmentByStreamId(usingStreamId, seg);
151 EXPECT_NE(seg, nullptr);
152 }
153
154 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_INIT_SEGMENT, TestSize.Level1)
155 {
156 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
157 std::shared_ptr<DashInitSegment> initSeg = g_mpdDownloader->GetInitSegmentByStreamId(usingStreamId);
158 EXPECT_NE(initSeg, nullptr);
159 }
160
161 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SET_CURRENT_NUMBER_SEQ, TestSize.Level1)
162 {
163 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
164 g_mpdDownloader->SetCurrentNumberSeqByStreamId(usingStreamId, 10);
165 std::shared_ptr<DashStreamDescription> stream = g_mpdDownloader->GetStreamByStreamId(usingStreamId);
166 EXPECT_NE(stream, nullptr);
167 EXPECT_EQ(stream->currentNumberSeq_, 10);
168 }
169
170 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_VIDEO_STREAM, TestSize.Level1)
171 {
172 int usingStreamId = g_mpdDownloader->GetInUseVideoStreamId();
173 std::shared_ptr<DashStreamDescription> stream = g_mpdDownloader->GetStreamByStreamId(usingStreamId);
174 EXPECT_NE(stream, nullptr);
175
176 uint32_t switchingBitrate = 0;
177 std::vector<uint32_t> bitrates = g_mpdDownloader->GetBitRates();
178 EXPECT_GT(bitrates.size(), 0);
179 for (auto bitrate : bitrates) {
180 if (bitrate != stream->bandwidth_) {
181 switchingBitrate = bitrate;
182 break;
183 }
184 }
185
186 DashMpdGetRet ret = DASH_MPD_GET_ERROR;
187 DashMpdBitrateParam bitrateParam;
188 bitrateParam.bitrate_ = switchingBitrate;
189 bitrateParam.type_ = DASH_MPD_SWITCH_TYPE_SMOOTH;
190 ret = g_mpdDownloader->GetNextVideoStream(bitrateParam, bitrateParam.streamId_);
191
192 EXPECT_NE(DASH_MPD_GET_ERROR, ret);
193 }
194
195 HWTEST_F(DashMpdDownloaderUnitTest, TEST_GET_NEXT_TRACK_STREAM, TestSize.Level1)
196 {
197 std::shared_ptr<DashStreamDescription> audioStream = g_mpdDownloader->GetUsingStreamByType(
198 MediaAVCodec::MediaType::MEDIA_TYPE_AUD);
199 EXPECT_NE(audioStream, nullptr);
200
201 DashMpdTrackParam trackParam;
202 trackParam.isEnd_ = false;
203 trackParam.type_ = MediaAVCodec::MediaType::MEDIA_TYPE_AUD;
204 trackParam.streamId_ = audioStream->streamId_;
205 trackParam.position_ = 2; // update by segment sequence, -1 means no segment downloading
206
207 DashMpdGetRet ret = g_mpdDownloader->GetNextTrackStream(trackParam);
208 EXPECT_NE(DASH_MPD_GET_ERROR, ret);
209 }
210
211 HWTEST_F(DashMpdDownloaderUnitTest, TEST_SET_HDR_START, TestSize.Level1)
212 {
213 DashMpdDownloader downloader;
214 std::string testUrl = MPD_SEGMENT_LIST;
215 downloader.SetHdrStart(true);
216 downloader.Open(testUrl);
217 downloader.GetSeekable();
218 int usingStreamId = downloader.GetInUseVideoStreamId();
219 std::shared_ptr<DashStreamDescription> stream = downloader.GetStreamByStreamId(usingStreamId);
220 EXPECT_NE(stream, nullptr);
221 EXPECT_FALSE(stream->videoType_ == DASH_VIDEO_TYPE_HDR_VIVID);
222 downloader.Close(true);
223 }
224
225 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_TIMELINE_MPD, TestSize.Level1)
226 {
227 DashMpdDownloader downloader;
228 std::string testUrl = MPD_SEGMENT_LIST_TIMELINE;
229 downloader.Open(testUrl);
230 downloader.GetSeekable();
231 int64_t duration = downloader.GetDuration();
232 EXPECT_GE(duration, 0);
233 downloader.Close(true);
234 }
235
236 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_TEMPLATE_TIMELINE_MPD, TestSize.Level1)
237 {
238 DashMpdDownloader downloader;
239 std::string testUrl = URL_TEMPLATE_TIMELINE;
240 downloader.Open(testUrl);
241 downloader.GetSeekable();
242 int64_t duration = downloader.GetDuration();
243 EXPECT_GE(duration, 0);
244 downloader.Close(true);
245 }
246
247 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_ADPT_MPD, TestSize.Level1)
248 {
249 DashMpdDownloader downloader;
250 std::string testUrl = MPD_SEGMENT_TEMPLATE_ADPT;
251 downloader.Open(testUrl);
252 downloader.GetSeekable();
253 int64_t duration = downloader.GetDuration();
254 EXPECT_GE(duration, 0);
255 downloader.Close(true);
256 }
257
258 HWTEST_F(DashMpdDownloaderUnitTest, TEST_OPEN_PERIOD_MPD, TestSize.Level1)
259 {
260 DashMpdDownloader downloader;
261 std::string testUrl = URL_SEGMENT_BASE_IN_PERIOD;
262 downloader.Open(testUrl);
263 downloader.GetSeekable();
264 int64_t duration = downloader.GetDuration();
265 EXPECT_GE(duration, 0);
266 downloader.Close(true);
267 }
268
269 }
270 }
271 }
272 }