Skip to content

Commit

Permalink
Cherry pick PR #2427: Switch from scoped_ptr to std::unique_ptr in sh…
Browse files Browse the repository at this point in the history
…ared code (#3032)

Refer to the original PR: #2427

Test-On-Device: True

b/291356560

Co-authored-by: alexanderbobrovnik <[email protected]>
  • Loading branch information
1 parent 1e5927a commit ce5a9de
Show file tree
Hide file tree
Showing 47 changed files with 209 additions and 175 deletions.
11 changes: 11 additions & 0 deletions starboard/common/scoped_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
// scoped_array, scoped_ptr_malloc.

#include <algorithm>
#include <memory>
#include <utility>

#include "starboard/common/log.h"
Expand Down Expand Up @@ -134,6 +135,8 @@ class scoped_ptr {
: ptr_(other.release()) {}
#endif

scoped_ptr(scoped_ptr&& other) : ptr_(other.release()) {}

// Constructor. Move constructor for C++03 move emulation of this type.
scoped_ptr(RValue rvalue) // NOLINT(runtime/explicit)
: ptr_(rvalue.object->release()) {}
Expand Down Expand Up @@ -497,6 +500,14 @@ scoped_ptr<T> make_scoped_ptr(T* ptr) {
return scoped_ptr<T>(ptr);
}

#ifdef DEPRECATED_SCOPED_PTR
template <class C>
using unique_ptr_alias = std::unique_ptr<C>;
#else
template <class C>
using unique_ptr_alias = scoped_ptr<C>;
#endif

} // namespace starboard

#endif // STARBOARD_COMMON_SCOPED_PTR_H_
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace filter {

class AdaptiveAudioDecoder : public AudioDecoder, private JobQueue::JobOwner {
public:
typedef std::function<scoped_ptr<filter::AudioDecoder>(
typedef std::function<unique_ptr_alias<filter::AudioDecoder>(
const media::AudioStreamInfo& audio_stream_info,
SbDrmSystem drm_system)>
AudioDecoderCreator;
Expand Down Expand Up @@ -80,9 +80,9 @@ class AdaptiveAudioDecoder : public AudioDecoder, private JobQueue::JobOwner {
OutputCB output_cb_ = nullptr;
ErrorCB error_cb_ = nullptr;

scoped_ptr<filter::AudioDecoder> audio_decoder_;
scoped_ptr<filter::AudioResampler> resampler_;
scoped_ptr<filter::AudioChannelLayoutMixer> channel_mixer_;
unique_ptr_alias<filter::AudioDecoder> audio_decoder_;
std::unique_ptr<filter::AudioResampler> resampler_;
std::unique_ptr<filter::AudioChannelLayoutMixer> channel_mixer_;
InputBuffers pending_input_buffers_;
ConsumedCB pending_consumed_cb_;
std::queue<scoped_refptr<DecodedAudio>> decoded_audios_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_CHANNEL_LAYOUT_MIXER_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_CHANNEL_LAYOUT_MIXER_H_

#include <memory>
#include <vector>

#include "starboard/common/ref_counted.h"
Expand All @@ -38,7 +39,7 @@ class AudioChannelLayoutMixer {
virtual scoped_refptr<DecodedAudio> Mix(
const scoped_refptr<DecodedAudio>& audio_data) = 0;

static scoped_ptr<AudioChannelLayoutMixer> Create(
static std::unique_ptr<AudioChannelLayoutMixer> Create(
SbMediaAudioSampleType sample_type,
SbMediaAudioFrameStorageType storage_type,
int output_channels);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,12 +388,13 @@ AudioChannelLayoutMixerImpl::MixMonoToStereoOptimized(
} // namespace

// static
scoped_ptr<AudioChannelLayoutMixer> AudioChannelLayoutMixer::Create(
std::unique_ptr<AudioChannelLayoutMixer> AudioChannelLayoutMixer::Create(
SbMediaAudioSampleType sample_type,
SbMediaAudioFrameStorageType storage_type,
int output_channels) {
return scoped_ptr<AudioChannelLayoutMixer>(new AudioChannelLayoutMixerImpl(
sample_type, storage_type, output_channels));
return std::unique_ptr<AudioChannelLayoutMixer>(
new AudioChannelLayoutMixerImpl(sample_type, storage_type,
output_channels));
}

} // namespace filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

#include "starboard/common/log.h"
#include "starboard/common/mutex.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/media.h"
#include "starboard/shared/starboard/thread_checker.h"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <algorithm>
#include <string>
#include <utility>

#include "starboard/common/time.h"
#include "starboard/memory.h"
Expand Down Expand Up @@ -67,8 +68,8 @@ SbMediaAudioSampleType GetSinkAudioSampleType(
} // namespace

AudioRendererPcm::AudioRendererPcm(
scoped_ptr<AudioDecoder> decoder,
scoped_ptr<AudioRendererSink> audio_renderer_sink,
unique_ptr_alias<AudioDecoder> decoder,
unique_ptr_alias<AudioRendererSink> audio_renderer_sink,
const media::AudioStreamInfo& audio_stream_info,
int max_cached_frames,
int min_frames_per_append)
Expand All @@ -79,10 +80,10 @@ AudioRendererPcm::AudioRendererPcm(
bytes_per_frame_(media::GetBytesPerSample(sink_sample_type_) * channels_),
frame_buffer_(max_cached_frames_ * bytes_per_frame_),
frames_consumed_set_at_(CurrentMonotonicTime()),
decoder_(decoder.Pass()),
decoder_(std::move(decoder)),
process_audio_data_job_(
std::bind(&AudioRendererPcm::ProcessAudioData, this)),
audio_renderer_sink_(audio_renderer_sink.Pass()) {
audio_renderer_sink_(std::move(audio_renderer_sink)) {
SB_DLOG(INFO) << "Creating AudioRendererPcm with " << channels_
<< " channels, " << bytes_per_frame_ << " bytes per frame, "
<< max_cached_frames_ << " max cached frames, and "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RENDERER_INTERNAL_PCM_H_

#include <functional>
#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -66,8 +67,8 @@ class AudioRendererPcm : public AudioRenderer,
// longer accept more data.
// |min_frames_per_append| is the min number of frames that the audio renderer
// tries to append to the sink buffer at once.
AudioRendererPcm(scoped_ptr<AudioDecoder> decoder,
scoped_ptr<AudioRendererSink> audio_renderer_sink,
AudioRendererPcm(unique_ptr_alias<AudioDecoder> decoder,
unique_ptr_alias<AudioRendererSink> audio_renderer_sink,
const media::AudioStreamInfo& audio_stream_info,
int max_cached_frames,
int min_frames_per_append);
Expand Down Expand Up @@ -128,7 +129,7 @@ class AudioRendererPcm : public AudioRenderer,
int64_t total_frames_consumed_by_sink_ = 0;
int32_t frames_consumed_by_sink_since_last_get_current_time_;

scoped_ptr<AudioDecoder> decoder_;
unique_ptr_alias<AudioDecoder> decoder_;

int64_t frames_consumed_set_at_;
double playback_rate_ = 1.0;
Expand Down Expand Up @@ -161,7 +162,7 @@ class AudioRendererPcm : public AudioRenderer,
const SbMediaAudioSampleType sink_sample_type_;
const int bytes_per_frame_;

scoped_ptr<AudioResampler> resampler_;
std::unique_ptr<AudioResampler> resampler_;
optional<int> decoder_sample_rate_;
AudioTimeStretcher time_stretcher_;

Expand All @@ -181,7 +182,7 @@ class AudioRendererPcm : public AudioRenderer,
// and can thus avoid doing a full reset.
bool first_input_written_ = false;

scoped_ptr<AudioRendererSink> audio_renderer_sink_;
unique_ptr_alias<AudioRendererSink> audio_renderer_sink_;
bool is_eos_reached_on_sink_thread_ = false;
bool is_playing_on_sink_thread_ = false;
int64_t frames_in_buffer_on_sink_thread_ = 0;
Expand Down
4 changes: 3 additions & 1 deletion starboard/shared/starboard/player/filter/audio_resampler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RESAMPLER_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_RESAMPLER_H_

#include <memory>

#include "starboard/common/ref_counted.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/media.h"
Expand Down Expand Up @@ -57,7 +59,7 @@ class AudioResampler {
// The |output_cb| will be called whenever there is resampled data ready. It
// is always called asynchronously on |job_queue| and then the user of
// AudioResampler can call Read() to read the next chunk of resampled data.
static scoped_ptr<AudioResampler> Create(
static std::unique_ptr<AudioResampler> Create(
SbMediaAudioSampleType source_sample_type,
SbMediaAudioFrameStorageType source_storage_type,
int source_sample_rate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ class AudioResamplerImpl : public AudioResampler {
} // namespace

// static
scoped_ptr<AudioResampler> AudioResampler::Create(
std::unique_ptr<AudioResampler> AudioResampler::Create(
SbMediaAudioSampleType source_sample_type,
SbMediaAudioFrameStorageType source_storage_type,
int source_sample_rate,
SbMediaAudioSampleType destination_sample_type,
SbMediaAudioFrameStorageType destination_storage_type,
int destination_sample_rate,
int channels) {
return scoped_ptr<AudioResampler>(new AudioResamplerImpl(
return std::unique_ptr<AudioResampler>(new AudioResamplerImpl(
source_sample_type, source_storage_type, source_sample_rate,
destination_sample_type, destination_storage_type,
destination_sample_rate, channels));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_TIME_STRETCHER_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_AUDIO_TIME_STRETCHER_H_

#include <memory>

#include "starboard/common/ref_counted.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/shared/internal_only.h"
Expand Down Expand Up @@ -193,11 +195,11 @@ class AudioTimeStretcher {
scoped_refptr<DecodedAudio> wsola_output_;

// Overlap-and-add window.
scoped_array<float> ola_window_;
std::unique_ptr<float[]> ola_window_;

// Transition window, used to update |optimal_block_| by a weighted sum of
// |optimal_block_| and |target_block_|.
scoped_array<float> transition_window_;
std::unique_ptr<float[]> transition_window_;

// Auxiliary variables to avoid allocation in every iteration.

Expand Down
3 changes: 2 additions & 1 deletion starboard/shared/starboard/player/filter/cpu_video_frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_CPU_VIDEO_FRAME_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_CPU_VIDEO_FRAME_H_

#include <memory>
#include <vector>

#include "starboard/common/ref_counted.h"
Expand Down Expand Up @@ -84,7 +85,7 @@ class CpuVideoFrame : public VideoFrame {

// The following two variables are valid when the frame contains pixel data.
std::vector<Plane> planes_;
scoped_array<uint8_t> pixel_buffer_;
std::unique_ptr<uint8_t[]> pixel_buffer_;
};

} // namespace filter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ HandlerResult FilterBasedPlayerWorkerHandler::Init(
update_player_state_cb_ = update_player_state_cb;
update_player_error_cb_ = update_player_error_cb;

scoped_ptr<PlayerComponents::Factory> factory =
unique_ptr_alias<PlayerComponents::Factory> factory =
PlayerComponents::Factory::Create();
SB_DCHECK(factory);

Expand Down Expand Up @@ -401,8 +401,7 @@ HandlerResult FilterBasedPlayerWorkerHandler::SetBounds(const Bounds& bounds) {
bounds_.z_index = bounds.z_index;
bool bounds_changed = memcmp(&bounds_, &bounds, sizeof(bounds_)) != 0;
SB_LOG_IF(INFO, bounds_changed)
<< "Set bounds to "
<< "x: " << bounds.x << ", y: " << bounds.y
<< "Set bounds to " << "x: " << bounds.x << ", y: " << bounds.y
<< ", width: " << bounds.width << ", height: " << bounds.height
<< ", z_index: " << bounds.z_index;

Expand Down Expand Up @@ -518,14 +517,14 @@ void FilterBasedPlayerWorkerHandler::Stop() {

RemoveJobByToken(update_job_token_);

scoped_ptr<PlayerComponents> player_components;
unique_ptr_alias<PlayerComponents> player_components;
{
// Set |player_components_| to null with the lock, but we actually destroy
// it outside of the lock. This is because the VideoRenderer destructor
// may post a task to destroy the SbDecodeTarget to the same thread that
// might call GetCurrentDecodeTarget(), which would try to take this lock.
::starboard::ScopedLock lock(player_components_existence_mutex_);
player_components = player_components_.Pass();
player_components = std::move(player_components_);
media_time_provider_ = nullptr;
audio_renderer_ = nullptr;
video_renderer_ = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_FILTER_BASED_PLAYER_WORKER_HANDLER_H_

#include <functional>
#include <memory>
#include <string>

#include "starboard/common/scoped_ptr.h"
Expand Down Expand Up @@ -91,7 +92,7 @@ class FilterBasedPlayerWorkerHandler : public PlayerWorker::Handler,
// other accesses are happening from the same thread.
Mutex player_components_existence_mutex_;

scoped_ptr<PlayerComponents> player_components_;
unique_ptr_alias<PlayerComponents> player_components_;
// The following three variables cache the return values of member functions
// of |player_components_|. Their lifetime is tied to |player_components_|.
MediaTimeProvider* media_time_provider_ = nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include <queue>
#include <utility>

#include "starboard/common/ref_counted.h"
#include "starboard/common/scoped_ptr.h"
Expand Down Expand Up @@ -73,8 +75,8 @@ class InterleavedSincResampler {
data_.reset(new float[frames * channel_count]);
memcpy(data_.get(), data, frames * channel_count * sizeof(float));
}
Buffer(scoped_array<float> data, int frames)
: data_(data.Pass()), frames_(frames) {}
Buffer(std::unique_ptr<float[]> data, int frames)
: data_(std::move(data)), frames_(frames) {}

const float* GetData() const { return data_.get(); }

Expand All @@ -84,7 +86,7 @@ class InterleavedSincResampler {

private:
friend class ::starboard::RefCountedThreadSafe<Buffer>;
scoped_array<float> data_;
std::unique_ptr<float[]> data_;
int frames_ = 0;

Buffer(const Buffer&) = delete;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

#include "starboard/shared/starboard/player/filter/media_time_provider_impl.h"

#include <utility>

#include "starboard/common/log.h"

namespace starboard {
Expand All @@ -23,8 +25,8 @@ namespace player {
namespace filter {

MediaTimeProviderImpl::MediaTimeProviderImpl(
scoped_ptr<MonotonicSystemTimeProvider> system_time_provider)
: system_time_provider_(system_time_provider.Pass()) {
std::unique_ptr<MonotonicSystemTimeProvider> system_time_provider)
: system_time_provider_(std::move(system_time_provider)) {
SB_DCHECK(system_time_provider_);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_MEDIA_TIME_PROVIDER_IMPL_H_
#define STARBOARD_SHARED_STARBOARD_PLAYER_FILTER_MEDIA_TIME_PROVIDER_IMPL_H_

#include <memory>

#include "starboard/common/mutex.h"
#include "starboard/common/scoped_ptr.h"
#include "starboard/common/time.h"
Expand All @@ -40,7 +42,7 @@ class MediaTimeProviderImpl : public MediaTimeProvider,
};

explicit MediaTimeProviderImpl(
scoped_ptr<MonotonicSystemTimeProvider> system_time_provider);
std::unique_ptr<MonotonicSystemTimeProvider> system_time_provider);

void Play() override;
void Pause() override;
Expand All @@ -58,7 +60,7 @@ class MediaTimeProviderImpl : public MediaTimeProvider,
// should handle this properly.
int64_t GetCurrentMediaTime_Locked(int64_t* current_time = NULL);

scoped_ptr<MonotonicSystemTimeProvider> system_time_provider_;
std::unique_ptr<MonotonicSystemTimeProvider> system_time_provider_;

Mutex mutex_;

Expand Down
Loading

0 comments on commit ce5a9de

Please sign in to comment.