diff --git a/packager/media/base/demuxer.cc b/packager/media/base/demuxer.cc index 85d60a4856..e9ce0ce3de 100644 --- a/packager/media/base/demuxer.cc +++ b/packager/media/base/demuxer.cc @@ -33,7 +33,8 @@ Demuxer::Demuxer(const std::string& file_name) : file_name_(file_name), media_file_(NULL), init_event_received_(false), - buffer_(new uint8_t[kBufSize]) { + buffer_(new uint8_t[kBufSize]), + cancelled_(false) { } Demuxer::~Demuxer() { @@ -127,9 +128,12 @@ Status Demuxer::Run() { return status; } - while ((status = Parse()).ok()) + while (!cancelled_ && (status = Parse()).ok()) continue; + if (cancelled_ && status.ok()) + return Status(error::CANCELLED, "Demuxer run cancelled"); + if (status.error_code() == error::END_OF_STREAM) { // Push EOS sample to muxer to indicate end of stream. const scoped_refptr& sample = MediaSample::CreateEOSBuffer(); @@ -169,5 +173,9 @@ Status Demuxer::Parse() { "Cannot parse media file " + file_name_); } +void Demuxer::Cancel() { + cancelled_ = true; +} + } // namespace media } // namespace edash_packager diff --git a/packager/media/base/demuxer.h b/packager/media/base/demuxer.h index 4315d5c26e..0a51e7e831 100644 --- a/packager/media/base/demuxer.h +++ b/packager/media/base/demuxer.h @@ -55,6 +55,10 @@ class Demuxer { /// Read from the source and send it to the parser. Status Parse(); + /// Cancel a demuxing job in progress. Will cause @a Run to exit with an error + /// status of type CANCELLED. + void Cancel(); + /// @return Streams in the media container being demuxed. The caller cannot /// add or remove streams from the returned vector, but the caller is /// allowed to change the internal state of the streams in the vector @@ -75,6 +79,7 @@ class Demuxer { std::vector streams_; scoped_ptr buffer_; scoped_ptr key_source_; + bool cancelled_; DISALLOW_COPY_AND_ASSIGN(Demuxer); }; diff --git a/packager/media/base/muxer.cc b/packager/media/base/muxer.cc index e975c7c05c..588d34024d 100644 --- a/packager/media/base/muxer.cc +++ b/packager/media/base/muxer.cc @@ -19,6 +19,7 @@ Muxer::Muxer(const MuxerOptions& options) max_sd_pixels_(0), clear_lead_in_seconds_(0), crypto_period_duration_in_seconds_(0), + cancelled_(false), muxer_listener_(NULL), clock_(NULL) {} @@ -56,6 +57,9 @@ Status Muxer::Run() { uint32_t current_stream_id = 0; while (status.ok()) { + if (cancelled_) + return Status(error::CANCELLED, "muxer run cancelled"); + scoped_refptr sample; status = streams_[current_stream_id]->PullSample(&sample); if (!status.ok()) @@ -72,6 +76,10 @@ Status Muxer::Run() { return status.error_code() == error::END_OF_STREAM ? Finalize() : status; } +void Muxer::Cancel() { + cancelled_ = true; +} + void Muxer::SetMuxerListener(media::event::MuxerListener* muxer_listener) { muxer_listener_ = muxer_listener; } diff --git a/packager/media/base/muxer.h b/packager/media/base/muxer.h index 37d7548a4c..0acd5d5e73 100644 --- a/packager/media/base/muxer.h +++ b/packager/media/base/muxer.h @@ -57,6 +57,10 @@ class Muxer { /// Drive the remuxing from muxer side (pull). Status Run(); + /// Cancel a muxing job in progress. Will cause @a Run to exit with an error + /// status of type CANCELLED. + void Cancel(); + /// Set a MuxerListener event handler for this object. /// @param muxer_listener should not be NULL. void SetMuxerListener(event::MuxerListener* muxer_listener); @@ -110,6 +114,7 @@ class Muxer { uint32_t max_sd_pixels_; double clear_lead_in_seconds_; double crypto_period_duration_in_seconds_; + bool cancelled_; event::MuxerListener* muxer_listener_; // An external injected clock, can be NULL.