| 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/client/chromoting_client.h" | 5 #include "remoting/client/chromoting_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "remoting/client/chromoting_view.h" | 8 #include "remoting/client/chromoting_view.h" |
| 9 #include "remoting/client/client_context.h" | 9 #include "remoting/client/client_context.h" |
| 10 #include "remoting/client/rectangle_update_decoder.h" | 10 #include "remoting/client/rectangle_update_decoder.h" |
| 11 #include "remoting/protocol/authentication_method.h" | 11 #include "remoting/protocol/authentication_method.h" |
| 12 #include "remoting/protocol/connection_to_host.h" | 12 #include "remoting/protocol/connection_to_host.h" |
| 13 #include "remoting/protocol/negotiating_authenticator.h" | 13 #include "remoting/protocol/negotiating_authenticator.h" |
| 14 #include "remoting/protocol/session_config.h" | 14 #include "remoting/protocol/session_config.h" |
| 15 #include "remoting/protocol/transport.h" | 15 #include "remoting/protocol/transport.h" |
| 16 | 16 |
| 17 namespace remoting { | 17 namespace remoting { |
| 18 | 18 |
| 19 using protocol::AuthenticationMethod; | 19 using protocol::AuthenticationMethod; |
| 20 | 20 |
| 21 ChromotingClient::QueuedVideoPacket::QueuedVideoPacket( | 21 ChromotingClient::QueuedVideoPacket::QueuedVideoPacket( |
| 22 scoped_ptr<VideoPacket> packet, const base::Closure& done) | 22 scoped_ptr<VideoPacket> packet, const base::Closure& done) |
| 23 : packet(packet.release()), done(done) { | 23 : packet(packet.release()), done(done) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 ChromotingClient::QueuedVideoPacket::~QueuedVideoPacket() { | 26 ChromotingClient::QueuedVideoPacket::~QueuedVideoPacket() { |
| 27 } | 27 } |
| 28 | 28 |
| 29 ChromotingClient::ChromotingClient(const ClientConfig& config, | 29 ChromotingClient::ChromotingClient( |
| 30 ClientContext* context, | 30 const ClientConfig& config, |
| 31 protocol::ConnectionToHost* connection, | 31 scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
| 32 ChromotingView* view, | 32 protocol::ConnectionToHost* connection, |
| 33 RectangleUpdateDecoder* rectangle_decoder, | 33 ChromotingView* view, |
| 34 const base::Closure& client_done) | 34 RectangleUpdateDecoder* rectangle_decoder) |
| 35 : config_(config), | 35 : config_(config), |
| 36 context_(context), | 36 task_runner_(task_runner), |
| 37 connection_(connection), | 37 connection_(connection), |
| 38 view_(view), | 38 view_(view), |
| 39 rectangle_decoder_(rectangle_decoder), | 39 rectangle_decoder_(rectangle_decoder), |
| 40 client_done_(client_done), | |
| 41 packet_being_processed_(false), | 40 packet_being_processed_(false), |
| 42 last_sequence_number_(0), | 41 last_sequence_number_(0), |
| 43 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 42 weak_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 44 } | 43 } |
| 45 | 44 |
| 46 ChromotingClient::~ChromotingClient() { | 45 ChromotingClient::~ChromotingClient() { |
| 47 } | 46 } |
| 48 | 47 |
| 49 void ChromotingClient::Start( | 48 void ChromotingClient::Start( |
| 50 scoped_refptr<XmppProxy> xmpp_proxy, | 49 scoped_refptr<XmppProxy> xmpp_proxy, |
| 51 scoped_ptr<protocol::TransportFactory> transport_factory) { | 50 scoped_ptr<protocol::TransportFactory> transport_factory) { |
| 52 DCHECK(message_loop()->BelongsToCurrentThread()); | 51 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 53 | 52 |
| 54 scoped_ptr<protocol::Authenticator> authenticator( | 53 scoped_ptr<protocol::Authenticator> authenticator( |
| 55 protocol::NegotiatingAuthenticator::CreateForClient( | 54 protocol::NegotiatingAuthenticator::CreateForClient( |
| 56 config_.authentication_tag, | 55 config_.authentication_tag, |
| 57 config_.shared_secret, config_.authentication_methods)); | 56 config_.shared_secret, config_.authentication_methods)); |
| 58 | 57 |
| 59 // Create a WeakPtr to ourself for to use for all posted tasks. | 58 // Create a WeakPtr to ourself for to use for all posted tasks. |
| 60 weak_ptr_ = weak_factory_.GetWeakPtr(); | 59 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 61 | 60 |
| 62 connection_->Connect(xmpp_proxy, config_.local_jid, config_.host_jid, | 61 connection_->Connect(xmpp_proxy, config_.local_jid, config_.host_jid, |
| 63 config_.host_public_key, transport_factory.Pass(), | 62 config_.host_public_key, transport_factory.Pass(), |
| 64 authenticator.Pass(), this, this, this, this); | 63 authenticator.Pass(), this, this, this, this); |
| 65 | 64 |
| 66 if (!view_->Initialize()) { | 65 view_->Initialize(); |
| 67 ClientDone(); | |
| 68 } | |
| 69 } | 66 } |
| 70 | 67 |
| 71 void ChromotingClient::Stop(const base::Closure& shutdown_task) { | 68 void ChromotingClient::Stop(const base::Closure& shutdown_task) { |
| 72 if (!message_loop()->BelongsToCurrentThread()) { | 69 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 73 message_loop()->PostTask( | |
| 74 FROM_HERE, base::Bind(&ChromotingClient::Stop, | |
| 75 weak_ptr_, shutdown_task)); | |
| 76 return; | |
| 77 } | |
| 78 | 70 |
| 79 // Drop all pending packets. | 71 // Drop all pending packets. |
| 80 while(!received_packets_.empty()) { | 72 while(!received_packets_.empty()) { |
| 81 delete received_packets_.front().packet; | 73 delete received_packets_.front().packet; |
| 82 received_packets_.front().done.Run(); | 74 received_packets_.front().done.Run(); |
| 83 received_packets_.pop_front(); | 75 received_packets_.pop_front(); |
| 84 } | 76 } |
| 85 | 77 |
| 86 connection_->Disconnect(base::Bind(&ChromotingClient::OnDisconnected, | 78 connection_->Disconnect(base::Bind(&ChromotingClient::OnDisconnected, |
| 87 weak_ptr_, shutdown_task)); | 79 weak_ptr_, shutdown_task)); |
| 88 } | 80 } |
| 89 | 81 |
| 90 void ChromotingClient::OnDisconnected(const base::Closure& shutdown_task) { | 82 void ChromotingClient::OnDisconnected(const base::Closure& shutdown_task) { |
| 91 view_->TearDown(); | 83 view_->TearDown(); |
| 92 | 84 |
| 93 shutdown_task.Run(); | 85 shutdown_task.Run(); |
| 94 } | 86 } |
| 95 | 87 |
| 96 void ChromotingClient::ClientDone() { | |
| 97 if (!client_done_.is_null()) { | |
| 98 message_loop()->PostTask(FROM_HERE, client_done_); | |
| 99 client_done_.Reset(); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 ChromotingStats* ChromotingClient::GetStats() { | 88 ChromotingStats* ChromotingClient::GetStats() { |
| 89 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 104 return &stats_; | 90 return &stats_; |
| 105 } | 91 } |
| 106 | 92 |
| 107 void ChromotingClient::InjectClipboardEvent( | 93 void ChromotingClient::InjectClipboardEvent( |
| 108 const protocol::ClipboardEvent& event) { | 94 const protocol::ClipboardEvent& event) { |
| 95 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 109 view_->GetClipboardStub()->InjectClipboardEvent(event); | 96 view_->GetClipboardStub()->InjectClipboardEvent(event); |
| 110 } | 97 } |
| 111 | 98 |
| 112 void ChromotingClient::SetCursorShape( | 99 void ChromotingClient::SetCursorShape( |
| 113 const protocol::CursorShapeInfo& cursor_shape) { | 100 const protocol::CursorShapeInfo& cursor_shape) { |
| 114 view_->GetCursorShapeStub()->SetCursorShape(cursor_shape); | 101 view_->GetCursorShapeStub()->SetCursorShape(cursor_shape); |
| 115 } | 102 } |
| 116 | 103 |
| 117 void ChromotingClient::ProcessVideoPacket(scoped_ptr<VideoPacket> packet, | 104 void ChromotingClient::ProcessVideoPacket(scoped_ptr<VideoPacket> packet, |
| 118 const base::Closure& done) { | 105 const base::Closure& done) { |
| 119 DCHECK(message_loop()->BelongsToCurrentThread()); | 106 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 120 | 107 |
| 121 // If the video packet is empty then drop it. Empty packets are used to | 108 // If the video packet is empty then drop it. Empty packets are used to |
| 122 // maintain activity on the network. | 109 // maintain activity on the network. |
| 123 if (!packet->has_data() || packet->data().size() == 0) { | 110 if (!packet->has_data() || packet->data().size() == 0) { |
| 124 done.Run(); | 111 done.Run(); |
| 125 return; | 112 return; |
| 126 } | 113 } |
| 127 | 114 |
| 128 // Add one frame to the counter. | 115 // Add one frame to the counter. |
| 129 stats_.video_frame_rate()->Record(1); | 116 stats_.video_frame_rate()->Record(1); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 142 base::Time::FromInternalValue(packet->client_sequence_number()); | 129 base::Time::FromInternalValue(packet->client_sequence_number()); |
| 143 stats_.round_trip_ms()->Record(round_trip_latency.InMilliseconds()); | 130 stats_.round_trip_ms()->Record(round_trip_latency.InMilliseconds()); |
| 144 } | 131 } |
| 145 | 132 |
| 146 received_packets_.push_back(QueuedVideoPacket(packet.Pass(), done)); | 133 received_packets_.push_back(QueuedVideoPacket(packet.Pass(), done)); |
| 147 if (!packet_being_processed_) | 134 if (!packet_being_processed_) |
| 148 DispatchPacket(); | 135 DispatchPacket(); |
| 149 } | 136 } |
| 150 | 137 |
| 151 int ChromotingClient::GetPendingPackets() { | 138 int ChromotingClient::GetPendingPackets() { |
| 139 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 152 return received_packets_.size(); | 140 return received_packets_.size(); |
| 153 } | 141 } |
| 154 | 142 |
| 155 void ChromotingClient::DispatchPacket() { | 143 void ChromotingClient::DispatchPacket() { |
| 156 DCHECK(message_loop()->BelongsToCurrentThread()); | 144 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 157 CHECK(!packet_being_processed_); | 145 CHECK(!packet_being_processed_); |
| 158 | 146 |
| 159 if (received_packets_.empty()) { | 147 if (received_packets_.empty()) { |
| 160 // Nothing to do! | 148 // Nothing to do! |
| 161 return; | 149 return; |
| 162 } | 150 } |
| 163 | 151 |
| 164 scoped_ptr<VideoPacket> packet(received_packets_.front().packet); | 152 scoped_ptr<VideoPacket> packet(received_packets_.front().packet); |
| 165 received_packets_.front().packet = NULL; | 153 received_packets_.front().packet = NULL; |
| 166 packet_being_processed_ = true; | 154 packet_being_processed_ = true; |
| 167 | 155 |
| 168 // Measure the latency between the last packet being received and presented. | 156 // Measure the latency between the last packet being received and presented. |
| 169 bool last_packet = (packet->flags() & VideoPacket::LAST_PACKET) != 0; | 157 bool last_packet = (packet->flags() & VideoPacket::LAST_PACKET) != 0; |
| 170 base::Time decode_start; | 158 base::Time decode_start; |
| 171 if (last_packet) | 159 if (last_packet) |
| 172 decode_start = base::Time::Now(); | 160 decode_start = base::Time::Now(); |
| 173 | 161 |
| 174 rectangle_decoder_->DecodePacket( | 162 rectangle_decoder_->DecodePacket( |
| 175 packet.Pass(), | 163 packet.Pass(), |
| 176 base::Bind(&ChromotingClient::OnPacketDone, base::Unretained(this), | 164 base::Bind(&ChromotingClient::OnPacketDone, base::Unretained(this), |
| 177 last_packet, decode_start)); | 165 last_packet, decode_start)); |
| 178 } | 166 } |
| 179 | 167 |
| 180 void ChromotingClient::OnConnectionState( | 168 void ChromotingClient::OnConnectionState( |
| 181 protocol::ConnectionToHost::State state, | 169 protocol::ConnectionToHost::State state, |
| 182 protocol::ErrorCode error) { | 170 protocol::ErrorCode error) { |
| 183 DCHECK(message_loop()->BelongsToCurrentThread()); | 171 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 184 VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")"; | 172 VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")"; |
| 185 if (state == protocol::ConnectionToHost::CONNECTED) | 173 if (state == protocol::ConnectionToHost::CONNECTED) |
| 186 Initialize(); | 174 Initialize(); |
| 187 view_->SetConnectionState(state, error); | 175 view_->SetConnectionState(state, error); |
| 188 } | 176 } |
| 189 | 177 |
| 190 base::MessageLoopProxy* ChromotingClient::message_loop() { | |
| 191 return context_->network_message_loop(); | |
| 192 } | |
| 193 | |
| 194 void ChromotingClient::OnPacketDone(bool last_packet, | 178 void ChromotingClient::OnPacketDone(bool last_packet, |
| 195 base::Time decode_start) { | 179 base::Time decode_start) { |
| 196 if (!message_loop()->BelongsToCurrentThread()) { | 180 if (!task_runner_->BelongsToCurrentThread()) { |
| 197 message_loop()->PostTask(FROM_HERE, base::Bind( | 181 task_runner_->PostTask(FROM_HERE, base::Bind( |
| 198 &ChromotingClient::OnPacketDone, base::Unretained(this), | 182 &ChromotingClient::OnPacketDone, base::Unretained(this), |
| 199 last_packet, decode_start)); | 183 last_packet, decode_start)); |
| 200 return; | 184 return; |
| 201 } | 185 } |
| 202 | 186 |
| 203 // Record the latency between the final packet being received and | 187 // Record the latency between the final packet being received and |
| 204 // presented. | 188 // presented. |
| 205 if (last_packet) { | 189 if (last_packet) { |
| 206 stats_.video_decode_ms()->Record( | 190 stats_.video_decode_ms()->Record( |
| 207 (base::Time::Now() - decode_start).InMilliseconds()); | 191 (base::Time::Now() - decode_start).InMilliseconds()); |
| 208 } | 192 } |
| 209 | 193 |
| 210 received_packets_.front().done.Run(); | 194 received_packets_.front().done.Run(); |
| 211 received_packets_.pop_front(); | 195 received_packets_.pop_front(); |
| 212 | 196 |
| 213 packet_being_processed_ = false; | 197 packet_being_processed_ = false; |
| 214 | 198 |
| 215 // Process the next video packet. | 199 // Process the next video packet. |
| 216 DispatchPacket(); | 200 DispatchPacket(); |
| 217 } | 201 } |
| 218 | 202 |
| 219 void ChromotingClient::Initialize() { | 203 void ChromotingClient::Initialize() { |
| 220 if (!message_loop()->BelongsToCurrentThread()) { | 204 DCHECK(task_runner_->BelongsToCurrentThread()); |
| 221 message_loop()->PostTask(FROM_HERE, base::Bind( | |
| 222 &ChromotingClient::Initialize, weak_ptr_)); | |
| 223 return; | |
| 224 } | |
| 225 | 205 |
| 226 // Initialize the decoder. | 206 // Initialize the decoder. |
| 227 rectangle_decoder_->Initialize(connection_->config()); | 207 rectangle_decoder_->Initialize(connection_->config()); |
| 228 } | 208 } |
| 229 | 209 |
| 230 } // namespace remoting | 210 } // namespace remoting |
| OLD | NEW |