• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_source_plugin.h"
17 #include "gtest/gtest.h"
18 #include "http_server_demo.h"
19 #include "source_callback.h"
20 
21 using namespace std;
22 using namespace testing::ext;
23 
24 namespace OHOS {
25 namespace Media {
26 namespace Plugins {
27 namespace HttpPlugin {
28 
29 const uint32_t BUFFERING_SIZE_ = 1024;
30 const uint32_t WATERLINE_HIGH_ = 512;
31 const std::string MP4_SEGMENT_BASE = "http://127.0.0.1:46666/dewu.mp4";
32 static const std::string MPD_SEGMENT_BASE = "http://127.0.0.1:46666/segment_base/index.mpd";
33 static const std::string M3U8_SEGMENT_BASE = "http://127.0.0.1:46666/test_hls/testHLSEncode.m3u8";
34 const std::string FLV_SEGMENT_BASE = "http://127.0.0.1:46666/h264.flv";
35 
36 std::unique_ptr<MediaAVCodec::HttpServerDemo> g_server;
37 class HttpSourcePluginUnitTest : public testing::Test {
38 public:
39     static void SetUpTestCase(void);
40     static void TearDownTestCase(void);
41     void SetUp(void);
42     void TearDown(void);
43 protected:
44     std::shared_ptr<HttpSourcePlugin> httpSourcePlugin;
45     std::shared_ptr<Meta> meta;
46     std::shared_ptr<Buffer> buffer;
47 };
48 
SetUpTestCase(void)49 void HttpSourcePluginUnitTest::SetUpTestCase(void)
50 {
51     g_server = std::make_unique<MediaAVCodec::HttpServerDemo>();
52     g_server->StartServer();
53 }
54 
TearDownTestCase(void)55 void HttpSourcePluginUnitTest::TearDownTestCase(void)
56 {
57     g_server->StopServer();
58     g_server = nullptr;
59 }
60 
SetUp(void)61 void HttpSourcePluginUnitTest::SetUp(void)
62 {
63     meta = std::make_shared<Meta>();
64     httpSourcePlugin = std::make_shared<HttpSourcePlugin>("test");
65     buffer = std::make_shared<Buffer>();
66 }
67 
TearDown(void)68 void HttpSourcePluginUnitTest::TearDown(void)
69 {
70     httpSourcePlugin.reset();
71 }
72 
73 HWTEST_F(HttpSourcePluginUnitTest, TEST_M3U8_pause_resume, TestSize.Level1)
74 {
75     httpSourcePlugin->SelectStream(0);
76     httpSourcePlugin->Pause();
77     httpSourcePlugin->Resume();
78     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(M3U8_SEGMENT_BASE);
79     Plugins::Callback* sourceCallback = new SourceCallback();
80     httpSourcePlugin->SetSource(source);
81     httpSourcePlugin->SetCallback(sourceCallback);
82     httpSourcePlugin->GetSeekable();
83     httpSourcePlugin->Read(1, buffer, 10, 100);
84     httpSourcePlugin->SelectStream(0);
85     httpSourcePlugin->Pause();
86     httpSourcePlugin->Resume();
87     EXPECT_TRUE(httpSourcePlugin);
88 }
89 
90 HWTEST_F(HttpSourcePluginUnitTest, TEST_MP4_SetPlayStrategy, TestSize.Level1)
91 {
92     std::shared_ptr<PlayStrategy> playStrategy = std::make_shared<PlayStrategy>();
93     playStrategy->width = 1920;
94     playStrategy->height = 1080;
95     playStrategy->duration = 100;
96     playStrategy->preferHDR = false;
97     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MP4_SEGMENT_BASE);
98     source->SetPlayStrategy(playStrategy);
99     Plugins::Callback* sourceCallback = new SourceCallback();
100     httpSourcePlugin->SetSource(source);
101     httpSourcePlugin->SetCallback(sourceCallback);
102     httpSourcePlugin->GetSeekable();
103     httpSourcePlugin->Read(1, buffer, 10, 100);
104     OSAL::SleepFor(1 * 1000);
105     EXPECT_TRUE(httpSourcePlugin);
106 }
107 
108 HWTEST_F(HttpSourcePluginUnitTest, TEST_M3U8_SetPlayStrategy, TestSize.Level1)
109 {
110     std::shared_ptr<PlayStrategy> playStrategy = std::make_shared<PlayStrategy>();
111     playStrategy->width = 1920;
112     playStrategy->height = 1080;
113     playStrategy->duration = 100;
114     playStrategy->preferHDR = false;
115     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(M3U8_SEGMENT_BASE);
116     source->SetPlayStrategy(playStrategy);
117     Plugins::Callback* sourceCallback = new SourceCallback();
118     httpSourcePlugin->SetSource(source);
119     httpSourcePlugin->SetCallback(sourceCallback);
120     httpSourcePlugin->GetSeekable();
121     httpSourcePlugin->Read(1, buffer, 10, 100);
122     OSAL::SleepFor(1 * 1000);
123     EXPECT_TRUE(httpSourcePlugin);
124 }
125 
126 HWTEST_F(HttpSourcePluginUnitTest, TEST_MPD_SetPlayStrategy, TestSize.Level1)
127 {
128     std::shared_ptr<PlayStrategy> playStrategy = std::make_shared<PlayStrategy>();
129     playStrategy->width = 1920;
130     playStrategy->height = 1080;
131     playStrategy->duration = 100;
132     playStrategy->preferHDR = false;
133     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MPD_SEGMENT_BASE);
134     source->SetPlayStrategy(playStrategy);
135     Plugins::Callback* sourceCallback = new SourceCallback();
136     httpSourcePlugin->SetSource(source);
137     httpSourcePlugin->SetCallback(sourceCallback);
138     httpSourcePlugin->GetSeekable();
139     httpSourcePlugin->Read(1, buffer, 10, 100);
140     OSAL::SleepFor(1 * 1000);
141     EXPECT_TRUE(httpSourcePlugin);
142 }
143 
144 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_MP4, TestSize.Level1)
145 {
146     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MP4_SEGMENT_BASE);
147     Plugins::Callback* sourceCallback = new SourceCallback();
148     httpSourcePlugin->SetSource(source);
149     httpSourcePlugin->SetCallback(sourceCallback);
150     httpSourcePlugin->GetSeekable();
151     httpSourcePlugin->Read(1, buffer, 10, 100);
152     OSAL::SleepFor(1 * 1000);
153     httpSourcePlugin->SeekTo(10);
154     httpSourcePlugin->SeekTo(100000);
155     EXPECT_TRUE(httpSourcePlugin);
156 }
157 
158 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_MP4_DumuxState, TestSize.Level1)
159 {
160     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MP4_SEGMENT_BASE);
161     Plugins::Callback* sourceCallback = new SourceCallback();
162     httpSourcePlugin->SetSource(source);
163     httpSourcePlugin->SetCallback(sourceCallback);
164     httpSourcePlugin->GetSeekable();
165     httpSourcePlugin->SetDemuxerState(0);
166     httpSourcePlugin->Read(1, buffer, 10, 100);
167     OSAL::SleepFor(1 * 1000);
168     httpSourcePlugin->SeekTo(10);
169     httpSourcePlugin->SeekTo(100000);
170     EXPECT_TRUE(httpSourcePlugin);
171 }
172 
173 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_FUNC, TestSize.Level1)
174 {
175     uint64_t size;
176     int64_t duration;
177     std::vector<uint32_t> bitRates;
178     httpSourcePlugin->GetDuration(duration);
179     httpSourcePlugin->GetBitRates(bitRates);
180     httpSourcePlugin->GetSize(size);
181     httpSourcePlugin->GetSeekable();
182     httpSourcePlugin->SetCurrentBitRate(10, 0);
183     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MP4_SEGMENT_BASE);
184     Plugins::Callback* sourceCallback = new SourceCallback();
185     httpSourcePlugin->SetSource(source);
186     httpSourcePlugin->SetCallback(sourceCallback);
187     httpSourcePlugin->GetSeekable();
188     httpSourcePlugin->Read(1, buffer, 10, 100);
189     OSAL::SleepFor(1 * 1000);
190     httpSourcePlugin->GetSize(size);
191     httpSourcePlugin->GetDuration(duration);
192     httpSourcePlugin->SelectBitRate(10);
193     httpSourcePlugin->GetBitRates(bitRates);
194     httpSourcePlugin->SetDemuxerState(0);
195     httpSourcePlugin->SetCurrentBitRate(10, 0);
196     EXPECT_TRUE(httpSourcePlugin);
197 }
198 
199 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_INFO, TestSize.Level1)
200 {
201     std::vector<StreamInfo> streams;
202     DownloadInfo downloadInfo;
203     PlaybackInfo playbackInfo;
204     httpSourcePlugin->GetDownloadInfo(downloadInfo);
205     httpSourcePlugin->GetPlaybackInfo(playbackInfo);
206     httpSourcePlugin->GetStreamInfo(streams);
207     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MP4_SEGMENT_BASE);
208     Plugins::Callback* sourceCallback = new SourceCallback();
209     httpSourcePlugin->SetSource(source);
210     httpSourcePlugin->SetCallback(sourceCallback);
211     httpSourcePlugin->GetSeekable();
212     httpSourcePlugin->Read(1, buffer, 10, 100);
213     OSAL::SleepFor(1 * 1000);
214     httpSourcePlugin->GetStreamInfo(streams);
215     httpSourcePlugin->SetReadBlockingFlag(true);
216     httpSourcePlugin->SetInterruptState(true);
217     httpSourcePlugin->GetDownloadInfo(downloadInfo);
218     httpSourcePlugin->GetPlaybackInfo(playbackInfo);
219     httpSourcePlugin->SetDownloadErrorState();
220     EXPECT_TRUE(httpSourcePlugin);
221 }
222 
223 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_MPD, TestSize.Level1)
224 {
225     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(MPD_SEGMENT_BASE);
226     Plugins::Callback* sourceCallback = new SourceCallback();
227     httpSourcePlugin->SetSource(source);
228     httpSourcePlugin->SetCallback(sourceCallback);
229     httpSourcePlugin->GetSeekable();
230     httpSourcePlugin->Read(1, buffer, 10, 100);
231     OSAL::SleepFor(1 * 1000);
232     httpSourcePlugin->SeekTo(10);
233     httpSourcePlugin->SeekTo(100000);
234     EXPECT_TRUE(httpSourcePlugin);
235 }
236 
237 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_M3U8, TestSize.Level1)
238 {
239     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(M3U8_SEGMENT_BASE);
240     Plugins::Callback* sourceCallback = new SourceCallback();
241     httpSourcePlugin->SetSource(source);
242     httpSourcePlugin->SetCallback(sourceCallback);
243     httpSourcePlugin->GetSeekable();
244     httpSourcePlugin->Read(1, buffer, 10, 100);
245     OSAL::SleepFor(1 * 1000);
246     httpSourcePlugin->SeekToTime(1, SeekMode::SEEK_NEXT_SYNC);
247     httpSourcePlugin->SeekToTime(100000, SeekMode::SEEK_NEXT_SYNC);
248     EXPECT_TRUE(httpSourcePlugin);
249 }
250 
251 HWTEST_F(HttpSourcePluginUnitTest, TEST_OPEN_M3U8_SetDemuxerState, TestSize.Level1)
252 {
253     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(M3U8_SEGMENT_BASE);
254     Plugins::Callback* sourceCallback = new SourceCallback();
255     httpSourcePlugin->SetSource(source);
256     httpSourcePlugin->SetCallback(sourceCallback);
257     httpSourcePlugin->GetSeekable();
258     httpSourcePlugin->SetDemuxerState(0);
259     httpSourcePlugin->Read(1, buffer, 10, 100);
260     OSAL::SleepFor(1 * 1000);
261     httpSourcePlugin->SeekToTime(1, SeekMode::SEEK_NEXT_SYNC);
262     httpSourcePlugin->SeekToTime(100000, SeekMode::SEEK_NEXT_SYNC);
263     EXPECT_TRUE(httpSourcePlugin);
264 }
265 
266 HWTEST_F(HttpSourcePluginUnitTest, Prepare_IsTrue, TestSize.Level1)
267 {
268     httpSourcePlugin->delayReady = true;
269     Status status = httpSourcePlugin->Prepare();
270     EXPECT_EQ(status, Status::ERROR_DELAY_READY);
271 }
272 
273 HWTEST_F(HttpSourcePluginUnitTest, Prepare_IsFalse, TestSize.Level1)
274 {
275     httpSourcePlugin->delayReady = false;
276     Status status = httpSourcePlugin->Prepare();
277     EXPECT_EQ(status, Status::OK);
278 }
279 
280 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_Reset, TestSize.Level1)
281 {
282     Status status = httpSourcePlugin->Reset();
283     EXPECT_EQ(status, Status::OK);
284 }
285 
286 HWTEST_F(HttpSourcePluginUnitTest, SetReadBlockingFlag_01, TestSize.Level1)
287 {
288     bool isReadBlockingAllowed = true;
289     Status status = httpSourcePlugin->SetReadBlockingFlag(isReadBlockingAllowed);
290     EXPECT_EQ(status, Status::OK);
291 }
292 
293 HWTEST_F(HttpSourcePluginUnitTest, SetReadBlockingFlag_02, TestSize.Level1)
294 {
295     httpSourcePlugin->downloader_ = nullptr;
296     bool isReadBlockingAllowed = true;
297     Status status = httpSourcePlugin->SetReadBlockingFlag(isReadBlockingAllowed);
298     EXPECT_EQ(status, Status::OK);
299 }
300 
301 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_Start, TestSize.Level1)
302 {
303     Status status = httpSourcePlugin->Start();
304     EXPECT_EQ(status, Status::OK);
305 }
306 
307 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_Stop, TestSize.Level1)
308 {
309     Status status = httpSourcePlugin->Stop();
310     EXPECT_EQ(status, Status::OK);
311 }
312 
313 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_GetParameter, TestSize.Level1)
314 {
315     Status status = httpSourcePlugin->GetParameter(meta);
316     EXPECT_EQ(status, Status::OK);
317 }
318 
319 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_SetParameter1, TestSize.Level1)
320 {
321     meta->SetData(Tag::BUFFERING_SIZE, BUFFERING_SIZE_);
322     meta->SetData(Tag::WATERLINE_HIGH, WATERLINE_HIGH_);
323     Status status = httpSourcePlugin->SetParameter(meta);
324     EXPECT_EQ(status, Status::OK);
325 }
326 
327 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_SetParameter2, TestSize.Level1)
328 {
329     meta->SetData(Tag::WATERLINE_HIGH, WATERLINE_HIGH_);
330     Status status = httpSourcePlugin->SetParameter(meta);
331     EXPECT_EQ(status, Status::OK);
332 }
333 
334 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_SetParameter3, TestSize.Level1)
335 {
336     meta->SetData(Tag::BUFFERING_SIZE, BUFFERING_SIZE_);
337     Status status = httpSourcePlugin->SetParameter(meta);
338     EXPECT_EQ(status, Status::OK);
339 }
340 
341 HWTEST_F(HttpSourcePluginUnitTest, HttpSourcePlugin_SetCallback, TestSize.Level1)
342 {
343     httpSourcePlugin->downloader_ = nullptr;
344     Status status = httpSourcePlugin->SetCallback(httpSourcePlugin->callback_);
345     EXPECT_EQ(status, Status::OK);
346 }
347 
348 HWTEST_F(HttpSourcePluginUnitTest, SetDownloaderBySource, TestSize.Level1)
349 {
350     httpSourcePlugin->SetDownloaderBySource(nullptr);
351     EXPECT_EQ(httpSourcePlugin->downloader_, nullptr);
352 }
353 
354 HWTEST_F(HttpSourcePluginUnitTest, IsSeekToTimeSupported1, TestSize.Level1)
355 {
356     httpSourcePlugin->mimeType_ = "video/mp4";
357     httpSourcePlugin->uri_ = "http://example.com/video.m3u8";
358     EXPECT_TRUE(httpSourcePlugin->IsSeekToTimeSupported());
359 }
360 
361 HWTEST_F(HttpSourcePluginUnitTest, IsSeekToTimeSupported2, TestSize.Level1)
362 {
363     httpSourcePlugin->mimeType_ = "video/mp4";
364     httpSourcePlugin->uri_ = "http://example.com/video.mpd";
365     EXPECT_TRUE(httpSourcePlugin->IsSeekToTimeSupported());
366 }
367 
368 HWTEST_F(HttpSourcePluginUnitTest, IsSeekToTimeSupported3, TestSize.Level1)
369 {
370     httpSourcePlugin->mimeType_ = "video/mp4";
371     httpSourcePlugin->uri_ = "http://example.com/video.mp4";
372     EXPECT_FALSE(httpSourcePlugin->IsSeekToTimeSupported());
373 }
374 
375 HWTEST_F(HttpSourcePluginUnitTest, IsSeekToTimeSupported4, TestSize.Level1)
376 {
377     httpSourcePlugin->mimeType_ = "application/x-mpegURL";
378     httpSourcePlugin->uri_ = "http://example.com/video.mp4";
379     EXPECT_FALSE(httpSourcePlugin->IsSeekToTimeSupported());
380 }
381 
382 HWTEST_F(HttpSourcePluginUnitTest, Read_IsNull, TestSize.Level1)
383 {
384     httpSourcePlugin->downloader_ = nullptr;
385     Status status = httpSourcePlugin->Read(1, buffer, 0, 10);
386     EXPECT_EQ(status, Status::ERROR_NULL_POINTER);
387 }
388 
389 HWTEST_F(HttpSourcePluginUnitTest, SetInterruptState1, TestSize.Level1)
390 {
391     bool isInterruptNeeded = true;
392     httpSourcePlugin->SetInterruptState(isInterruptNeeded);
393     EXPECT_EQ(httpSourcePlugin->isInterruptNeeded_, isInterruptNeeded);
394 }
395 
396 HWTEST_F(HttpSourcePluginUnitTest, SetInterruptState2, TestSize.Level1)
397 {
398     bool isInterruptNeeded = false;
399     httpSourcePlugin->SetInterruptState(isInterruptNeeded);
400     EXPECT_EQ(httpSourcePlugin->isInterruptNeeded_, isInterruptNeeded);
401 }
402 
403 HWTEST_F(HttpSourcePluginUnitTest, INIT_HTTP_SOURCE_001, TestSize.Level1)
404 {
405     std::shared_ptr<MediaSource> source = std::make_shared<MediaSource>(FLV_SEGMENT_BASE);
406     Plugins::Callback* sourceCallback = new SourceCallback();
407     MediaStreamList mediaStreams;
408     std::shared_ptr<PlayMediaStream> mediaStreamA = std::make_shared<PlayMediaStream>();
409     mediaStreamA->width = 480;
410     mediaStreamA->height = 360;
411     mediaStreamA->bitrate = 3200;
412     mediaStreamA->url = FLV_SEGMENT_BASE;
413     mediaStreams.push_back(mediaStreamA);
414     std::shared_ptr<PlayMediaStream> mediaStreamB = std::make_shared<PlayMediaStream>();
415     mediaStreamB->width = 640;
416     mediaStreamB->height = 480;
417     mediaStreamB->bitrate = 4800;
418     mediaStreamB->url = FLV_SEGMENT_BASE;
419     mediaStreams.push_back(mediaStreamB);
420     std::shared_ptr<PlayMediaStream> mediaStreamC = std::make_shared<PlayMediaStream>();
421     mediaStreamC->width = 640;
422     mediaStreamC->height = 480;
423     mediaStreamC->bitrate = 4000;
424     mediaStreamC->url = FLV_SEGMENT_BASE;
425     mediaStreams.push_back(mediaStreamC);
426     std::shared_ptr<PlayMediaStream> mediaStreamD = std::make_shared<PlayMediaStream>();
427     mediaStreamD->width = 1280;
428     mediaStreamD->height = 720;
429     mediaStreamD->bitrate = 8000;
430     mediaStreamD->url = FLV_SEGMENT_BASE;
431     mediaStreams.push_back(mediaStreamD);
432     std::shared_ptr<PlayMediaStream> mediaStreamE = std::make_shared<PlayMediaStream>();
433     mediaStreamE->width = 1280;
434     mediaStreamE->height = 720;
435     mediaStreamE->bitrate = 4000;
436     mediaStreamE->url = FLV_SEGMENT_BASE;
437     mediaStreams.push_back(mediaStreamE);
438 
439     source->playMediaStreamVec_ = mediaStreams;
440     httpSourcePlugin->SetSource(source);
441     httpSourcePlugin->SetCallback(sourceCallback);
442     EXPECT_NE(httpSourcePlugin->downloader_, nullptr);
443 }
444 }
445 }
446 }
447 }