From f3d35b730cfb1b6d13f2c5c29e2b8ec7da02d9bb Mon Sep 17 00:00:00 2001 From: Kongqun Yang Date: Thu, 7 Jul 2016 12:34:07 -0700 Subject: [PATCH] Add version information to generated HLS manifests - Also refactor the existing version code. Change-Id: Ib409630c4f87965a37b1d6a1bcec68178a704ce6 --- packager/app/mpd_generator.cc | 4 +-- packager/app/packager_main.cc | 16 +++++++-- packager/app/packager_util.cc | 14 -------- packager/app/test/packager_test.py | 3 +- .../testdata/bear-640x360-a-enc-golden.m3u8 | 1 + .../test/testdata/bear-640x360-a-golden.m3u8 | 1 + .../bear-640x360-av-enc-master-golden.m3u8 | 1 + .../bear-640x360-av-master-golden.m3u8 | 1 + .../testdata/bear-640x360-v-enc-golden.m3u8 | 1 + .../test/testdata/bear-640x360-v-golden.m3u8 | 1 + packager/hls/base/master_playlist.cc | 12 ++++++- packager/hls/base/master_playlist_unittest.cc | 8 +++++ packager/hls/base/media_playlist.cc | 19 +++++++--- packager/hls/base/media_playlist_unittest.cc | 16 +++++++++ packager/media/base/muxer_options.cc | 4 +-- packager/media/base/muxer_options.h | 3 -- packager/media/formats/mp4/segmenter.cc | 13 ++++--- packager/media/formats/webm/segmenter.cc | 13 ++++--- .../media/formats/webm/segmenter_test_base.cc | 4 ++- packager/mpd/base/mpd_builder.cc | 20 +++++++---- packager/mpd/base/mpd_builder_unittest.cc | 4 +-- packager/mpd/base/mpd_options.h | 6 +--- packager/version/version.cc | 36 ++++++++++++++++++- packager/version/version.gyp | 3 ++ packager/version/version.h | 12 ++++++- 25 files changed, 158 insertions(+), 58 deletions(-) diff --git a/packager/app/mpd_generator.cc b/packager/app/mpd_generator.cc index 0ad464e249..b0e080b304 100644 --- a/packager/app/mpd_generator.cc +++ b/packager/app/mpd_generator.cc @@ -91,8 +91,8 @@ int MpdMain(int argc, char** argv) { ExitStatus status = CheckRequiredFlags(); if (status != kSuccess) { - std::string version_string = - base::StringPrintf("mpd_generator version %s", kPackagerVersion); + const std::string version_string = base::StringPrintf( + "mpd_generator version %s", GetPackagerVersion().c_str()); google::ShowUsageWithFlags(version_string.c_str()); return status; } diff --git a/packager/app/packager_main.cc b/packager/app/packager_main.cc index 11a699656f..882f31acfe 100644 --- a/packager/app/packager_main.cc +++ b/packager/app/packager_main.cc @@ -51,6 +51,15 @@ DEFINE_bool(use_fake_clock_for_muxer, "Set to true to use a fake clock for muxer. With this flag set, " "creation time and modification time in outputs are set to 0. " "Should only be used for testing."); +DEFINE_bool(override_version, + false, + "Override packager version in the generated outputs with " + "--test_version if it is set to true. Should be used for " + "testing only."); +DEFINE_string(test_version, + "", + "Packager version for testing. Ignored if --override_version is " + "false. Should be used for testing only."); namespace shaka { namespace media { @@ -517,8 +526,8 @@ int PackagerMain(int argc, char** argv) { google::SetUsageMessage(base::StringPrintf(kUsage, argv[0])); google::ParseCommandLineFlags(&argc, &argv, true); if (argc < 2) { - std::string version_string = - base::StringPrintf("shaka-packager version %s", kPackagerVersion); + const std::string version_string = base::StringPrintf( + "shaka-packager version %s", GetPackagerVersion().c_str()); google::ShowUsageWithFlags(version_string.c_str()); return kSuccess; } @@ -526,6 +535,9 @@ int PackagerMain(int argc, char** argv) { if (!ValidateWidevineCryptoFlags() || !ValidateFixedCryptoFlags()) return kArgumentValidationFailed; + if (FLAGS_override_version) + SetPackagerVersionForTesting(FLAGS_test_version); + LibcryptoThreading libcrypto_threading; // TODO(tinskip): Make InsertStreamDescriptor a member of // StreamDescriptorList. diff --git a/packager/app/packager_util.cc b/packager/app/packager_util.cc index 4696518da6..bb7bb57872 100644 --- a/packager/app/packager_util.cc +++ b/packager/app/packager_util.cc @@ -26,16 +26,6 @@ #include "packager/mpd/base/mpd_builder.h" DEFINE_bool(dump_stream_info, false, "Dump demuxed stream info."); -DEFINE_bool(override_version_string, - false, - "Override packager version string in the generated outputs with " - "--test_version_string if it is set to true. Should be used for " - "testing only."); -DEFINE_string(test_version_string, - "", - "Packager version string for testing. Ignored if " - "--override_version_string is false. Should be used for testing " - "only."); namespace shaka { namespace media { @@ -159,8 +149,6 @@ bool GetMuxerOptions(MuxerOptions* muxer_options) { muxer_options->fragment_sap_aligned = FLAGS_fragment_sap_aligned; muxer_options->num_subsegments_per_sidx = FLAGS_num_subsegments_per_sidx; muxer_options->temp_dir = FLAGS_temp_dir; - if (FLAGS_override_version_string) - muxer_options->packager_version_string = FLAGS_test_version_string; return true; } @@ -173,8 +161,6 @@ bool GetMpdOptions(MpdOptions* mpd_options) { mpd_options->time_shift_buffer_depth = FLAGS_time_shift_buffer_depth; mpd_options->suggested_presentation_delay = FLAGS_suggested_presentation_delay; - if (FLAGS_override_version_string) - mpd_options->packager_version_string = FLAGS_test_version_string; return true; } diff --git a/packager/app/test/packager_test.py b/packager/app/test/packager_test.py index 8957a8de4b..836c8283df 100755 --- a/packager/app/test/packager_test.py +++ b/packager/app/test/packager_test.py @@ -504,8 +504,7 @@ class PackagerAppTest(unittest.TestCase): flags.append('--use_fake_clock_for_muxer') # Override packager version string for testing. - flags += ['--override_version_string', '--test_version_string', - '--'] + flags += ['--override_version', '--test_version', '--'] return flags def _CompareWithGold(self, test_output, golden_file_name): diff --git a/packager/app/test/testdata/bear-640x360-a-enc-golden.m3u8 b/packager/app/test/testdata/bear-640x360-a-enc-golden.m3u8 index 2f53f5c57d..ad8b0a8560 100644 --- a/packager/app/test/testdata/bear-640x360-a-enc-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-a-enc-golden.m3u8 @@ -1,5 +1,6 @@ #EXTM3U #EXT-X-VERSION:5 +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-TARGETDURATION:2 #EXT-X-PLAYLIST-TYPE:VOD #EXTINF:1.021, diff --git a/packager/app/test/testdata/bear-640x360-a-golden.m3u8 b/packager/app/test/testdata/bear-640x360-a-golden.m3u8 index a64e3c7bf1..4dfd5d3c6f 100644 --- a/packager/app/test/testdata/bear-640x360-a-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-a-golden.m3u8 @@ -1,5 +1,6 @@ #EXTM3U #EXT-X-VERSION:5 +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-TARGETDURATION:2 #EXT-X-PLAYLIST-TYPE:VOD #EXTINF:1.021, diff --git a/packager/app/test/testdata/bear-640x360-av-enc-master-golden.m3u8 b/packager/app/test/testdata/bear-640x360-av-enc-master-golden.m3u8 index 6b7ee89a16..165ca15afe 100644 --- a/packager/app/test/testdata/bear-640x360-av-enc-master-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-av-enc-master-golden.m3u8 @@ -1,4 +1,5 @@ #EXTM3U +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="stream_0",URI="audio.m3u8" #EXT-X-STREAM-INF:AUDIO="audio",CODECS="avc1.64001e,mp4a.40.2",BANDWIDTH=1217603 video.m3u8 diff --git a/packager/app/test/testdata/bear-640x360-av-master-golden.m3u8 b/packager/app/test/testdata/bear-640x360-av-master-golden.m3u8 index 6b7ee89a16..165ca15afe 100644 --- a/packager/app/test/testdata/bear-640x360-av-master-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-av-master-golden.m3u8 @@ -1,4 +1,5 @@ #EXTM3U +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="audio",NAME="stream_0",URI="audio.m3u8" #EXT-X-STREAM-INF:AUDIO="audio",CODECS="avc1.64001e,mp4a.40.2",BANDWIDTH=1217603 video.m3u8 diff --git a/packager/app/test/testdata/bear-640x360-v-enc-golden.m3u8 b/packager/app/test/testdata/bear-640x360-v-enc-golden.m3u8 index ca686acd5e..bc95bc1d1a 100644 --- a/packager/app/test/testdata/bear-640x360-v-enc-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-v-enc-golden.m3u8 @@ -1,5 +1,6 @@ #EXTM3U #EXT-X-VERSION:5 +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-TARGETDURATION:2 #EXT-X-PLAYLIST-TYPE:VOD #EXTINF:1.001, diff --git a/packager/app/test/testdata/bear-640x360-v-golden.m3u8 b/packager/app/test/testdata/bear-640x360-v-golden.m3u8 index 74edb5a1df..5389b34b76 100644 --- a/packager/app/test/testdata/bear-640x360-v-golden.m3u8 +++ b/packager/app/test/testdata/bear-640x360-v-golden.m3u8 @@ -1,5 +1,6 @@ #EXTM3U #EXT-X-VERSION:5 +## Generated with https://github.com/google/shaka-packager version -- #EXT-X-TARGETDURATION:2 #EXT-X-PLAYLIST-TYPE:VOD #EXTINF:1.001, diff --git a/packager/hls/base/master_playlist.cc b/packager/hls/base/master_playlist.cc index 07b1e26460..3e9c689a6b 100644 --- a/packager/hls/base/master_playlist.cc +++ b/packager/hls/base/master_playlist.cc @@ -18,6 +18,7 @@ #include "packager/hls/base/media_playlist.h" #include "packager/media/file/file.h" #include "packager/media/file/file_closer.h" +#include "packager/version/version.h" namespace shaka { namespace hls { @@ -147,7 +148,16 @@ bool MasterPlaylist::WriteMasterPlaylist(const std::string& base_url, } } - std::string content = "#EXTM3U\n" + audio_output + video_output; + const std::string version = GetPackagerVersion(); + std::string version_line; + if (!version.empty()) { + version_line = + base::StringPrintf("## Generated with %s version %s\n", + GetPackagerProjectUrl().c_str(), version.c_str()); + } + + std::string content = + "#EXTM3U\n" + version_line + audio_output + video_output; int64_t bytes_written = file->Write(content.data(), content.size()); if (bytes_written < 0) { LOG(ERROR) << "Error while writing master playlist " << file_path; diff --git a/packager/hls/base/master_playlist_unittest.cc b/packager/hls/base/master_playlist_unittest.cc index ceff1074ba..72835a8b8d 100644 --- a/packager/hls/base/master_playlist_unittest.cc +++ b/packager/hls/base/master_playlist_unittest.cc @@ -13,6 +13,7 @@ #include "packager/hls/base/media_playlist.h" #include "packager/hls/base/mock_media_playlist.h" #include "packager/media/file/file.h" +#include "packager/version/version.h" namespace shaka { namespace hls { @@ -33,6 +34,7 @@ class MasterPlaylistTest : public ::testing::Test { MasterPlaylistTest() : master_playlist_(kDefaultMasterPlaylistName) {} void SetUp() override { + SetPackagerVersionForTesting("test"); GetOutputDir(&test_output_dir_path_, &test_output_dir_); } @@ -86,6 +88,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistOneVideo) { const std::string expected = "#EXTM3U\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-STREAM-INF:CODECS=\"avc1\",BANDWIDTH=435889\n" "http://myplaylistdomain.com/media1.m3u8\n"; @@ -155,6 +159,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistVideoAndAudio) { const std::string expected = "#EXTM3U\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audiogroup\",NAME=\"english\"," "URI=\"http://playlists.org/eng.m3u8\"\n" "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audiogroup\",NAME=\"espanol\"," @@ -220,6 +226,8 @@ TEST_F(MasterPlaylistTest, WriteMasterPlaylistMultipleAudioGroups) { const std::string expected = "#EXTM3U\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio_hi\",NAME=\"english_hi\"," "URI=\"http://anydomain.com/eng_hi.m3u8\"\n" "#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID=\"audio_lo\",NAME=\"english_lo\"," diff --git a/packager/hls/base/media_playlist.cc b/packager/hls/base/media_playlist.cc index 7e2d55fc11..b2f92f6652 100644 --- a/packager/hls/base/media_playlist.cc +++ b/packager/hls/base/media_playlist.cc @@ -12,6 +12,7 @@ #include "packager/base/logging.h" #include "packager/base/strings/stringprintf.h" #include "packager/media/file/file.h" +#include "packager/version/version.h" namespace shaka { namespace hls { @@ -272,11 +273,21 @@ bool MediaPlaylist::WriteToFile(media::File* file) { SetTargetDuration(ceil(GetLongestSegmentDuration())); } + const std::string version = GetPackagerVersion(); + std::string version_line; + if (!version.empty()) { + version_line = + base::StringPrintf("## Generated with %s version %s\n", + GetPackagerProjectUrl().c_str(), version.c_str()); + } + // KEYFORMAT and KEYFORMATVERSIONS on EXT-X-KEY requires 5 or above. - std::string header = base::StringPrintf("#EXTM3U\n" - "#EXT-X-VERSION:5\n" - "#EXT-X-TARGETDURATION:%d\n", - target_duration_); + std::string header = base::StringPrintf( + "#EXTM3U\n" + "#EXT-X-VERSION:5\n" + "%s" + "#EXT-X-TARGETDURATION:%d\n", + version_line.c_str(), target_duration_); if (type_ == MediaPlaylistType::kVod) { header += "#EXT-X-PLAYLIST-TYPE:VOD\n"; } diff --git a/packager/hls/base/media_playlist_unittest.cc b/packager/hls/base/media_playlist_unittest.cc index 60efc0f612..4e884d0c5f 100644 --- a/packager/hls/base/media_playlist_unittest.cc +++ b/packager/hls/base/media_playlist_unittest.cc @@ -9,6 +9,7 @@ #include "packager/media/file/file.h" #include "packager/hls/base/media_playlist.h" +#include "packager/version/version.h" namespace shaka { namespace hls { @@ -56,6 +57,8 @@ class MediaPlaylistTest : public ::testing::Test { default_group_id_) {} void SetUp() override { + SetPackagerVersionForTesting("test"); + MediaInfo::VideoInfo* video_info = valid_video_media_info_.mutable_video_info(); video_info->set_codec("avc1"); @@ -117,6 +120,8 @@ TEST_F(MediaPlaylistTest, WriteToFile) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:0\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXT-X-ENDLIST\n"; @@ -172,6 +177,8 @@ TEST_F(MediaPlaylistTest, SetTargetDuration) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:20\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXT-X-ENDLIST\n"; @@ -198,6 +205,8 @@ TEST_F(MediaPlaylistTest, WriteToFileWithSegments) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:30\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXTINF:10.000,\n" @@ -227,6 +236,8 @@ TEST_F(MediaPlaylistTest, WriteToFileWithEncryptionInfo) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:30\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXT-X-KEY:METHOD=SAMPLE-AES," @@ -259,6 +270,8 @@ TEST_F(MediaPlaylistTest, WriteToFileWithEncryptionInfoEmptyIv) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:30\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXT-X-KEY:METHOD=SAMPLE-AES," @@ -290,6 +303,7 @@ TEST_F(MediaPlaylistTest, WriteToFileWithClearLead) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version test\n" "#EXT-X-TARGETDURATION:30\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXTINF:10.000,\n" @@ -323,6 +337,8 @@ TEST_F(MediaPlaylistTest, RemoveOldestSegment) { const std::string kExpectedOutput = "#EXTM3U\n" "#EXT-X-VERSION:5\n" + "## Generated with https://github.com/google/shaka-packager version " + "test\n" "#EXT-X-TARGETDURATION:30\n" "#EXT-X-PLAYLIST-TYPE:VOD\n" "#EXTINF:30.000,\n" diff --git a/packager/media/base/muxer_options.cc b/packager/media/base/muxer_options.cc index 363fd468ba..25ca44c2d2 100644 --- a/packager/media/base/muxer_options.cc +++ b/packager/media/base/muxer_options.cc @@ -5,7 +5,6 @@ // https://developers.google.com/open-source/licenses/bsd #include "packager/media/base/muxer_options.h" -#include "packager/version/version.h" namespace shaka { namespace media { @@ -17,8 +16,7 @@ MuxerOptions::MuxerOptions() segment_sap_aligned(false), fragment_sap_aligned(false), num_subsegments_per_sidx(0), - bandwidth(0), - packager_version_string(kPackagerVersion) {} + bandwidth(0) {} MuxerOptions::~MuxerOptions() {} } // namespace media diff --git a/packager/media/base/muxer_options.h b/packager/media/base/muxer_options.h index c82adc0ab1..87167dad19 100644 --- a/packager/media/base/muxer_options.h +++ b/packager/media/base/muxer_options.h @@ -66,9 +66,6 @@ struct MuxerOptions { /// User-specified bit rate for the media stream. If zero, the muxer will /// attempt to estimate. uint32_t bandwidth; - - /// Specify the version string to be embedded in the output files. - std::string packager_version_string; }; } // namespace media diff --git a/packager/media/formats/mp4/segmenter.cc b/packager/media/formats/mp4/segmenter.cc index a487be4919..b015021d5a 100644 --- a/packager/media/formats/mp4/segmenter.cc +++ b/packager/media/formats/mp4/segmenter.cc @@ -22,6 +22,7 @@ #include "packager/media/event/progress_listener.h" #include "packager/media/formats/mp4/box_definitions.h" #include "packager/media/formats/mp4/key_rotation_fragmenter.h" +#include "packager/version/version.h" namespace shaka { namespace media { @@ -276,11 +277,13 @@ Status Segmenter::Initialize(const std::vector& streams, moof_->header.sequence_number = 1; // Fill in version information. - moov_->metadata.handler.handler_type = FOURCC_ID32; - moov_->metadata.id3v2.language.code = "eng"; - moov_->metadata.id3v2.private_frame.owner = - "https://github.com/google/shaka-packager"; - moov_->metadata.id3v2.private_frame.value = options_.packager_version_string; + const std::string version = GetPackagerVersion(); + if (!version.empty()) { + moov_->metadata.handler.handler_type = FOURCC_ID32; + moov_->metadata.id3v2.language.code = "eng"; + moov_->metadata.id3v2.private_frame.owner = GetPackagerProjectUrl(); + moov_->metadata.id3v2.private_frame.value = version; + } return DoInitialize(); } diff --git a/packager/media/formats/webm/segmenter.cc b/packager/media/formats/webm/segmenter.cc index f40771953a..4b4ca15530 100644 --- a/packager/media/formats/webm/segmenter.cc +++ b/packager/media/formats/webm/segmenter.cc @@ -19,6 +19,7 @@ #include "packager/media/event/progress_listener.h" #include "packager/third_party/libwebm/src/mkvmuxerutil.hpp" #include "packager/third_party/libwebm/src/webmids.hpp" +#include "packager/version/version.h" namespace shaka { namespace media { @@ -60,13 +61,15 @@ Status Segmenter::Initialize(scoped_ptr writer, progress_target_ = info_->duration(); progress_listener_ = progress_listener; - const std::string version_string = - "https://github.com/google/shaka-packager version " + - options().packager_version_string; - segment_info_.Init(); segment_info_.set_timecode_scale(kTimecodeScale); - segment_info_.set_writing_app(version_string.c_str()); + + const std::string version = GetPackagerVersion(); + if (!version.empty()) { + segment_info_.set_writing_app( + (GetPackagerProjectUrl() + " version " + version).c_str()); + } + if (options().single_segment) { // Set an initial duration so the duration element is written; will be // overwritten at the end. This works because this is a float and floats diff --git a/packager/media/formats/webm/segmenter_test_base.cc b/packager/media/formats/webm/segmenter_test_base.cc index b79d435c65..4ef10dbc45 100644 --- a/packager/media/formats/webm/segmenter_test_base.cc +++ b/packager/media/formats/webm/segmenter_test_base.cc @@ -9,6 +9,7 @@ #include "packager/media/base/muxer_util.h" #include "packager/media/file/memory_file.h" #include "packager/media/formats/webm/webm_constants.h" +#include "packager/version/version.h" namespace shaka { namespace media { @@ -39,6 +40,8 @@ const uint8_t kNaluLengthSize = 0; SegmentTestBase::SegmentTestBase() {} void SegmentTestBase::SetUp() { + SetPackagerVersionForTesting("test"); + output_file_name_ = std::string(kMemoryFilePrefix) + "output-file.webm"; segment_template_ = std::string(kMemoryFilePrefix) + "output-template-$Number$.webm"; @@ -78,7 +81,6 @@ MuxerOptions SegmentTestBase::CreateMuxerOptions() const { ret.single_segment = single_segment_; ret.output_file_name = output_file_name_; ret.segment_template = segment_template_; - ret.packager_version_string = "test"; ret.segment_duration = 30; // seconds ret.fragment_duration = 30; // seconds ret.segment_sap_aligned = false; diff --git a/packager/mpd/base/mpd_builder.cc b/packager/mpd/base/mpd_builder.cc index e333ef00f1..8339084d0b 100644 --- a/packager/mpd/base/mpd_builder.cc +++ b/packager/mpd/base/mpd_builder.cc @@ -29,6 +29,7 @@ #include "packager/mpd/base/language_utils.h" #include "packager/mpd/base/mpd_utils.h" #include "packager/mpd/base/xml/xml_node.h" +#include "packager/version/version.h" namespace shaka { @@ -505,13 +506,18 @@ xmlDocPtr MpdBuilder::GenerateMpd() { } DCHECK(doc); - std::string version_string = - "Generated with https://github.com/google/shaka-packager version " + - mpd_options_.packager_version_string; - xml::scoped_xml_ptr comment( - xmlNewDocComment(doc.get(), BAD_CAST version_string.c_str())); - xmlDocSetRootElement(doc.get(), comment.get()); - xmlAddSibling(comment.release(), mpd.Release()); + const std::string version = GetPackagerVersion(); + if (!version.empty()) { + std::string version_string = + base::StringPrintf("Generated with %s version %s", + GetPackagerProjectUrl().c_str(), version.c_str()); + xml::scoped_xml_ptr comment( + xmlNewDocComment(doc.get(), BAD_CAST version_string.c_str())); + xmlDocSetRootElement(doc.get(), comment.get()); + xmlAddSibling(comment.release(), mpd.Release()); + } else { + xmlDocSetRootElement(doc.get(), mpd.Release()); + } return doc.release(); } diff --git a/packager/mpd/base/mpd_builder_unittest.cc b/packager/mpd/base/mpd_builder_unittest.cc index 0dea219809..e72e6ccb8d 100644 --- a/packager/mpd/base/mpd_builder_unittest.cc +++ b/packager/mpd/base/mpd_builder_unittest.cc @@ -20,6 +20,7 @@ #include "packager/mpd/base/mpd_utils.h" #include "packager/mpd/test/mpd_builder_test_helper.h" #include "packager/mpd/test/xml_compare.h" +#include "packager/version/version.h" namespace shaka { @@ -184,9 +185,8 @@ class DynamicMpdBuilderTest : public MpdBuilderTest { // Anchors availabilityStartTime so that the test result doesn't depend on the // current time. void SetUp() override { + SetPackagerVersionForTesting("--"); mpd_.availability_start_time_ = "2011-12-25T12:30:00"; - // Override packager version string for testing. - mpd_.mpd_options_.packager_version_string = "--"; InjectTestClock(); } diff --git a/packager/mpd/base/mpd_options.h b/packager/mpd/base/mpd_options.h index 0b5e1fa03e..28cfac91a4 100644 --- a/packager/mpd/base/mpd_options.h +++ b/packager/mpd/base/mpd_options.h @@ -9,8 +9,6 @@ #include -#include "packager/version/version.h" - namespace shaka { /// Defines Mpd Options. @@ -21,8 +19,7 @@ struct MpdOptions { // TODO(tinskip): Set min_buffer_time in unit tests rather than here. min_buffer_time(2.0), time_shift_buffer_depth(0), - suggested_presentation_delay(0), - packager_version_string(kPackagerVersion) {} + suggested_presentation_delay(0) {} ~MpdOptions() {}; @@ -31,7 +28,6 @@ struct MpdOptions { double min_buffer_time; double time_shift_buffer_depth; double suggested_presentation_delay; - std::string packager_version_string; }; } // namespace shaka diff --git a/packager/version/version.cc b/packager/version/version.cc index 9e700230dc..aa1e733a61 100644 --- a/packager/version/version.cc +++ b/packager/version/version.cc @@ -6,7 +6,9 @@ #include "packager/version/version.h" -namespace shaka { +#include "packager/base/lazy_instance.h" + +namespace { #if defined(PACKAGER_VERSION) // PACKAGER_VERSION is generated in gyp file using script @@ -20,4 +22,36 @@ const char kPackagerVersion[] = PACKAGER_VERSION "-debug"; const char kPackagerVersion[] = ""; #endif // #if defined(PACKAGER_VERSION) +const char kPackagerGithubUrl[] = "https://github.com/google/shaka-packager"; + +class Version { + public: + Version() : version_(kPackagerVersion) {} + ~Version() {} + + const std::string& version() { return version_; } + void set_version(const std::string& version) { version_ = version; } + + private: + std::string version_; +}; + +} // namespace + +namespace shaka { + +base::LazyInstance g_packager_version; + +std::string GetPackagerProjectUrl(){ + return kPackagerGithubUrl; +} + +std::string GetPackagerVersion() { + return g_packager_version.Get().version(); +} + +void SetPackagerVersionForTesting(const std::string& version) { + g_packager_version.Get().set_version(version); +} + } // namespace shaka diff --git a/packager/version/version.gyp b/packager/version/version.gyp index cedc703f32..37ab00d805 100644 --- a/packager/version/version.gyp +++ b/packager/version/version.gyp @@ -19,6 +19,9 @@ 'version.cc', 'version.h', ], + 'dependencies': [ + '../base/base.gyp:base', + ], }, ], } diff --git a/packager/version/version.h b/packager/version/version.h index 31d91dc55e..3159356ce9 100644 --- a/packager/version/version.h +++ b/packager/version/version.h @@ -4,8 +4,18 @@ // license that can be found in the LICENSE file or at // https://developers.google.com/open-source/licenses/bsd +#include + namespace shaka { -extern const char kPackagerVersion[]; +/// @return URL of shaka-packager project. +std::string GetPackagerProjectUrl(); + +/// @return The version string. +std::string GetPackagerVersion(); + +/// Set version for testing. +/// @param version contains the injected testing version. +void SetPackagerVersionForTesting(const std::string& version); } // namespace shaka