chromoting_client.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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
  3. // found in the LICENSE file.
  4. #include "remoting/client/chromoting_client.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/logging.h"
  8. #include "remoting/base/capabilities.h"
  9. #include "remoting/base/constants.h"
  10. #include "remoting/client/client_context.h"
  11. #include "remoting/client/client_user_interface.h"
  12. #include "remoting/protocol/authenticator.h"
  13. #include "remoting/protocol/connection_to_host.h"
  14. #include "remoting/protocol/host_stub.h"
  15. #include "remoting/protocol/ice_connection_to_host.h"
  16. #include "remoting/protocol/jingle_session_manager.h"
  17. #include "remoting/protocol/negotiating_client_authenticator.h"
  18. #include "remoting/protocol/session_config.h"
  19. #include "remoting/protocol/transport_context.h"
  20. #include "remoting/protocol/video_renderer.h"
  21. #include "remoting/protocol/webrtc_connection_to_host.h"
  22. #include "remoting/signaling/signaling_address.h"
  23. #include "remoting/signaling/signaling_id_util.h"
  24. #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
  25. namespace remoting {
  26. ChromotingClient::ChromotingClient(
  27. ClientContext* client_context,
  28. ClientUserInterface* user_interface,
  29. protocol::VideoRenderer* video_renderer,
  30. base::WeakPtr<protocol::AudioStub> audio_stream_consumer)
  31. : user_interface_(user_interface), video_renderer_(video_renderer) {
  32. DCHECK(client_context->main_task_runner()->BelongsToCurrentThread());
  33. audio_decode_task_runner_ = client_context->audio_decode_task_runner();
  34. audio_stream_consumer_ = audio_stream_consumer;
  35. }
  36. ChromotingClient::~ChromotingClient() {
  37. DCHECK(thread_checker_.CalledOnValidThread());
  38. if (signal_strategy_)
  39. signal_strategy_->RemoveListener(this);
  40. }
  41. void ChromotingClient::set_protocol_config(
  42. std::unique_ptr<protocol::CandidateSessionConfig> config) {
  43. DCHECK(!connection_)
  44. << "set_protocol_config() cannot be called after Start().";
  45. protocol_config_ = std::move(config);
  46. }
  47. void ChromotingClient::set_host_experiment_config(
  48. const std::string& experiment_config) {
  49. DCHECK(!connection_)
  50. << "set_host_experiment_config() cannot be called after Start().";
  51. host_experiment_sender_ =
  52. std::make_unique<HostExperimentSender>(experiment_config);
  53. }
  54. void ChromotingClient::SetConnectionToHostForTests(
  55. std::unique_ptr<protocol::ConnectionToHost> connection_to_host) {
  56. connection_ = std::move(connection_to_host);
  57. }
  58. void ChromotingClient::Start(
  59. SignalStrategy* signal_strategy,
  60. const protocol::ClientAuthenticationConfig& client_auth_config,
  61. scoped_refptr<protocol::TransportContext> transport_context,
  62. const std::string& host_jid,
  63. const std::string& capabilities) {
  64. DCHECK(thread_checker_.CalledOnValidThread());
  65. DCHECK(!session_manager_); // Start must not be called more than once.
  66. host_jid_ = NormalizeSignalingId(host_jid);
  67. local_capabilities_ = capabilities;
  68. if (!protocol_config_) {
  69. protocol_config_ = protocol::CandidateSessionConfig::CreateDefault();
  70. }
  71. if (!connection_) {
  72. if (protocol_config_->webrtc_supported()) {
  73. LOG(FATAL) << "WebRTC is not supported.";
  74. } else {
  75. DCHECK(protocol_config_->ice_supported());
  76. connection_ = std::make_unique<protocol::IceConnectionToHost>();
  77. }
  78. }
  79. connection_->set_client_stub(this);
  80. connection_->set_clipboard_stub(this);
  81. connection_->set_video_renderer(video_renderer_);
  82. if (audio_stream_consumer_) {
  83. connection_->InitializeAudio(audio_decode_task_runner_,
  84. audio_stream_consumer_);
  85. } else {
  86. protocol_config_->DisableAudioChannel();
  87. }
  88. session_manager_ =
  89. std::make_unique<protocol::JingleSessionManager>(signal_strategy);
  90. session_manager_->set_protocol_config(std::move(protocol_config_));
  91. client_auth_config_ = client_auth_config;
  92. transport_context_ = transport_context;
  93. signal_strategy_ = signal_strategy;
  94. signal_strategy_->AddListener(this);
  95. switch (signal_strategy_->GetState()) {
  96. case SignalStrategy::CONNECTING:
  97. // Nothing to do here. Just need to wait until |signal_strategy_| becomes
  98. // connected.
  99. break;
  100. case SignalStrategy::CONNECTED:
  101. StartConnection();
  102. break;
  103. case SignalStrategy::DISCONNECTED:
  104. signal_strategy_->Connect();
  105. break;
  106. }
  107. }
  108. void ChromotingClient::Close() {
  109. DCHECK(thread_checker_.CalledOnValidThread());
  110. connection_->Disconnect(protocol::OK);
  111. }
  112. void ChromotingClient::SetCapabilities(
  113. const protocol::Capabilities& capabilities) {
  114. DCHECK(thread_checker_.CalledOnValidThread());
  115. // Only accept the first |protocol::Capabilities| message.
  116. if (host_capabilities_received_) {
  117. LOG(WARNING) << "protocol::Capabilities has been received already.";
  118. return;
  119. }
  120. host_capabilities_received_ = true;
  121. if (capabilities.has_capabilities())
  122. host_capabilities_ = capabilities.capabilities();
  123. VLOG(1) << "Host capabilities: " << host_capabilities_;
  124. // Calculate the set of capabilities enabled by both client and host and pass
  125. // it to the webapp.
  126. user_interface_->SetCapabilities(
  127. IntersectCapabilities(local_capabilities_, host_capabilities_));
  128. }
  129. void ChromotingClient::SetPairingResponse(
  130. const protocol::PairingResponse& pairing_response) {
  131. DCHECK(thread_checker_.CalledOnValidThread());
  132. user_interface_->SetPairingResponse(pairing_response);
  133. }
  134. void ChromotingClient::DeliverHostMessage(
  135. const protocol::ExtensionMessage& message) {
  136. DCHECK(thread_checker_.CalledOnValidThread());
  137. user_interface_->DeliverHostMessage(message);
  138. }
  139. void ChromotingClient::SetVideoLayout(const protocol::VideoLayout& layout) {
  140. int num_video_tracks = layout.video_track_size();
  141. if (num_video_tracks < 1) {
  142. LOG(ERROR) << "Received VideoLayout message with 0 tracks.";
  143. return;
  144. }
  145. if (num_video_tracks > 2) {
  146. LOG(WARNING) << "Received VideoLayout message with " << num_video_tracks
  147. << " tracks. Only one track is supported.";
  148. }
  149. const protocol::VideoTrackLayout& track_layout = layout.video_track(0);
  150. int x_dpi = track_layout.has_x_dpi() ? track_layout.x_dpi() : kDefaultDpi;
  151. int y_dpi = track_layout.has_y_dpi() ? track_layout.y_dpi() : kDefaultDpi;
  152. if (x_dpi != y_dpi) {
  153. LOG(WARNING) << "Mismatched x,y dpi. x=" << x_dpi << " y=" << y_dpi;
  154. }
  155. webrtc::DesktopSize size_dips(track_layout.width(), track_layout.height());
  156. webrtc::DesktopSize size_px(size_dips.width() * x_dpi / kDefaultDpi,
  157. size_dips.height() * y_dpi / kDefaultDpi);
  158. user_interface_->SetDesktopSize(size_px, webrtc::DesktopVector(x_dpi, y_dpi));
  159. mouse_input_scaler_.set_input_size(size_px.width(), size_px.height());
  160. if (connection_->config().protocol() ==
  161. protocol::SessionConfig::Protocol::ICE) {
  162. mouse_input_scaler_.set_output_size(size_px.width(), size_px.height());
  163. } else {
  164. mouse_input_scaler_.set_output_size(size_dips.width(), size_dips.height());
  165. }
  166. }
  167. void ChromotingClient::SetTransportInfo(
  168. const protocol::TransportInfo& transport_info) {}
  169. void ChromotingClient::InjectClipboardEvent(
  170. const protocol::ClipboardEvent& event) {
  171. DCHECK(thread_checker_.CalledOnValidThread());
  172. user_interface_->GetClipboardStub()->InjectClipboardEvent(event);
  173. }
  174. void ChromotingClient::SetCursorShape(
  175. const protocol::CursorShapeInfo& cursor_shape) {
  176. DCHECK(thread_checker_.CalledOnValidThread());
  177. user_interface_->GetCursorShapeStub()->SetCursorShape(cursor_shape);
  178. }
  179. void ChromotingClient::SetKeyboardLayout(
  180. const protocol::KeyboardLayout& layout) {
  181. DCHECK(thread_checker_.CalledOnValidThread());
  182. user_interface_->GetKeyboardLayoutStub()->SetKeyboardLayout(layout);
  183. }
  184. void ChromotingClient::OnConnectionState(
  185. protocol::ConnectionToHost::State state,
  186. protocol::ErrorCode error) {
  187. DCHECK(thread_checker_.CalledOnValidThread());
  188. VLOG(1) << "ChromotingClient::OnConnectionState(" << state << ")";
  189. if (state == protocol::ConnectionToHost::CONNECTED) {
  190. OnChannelsConnected();
  191. }
  192. user_interface_->OnConnectionState(state, error);
  193. }
  194. void ChromotingClient::OnConnectionReady(bool ready) {
  195. VLOG(1) << "ChromotingClient::OnConnectionReady(" << ready << ")";
  196. user_interface_->OnConnectionReady(ready);
  197. }
  198. void ChromotingClient::OnRouteChanged(const std::string& channel_name,
  199. const protocol::TransportRoute& route) {
  200. VLOG(0) << "Using " << protocol::TransportRoute::GetTypeString(route.type)
  201. << " connection for " << channel_name << " channel";
  202. user_interface_->OnRouteChanged(channel_name, route);
  203. }
  204. void ChromotingClient::OnSignalStrategyStateChange(
  205. SignalStrategy::State state) {
  206. DCHECK(thread_checker_.CalledOnValidThread());
  207. if (state == SignalStrategy::CONNECTED) {
  208. VLOG(1) << "Connected as: " << signal_strategy_->GetLocalAddress().id();
  209. // After signaling has been connected we can try connecting to the host.
  210. if (connection_ &&
  211. connection_->state() == protocol::ConnectionToHost::INITIALIZING) {
  212. StartConnection();
  213. }
  214. } else if (state == SignalStrategy::DISCONNECTED) {
  215. VLOG(1) << "Signaling connection closed.";
  216. mouse_input_scaler_.set_input_stub(nullptr);
  217. connection_.reset();
  218. user_interface_->OnConnectionState(protocol::ConnectionToHost::FAILED,
  219. protocol::SIGNALING_ERROR);
  220. }
  221. }
  222. bool ChromotingClient::OnSignalStrategyIncomingStanza(
  223. const jingle_xmpp::XmlElement* stanza) {
  224. return false;
  225. }
  226. void ChromotingClient::StartConnection() {
  227. DCHECK(thread_checker_.CalledOnValidThread());
  228. auto session = session_manager_->Connect(
  229. SignalingAddress(host_jid_),
  230. std::make_unique<protocol::NegotiatingClientAuthenticator>(
  231. signal_strategy_->GetLocalAddress().id(), host_jid_,
  232. client_auth_config_));
  233. if (host_experiment_sender_) {
  234. session->AddPlugin(host_experiment_sender_.get());
  235. }
  236. connection_->Connect(std::move(session), transport_context_, this);
  237. }
  238. void ChromotingClient::OnChannelsConnected() {
  239. DCHECK(thread_checker_.CalledOnValidThread());
  240. // Negotiate capabilities with the host.
  241. VLOG(1) << "Client capabilities: " << local_capabilities_;
  242. protocol::Capabilities capabilities;
  243. capabilities.set_capabilities(local_capabilities_);
  244. connection_->host_stub()->SetCapabilities(capabilities);
  245. mouse_input_scaler_.set_input_stub(connection_->input_stub());
  246. }
  247. } // namespace remoting