Skip to content

Commit

Permalink
Cast: Reduce number of threads in cast environment.
Browse files Browse the repository at this point in the history
Since encoding and decoding rarely or never use the same cast environment,
there is no need to have specific threads for encode and decode.

Review URL: https://codereview.chromium.org/206493004

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@258650 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
hubbe@chromium.org committed Mar 21, 2014
1 parent d173a2c commit 0e2c717
Show file tree
Hide file tree
Showing 31 changed files with 77 additions and 132 deletions.
3 changes: 0 additions & 3 deletions chrome/renderer/media/cast_session_delegate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,11 @@ void CastSessionDelegate::StartUDP(const net::IPEndPoint& remote_endpoint) {

// CastSender uses the renderer's IO thread as the main thread. This reduces
// thread hopping for incoming video frames and outgoing network packets.
// There's no need to decode so no thread assigned for decoding.
cast_environment_ = new CastEnvironment(
scoped_ptr<base::TickClock>(new base::DefaultTickClock()).Pass(),
base::MessageLoopProxy::current(),
g_cast_threads.Get().GetAudioEncodeMessageLoopProxy(),
NULL,
g_cast_threads.Get().GetVideoEncodeMessageLoopProxy(),
NULL,
base::MessageLoopProxy::current(),
logging_config);

Expand Down
2 changes: 1 addition & 1 deletion media/cast/audio_receiver/audio_decoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ bool AudioDecoder::GetRawAudioFrame(int number_of_10ms_blocks,
int desired_frequency,
PcmAudioFrame* audio_frame,
uint32* rtp_timestamp) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_DECODER));
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO));
// We don't care about the race case where a packet arrives at the same time
// as this function in called. The data will be there the next time this
// function is called.
Expand Down
2 changes: 0 additions & 2 deletions media/cast/audio_receiver/audio_decoder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@ class AudioDecoderTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetDefaultCastReceiverLoggingConfig());
}
virtual ~AudioDecoderTest() {}
Expand Down
6 changes: 3 additions & 3 deletions media/cast/audio_receiver/audio_receiver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ void AudioReceiver::IncomingParsedRtpPacket(const uint8* payload_data,
DecodedAudioCallbackData decoded_data = queued_decoded_callbacks_.front();
queued_decoded_callbacks_.pop_front();
cast_environment_->PostTask(
CastEnvironment::AUDIO_DECODER, FROM_HERE,
CastEnvironment::AUDIO, FROM_HERE,
base::Bind(&AudioReceiver::DecodeAudioFrameThread,
base::Unretained(this), decoded_data.number_of_10ms_blocks,
decoded_data.desired_frequency, decoded_data.callback));
Expand Down Expand Up @@ -215,15 +215,15 @@ void AudioReceiver::GetRawAudioFrame(
DCHECK(audio_decoder_) << "Invalid function call in this configuration";
// TODO(pwestin): we can skip this function by posting direct to the decoder.
cast_environment_->PostTask(
CastEnvironment::AUDIO_DECODER, FROM_HERE,
CastEnvironment::AUDIO, FROM_HERE,
base::Bind(&AudioReceiver::DecodeAudioFrameThread, base::Unretained(this),
number_of_10ms_blocks, desired_frequency, callback));
}

void AudioReceiver::DecodeAudioFrameThread(
int number_of_10ms_blocks, int desired_frequency,
const AudioFrameDecodedCallback callback) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO_DECODER));
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::AUDIO));
// TODO(mikhal): Allow the application to allocate this memory.
scoped_ptr<PcmAudioFrame> audio_frame(new PcmAudioFrame());

Expand Down
7 changes: 5 additions & 2 deletions media/cast/audio_receiver/audio_receiver_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,11 @@ class AudioReceiverTest : public ::testing::Test {
logging_config.enable_raw_data_collection = true;

cast_environment_ = new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(), task_runner_,
task_runner_, task_runner_, task_runner_, task_runner_, task_runner_,
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_,
task_runner_,
logging_config);

