Commit d202ba7e authored by Jose Lopes's avatar Jose Lopes Committed by Commit Bot

media: Migrate video FrameEncodedCallback to once callback.

This is a once callback because it is run whe the EncodeVideoFrame
method completes, as documented in:
* https://cs.chromium.org/chromium/src/media/cast/sender/video_encoder.h?l=27&gsn=FrameEncodedCallback
* https://cs.chromium.org/chromium/src/media/cast/sender/video_encoder.h?rcl=d00d2b00c0d2b47044ea1816d51ad559d2bdcfd3&l=48

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: I3e62ce9a72e632b0aa87dd2d3f839e862b63af6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2074503
Auto-Submit: Jose Lopes <jabolopes@google.com>
Commit-Queue: Xiaohan Wang <xhwang@chromium.org>
Reviewed-by: default avatarXiaohan Wang <xhwang@chromium.org>
Cr-Commit-Position: refs/heads/master@{#747031}
parent d6a48a35
This diff is collapsed.
...@@ -42,10 +42,9 @@ class ExternalVideoEncoder : public VideoEncoder { ...@@ -42,10 +42,9 @@ class ExternalVideoEncoder : public VideoEncoder {
~ExternalVideoEncoder() final; ~ExternalVideoEncoder() final;
// VideoEncoder implementation. // VideoEncoder implementation.
bool EncodeVideoFrame( bool EncodeVideoFrame(scoped_refptr<media::VideoFrame> video_frame,
scoped_refptr<media::VideoFrame> video_frame, base::TimeTicks reference_time,
const base::TimeTicks& reference_time, FrameEncodedCallback frame_encoded_callback) final;
const FrameEncodedCallback& frame_encoded_callback) final;
void SetBitRate(int new_bit_rate) final; void SetBitRate(int new_bit_rate) final;
void GenerateKeyFrame() final; void GenerateKeyFrame() final;
......
...@@ -32,14 +32,14 @@ namespace { ...@@ -32,14 +32,14 @@ namespace {
struct InProgressH264VTFrameEncode { struct InProgressH264VTFrameEncode {
const RtpTimeTicks rtp_timestamp; const RtpTimeTicks rtp_timestamp;
const base::TimeTicks reference_time; const base::TimeTicks reference_time;
const VideoEncoder::FrameEncodedCallback frame_encoded_callback; VideoEncoder::FrameEncodedCallback frame_encoded_callback;
InProgressH264VTFrameEncode(RtpTimeTicks rtp, InProgressH264VTFrameEncode(RtpTimeTicks rtp,
base::TimeTicks r_time, base::TimeTicks r_time,
VideoEncoder::FrameEncodedCallback callback) VideoEncoder::FrameEncodedCallback callback)
: rtp_timestamp(rtp), : rtp_timestamp(rtp),
reference_time(r_time), reference_time(r_time),
frame_encoded_callback(callback) {} frame_encoded_callback(std::move(callback)) {}
}; };
} // namespace } // namespace
...@@ -347,8 +347,8 @@ void H264VideoToolboxEncoder::DestroyCompressionSession() { ...@@ -347,8 +347,8 @@ void H264VideoToolboxEncoder::DestroyCompressionSession() {
bool H264VideoToolboxEncoder::EncodeVideoFrame( bool H264VideoToolboxEncoder::EncodeVideoFrame(
scoped_refptr<media::VideoFrame> video_frame, scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& reference_time, base::TimeTicks reference_time,
const FrameEncodedCallback& frame_encoded_callback) { FrameEncodedCallback frame_encoded_callback) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!frame_encoded_callback.is_null()); DCHECK(!frame_encoded_callback.is_null());
...@@ -393,7 +393,7 @@ bool H264VideoToolboxEncoder::EncodeVideoFrame( ...@@ -393,7 +393,7 @@ bool H264VideoToolboxEncoder::EncodeVideoFrame(
new InProgressH264VTFrameEncode( new InProgressH264VTFrameEncode(
RtpTimeTicks::FromTimeDelta(video_frame->timestamp(), RtpTimeTicks::FromTimeDelta(video_frame->timestamp(),
kVideoFrequency), kVideoFrequency),
reference_time, frame_encoded_callback)); reference_time, std::move(frame_encoded_callback)));
// Build a suitable frame properties dictionary for keyframes. // Build a suitable frame properties dictionary for keyframes.
base::ScopedCFTypeRef<CFDictionaryRef> frame_props; base::ScopedCFTypeRef<CFDictionaryRef> frame_props;
...@@ -498,7 +498,7 @@ void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque, ...@@ -498,7 +498,7 @@ void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque,
VTEncodeInfoFlags info, VTEncodeInfoFlags info,
CMSampleBufferRef sbuf) { CMSampleBufferRef sbuf) {
auto* encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque); auto* encoder = reinterpret_cast<H264VideoToolboxEncoder*>(encoder_opaque);
const std::unique_ptr<InProgressH264VTFrameEncode> request( std::unique_ptr<InProgressH264VTFrameEncode> request(
reinterpret_cast<InProgressH264VTFrameEncode*>(request_opaque)); reinterpret_cast<InProgressH264VTFrameEncode*>(request_opaque));
bool keyframe = false; bool keyframe = false;
bool has_frame_data = false; bool has_frame_data = false;
...@@ -558,10 +558,10 @@ void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque, ...@@ -558,10 +558,10 @@ void H264VideoToolboxEncoder::CompressionCallback(void* encoder_opaque,
encoded_frame->encode_completion_time = encoded_frame->encode_completion_time =
encoder->cast_environment_->Clock()->NowTicks(); encoder->cast_environment_->Clock()->NowTicks();
encoder->cast_environment_->PostTask( encoder->cast_environment_->GetTaskRunner(CastEnvironment::MAIN)
CastEnvironment::MAIN, FROM_HERE, ->PostTask(FROM_HERE,
base::Bind(request->frame_encoded_callback, base::BindOnce(std::move(request->frame_encoded_callback),
base::Passed(&encoded_frame))); base::Passed(&encoded_frame)));
} }
} // namespace cast } // namespace cast
......
...@@ -37,10 +37,9 @@ class H264VideoToolboxEncoder : public VideoEncoder, ...@@ -37,10 +37,9 @@ class H264VideoToolboxEncoder : public VideoEncoder,
~H264VideoToolboxEncoder() final; ~H264VideoToolboxEncoder() final;
// media::cast::VideoEncoder implementation // media::cast::VideoEncoder implementation
bool EncodeVideoFrame( bool EncodeVideoFrame(scoped_refptr<media::VideoFrame> video_frame,
scoped_refptr<media::VideoFrame> video_frame, base::TimeTicks reference_time,
const base::TimeTicks& reference_time, FrameEncodedCallback frame_encoded_callback) final;
const FrameEncodedCallback& frame_encoded_callback) final;
void SetBitRate(int new_bit_rate) final; void SetBitRate(int new_bit_rate) final;
void GenerateKeyFrame() final; void GenerateKeyFrame() final;
std::unique_ptr<VideoFrameFactory> CreateVideoFrameFactory() final; std::unique_ptr<VideoFrameFactory> CreateVideoFrameFactory() final;
......
...@@ -218,9 +218,9 @@ class H264VideoToolboxEncoderTest : public ::testing::Test { ...@@ -218,9 +218,9 @@ class H264VideoToolboxEncoderTest : public ::testing::Test {
&clock_, task_environment_.GetMainThreadTaskRunner(), &clock_, task_environment_.GetMainThreadTaskRunner(),
task_environment_.GetMainThreadTaskRunner(), task_environment_.GetMainThreadTaskRunner(),
task_environment_.GetMainThreadTaskRunner()); task_environment_.GetMainThreadTaskRunner());
encoder_.reset(new H264VideoToolboxEncoder( encoder_ = std::make_unique<H264VideoToolboxEncoder>(
cast_environment_, video_sender_config_, cast_environment_, video_sender_config_,
base::Bind(&SaveOperationalStatus, &operational_status_))); base::Bind(&SaveOperationalStatus, &operational_status_));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
EXPECT_EQ(STATUS_INITIALIZED, operational_status_); EXPECT_EQ(STATUS_INITIALIZED, operational_status_);
} }
...@@ -269,15 +269,15 @@ FrameSenderConfig H264VideoToolboxEncoderTest::video_sender_config_; ...@@ -269,15 +269,15 @@ FrameSenderConfig H264VideoToolboxEncoderTest::video_sender_config_;
// Failed on mac-rel trybot. http://crbug.com/627260 // Failed on mac-rel trybot. http://crbug.com/627260
TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFrameMetadataSequence) { TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFrameMetadataSequence) {
scoped_refptr<MetadataRecorder> metadata_recorder(new MetadataRecorder()); auto metadata_recorder = base::MakeRefCounted<MetadataRecorder>();
VideoEncoder::FrameEncodedCallback cb = base::Bind(
&MetadataRecorder::CompareFrameWithExpected, metadata_recorder.get());
metadata_recorder->PushExpectation( metadata_recorder->PushExpectation(
FrameId::first(), FrameId::first(), FrameId::first(), FrameId::first(),
RtpTimeTicks::FromTimeDelta(frame_->timestamp(), kVideoFrequency), RtpTimeTicks::FromTimeDelta(frame_->timestamp(), kVideoFrequency),
clock_.NowTicks()); clock_.NowTicks());
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_TRUE(encoder_->EncodeVideoFrame(
frame_, clock_.NowTicks(),
base::BindOnce(&MetadataRecorder::CompareFrameWithExpected,
metadata_recorder.get())));
base::RunLoop().RunUntilIdle(); base::RunLoop().RunUntilIdle();
for (FrameId frame_id = FrameId::first() + 1; for (FrameId frame_id = FrameId::first() + 1;
...@@ -287,7 +287,10 @@ TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFrameMetadataSequence) { ...@@ -287,7 +287,10 @@ TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFrameMetadataSequence) {
frame_id, frame_id - 1, frame_id, frame_id - 1,
RtpTimeTicks::FromTimeDelta(frame_->timestamp(), kVideoFrequency), RtpTimeTicks::FromTimeDelta(frame_->timestamp(), kVideoFrequency),
clock_.NowTicks()); clock_.NowTicks());
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_TRUE(encoder_->EncodeVideoFrame(
frame_, clock_.NowTicks(),
base::BindOnce(&MetadataRecorder::CompareFrameWithExpected,
metadata_recorder.get())));
} }
encoder_.reset(); encoder_.reset();
...@@ -308,12 +311,12 @@ TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFramesAreDecodable) { ...@@ -308,12 +311,12 @@ TEST_F(H264VideoToolboxEncoderTest, DISABLED_CheckFramesAreDecodable) {
frame_->natural_size(), EmptyExtraData(), EncryptionScheme::kUnencrypted); frame_->natural_size(), EmptyExtraData(), EncryptionScheme::kUnencrypted);
scoped_refptr<EndToEndFrameChecker> checker(new EndToEndFrameChecker(config)); scoped_refptr<EndToEndFrameChecker> checker(new EndToEndFrameChecker(config));
VideoEncoder::FrameEncodedCallback cb =
base::Bind(&EndToEndFrameChecker::EncodeDone, checker.get());
for (FrameId frame_id = FrameId::first(); frame_id < FrameId::first() + 6; for (FrameId frame_id = FrameId::first(); frame_id < FrameId::first() + 6;
++frame_id) { ++frame_id) {
checker->PushExpectation(frame_); checker->PushExpectation(frame_);
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_TRUE(encoder_->EncodeVideoFrame(
frame_, clock_.NowTicks(),
base::BindOnce(&EndToEndFrameChecker::EncodeDone, checker.get())));
AdvanceClockAndVideoFrameTimestamp(); AdvanceClockAndVideoFrameTimestamp();
} }
...@@ -340,26 +343,28 @@ TEST_F(H264VideoToolboxEncoderTest, CheckVideoFrameFactory) { ...@@ -340,26 +343,28 @@ TEST_F(H264VideoToolboxEncoderTest, CheckVideoFrameFactory) {
TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoring) { TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoring) {
// Encode a frame, suspend, encode a frame, resume, encode a frame. // Encode a frame, suspend, encode a frame, resume, encode a frame.
VideoEncoder::FrameEncodedCallback cb = base::DoNothing(); EXPECT_TRUE(
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), base::DoNothing()));
power_source_->GenerateSuspendEvent(); power_source_->GenerateSuspendEvent();
EXPECT_FALSE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_FALSE(
encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), base::DoNothing()));
power_source_->GenerateResumeEvent(); power_source_->GenerateResumeEvent();
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_TRUE(
encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), base::DoNothing()));
} }
TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringNoInitialFrame) { TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringNoInitialFrame) {
// Suspend, encode a frame, resume, encode a frame. // Suspend, encode a frame, resume, encode a frame.
VideoEncoder::FrameEncodedCallback cb = base::DoNothing();
power_source_->GenerateSuspendEvent(); power_source_->GenerateSuspendEvent();
EXPECT_FALSE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_FALSE(
encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), base::DoNothing()));
power_source_->GenerateResumeEvent(); power_source_->GenerateResumeEvent();
EXPECT_TRUE(encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), cb)); EXPECT_TRUE(
encoder_->EncodeVideoFrame(frame_, clock_.NowTicks(), base::DoNothing()));
} }
TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringVideoFrameFactory) { TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringVideoFrameFactory) {
VideoEncoder::FrameEncodedCallback cb = base::DoNothing();
auto video_frame_factory = encoder_->CreateVideoFrameFactory(); auto video_frame_factory = encoder_->CreateVideoFrameFactory();
ASSERT_TRUE(video_frame_factory.get()); ASSERT_TRUE(video_frame_factory.get());
...@@ -389,7 +394,6 @@ TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringVideoFrameFactory) { ...@@ -389,7 +394,6 @@ TEST_F(H264VideoToolboxEncoderTest, CheckPowerMonitoringVideoFrameFactory) {
TEST_F(H264VideoToolboxEncoderTest, TEST_F(H264VideoToolboxEncoderTest,
CheckPowerMonitoringVideoFrameFactoryNoInitialFrame) { CheckPowerMonitoringVideoFrameFactoryNoInitialFrame) {
VideoEncoder::FrameEncodedCallback cb = base::DoNothing();
auto video_frame_factory = encoder_->CreateVideoFrameFactory(); auto video_frame_factory = encoder_->CreateVideoFrameFactory();
ASSERT_TRUE(video_frame_factory.get()); ASSERT_TRUE(video_frame_factory.get());
......
...@@ -34,8 +34,8 @@ SizeAdaptableVideoEncoderBase::~SizeAdaptableVideoEncoderBase() { ...@@ -34,8 +34,8 @@ SizeAdaptableVideoEncoderBase::~SizeAdaptableVideoEncoderBase() {
bool SizeAdaptableVideoEncoderBase::EncodeVideoFrame( bool SizeAdaptableVideoEncoderBase::EncodeVideoFrame(
scoped_refptr<media::VideoFrame> video_frame, scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& reference_time, base::TimeTicks reference_time,
const FrameEncodedCallback& frame_encoded_callback) { FrameEncodedCallback frame_encoded_callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
const gfx::Size frame_size = video_frame->visible_rect().size(); const gfx::Size frame_size = video_frame->visible_rect().size();
...@@ -56,24 +56,28 @@ bool SizeAdaptableVideoEncoderBase::EncodeVideoFrame( ...@@ -56,24 +56,28 @@ bool SizeAdaptableVideoEncoderBase::EncodeVideoFrame(
const bool is_frame_accepted = encoder_->EncodeVideoFrame( const bool is_frame_accepted = encoder_->EncodeVideoFrame(
std::move(video_frame), reference_time, std::move(video_frame), reference_time,
base::Bind(&SizeAdaptableVideoEncoderBase::OnEncodedVideoFrame, base::BindOnce(&SizeAdaptableVideoEncoderBase::OnEncodedVideoFrame,
weak_factory_.GetWeakPtr(), frame_encoded_callback)); weak_factory_.GetWeakPtr(),
if (is_frame_accepted) base::Passed(std::move(frame_encoded_callback))));
if (is_frame_accepted) {
++frames_in_encoder_; ++frames_in_encoder_;
}
return is_frame_accepted; return is_frame_accepted;
} }
void SizeAdaptableVideoEncoderBase::SetBitRate(int new_bit_rate) { void SizeAdaptableVideoEncoderBase::SetBitRate(int new_bit_rate) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
video_config_.start_bitrate = new_bit_rate; video_config_.start_bitrate = new_bit_rate;
if (encoder_) if (encoder_) {
encoder_->SetBitRate(new_bit_rate); encoder_->SetBitRate(new_bit_rate);
}
} }
void SizeAdaptableVideoEncoderBase::GenerateKeyFrame() { void SizeAdaptableVideoEncoderBase::GenerateKeyFrame() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (encoder_) if (encoder_) {
encoder_->GenerateKeyFrame(); encoder_->GenerateKeyFrame();
}
} }
std::unique_ptr<VideoFrameFactory> std::unique_ptr<VideoFrameFactory>
...@@ -84,8 +88,9 @@ SizeAdaptableVideoEncoderBase::CreateVideoFrameFactory() { ...@@ -84,8 +88,9 @@ SizeAdaptableVideoEncoderBase::CreateVideoFrameFactory() {
void SizeAdaptableVideoEncoderBase::EmitFrames() { void SizeAdaptableVideoEncoderBase::EmitFrames() {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
if (encoder_) if (encoder_) {
encoder_->EmitFrames(); encoder_->EmitFrames();
}
} }
StatusChangeCallback StatusChangeCallback
...@@ -117,12 +122,14 @@ void SizeAdaptableVideoEncoderBase::TrySpawningReplacementEncoder( ...@@ -117,12 +122,14 @@ void SizeAdaptableVideoEncoderBase::TrySpawningReplacementEncoder(
encoder_->EmitFrames(); encoder_->EmitFrames();
// Check again, since EmitFrames() is a synchronous operation for some // Check again, since EmitFrames() is a synchronous operation for some
// encoders. // encoders.
if (frames_in_encoder_ > 0) if (frames_in_encoder_ > 0) {
return; return;
}
} }
if (frames_in_encoder_ == kEncoderIsInitializing) if (frames_in_encoder_ == kEncoderIsInitializing) {
return; // Already spawned. return; // Already spawned.
}
DestroyEncoder(); DestroyEncoder();
frames_in_encoder_ = kEncoderIsInitializing; frames_in_encoder_ = kEncoderIsInitializing;
...@@ -148,15 +155,16 @@ void SizeAdaptableVideoEncoderBase::OnEncoderStatusChange( ...@@ -148,15 +155,16 @@ void SizeAdaptableVideoEncoderBase::OnEncoderStatusChange(
} }
void SizeAdaptableVideoEncoderBase::OnEncodedVideoFrame( void SizeAdaptableVideoEncoderBase::OnEncodedVideoFrame(
const FrameEncodedCallback& frame_encoded_callback, FrameEncodedCallback frame_encoded_callback,
std::unique_ptr<SenderEncodedFrame> encoded_frame) { std::unique_ptr<SenderEncodedFrame> encoded_frame) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
--frames_in_encoder_; --frames_in_encoder_;
DCHECK_GE(frames_in_encoder_, 0); DCHECK_GE(frames_in_encoder_, 0);
if (encoded_frame) if (encoded_frame) {
next_frame_id_ = encoded_frame->frame_id + 1; next_frame_id_ = encoded_frame->frame_id + 1;
frame_encoded_callback.Run(std::move(encoded_frame)); }
std::move(frame_encoded_callback).Run(std::move(encoded_frame));
} }
} // namespace cast } // namespace cast
......
...@@ -36,10 +36,9 @@ class SizeAdaptableVideoEncoderBase : public VideoEncoder { ...@@ -36,10 +36,9 @@ class SizeAdaptableVideoEncoderBase : public VideoEncoder {
~SizeAdaptableVideoEncoderBase() override; ~SizeAdaptableVideoEncoderBase() override;
// VideoEncoder implementation. // VideoEncoder implementation.
bool EncodeVideoFrame( bool EncodeVideoFrame(scoped_refptr<media::VideoFrame> video_frame,
scoped_refptr<media::VideoFrame> video_frame, base::TimeTicks reference_time,
const base::TimeTicks& reference_time, FrameEncodedCallback frame_encoded_callback) final;
const FrameEncodedCallback& frame_encoded_callback) final;
void SetBitRate(int new_bit_rate) final; void SetBitRate(int new_bit_rate) final;
void GenerateKeyFrame() final; void GenerateKeyFrame() final;
std::unique_ptr<VideoFrameFactory> CreateVideoFrameFactory() final; std::unique_ptr<VideoFrameFactory> CreateVideoFrameFactory() final;
...@@ -84,7 +83,7 @@ class SizeAdaptableVideoEncoderBase : public VideoEncoder { ...@@ -84,7 +83,7 @@ class SizeAdaptableVideoEncoderBase : public VideoEncoder {
void OnEncoderStatusChange(OperationalStatus status); void OnEncoderStatusChange(OperationalStatus status);
// Called by the |encoder_| with the next EncodedFrame. // Called by the |encoder_| with the next EncodedFrame.
void OnEncodedVideoFrame(const FrameEncodedCallback& frame_encoded_callback, void OnEncodedVideoFrame(FrameEncodedCallback frame_encoded_callback,
std::unique_ptr<SenderEncodedFrame> encoded_frame); std::unique_ptr<SenderEncodedFrame> encoded_frame);
const scoped_refptr<CastEnvironment> cast_environment_; const scoped_refptr<CastEnvironment> cast_environment_;
......
...@@ -25,7 +25,7 @@ class VideoEncoder { ...@@ -25,7 +25,7 @@ class VideoEncoder {
public: public:
// Callback used to deliver an encoded frame on the Cast MAIN thread. // Callback used to deliver an encoded frame on the Cast MAIN thread.
using FrameEncodedCallback = using FrameEncodedCallback =
base::Callback<void(std::unique_ptr<SenderEncodedFrame>)>; base::OnceCallback<void(std::unique_ptr<SenderEncodedFrame>)>;
// Creates a VideoEncoder instance from the given |video_config| and based on // Creates a VideoEncoder instance from the given |video_config| and based on
// the current platform's hardware/library support; or null if no // the current platform's hardware/library support; or null if no
...@@ -51,8 +51,8 @@ class VideoEncoder { ...@@ -51,8 +51,8 @@ class VideoEncoder {
// happens and the callback will not be run. // happens and the callback will not be run.
virtual bool EncodeVideoFrame( virtual bool EncodeVideoFrame(
scoped_refptr<media::VideoFrame> video_frame, scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& reference_time, base::TimeTicks reference_time,
const FrameEncodedCallback& frame_encoded_callback) = 0; FrameEncodedCallback frame_encoded_callback) = 0;
// Inform the encoder about the new target bit rate. // Inform the encoder about the new target bit rate.
virtual void SetBitRate(int new_bit_rate) = 0; virtual void SetBitRate(int new_bit_rate) = 0;
......
...@@ -30,9 +30,9 @@ void EncodeVideoFrameOnEncoderThread( ...@@ -30,9 +30,9 @@ void EncodeVideoFrameOnEncoderThread(
scoped_refptr<CastEnvironment> environment, scoped_refptr<CastEnvironment> environment,
SoftwareVideoEncoder* encoder, SoftwareVideoEncoder* encoder,
scoped_refptr<media::VideoFrame> video_frame, scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& reference_time, base::TimeTicks reference_time,
const VideoEncoderImpl::CodecDynamicConfig& dynamic_config, const VideoEncoderImpl::CodecDynamicConfig& dynamic_config,
const VideoEncoderImpl::FrameEncodedCallback& frame_encoded_callback) { VideoEncoderImpl::FrameEncodedCallback frame_encoded_callback) {
DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO)); DCHECK(environment->CurrentlyOn(CastEnvironment::VIDEO));
if (dynamic_config.key_frame_requested) { if (dynamic_config.key_frame_requested) {
encoder->GenerateKeyFrame(); encoder->GenerateKeyFrame();
...@@ -42,18 +42,18 @@ void EncodeVideoFrameOnEncoderThread( ...@@ -42,18 +42,18 @@ void EncodeVideoFrameOnEncoderThread(
std::unique_ptr<SenderEncodedFrame> encoded_frame(new SenderEncodedFrame()); std::unique_ptr<SenderEncodedFrame> encoded_frame(new SenderEncodedFrame());
encoder->Encode(std::move(video_frame), reference_time, encoded_frame.get()); encoder->Encode(std::move(video_frame), reference_time, encoded_frame.get());
encoded_frame->encode_completion_time = environment->Clock()->NowTicks(); encoded_frame->encode_completion_time = environment->Clock()->NowTicks();
environment->PostTask( environment->GetTaskRunner(CastEnvironment::MAIN)
CastEnvironment::MAIN, ->PostTask(FROM_HERE, base::BindOnce(std::move(frame_encoded_callback),
FROM_HERE, base::Passed(&encoded_frame)));
base::Bind(frame_encoded_callback, base::Passed(&encoded_frame)));
} }
} // namespace } // namespace
// static // static
bool VideoEncoderImpl::IsSupported(const FrameSenderConfig& video_config) { bool VideoEncoderImpl::IsSupported(const FrameSenderConfig& video_config) {
#ifndef OFFICIAL_BUILD #ifndef OFFICIAL_BUILD
if (video_config.codec == CODEC_VIDEO_FAKE) if (video_config.codec == CODEC_VIDEO_FAKE) {
return true; return true;
}
#endif #endif
return video_config.codec == CODEC_VIDEO_VP8; return video_config.codec == CODEC_VIDEO_VP8;
} }
...@@ -67,15 +67,13 @@ VideoEncoderImpl::VideoEncoderImpl( ...@@ -67,15 +67,13 @@ VideoEncoderImpl::VideoEncoderImpl(
DCHECK(status_change_cb); DCHECK(status_change_cb);
if (video_config.codec == CODEC_VIDEO_VP8) { if (video_config.codec == CODEC_VIDEO_VP8) {
encoder_.reset(new Vp8Encoder(video_config)); encoder_ = std::make_unique<Vp8Encoder>(video_config);
cast_environment_->PostTask(CastEnvironment::VIDEO, cast_environment_->PostTask(CastEnvironment::VIDEO, FROM_HERE,
FROM_HERE,
base::Bind(&InitializeEncoderOnEncoderThread, base::Bind(&InitializeEncoderOnEncoderThread,
cast_environment, cast_environment, encoder_.get()));
encoder_.get()));
#ifndef OFFICIAL_BUILD #ifndef OFFICIAL_BUILD
} else if (video_config.codec == CODEC_VIDEO_FAKE) { } else if (video_config.codec == CODEC_VIDEO_FAKE) {
encoder_.reset(new FakeSoftwareVideoEncoder(video_config)); encoder_ = std::make_unique<FakeSoftwareVideoEncoder>(video_config);
#endif #endif
} else { } else {
DCHECK(false) << "Invalid config"; // Codec not supported. DCHECK(false) << "Invalid config"; // Codec not supported.
...@@ -105,8 +103,8 @@ VideoEncoderImpl::~VideoEncoderImpl() { ...@@ -105,8 +103,8 @@ VideoEncoderImpl::~VideoEncoderImpl() {
bool VideoEncoderImpl::EncodeVideoFrame( bool VideoEncoderImpl::EncodeVideoFrame(
scoped_refptr<media::VideoFrame> video_frame, scoped_refptr<media::VideoFrame> video_frame,
const base::TimeTicks& reference_time, base::TimeTicks reference_time,
const FrameEncodedCallback& frame_encoded_callback) { FrameEncodedCallback frame_encoded_callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
DCHECK(!video_frame->visible_rect().IsEmpty()); DCHECK(!video_frame->visible_rect().IsEmpty());
DCHECK(!frame_encoded_callback.is_null()); DCHECK(!frame_encoded_callback.is_null());
...@@ -116,7 +114,7 @@ bool VideoEncoderImpl::EncodeVideoFrame( ...@@ -116,7 +114,7 @@ bool VideoEncoderImpl::EncodeVideoFrame(
base::BindRepeating(&EncodeVideoFrameOnEncoderThread, cast_environment_, base::BindRepeating(&EncodeVideoFrameOnEncoderThread, cast_environment_,
encoder_.get(), std::move(video_frame), encoder_.get(), std::move(video_frame),
reference_time, dynamic_config_, reference_time, dynamic_config_,
frame_encoded_callback)); base::Passed(std::move(frame_encoded_callback))));
dynamic_config_.key_frame_requested = false; dynamic_config_.key_frame_requested = false;
return true; return true;
......
...@@ -37,10 +37,9 @@ class VideoEncoderImpl : public VideoEncoder { ...@@ -37,10 +37,9 @@ class VideoEncoderImpl : public VideoEncoder {
~VideoEncoderImpl() final; ~VideoEncoderImpl() final;
// VideoEncoder implementation. // VideoEncoder implementation.
bool EncodeVideoFrame( bool EncodeVideoFrame(scoped_refptr<media::VideoFrame> video_frame,
scoped_refptr<media::VideoFrame> video_frame, base::TimeTicks reference_time,
const base::TimeTicks& reference_time, FrameEncodedCallback frame_encoded_callback) final;
const FrameEncodedCallback& frame_encoded_callback) final;
void SetBitRate(int new_bit_rate) final; void SetBitRate(int new_bit_rate) final;
void GenerateKeyFrame() final; void GenerateKeyFrame() final;
......
...@@ -254,8 +254,8 @@ void VideoSender::InsertRawVideoFrame( ...@@ -254,8 +254,8 @@ void VideoSender::InsertRawVideoFrame(
last_reported_lossy_utilization_, std::move(video_frame)); last_reported_lossy_utilization_, std::move(video_frame));
if (video_encoder_->EncodeVideoFrame( if (video_encoder_->EncodeVideoFrame(
frame_to_encode, reference_time, frame_to_encode, reference_time,
base::Bind(&VideoSender::OnEncodedVideoFrame, AsWeakPtr(), base::BindOnce(&VideoSender::OnEncodedVideoFrame, AsWeakPtr(),
frame_to_encode, bitrate))) { frame_to_encode, bitrate))) {
TRACE_EVENT_ASYNC_BEGIN1("cast.stream", "Video Encode", TRACE_EVENT_ASYNC_BEGIN1("cast.stream", "Video Encode",
frame_to_encode.get(), "rtp_timestamp", frame_to_encode.get(), "rtp_timestamp",
rtp_timestamp.lower_32_bits()); rtp_timestamp.lower_32_bits());
......
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