Chromium Code Reviews| Index: media/cast/cast_environment.cc |
| diff --git a/media/cast/cast_environment.cc b/media/cast/cast_environment.cc |
| index 9b1a5696486d5c85187121c5bf7c95ea647ab4e6..f3e18507e63bb6f397081df0903889ebf7792e57 100644 |
| --- a/media/cast/cast_environment.cc |
| +++ b/media/cast/cast_environment.cc |
| @@ -7,6 +7,8 @@ |
| #include "base/bind.h" |
| #include "base/location.h" |
| #include "base/logging.h" |
| +#include "base/threading/thread.h" |
| +#include "base/time/default_tick_clock.h" |
| using base::SingleThreadTaskRunner; |
| @@ -21,6 +23,14 @@ void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) { |
| namespace media { |
| namespace cast { |
| +CastEnvironment::CastEnvironment() |
| + : clock_(new base::DefaultTickClock()), |
| + logging_(new LoggingImpl(CastLoggingConfig())) {} |
| + |
| +CastEnvironment::CastEnvironment(const CastLoggingConfig& logging_config) |
| + : clock_(new base::DefaultTickClock()), |
| + logging_(new LoggingImpl(logging_config)) {} |
| + |
| CastEnvironment::CastEnvironment( |
| scoped_ptr<base::TickClock> clock, |
| scoped_refptr<SingleThreadTaskRunner> main_thread_proxy, |
| @@ -29,7 +39,7 @@ CastEnvironment::CastEnvironment( |
| scoped_refptr<SingleThreadTaskRunner> video_encode_thread_proxy, |
| scoped_refptr<SingleThreadTaskRunner> video_decode_thread_proxy, |
| scoped_refptr<SingleThreadTaskRunner> transport_thread_proxy, |
| - const CastLoggingConfig& config) |
| + const CastLoggingConfig& logging_config) |
| : clock_(clock.Pass()), |
| main_thread_proxy_(main_thread_proxy), |
| audio_encode_thread_proxy_(audio_encode_thread_proxy), |
| @@ -37,15 +47,13 @@ CastEnvironment::CastEnvironment( |
| video_encode_thread_proxy_(video_encode_thread_proxy), |
| video_decode_thread_proxy_(video_decode_thread_proxy), |
| transport_thread_proxy_(transport_thread_proxy), |
| - logging_(new LoggingImpl(main_thread_proxy, config)) { |
| + logging_(new LoggingImpl(logging_config)) { |
| DCHECK(main_thread_proxy); |
| } |
| CastEnvironment::~CastEnvironment() { |
| // Logging must be deleted on the main thread. |
| - if (main_thread_proxy_->RunsTasksOnCurrentThread()) { |
| - logging_.reset(); |
| - } else { |
| + if (main_thread_proxy_ && !main_thread_proxy_->RunsTasksOnCurrentThread()) { |
| main_thread_proxy_->PostTask( |
| FROM_HERE, |
| base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_))); |
| @@ -75,52 +83,71 @@ bool CastEnvironment::PostDelayedTask( |
| scoped_refptr<SingleThreadTaskRunner> |
| CastEnvironment::GetMessageSingleThreadTaskRunnerForThread( |
| ThreadId identifier) { |
| +// A convenience macro to efficiently check and create a missing task runner |
|
hubbe
2014/03/04 22:42:26
I don't think this is a good idea.
I think it woul
miu
2014/03/06 06:09:15
Done.
|
| +// on-demand. |
| +#define AUTO_CREATE_TASK_RUNNER(name, options) \ |
| + if (!name##_thread_proxy_) { \ |
| + base::AutoLock auto_lock(create_lock_); \ |
| + if (!name##_thread_proxy_) { \ |
| + name##_thread_.reset(new base::Thread("CastEnvironment/" #name)); \ |
| + name##_thread_->StartWithOptions(options); \ |
| + name##_thread_proxy_ = name##_thread_->message_loop_proxy(); \ |
| + } \ |
| + } |
| + |
| switch (identifier) { |
| case CastEnvironment::MAIN: |
| + AUTO_CREATE_TASK_RUNNER( |
| + main, base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); |
| return main_thread_proxy_; |
| case CastEnvironment::AUDIO_ENCODER: |
| + AUTO_CREATE_TASK_RUNNER(audio_encode, base::Thread::Options()); |
| return audio_encode_thread_proxy_; |
| case CastEnvironment::AUDIO_DECODER: |
| + AUTO_CREATE_TASK_RUNNER(audio_decode, base::Thread::Options()); |
| return audio_decode_thread_proxy_; |
| case CastEnvironment::VIDEO_ENCODER: |
| + AUTO_CREATE_TASK_RUNNER(video_encode, base::Thread::Options()); |
| return video_encode_thread_proxy_; |
| case CastEnvironment::VIDEO_DECODER: |
| + AUTO_CREATE_TASK_RUNNER(video_decode, base::Thread::Options()); |
| return video_decode_thread_proxy_; |
| case CastEnvironment::TRANSPORT: |
| + AUTO_CREATE_TASK_RUNNER(transport, base::Thread::Options()); |
| return transport_thread_proxy_; |
| default: |
| NOTREACHED() << "Invalid Thread identifier"; |
| return NULL; |
| } |
| + |
| +#undef AUTO_CREATE_TASK_RUNNER |
| } |
| bool CastEnvironment::CurrentlyOn(ThreadId identifier) { |
| switch (identifier) { |
| case CastEnvironment::MAIN: |
| - return main_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return main_thread_proxy_ && |
| + main_thread_proxy_->RunsTasksOnCurrentThread(); |
| case CastEnvironment::AUDIO_ENCODER: |
| - return audio_encode_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return audio_encode_thread_proxy_ && |
| + audio_encode_thread_proxy_->RunsTasksOnCurrentThread(); |
| case CastEnvironment::AUDIO_DECODER: |
| - return audio_decode_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return audio_decode_thread_proxy_ && |
| + audio_decode_thread_proxy_->RunsTasksOnCurrentThread(); |
| case CastEnvironment::VIDEO_ENCODER: |
| - return video_encode_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return video_encode_thread_proxy_ && |
| + video_encode_thread_proxy_->RunsTasksOnCurrentThread(); |
| case CastEnvironment::VIDEO_DECODER: |
| - return video_decode_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return video_decode_thread_proxy_ && |
| + video_decode_thread_proxy_->RunsTasksOnCurrentThread(); |
| case CastEnvironment::TRANSPORT: |
| - return transport_thread_proxy_->RunsTasksOnCurrentThread(); |
| + return transport_thread_proxy_ && |
| + transport_thread_proxy_->RunsTasksOnCurrentThread(); |
| default: |
| NOTREACHED() << "Invalid thread identifier"; |
| return false; |
| } |
| } |
| -base::TickClock* CastEnvironment::Clock() const { return clock_.get(); } |
| - |
| -LoggingImpl* CastEnvironment::Logging() { |
| - DCHECK(CurrentlyOn(CastEnvironment::MAIN)) |
| - << "Must be called from main thread"; |
| - return logging_.get(); |
| -} |
| - |
| } // namespace cast |
| } // namespace media |