test_audio_encoder_callback_ = new TestAudioEncoderCallback();
Expand Down
2 changes: 1 addition & 1 deletion media/cast/audio_sender/audio_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ void AudioEncoder::InsertAudio(scoped_ptr<AudioBus> audio_bus,
NOTREACHED();
return;
}
cast_environment_->PostTask(CastEnvironment::AUDIO_ENCODER,
cast_environment_->PostTask(CastEnvironment::AUDIO,
FROM_HERE,
base::Bind(&AudioEncoder::ImplBase::EncodeAudio,
impl_,
Expand Down
2 changes: 0 additions & 2 deletions media/cast/audio_sender/audio_encoder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ class AudioEncoderTest : public ::testing::TestWithParam<TestScenario> {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetDefaultCastSenderLoggingConfig());
}

Expand Down
2 changes: 0 additions & 2 deletions media/cast/audio_sender/audio_sender_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ class AudioSenderTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
logging_config);
audio_config_.codec = transport::kOpus;
audio_config_.use_external_encoder = false;
Expand Down
42 changes: 14 additions & 28 deletions media/cast/cast_environment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,13 @@ namespace cast {
CastEnvironment::CastEnvironment(
scoped_ptr<base::TickClock> clock,
scoped_refptr<SingleThreadTaskRunner> main_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> audio_encode_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> audio_decode_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> video_encode_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> video_decode_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> audio_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> video_thread_proxy,
scoped_refptr<SingleThreadTaskRunner> transport_thread_proxy,
const CastLoggingConfig& logging_config)
: main_thread_proxy_(main_thread_proxy),
audio_encode_thread_proxy_(audio_encode_thread_proxy),
audio_decode_thread_proxy_(audio_decode_thread_proxy),
video_encode_thread_proxy_(video_encode_thread_proxy),
video_decode_thread_proxy_(video_decode_thread_proxy),
audio_thread_proxy_(audio_thread_proxy),
video_thread_proxy_(video_thread_proxy),
transport_thread_proxy_(transport_thread_proxy),
clock_(clock.Pass()),
logging_(new LoggingImpl(logging_config)) {}
Expand Down Expand Up @@ -67,14 +63,10 @@ scoped_refptr<SingleThreadTaskRunner> CastEnvironment::GetTaskRunner(
switch (identifier) {
case CastEnvironment::MAIN:
return main_thread_proxy_;
case CastEnvironment::AUDIO_ENCODER:
return audio_encode_thread_proxy_;
case CastEnvironment::AUDIO_DECODER:
return audio_decode_thread_proxy_;
case CastEnvironment::VIDEO_ENCODER:
return video_encode_thread_proxy_;
case CastEnvironment::VIDEO_DECODER:
return video_decode_thread_proxy_;
case CastEnvironment::AUDIO:
return audio_thread_proxy_;
case CastEnvironment::VIDEO:
return video_thread_proxy_;
case CastEnvironment::TRANSPORT:
return transport_thread_proxy_;
default:
Expand All @@ -88,18 +80,12 @@ bool CastEnvironment::CurrentlyOn(ThreadId identifier) {
case CastEnvironment::MAIN:
return main_thread_proxy_ &&
main_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::AUDIO_ENCODER:
return audio_encode_thread_proxy_ &&
audio_encode_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::AUDIO_DECODER:
return audio_decode_thread_proxy_ &&
audio_decode_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::VIDEO_ENCODER:
return video_encode_thread_proxy_ &&
video_encode_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::VIDEO_DECODER:
return video_decode_thread_proxy_ &&
video_decode_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::AUDIO:
return audio_thread_proxy_ &&
audio_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::VIDEO:
return video_thread_proxy_ &&
video_thread_proxy_->RunsTasksOnCurrentThread();
case CastEnvironment::TRANSPORT:
return transport_thread_proxy_ &&
transport_thread_proxy_->RunsTasksOnCurrentThread();
Expand Down
36 changes: 14 additions & 22 deletions media/cast/cast_environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,20 @@ class CastEnvironment : public base::RefCountedThreadSafe<CastEnvironment> {
// The main thread is where the cast system is configured and where timers
// and network IO is performed.
MAIN,
// The audio encoder thread is where all send side audio processing is done,
// primarily encoding but also re-sampling.
AUDIO_ENCODER,
// The audio decoder thread is where all receive side audio processing is
// done, primarily decoding but also error concealment and re-sampling.
AUDIO_DECODER,
// The video encoder thread is where the video encode processing is done.
VIDEO_ENCODER,
// The video decoder thread is where the video decode processing is done.
VIDEO_DECODER,
// The audio thread is where all send side audio processing is done,
// primarily encoding / decoding but also re-sampling.
AUDIO,
// The video encoder thread is where the video processing is done.
VIDEO,
// The transport thread is where the transport processing is done.
TRANSPORT,
};

CastEnvironment(
scoped_ptr<base::TickClock> clock,
scoped_refptr<base::SingleThreadTaskRunner> main_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> audio_encode_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> audio_decode_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> video_encode_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> video_decode_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> audio_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> video_thread_proxy,
scoped_refptr<base::SingleThreadTaskRunner> transport_thread_proxy,
const CastLoggingConfig& logging_config);

Expand All @@ -69,28 +62,27 @@ class CastEnvironment : public base::RefCountedThreadSafe<CastEnvironment> {

// Logging is not thread safe. Its methods should always be called from the
// main thread.
// TODO(hubbe): Logging should be a thread-safe interface.
LoggingImpl* Logging() const { return logging_.get(); }

scoped_refptr<base::SingleThreadTaskRunner> GetTaskRunner(
ThreadId identifier) const;

bool HasAudioEncoderThread() {
return audio_encode_thread_proxy_ ? true : false;
bool HasAudioThread() {
return audio_thread_proxy_ ? true : false;
}

bool HasVideoEncoderThread() {
return video_encode_thread_proxy_ ? true : false;
bool HasVideoThread() {
return video_thread_proxy_ ? true : false;
}

protected:
virtual ~CastEnvironment();

// Subclasses may override these.
scoped_refptr<base::SingleThreadTaskRunner> main_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> audio_encode_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> audio_decode_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> video_encode_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> video_decode_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> audio_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> video_thread_proxy_;
scoped_refptr<base::SingleThreadTaskRunner> transport_thread_proxy_;

private:
Expand Down
4 changes: 2 additions & 2 deletions media/cast/cast_sender_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ void CastSenderImpl::InitializeAudio(
const CastInitializationCallback& cast_initialization_cb) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
CHECK(audio_config.use_external_encoder ||
cast_environment_->HasAudioEncoderThread());
cast_environment_->HasAudioThread());

audio_sender_.reset(
new AudioSender(cast_environment_, audio_config, transport_sender_));
Expand All @@ -116,7 +116,7 @@ void CastSenderImpl::InitializeVideo(
const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories) {
DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN));
CHECK(video_config.use_external_encoder ||
cast_environment_->HasVideoEncoderThread());
cast_environment_->HasVideoThread());

video_sender_.reset(new VideoSender(cast_environment_,
video_config,
Expand Down
2 changes: 0 additions & 2 deletions media/cast/logging/encoding_event_subscriber_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ class EncodingEventSubscriberTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetLoggingConfigWithRawEventsAndStatsEnabled())),
first_rtp_timestamp_(0) {}

Expand Down
9 changes: 6 additions & 3 deletions media/cast/logging/simple_event_subscriber_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ class SimpleEventSubscriberTest : public ::testing::Test {
: testing_clock_(new base::SimpleTestTickClock()),
task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
cast_environment_(new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(), task_runner_,
task_runner_, task_runner_, task_runner_, task_runner_,
task_runner_, GetLoggingConfigWithRawEventsAndStatsEnabled())) {
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetLoggingConfigWithRawEventsAndStatsEnabled())) {
cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
}

Expand Down
9 changes: 6 additions & 3 deletions media/cast/rtcp/receiver_rtcp_event_subscriber_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ class ReceiverRtcpEventSubscriberTest : public ::testing::Test {
: testing_clock_(new base::SimpleTestTickClock()),
task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
cast_environment_(new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(), task_runner_,
task_runner_, task_runner_, task_runner_, task_runner_,
task_runner_, GetLoggingConfigWithRawEventsAndStatsEnabled())) {}
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetLoggingConfigWithRawEventsAndStatsEnabled())) {}

virtual ~ReceiverRtcpEventSubscriberTest() {}

Expand Down
2 changes: 0 additions & 2 deletions media/cast/rtcp/rtcp_receiver_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,6 @@ class RtcpReceiverTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetDefaultCastReceiverLoggingConfig())),
rtcp_receiver_(new RtcpReceiver(cast_environment_,
&mock_sender_feedback_,
Expand Down
2 changes: 0 additions & 2 deletions media/cast/rtcp/rtcp_sender_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ class RtcpSenderTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetDefaultCastSenderLoggingConfig())),
rtcp_sender_(new RtcpSender(cast_environment_,
&test_transport_,
Expand Down
2 changes: 0 additions & 2 deletions media/cast/rtcp/rtcp_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,6 @@ class RtcpTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
logging_config_)),
sender_to_receiver_(testing_clock_),
receiver_to_sender_(cast_environment_, testing_clock_),
Expand Down
9 changes: 6 additions & 3 deletions media/cast/rtcp/sender_rtcp_event_subscriber_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@ class SenderRtcpEventSubscriberTest : public ::testing::Test {
: testing_clock_(new base::SimpleTestTickClock()),
task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
cast_environment_(new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(), task_runner_,
task_runner_, task_runner_, task_runner_, task_runner_,
task_runner_, GetLoggingConfigWithRawEventsAndStatsEnabled())),
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
task_runner_,
task_runner_,
task_runner_,
GetLoggingConfigWithRawEventsAndStatsEnabled())),
event_subscriber_(kMaxEventEntries) {
cast_environment_->Logging()->AddRawEventSubscriber(&event_subscriber_);
}
Expand Down
4 changes: 1 addition & 3 deletions media/cast/test/encode_decode_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ class EncodeDecodeTest : public ::testing::Test {
: testing_clock_(new base::SimpleTestTickClock()),
task_runner_(new test::FakeSingleThreadTaskRunner(testing_clock_)),
// CastEnvironment will only be used by the vp8 decoder; Enable only the
// video decoder and main threads.
// video and main threads.
cast_environment_(new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_).Pass(),
task_runner_,
NULL,
NULL,
NULL,
task_runner_,
NULL,
GetDefaultCastReceiverLoggingConfig())),
Expand Down
4 changes: 0 additions & 4 deletions media/cast/test/end2end_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -409,17 +409,13 @@ class End2EndTest : public ::testing::Test {
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
logging_config_)),
cast_environment_receiver_(new CastEnvironment(
scoped_ptr<base::TickClock>(testing_clock_receiver_).Pass(),
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
task_runner_,
logging_config_)),
receiver_to_sender_(cast_environment_receiver_),
sender_to_receiver_(cast_environment_sender_),
Expand Down
4 changes: 1 addition & 3 deletions media/cast/test/sender.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,16 +467,14 @@ int main(int argc, char** argv) {
transport_video_config.base.ssrc = video_config.sender_ssrc;
transport_video_config.base.rtp_config = video_config.rtp_config;

// Enable main and send side threads only. Enable raw event and stats logging.
// Enable raw event and stats logging.
// Running transport on the main thread.
scoped_refptr<media::cast::CastEnvironment> cast_environment(
new media::cast::CastEnvironment(
make_scoped_ptr<base::TickClock>(new base::DefaultTickClock()),
io_message_loop.message_loop_proxy(),
audio_thread.message_loop_proxy(),
NULL,
video_thread.message_loop_proxy(),
NULL,
io_message_loop.message_loop_proxy(),
media::cast::GetLoggingConfigWithRawEventsAndStatsEnabled()));

Expand Down
Loading

0 comments on commit 0e2c717

Please sign in to comment.