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,16 +35,17 @@ class AudioDecoder::ImplBase ...@@ -35,16 +35,17 @@ class AudioDecoder::ImplBase
codec_(codec), codec_(codec),
num_channels_(num_channels), num_channels_(num_channels),
operational_status_(STATUS_UNINITIALIZED) { 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; operational_status_ = STATUS_INVALID_CONFIGURATION;
} }
}
OperationalStatus InitializationResult() const { OperationalStatus InitializationResult() const {
return operational_status_; return operational_status_;
} }
void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame, void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback) { DecodeFrameCallback callback) {
DCHECK_EQ(operational_status_, STATUS_INITIALIZED); DCHECK_EQ(operational_status_, STATUS_INITIALIZED);
bool is_continuous = true; bool is_continuous = true;
...@@ -62,9 +63,10 @@ class AudioDecoder::ImplBase ...@@ -62,9 +63,10 @@ class AudioDecoder::ImplBase
static_cast<int>(encoded_frame->data.size())); static_cast<int>(encoded_frame->data.size()));
if (!decoded_audio) { if (!decoded_audio) {
VLOG(2) << "Decoding of frame " << encoded_frame->frame_id << " failed."; VLOG(2) << "Decoding of frame " << encoded_frame->frame_id << " failed.";
cast_environment_->PostTask( cast_environment_->GetTaskRunner(CastEnvironment::MAIN)
CastEnvironment::MAIN, FROM_HERE, ->PostTask(FROM_HERE,
base::Bind(callback, base::Passed(&decoded_audio), false)); base::BindOnce(std::move(callback),
base::Passed(&decoded_audio), false));
return; return;
} }
...@@ -76,11 +78,10 @@ class AudioDecoder::ImplBase ...@@ -76,11 +78,10 @@ class AudioDecoder::ImplBase
event->frame_id = encoded_frame->frame_id; event->frame_id = encoded_frame->frame_id;
cast_environment_->logger()->DispatchFrameEvent(std::move(event)); cast_environment_->logger()->DispatchFrameEvent(std::move(event));
cast_environment_->PostTask(CastEnvironment::MAIN, cast_environment_->GetTaskRunner(CastEnvironment::MAIN)
FROM_HERE, ->PostTask(FROM_HERE,
base::Bind(callback, base::BindOnce(std::move(callback),
base::Passed(&decoded_audio), base::Passed(&decoded_audio), is_continuous));
is_continuous));
} }
protected: protected:
...@@ -119,8 +120,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase { ...@@ -119,8 +120,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase {
max_samples_per_frame_(kOpusMaxFrameDurationMillis * sampling_rate / max_samples_per_frame_(kOpusMaxFrameDurationMillis * sampling_rate /
1000), 1000),
buffer_(new float[max_samples_per_frame_ * num_channels]) { buffer_(new float[max_samples_per_frame_ * num_channels]) {
if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) if (ImplBase::operational_status_ != STATUS_UNINITIALIZED) {
return; return;
}
if (opus_decoder_init(opus_decoder_, sampling_rate, num_channels) != if (opus_decoder_init(opus_decoder_, sampling_rate, num_channels) !=
OPUS_OK) { OPUS_OK) {
ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION; ImplBase::operational_status_ = STATUS_INVALID_CONFIGURATION;
...@@ -133,10 +135,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase { ...@@ -133,10 +135,9 @@ class AudioDecoder::OpusImpl : public AudioDecoder::ImplBase {
~OpusImpl() final = default; ~OpusImpl() final = default;
void RecoverBecauseFramesWereDropped() final { void RecoverBecauseFramesWereDropped() final {
// Passing NULL for the input data notifies the decoder of frame loss. // Passing nullptr for the input data notifies the decoder of frame loss.
const opus_int32 result = const opus_int32 result = opus_decode_float(
opus_decode_float( opus_decoder_, nullptr, 0, buffer_.get(), max_samples_per_frame_, 0);
opus_decoder_, NULL, 0, buffer_.get(), max_samples_per_frame_, 0);
DCHECK_GE(result, 0); DCHECK_GE(result, 0);
} }
...@@ -241,19 +242,17 @@ OperationalStatus AudioDecoder::InitializationResult() const { ...@@ -241,19 +242,17 @@ OperationalStatus AudioDecoder::InitializationResult() const {
} }
void AudioDecoder::DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame, void AudioDecoder::DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback) { DecodeFrameCallback callback) {
DCHECK(encoded_frame.get()); DCHECK(encoded_frame.get());
DCHECK(!callback.is_null()); DCHECK(!callback.is_null());
if (!impl_.get() || impl_->InitializationResult() != STATUS_INITIALIZED) { if (!impl_.get() || impl_->InitializationResult() != STATUS_INITIALIZED) {
callback.Run(base::WrapUnique<AudioBus>(NULL), false); std::move(callback).Run(base::WrapUnique<AudioBus>(nullptr), false);
return; return;
} }
cast_environment_->PostTask(CastEnvironment::AUDIO, cast_environment_->GetTaskRunner(CastEnvironment::AUDIO)
FROM_HERE, ->PostTask(FROM_HERE, base::BindOnce(&AudioDecoder::ImplBase::DecodeFrame,
base::Bind(&AudioDecoder::ImplBase::DecodeFrame, impl_, base::Passed(&encoded_frame),
impl_, std::move(callback)));
base::Passed(&encoded_frame),
callback));
} }
} // namespace cast } // namespace cast
......
...@@ -24,9 +24,9 @@ class AudioDecoder { ...@@ -24,9 +24,9 @@ class AudioDecoder {
// be false if the decoder has detected a frame skip since the last decode // 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 // operation; and the client should take steps to smooth audio discontinuities
// in this case. // in this case.
typedef base::Callback<void(std::unique_ptr<AudioBus> audio_bus, using DecodeFrameCallback =
bool is_continuous)> base::OnceCallback<void(std::unique_ptr<AudioBus> audio_bus,
DecodeFrameCallback; bool is_continuous)>;
AudioDecoder(const scoped_refptr<CastEnvironment>& cast_environment, AudioDecoder(const scoped_refptr<CastEnvironment>& cast_environment,
int channels, int channels,
...@@ -47,7 +47,7 @@ class AudioDecoder { ...@@ -47,7 +47,7 @@ class AudioDecoder {
// When it is not, the decoder will assume one or more frames have been // 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. // dropped (e.g., due to packet loss), and will perform recovery actions.
void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame, void DecodeFrame(std::unique_ptr<EncodedFrame> encoded_frame,
const DecodeFrameCallback& callback); DecodeFrameCallback callback);
private: private:
class ImplBase; class ImplBase;
......
...@@ -118,23 +118,23 @@ void CastReceiverImpl::DecodeEncodedAudioFrame( ...@@ -118,23 +118,23 @@ void CastReceiverImpl::DecodeEncodedAudioFrame(
std::unique_ptr<EncodedFrame> encoded_frame) { std::unique_ptr<EncodedFrame> encoded_frame) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (!encoded_frame) { if (!encoded_frame) {
callback.Run(base::WrapUnique<AudioBus>(NULL), base::TimeTicks(), false); callback.Run(base::WrapUnique<AudioBus>(nullptr), base::TimeTicks(), false);
return; return;
} }
if (!audio_decoder_) { if (!audio_decoder_) {
audio_decoder_.reset(new AudioDecoder(cast_environment_, audio_decoder_ =
num_audio_channels_, std::make_unique<AudioDecoder>(cast_environment_, num_audio_channels_,
audio_sampling_rate_, audio_sampling_rate_, audio_codec_);
audio_codec_));
} }
const FrameId frame_id = encoded_frame->frame_id; const FrameId frame_id = encoded_frame->frame_id;
const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp; const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp;
const base::TimeTicks playout_time = encoded_frame->reference_time; const base::TimeTicks playout_time = encoded_frame->reference_time;
audio_decoder_->DecodeFrame( audio_decoder_->DecodeFrame(
std::move(encoded_frame), std::move(encoded_frame),
base::Bind(&CastReceiverImpl::EmitDecodedAudioFrame, cast_environment_, base::BindOnce(&CastReceiverImpl::EmitDecodedAudioFrame,
callback, frame_id, rtp_timestamp, playout_time)); cast_environment_, callback, frame_id, rtp_timestamp,
playout_time));
} }
void CastReceiverImpl::DecodeEncodedVideoFrame( void CastReceiverImpl::DecodeEncodedVideoFrame(
...@@ -142,7 +142,7 @@ void CastReceiverImpl::DecodeEncodedVideoFrame( ...@@ -142,7 +142,7 @@ void CastReceiverImpl::DecodeEncodedVideoFrame(
std::unique_ptr<EncodedFrame> encoded_frame) { std::unique_ptr<EncodedFrame> encoded_frame) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (!encoded_frame) { if (!encoded_frame) {
callback.Run(base::WrapRefCounted<VideoFrame>(NULL), base::TimeTicks(), callback.Run(base::WrapRefCounted<VideoFrame>(nullptr), base::TimeTicks(),
false); false);
return; return;
} }
...@@ -152,8 +152,10 @@ void CastReceiverImpl::DecodeEncodedVideoFrame( ...@@ -152,8 +152,10 @@ void CastReceiverImpl::DecodeEncodedVideoFrame(
TRACE_EVENT_SCOPE_THREAD, "rtp_timestamp", TRACE_EVENT_SCOPE_THREAD, "rtp_timestamp",
encoded_frame->rtp_timestamp.lower_32_bits()); encoded_frame->rtp_timestamp.lower_32_bits());
if (!video_decoder_) if (!video_decoder_) {
video_decoder_.reset(new VideoDecoder(cast_environment_, video_codec_)); video_decoder_ =
std::make_unique<VideoDecoder>(cast_environment_, video_codec_);
}
const FrameId frame_id = encoded_frame->frame_id; const FrameId frame_id = encoded_frame->frame_id;
const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp; const RtpTimeTicks rtp_timestamp = encoded_frame->rtp_timestamp;
const base::TimeTicks playout_time = encoded_frame->reference_time; 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