From 9eaf1dcae0d90c763a033b5823d98da25f8fa921 Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Thu, 27 Jul 2017 10:23:27 -0700 Subject: [PATCH] Consolidate ChunkingParams and ChunkingOptions Change-Id: Ibbb85981d2b424432b61ca693e28334ef00d475e --- packager/app/packager_util.cc | 12 ------- packager/app/packager_util.h | 3 -- packager/media/chunking/chunking_handler.cc | 14 ++++---- packager/media/chunking/chunking_handler.h | 25 +++----------- .../chunking/chunking_handler_unittest.cc | 34 +++++++++---------- packager/media/public/chunking_params.h | 32 +++++++++++++++++ packager/packager.cc | 14 ++++---- packager/packager.h | 19 +---------- 8 files changed, 67 insertions(+), 86 deletions(-) create mode 100644 packager/media/public/chunking_params.h diff --git a/packager/app/packager_util.cc b/packager/app/packager_util.cc index e3c6956ec0..e3fa9cebca 100644 --- a/packager/app/packager_util.cc +++ b/packager/app/packager_util.cc @@ -171,18 +171,6 @@ std::unique_ptr CreateDecryptionKeySource( return decryption_key_source; } -ChunkingOptions GetChunkingOptions(const ChunkingParams& chunking_params) { - ChunkingOptions chunking_options; - chunking_options.segment_duration_in_seconds = - chunking_params.segment_duration_in_seconds; - chunking_options.subsegment_duration_in_seconds = - chunking_params.subsegment_duration_in_seconds; - chunking_options.segment_sap_aligned = chunking_params.segment_sap_aligned; - chunking_options.subsegment_sap_aligned = - chunking_params.subsegment_sap_aligned; - return chunking_options; -} - MuxerOptions GetMuxerOptions(const std::string& temp_dir, const Mp4OutputParams& mp4_params) { MuxerOptions muxer_options; diff --git a/packager/app/packager_util.h b/packager/app/packager_util.h index b8e495d960..e169cb8f83 100644 --- a/packager/app/packager_util.h +++ b/packager/app/packager_util.h @@ -50,9 +50,6 @@ std::unique_ptr CreateEncryptionKeySource( std::unique_ptr CreateDecryptionKeySource( const DecryptionParams& decryption_params); -/// @return ChunkingOptions from provided command line options. -ChunkingOptions GetChunkingOptions(const ChunkingParams& chunking_params); - /// @return MuxerOptions from provided command line options. MuxerOptions GetMuxerOptions(const std::string& temp_dir, const Mp4OutputParams& mp4_params); diff --git a/packager/media/chunking/chunking_handler.cc b/packager/media/chunking/chunking_handler.cc index 34b682fe6d..ccf86f5241 100644 --- a/packager/media/chunking/chunking_handler.cc +++ b/packager/media/chunking/chunking_handler.cc @@ -18,9 +18,9 @@ int64_t kTimeStampToDispatchAllSamples = -1; namespace shaka { namespace media { -ChunkingHandler::ChunkingHandler(const ChunkingOptions& chunking_options) - : chunking_options_(chunking_options), thread_id_(kThreadIdUnset) { - CHECK_NE(chunking_options.segment_duration_in_seconds, 0u); +ChunkingHandler::ChunkingHandler(const ChunkingParams& chunking_params) + : chunking_params_(chunking_params), thread_id_(kThreadIdUnset) { + CHECK_NE(chunking_params.segment_duration_in_seconds, 0u); } ChunkingHandler::~ChunkingHandler() {} @@ -56,9 +56,9 @@ Status ChunkingHandler::Process(std::unique_ptr stream_data) { if (is_main_stream) { main_stream_index_ = stream_data->stream_index; segment_duration_ = - chunking_options_.segment_duration_in_seconds * time_scale; + chunking_params_.segment_duration_in_seconds * time_scale; subsegment_duration_ = - chunking_options_.subsegment_duration_in_seconds * time_scale; + chunking_params_.subsegment_duration_in_seconds * time_scale; } else if (stream_data->stream_info->stream_type() == kStreamVideo) { return Status(error::CHUNKING_ERROR, "Only one video stream is allowed per chunking handler."); @@ -138,7 +138,7 @@ Status ChunkingHandler::ProcessMediaSample(const MediaSample* sample) { // Check if we need to terminate the current (sub)segment. bool new_segment = false; bool new_subsegment = false; - if (is_key_frame || !chunking_options_.segment_sap_aligned) { + if (is_key_frame || !chunking_params_.segment_sap_aligned) { const int64_t segment_index = timestamp / segment_duration_; if (segment_index != current_segment_index_) { current_segment_index_ = segment_index; @@ -148,7 +148,7 @@ Status ChunkingHandler::ProcessMediaSample(const MediaSample* sample) { } } if (!new_segment && subsegment_duration_ > 0 && - (is_key_frame || !chunking_options_.subsegment_sap_aligned)) { + (is_key_frame || !chunking_params_.subsegment_sap_aligned)) { const int64_t subsegment_index = (timestamp - segment_info_[main_stream_index_]->start_timestamp) / subsegment_duration_; diff --git a/packager/media/chunking/chunking_handler.h b/packager/media/chunking/chunking_handler.h index e392ac0361..9910d01f6e 100644 --- a/packager/media/chunking/chunking_handler.h +++ b/packager/media/chunking/chunking_handler.h @@ -10,30 +10,13 @@ #include #include "packager/media/base/media_handler.h" +#include "packager/media/public/chunking_params.h" namespace shaka { namespace media { -struct ChunkingOptions { - /// Segment duration in seconds. - double segment_duration_in_seconds = 0; - - /// Subsegment duration in seconds. Should not be larger than the segment - /// duration. - double subsegment_duration_in_seconds = 0; - - /// Force segments to begin with stream access points. Actual segment duration - /// may not be exactly what is specified by segment_duration. - bool segment_sap_aligned = true; - - /// Force subsegments to begin with stream access points. Actual subsegment - /// duration may not be exactly what is specified by subsegment_duration. - /// Setting to true implies that segment_sap_aligned is true as well. - bool subsegment_sap_aligned = true; -}; - /// ChunkingHandler splits the samples into segments / subsegments based on the -/// specified chunking options. +/// specified chunking params. /// This handler is a multi-in multi-out handler. If more than one input is /// provided, there should be one and only one video stream; also, all inputs /// should come from the same thread and are synchronized. @@ -60,7 +43,7 @@ struct ChunkingOptions { /// are aligned. class ChunkingHandler : public MediaHandler { public: - explicit ChunkingHandler(const ChunkingOptions& chunking_options); + explicit ChunkingHandler(const ChunkingParams& chunking_params); ~ChunkingHandler() override; protected: @@ -87,7 +70,7 @@ class ChunkingHandler : public MediaHandler { Status DispatchSegmentInfoForAllStreams(); Status DispatchSubsegmentInfoForAllStreams(); - const ChunkingOptions chunking_options_; + const ChunkingParams chunking_params_; // The inputs are expected to come from the same thread. std::atomic thread_id_; diff --git a/packager/media/chunking/chunking_handler_unittest.cc b/packager/media/chunking/chunking_handler_unittest.cc index 5e69169bd2..3e7e086eee 100644 --- a/packager/media/chunking/chunking_handler_unittest.cc +++ b/packager/media/chunking/chunking_handler_unittest.cc @@ -33,8 +33,8 @@ const bool kEncrypted = true; class ChunkingHandlerTest : public MediaHandlerTestBase { public: void SetUpChunkingHandler(int num_inputs, - const ChunkingOptions& chunking_options) { - chunking_handler_.reset(new ChunkingHandler(chunking_options)); + const ChunkingParams& chunking_params) { + chunking_handler_.reset(new ChunkingHandler(chunking_params)); SetUpGraph(num_inputs, num_inputs, chunking_handler_); ASSERT_OK(chunking_handler_->Initialize()); } @@ -52,9 +52,9 @@ class ChunkingHandlerTest : public MediaHandlerTestBase { }; TEST_F(ChunkingHandlerTest, AudioNoSubsegmentsThenFlush) { - ChunkingOptions chunking_options; - chunking_options.segment_duration_in_seconds = 1; - SetUpChunkingHandler(1, chunking_options); + ChunkingParams chunking_params; + chunking_params.segment_duration_in_seconds = 1; + SetUpChunkingHandler(1, chunking_params); ASSERT_OK(Process(GetAudioStreamInfoStreamData(kStreamIndex0, kTimeScale0))); EXPECT_THAT( @@ -88,10 +88,10 @@ TEST_F(ChunkingHandlerTest, AudioNoSubsegmentsThenFlush) { } TEST_F(ChunkingHandlerTest, AudioWithSubsegments) { - ChunkingOptions chunking_options; - chunking_options.segment_duration_in_seconds = 1; - chunking_options.subsegment_duration_in_seconds = 0.5; - SetUpChunkingHandler(1, chunking_options); + ChunkingParams chunking_params; + chunking_params.segment_duration_in_seconds = 1; + chunking_params.subsegment_duration_in_seconds = 0.5; + SetUpChunkingHandler(1, chunking_params); ASSERT_OK(Process(GetAudioStreamInfoStreamData(kStreamIndex0, kTimeScale0))); for (int i = 0; i < 5; ++i) { @@ -115,10 +115,10 @@ TEST_F(ChunkingHandlerTest, AudioWithSubsegments) { } TEST_F(ChunkingHandlerTest, VideoAndSubsegmentAndNonzeroStart) { - ChunkingOptions chunking_options; - chunking_options.segment_duration_in_seconds = 1; - chunking_options.subsegment_duration_in_seconds = 0.3; - SetUpChunkingHandler(1, chunking_options); + ChunkingParams chunking_params; + chunking_params.segment_duration_in_seconds = 1; + chunking_params.subsegment_duration_in_seconds = 0.3; + SetUpChunkingHandler(1, chunking_params); ASSERT_OK(Process(GetVideoStreamInfoStreamData(kStreamIndex0, kTimeScale1))); const int64_t kVideoStartTimestamp = 12345; @@ -154,10 +154,10 @@ TEST_F(ChunkingHandlerTest, VideoAndSubsegmentAndNonzeroStart) { } TEST_F(ChunkingHandlerTest, AudioAndVideo) { - ChunkingOptions chunking_options; - chunking_options.segment_duration_in_seconds = 1; - chunking_options.subsegment_duration_in_seconds = 0.3; - SetUpChunkingHandler(2, chunking_options); + ChunkingParams chunking_params; + chunking_params.segment_duration_in_seconds = 1; + chunking_params.subsegment_duration_in_seconds = 0.3; + SetUpChunkingHandler(2, chunking_params); ASSERT_OK(Process(GetAudioStreamInfoStreamData(kStreamIndex0, kTimeScale0))); ASSERT_OK(Process(GetVideoStreamInfoStreamData(kStreamIndex1, kTimeScale1))); diff --git a/packager/media/public/chunking_params.h b/packager/media/public/chunking_params.h new file mode 100644 index 0000000000..c22e1b9230 --- /dev/null +++ b/packager/media/public/chunking_params.h @@ -0,0 +1,32 @@ +// Copyright 2017 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 + +#ifndef PACKAGER_MEDIA_PUBLIC_CHUNKING_PARAMS_H_ +#define PACKAGER_MEDIA_PUBLIC_CHUNKING_PARAMS_H_ + +namespace shaka { + +/// Chunking (segmentation) related parameters. +struct ChunkingParams { + /// Segment duration in seconds. + double segment_duration_in_seconds = 0; + /// Subsegment duration in seconds. Should not be larger than the segment + /// duration. + double subsegment_duration_in_seconds = 0; + + /// Force segments to begin with stream access points. Actual segment duration + /// may not be exactly what is specified by segment_duration. + bool segment_sap_aligned = true; + /// Force subsegments to begin with stream access points. Actual subsegment + /// duration may not be exactly what is specified by subsegment_duration. + /// Setting to subsegment_sap_aligned to true but segment_sap_aligned to false + /// is not allowed. + bool subsegment_sap_aligned = true; +}; + +} // namespace shaka + +#endif // PACKAGER_MEDIA_PUBLIC_CHUNKING_PARAMS_H_ diff --git a/packager/packager.cc b/packager/packager.cc index c674704b01..c79fe36ed7 100644 --- a/packager/packager.cc +++ b/packager/packager.cc @@ -315,7 +315,6 @@ std::shared_ptr CreateOutputMuxer(const MuxerOptions& options, bool CreateRemuxJobs(const StreamDescriptorList& stream_descriptors, const PackagingParams& packaging_params, - const ChunkingOptions& chunking_options, const MuxerOptions& muxer_options, FakeClock* fake_clock, KeySource* encryption_key_source, @@ -466,7 +465,8 @@ bool CreateRemuxJobs(const StreamDescriptorList& stream_descriptors, std::vector> handlers; - auto chunking_handler = std::make_shared(chunking_options); + auto chunking_handler = + std::make_shared(packaging_params.chunking_params); handlers.push_back(chunking_handler); Status status; @@ -583,8 +583,6 @@ Status Packager::Initialize( std::unique_ptr internal(new PackagerInternal); - ChunkingOptions chunking_options = - media::GetChunkingOptions(packaging_params.chunking_params); MuxerOptions muxer_options = media::GetMuxerOptions( packaging_params.temp_dir, packaging_params.mp4_output_params); @@ -636,10 +634,10 @@ Status Packager::Initialize( for (const StreamDescriptor& descriptor : stream_descriptors) stream_descriptor_list.insert(descriptor); if (!media::CreateRemuxJobs( - stream_descriptor_list, packaging_params, chunking_options, - muxer_options, &internal->fake_clock, - internal->encryption_key_source.get(), internal->mpd_notifier.get(), - internal->hls_notifier.get(), &internal->jobs)) { + stream_descriptor_list, packaging_params, muxer_options, + &internal->fake_clock, internal->encryption_key_source.get(), + internal->mpd_notifier.get(), internal->hls_notifier.get(), + &internal->jobs)) { return Status(error::INVALID_ARGUMENT, "Failed to create remux jobs."); } internal_ = std::move(internal); diff --git a/packager/packager.h b/packager/packager.h index 974d10e329..1f84a44396 100644 --- a/packager/packager.h +++ b/packager/packager.h @@ -12,6 +12,7 @@ #include #include "packager/hls/public/hls_playlist_type.h" +#include "packager/media/public/chunking_params.h" #include "packager/media/public/crypto_params.h" #include "packager/status.h" @@ -39,24 +40,6 @@ struct Mp4OutputParams { bool use_decoding_timestamp_in_timeline = false; }; -/// Chunking (segmentation) related parameters. -struct ChunkingParams { - /// Segment duration in seconds. - double segment_duration_in_seconds = 0; - /// Subsegment duration in seconds. Should not be larger than the segment - /// duration. - double subsegment_duration_in_seconds = 0; - - /// Force segments to begin with stream access points. Actual segment duration - /// may not be exactly what is specified by segment_duration. - bool segment_sap_aligned = true; - /// Force subsegments to begin with stream access points. Actual subsegment - /// duration may not be exactly what is specified by subsegment_duration. - /// Setting to subsegment_sap_aligned to true but segment_sap_aligned to false - /// is not allowed. - bool subsegment_sap_aligned = true; -}; - /// DASH MPD related parameters. struct MpdParams { /// MPD output file path.