Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "media/cast/cast_environment.h" | 5 #include "media/cast/cast_environment.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/threading/thread.h" | |
| 11 #include "base/time/default_tick_clock.h" | |
| 10 | 12 |
| 11 using base::SingleThreadTaskRunner; | 13 using base::SingleThreadTaskRunner; |
| 12 | 14 |
| 13 namespace { | 15 namespace { |
| 14 | 16 |
| 15 void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) { | 17 void DeleteLoggingOnMainThread(scoped_ptr<media::cast::LoggingImpl> logging) { |
| 16 logging.reset(); | 18 logging.reset(); |
| 17 } | 19 } |
| 18 | 20 |
| 19 } // namespace | 21 } // namespace |
| 20 | 22 |
| 21 namespace media { | 23 namespace media { |
| 22 namespace cast { | 24 namespace cast { |
| 23 | 25 |
| 26 CastEnvironment::CastEnvironment() | |
| 27 : clock_(new base::DefaultTickClock()), | |
| 28 logging_(new LoggingImpl(CastLoggingConfig())) {} | |
| 29 | |
| 30 CastEnvironment::CastEnvironment(const CastLoggingConfig& logging_config) | |
| 31 : clock_(new base::DefaultTickClock()), | |
| 32 logging_(new LoggingImpl(logging_config)) {} | |
| 33 | |
| 24 CastEnvironment::CastEnvironment( | 34 CastEnvironment::CastEnvironment( |
| 25 scoped_ptr<base::TickClock> clock, | 35 scoped_ptr<base::TickClock> clock, |
| 26 scoped_refptr<SingleThreadTaskRunner> main_thread_proxy, | 36 scoped_refptr<SingleThreadTaskRunner> main_thread_proxy, |
| 27 scoped_refptr<SingleThreadTaskRunner> audio_encode_thread_proxy, | 37 scoped_refptr<SingleThreadTaskRunner> audio_encode_thread_proxy, |
| 28 scoped_refptr<SingleThreadTaskRunner> audio_decode_thread_proxy, | 38 scoped_refptr<SingleThreadTaskRunner> audio_decode_thread_proxy, |
| 29 scoped_refptr<SingleThreadTaskRunner> video_encode_thread_proxy, | 39 scoped_refptr<SingleThreadTaskRunner> video_encode_thread_proxy, |
| 30 scoped_refptr<SingleThreadTaskRunner> video_decode_thread_proxy, | 40 scoped_refptr<SingleThreadTaskRunner> video_decode_thread_proxy, |
| 31 scoped_refptr<SingleThreadTaskRunner> transport_thread_proxy, | 41 scoped_refptr<SingleThreadTaskRunner> transport_thread_proxy, |
| 32 const CastLoggingConfig& config) | 42 const CastLoggingConfig& logging_config) |
| 33 : clock_(clock.Pass()), | 43 : clock_(clock.Pass()), |
| 34 main_thread_proxy_(main_thread_proxy), | 44 main_thread_proxy_(main_thread_proxy), |
| 35 audio_encode_thread_proxy_(audio_encode_thread_proxy), | 45 audio_encode_thread_proxy_(audio_encode_thread_proxy), |
| 36 audio_decode_thread_proxy_(audio_decode_thread_proxy), | 46 audio_decode_thread_proxy_(audio_decode_thread_proxy), |
| 37 video_encode_thread_proxy_(video_encode_thread_proxy), | 47 video_encode_thread_proxy_(video_encode_thread_proxy), |
| 38 video_decode_thread_proxy_(video_decode_thread_proxy), | 48 video_decode_thread_proxy_(video_decode_thread_proxy), |
| 39 transport_thread_proxy_(transport_thread_proxy), | 49 transport_thread_proxy_(transport_thread_proxy), |
| 40 logging_(new LoggingImpl(main_thread_proxy, config)) { | 50 logging_(new LoggingImpl(logging_config)) { |
| 41 DCHECK(main_thread_proxy); | 51 DCHECK(main_thread_proxy); |
| 42 } | 52 } |
| 43 | 53 |
| 44 CastEnvironment::~CastEnvironment() { | 54 CastEnvironment::~CastEnvironment() { |
| 45 // Logging must be deleted on the main thread. | 55 // Logging must be deleted on the main thread. |
| 46 if (main_thread_proxy_->RunsTasksOnCurrentThread()) { | 56 if (main_thread_proxy_ && !main_thread_proxy_->RunsTasksOnCurrentThread()) { |
| 47 logging_.reset(); | |
| 48 } else { | |
| 49 main_thread_proxy_->PostTask( | 57 main_thread_proxy_->PostTask( |
| 50 FROM_HERE, | 58 FROM_HERE, |
| 51 base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_))); | 59 base::Bind(&DeleteLoggingOnMainThread, base::Passed(&logging_))); |
| 52 } | 60 } |
| 53 } | 61 } |
| 54 | 62 |
| 55 bool CastEnvironment::PostTask(ThreadId identifier, | 63 bool CastEnvironment::PostTask(ThreadId identifier, |
| 56 const tracked_objects::Location& from_here, | 64 const tracked_objects::Location& from_here, |
| 57 const base::Closure& task) { | 65 const base::Closure& task) { |
| 58 scoped_refptr<SingleThreadTaskRunner> task_runner = | 66 scoped_refptr<SingleThreadTaskRunner> task_runner = |
| 59 GetMessageSingleThreadTaskRunnerForThread(identifier); | 67 GetMessageSingleThreadTaskRunnerForThread(identifier); |
| 60 | 68 |
| 61 return task_runner->PostTask(from_here, task); | 69 return task_runner->PostTask(from_here, task); |
| 62 } | 70 } |
| 63 | 71 |
| 64 bool CastEnvironment::PostDelayedTask( | 72 bool CastEnvironment::PostDelayedTask( |
| 65 ThreadId identifier, | 73 ThreadId identifier, |
| 66 const tracked_objects::Location& from_here, | 74 const tracked_objects::Location& from_here, |
| 67 const base::Closure& task, | 75 const base::Closure& task, |
| 68 base::TimeDelta delay) { | 76 base::TimeDelta delay) { |
| 69 scoped_refptr<SingleThreadTaskRunner> task_runner = | 77 scoped_refptr<SingleThreadTaskRunner> task_runner = |
| 70 GetMessageSingleThreadTaskRunnerForThread(identifier); | 78 GetMessageSingleThreadTaskRunnerForThread(identifier); |
| 71 | 79 |
| 72 return task_runner->PostDelayedTask(from_here, task, delay); | 80 return task_runner->PostDelayedTask(from_here, task, delay); |
| 73 } | 81 } |
| 74 | 82 |
| 75 scoped_refptr<SingleThreadTaskRunner> | 83 scoped_refptr<SingleThreadTaskRunner> |
| 76 CastEnvironment::GetMessageSingleThreadTaskRunnerForThread( | 84 CastEnvironment::GetMessageSingleThreadTaskRunnerForThread( |
| 77 ThreadId identifier) { | 85 ThreadId identifier) { |
| 86 // 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.
| |
| 87 // on-demand. | |
| 88 #define AUTO_CREATE_TASK_RUNNER(name, options) \ | |
| 89 if (!name##_thread_proxy_) { \ | |
| 90 base::AutoLock auto_lock(create_lock_); \ | |
| 91 if (!name##_thread_proxy_) { \ | |
| 92 name##_thread_.reset(new base::Thread("CastEnvironment/" #name)); \ | |
| 93 name##_thread_->StartWithOptions(options); \ | |
| 94 name##_thread_proxy_ = name##_thread_->message_loop_proxy(); \ | |
| 95 } \ | |
| 96 } | |
| 97 | |
| 78 switch (identifier) { | 98 switch (identifier) { |
| 79 case CastEnvironment::MAIN: | 99 case CastEnvironment::MAIN: |
| 100 AUTO_CREATE_TASK_RUNNER( | |
| 101 main, base::Thread::Options(base::MessageLoop::TYPE_IO, 0)); | |
| 80 return main_thread_proxy_; | 102 return main_thread_proxy_; |
| 81 case CastEnvironment::AUDIO_ENCODER: | 103 case CastEnvironment::AUDIO_ENCODER: |
| 104 AUTO_CREATE_TASK_RUNNER(audio_encode, base::Thread::Options()); | |
| 82 return audio_encode_thread_proxy_; | 105 return audio_encode_thread_proxy_; |
| 83 case CastEnvironment::AUDIO_DECODER: | 106 case CastEnvironment::AUDIO_DECODER: |
| 107 AUTO_CREATE_TASK_RUNNER(audio_decode, base::Thread::Options()); | |
| 84 return audio_decode_thread_proxy_; | 108 return audio_decode_thread_proxy_; |
| 85 case CastEnvironment::VIDEO_ENCODER: | 109 case CastEnvironment::VIDEO_ENCODER: |
| 110 AUTO_CREATE_TASK_RUNNER(video_encode, base::Thread::Options()); | |
| 86 return video_encode_thread_proxy_; | 111 return video_encode_thread_proxy_; |
| 87 case CastEnvironment::VIDEO_DECODER: | 112 case CastEnvironment::VIDEO_DECODER: |
| 113 AUTO_CREATE_TASK_RUNNER(video_decode, base::Thread::Options()); | |
| 88 return video_decode_thread_proxy_; | 114 return video_decode_thread_proxy_; |
| 89 case CastEnvironment::TRANSPORT: | 115 case CastEnvironment::TRANSPORT: |
| 116 AUTO_CREATE_TASK_RUNNER(transport, base::Thread::Options()); | |
| 90 return transport_thread_proxy_; | 117 return transport_thread_proxy_; |
| 91 default: | 118 default: |
| 92 NOTREACHED() << "Invalid Thread identifier"; | 119 NOTREACHED() << "Invalid Thread identifier"; |
| 93 return NULL; | 120 return NULL; |
| 94 } | 121 } |
| 122 | |
| 123 #undef AUTO_CREATE_TASK_RUNNER | |
| 95 } | 124 } |
| 96 | 125 |
| 97 bool CastEnvironment::CurrentlyOn(ThreadId identifier) { | 126 bool CastEnvironment::CurrentlyOn(ThreadId identifier) { |
| 98 switch (identifier) { | 127 switch (identifier) { |
| 99 case CastEnvironment::MAIN: | 128 case CastEnvironment::MAIN: |
| 100 return main_thread_proxy_->RunsTasksOnCurrentThread(); | 129 return main_thread_proxy_ && |
| 130 main_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 101 case CastEnvironment::AUDIO_ENCODER: | 131 case CastEnvironment::AUDIO_ENCODER: |
| 102 return audio_encode_thread_proxy_->RunsTasksOnCurrentThread(); | 132 return audio_encode_thread_proxy_ && |
| 133 audio_encode_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 103 case CastEnvironment::AUDIO_DECODER: | 134 case CastEnvironment::AUDIO_DECODER: |
| 104 return audio_decode_thread_proxy_->RunsTasksOnCurrentThread(); | 135 return audio_decode_thread_proxy_ && |
| 136 audio_decode_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 105 case CastEnvironment::VIDEO_ENCODER: | 137 case CastEnvironment::VIDEO_ENCODER: |
| 106 return video_encode_thread_proxy_->RunsTasksOnCurrentThread(); | 138 return video_encode_thread_proxy_ && |
| 139 video_encode_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 107 case CastEnvironment::VIDEO_DECODER: | 140 case CastEnvironment::VIDEO_DECODER: |
| 108 return video_decode_thread_proxy_->RunsTasksOnCurrentThread(); | 141 return video_decode_thread_proxy_ && |
| 142 video_decode_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 109 case CastEnvironment::TRANSPORT: | 143 case CastEnvironment::TRANSPORT: |
| 110 return transport_thread_proxy_->RunsTasksOnCurrentThread(); | 144 return transport_thread_proxy_ && |
| 145 transport_thread_proxy_->RunsTasksOnCurrentThread(); | |
| 111 default: | 146 default: |
| 112 NOTREACHED() << "Invalid thread identifier"; | 147 NOTREACHED() << "Invalid thread identifier"; |
| 113 return false; | 148 return false; |
| 114 } | 149 } |
| 115 } | 150 } |
| 116 | 151 |
| 117 base::TickClock* CastEnvironment::Clock() const { return clock_.get(); } | |
| 118 | |
| 119 LoggingImpl* CastEnvironment::Logging() { | |
| 120 DCHECK(CurrentlyOn(CastEnvironment::MAIN)) | |
| 121 << "Must be called from main thread"; | |
| 122 return logging_.get(); | |
| 123 } | |
| 124 | |
| 125 } // namespace cast | 152 } // namespace cast |
| 126 } // namespace media | 153 } // namespace media |
| OLD | NEW |