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