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 LOG(ERROR) << "[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), |
24 weak_factory_(this) { | 38 weak_factory_(this) { |
25 DCHECK(params_.render_frame); | 39 DCHECK(params_.render_frame); |
40 wds::LogSystem::set_error_func(&LogWDSError); | |
26 params.render_frame->GetServiceRegistry()->ConnectToRemoteService( | 41 params.render_frame->GetServiceRegistry()->ConnectToRemoteService( |
27 mojo::GetProxy(&service_)); | 42 mojo::GetProxy(&service_)); |
28 service_.set_connection_error_handler(base::Bind( | 43 service_.set_connection_error_handler(base::Bind( |
29 &WiFiDisplaySession::OnConnectionError, | 44 &WiFiDisplaySession::OnIPCConnectionError, |
30 weak_factory_.GetWeakPtr())); | 45 weak_factory_.GetWeakPtr())); |
31 | 46 |
32 service_->SetClient(binding_.CreateInterfacePtrAndBind()); | 47 service_->SetClient(binding_.CreateInterfacePtrAndBind()); |
33 binding_.set_connection_error_handler(base::Bind( | 48 binding_.set_connection_error_handler(base::Bind( |
34 &WiFiDisplaySession::OnConnectionError, | 49 &WiFiDisplaySession::OnIPCConnectionError, |
35 weak_factory_.GetWeakPtr())); | 50 weak_factory_.GetWeakPtr())); |
36 } | 51 } |
37 | 52 |
38 WiFiDisplaySession::~WiFiDisplaySession() { | 53 WiFiDisplaySession::~WiFiDisplaySession() { |
39 } | 54 } |
40 | 55 |
41 void WiFiDisplaySession::Start() { | 56 void WiFiDisplaySession::Start() { |
42 DCHECK(state_ == DisplaySourceSession::Idle); | 57 DCHECK(state_ == DisplaySourceSession::Idle); |
43 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); | 58 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); |
44 state_ = DisplaySourceSession::Establishing; | 59 state_ = DisplaySourceSession::Establishing; |
(...skipping 14 matching lines...) Expand all Loading... | |
59 state_ = DisplaySourceSession::Terminating; | 74 state_ = DisplaySourceSession::Terminating; |
60 break; | 75 break; |
61 default: | 76 default: |
62 NOTREACHED(); | 77 NOTREACHED(); |
63 } | 78 } |
64 } | 79 } |
65 | 80 |
66 void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) { | 81 void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) { |
67 DCHECK(state_ != DisplaySourceSession::Established); | 82 DCHECK(state_ != DisplaySourceSession::Established); |
68 ip_address_ = ip_address; | 83 ip_address_ = ip_address; |
84 media_manager_.reset(new WiFiDisplayMediaManager()); | |
85 wfd_source_.reset(wds::Source::Create(this, media_manager_.get(), this)); | |
86 wfd_source_->Start(); | |
69 state_ = DisplaySourceSession::Established; | 87 state_ = DisplaySourceSession::Established; |
70 } | 88 } |
71 | 89 |
72 void WiFiDisplaySession::OnTerminated() { | 90 void WiFiDisplaySession::OnTerminated() { |
73 DCHECK(state_ != DisplaySourceSession::Idle); | 91 DCHECK(state_ != DisplaySourceSession::Idle); |
74 state_ = DisplaySourceSession::Idle; | 92 state_ = DisplaySourceSession::Idle; |
93 media_manager_.reset(); | |
94 wfd_source_.reset(); | |
75 if (!terminated_callback_.is_null()) | 95 if (!terminated_callback_.is_null()) |
76 terminated_callback_.Run(params_.sink_id); | 96 terminated_callback_.Run(params_.sink_id); |
77 } | 97 } |
78 | 98 |
79 void WiFiDisplaySession::OnError(int32_t type, | 99 void WiFiDisplaySession::OnError(int32_t type, |
80 const mojo::String& description) { | 100 const mojo::String& description) { |
81 DCHECK(type > api::display_source::ERROR_TYPE_NONE | 101 DCHECK(type > api::display_source::ERROR_TYPE_NONE |
82 && type <= api::display_source::ERROR_TYPE_LAST); | 102 && type <= api::display_source::ERROR_TYPE_LAST); |
83 if (!error_callback_.is_null()) | 103 if (!error_callback_.is_null()) |
84 error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type), | 104 error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type), |
85 description); | 105 description); |
86 } | 106 } |
87 | 107 |
88 void WiFiDisplaySession::OnMessage(const mojo::String& data) { | 108 void WiFiDisplaySession::OnMessage(const mojo::String& data) { |
89 DCHECK(state_ == DisplaySourceSession::Established); | 109 DCHECK(state_ == DisplaySourceSession::Established); |
110 DCHECK(wfd_source_); | |
111 wfd_source_->RTSPDataReceived(data); | |
90 } | 112 } |
91 | 113 |
92 void WiFiDisplaySession::OnConnectionError() { | 114 std::string WiFiDisplaySession::GetLocalIPAddress() const { |
115 return ip_address_; | |
116 } | |
117 | |
118 int WiFiDisplaySession::GetNextCSeq(int* initial_peer_cseq) const { | |
119 static int sCseq = 0; | |
shalamov
2016/02/15 10:29:02
Make cseq a member.
| |
120 return ++sCseq; | |
121 } | |
122 | |
123 void WiFiDisplaySession::SendRTSPData(const std::string& message) { | |
124 service_->SendMessage(message); | |
125 } | |
126 | |
127 uint WiFiDisplaySession::CreateTimer(int seconds) { | |
128 static uint sTimerId = 0; | |
shalamov
2016/02/15 10:29:02
Make timerId as a class member.
| |
129 scoped_ptr<base::Timer> timer(new base::Timer(true, true)); | |
130 auto insert_ret = timers_.insert( | |
131 std::pair<uint, scoped_ptr<base::Timer>>( | |
132 ++sTimerId, std::move(timer))); | |
133 DCHECK(insert_ret.second); | |
134 timer->Start(FROM_HERE, | |
135 base::TimeDelta::FromSeconds(seconds), | |
136 base::Bind(&wds::Source::OnTimerEvent, | |
137 base::Unretained(wfd_source_.get()), | |
138 sTimerId)); | |
139 return sTimerId; | |
140 } | |
141 | |
142 void WiFiDisplaySession::ReleaseTimer(uint timer_id) { | |
143 auto it = timers_.find(timer_id); | |
144 if (it != timers_.end()) | |
145 timers_.erase(it); | |
146 } | |
147 | |
148 void WiFiDisplaySession::ErrorOccurred(wds::ErrorType error) { | |
149 if (!error_callback_.is_null()) { | |
150 if (error == wds::TimeoutError) { | |
151 error_callback_.Run(params_.sink_id, | |
152 api::display_source::ERROR_TYPE_TIMEOUT_ERROR, | |
153 kErrorTimeout); | |
154 } else { | |
155 error_callback_.Run(params_.sink_id, | |
156 api::display_source::ERROR_TYPE_UNKNOWN_ERROR, | |
157 kErrorInternal); | |
158 } | |
159 } | |
160 | |
161 Terminate(); | |
162 } | |
163 | |
164 void WiFiDisplaySession::SessionCompleted() { | |
165 Terminate(); | |
166 } | |
167 | |
168 void WiFiDisplaySession::OnIPCConnectionError() { | |
93 if (!error_callback_.is_null()) { | 169 if (!error_callback_.is_null()) { |
94 error_callback_.Run(params_.sink_id, | 170 error_callback_.Run(params_.sink_id, |
95 api::display_source::ERROR_TYPE_UNKNOWN_ERROR, | 171 api::display_source::ERROR_TYPE_UNKNOWN_ERROR, |
96 kErrorInternal); | 172 kErrorInternal); |
97 } | 173 } |
98 | 174 |
99 if (state_ != DisplaySourceSession::Idle) { | 175 if (state_ != DisplaySourceSession::Idle) { |
100 // We must explicitly notify the session termination as it will never | 176 // We must explicitly notify the session termination as it will never |
101 // arrive from browser process (IPC is broken). | 177 // arrive from browser process (IPC is broken). |
102 if (!terminated_callback_.is_null()) | 178 if (!terminated_callback_.is_null()) |
103 terminated_callback_.Run(params_.sink_id); | 179 terminated_callback_.Run(params_.sink_id); |
104 } | 180 } |
105 } | 181 } |
106 | 182 |
107 } // namespace extensions | 183 } // namespace extensions |
OLD | NEW |