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