Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(974)

Side by Side Diff: extensions/renderer/api/display_source/wifi_display/wifi_display_session.cc

Issue 1698473004: [chrome.displaySource] Use WDS for Wi-Fi Display implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from Alex Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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;
asargent_no_longer_on_chrome 2016/02/25 22:09:39 nit: the general policy is not to leave in logging
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() { 58 void WiFiDisplaySession::Start() {
42 DCHECK(state_ == DisplaySourceSession::Idle); 59 DCHECK(state_ == DisplaySourceSession::Idle);
43 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data); 60 service_->Connect(params_.sink_id, params_.auth_method, params_.auth_data);
44 state_ = DisplaySourceSession::Establishing; 61 state_ = DisplaySourceSession::Establishing;
(...skipping 14 matching lines...) Expand all
59 state_ = DisplaySourceSession::Terminating; 76 state_ = DisplaySourceSession::Terminating;
60 break; 77 break;
61 default: 78 default:
62 NOTREACHED(); 79 NOTREACHED();
63 } 80 }
64 } 81 }
65 82
66 void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) { 83 void WiFiDisplaySession::OnEstablished(const mojo::String& ip_address) {
67 DCHECK(state_ != DisplaySourceSession::Established); 84 DCHECK(state_ != DisplaySourceSession::Established);
68 ip_address_ = ip_address; 85 ip_address_ = ip_address;
86 media_manager_.reset(new WiFiDisplayMediaManager());
87 wfd_source_.reset(wds::Source::Create(this, media_manager_.get(), this));
88 wfd_source_->Start();
69 state_ = DisplaySourceSession::Established; 89 state_ = DisplaySourceSession::Established;
70 } 90 }
71 91
72 void WiFiDisplaySession::OnTerminated() { 92 void WiFiDisplaySession::OnTerminated() {
73 DCHECK(state_ != DisplaySourceSession::Idle); 93 DCHECK(state_ != DisplaySourceSession::Idle);
74 state_ = DisplaySourceSession::Idle; 94 state_ = DisplaySourceSession::Idle;
95 media_manager_.reset();
96 wfd_source_.reset();
75 if (!terminated_callback_.is_null()) 97 if (!terminated_callback_.is_null())
76 terminated_callback_.Run(params_.sink_id); 98 terminated_callback_.Run(params_.sink_id);
77 } 99 }
78 100
79 void WiFiDisplaySession::OnError(int32_t type, 101 void WiFiDisplaySession::OnError(int32_t type,
80 const mojo::String& description) { 102 const mojo::String& description) {
81 DCHECK(type > api::display_source::ERROR_TYPE_NONE 103 DCHECK(type > api::display_source::ERROR_TYPE_NONE
82 && type <= api::display_source::ERROR_TYPE_LAST); 104 && type <= api::display_source::ERROR_TYPE_LAST);
83 if (!error_callback_.is_null()) 105 if (!error_callback_.is_null())
84 error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type), 106 error_callback_.Run(params_.sink_id, static_cast<ErrorType>(type),
85 description); 107 description);
86 } 108 }
87 109
88 void WiFiDisplaySession::OnMessage(const mojo::String& data) { 110 void WiFiDisplaySession::OnMessage(const mojo::String& data) {
89 DCHECK(state_ == DisplaySourceSession::Established); 111 DCHECK(state_ == DisplaySourceSession::Established);
112 DCHECK(wfd_source_);
113 wfd_source_->RTSPDataReceived(data);
90 } 114 }
91 115
92 void WiFiDisplaySession::OnConnectionError() { 116 std::string WiFiDisplaySession::GetLocalIPAddress() const {
117 return ip_address_;
118 }
119
120 int WiFiDisplaySession::GetNextCSeq(int* initial_peer_cseq) const {
121 return ++cseq_;
122 }
123
124 void WiFiDisplaySession::SendRTSPData(const std::string& message) {
125 service_->SendMessage(message);
126 }
127
128 unsigned WiFiDisplaySession::CreateTimer(int seconds) {
129 scoped_ptr<base::Timer> timer(new base::Timer(true, true));
130 auto insert_ret = timers_.insert(
131 std::pair<unsigned, scoped_ptr<base::Timer>>(
132 ++timer_id_, 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 timer_id_));
139 return timer_id_;
140 }
141
142 void WiFiDisplaySession::ReleaseTimer(unsigned 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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698