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 video_capture_task_runner_ = AutoThread::Create( | |
alexeypa (please no reviews)
2012/12/03 20:34:23
nit: sort these by the task runners names.
Wez
2012/12/03 22:38:46
Done.
| |
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_); | |
25 } | 42 } |
26 | 43 |
27 ChromotingHostContext::~ChromotingHostContext() { | 44 ChromotingHostContext::~ChromotingHostContext() { |
45 DCHECK(ui_task_runner_->BelongsToCurrentThread()); | |
alexeypa (please no reviews)
2012/12/03 20:34:23
It2MeImpl does not try to delete the context on th
Wez
2012/12/03 22:38:46
Done.
| |
28 } | 46 } |
29 | 47 |
30 bool ChromotingHostContext::Start() { | 48 scoped_ptr<ChromotingHostContext> ChromotingHostContext::Create( |
31 // Start all the threads. | 49 scoped_refptr<AutoThreadTaskRunner> ui_task_runner) { |
32 base::Thread::Options io_thread_options(MessageLoop::TYPE_IO, 0); | 50 DCHECK(ui_task_runner->BelongsToCurrentThread()); |
33 | 51 |
34 bool started = video_capture_thread_.Start() && video_encode_thread_.Start(); | 52 scoped_ptr<ChromotingHostContext> context( |
53 new ChromotingHostContext(ui_task_runner)); | |
54 if (!context->audio_task_runner_ || | |
alexeypa (please no reviews)
2012/12/03 20:34:23
nit: sort these by the task runners names.
Wez
2012/12/03 22:38:46
Done.
| |
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 } | |
35 | 63 |
36 #if defined(OS_WIN) | 64 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 } | 65 } |
78 | 66 |
79 base::SingleThreadTaskRunner* ChromotingHostContext::audio_task_runner() { | 67 scoped_refptr<AutoThreadTaskRunner> |
68 ChromotingHostContext::audio_task_runner() { | |
80 return audio_task_runner_; | 69 return audio_task_runner_; |
81 } | 70 } |
82 | 71 |
83 base::SingleThreadTaskRunner* | 72 scoped_refptr<AutoThreadTaskRunner> |
84 ChromotingHostContext::video_capture_task_runner() { | 73 ChromotingHostContext::video_capture_task_runner() { |
85 return video_capture_task_runner_; | 74 return video_capture_task_runner_; |
86 } | 75 } |
87 | 76 |
88 base::SingleThreadTaskRunner* | 77 scoped_refptr<AutoThreadTaskRunner> |
89 ChromotingHostContext::video_encode_task_runner() { | 78 ChromotingHostContext::video_encode_task_runner() { |
90 return video_encode_task_runner_; | 79 return video_encode_task_runner_; |
91 } | 80 } |
92 | 81 |
93 base::SingleThreadTaskRunner* ChromotingHostContext::file_task_runner() { | 82 scoped_refptr<AutoThreadTaskRunner> |
83 ChromotingHostContext::file_task_runner() { | |
94 return file_task_runner_; | 84 return file_task_runner_; |
95 } | 85 } |
96 | 86 |
97 base::SingleThreadTaskRunner* ChromotingHostContext::input_task_runner() { | 87 scoped_refptr<AutoThreadTaskRunner> |
88 ChromotingHostContext::input_task_runner() { | |
98 return input_task_runner_; | 89 return input_task_runner_; |
99 } | 90 } |
100 | 91 |
101 base::SingleThreadTaskRunner* ChromotingHostContext::network_task_runner() { | 92 scoped_refptr<AutoThreadTaskRunner> |
93 ChromotingHostContext::network_task_runner() { | |
102 return network_task_runner_; | 94 return network_task_runner_; |
103 } | 95 } |
104 | 96 |
105 base::SingleThreadTaskRunner* ChromotingHostContext::ui_task_runner() { | 97 scoped_refptr<AutoThreadTaskRunner> |
98 ChromotingHostContext::ui_task_runner() { | |
106 return ui_task_runner_; | 99 return ui_task_runner_; |
107 } | 100 } |
108 | 101 |
109 const scoped_refptr<net::URLRequestContextGetter>& | 102 scoped_refptr<net::URLRequestContextGetter> |
110 ChromotingHostContext::url_request_context_getter() { | 103 ChromotingHostContext::url_request_context_getter() { |
111 DCHECK(url_request_context_getter_.get()); | |
112 return url_request_context_getter_; | 104 return url_request_context_getter_; |
113 } | 105 } |
114 | 106 |
115 } // namespace remoting | 107 } // namespace remoting |
OLD | NEW |