diff --git a/packager/app/test/packager_test.py b/packager/app/test/packager_test.py index 75a91ed3af..e3159dcbbe 100755 --- a/packager/app/test/packager_test.py +++ b/packager/app/test/packager_test.py @@ -1618,6 +1618,18 @@ class PackagerCommandParsingTest(PackagerAppTest): # Expect the test to fail but we do not expect a crash. self.assertEqual(packaging_result, 1) + def testInconsistentOutputAndSegmentTemplateFormat(self): + test_file = os.path.join(self.test_data_dir, 'bear-640x360.mp4') + video_output_prefix = os.path.join(self.tmp_dir, 'video') + + packaging_result = self.packager.Package([ + 'input=%s,stream=video,init_segment=%s-init.mp4,' + 'segment_template=%s-$Number$.webm' % + (test_file, video_output_prefix, video_output_prefix), + ], self._GetFlags()) + # Expect the test to fail but we do not expect a crash. + self.assertEqual(packaging_result, 1) + if __name__ == '__main__': unittest.main() diff --git a/packager/packager.cc b/packager/packager.cc index b8609a0e58..2a34f5c13f 100644 --- a/packager/packager.cc +++ b/packager/packager.cc @@ -16,6 +16,7 @@ #include "packager/base/at_exit.h" #include "packager/base/files/file_path.h" #include "packager/base/logging.h" +#include "packager/base/optional.h" #include "packager/base/path_service.h" #include "packager/base/strings/string_util.h" #include "packager/base/strings/stringprintf.h" @@ -142,26 +143,48 @@ bool DetermineTextFileCodec(const std::string& file, std::string* out) { } MediaContainerName GetOutputFormat(const StreamDescriptor& descriptor) { - MediaContainerName output_format = CONTAINER_UNKNOWN; if (!descriptor.output_format.empty()) { - output_format = DetermineContainerFromFormatName(descriptor.output_format); - if (output_format == CONTAINER_UNKNOWN) { + MediaContainerName format = + DetermineContainerFromFormatName(descriptor.output_format); + if (format == CONTAINER_UNKNOWN) { LOG(ERROR) << "Unable to determine output format from '" << descriptor.output_format << "'."; } - } else { - const std::string& output_name = descriptor.output.empty() - ? descriptor.segment_template - : descriptor.output; - if (output_name.empty()) - return CONTAINER_UNKNOWN; - output_format = DetermineContainerFromFileName(output_name); - if (output_format == CONTAINER_UNKNOWN) { - LOG(ERROR) << "Unable to determine output format from '" << output_name - << "'."; + return format; + } + + base::Optional format_from_output; + base::Optional format_from_segment; + if (!descriptor.output.empty()) { + format_from_output = DetermineContainerFromFileName(descriptor.output); + if (format_from_output.value() == CONTAINER_UNKNOWN) { + LOG(ERROR) << "Unable to determine output format from '" + << descriptor.output << "'."; } } - return output_format; + if (!descriptor.segment_template.empty()) { + format_from_segment = + DetermineContainerFromFileName(descriptor.segment_template); + if (format_from_segment.value() == CONTAINER_UNKNOWN) { + LOG(ERROR) << "Unable to determine output format from '" + << descriptor.segment_template << "'."; + } + } + + if (format_from_output && format_from_segment) { + if (format_from_output.value() != format_from_segment.value()) { + LOG(ERROR) << "Output format determined from '" << descriptor.output + << "' differs from output format determined from '" + << descriptor.segment_template << "'."; + return CONTAINER_UNKNOWN; + } + } + + if (format_from_output) + return format_from_output.value(); + if (format_from_segment) + return format_from_segment.value(); + return CONTAINER_UNKNOWN; } Status ValidateStreamDescriptor(bool dump_stream_info,