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/v1_authenticator.h" | 14 #include "remoting/protocol/v1_authenticator.h" |
15 #include "remoting/protocol/session_config.h" | 15 #include "remoting/protocol/session_config.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 const VideoPacket* packet, const base::Closure& done) | 22 scoped_ptr<VideoPacket> packet, const base::Closure& done) |
23 : packet(packet), 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, |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 void ChromotingClient::Stop(const base::Closure& shutdown_task) { | 71 void ChromotingClient::Stop(const base::Closure& shutdown_task) { |
72 if (!message_loop()->BelongsToCurrentThread()) { | 72 if (!message_loop()->BelongsToCurrentThread()) { |
73 message_loop()->PostTask( | 73 message_loop()->PostTask( |
74 FROM_HERE, base::Bind(&ChromotingClient::Stop, | 74 FROM_HERE, base::Bind(&ChromotingClient::Stop, |
75 base::Unretained(this), shutdown_task)); | 75 base::Unretained(this), shutdown_task)); |
76 return; | 76 return; |
77 } | 77 } |
78 | 78 |
79 // Drop all pending packets. | 79 // Drop all pending packets. |
80 while(!received_packets_.empty()) { | 80 while(!received_packets_.empty()) { |
| 81 delete received_packets_.front().packet; |
81 received_packets_.front().done.Run(); | 82 received_packets_.front().done.Run(); |
82 received_packets_.pop_front(); | 83 received_packets_.pop_front(); |
83 } | 84 } |
84 | 85 |
85 connection_->Disconnect(base::Bind(&ChromotingClient::OnDisconnected, | 86 connection_->Disconnect(base::Bind(&ChromotingClient::OnDisconnected, |
86 base::Unretained(this), shutdown_task)); | 87 base::Unretained(this), shutdown_task)); |
87 } | 88 } |
88 | 89 |
89 void ChromotingClient::OnDisconnected(const base::Closure& shutdown_task) { | 90 void ChromotingClient::OnDisconnected(const base::Closure& shutdown_task) { |
90 view_->TearDown(); | 91 view_->TearDown(); |
91 | 92 |
92 shutdown_task.Run(); | 93 shutdown_task.Run(); |
93 } | 94 } |
94 | 95 |
95 void ChromotingClient::ClientDone() { | 96 void ChromotingClient::ClientDone() { |
96 if (!client_done_.is_null()) { | 97 if (!client_done_.is_null()) { |
97 message_loop()->PostTask(FROM_HERE, client_done_); | 98 message_loop()->PostTask(FROM_HERE, client_done_); |
98 client_done_.Reset(); | 99 client_done_.Reset(); |
99 } | 100 } |
100 } | 101 } |
101 | 102 |
102 ChromotingStats* ChromotingClient::GetStats() { | 103 ChromotingStats* ChromotingClient::GetStats() { |
103 return &stats_; | 104 return &stats_; |
104 } | 105 } |
105 | 106 |
106 void ChromotingClient::ProcessVideoPacket(const VideoPacket* packet, | 107 void ChromotingClient::ProcessVideoPacket(scoped_ptr<VideoPacket> packet, |
107 const base::Closure& done) { | 108 const base::Closure& done) { |
108 DCHECK(message_loop()->BelongsToCurrentThread()); | 109 DCHECK(message_loop()->BelongsToCurrentThread()); |
109 | 110 |
110 // If the video packet is empty then drop it. Empty packets are used to | 111 // If the video packet is empty then drop it. Empty packets are used to |
111 // maintain activity on the network. | 112 // maintain activity on the network. |
112 if (!packet->has_data() || packet->data().size() == 0) { | 113 if (!packet->has_data() || packet->data().size() == 0) { |
113 done.Run(); | 114 done.Run(); |
114 return; | 115 return; |
115 } | 116 } |
116 | 117 |
117 // Add one frame to the counter. | 118 // Add one frame to the counter. |
118 stats_.video_frame_rate()->Record(1); | 119 stats_.video_frame_rate()->Record(1); |
119 | 120 |
120 // Record other statistics received from host. | 121 // Record other statistics received from host. |
121 stats_.video_bandwidth()->Record(packet->data().size()); | 122 stats_.video_bandwidth()->Record(packet->data().size()); |
122 if (packet->has_capture_time_ms()) | 123 if (packet->has_capture_time_ms()) |
123 stats_.video_capture_ms()->Record(packet->capture_time_ms()); | 124 stats_.video_capture_ms()->Record(packet->capture_time_ms()); |
124 if (packet->has_encode_time_ms()) | 125 if (packet->has_encode_time_ms()) |
125 stats_.video_encode_ms()->Record(packet->encode_time_ms()); | 126 stats_.video_encode_ms()->Record(packet->encode_time_ms()); |
126 if (packet->has_client_sequence_number() && | 127 if (packet->has_client_sequence_number() && |
127 packet->client_sequence_number() > last_sequence_number_) { | 128 packet->client_sequence_number() > last_sequence_number_) { |
128 last_sequence_number_ = packet->client_sequence_number(); | 129 last_sequence_number_ = packet->client_sequence_number(); |
129 base::TimeDelta round_trip_latency = | 130 base::TimeDelta round_trip_latency = |
130 base::Time::Now() - | 131 base::Time::Now() - |
131 base::Time::FromInternalValue(packet->client_sequence_number()); | 132 base::Time::FromInternalValue(packet->client_sequence_number()); |
132 stats_.round_trip_ms()->Record(round_trip_latency.InMilliseconds()); | 133 stats_.round_trip_ms()->Record(round_trip_latency.InMilliseconds()); |
133 } | 134 } |
134 | 135 |
135 received_packets_.push_back(QueuedVideoPacket(packet, done)); | 136 received_packets_.push_back(QueuedVideoPacket(packet.Pass(), done)); |
136 if (!packet_being_processed_) | 137 if (!packet_being_processed_) |
137 DispatchPacket(); | 138 DispatchPacket(); |
138 } | 139 } |
139 | 140 |
140 int ChromotingClient::GetPendingPackets() { | 141 int ChromotingClient::GetPendingPackets() { |
141 return received_packets_.size(); | 142 return received_packets_.size(); |
142 } | 143 } |
143 | 144 |
144 void ChromotingClient::DispatchPacket() { | 145 void ChromotingClient::DispatchPacket() { |
145 DCHECK(message_loop()->BelongsToCurrentThread()); | 146 DCHECK(message_loop()->BelongsToCurrentThread()); |
146 CHECK(!packet_being_processed_); | 147 CHECK(!packet_being_processed_); |
147 | 148 |
148 if (received_packets_.empty()) { | 149 if (received_packets_.empty()) { |
149 // Nothing to do! | 150 // Nothing to do! |
150 return; | 151 return; |
151 } | 152 } |
152 | 153 |
153 const VideoPacket* packet = received_packets_.front().packet; | 154 scoped_ptr<VideoPacket> packet(received_packets_.front().packet); |
| 155 received_packets_.front().packet = NULL; |
154 packet_being_processed_ = true; | 156 packet_being_processed_ = true; |
155 | 157 |
156 // Measure the latency between the last packet being received and presented. | 158 // Measure the latency between the last packet being received and presented. |
157 bool last_packet = (packet->flags() & VideoPacket::LAST_PACKET) != 0; | 159 bool last_packet = (packet->flags() & VideoPacket::LAST_PACKET) != 0; |
158 base::Time decode_start; | 160 base::Time decode_start; |
159 if (last_packet) | 161 if (last_packet) |
160 decode_start = base::Time::Now(); | 162 decode_start = base::Time::Now(); |
161 | 163 |
162 rectangle_decoder_->DecodePacket( | 164 rectangle_decoder_->DecodePacket( |
163 packet, base::Bind(&ChromotingClient::OnPacketDone, | 165 packet.Pass(), |
164 base::Unretained(this), last_packet, decode_start)); | 166 base::Bind(&ChromotingClient::OnPacketDone, base::Unretained(this), |
| 167 last_packet, decode_start)); |
165 } | 168 } |
166 | 169 |
167 void ChromotingClient::OnConnectionState( | 170 void ChromotingClient::OnConnectionState( |
168 protocol::ConnectionToHost::State state, | 171 protocol::ConnectionToHost::State state, |
169 protocol::ErrorCode error) { | 172 protocol::ErrorCode error) { |
170 DCHECK(message_loop()->BelongsToCurrentThread()); | 173 DCHECK(message_loop()->BelongsToCurrentThread()); |
171 VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")"; | 174 VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")"; |
172 if (state == protocol::ConnectionToHost::CONNECTED) | 175 if (state == protocol::ConnectionToHost::CONNECTED) |
173 Initialize(); | 176 Initialize(); |
174 view_->SetConnectionState(state, error); | 177 view_->SetConnectionState(state, error); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 thread_proxy_.PostTask(FROM_HERE, base::Bind( | 211 thread_proxy_.PostTask(FROM_HERE, base::Bind( |
209 &ChromotingClient::Initialize, base::Unretained(this))); | 212 &ChromotingClient::Initialize, base::Unretained(this))); |
210 return; | 213 return; |
211 } | 214 } |
212 | 215 |
213 // Initialize the decoder. | 216 // Initialize the decoder. |
214 rectangle_decoder_->Initialize(connection_->config()); | 217 rectangle_decoder_->Initialize(connection_->config()); |
215 } | 218 } |
216 | 219 |
217 } // namespace remoting | 220 } // namespace remoting |
OLD | NEW |