OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "extensions/renderer/api/display_source/wifi_display/wifi_display_sessi
on.h" | 5 #include "extensions/renderer/api/display_source/wifi_display/wifi_display_sessi
on.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/timer/timer.h" | 8 #include "base/timer/timer.h" |
9 #include "content/public/common/service_registry.h" | 9 #include "content/public/common/service_registry.h" |
10 #include "content/public/renderer/render_frame.h" | 10 #include "content/public/renderer/render_frame.h" |
| 11 #include "extensions/renderer/api/display_source/wifi_display/wifi_display_media
_manager.h" |
| 12 #include "third_party/wds/src/libwds/public/logging.h" |
| 13 #include "third_party/wds/src/libwds/public/media_manager.h" |
11 | 14 |
12 namespace { | 15 namespace { |
13 const char kErrorInternal[] = "An internal error has occurred"; | 16 const char kErrorInternal[] = "An internal error has occurred"; |
| 17 const char kErrorTimeout[] = "Sink became unresponsive"; |
| 18 |
| 19 static void LogWDSError(const char* format, ...) { |
| 20 va_list args; |
| 21 va_start(args, format); |
| 22 char buffer[256]; |
| 23 vsnprintf(buffer, 256, format, args); |
| 24 va_end(args); |
| 25 DVLOG(1) << "[WDS] " << buffer; |
| 26 } |
| 27 |
14 } // namespace | 28 } // namespace |
15 | 29 |
16 namespace extensions { | 30 namespace extensions { |
17 | 31 |
18 using api::display_source::ErrorType; | 32 using api::display_source::ErrorType; |
19 | 33 |
20 WiFiDisplaySession::WiFiDisplaySession( | 34 WiFiDisplaySession::WiFiDisplaySession( |
21 const DisplaySourceSessionParams& params) | 35 const DisplaySourceSessionParams& params) |
22 : binding_(this), | 36 : binding_(this), |
23 params_(params), | 37 params_(params), |
| 38 cseq_(0), |
| 39 timer_id_(0), |
24 weak_factory_(this) { | 40 weak_factory_(this) { |
25 DCHECK(params_.render_frame); | 41 DCHECK(params_.render_frame); |
| 42 wds::LogSystem::set_error_func(&LogWDSError); |
26 params.render_frame->GetServiceRegistry()->ConnectToRemoteService( | 43 params.render_frame->GetServiceRegistry()->ConnectToRemoteService( |
27 mojo::GetProxy(&service_)); | 44 mojo::GetProxy(&service_)); |
28 service_.set_connection_error_handler(base::Bind( | 45 service_.set_connection_error_handler(base::Bind( |
29 &WiFiDisplaySession::OnConnectionError, | 46 &WiFiDisplaySession::OnIPCConnectionError, |
30 weak_factory_.GetWeakPtr())); | 47 weak_factory_.GetWeakPtr())); |
31 | 48 |
32 service_->SetClient(binding_.CreateInterfacePtrAndBind()); | 49 service_->SetClient(binding_.CreateInterfacePtrAndBind()); |
33 binding_.set_connection_error_handler(base::Bind( | 50 binding_.set_connection_error_handler(base::Bind( |
34 &WiFiDisplaySession::OnConnectionError, | 51 &WiFiDisplaySession::OnIPCConnectionError, |
35 weak_factory_.GetWeakPtr())); | 52 weak_factory_.GetWeakPtr())); |
36 } | 53 } |
37 | 54 |
38 WiFiDisplaySession::~WiFiDisplaySession() { | 55 WiFiDisplaySession::~WiFiDisplaySession() { |
39 } | 56 } |
40 | 57 |
41 void WiFiDisplaySession::Start(const CompletionCallback& callback) { | 58 void WiFiDisplaySession::Start(const CompletionCallback& callback) { |
42 DCHECK_EQ(DisplaySourceSession::Idle, state_); | 59 DCHECK_EQ(DisplaySourceSession::Idle, state_); |
43 DCHECK(!terminated_callback_.is_null()) | 60 DCHECK(!terminated_callback_.is_null()) |
44 << "Should be set with 'SetNotificationCallbacks'"; | 61 << "Should be set with 'SetNotificationCallbacks'"; |
45 DCHECK(!error_callback_.is_null()) | 62 DCHECK(!error_callback_.is_null()) |
46 << "Should be set with 'SetNotificationCallbacks'"; | 63 << "Should be set with 'SetNotificationCallbacks'"; |
47 | 64 |
48 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); | 65 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); |
49 state_ = DisplaySourceSession::Establishing; | 66 state_ = DisplaySourceSession::Establishing; |
50 start_completion_callback_ = callback; | 67 start_completion_callback_ = callback; |
51 } | 68 } |
52 | 69 |
53 void WiFiDisplaySession::Terminate(const CompletionCallback& callback) { | 70 void WiFiDisplaySession::Terminate(const CompletionCallback& callback) { |
54 DCHECK_EQ(DisplaySourceSession::Established, state_); | 71 DCHECK_EQ(DisplaySourceSession::Established, state_); |
55 service_->Disconnect(); | 72 service_->Disconnect(); |
56 state_ = DisplaySourceSession::Terminating; | 73 state_ = DisplaySourceSession::Terminating; |
57 teminate_completion_callback_ = callback; | 74 teminate_completion_callback_ = callback; |
58 } | 75 } |
59 | 76 |
60 void WiFiDisplaySession::OnConnected(const mojo::String& ip_address) { | 77 void WiFiDisplaySession::OnConnected(const mojo::String& ip_address) { |
61 DCHECK_EQ(DisplaySourceSession::Established, state_); | 78 DCHECK_EQ(DisplaySourceSession::Established, state_); |
62 ip_address_ = ip_address; | 79 ip_address_ = ip_address; |
63 // TODO(Mikhail): Start Wi-Fi Display session control message exchange. | 80 media_manager_.reset(new WiFiDisplayMediaManager()); |
| 81 wfd_source_.reset(wds::Source::Create(this, media_manager_.get(), this)); |
| 82 wfd_source_->Start(); |
64 } | 83 } |
65 | 84 |
66 void WiFiDisplaySession::OnConnectRequestHandled(bool success, | 85 void WiFiDisplaySession::OnConnectRequestHandled(bool success, |
67 const mojo::String& error) { | 86 const mojo::String& error) { |
68 DCHECK_EQ(DisplaySourceSession::Establishing, state_); | 87 DCHECK_EQ(DisplaySourceSession::Establishing, state_); |
69 state_ = | 88 state_ = |
70 success ? DisplaySourceSession::Established : DisplaySourceSession::Idle; | 89 success ? DisplaySourceSession::Established : DisplaySourceSession::Idle; |
71 RunStartCallback(success, error); | 90 RunStartCallback(success, error); |
72 } | 91 } |
73 | 92 |
74 void WiFiDisplaySession::OnTerminated() { | 93 void WiFiDisplaySession::OnTerminated() { |
75 DCHECK_NE(DisplaySourceSession::Idle, state_); | 94 DCHECK_NE(DisplaySourceSession::Idle, state_); |
76 state_ = DisplaySourceSession::Idle; | 95 state_ = DisplaySourceSession::Idle; |
| 96 media_manager_.reset(); |
| 97 wfd_source_.reset(); |
77 terminated_callback_.Run(); | 98 terminated_callback_.Run(); |
78 } | 99 } |
79 | 100 |
80 void WiFiDisplaySession::OnDisconnectRequestHandled(bool success, | 101 void WiFiDisplaySession::OnDisconnectRequestHandled(bool success, |
81 const mojo::String& error) { | 102 const mojo::String& error) { |
82 RunTerminateCallback(success, error); | 103 RunTerminateCallback(success, error); |
83 } | 104 } |
84 | 105 |
85 void WiFiDisplaySession::OnError(int32_t type, | 106 void WiFiDisplaySession::OnError(int32_t type, |
86 const mojo::String& description) { | 107 const mojo::String& description) { |
87 DCHECK(type > api::display_source::ERROR_TYPE_NONE | 108 DCHECK(type > api::display_source::ERROR_TYPE_NONE |
88 && type <= api::display_source::ERROR_TYPE_LAST); | 109 && type <= api::display_source::ERROR_TYPE_LAST); |
89 DCHECK_EQ(DisplaySourceSession::Established, state_); | 110 DCHECK_EQ(DisplaySourceSession::Established, state_); |
90 error_callback_.Run(static_cast<ErrorType>(type), description); | 111 error_callback_.Run(static_cast<ErrorType>(type), description); |
91 } | 112 } |
92 | 113 |
93 void WiFiDisplaySession::OnMessage(const mojo::String& data) { | 114 void WiFiDisplaySession::OnMessage(const mojo::String& data) { |
94 DCHECK_EQ(DisplaySourceSession::Established, state_); | 115 DCHECK_EQ(DisplaySourceSession::Established, state_); |
| 116 DCHECK(wfd_source_); |
| 117 wfd_source_->RTSPDataReceived(data); |
95 } | 118 } |
96 | 119 |
97 void WiFiDisplaySession::OnConnectionError() { | 120 void WiFiDisplaySession::OnIPCConnectionError() { |
98 // We must explicitly notify the session termination as it will never | 121 // We must explicitly notify the session termination as it will never |
99 // arrive from browser process (IPC is broken). | 122 // arrive from browser process (IPC is broken). |
100 switch (state_) { | 123 switch (state_) { |
101 case DisplaySourceSession::Idle: | 124 case DisplaySourceSession::Idle: |
102 case DisplaySourceSession::Establishing: | 125 case DisplaySourceSession::Establishing: |
103 RunStartCallback(false, kErrorInternal); | 126 RunStartCallback(false, kErrorInternal); |
104 break; | 127 break; |
105 case DisplaySourceSession::Terminating: | 128 case DisplaySourceSession::Terminating: |
106 case DisplaySourceSession::Established: | 129 case DisplaySourceSession::Established: |
107 error_callback_.Run(api::display_source::ERROR_TYPE_UNKNOWN_ERROR, | 130 error_callback_.Run(api::display_source::ERROR_TYPE_UNKNOWN_ERROR, |
108 kErrorInternal); | 131 kErrorInternal); |
109 state_ = DisplaySourceSession::Idle; | 132 state_ = DisplaySourceSession::Idle; |
110 terminated_callback_.Run(); | 133 terminated_callback_.Run(); |
111 break; | 134 break; |
112 default: | 135 default: |
113 NOTREACHED(); | 136 NOTREACHED(); |
114 } | 137 } |
115 } | 138 } |
116 | 139 |
| 140 std::string WiFiDisplaySession::GetLocalIPAddress() const { |
| 141 return ip_address_; |
| 142 } |
| 143 |
| 144 int WiFiDisplaySession::GetNextCSeq(int* initial_peer_cseq) const { |
| 145 return ++cseq_; |
| 146 } |
| 147 |
| 148 void WiFiDisplaySession::SendRTSPData(const std::string& message) { |
| 149 service_->SendMessage(message); |
| 150 } |
| 151 |
| 152 unsigned WiFiDisplaySession::CreateTimer(int seconds) { |
| 153 scoped_ptr<base::Timer> timer(new base::Timer(true, true)); |
| 154 auto insert_ret = timers_.insert( |
| 155 std::pair<int, scoped_ptr<base::Timer>>( |
| 156 ++timer_id_, std::move(timer))); |
| 157 DCHECK(insert_ret.second); |
| 158 timer->Start(FROM_HERE, |
| 159 base::TimeDelta::FromSeconds(seconds), |
| 160 base::Bind(&wds::Source::OnTimerEvent, |
| 161 base::Unretained(wfd_source_.get()), |
| 162 timer_id_)); |
| 163 return static_cast<unsigned>(timer_id_); |
| 164 } |
| 165 |
| 166 void WiFiDisplaySession::ReleaseTimer(unsigned timer_id) { |
| 167 auto it = timers_.find(static_cast<int>(timer_id)); |
| 168 if (it != timers_.end()) |
| 169 timers_.erase(it); |
| 170 } |
| 171 |
| 172 void WiFiDisplaySession::ErrorOccurred(wds::ErrorType error) { |
| 173 DCHECK_NE(DisplaySourceSession::Idle, state_); |
| 174 if (error == wds::TimeoutError) { |
| 175 error_callback_.Run(api::display_source::ERROR_TYPE_TIMEOUT_ERROR, |
| 176 kErrorTimeout); |
| 177 } else { |
| 178 error_callback_.Run(api::display_source::ERROR_TYPE_UNKNOWN_ERROR, |
| 179 kErrorInternal); |
| 180 } |
| 181 |
| 182 if (state_ == DisplaySourceSession::Established) { |
| 183 // The session cannot continue. |
| 184 service_->Disconnect(); |
| 185 state_ = DisplaySourceSession::Terminating; |
| 186 } |
| 187 } |
| 188 |
| 189 void WiFiDisplaySession::SessionCompleted() { |
| 190 DCHECK_NE(DisplaySourceSession::Idle, state_); |
| 191 if (state_ == DisplaySourceSession::Established) { |
| 192 // The session has finished normally. |
| 193 service_->Disconnect(); |
| 194 state_ = DisplaySourceSession::Terminating; |
| 195 } |
| 196 } |
| 197 |
117 void WiFiDisplaySession::RunStartCallback(bool success, | 198 void WiFiDisplaySession::RunStartCallback(bool success, |
118 const std::string& error_message) { | 199 const std::string& error_message) { |
119 if (!start_completion_callback_.is_null()) | 200 if (!start_completion_callback_.is_null()) |
120 start_completion_callback_.Run(success, error_message); | 201 start_completion_callback_.Run(success, error_message); |
121 } | 202 } |
122 | 203 |
123 void WiFiDisplaySession::RunTerminateCallback( | 204 void WiFiDisplaySession::RunTerminateCallback( |
124 bool success, | 205 bool success, |
125 const std::string& error_message) { | 206 const std::string& error_message) { |
126 if (!teminate_completion_callback_.is_null()) | 207 if (!teminate_completion_callback_.is_null()) |
127 teminate_completion_callback_.Run(success, error_message); | 208 teminate_completion_callback_.Run(success, error_message); |
128 } | 209 } |
129 | 210 |
130 } // namespace extensions | 211 } // namespace extensions |
OLD | NEW |