2022-08-26 15:44:59 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// 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
|
|
|
|
|
2018-03-24 01:08:00 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
2023-10-09 23:21:41 +00:00
|
|
|
#include <absl/flags/parse.h>
|
|
|
|
#include <absl/flags/usage.h>
|
|
|
|
#include <absl/flags/usage_config.h>
|
2023-09-09 00:44:17 +00:00
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
#include <absl/strings/str_split.h>
|
|
|
|
#include <glog/logging.h>
|
2023-10-10 23:51:11 +00:00
|
|
|
#include <packager/app/mpd_generator_flags.h>
|
|
|
|
#include <packager/app/vlog_flags.h>
|
|
|
|
#include <packager/mpd/util/mpd_writer.h>
|
|
|
|
#include <packager/tools/license_notice.h>
|
|
|
|
#include <packager/version/version.h>
|
2014-01-29 21:40:13 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <codecvt>
|
|
|
|
#include <functional>
|
|
|
|
#endif // defined(OS_WIN)
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
ABSL_FLAG(bool, licenses, false, "Dump licenses.");
|
|
|
|
ABSL_FLAG(std::string,
|
|
|
|
test_packager_version,
|
|
|
|
"",
|
|
|
|
"Packager version for testing. Should be used for testing only.");
|
2018-03-24 01:08:00 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2014-01-29 21:40:13 +00:00
|
|
|
namespace {
|
|
|
|
const char kUsage[] =
|
|
|
|
"MPD generation driver program.\n"
|
|
|
|
"This program accepts MediaInfo files in human readable text "
|
|
|
|
"format and outputs an MPD.\n"
|
|
|
|
"The main use case for this is to output MPD for VOD.\n"
|
|
|
|
"Limitations:\n"
|
|
|
|
" Each MediaInfo can only have one of VideoInfo, AudioInfo, or TextInfo.\n"
|
|
|
|
" There will be at most 3 AdaptationSets in the MPD, i.e. 1 video, 1 "
|
|
|
|
"audio, and 1 text.\n"
|
|
|
|
"Sample Usage:\n"
|
2014-02-13 00:35:05 +00:00
|
|
|
"%s --input=\"video1.media_info,video2.media_info,audio1.media_info\" "
|
2014-01-29 21:40:13 +00:00
|
|
|
"--output=\"video_audio.mpd\"";
|
|
|
|
|
|
|
|
enum ExitStatus {
|
|
|
|
kSuccess = 0,
|
|
|
|
kEmptyInputError,
|
|
|
|
kEmptyOutputError,
|
|
|
|
kFailedToWriteMpdToFileError
|
|
|
|
};
|
|
|
|
|
|
|
|
ExitStatus CheckRequiredFlags() {
|
2023-09-09 00:44:17 +00:00
|
|
|
if (absl::GetFlag(FLAGS_input).empty()) {
|
2014-01-29 21:40:13 +00:00
|
|
|
LOG(ERROR) << "--input is required.";
|
|
|
|
return kEmptyInputError;
|
|
|
|
}
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
if (absl::GetFlag(FLAGS_output).empty()) {
|
2014-01-29 21:40:13 +00:00
|
|
|
LOG(ERROR) << "--output is required.";
|
|
|
|
return kEmptyOutputError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kSuccess;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExitStatus RunMpdGenerator() {
|
|
|
|
DCHECK_EQ(CheckRequiredFlags(), kSuccess);
|
|
|
|
std::vector<std::string> base_urls;
|
|
|
|
typedef std::vector<std::string>::const_iterator Iterator;
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
std::vector<std::string> input_files =
|
|
|
|
absl::StrSplit(absl::GetFlag(FLAGS_input), ",", absl::AllowEmpty());
|
2014-01-29 21:40:13 +00:00
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
if (!absl::GetFlag(FLAGS_base_urls).empty()) {
|
|
|
|
base_urls =
|
|
|
|
absl::StrSplit(absl::GetFlag(FLAGS_base_urls), ",", absl::AllowEmpty());
|
2014-01-29 21:40:13 +00:00
|
|
|
}
|
|
|
|
|
2016-05-19 21:38:15 +00:00
|
|
|
MpdWriter mpd_writer;
|
2014-01-29 21:40:13 +00:00
|
|
|
for (Iterator it = base_urls.begin(); it != base_urls.end(); ++it)
|
|
|
|
mpd_writer.AddBaseUrl(*it);
|
|
|
|
|
2018-04-17 17:42:45 +00:00
|
|
|
for (const std::string& file : input_files) {
|
|
|
|
if (!mpd_writer.AddFile(file)) {
|
|
|
|
LOG(WARNING) << "MpdWriter failed to read " << file << ", skipping.";
|
2014-01-29 21:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
if (!mpd_writer.WriteMpdToFile(absl::GetFlag(FLAGS_output).c_str())) {
|
|
|
|
LOG(ERROR) << "Failed to write MPD to " << absl::GetFlag(FLAGS_output);
|
2014-01-29 21:40:13 +00:00
|
|
|
return kFailedToWriteMpdToFileError;
|
|
|
|
}
|
|
|
|
|
|
|
|
return kSuccess;
|
|
|
|
}
|
|
|
|
|
2014-10-03 05:17:54 +00:00
|
|
|
int MpdMain(int argc, char** argv) {
|
2023-09-09 00:44:17 +00:00
|
|
|
absl::FlagsUsageConfig flag_config;
|
|
|
|
flag_config.version_string = []() -> std::string {
|
|
|
|
return "mpd_generator version " + GetPackagerVersion() + "\n";
|
|
|
|
};
|
|
|
|
flag_config.contains_help_flags =
|
|
|
|
[](absl::string_view flag_file_name) -> bool { return true; };
|
|
|
|
absl::SetFlagsUsageConfig(flag_config);
|
|
|
|
|
|
|
|
auto usage = absl::StrFormat(kUsage, argv[0]);
|
|
|
|
absl::SetProgramUsageMessage(usage);
|
|
|
|
absl::ParseCommandLine(argc, argv);
|
|
|
|
|
|
|
|
if (absl::GetFlag(FLAGS_licenses)) {
|
2018-03-24 01:08:00 +00:00
|
|
|
for (const char* line : kLicenseNotice)
|
|
|
|
std::cout << line << std::endl;
|
|
|
|
return kSuccess;
|
|
|
|
}
|
2014-01-29 21:40:13 +00:00
|
|
|
|
|
|
|
ExitStatus status = CheckRequiredFlags();
|
|
|
|
if (status != kSuccess) {
|
2023-09-09 00:44:17 +00:00
|
|
|
std::cerr << "Usage " << absl::ProgramUsageMessage();
|
2014-01-29 21:40:13 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
2023-10-09 18:08:08 +00:00
|
|
|
register_flags_with_glog();
|
|
|
|
|
2023-09-09 00:44:17 +00:00
|
|
|
if (!absl::GetFlag(FLAGS_test_packager_version).empty())
|
|
|
|
SetPackagerVersionForTesting(absl::GetFlag(FLAGS_test_packager_version));
|
2018-05-30 20:16:30 +00:00
|
|
|
|
2014-01-29 21:40:13 +00:00
|
|
|
return RunMpdGenerator();
|
|
|
|
}
|
2014-10-03 05:17:54 +00:00
|
|
|
|
|
|
|
} // namespace
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2014-10-03 05:17:54 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
// Windows wmain, which converts wide character arguments to UTF-8.
|
|
|
|
int wmain(int argc, wchar_t* argv[], wchar_t* envp[]) {
|
|
|
|
std::unique_ptr<char* [], std::function<void(char**)>> utf8_argv(
|
|
|
|
new char*[argc], [argc](char** utf8_args) {
|
|
|
|
// TODO(tinskip): This leaks, but if this code is enabled, it crashes.
|
|
|
|
// Figure out why. I suspect gflags does something funny with the
|
|
|
|
// argument array.
|
|
|
|
// for (int idx = 0; idx < argc; ++idx)
|
|
|
|
// delete[] utf8_args[idx];
|
|
|
|
delete[] utf8_args;
|
|
|
|
});
|
|
|
|
std::wstring_convert<std::codecvt_utf8<wchar_t>> converter;
|
2023-07-18 18:59:21 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
for (int idx = 0; idx < argc; ++idx) {
|
|
|
|
std::string utf8_arg(converter.to_bytes(argv[idx]));
|
|
|
|
utf8_arg += '\0';
|
|
|
|
utf8_argv[idx] = new char[utf8_arg.size()];
|
|
|
|
memcpy(utf8_argv[idx], &utf8_arg[0], utf8_arg.size());
|
|
|
|
}
|
2023-07-18 18:59:21 +00:00
|
|
|
|
|
|
|
// Because we just converted wide character args into UTF8, and because
|
|
|
|
// std::filesystem::u8path is used to interpret all std::string paths as
|
|
|
|
// UTF8, we should set the locale to UTF8 as well, for the transition point
|
|
|
|
// to C library functions like fopen to work correctly with non-ASCII paths.
|
|
|
|
std::setlocale(LC_ALL, ".UTF8");
|
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
return shaka::MpdMain(argc, utf8_argv.get());
|
|
|
|
}
|
|
|
|
#else
|
2014-10-03 05:17:54 +00:00
|
|
|
int main(int argc, char** argv) {
|
2016-05-20 21:19:33 +00:00
|
|
|
return shaka::MpdMain(argc, argv);
|
2014-10-03 05:17:54 +00:00
|
|
|
}
|
2016-08-14 22:28:21 +00:00
|
|
|
#endif // !defined(OS_WIN)
|