OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/session_manager.h" | 5 #include "remoting/host/session_manager.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/stl_util-inl.h" | 10 #include "base/stl_util-inl.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
42 network_loop_(network_loop), | 42 network_loop_(network_loop), |
43 capturer_(capturer), | 43 capturer_(capturer), |
44 encoder_(encoder), | 44 encoder_(encoder), |
45 rate_(kDefaultCaptureRate), | 45 rate_(kDefaultCaptureRate), |
46 max_rate_(kDefaultCaptureRate), | 46 max_rate_(kDefaultCaptureRate), |
47 started_(false), | 47 started_(false), |
48 recordings_(0), | 48 recordings_(0), |
49 rate_control_started_(false), | 49 rate_control_started_(false), |
50 capture_width_(0), | 50 capture_width_(0), |
51 capture_height_(0), | 51 capture_height_(0), |
52 capture_pixel_format_(chromotocol_pb::PixelFormatInvalid), | 52 capture_pixel_format_(PixelFormatInvalid), |
53 encode_stream_started_(false), | 53 encode_stream_started_(false), |
54 encode_done_(false) { | 54 encode_done_(false) { |
55 DCHECK(capture_loop_); | 55 DCHECK(capture_loop_); |
56 DCHECK(encode_loop_); | 56 DCHECK(encode_loop_); |
57 DCHECK(network_loop_); | 57 DCHECK(network_loop_); |
58 } | 58 } |
59 | 59 |
60 SessionManager::~SessionManager() { | 60 SessionManager::~SessionManager() { |
61 clients_.clear(); | 61 clients_.clear(); |
62 DCHECK_EQ(0u, clients_.size()); | |
63 } | 62 } |
64 | 63 |
65 void SessionManager::Start() { | 64 void SessionManager::Start() { |
66 capture_loop_->PostTask( | 65 capture_loop_->PostTask( |
67 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoStart)); | 66 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoStart)); |
68 } | 67 } |
69 | 68 |
70 void SessionManager::DoStart() { | 69 void SessionManager::DoStart() { |
71 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 70 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
72 | 71 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 } | 124 } |
126 rate_control_started_ = false; | 125 rate_control_started_ = false; |
127 } | 126 } |
128 | 127 |
129 void SessionManager::SetMaxRate(double rate) { | 128 void SessionManager::SetMaxRate(double rate) { |
130 capture_loop_->PostTask( | 129 capture_loop_->PostTask( |
131 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoSetMaxRate, rate)); | 130 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoSetMaxRate, rate)); |
132 } | 131 } |
133 | 132 |
134 void SessionManager::AddClient(scoped_refptr<ClientConnection> client) { | 133 void SessionManager::AddClient(scoped_refptr<ClientConnection> client) { |
135 network_loop_->PostTask( | 134 // Gets the init information for the client. |
| 135 capture_loop_->PostTask( |
136 FROM_HERE, | 136 FROM_HERE, |
137 NewRunnableMethod(this, &SessionManager::DoAddClient, client)); | 137 NewRunnableMethod(this, &SessionManager::DoGetInitInfo, client)); |
138 } | 138 } |
139 | 139 |
140 void SessionManager::RemoveClient(scoped_refptr<ClientConnection> client) { | 140 void SessionManager::RemoveClient(scoped_refptr<ClientConnection> client) { |
141 network_loop_->PostTask( | 141 network_loop_->PostTask( |
142 FROM_HERE, | 142 FROM_HERE, |
143 NewRunnableMethod(this, &SessionManager::DoRemoveClient, client)); | 143 NewRunnableMethod(this, &SessionManager::DoRemoveClient, client)); |
144 } | 144 } |
145 | 145 |
146 void SessionManager::DoCapture() { | 146 void SessionManager::DoCapture() { |
147 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 147 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 capture_data_, | 207 capture_data_, |
208 capture_data_strides_, | 208 capture_data_strides_, |
209 false, | 209 false, |
210 &encoded_data_header_, | 210 &encoded_data_header_, |
211 &encoded_data_, | 211 &encoded_data_, |
212 &encode_done_, | 212 &encode_done_, |
213 NewRunnableMethod(this, &SessionManager::EncodeDataAvailableTask)); | 213 NewRunnableMethod(this, &SessionManager::EncodeDataAvailableTask)); |
214 } | 214 } |
215 | 215 |
216 void SessionManager::DoSendUpdate( | 216 void SessionManager::DoSendUpdate( |
217 chromotocol_pb::UpdateStreamPacketHeader* header, | 217 UpdateStreamPacketHeader* header, |
218 scoped_refptr<media::DataBuffer> encoded_data, | 218 scoped_refptr<media::DataBuffer> encoded_data, |
219 bool begin_update, bool end_update) { | 219 bool begin_update, bool end_update) { |
220 DCHECK_EQ(network_loop_, MessageLoop::current()); | 220 DCHECK_EQ(network_loop_, MessageLoop::current()); |
221 | 221 |
222 for (size_t i = 0; i < clients_.size(); ++i) { | 222 for (size_t i = 0; i < clients_.size(); ++i) { |
223 if (begin_update) | 223 if (begin_update) |
224 clients_[i]->SendBeginUpdateStreamMessage(); | 224 clients_[i]->SendBeginUpdateStreamMessage(); |
225 | 225 |
226 // This will pass the ownership of the DataBuffer to the ClientConnection. | 226 // This will pass the ownership of the DataBuffer to the ClientConnection. |
227 clients_[i]->SendUpdateStreamPacketMessage(header, encoded_data); | 227 clients_[i]->SendUpdateStreamPacketMessage(header, encoded_data); |
228 | 228 |
229 if (end_update) | 229 if (end_update) |
230 clients_[i]->SendEndUpdateStreamMessage(); | 230 clients_[i]->SendEndUpdateStreamMessage(); |
231 } | 231 } |
232 } | 232 } |
233 | 233 |
234 void SessionManager::DoSendInit(scoped_refptr<ClientConnection> client, | 234 void SessionManager::DoSendInit(scoped_refptr<ClientConnection> client, |
235 int width, int height) { | 235 int width, int height) { |
236 DCHECK_EQ(network_loop_, MessageLoop::current()); | 236 DCHECK_EQ(network_loop_, MessageLoop::current()); |
237 | 237 |
238 // Sends the client init information. | 238 // Sends the client init information. |
239 client->SendInitClientMessage(width, height); | 239 client->SendInitClientMessage(width, height); |
240 } | 240 } |
241 | 241 |
242 void SessionManager::DoGetInitInfo(scoped_refptr<ClientConnection> client) { | 242 void SessionManager::DoGetInitInfo(scoped_refptr<ClientConnection> client) { |
243 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 243 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
244 | 244 |
| 245 // Sends the init message to the cleint. |
245 network_loop_->PostTask( | 246 network_loop_->PostTask( |
246 FROM_HERE, | 247 FROM_HERE, |
247 NewRunnableMethod(this, &SessionManager::DoSendInit, client, | 248 NewRunnableMethod(this, &SessionManager::DoSendInit, client, |
248 capturer_->GetWidth(), capturer_->GetHeight())); | 249 capturer_->GetWidth(), capturer_->GetHeight())); |
| 250 |
| 251 // And then add the client to the list so it can receive update stream. |
| 252 // It is important we do so in such order or the client will receive |
| 253 // update stream before init message. |
| 254 network_loop_->PostTask( |
| 255 FROM_HERE, |
| 256 NewRunnableMethod(this, &SessionManager::DoAddClient, client)); |
249 } | 257 } |
250 | 258 |
251 void SessionManager::DoSetRate(double rate) { | 259 void SessionManager::DoSetRate(double rate) { |
252 DCHECK_EQ(capture_loop_, MessageLoop::current()); | 260 DCHECK_EQ(capture_loop_, MessageLoop::current()); |
253 if (rate == rate_) | 261 if (rate == rate_) |
254 return; | 262 return; |
255 | 263 |
256 // Change the current capture rate. | 264 // Change the current capture rate. |
257 rate_ = rate; | 265 rate_ = rate; |
258 | 266 |
(...skipping 13 matching lines...) Expand all Loading... |
272 } else { | 280 } else { |
273 NOTREACHED() << "Rate is too small."; | 281 NOTREACHED() << "Rate is too small."; |
274 } | 282 } |
275 } | 283 } |
276 | 284 |
277 void SessionManager::DoAddClient(scoped_refptr<ClientConnection> client) { | 285 void SessionManager::DoAddClient(scoped_refptr<ClientConnection> client) { |
278 DCHECK_EQ(network_loop_, MessageLoop::current()); | 286 DCHECK_EQ(network_loop_, MessageLoop::current()); |
279 | 287 |
280 // TODO(hclam): Force a full frame for next encode. | 288 // TODO(hclam): Force a full frame for next encode. |
281 clients_.push_back(client); | 289 clients_.push_back(client); |
282 | |
283 // Gets the init information for the client. | |
284 capture_loop_->PostTask( | |
285 FROM_HERE, | |
286 NewRunnableMethod(this, &SessionManager::DoGetInitInfo, client)); | |
287 } | 290 } |
288 | 291 |
289 void SessionManager::DoRemoveClient(scoped_refptr<ClientConnection> client) { | 292 void SessionManager::DoRemoveClient(scoped_refptr<ClientConnection> client) { |
290 DCHECK_EQ(network_loop_, MessageLoop::current()); | 293 DCHECK_EQ(network_loop_, MessageLoop::current()); |
291 | 294 |
292 // TODO(hclam): Is it correct to do to a scoped_refptr? | 295 // TODO(hclam): Is it correct to do to a scoped_refptr? |
293 ClientConnectionList::iterator it | 296 ClientConnectionList::iterator it |
294 = std::find(clients_.begin(), clients_.end(), client); | 297 = std::find(clients_.begin(), clients_.end(), client); |
295 if (it != clients_.end()) | 298 if (it != clients_.end()) |
296 clients_.erase(it); | 299 clients_.erase(it); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
392 // the ClientConnections. | 395 // the ClientConnections. |
393 encoded_data_ = NULL; | 396 encoded_data_ = NULL; |
394 | 397 |
395 if (encode_done_) { | 398 if (encode_done_) { |
396 capture_loop_->PostTask( | 399 capture_loop_->PostTask( |
397 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoFinishEncode)); | 400 FROM_HERE, NewRunnableMethod(this, &SessionManager::DoFinishEncode)); |
398 } | 401 } |
399 } | 402 } |
400 | 403 |
401 } // namespace remoting | 404 } // namespace remoting |
OLD | NEW |