From 9a60760815d848bad0409c944811bafa2759dac6 Mon Sep 17 00:00:00 2001 From: KongQun Yang Date: Mon, 25 Sep 2017 15:22:18 -0700 Subject: [PATCH] Change MediaSample::CopyFrom to MediaSample::Clone Change-Id: Ibc8e6ba149496bb62179230e95aa6803a015b12b --- packager/media/base/media_sample.cc | 48 +++++++++---------- packager/media/base/media_sample.h | 7 ++- packager/media/crypto/encryption_handler.cc | 3 +- packager/media/formats/webm/segmenter.cc | 2 +- .../media/trick_play/trick_play_handler.cc | 2 +- 5 files changed, 28 insertions(+), 34 deletions(-) diff --git a/packager/media/base/media_sample.cc b/packager/media/base/media_sample.cc index d5b8df623b..a490926c69 100644 --- a/packager/media/base/media_sample.cc +++ b/packager/media/base/media_sample.cc @@ -60,32 +60,6 @@ std::shared_ptr MediaSample::CopyFrom(const uint8_t* data, data, data_size, side_data, side_data_size, is_key_frame)); } -// static -std::shared_ptr MediaSample::CopyFrom( - const MediaSample& media_sample) { - std::shared_ptr new_media_sample(new MediaSample); - new_media_sample->dts_ = media_sample.dts_; - new_media_sample->pts_ = media_sample.pts_; - new_media_sample->duration_ = media_sample.duration_; - new_media_sample->is_key_frame_ = media_sample.is_key_frame_; - new_media_sample->is_encrypted_ = media_sample.is_encrypted_; - new_media_sample->data_ = media_sample.data_; - new_media_sample->data_size_ = media_sample.data_size_; - new_media_sample->side_data_ = media_sample.side_data_; - new_media_sample->side_data_size_ = media_sample.side_data_size_; - new_media_sample->config_id_ = media_sample.config_id_; - if (media_sample.decrypt_config_) { - new_media_sample->decrypt_config_.reset( - new DecryptConfig(media_sample.decrypt_config_->key_id(), - media_sample.decrypt_config_->iv(), - media_sample.decrypt_config_->subsamples(), - media_sample.decrypt_config_->protection_scheme(), - media_sample.decrypt_config_->crypt_byte_block(), - media_sample.decrypt_config_->skip_byte_block())); - } - return new_media_sample; -} - // static std::shared_ptr MediaSample::FromMetadata(const uint8_t* metadata, size_t metadata_size) { @@ -104,6 +78,28 @@ std::shared_ptr MediaSample::CreateEOSBuffer() { new MediaSample(nullptr, 0, nullptr, 0, false)); } +std::shared_ptr MediaSample::Clone() const { + std::shared_ptr new_media_sample(new MediaSample); + new_media_sample->dts_ = dts_; + new_media_sample->pts_ = pts_; + new_media_sample->duration_ = duration_; + new_media_sample->is_key_frame_ = is_key_frame_; + new_media_sample->is_encrypted_ = is_encrypted_; + new_media_sample->data_ = data_; + new_media_sample->data_size_ = data_size_; + new_media_sample->side_data_ = side_data_; + new_media_sample->side_data_size_ = side_data_size_; + new_media_sample->config_id_ = config_id_; + if (decrypt_config_) { + new_media_sample->decrypt_config_.reset(new DecryptConfig( + decrypt_config_->key_id(), decrypt_config_->iv(), + decrypt_config_->subsamples(), decrypt_config_->protection_scheme(), + decrypt_config_->crypt_byte_block(), + decrypt_config_->skip_byte_block())); + } + return new_media_sample; +} + void MediaSample::TransferData(std::shared_ptr data, size_t data_size) { data_ = std::move(data); diff --git a/packager/media/base/media_sample.h b/packager/media/base/media_sample.h index 3a8a30bfd0..7e9823b563 100644 --- a/packager/media/base/media_sample.h +++ b/packager/media/base/media_sample.h @@ -45,10 +45,6 @@ class MediaSample { size_t side_data_size, bool is_key_frame); - /// Make a copy of MediaSample. - /// @param media_sample is the source MediaSample to copy from. - static std::shared_ptr CopyFrom(const MediaSample& media_sample); - /// Create a MediaSample object from metadata. /// Unlike other factory methods, this cannot be a key frame. It must be only /// for metadata. @@ -68,6 +64,9 @@ class MediaSample { virtual ~MediaSample(); + /// Clone the object and return a new MediaSample. + std::shared_ptr Clone() const; + /// Transfer data to this media sample. No data copying is involved. /// @param data points to the data to be transferred. /// @param data_size is the size of the data to be transferred. diff --git a/packager/media/crypto/encryption_handler.cc b/packager/media/crypto/encryption_handler.cc index 7fb871ead9..8ad5db9077 100644 --- a/packager/media/crypto/encryption_handler.cc +++ b/packager/media/crypto/encryption_handler.cc @@ -258,8 +258,7 @@ Status EncryptionHandler::ProcessMediaSample( // Now that we know that this sample must be encrypted, make a copy of // the sample first so that all the encryption operations can be done // in-place. - std::shared_ptr cipher_sample = - MediaSample::CopyFrom(*clear_sample); + std::shared_ptr cipher_sample(clear_sample->Clone()); // |cipher_sample| above still contains the old clear sample data. We will // use |cipher_sample_data| to hold cipher sample data then transfer it to // |cipher_sample| after encryption. diff --git a/packager/media/formats/webm/segmenter.cc b/packager/media/formats/webm/segmenter.cc index 6adb114005..6435284ba7 100644 --- a/packager/media/formats/webm/segmenter.cc +++ b/packager/media/formats/webm/segmenter.cc @@ -157,7 +157,7 @@ Status Segmenter::Finalize() { } Status Segmenter::AddSample(const MediaSample& source_sample) { - std::shared_ptr sample = MediaSample::CopyFrom(source_sample); + std::shared_ptr sample(source_sample.Clone()); if (sample_duration_ == 0) { first_timestamp_ = sample->pts(); diff --git a/packager/media/trick_play/trick_play_handler.cc b/packager/media/trick_play/trick_play_handler.cc index 97752b2220..e351dda462 100644 --- a/packager/media/trick_play/trick_play_handler.cc +++ b/packager/media/trick_play/trick_play_handler.cc @@ -164,7 +164,7 @@ Status TrickPlayHandler::OnTrickFrame(const MediaSample& sample) { total_trick_frames_++; // Make a message we can store until later. - previous_trick_frame_ = MediaSample::CopyFrom(sample); + previous_trick_frame_ = sample.Clone(); // Add the message to our queue so that it will be ready to go out. delayed_messages_.push_back(