From 1d920a1a4e3651d705ce1f11d42ce58b8efe7e80 Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Thu, 22 May 2014 11:51:55 -0700 Subject: [PATCH] Media code change to support live mpd Change-Id: I24ca877ec62e69df5dbf3c9ede54da4df189dbc4 --- media/formats/mp4/mp4_muxer.cc | 1 + media/formats/mp4/multi_segment_segmenter.cc | 15 +++++++++++++++ media/formats/mp4/segmenter.cc | 5 ++++- media/formats/mp4/segmenter.h | 7 +++++++ 4 files changed, 27 insertions(+), 1 deletion(-) diff --git a/media/formats/mp4/mp4_muxer.cc b/media/formats/mp4/mp4_muxer.cc index b0bd46f199..8c6cf4f754 100644 --- a/media/formats/mp4/mp4_muxer.cc +++ b/media/formats/mp4/mp4_muxer.cc @@ -100,6 +100,7 @@ Status MP4Muxer::Initialize() { Status segmenter_initialized = segmenter_->Initialize(streams(), + muxer_listener(), encryption_key_source(), max_sd_pixels(), clear_lead_in_seconds(), diff --git a/media/formats/mp4/multi_segment_segmenter.cc b/media/formats/mp4/multi_segment_segmenter.cc index 7547fa5a02..9ea9fa44d1 100644 --- a/media/formats/mp4/multi_segment_segmenter.cc +++ b/media/formats/mp4/multi_segment_segmenter.cc @@ -11,6 +11,7 @@ #include "media/base/buffer_writer.h" #include "media/base/media_stream.h" #include "media/base/muxer_options.h" +#include "media/event/muxer_listener.h" #include "media/file/file.h" #include "media/formats/mp4/box_definitions.h" @@ -159,12 +160,26 @@ Status MultiSegmentSegmenter::WriteSegment() { if (options().num_subsegments_per_sidx >= 0) sidx()->Write(buffer.get()); + const size_t segment_size = buffer->Size() + fragment_buffer()->Size(); + DCHECK_NE(segment_size, 0u); + Status status = buffer->WriteToFile(file); if (status.ok()) status = fragment_buffer()->WriteToFile(file); if (!file->Close()) LOG(WARNING) << "Failed to close the file properly: " << file_name; + + if (status.ok() && muxer_listener()) { + uint64 segment_duration = 0; + // ISO/IEC 23009-1:2012: the value shall be identical to sum of the the + // values of all Subsegment_duration fields in the first ‘sidx’ box. + for (size_t i = 0; i < sidx()->references.size(); ++i) + segment_duration += sidx()->references[i].subsegment_duration; + muxer_listener()->OnNewSegment( + sidx()->earliest_presentation_time, segment_duration, segment_size); + } + return status; } diff --git a/media/formats/mp4/segmenter.cc b/media/formats/mp4/segmenter.cc index ac4b0eb02f..067957a46b 100644 --- a/media/formats/mp4/segmenter.cc +++ b/media/formats/mp4/segmenter.cc @@ -119,16 +119,19 @@ Segmenter::Segmenter(const MuxerOptions& options, fragment_buffer_(new BufferWriter()), sidx_(new SegmentIndex()), segment_initialized_(false), - end_of_segment_(false) {} + end_of_segment_(false), + muxer_listener_(NULL) {} Segmenter::~Segmenter() { STLDeleteElements(&fragmenters_); } Status Segmenter::Initialize(const std::vector& streams, + event::MuxerListener* muxer_listener, EncryptionKeySource* encryption_key_source, uint32 max_sd_pixels, double clear_lead_in_seconds, double crypto_period_duration_in_seconds) { DCHECK_LT(0u, streams.size()); + muxer_listener_ = muxer_listener; moof_->header.sequence_number = 0; moof_->tracks.resize(streams.size()); diff --git a/media/formats/mp4/segmenter.h b/media/formats/mp4/segmenter.h index 4fc23d3fcb..2bf48e53ef 100644 --- a/media/formats/mp4/segmenter.h +++ b/media/formats/mp4/segmenter.h @@ -23,6 +23,10 @@ class EncryptionKeySource; class MediaSample; class MediaStream; +namespace event { +class MuxerListener; +} // namespace event + namespace mp4 { class Fragmenter; @@ -59,6 +63,7 @@ class Segmenter { /// @param crypto_period_duration specifies crypto period duration in seconds. /// @return OK on success, an error status otherwise. Status Initialize(const std::vector& streams, + event::MuxerListener* muxer_listener, EncryptionKeySource* encryption_key_source, uint32 max_sd_pixels, double clear_lead_in_seconds, @@ -95,6 +100,7 @@ class Segmenter { Movie* moov() { return moov_.get(); } BufferWriter* fragment_buffer() { return fragment_buffer_.get(); } SegmentIndex* sidx() { return sidx_.get(); } + event::MuxerListener* muxer_listener() { return muxer_listener_; } private: virtual Status DoInitialize() = 0; @@ -119,6 +125,7 @@ class Segmenter { std::map stream_map_; bool segment_initialized_; bool end_of_segment_; + event::MuxerListener* muxer_listener_; DISALLOW_COPY_AND_ASSIGN(Segmenter); };