Commit 2072c031 authored by Jose Lopes's avatar Jose Lopes Committed by Commit Bot

media: Migrate DecodeFrameCallback to once callback.

This callback is used to report that the DecodeFrame operation is
complete, therefore it's a once callback.

Documentation:
* https://cs.chromium.org/chromium/src/media/cast/receiver/audio_decoder.h?rcl=292ee8149fd602c22de27e034db2fd7b1113cb8d&l=21

The callback is executed via PostTask:
* https://cs.chromium.org/chromium/src/media/cast/receiver/audio_decoder.cc?rcl=292ee8149fd602c22de27e034db2fd7b1113cb8d&l=67
* https://cs.chromium.org/chromium/src/media/cast/receiver/audio_decoder.cc?rcl=292ee8149fd602c22de27e034db2fd7b1113cb8d&l=81

This is part of the base::Callback migration.

Context: https://cs.chromium.org/chromium/src/docs/callback.md?rcl=9fcc3764aea8f97e9f6de4a9ee61d554e67edcda&l=40

Bug: 714018
Change-Id: Id6822683089aa7547171d97a33aa14c0842f97d7
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2074480
Commit-Queue: Jose Lopes <jabolopes@google.com>
Reviewed-by: default avatarXiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#745432}
parent 12cb5600
......@@ -35,8 +35,9 @@ class AudioDecoder::ImplBase
codec_(codec),
num_channels_(num_channels),
operational_status_(STATUS_UNINITIALIZED) {
if (num_channels_ <= 0 || sampling_rate <= 0 || sampling_rate % 100 != 0)
if (num_channels_ <= 0 || sampling_rate <= 0 || sampling_rate % 100 != 0) {
operational_status_ = STATUS_INVALID_CONFIGURATION;
}
}
OperationalStatus InitializationResult() const {
......@@ -44,7 +45,7 @@ class AudioDecoder::ImplBase
}
void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback) {
DecodeFrameCallback callback) {
DCHECK_EQ(operational_status_, STATUS_INITIALIZED);
bool is_continuous = true;
......@@ -62,9 +63,10 @@ class AudioDecoder::ImplBase
static_cast<int>(encoded_frame->data.size()));
if (!decoded_audio) {
VLOG(2) << "Decoding of frame " << encoded_frame->frame_id << " failed.";
cast_environment_->PostTask(
CastEnvironment::MAIN, FROM_HERE,
base::Bind(callback, base::Passed(&decoded_audio), false));
cast_environment_->GetTaskRunner(CastEnvironment::MAIN)
->PostTask(FROM_HERE,
base::BindOnce(std::move(callback),
base::Passed(&decoded_audio), false));
return;
}
......@@ -76,11 +78,10 @@ class AudioDecoder::ImplBase
event->frame_id = encoded_frame->frame_id;
cast_environment_->logger()->DispatchFrameEvent(std::move(event));
cast_environment_->PostTask(CastEnvironment::MAIN,
FROM_HERE,
base::Bind(callback,
base::Passed(&decoded_audio),
is_continuous));
cast_environment_->GetTaskRunner(CastEnvironment::MAIN)
->PostTask(FROM_HERE,
base::BindOnce(std::move(callback),
base::Passed(&decoded_audio), is_continuous));
}
protected:
......@@ -119,8 +120,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase {
max_samples_per_frame_(kOpusMaxFrameDurationMillis * sampling_rate /
1000),
buffer_(new float[max_samples_per_frame_ * num_channels]) {
if (ImplBase::operational_status_ != STATUS_UNINITIALIZED)
if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) {
return;
}
if (opus_decoder_init(opus_decoder_, sampling_rate, num_channels) !=
OPUS_OK) {
ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION;
......@@ -133,10 +135,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase {
~OpusImpl() final = default;
void RecoverBecauseFramesWereDropped() final {
// Passing NULL for the input data notifies the decoder of frame loss.
const opus_int32 result =
opus_decode_float(
opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0);
// Passing nullptr for the input data notifies the decoder of frame loss.
const opus_int32 result = opus_decode_float(
opus_decoder_, nullptr, 0, buffer_.get(), max_samples_per_frame_, 0);
DCHECK_GE(result, 0);
}
......@@ -241,19 +242,17 @@ OperationalStatus AudioDecoder::InitializationResult() const {
}
void AudioDecoder::DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback) {
DecodeFrameCallback callback) {
DCHECK(encoded_frame.get());
DCHECK(!callback.is_null());
if (!impl_.get() || impl_->InitializationResult() != STATUS_INITIALIZED) {
callback.Run(base::WrapUnique<AudioBus>(NULL), false);
std::move(callback).Run(base::WrapUnique<AudioBus>(nullptr), false);
return;
}
cast_environment_->PostTask(CastEnvironment::AUDIO,
FROM_HERE,
base::Bind(&AudioDecoder::ImplBase::DecodeFrame,
impl_,
base::Passed(&encoded_frame),
callback));
cast_environment_->GetTaskRunner(CastEnvironment::AUDIO)
->PostTask(FROM_HERE, base::BindOnce(&AudioDecoder::ImplBase::DecodeFrame,
impl_, base::Passed(&encoded_frame),
std::move(callback)));
}
} // namespace cast
......
......@@ -24,9 +24,9 @@ class AudioDecoder {
// be false if the decoder has detected a frame skip since the last decode
// operation; and the client should take steps to smooth audio discontinuities
// in this case.
typedef base::Callback<void(std::unique_ptr<AudioBus> audio_bus,
bool is_continuous)>
DecodeFrameCallback;
using DecodeFrameCallback =
base::OnceCallback<void(std::unique_ptr<AudioBus> audio_bus,
bool is_continuous)>;
AudioDecoder(const scoped_refptr<CastEnvironment>& cast_environment,
int channels,
......@@ -47,7 +47,7 @@ class AudioDecoder {
// When it is not, the decoder will assume one or more frames have been
// dropped (e.g., due to packet loss), and will perform recovery actions.
void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback);
DecodeFrameCallback callback);
private:
class ImplBase;
......
......@@ -118,23 +118,23 @@ void CastReceiverImpl::DecodeEncodedAudioFrame(
std::unique_ptr<EncodedFrame> encoded_frame) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (!encoded_frame) {
callback.Run(base::WrapUnique<AudioBus>(NULL), base::TimeTicks(), false);
callback.Run(base::WrapUnique<AudioBus>(nullptr), base::TimeTicks(), false);
return;
}
if (!audio_decoder_) {
audio_decoder_.reset(new AudioDecoder(cast_environment_,
num_audio_channels_,
audio_sampling_rate_,
audio_codec_));
audio_decoder_ =
std::make_unique<AudioDecoder>(cast_environment_, num_audio_channels_,
audio_sampling_rate_, audio_codec_);
}
const FrameId frame_id = encoded_frame->frame_id;
const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp;
const base::TimeTicks playout_time = encoded_frame->reference_time;
audio_decoder_->DecodeFrame(
std::move(encoded_frame),
base::Bind(&CastReceiverImpl::EmitDecodedAudioFrame, cast_environment_,
callback, frame_id, rtp_timestamp, playout_time));
base::BindOnce(&CastReceiverImpl::EmitDecodedAudioFrame,
cast_environment_, callback, frame_id, rtp_timestamp,
playout_time));
}
void CastReceiverImpl::DecodeEncodedVideoFrame(
......@@ -142,7 +142,7 @@ void CastReceiverImpl::DecodeEncodedVideoFrame(
std::unique_ptr<EncodedFrame> encoded_frame) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (!encoded_frame) {
callback.Run(base::WrapRefCounted<VideoFrame>(NULL), base::TimeTicks(),
callback.Run(base::WrapRefCounted<VideoFrame>(nullptr), base::TimeTicks(),
false);
return;
}
......@@ -152,8 +152,10 @@ void CastReceiverImpl::DecodeEncodedVideoFrame(
TRACE_EVENT_SCOPE_THREAD, "rtp_timestamp",
encoded_frame->rtp_timestamp.lower_32_bits());
if (!video_decoder_)
video_decoder_.reset(new VideoDecoder(cast_environment_, video_codec_));
if (!video_decoder_) {
video_decoder_ =
std::make_unique<VideoDecoder>(cast_environment_, video_codec_);
}
const FrameId frame_id = encoded_frame->frame_id;
const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp;
const base::TimeTicks playout_time = encoded_frame->reference_time;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment