| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "remoting/host/chromoting_host_context.h" | 5 #include "remoting/host/chromoting_host_context.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "remoting/base/auto_thread.h" | 10 #include "base/threading/thread.h" |
| 11 #include "remoting/base/auto_thread_task_runner.h" |
| 11 #include "remoting/host/url_request_context.h" | 12 #include "remoting/host/url_request_context.h" |
| 12 | 13 |
| 13 namespace remoting { | 14 namespace remoting { |
| 14 | 15 |
| 15 ChromotingHostContext::ChromotingHostContext( | 16 ChromotingHostContext::ChromotingHostContext( |
| 16 AutoThreadTaskRunner* ui_task_runner) | 17 scoped_refptr<AutoThreadTaskRunner> ui_task_runner) |
| 17 : ui_task_runner_(ui_task_runner) { | 18 : audio_thread_("ChromotingAudioThread"), |
| 18 #if defined(OS_WIN) | 19 video_capture_thread_("ChromotingCaptureThread"), |
| 19 // On Windows the AudioCapturer requires COM, so we run a single-threaded | 20 video_encode_thread_("ChromotingEncodeThread"), |
| 20 // apartment, which requires a UI thread. | 21 file_thread_("ChromotingFileIOThread"), |
| 21 audio_task_runner_ = AutoThread::CreateWithLoopAndComInitTypes( | 22 input_thread_("ChromotingInputThread"), |
| 22 "ChromotingAudioThread", ui_task_runner_, MessageLoop::TYPE_UI, | 23 network_thread_("ChromotingNetworkThread"), |
| 23 AutoThread::COM_INIT_STA); | 24 ui_task_runner_(ui_task_runner) { |
| 24 #else // !defined(OS_WIN) | |
| 25 audio_task_runner_ = AutoThread::CreateWithType( | |
| 26 "ChromotingAudioThread", ui_task_runner_, MessageLoop::TYPE_IO); | |
| 27 #endif // !defined(OS_WIN) | |
| 28 | |
| 29 video_capture_task_runner_ = AutoThread::Create( | |
| 30 "ChromotingCaptureThread", ui_task_runner_); | |
| 31 video_encode_task_runner_ = AutoThread::Create( | |
| 32 "ChromotingEncodeThread", ui_task_runner_); | |
| 33 file_task_runner_ = AutoThread::CreateWithType( | |
| 34 "ChromotingFileThread", ui_task_runner_, MessageLoop::TYPE_IO); | |
| 35 input_task_runner_ = AutoThread::CreateWithType( | |
| 36 "ChromotingInputThread", ui_task_runner_, MessageLoop::TYPE_IO); | |
| 37 network_task_runner_ = AutoThread::CreateWithType( | |
| 38 "ChromotingNetworkThread", ui_task_runner_, MessageLoop::TYPE_IO); | |
| 39 | |
| 40 url_request_context_getter_ = new URLRequestContextGetter( | |
| 41 ui_task_runner_, network_task_runner_); | |
| 42 } | 25 } |
| 43 | 26 |
| 44 ChromotingHostContext::~ChromotingHostContext() { | 27 ChromotingHostContext::~ChromotingHostContext() { |
| 45 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 46 } | 28 } |
| 47 | 29 |
| 48 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Create( | 30 bool ChromotingHostContext::Start() { |
| 49 scoped_refptr<AutoThreadTaskRunner> ui_task_runner) { | 31 // Start all the threads. |
| 50 DCHECK(ui_task_runner->BelongsToCurrentThread()); | 32 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); |
| 51 | 33 |
| 52 scoped_ptr<ChromotingHostContext> context( | 34 bool started = video_capture_thread_.Start() && video_encode_thread_.Start(); |
| 53 new ChromotingHostContext(ui_task_runner)); | |
| 54 if (!context->audio_task_runner_ || | |
| 55 !context->video_capture_task_runner_ || | |
| 56 !context->video_encode_task_runner_ || | |
| 57 !context->file_task_runner_ || | |
| 58 !context->input_task_runner_ || | |
| 59 !context->network_task_runner_ || | |
| 60 !context->url_request_context_getter_) { | |
| 61 context.reset(); | |
| 62 } | |
| 63 | 35 |
| 64 return context.Pass(); | 36 #if defined(OS_WIN) |
| 37 // On Windows audio capturer needs to run on a UI thread that has COM |
| 38 // initialized. |
| 39 audio_thread_.init_com_with_mta(false); |
| 40 started = started && audio_thread_.Start(); |
| 41 #else // defined(OS_WIN) |
| 42 started = started && audio_thread_.StartWithOptions(io_thread_options); |
| 43 #endif // !defined(OS_WIN) |
| 44 |
| 45 started = started && |
| 46 file_thread_.StartWithOptions(io_thread_options) && |
| 47 input_thread_.StartWithOptions(io_thread_options) && |
| 48 network_thread_.StartWithOptions(io_thread_options); |
| 49 if (!started) |
| 50 return false; |
| 51 |
| 52 // Wrap worker threads with |AutoThreadTaskRunner| and have them reference |
| 53 // the main thread via |ui_task_runner_|, to ensure that it remain active to |
| 54 // Stop() them when no references remain. |
| 55 audio_task_runner_ = |
| 56 new AutoThreadTaskRunner(audio_thread_.message_loop_proxy(), |
| 57 ui_task_runner_); |
| 58 video_capture_task_runner_ = |
| 59 new AutoThreadTaskRunner(video_capture_thread_.message_loop_proxy(), |
| 60 ui_task_runner_); |
| 61 video_encode_task_runner_ = |
| 62 new AutoThreadTaskRunner(video_encode_thread_.message_loop_proxy(), |
| 63 ui_task_runner_); |
| 64 file_task_runner_ = |
| 65 new AutoThreadTaskRunner(file_thread_.message_loop_proxy(), |
| 66 ui_task_runner_); |
| 67 input_task_runner_ = |
| 68 new AutoThreadTaskRunner(input_thread_.message_loop_proxy(), |
| 69 ui_task_runner_); |
| 70 network_task_runner_ = |
| 71 new AutoThreadTaskRunner(network_thread_.message_loop_proxy(), |
| 72 ui_task_runner_); |
| 73 |
| 74 url_request_context_getter_ = new URLRequestContextGetter( |
| 75 ui_task_runner(), network_task_runner()); |
| 76 return true; |
| 65 } | 77 } |
| 66 | 78 |
| 67 scoped_refptr<AutoThreadTaskRunner> | 79 base::SingleThreadTaskRunner* ChromotingHostContext::audio_task_runner() { |
| 68 ChromotingHostContext::audio_task_runner() { | |
| 69 return audio_task_runner_; | 80 return audio_task_runner_; |
| 70 } | 81 } |
| 71 | 82 |
| 72 scoped_refptr<AutoThreadTaskRunner> | 83 base::SingleThreadTaskRunner* |
| 73 ChromotingHostContext::video_capture_task_runner() { | 84 ChromotingHostContext::video_capture_task_runner() { |
| 74 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 75 return video_capture_task_runner_; | 85 return video_capture_task_runner_; |
| 76 } | 86 } |
| 77 | 87 |
| 78 scoped_refptr<AutoThreadTaskRunner> | 88 base::SingleThreadTaskRunner* |
| 79 ChromotingHostContext::video_encode_task_runner() { | 89 ChromotingHostContext::video_encode_task_runner() { |
| 80 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
| 81 return video_encode_task_runner_; | 90 return video_encode_task_runner_; |
| 82 } | 91 } |
| 83 | 92 |
| 84 scoped_refptr<AutoThreadTaskRunner> | 93 base::SingleThreadTaskRunner* ChromotingHostContext::file_task_runner() { |
| 85 ChromotingHostContext::file_task_runner() { | |
| 86 return file_task_runner_; | 94 return file_task_runner_; |
| 87 } | 95 } |
| 88 | 96 |
| 89 scoped_refptr<AutoThreadTaskRunner> | 97 base::SingleThreadTaskRunner* ChromotingHostContext::input_task_runner() { |
| 90 ChromotingHostContext::input_task_runner() { | |
| 91 return input_task_runner_; | 98 return input_task_runner_; |
| 92 } | 99 } |
| 93 | 100 |
| 94 scoped_refptr<AutoThreadTaskRunner> | 101 base::SingleThreadTaskRunner* ChromotingHostContext::network_task_runner() { |
| 95 ChromotingHostContext::network_task_runner() { | |
| 96 return network_task_runner_; | 102 return network_task_runner_; |
| 97 } | 103 } |
| 98 | 104 |
| 99 scoped_refptr<AutoThreadTaskRunner> | 105 base::SingleThreadTaskRunner* ChromotingHostContext::ui_task_runner() { |
| 100 ChromotingHostContext::ui_task_runner() { | |
| 101 return ui_task_runner_; | 106 return ui_task_runner_; |
| 102 } | 107 } |
| 103 | 108 |
| 104 scoped_refptr<net::URLRequestContextGetter> | 109 const scoped_refptr<net::URLRequestContextGetter>& |
| 105 ChromotingHostContext::url_request_context_getter() { | 110 ChromotingHostContext::url_request_context_getter() { |
| 111 DCHECK(url_request_context_getter_.get()); |
| 106 return url_request_context_getter_; | 112 return url_request_context_getter_; |
| 107 } | 113 } |
| 108 | 114 |
| 109 } // namespace remoting | 115 } // namespace remoting |
| OLD | NEW |