| 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/client/chromoting_client.h" | 5 #include "remoting/client/chromoting_client.h" |
| 6 | 6 |
| 7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
| 8 #include "remoting/base/tracer.h" | 8 #include "remoting/base/tracer.h" |
| 9 #include "remoting/client/chromoting_view.h" | 9 #include "remoting/client/chromoting_view.h" |
| 10 #include "remoting/client/client_context.h" | 10 #include "remoting/client/client_context.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 InputHandler* input_handler, | 23 InputHandler* input_handler, |
| 24 CancelableTask* client_done) | 24 CancelableTask* client_done) |
| 25 : config_(config), | 25 : config_(config), |
| 26 context_(context), | 26 context_(context), |
| 27 connection_(connection), | 27 connection_(connection), |
| 28 view_(view), | 28 view_(view), |
| 29 rectangle_decoder_(rectangle_decoder), | 29 rectangle_decoder_(rectangle_decoder), |
| 30 input_handler_(input_handler), | 30 input_handler_(input_handler), |
| 31 client_done_(client_done), | 31 client_done_(client_done), |
| 32 state_(CREATED), | 32 state_(CREATED), |
| 33 message_being_processed_(false) { | 33 packet_being_processed_(false) { |
| 34 } | 34 } |
| 35 | 35 |
| 36 ChromotingClient::~ChromotingClient() { | 36 ChromotingClient::~ChromotingClient() { |
| 37 } | 37 } |
| 38 | 38 |
| 39 void ChromotingClient::Start() { | 39 void ChromotingClient::Start() { |
| 40 if (message_loop() != MessageLoop::current()) { | 40 if (message_loop() != MessageLoop::current()) { |
| 41 message_loop()->PostTask( | 41 message_loop()->PostTask( |
| 42 FROM_HERE, | 42 FROM_HERE, |
| 43 NewRunnableMethod(this, &ChromotingClient::Start)); | 43 NewRunnableMethod(this, &ChromotingClient::Start)); |
| 44 return; | 44 return; |
| 45 } | 45 } |
| 46 | 46 |
| 47 connection_->Connect(config_, this); | 47 connection_->Connect(config_, this, this); |
| 48 | 48 |
| 49 if (!view_->Initialize()) { | 49 if (!view_->Initialize()) { |
| 50 ClientDone(); | 50 ClientDone(); |
| 51 } | 51 } |
| 52 } | 52 } |
| 53 | 53 |
| 54 void ChromotingClient::Stop() { | 54 void ChromotingClient::Stop() { |
| 55 if (message_loop() != MessageLoop::current()) { | 55 if (message_loop() != MessageLoop::current()) { |
| 56 message_loop()->PostTask( | 56 message_loop()->PostTask( |
| 57 FROM_HERE, | 57 FROM_HERE, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 void ChromotingClient::HandleMessage(HostConnection* conn, | 96 void ChromotingClient::HandleMessage(HostConnection* conn, |
| 97 ChromotingHostMessage* msg) { | 97 ChromotingHostMessage* msg) { |
| 98 if (message_loop() != MessageLoop::current()) { | 98 if (message_loop() != MessageLoop::current()) { |
| 99 message_loop()->PostTask( | 99 message_loop()->PostTask( |
| 100 FROM_HERE, | 100 FROM_HERE, |
| 101 NewRunnableMethod(this, &ChromotingClient::HandleMessage, | 101 NewRunnableMethod(this, &ChromotingClient::HandleMessage, |
| 102 conn, msg)); | 102 conn, msg)); |
| 103 return; | 103 return; |
| 104 } | 104 } |
| 105 | 105 |
| 106 // Put all messages in the queue. | |
| 107 received_messages_.push_back(msg); | |
| 108 | |
| 109 if (!message_being_processed_) { | |
| 110 DispatchMessage(); | |
| 111 } | |
| 112 } | |
| 113 | |
| 114 void ChromotingClient::DispatchMessage() { | |
| 115 DCHECK_EQ(message_loop(), MessageLoop::current()); | |
| 116 CHECK(!message_being_processed_); | |
| 117 | |
| 118 if (received_messages_.empty()) { | |
| 119 // Nothing to do! | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 ChromotingHostMessage* msg = received_messages_.front(); | |
| 124 received_messages_.pop_front(); | |
| 125 message_being_processed_ = true; | |
| 126 | |
| 127 // TODO(ajwong): Consider creating a macro similar to the IPC message | 106 // TODO(ajwong): Consider creating a macro similar to the IPC message |
| 128 // mappings. Also reconsider the lifetime of the message object. | 107 // mappings. Also reconsider the lifetime of the message object. |
| 129 if (msg->has_init_client()) { | 108 if (msg->has_init_client()) { |
| 130 ScopedTracer tracer("Handle Init Client"); | 109 ScopedTracer tracer("Handle Init Client"); |
| 131 // TODO(ajwong): Change this to use a done callback. | 110 InitClient(msg->init_client()); |
| 132 InitClient(msg->init_client(), | 111 delete msg; |
| 133 NewTracedMethod(this, &ChromotingClient::OnMessageDone, msg)); | |
| 134 } else if (msg->has_video_packet()) { | |
| 135 ScopedTracer tracer("Handle Rectangle Update"); | |
| 136 rectangle_decoder_->DecodePacket( | |
| 137 msg->video_packet(), | |
| 138 NewTracedMethod(this, &ChromotingClient::OnMessageDone, msg)); | |
| 139 } else { | 112 } else { |
| 140 NOTREACHED() << "Unknown message received"; | 113 NOTREACHED() << "Unknown message received"; |
| 141 | |
| 142 // We have an unknown message. Drop it, and schedule another dispatch. | |
| 143 // Call DispatchMessage as a continuation to avoid growing the stack. | |
| 144 delete msg; | |
| 145 message_being_processed_ = false; | |
| 146 message_loop()->PostTask( | |
| 147 FROM_HERE, | |
| 148 NewTracedMethod(this, &ChromotingClient::DispatchMessage)); | |
| 149 return; | |
| 150 } | 114 } |
| 151 } | 115 } |
| 152 | 116 |
| 117 void ChromotingClient::ProcessVideoPacket(const VideoPacket* packet, |
| 118 Task* done) { |
| 119 if (message_loop() != MessageLoop::current()) { |
| 120 message_loop()->PostTask( |
| 121 FROM_HERE, |
| 122 NewRunnableMethod(this, &ChromotingClient::ProcessVideoPacket, |
| 123 packet, done)); |
| 124 return; |
| 125 } |
| 126 |
| 127 received_packets_.push_back(QueuedVideoPacket(packet, done)); |
| 128 if (!packet_being_processed_) |
| 129 DispatchPacket(); |
| 130 } |
| 131 |
| 132 void ChromotingClient::DispatchPacket() { |
| 133 DCHECK_EQ(message_loop(), MessageLoop::current()); |
| 134 CHECK(!packet_being_processed_); |
| 135 |
| 136 if (received_packets_.empty()) { |
| 137 // Nothing to do! |
| 138 return; |
| 139 } |
| 140 |
| 141 const VideoPacket* packet = received_packets_.front().packet; |
| 142 packet_being_processed_ = true; |
| 143 |
| 144 ScopedTracer tracer("Handle video packet"); |
| 145 rectangle_decoder_->DecodePacket( |
| 146 *packet, NewTracedMethod(this, &ChromotingClient::OnPacketDone)); |
| 147 } |
| 148 |
| 153 void ChromotingClient::OnConnectionOpened(HostConnection* conn) { | 149 void ChromotingClient::OnConnectionOpened(HostConnection* conn) { |
| 154 VLOG(1) << "ChromotingClient::OnConnectionOpened"; | 150 VLOG(1) << "ChromotingClient::OnConnectionOpened"; |
| 155 SetConnectionState(CONNECTED); | 151 SetConnectionState(CONNECTED); |
| 156 } | 152 } |
| 157 | 153 |
| 158 void ChromotingClient::OnConnectionClosed(HostConnection* conn) { | 154 void ChromotingClient::OnConnectionClosed(HostConnection* conn) { |
| 159 VLOG(1) << "ChromotingClient::OnConnectionClosed"; | 155 VLOG(1) << "ChromotingClient::OnConnectionClosed"; |
| 160 SetConnectionState(DISCONNECTED); | 156 SetConnectionState(DISCONNECTED); |
| 161 } | 157 } |
| 162 | 158 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 178 NewRunnableMethod(this, &ChromotingClient::SetConnectionState, s)); | 174 NewRunnableMethod(this, &ChromotingClient::SetConnectionState, s)); |
| 179 return; | 175 return; |
| 180 } | 176 } |
| 181 | 177 |
| 182 state_ = s; | 178 state_ = s; |
| 183 view_->SetConnectionState(s); | 179 view_->SetConnectionState(s); |
| 184 | 180 |
| 185 Repaint(); | 181 Repaint(); |
| 186 } | 182 } |
| 187 | 183 |
| 188 void ChromotingClient::OnMessageDone(ChromotingHostMessage* message) { | 184 void ChromotingClient::OnPacketDone() { |
| 189 if (message_loop() != MessageLoop::current()) { | 185 if (message_loop() != MessageLoop::current()) { |
| 190 message_loop()->PostTask( | 186 message_loop()->PostTask( |
| 191 FROM_HERE, | 187 FROM_HERE, |
| 192 NewTracedMethod(this, &ChromotingClient::OnMessageDone, message)); | 188 NewTracedMethod(this, &ChromotingClient::OnPacketDone)); |
| 193 return; | 189 return; |
| 194 } | 190 } |
| 195 | 191 |
| 196 TraceContext::tracer()->PrintString("Message done"); | 192 TraceContext::tracer()->PrintString("Packet done"); |
| 197 | 193 |
| 198 message_being_processed_ = false; | 194 received_packets_.front().done->Run(); |
| 199 delete message; | 195 delete received_packets_.front().done; |
| 200 DispatchMessage(); | 196 received_packets_.pop_front(); |
| 197 |
| 198 packet_being_processed_ = false; |
| 199 |
| 200 DispatchPacket(); |
| 201 } | 201 } |
| 202 | 202 |
| 203 void ChromotingClient::InitClient(const InitClientMessage& init_client, | 203 void ChromotingClient::InitClient(const InitClientMessage& init_client) { |
| 204 Task* done) { | |
| 205 DCHECK_EQ(message_loop(), MessageLoop::current()); | 204 DCHECK_EQ(message_loop(), MessageLoop::current()); |
| 206 TraceContext::tracer()->PrintString("Init received"); | 205 TraceContext::tracer()->PrintString("Init received"); |
| 207 | 206 |
| 208 // Resize the window. | 207 // Resize the window. |
| 209 int width = init_client.width(); | 208 int width = init_client.width(); |
| 210 int height = init_client.height(); | 209 int height = init_client.height(); |
| 211 VLOG(1) << "Init client received geometry: " << width << "x" << height; | 210 VLOG(1) << "Init client received geometry: " << width << "x" << height; |
| 212 | 211 |
| 213 // TODO(ajwong): What to do here? Does the decoder actually need to request | 212 // TODO(ajwong): What to do here? Does the decoder actually need to request |
| 214 // the right frame size? This is mainly an optimization right? | 213 // the right frame size? This is mainly an optimization right? |
| 215 // rectangle_decoder_->SetOutputFrameSize(width, height); | 214 // rectangle_decoder_->SetOutputFrameSize(width, height); |
| 216 view_->SetViewport(0, 0, width, height); | 215 view_->SetViewport(0, 0, width, height); |
| 217 | 216 |
| 218 // Schedule the input handler to process the event queue. | 217 // Schedule the input handler to process the event queue. |
| 219 input_handler_->Initialize(); | 218 input_handler_->Initialize(); |
| 220 | |
| 221 done->Run(); | |
| 222 delete done; | |
| 223 } | 219 } |
| 224 | 220 |
| 225 } // namespace remoting | 221 } // namespace remoting |
| OLD | NEW |