2016-03-25 08:39:07 +00:00
|
|
|
// Copyright 2016 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2017-06-15 20:00:28 +00:00
|
|
|
#include "packager/base/files/file_path.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
2016-03-25 08:39:07 +00:00
|
|
|
#include "packager/hls/base/master_playlist.h"
|
|
|
|
#include "packager/hls/base/media_playlist.h"
|
|
|
|
#include "packager/hls/base/mock_media_playlist.h"
|
2016-07-07 19:34:07 +00:00
|
|
|
#include "packager/version/version.h"
|
2016-03-25 08:39:07 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2016-03-25 08:39:07 +00:00
|
|
|
namespace hls {
|
|
|
|
|
|
|
|
using ::testing::AtLeast;
|
2017-06-14 19:25:17 +00:00
|
|
|
using ::testing::NotNull;
|
2016-03-25 08:39:07 +00:00
|
|
|
using ::testing::Return;
|
|
|
|
using ::testing::ReturnRef;
|
2017-06-14 19:25:17 +00:00
|
|
|
using ::testing::SetArgPointee;
|
2017-06-03 00:05:47 +00:00
|
|
|
using ::testing::StrEq;
|
2016-03-25 08:39:07 +00:00
|
|
|
using ::testing::_;
|
2016-08-14 22:28:21 +00:00
|
|
|
using base::FilePath;
|
2016-03-25 08:39:07 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
const char kDefaultMasterPlaylistName[] = "playlist.m3u8";
|
2017-06-14 19:25:17 +00:00
|
|
|
const uint32_t kWidth = 800;
|
|
|
|
const uint32_t kHeight = 600;
|
2016-04-20 22:23:19 +00:00
|
|
|
const MediaPlaylist::MediaPlaylistType kVodPlaylist =
|
|
|
|
MediaPlaylist::MediaPlaylistType::kVod;
|
2016-03-25 08:39:07 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
class MasterPlaylistTest : public ::testing::Test {
|
|
|
|
protected:
|
2017-06-15 20:00:28 +00:00
|
|
|
MasterPlaylistTest()
|
|
|
|
: master_playlist_(kDefaultMasterPlaylistName),
|
|
|
|
test_output_dir_("memory://test_dir"),
|
|
|
|
master_playlist_path_(
|
|
|
|
FilePath::FromUTF8Unsafe(test_output_dir_)
|
|
|
|
.Append(FilePath::FromUTF8Unsafe(kDefaultMasterPlaylistName))
|
|
|
|
.AsUTF8Unsafe()) {}
|
2016-03-25 08:39:07 +00:00
|
|
|
|
|
|
|
void SetUp() override {
|
2016-07-07 19:34:07 +00:00
|
|
|
SetPackagerVersionForTesting("test");
|
2016-03-25 08:39:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MasterPlaylist master_playlist_;
|
|
|
|
std::string test_output_dir_;
|
2017-06-15 20:00:28 +00:00
|
|
|
std::string master_playlist_path_;
|
2016-03-25 08:39:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(MasterPlaylistTest, AddMediaPlaylist) {
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist mock_playlist(kVodPlaylist, "playlist1.m3u8", "somename",
|
|
|
|
"somegroupid");
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&mock_playlist);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) {
|
|
|
|
std::string codec = "avc1";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist mock_playlist(kVodPlaylist, "media1.m3u8", "somename",
|
|
|
|
"somegroupid");
|
|
|
|
mock_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListVideo);
|
2016-03-25 08:39:07 +00:00
|
|
|
mock_playlist.SetCodecForTesting(codec);
|
|
|
|
EXPECT_CALL(mock_playlist, Bitrate()).WillOnce(Return(435889));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(mock_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.WillOnce(DoAll(SetArgPointee<0>(kWidth),
|
|
|
|
SetArgPointee<1>(kHeight),
|
|
|
|
Return(true)));
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&mock_playlist);
|
|
|
|
|
|
|
|
const char kBaseUrl[] = "http://myplaylistdomain.com/";
|
|
|
|
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
|
|
|
|
|
|
|
|
std::string actual;
|
2017-07-10 18:26:22 +00:00
|
|
|
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
|
2016-03-25 08:39:07 +00:00
|
|
|
|
|
|
|
const std::string expected =
|
|
|
|
"#EXTM3U\n"
|
2016-07-07 19:34:07 +00:00
|
|
|
"## Generated with https://github.com/google/shaka-packager version "
|
|
|
|
"test\n"
|
2017-06-14 19:25:17 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=435889,CODECS=\"avc1\",RESOLUTION=800x600\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"http://myplaylistdomain.com/media1.m3u8\n";
|
|
|
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudio) {
|
|
|
|
// First video, sd.m3u8.
|
|
|
|
std::string sd_video_codec = "sdvideocodec";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist sd_video_playlist(kVodPlaylist, "sd.m3u8", "somename",
|
|
|
|
"somegroupid");
|
|
|
|
sd_video_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListVideo);
|
2016-03-25 08:39:07 +00:00
|
|
|
sd_video_playlist.SetCodecForTesting(sd_video_codec);
|
|
|
|
EXPECT_CALL(sd_video_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(300000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(sd_video_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth),
|
|
|
|
SetArgPointee<1>(kHeight),
|
|
|
|
Return(true)));
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&sd_video_playlist);
|
|
|
|
|
|
|
|
// Second video, hd.m3u8.
|
|
|
|
std::string hd_video_codec = "hdvideocodec";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist hd_video_playlist(kVodPlaylist, "hd.m3u8", "somename",
|
|
|
|
"somegroupid");
|
|
|
|
hd_video_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListVideo);
|
2016-03-25 08:39:07 +00:00
|
|
|
hd_video_playlist.SetCodecForTesting(hd_video_codec);
|
|
|
|
EXPECT_CALL(hd_video_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(700000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(hd_video_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth),
|
|
|
|
SetArgPointee<1>(kHeight),
|
|
|
|
Return(true)));
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&hd_video_playlist);
|
|
|
|
|
|
|
|
// First audio, english.m3u8.
|
|
|
|
// Note that audiocodecs should match for different audio tracks with same
|
|
|
|
// group ID.
|
|
|
|
std::string audio_codec = "audiocodec";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist english_playlist(kVodPlaylist, "eng.m3u8", "english",
|
|
|
|
"audiogroup");
|
2017-03-10 23:18:42 +00:00
|
|
|
EXPECT_CALL(english_playlist, GetLanguage()).WillRepeatedly(Return("en"));
|
2016-04-20 22:23:19 +00:00
|
|
|
english_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListAudio);
|
2016-03-25 08:39:07 +00:00
|
|
|
english_playlist.SetCodecForTesting(audio_codec);
|
|
|
|
EXPECT_CALL(english_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(50000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(english_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.Times(0);
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&english_playlist);
|
|
|
|
|
|
|
|
// Second audio, spanish.m3u8.
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist spanish_playlist(kVodPlaylist, "spa.m3u8", "espanol",
|
|
|
|
"audiogroup");
|
2017-03-10 23:18:42 +00:00
|
|
|
EXPECT_CALL(spanish_playlist, GetLanguage()).WillRepeatedly(Return("es"));
|
2016-04-20 22:23:19 +00:00
|
|
|
spanish_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListAudio);
|
2016-03-25 08:39:07 +00:00
|
|
|
spanish_playlist.SetCodecForTesting(audio_codec);
|
|
|
|
EXPECT_CALL(spanish_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(60000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(spanish_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.Times(0);
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&spanish_playlist);
|
|
|
|
|
|
|
|
const char kBaseUrl[] = "http://playlists.org/";
|
|
|
|
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
|
|
|
|
|
|
|
|
std::string actual;
|
2017-07-10 18:26:22 +00:00
|
|
|
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
|
2016-03-25 08:39:07 +00:00
|
|
|
|
|
|
|
const std::string expected =
|
|
|
|
"#EXTM3U\n"
|
2016-07-07 19:34:07 +00:00
|
|
|
"## Generated with https://github.com/google/shaka-packager version "
|
|
|
|
"test\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audiogroup\",NAME=\"english\","
|
2017-03-10 23:18:42 +00:00
|
|
|
"LANGUAGE=\"en\",URI=\"http://playlists.org/eng.m3u8\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audiogroup\",NAME=\"espanol\","
|
2017-03-10 23:18:42 +00:00
|
|
|
"LANGUAGE=\"es\",URI=\"http://playlists.org/spa.m3u8\"\n"
|
2017-06-14 19:25:17 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=360000,CODECS=\"sdvideocodec,audiocodec\""
|
|
|
|
",RESOLUTION=800x600,AUDIO=\"audiogroup\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"http://playlists.org/sd.m3u8\n"
|
2017-06-14 19:25:17 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=760000,CODECS=\"hdvideocodec,audiocodec\""
|
|
|
|
",RESOLUTION=800x600,AUDIO=\"audiogroup\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"http://playlists.org/hd.m3u8\n";
|
|
|
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MasterPlaylistTest, WriteMasterPlaylistMultipleAudioGroups) {
|
|
|
|
// First video, sd.m3u8.
|
|
|
|
std::string video_codec = "videocodec";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist video_playlist(kVodPlaylist, "video.m3u8", "somename",
|
|
|
|
"somegroupid");
|
|
|
|
video_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListVideo);
|
2016-03-25 08:39:07 +00:00
|
|
|
video_playlist.SetCodecForTesting(video_codec);
|
|
|
|
EXPECT_CALL(video_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(300000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(video_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.WillRepeatedly(DoAll(SetArgPointee<0>(kWidth),
|
|
|
|
SetArgPointee<1>(kHeight),
|
|
|
|
Return(true)));
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&video_playlist);
|
|
|
|
|
|
|
|
// First audio, eng_lo.m3u8.
|
|
|
|
std::string audio_codec_lo = "audiocodec_lo";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist eng_lo_playlist(kVodPlaylist, "eng_lo.m3u8", "english_lo",
|
|
|
|
"audio_lo");
|
2017-03-10 23:18:42 +00:00
|
|
|
EXPECT_CALL(eng_lo_playlist, GetLanguage()).WillRepeatedly(Return("en"));
|
2016-04-20 22:23:19 +00:00
|
|
|
eng_lo_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListAudio);
|
2016-03-25 08:39:07 +00:00
|
|
|
eng_lo_playlist.SetCodecForTesting(audio_codec_lo);
|
|
|
|
EXPECT_CALL(eng_lo_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(50000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(eng_lo_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.Times(0);
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&eng_lo_playlist);
|
|
|
|
|
|
|
|
std::string audio_codec_hi = "audiocodec_hi";
|
2016-04-20 22:23:19 +00:00
|
|
|
MockMediaPlaylist eng_hi_playlist(kVodPlaylist, "eng_hi.m3u8", "english_hi",
|
|
|
|
"audio_hi");
|
2017-03-10 23:18:42 +00:00
|
|
|
EXPECT_CALL(eng_hi_playlist, GetLanguage()).WillRepeatedly(Return("en"));
|
2016-04-20 22:23:19 +00:00
|
|
|
eng_hi_playlist.SetStreamTypeForTesting(
|
|
|
|
MediaPlaylist::MediaPlaylistStreamType::kPlayListAudio);
|
2016-03-25 08:39:07 +00:00
|
|
|
eng_hi_playlist.SetCodecForTesting(audio_codec_hi);
|
|
|
|
EXPECT_CALL(eng_hi_playlist, Bitrate())
|
|
|
|
.Times(AtLeast(1))
|
|
|
|
.WillRepeatedly(Return(100000));
|
2017-07-13 20:25:36 +00:00
|
|
|
EXPECT_CALL(eng_hi_playlist, GetDisplayResolution(NotNull(), NotNull()))
|
2017-06-14 19:25:17 +00:00
|
|
|
.Times(0);
|
2016-03-25 08:39:07 +00:00
|
|
|
master_playlist_.AddMediaPlaylist(&eng_hi_playlist);
|
|
|
|
|
|
|
|
const char kBaseUrl[] = "http://anydomain.com/";
|
|
|
|
EXPECT_TRUE(master_playlist_.WriteMasterPlaylist(kBaseUrl, test_output_dir_));
|
|
|
|
|
|
|
|
std::string actual;
|
2017-07-10 18:26:22 +00:00
|
|
|
ASSERT_TRUE(File::ReadFileToString(master_playlist_path_.c_str(), &actual));
|
2016-03-25 08:39:07 +00:00
|
|
|
|
|
|
|
const std::string expected =
|
|
|
|
"#EXTM3U\n"
|
2016-07-07 19:34:07 +00:00
|
|
|
"## Generated with https://github.com/google/shaka-packager version "
|
|
|
|
"test\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio_hi\",NAME=\"english_hi\","
|
2017-03-10 23:18:42 +00:00
|
|
|
"LANGUAGE=\"en\",URI=\"http://anydomain.com/eng_hi.m3u8\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio_lo\",NAME=\"english_lo\","
|
2017-03-10 23:18:42 +00:00
|
|
|
"LANGUAGE=\"en\",URI=\"http://anydomain.com/eng_lo.m3u8\"\n"
|
2017-06-14 19:25:17 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=400000,CODECS=\"videocodec,audiocodec_hi\""
|
|
|
|
",RESOLUTION=800x600,AUDIO=\"audio_hi\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"http://anydomain.com/video.m3u8\n"
|
2017-06-14 19:25:17 +00:00
|
|
|
"#EXT-X-STREAM-INF:BANDWIDTH=350000,CODECS=\"videocodec,audiocodec_lo\""
|
|
|
|
",RESOLUTION=800x600,AUDIO=\"audio_lo\"\n"
|
2016-03-25 08:39:07 +00:00
|
|
|
"http://anydomain.com/video.m3u8\n";
|
|
|
|
|
|
|
|
ASSERT_EQ(expected, actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace hls
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|