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

Side by Side Diff: content/renderer/media/rtc_peer_connection_handler.cc

Issue 675013005: Split libjingle's signaling thread from the UI thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase after landing data channel change Created 6 years, 1 month 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 (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 "content/renderer/media/rtc_peer_connection_handler.h" 5 #include "content/renderer/media/rtc_peer_connection_handler.h"
6 6
7 #include <string> 7 #include <string>
8 #include <utility> 8 #include <utility>
9 #include <vector> 9 #include <vector>
10 10
(...skipping 23 matching lines...) Expand all
34 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" 34 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h"
35 #include "third_party/WebKit/public/platform/WebRTCConfiguration.h" 35 #include "third_party/WebKit/public/platform/WebRTCConfiguration.h"
36 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h" 36 #include "third_party/WebKit/public/platform/WebRTCDataChannelInit.h"
37 #include "third_party/WebKit/public/platform/WebRTCICECandidate.h" 37 #include "third_party/WebKit/public/platform/WebRTCICECandidate.h"
38 #include "third_party/WebKit/public/platform/WebRTCOfferOptions.h" 38 #include "third_party/WebKit/public/platform/WebRTCOfferOptions.h"
39 #include "third_party/WebKit/public/platform/WebRTCSessionDescription.h" 39 #include "third_party/WebKit/public/platform/WebRTCSessionDescription.h"
40 #include "third_party/WebKit/public/platform/WebRTCSessionDescriptionRequest.h" 40 #include "third_party/WebKit/public/platform/WebRTCSessionDescriptionRequest.h"
41 #include "third_party/WebKit/public/platform/WebRTCVoidRequest.h" 41 #include "third_party/WebKit/public/platform/WebRTCVoidRequest.h"
42 #include "third_party/WebKit/public/platform/WebURL.h" 42 #include "third_party/WebKit/public/platform/WebURL.h"
43 43
44 using webrtc::DataChannelInterface;
45 using webrtc::IceCandidateInterface;
46 using webrtc::MediaStreamInterface;
47 using webrtc::PeerConnectionInterface;
48 using webrtc::PeerConnectionObserver;
44 using webrtc::StatsReport; 49 using webrtc::StatsReport;
50 using webrtc::StatsReportCopyable;
45 using webrtc::StatsReports; 51 using webrtc::StatsReports;
46 52
47 namespace content { 53 namespace content {
48 54
49 // Converter functions from libjingle types to WebKit types. 55 // Converter functions from libjingle types to WebKit types.
50 blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState 56 blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState
51 GetWebKitIceGatheringState( 57 GetWebKitIceGatheringState(
52 webrtc::PeerConnectionInterface::IceGatheringState state) { 58 webrtc::PeerConnectionInterface::IceGatheringState state) {
53 using blink::WebRTCPeerConnectionHandlerClient; 59 using blink::WebRTCPeerConnectionHandlerClient;
54 switch (state) { 60 switch (state) {
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
164 } 170 }
165 } 171 }
166 172
167 class SessionDescriptionRequestTracker { 173 class SessionDescriptionRequestTracker {
168 public: 174 public:
169 SessionDescriptionRequestTracker(RTCPeerConnectionHandler* handler, 175 SessionDescriptionRequestTracker(RTCPeerConnectionHandler* handler,
170 PeerConnectionTracker::Action action) 176 PeerConnectionTracker::Action action)
171 : handler_(handler), action_(action) {} 177 : handler_(handler), action_(action) {}
172 178
173 void TrackOnSuccess(const webrtc::SessionDescriptionInterface* desc) { 179 void TrackOnSuccess(const webrtc::SessionDescriptionInterface* desc) {
180 DCHECK(thread_checker_.CalledOnValidThread());
174 std::string value; 181 std::string value;
175 if (desc) { 182 if (desc) {
176 desc->ToString(&value); 183 desc->ToString(&value);
177 value = "type: " + desc->type() + ", sdp: " + value; 184 value = "type: " + desc->type() + ", sdp: " + value;
178 } 185 }
179 if (handler_->peer_connection_tracker()) 186 if (handler_->peer_connection_tracker()) {
180 handler_->peer_connection_tracker()->TrackSessionDescriptionCallback( 187 handler_->peer_connection_tracker()->TrackSessionDescriptionCallback(
181 handler_, action_, "OnSuccess", value); 188 handler_, action_, "OnSuccess", value);
189 }
182 } 190 }
183 191
184 void TrackOnFailure(const std::string& error) { 192 void TrackOnFailure(const std::string& error) {
185 if (handler_->peer_connection_tracker()) 193 DCHECK(thread_checker_.CalledOnValidThread());
194 if (handler_->peer_connection_tracker()) {
186 handler_->peer_connection_tracker()->TrackSessionDescriptionCallback( 195 handler_->peer_connection_tracker()->TrackSessionDescriptionCallback(
187 handler_, action_, "OnFailure", error); 196 handler_, action_, "OnFailure", error);
197 }
188 } 198 }
189 199
190 private: 200 private:
191 RTCPeerConnectionHandler* handler_; 201 RTCPeerConnectionHandler* handler_;
192 PeerConnectionTracker::Action action_; 202 PeerConnectionTracker::Action action_;
203 base::ThreadChecker thread_checker_;
193 }; 204 };
194 205
195 // Class mapping responses from calls to libjingle CreateOffer/Answer and 206 // Class mapping responses from calls to libjingle CreateOffer/Answer and
196 // the blink::WebRTCSessionDescriptionRequest. 207 // the blink::WebRTCSessionDescriptionRequest.
197 class CreateSessionDescriptionRequest 208 class CreateSessionDescriptionRequest
198 : public webrtc::CreateSessionDescriptionObserver { 209 : public webrtc::CreateSessionDescriptionObserver {
199 public: 210 public:
200 explicit CreateSessionDescriptionRequest( 211 explicit CreateSessionDescriptionRequest(
212 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
201 const blink::WebRTCSessionDescriptionRequest& request, 213 const blink::WebRTCSessionDescriptionRequest& request,
202 RTCPeerConnectionHandler* handler, 214 RTCPeerConnectionHandler* handler,
203 PeerConnectionTracker::Action action) 215 PeerConnectionTracker::Action action)
204 : webkit_request_(request), tracker_(handler, action) {} 216 : main_thread_(main_thread),
217 webkit_request_(request),
218 tracker_(handler, action) {
219 }
205 220
206 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override { 221 void OnSuccess(webrtc::SessionDescriptionInterface* desc) override {
222 if (!main_thread_->BelongsToCurrentThread()) {
perkj_chrome 2014/10/30 12:42:35 Why is this? To avoid having a separate method OnS
tommi (sloooow) - chröme 2014/10/30 20:37:36 Yes, this is first always called on the signaling
223 main_thread_->PostTask(FROM_HERE,
224 base::Bind(&CreateSessionDescriptionRequest::OnSuccess, this, desc));
225 return;
226 }
227
207 tracker_.TrackOnSuccess(desc); 228 tracker_.TrackOnSuccess(desc);
208 webkit_request_.requestSucceeded(CreateWebKitSessionDescription(desc)); 229 webkit_request_.requestSucceeded(CreateWebKitSessionDescription(desc));
209 delete desc; 230 delete desc;
210 } 231 }
211 void OnFailure(const std::string& error) override { 232 void OnFailure(const std::string& error) override {
233 if (!main_thread_->BelongsToCurrentThread()) {
234 main_thread_->PostTask(FROM_HERE,
235 base::Bind(&CreateSessionDescriptionRequest::OnFailure, this, error));
236 return;
237 }
238
212 tracker_.TrackOnFailure(error); 239 tracker_.TrackOnFailure(error);
213 webkit_request_.requestFailed(base::UTF8ToUTF16(error)); 240 webkit_request_.requestFailed(base::UTF8ToUTF16(error));
214 } 241 }
215 242
216 protected: 243 protected:
217 ~CreateSessionDescriptionRequest() override {} 244 ~CreateSessionDescriptionRequest() override {}
218 245
219 private: 246 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
220 blink::WebRTCSessionDescriptionRequest webkit_request_; 247 blink::WebRTCSessionDescriptionRequest webkit_request_;
221 SessionDescriptionRequestTracker tracker_; 248 SessionDescriptionRequestTracker tracker_;
222 }; 249 };
223 250
224 // Class mapping responses from calls to libjingle 251 // Class mapping responses from calls to libjingle
225 // SetLocalDescription/SetRemoteDescription and a blink::WebRTCVoidRequest. 252 // SetLocalDescription/SetRemoteDescription and a blink::WebRTCVoidRequest.
226 class SetSessionDescriptionRequest 253 class SetSessionDescriptionRequest
227 : public webrtc::SetSessionDescriptionObserver { 254 : public webrtc::SetSessionDescriptionObserver {
228 public: 255 public:
229 explicit SetSessionDescriptionRequest( 256 explicit SetSessionDescriptionRequest(
257 const scoped_refptr<base::SingleThreadTaskRunner>& main_thread,
230 const blink::WebRTCVoidRequest& request, 258 const blink::WebRTCVoidRequest& request,
231 RTCPeerConnectionHandler* handler, 259 RTCPeerConnectionHandler* handler,
232 PeerConnectionTracker::Action action) 260 PeerConnectionTracker::Action action)
233 : webkit_request_(request), tracker_(handler, action) {} 261 : main_thread_(main_thread),
262 webkit_request_(request),
263 tracker_(handler, action) {
264 }
234 265
235 void OnSuccess() override { 266 void OnSuccess() override {
267 if (!main_thread_->BelongsToCurrentThread()) {
perkj_chrome 2014/10/30 12:42:35 Same question as 222.
tommi (sloooow) - chröme 2014/10/30 20:37:36 Let me know if you prefer to split these up into s
perkj_chrome 2014/10/31 08:37:24 I personally think this is fine at least on this t
268 main_thread_->PostTask(FROM_HERE,
269 base::Bind(&SetSessionDescriptionRequest::OnSuccess, this));
270 return;
271 }
236 tracker_.TrackOnSuccess(NULL); 272 tracker_.TrackOnSuccess(NULL);
237 webkit_request_.requestSucceeded(); 273 webkit_request_.requestSucceeded();
238 } 274 }
239 void OnFailure(const std::string& error) override { 275 void OnFailure(const std::string& error) override {
276 if (!main_thread_->BelongsToCurrentThread()) {
277 main_thread_->PostTask(FROM_HERE,
278 base::Bind(&SetSessionDescriptionRequest::OnFailure, this, error));
279 return;
280 }
240 tracker_.TrackOnFailure(error); 281 tracker_.TrackOnFailure(error);
241 webkit_request_.requestFailed(base::UTF8ToUTF16(error)); 282 webkit_request_.requestFailed(base::UTF8ToUTF16(error));
242 } 283 }
243 284
244 protected: 285 protected:
245 ~SetSessionDescriptionRequest() override {} 286 ~SetSessionDescriptionRequest() override {}
246 287
247 private: 288 private:
289 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
248 blink::WebRTCVoidRequest webkit_request_; 290 blink::WebRTCVoidRequest webkit_request_;
249 SessionDescriptionRequestTracker tracker_; 291 SessionDescriptionRequestTracker tracker_;
250 }; 292 };
251 293
252 // Class mapping responses from calls to libjingle 294 // Class mapping responses from calls to libjingle
253 // GetStats into a blink::WebRTCStatsCallback. 295 // GetStats into a blink::WebRTCStatsCallback.
254 class StatsResponse : public webrtc::StatsObserver { 296 class StatsResponse : public webrtc::StatsObserver {
255 public: 297 public:
256 explicit StatsResponse(const scoped_refptr<LocalRTCStatsRequest>& request) 298 explicit StatsResponse(const scoped_refptr<LocalRTCStatsRequest>& request)
257 : request_(request.get()), response_(request_->createResponse().get()) { 299 : request_(request.get()),
300 main_thread_(base::ThreadTaskRunnerHandle::Get()) {
258 // Measure the overall time it takes to satisfy a getStats request. 301 // Measure the overall time it takes to satisfy a getStats request.
259 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "getStats_Native", this); 302 TRACE_EVENT_ASYNC_BEGIN0("webrtc", "getStats_Native", this);
260 } 303 }
261 304
262 void OnComplete(const StatsReports& reports) override { 305 void OnComplete(const StatsReports& reports) override {
263 TRACE_EVENT0("webrtc", "StatsResponse::OnComplete") 306 TRACE_EVENT0("webrtc", "StatsResponse::OnComplete");
264 for (StatsReports::const_iterator it = reports.begin(); 307 DCHECK(reports_.empty());
265 it != reports.end(); ++it) { 308 reports_.reserve(reports.size());
perkj_chrome 2014/10/30 12:42:35 reports_ are used to two threads. Please add threa
tommi (sloooow) - chröme 2014/10/30 20:37:36 ah, right, there's no need for that (meant to do t
266 if ((*it)->values.size() > 0) { 309 for (auto it : reports)
267 AddReport(*(*it)); 310 reports_.push_back(StatsReportCopyable(*it));
268 } 311
312 main_thread_->PostTask(FROM_HERE,
313 base::Bind(&StatsResponse::DeliverCallback, this));
314 }
315
316 private:
317 void DeliverCallback() {
318 DCHECK(main_thread_->BelongsToCurrentThread());
319 TRACE_EVENT0("webrtc", "StatsResponse::DeliverCallback");
320
321 // TODO: response_ doesn't need to be a member.
perkj_chrome 2014/10/30 12:42:35 please fix.
tommi (sloooow) - chröme 2014/10/30 20:37:36 Done.
322 response_ = request_->createResponse().get();
323 for (const auto& report : reports_) {
324 if (report.values.size() > 0)
325 AddReport(report);
269 } 326 }
270 327
328 reports_.clear();
329
271 // Record the getSync operation as done before calling into Blink so that 330 // Record the getSync operation as done before calling into Blink so that
272 // we don't skew the perf measurements of the native code with whatever the 331 // we don't skew the perf measurements of the native code with whatever the
273 // callback might be doing. 332 // callback might be doing.
274 TRACE_EVENT_ASYNC_END0("webrtc", "getStats_Native", this); 333 TRACE_EVENT_ASYNC_END0("webrtc", "getStats_Native", this);
275
276 request_->requestSucceeded(response_); 334 request_->requestSucceeded(response_);
277 } 335 }
278 336
279 private:
280 void AddReport(const StatsReport& report) { 337 void AddReport(const StatsReport& report) {
281 int idx = response_->addReport(blink::WebString::fromUTF8(report.id), 338 int idx = response_->addReport(blink::WebString::fromUTF8(report.id),
282 blink::WebString::fromUTF8(report.type), 339 blink::WebString::fromUTF8(report.type),
283 report.timestamp); 340 report.timestamp);
284 for (StatsReport::Values::const_iterator value_it = report.values.begin(); 341 for (StatsReport::Values::const_iterator value_it = report.values.begin();
285 value_it != report.values.end(); ++value_it) { 342 value_it != report.values.end(); ++value_it) {
286 AddStatistic(idx, value_it->display_name(), value_it->value); 343 AddStatistic(idx, value_it->display_name(), value_it->value);
287 } 344 }
288 } 345 }
289 346
290 void AddStatistic(int idx, const char* name, const std::string& value) { 347 void AddStatistic(int idx, const char* name, const std::string& value) {
291 response_->addStatistic(idx, 348 response_->addStatistic(idx,
292 blink::WebString::fromUTF8(name), 349 blink::WebString::fromUTF8(name),
293 blink::WebString::fromUTF8(value)); 350 blink::WebString::fromUTF8(value));
294 } 351 }
295 352
296 rtc::scoped_refptr<LocalRTCStatsRequest> request_; 353 rtc::scoped_refptr<LocalRTCStatsRequest> request_;
354 std::vector<StatsReportCopyable> reports_;
297 rtc::scoped_refptr<LocalRTCStatsResponse> response_; 355 rtc::scoped_refptr<LocalRTCStatsResponse> response_;
356 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
298 }; 357 };
299 358
300 // Implementation of LocalRTCStatsRequest. 359 // Implementation of LocalRTCStatsRequest.
301 LocalRTCStatsRequest::LocalRTCStatsRequest(blink::WebRTCStatsRequest impl) 360 LocalRTCStatsRequest::LocalRTCStatsRequest(blink::WebRTCStatsRequest impl)
302 : impl_(impl), 361 : impl_(impl) {
303 response_(NULL) {
304 } 362 }
305 363
306 LocalRTCStatsRequest::LocalRTCStatsRequest() {} 364 LocalRTCStatsRequest::LocalRTCStatsRequest() {}
307 LocalRTCStatsRequest::~LocalRTCStatsRequest() {} 365 LocalRTCStatsRequest::~LocalRTCStatsRequest() {}
308 366
309 bool LocalRTCStatsRequest::hasSelector() const { 367 bool LocalRTCStatsRequest::hasSelector() const {
310 return impl_.hasSelector(); 368 return impl_.hasSelector();
311 } 369 }
312 370
313 blink::WebMediaStreamTrack LocalRTCStatsRequest::component() const { 371 blink::WebMediaStreamTrack LocalRTCStatsRequest::component() const {
314 return impl_.component(); 372 return impl_.component();
315 } 373 }
316 374
317 scoped_refptr<LocalRTCStatsResponse> LocalRTCStatsRequest::createResponse() { 375 scoped_refptr<LocalRTCStatsResponse> LocalRTCStatsRequest::createResponse() {
318 DCHECK(!response_); 376 return scoped_refptr<LocalRTCStatsResponse>(
319 response_ = new rtc::RefCountedObject<LocalRTCStatsResponse>( 377 new rtc::RefCountedObject<LocalRTCStatsResponse>(impl_.createResponse()));
320 impl_.createResponse());
321 return response_.get();
322 } 378 }
323 379
324 void LocalRTCStatsRequest::requestSucceeded( 380 void LocalRTCStatsRequest::requestSucceeded(
325 const LocalRTCStatsResponse* response) { 381 const LocalRTCStatsResponse* response) {
326 impl_.requestSucceeded(response->webKitStatsResponse()); 382 impl_.requestSucceeded(response->webKitStatsResponse());
327 } 383 }
328 384
329 // Implementation of LocalRTCStatsResponse. 385 // Implementation of LocalRTCStatsResponse.
330 blink::WebRTCStatsResponse LocalRTCStatsResponse::webKitStatsResponse() const { 386 blink::WebRTCStatsResponse LocalRTCStatsResponse::webKitStatsResponse() const {
331 return impl_; 387 return impl_;
332 } 388 }
333 389
334 size_t LocalRTCStatsResponse::addReport(blink::WebString type, 390 size_t LocalRTCStatsResponse::addReport(blink::WebString type,
335 blink::WebString id, 391 blink::WebString id,
336 double timestamp) { 392 double timestamp) {
337 return impl_.addReport(type, id, timestamp); 393 return impl_.addReport(type, id, timestamp);
338 } 394 }
339 395
340 void LocalRTCStatsResponse::addStatistic(size_t report, 396 void LocalRTCStatsResponse::addStatistic(size_t report,
341 blink::WebString name, 397 blink::WebString name,
342 blink::WebString value) { 398 blink::WebString value) {
343 impl_.addStatistic(report, name, value); 399 impl_.addStatistic(report, name, value);
344 } 400 }
345 401
346 namespace { 402 namespace {
347 403
404 void GetStatsOnSignalingThread(
405 const scoped_refptr<webrtc::PeerConnectionInterface>& pc,
406 webrtc::PeerConnectionInterface::StatsOutputLevel level,
407 const scoped_refptr<webrtc::StatsObserver>& observer,
408 const scoped_refptr<webrtc::MediaStreamTrackInterface>& track) {
409 TRACE_EVENT0("webrtc", "GetStatsOnSignalingThread");
410 if (!pc->GetStats(observer.get(), track.get(), level)) {
411 DVLOG(1) << "GetStats failed.";
412 // TODO(hta): Consider how to get an error back.
413 observer->OnComplete(StatsReports());
414 }
415 }
416
348 class PeerConnectionUMAObserver : public webrtc::UMAObserver { 417 class PeerConnectionUMAObserver : public webrtc::UMAObserver {
349 public: 418 public:
350 PeerConnectionUMAObserver() {} 419 PeerConnectionUMAObserver() {}
351 ~PeerConnectionUMAObserver() override {} 420 ~PeerConnectionUMAObserver() override {}
352 421
353 void IncrementCounter( 422 void IncrementCounter(
354 webrtc::PeerConnectionUMAMetricsCounter counter) override { 423 webrtc::PeerConnectionUMAMetricsCounter counter) override {
424 // Runs on libjingle's signaling thread.
355 UMA_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics", 425 UMA_HISTOGRAM_ENUMERATION("WebRTC.PeerConnection.IPMetrics",
356 counter, 426 counter,
357 webrtc::kBoundary); 427 webrtc::kBoundary);
358 } 428 }
359 429
360 void AddHistogramSample(webrtc::PeerConnectionUMAMetricsName type, 430 void AddHistogramSample(webrtc::PeerConnectionUMAMetricsName type,
361 int value) override { 431 int value) override {
432 // Runs on libjingle's signaling thread.
362 switch (type) { 433 switch (type) {
363 case webrtc::kTimeToConnect: 434 case webrtc::kTimeToConnect:
364 UMA_HISTOGRAM_MEDIUM_TIMES( 435 UMA_HISTOGRAM_MEDIUM_TIMES(
365 "WebRTC.PeerConnection.TimeToConnect", 436 "WebRTC.PeerConnection.TimeToConnect",
366 base::TimeDelta::FromMilliseconds(value)); 437 base::TimeDelta::FromMilliseconds(value));
367 break; 438 break;
368 case webrtc::kNetworkInterfaces_IPv4: 439 case webrtc::kNetworkInterfaces_IPv4:
369 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces", 440 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4Interfaces",
370 value); 441 value);
371 break; 442 break;
372 case webrtc::kNetworkInterfaces_IPv6: 443 case webrtc::kNetworkInterfaces_IPv6:
373 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces", 444 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6Interfaces",
374 value); 445 value);
375 break; 446 break;
376 default: 447 default:
377 NOTREACHED(); 448 NOTREACHED();
378 } 449 }
379 } 450 }
380 }; 451 };
381 452
382 base::LazyInstance<std::set<RTCPeerConnectionHandler*> >::Leaky 453 base::LazyInstance<std::set<RTCPeerConnectionHandler*> >::Leaky
383 g_peer_connection_handlers = LAZY_INSTANCE_INITIALIZER; 454 g_peer_connection_handlers = LAZY_INSTANCE_INITIALIZER;
384 455
385 } // namespace 456 } // namespace
386 457
458 class RTCPeerConnectionHandler::Observer
perkj_chrome 2014/10/30 12:42:35 Please add a comment about the purpose of this cla
tommi (sloooow) - chröme 2014/10/30 20:37:36 Done.
459 : public base::RefCountedThreadSafe<RTCPeerConnectionHandler::Observer>,
460 public PeerConnectionObserver {
461 public:
462 Observer(const base::WeakPtr<RTCPeerConnectionHandler>& handler)
463 : handler_(handler), main_thread_(base::ThreadTaskRunnerHandle::Get()) {}
464
465 protected:
466 friend class base::RefCountedThreadSafe<RTCPeerConnectionHandler::Observer>;
467 virtual ~Observer() {}
468
469 void OnError() override {
470 // TODO(perkj): Implement.
perkj_chrome 2014/10/30 12:42:35 Or rater remove from the pc interface.
tommi (sloooow) - chröme 2014/10/30 20:37:36 Done.
471 NOTIMPLEMENTED();
472 }
473
474 void OnSignalingChange(
475 PeerConnectionInterface::SignalingState new_state) override {
476 if (!main_thread_->BelongsToCurrentThread()) {
477 main_thread_->PostTask(FROM_HERE,
478 base::Bind(&RTCPeerConnectionHandler::Observer::OnSignalingChange,
479 this, new_state));
480 } else if (handler_) {
481 handler_->OnSignalingChange(new_state);
482 }
483 }
484
485 void OnAddStream(MediaStreamInterface* stream) override {
486 DCHECK(stream);
487 scoped_ptr<RemoteMediaStreamImpl> remote_stream(
488 new RemoteMediaStreamImpl(main_thread_, stream));
perkj_chrome 2014/10/30 12:42:35 So this is instantiating |remote_stream| on the si
tommi (sloooow) - chröme 2014/10/30 20:37:36 Discussed offline
489
490 // The webkit object owned by RemoteMediaStreamImpl, will be initialized
491 // asynchronously and the posted task will execude after that initialization
492 // is done.
493 main_thread_->PostTask(FROM_HERE,
494 base::Bind(&RTCPeerConnectionHandler::Observer::OnAddStreamImpl,
495 this, base::Passed(&remote_stream)));
496 }
497
498 void OnRemoveStream(MediaStreamInterface* stream) override {
499 main_thread_->PostTask(FROM_HERE,
500 base::Bind(&RTCPeerConnectionHandler::Observer::OnRemoveStreamImpl,
501 this, make_scoped_refptr(stream)));
502 }
503
504 void OnDataChannel(DataChannelInterface* data_channel) override {
505 scoped_ptr<RtcDataChannelHandler> handler(
506 new RtcDataChannelHandler(main_thread_, data_channel));
507 main_thread_->PostTask(FROM_HERE,
508 base::Bind(&RTCPeerConnectionHandler::Observer::OnDataChannelImpl,
509 this, base::Passed(&handler)));
510 }
511
512 void OnRenegotiationNeeded() override {
513 if (!main_thread_->BelongsToCurrentThread()) {
514 main_thread_->PostTask(FROM_HERE,
515 base::Bind(&RTCPeerConnectionHandler::Observer::OnRenegotiationNeeded,
516 this));
517 } else if (handler_) {
518 handler_->OnRenegotiationNeeded();
519 }
520 }
521
522 void OnIceConnectionChange(
523 PeerConnectionInterface::IceConnectionState new_state) override {
524 if (!main_thread_->BelongsToCurrentThread()) {
525 main_thread_->PostTask(FROM_HERE,
526 base::Bind(
527 &RTCPeerConnectionHandler::Observer::OnIceConnectionChange, this,
528 new_state));
529 } else if (handler_) {
530 handler_->OnIceConnectionChange(new_state);
531 }
532 }
533
534 void OnIceGatheringChange(
535 PeerConnectionInterface::IceGatheringState new_state) override {
536 if (!main_thread_->BelongsToCurrentThread()) {
537 main_thread_->PostTask(FROM_HERE,
538 base::Bind(&RTCPeerConnectionHandler::Observer::OnIceGatheringChange,
539 this, new_state));
540 } else if (handler_) {
541 handler_->OnIceGatheringChange(new_state);
542 }
543 }
544
545 void OnIceCandidate(const IceCandidateInterface* candidate) override {
546 std::string sdp;
547 if (!candidate->ToString(&sdp)) {
548 NOTREACHED() << "OnIceCandidate: Could not get SDP string.";
549 return;
550 }
551
552 main_thread_->PostTask(FROM_HERE,
553 base::Bind(&RTCPeerConnectionHandler::Observer::OnIceCandidateImpl,
554 this, sdp, candidate->sdp_mid(), candidate->sdp_mline_index(),
555 candidate->candidate().component(),
556 candidate->candidate().address().family()));
557 }
558
559 void OnAddStreamImpl(scoped_ptr<RemoteMediaStreamImpl> stream) {
560 DCHECK(stream->webkit_stream().extraData()) << "Initialization not done";
561 if (handler_)
562 handler_->OnAddStream(stream.Pass());
563 }
564
565 void OnRemoveStreamImpl(const scoped_refptr<MediaStreamInterface>& stream) {
566 if (handler_)
567 handler_->OnRemoveStream(stream);
568 }
569
570 void OnDataChannelImpl(scoped_ptr<RtcDataChannelHandler> handler) {
571 if (handler_)
572 handler_->OnDataChannel(handler.Pass());
573 }
574
575 void OnIceCandidateImpl(const std::string& sdp, const std::string& sdp_mid,
576 int sdp_mline_index, int component, int address_family) {
577 if (handler_) {
578 handler_->OnIceCandidate(sdp, sdp_mid, sdp_mline_index, component,
579 address_family);
580 }
581 }
582
583 private:
584 const base::WeakPtr<RTCPeerConnectionHandler> handler_;
585 const scoped_refptr<base::SingleThreadTaskRunner> main_thread_;
586 };
587
387 RTCPeerConnectionHandler::RTCPeerConnectionHandler( 588 RTCPeerConnectionHandler::RTCPeerConnectionHandler(
388 blink::WebRTCPeerConnectionHandlerClient* client, 589 blink::WebRTCPeerConnectionHandlerClient* client,
389 PeerConnectionDependencyFactory* dependency_factory) 590 PeerConnectionDependencyFactory* dependency_factory,
591 const scoped_refptr<base::SingleThreadTaskRunner>& signaling_thread)
390 : client_(client), 592 : client_(client),
391 dependency_factory_(dependency_factory), 593 dependency_factory_(dependency_factory),
392 frame_(NULL), 594 frame_(NULL),
595 signaling_thread_(signaling_thread),
393 peer_connection_tracker_(NULL), 596 peer_connection_tracker_(NULL),
394 num_data_channels_created_(0), 597 num_data_channels_created_(0),
395 num_local_candidates_ipv4_(0), 598 num_local_candidates_ipv4_(0),
396 num_local_candidates_ipv6_(0) { 599 num_local_candidates_ipv6_(0),
600 weak_factory_(this) {
397 g_peer_connection_handlers.Get().insert(this); 601 g_peer_connection_handlers.Get().insert(this);
398 } 602 }
399 603
400 RTCPeerConnectionHandler::~RTCPeerConnectionHandler() { 604 RTCPeerConnectionHandler::~RTCPeerConnectionHandler() {
605 DCHECK(thread_checker_.CalledOnValidThread());
401 g_peer_connection_handlers.Get().erase(this); 606 g_peer_connection_handlers.Get().erase(this);
402 if (peer_connection_tracker_) 607 if (peer_connection_tracker_)
403 peer_connection_tracker_->UnregisterPeerConnection(this); 608 peer_connection_tracker_->UnregisterPeerConnection(this);
404 STLDeleteValues(&remote_streams_); 609 STLDeleteValues(&remote_streams_);
405 610
406 UMA_HISTOGRAM_COUNTS_10000( 611 UMA_HISTOGRAM_COUNTS_10000(
407 "WebRTC.NumDataChannelsPerPeerConnection", num_data_channels_created_); 612 "WebRTC.NumDataChannelsPerPeerConnection", num_data_channels_created_);
408 } 613 }
409 614
410 // static 615 // static
411 void RTCPeerConnectionHandler::DestructAllHandlers() { 616 void RTCPeerConnectionHandler::DestructAllHandlers() {
412 std::set<RTCPeerConnectionHandler*> handlers( 617 std::set<RTCPeerConnectionHandler*> handlers(
413 g_peer_connection_handlers.Get().begin(), 618 g_peer_connection_handlers.Get().begin(),
414 g_peer_connection_handlers.Get().end()); 619 g_peer_connection_handlers.Get().end());
415 for (std::set<RTCPeerConnectionHandler*>::iterator handler = handlers.begin(); 620 for (auto handler : handlers) {
416 handler != handlers.end(); 621 if (handler->client_)
417 ++handler) { 622 handler->client_->releasePeerConnectionHandler();
418 (*handler)->client_->releasePeerConnectionHandler();
419 } 623 }
420 } 624 }
421 625
626 // static
422 void RTCPeerConnectionHandler::ConvertOfferOptionsToConstraints( 627 void RTCPeerConnectionHandler::ConvertOfferOptionsToConstraints(
423 const blink::WebRTCOfferOptions& options, 628 const blink::WebRTCOfferOptions& options,
424 RTCMediaConstraints* output) { 629 RTCMediaConstraints* output) {
425 output->AddMandatory( 630 output->AddMandatory(
426 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio, 631 webrtc::MediaConstraintsInterface::kOfferToReceiveAudio,
427 options.offerToReceiveAudio() > 0 ? "true" : "false", 632 options.offerToReceiveAudio() > 0 ? "true" : "false",
428 true); 633 true);
429 634
430 output->AddMandatory( 635 output->AddMandatory(
431 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo, 636 webrtc::MediaConstraintsInterface::kOfferToReceiveVideo,
432 options.offerToReceiveVideo() > 0 ? "true" : "false", 637 options.offerToReceiveVideo() > 0 ? "true" : "false",
433 true); 638 true);
434 639
435 if (!options.voiceActivityDetection()) { 640 if (!options.voiceActivityDetection()) {
436 output->AddMandatory( 641 output->AddMandatory(
437 webrtc::MediaConstraintsInterface::kVoiceActivityDetection, 642 webrtc::MediaConstraintsInterface::kVoiceActivityDetection,
438 "false", 643 "false",
439 true); 644 true);
440 } 645 }
441 646
442 if (options.iceRestart()) { 647 if (options.iceRestart()) {
443 output->AddMandatory( 648 output->AddMandatory(
444 webrtc::MediaConstraintsInterface::kIceRestart, "true", true); 649 webrtc::MediaConstraintsInterface::kIceRestart, "true", true);
445 } 650 }
446 } 651 }
447 652
448 void RTCPeerConnectionHandler::associateWithFrame(blink::WebFrame* frame) { 653 void RTCPeerConnectionHandler::associateWithFrame(blink::WebFrame* frame) {
654 DCHECK(thread_checker_.CalledOnValidThread());
449 DCHECK(frame); 655 DCHECK(frame);
450 frame_ = frame; 656 frame_ = frame;
451 } 657 }
452 658
453 bool RTCPeerConnectionHandler::initialize( 659 bool RTCPeerConnectionHandler::initialize(
454 const blink::WebRTCConfiguration& server_configuration, 660 const blink::WebRTCConfiguration& server_configuration,
455 const blink::WebMediaConstraints& options) { 661 const blink::WebMediaConstraints& options) {
662 DCHECK(thread_checker_.CalledOnValidThread());
456 DCHECK(frame_); 663 DCHECK(frame_);
457 664
458 peer_connection_tracker_ = 665 peer_connection_tracker_ =
459 RenderThreadImpl::current()->peer_connection_tracker(); 666 RenderThreadImpl::current()->peer_connection_tracker();
460 667
461 webrtc::PeerConnectionInterface::RTCConfiguration config; 668 webrtc::PeerConnectionInterface::RTCConfiguration config;
462 GetNativeRtcConfiguration(server_configuration, &config); 669 GetNativeRtcConfiguration(server_configuration, &config);
463 670
464 RTCMediaConstraints constraints(options); 671 RTCMediaConstraints constraints(options);
465 672
466 native_peer_connection_ = 673 peer_connection_observer_ = new Observer(weak_factory_.GetWeakPtr());
467 dependency_factory_->CreatePeerConnection( 674 native_peer_connection_ = dependency_factory_->CreatePeerConnection(
468 config, &constraints, frame_, this); 675 config, &constraints, frame_, peer_connection_observer_.get());
469 676
470 if (!native_peer_connection_.get()) { 677 if (!native_peer_connection_.get()) {
471 LOG(ERROR) << "Failed to initialize native PeerConnection."; 678 LOG(ERROR) << "Failed to initialize native PeerConnection.";
472 return false; 679 return false;
473 } 680 }
474 if (peer_connection_tracker_) 681
682 if (peer_connection_tracker_) {
475 peer_connection_tracker_->RegisterPeerConnection( 683 peer_connection_tracker_->RegisterPeerConnection(
476 this, config, constraints, frame_); 684 this, config, constraints, frame_);
685 }
477 686
478 uma_observer_ = new rtc::RefCountedObject<PeerConnectionUMAObserver>(); 687 uma_observer_ = new rtc::RefCountedObject<PeerConnectionUMAObserver>();
479 native_peer_connection_->RegisterUMAObserver(uma_observer_.get()); 688 native_peer_connection_->RegisterUMAObserver(uma_observer_.get());
480 return true; 689 return true;
481 } 690 }
482 691
483 bool RTCPeerConnectionHandler::InitializeForTest( 692 bool RTCPeerConnectionHandler::InitializeForTest(
484 const blink::WebRTCConfiguration& server_configuration, 693 const blink::WebRTCConfiguration& server_configuration,
485 const blink::WebMediaConstraints& options, 694 const blink::WebMediaConstraints& options,
486 PeerConnectionTracker* peer_connection_tracker) { 695 PeerConnectionTracker* peer_connection_tracker) {
696 DCHECK(thread_checker_.CalledOnValidThread());
487 webrtc::PeerConnectionInterface::RTCConfiguration config; 697 webrtc::PeerConnectionInterface::RTCConfiguration config;
488 GetNativeRtcConfiguration(server_configuration, &config); 698 GetNativeRtcConfiguration(server_configuration, &config);
489 699
700 peer_connection_observer_ = new Observer(weak_factory_.GetWeakPtr());
490 RTCMediaConstraints constraints(options); 701 RTCMediaConstraints constraints(options);
491 native_peer_connection_ = 702 native_peer_connection_ = dependency_factory_->CreatePeerConnection(
492 dependency_factory_->CreatePeerConnection( 703 config, &constraints, NULL, peer_connection_observer_.get());
493 config, &constraints, NULL, this);
494 if (!native_peer_connection_.get()) { 704 if (!native_peer_connection_.get()) {
495 LOG(ERROR) << "Failed to initialize native PeerConnection."; 705 LOG(ERROR) << "Failed to initialize native PeerConnection.";
496 return false; 706 return false;
497 } 707 }
498 peer_connection_tracker_ = peer_connection_tracker; 708 peer_connection_tracker_ = peer_connection_tracker;
499 return true; 709 return true;
500 } 710 }
501 711
502 void RTCPeerConnectionHandler::createOffer( 712 void RTCPeerConnectionHandler::createOffer(
503 const blink::WebRTCSessionDescriptionRequest& request, 713 const blink::WebRTCSessionDescriptionRequest& request,
504 const blink::WebMediaConstraints& options) { 714 const blink::WebMediaConstraints& options) {
715 DCHECK(thread_checker_.CalledOnValidThread());
505 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createOffer"); 716 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createOffer");
717
506 scoped_refptr<CreateSessionDescriptionRequest> description_request( 718 scoped_refptr<CreateSessionDescriptionRequest> description_request(
507 new rtc::RefCountedObject<CreateSessionDescriptionRequest>( 719 new rtc::RefCountedObject<CreateSessionDescriptionRequest>(
508 request, this, PeerConnectionTracker::ACTION_CREATE_OFFER)); 720 base::ThreadTaskRunnerHandle::Get(), request, this,
721 PeerConnectionTracker::ACTION_CREATE_OFFER));
722
509 RTCMediaConstraints constraints(options); 723 RTCMediaConstraints constraints(options);
510 native_peer_connection_->CreateOffer(description_request.get(), &constraints); 724 native_peer_connection_->CreateOffer(description_request.get(), &constraints);
511 725
512 if (peer_connection_tracker_) 726 if (peer_connection_tracker_)
513 peer_connection_tracker_->TrackCreateOffer(this, constraints); 727 peer_connection_tracker_->TrackCreateOffer(this, constraints);
514 } 728 }
515 729
516 void RTCPeerConnectionHandler::createOffer( 730 void RTCPeerConnectionHandler::createOffer(
517 const blink::WebRTCSessionDescriptionRequest& request, 731 const blink::WebRTCSessionDescriptionRequest& request,
518 const blink::WebRTCOfferOptions& options) { 732 const blink::WebRTCOfferOptions& options) {
733 DCHECK(thread_checker_.CalledOnValidThread());
519 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createOffer"); 734 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createOffer");
735
520 scoped_refptr<CreateSessionDescriptionRequest> description_request( 736 scoped_refptr<CreateSessionDescriptionRequest> description_request(
521 new rtc::RefCountedObject<CreateSessionDescriptionRequest>( 737 new rtc::RefCountedObject<CreateSessionDescriptionRequest>(
522 request, this, PeerConnectionTracker::ACTION_CREATE_OFFER)); 738 base::ThreadTaskRunnerHandle::Get(), request, this,
739 PeerConnectionTracker::ACTION_CREATE_OFFER));
523 740
524 RTCMediaConstraints constraints; 741 RTCMediaConstraints constraints;
525 ConvertOfferOptionsToConstraints(options, &constraints); 742 ConvertOfferOptionsToConstraints(options, &constraints);
526 native_peer_connection_->CreateOffer(description_request.get(), &constraints); 743 native_peer_connection_->CreateOffer(description_request.get(), &constraints);
527 744
528 if (peer_connection_tracker_) 745 if (peer_connection_tracker_)
529 peer_connection_tracker_->TrackCreateOffer(this, constraints); 746 peer_connection_tracker_->TrackCreateOffer(this, constraints);
530 } 747 }
531 748
532 void RTCPeerConnectionHandler::createAnswer( 749 void RTCPeerConnectionHandler::createAnswer(
533 const blink::WebRTCSessionDescriptionRequest& request, 750 const blink::WebRTCSessionDescriptionRequest& request,
534 const blink::WebMediaConstraints& options) { 751 const blink::WebMediaConstraints& options) {
752 DCHECK(thread_checker_.CalledOnValidThread());
535 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createAnswer"); 753 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createAnswer");
536 scoped_refptr<CreateSessionDescriptionRequest> description_request( 754 scoped_refptr<CreateSessionDescriptionRequest> description_request(
537 new rtc::RefCountedObject<CreateSessionDescriptionRequest>( 755 new rtc::RefCountedObject<CreateSessionDescriptionRequest>(
538 request, this, PeerConnectionTracker::ACTION_CREATE_ANSWER)); 756 base::ThreadTaskRunnerHandle::Get(), request, this,
757 PeerConnectionTracker::ACTION_CREATE_ANSWER));
539 RTCMediaConstraints constraints(options); 758 RTCMediaConstraints constraints(options);
540 native_peer_connection_->CreateAnswer(description_request.get(), 759 native_peer_connection_->CreateAnswer(description_request.get(),
541 &constraints); 760 &constraints);
542 761
543 if (peer_connection_tracker_) 762 if (peer_connection_tracker_)
544 peer_connection_tracker_->TrackCreateAnswer(this, constraints); 763 peer_connection_tracker_->TrackCreateAnswer(this, constraints);
545 } 764 }
546 765
547 void RTCPeerConnectionHandler::setLocalDescription( 766 void RTCPeerConnectionHandler::setLocalDescription(
548 const blink::WebRTCVoidRequest& request, 767 const blink::WebRTCVoidRequest& request,
549 const blink::WebRTCSessionDescription& description) { 768 const blink::WebRTCSessionDescription& description) {
769 DCHECK(thread_checker_.CalledOnValidThread());
550 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::setLocalDescription"); 770 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::setLocalDescription");
551 webrtc::SdpParseError error; 771 webrtc::SdpParseError error;
552 webrtc::SessionDescriptionInterface* native_desc = 772 webrtc::SessionDescriptionInterface* native_desc =
553 CreateNativeSessionDescription(description, &error); 773 CreateNativeSessionDescription(description, &error);
554 if (!native_desc) { 774 if (!native_desc) {
555 std::string reason_str = "Failed to parse SessionDescription. "; 775 std::string reason_str = "Failed to parse SessionDescription. ";
556 reason_str.append(error.line); 776 reason_str.append(error.line);
557 reason_str.append(" "); 777 reason_str.append(" ");
558 reason_str.append(error.description); 778 reason_str.append(error.description);
559 LOG(ERROR) << reason_str; 779 LOG(ERROR) << reason_str;
560 request.requestFailed(blink::WebString::fromUTF8(reason_str)); 780 request.requestFailed(blink::WebString::fromUTF8(reason_str));
561 return; 781 return;
562 } 782 }
563 if (peer_connection_tracker_) 783
784 if (peer_connection_tracker_) {
564 peer_connection_tracker_->TrackSetSessionDescription( 785 peer_connection_tracker_->TrackSetSessionDescription(
565 this, description, PeerConnectionTracker::SOURCE_LOCAL); 786 this, description, PeerConnectionTracker::SOURCE_LOCAL);
787 }
566 788
567 scoped_refptr<SetSessionDescriptionRequest> set_request( 789 scoped_refptr<SetSessionDescriptionRequest> set_request(
568 new rtc::RefCountedObject<SetSessionDescriptionRequest>( 790 new rtc::RefCountedObject<SetSessionDescriptionRequest>(
569 request, this, PeerConnectionTracker::ACTION_SET_LOCAL_DESCRIPTION)); 791 base::ThreadTaskRunnerHandle::Get(), request, this,
792 PeerConnectionTracker::ACTION_SET_LOCAL_DESCRIPTION));
570 native_peer_connection_->SetLocalDescription(set_request.get(), native_desc); 793 native_peer_connection_->SetLocalDescription(set_request.get(), native_desc);
571 } 794 }
572 795
573 void RTCPeerConnectionHandler::setRemoteDescription( 796 void RTCPeerConnectionHandler::setRemoteDescription(
574 const blink::WebRTCVoidRequest& request, 797 const blink::WebRTCVoidRequest& request,
575 const blink::WebRTCSessionDescription& description) { 798 const blink::WebRTCSessionDescription& description) {
799 DCHECK(thread_checker_.CalledOnValidThread());
576 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::setRemoteDescription"); 800 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::setRemoteDescription");
577 webrtc::SdpParseError error; 801 webrtc::SdpParseError error;
578 webrtc::SessionDescriptionInterface* native_desc = 802 webrtc::SessionDescriptionInterface* native_desc =
579 CreateNativeSessionDescription(description, &error); 803 CreateNativeSessionDescription(description, &error);
580 if (!native_desc) { 804 if (!native_desc) {
581 std::string reason_str = "Failed to parse SessionDescription. "; 805 std::string reason_str = "Failed to parse SessionDescription. ";
582 reason_str.append(error.line); 806 reason_str.append(error.line);
583 reason_str.append(" "); 807 reason_str.append(" ");
584 reason_str.append(error.description); 808 reason_str.append(error.description);
585 LOG(ERROR) << reason_str; 809 LOG(ERROR) << reason_str;
586 request.requestFailed(blink::WebString::fromUTF8(reason_str)); 810 request.requestFailed(blink::WebString::fromUTF8(reason_str));
587 return; 811 return;
588 } 812 }
589 if (peer_connection_tracker_) 813
814 if (peer_connection_tracker_) {
590 peer_connection_tracker_->TrackSetSessionDescription( 815 peer_connection_tracker_->TrackSetSessionDescription(
591 this, description, PeerConnectionTracker::SOURCE_REMOTE); 816 this, description, PeerConnectionTracker::SOURCE_REMOTE);
817 }
592 818
593 scoped_refptr<SetSessionDescriptionRequest> set_request( 819 scoped_refptr<SetSessionDescriptionRequest> set_request(
594 new rtc::RefCountedObject<SetSessionDescriptionRequest>( 820 new rtc::RefCountedObject<SetSessionDescriptionRequest>(
595 request, this, PeerConnectionTracker::ACTION_SET_REMOTE_DESCRIPTION)); 821 base::ThreadTaskRunnerHandle::Get(), request, this,
822 PeerConnectionTracker::ACTION_SET_REMOTE_DESCRIPTION));
823 // TODO(tommi): This cannot be posted to the signaling thread unfortunately
perkj_chrome 2014/10/30 12:42:35 Can we talk about this? Is this only due to our t
tommi (sloooow) - chröme 2014/10/30 20:37:36 Added a TODO to do this in a following cl as discu
824 // because the caller depends on the state being changed synchronously.
perkj_chrome 2014/10/30 12:42:35 We should at least but a similar explanation with
tommi (sloooow) - chröme 2014/10/30 20:37:36 Done.
825 // I'm guessing this is so that the local and remote getter functions will
826 // immediately return what was just set.
827 // This unfortunately does block the UI thread for some time (tens of ms on
828 // beefy machines) each time it's called.
596 native_peer_connection_->SetRemoteDescription(set_request.get(), native_desc); 829 native_peer_connection_->SetRemoteDescription(set_request.get(), native_desc);
597 } 830 }
598 831
599 blink::WebRTCSessionDescription 832 blink::WebRTCSessionDescription
600 RTCPeerConnectionHandler::localDescription() { 833 RTCPeerConnectionHandler::localDescription() {
834 DCHECK(thread_checker_.CalledOnValidThread());
601 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::localDescription"); 835 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::localDescription");
602 const webrtc::SessionDescriptionInterface* native_desc = 836 const webrtc::SessionDescriptionInterface* native_desc =
603 native_peer_connection_->local_description(); 837 native_peer_connection_->local_description();
perkj_chrome 2014/10/30 12:42:35 Are these methods the problem?
tommi (sloooow) - chröme 2014/10/30 20:37:36 Yes - or rather the html test page had a bug which
604 blink::WebRTCSessionDescription description = 838 blink::WebRTCSessionDescription description =
605 CreateWebKitSessionDescription(native_desc); 839 CreateWebKitSessionDescription(native_desc);
606 return description; 840 return description;
607 } 841 }
608 842
609 blink::WebRTCSessionDescription 843 blink::WebRTCSessionDescription
610 RTCPeerConnectionHandler::remoteDescription() { 844 RTCPeerConnectionHandler::remoteDescription() {
845 DCHECK(thread_checker_.CalledOnValidThread());
611 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::remoteDescription"); 846 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::remoteDescription");
612 const webrtc::SessionDescriptionInterface* native_desc = 847 const webrtc::SessionDescriptionInterface* native_desc =
613 native_peer_connection_->remote_description(); 848 native_peer_connection_->remote_description();
614 blink::WebRTCSessionDescription description = 849 blink::WebRTCSessionDescription description =
615 CreateWebKitSessionDescription(native_desc); 850 CreateWebKitSessionDescription(native_desc);
616 return description; 851 return description;
617 } 852 }
618 853
619 bool RTCPeerConnectionHandler::updateICE( 854 bool RTCPeerConnectionHandler::updateICE(
620 const blink::WebRTCConfiguration& server_configuration, 855 const blink::WebRTCConfiguration& server_configuration,
621 const blink::WebMediaConstraints& options) { 856 const blink::WebMediaConstraints& options) {
857 DCHECK(thread_checker_.CalledOnValidThread());
622 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::updateICE"); 858 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::updateICE");
623 webrtc::PeerConnectionInterface::RTCConfiguration config; 859 webrtc::PeerConnectionInterface::RTCConfiguration config;
624 GetNativeRtcConfiguration(server_configuration, &config); 860 GetNativeRtcConfiguration(server_configuration, &config);
625 RTCMediaConstraints constraints(options); 861 RTCMediaConstraints constraints(options);
626 862
627 if (peer_connection_tracker_) 863 if (peer_connection_tracker_)
628 peer_connection_tracker_->TrackUpdateIce(this, config, constraints); 864 peer_connection_tracker_->TrackUpdateIce(this, config, constraints);
629 865
630 return native_peer_connection_->UpdateIce(config.servers, 866 return native_peer_connection_->UpdateIce(config.servers, &constraints);
631 &constraints);
632 } 867 }
633 868
634 bool RTCPeerConnectionHandler::addICECandidate( 869 bool RTCPeerConnectionHandler::addICECandidate(
635 const blink::WebRTCVoidRequest& request, 870 const blink::WebRTCVoidRequest& request,
636 const blink::WebRTCICECandidate& candidate) { 871 const blink::WebRTCICECandidate& candidate) {
872 DCHECK(thread_checker_.CalledOnValidThread());
637 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addICECandidate"); 873 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addICECandidate");
638 // Libjingle currently does not accept callbacks for addICECandidate. 874 // Libjingle currently does not accept callbacks for addICECandidate.
639 // For that reason we are going to call callbacks from here. 875 // For that reason we are going to call callbacks from here.
640 bool result = addICECandidate(candidate); 876 bool result = addICECandidate(candidate);
641 base::MessageLoop::current()->PostTask( 877 base::MessageLoop::current()->PostTask(
642 FROM_HERE, 878 FROM_HERE,
643 base::Bind(&RTCPeerConnectionHandler::OnaddICECandidateResult, 879 base::Bind(&RTCPeerConnectionHandler::OnaddICECandidateResult,
644 base::Unretained(this), request, result)); 880 weak_factory_.GetWeakPtr(), request, result));
645 // On failure callback will be triggered. 881 // On failure callback will be triggered.
646 return true; 882 return true;
647 } 883 }
648 884
649 bool RTCPeerConnectionHandler::addICECandidate( 885 bool RTCPeerConnectionHandler::addICECandidate(
650 const blink::WebRTCICECandidate& candidate) { 886 const blink::WebRTCICECandidate& candidate) {
887 DCHECK(thread_checker_.CalledOnValidThread());
651 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addICECandidate"); 888 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addICECandidate");
652 scoped_ptr<webrtc::IceCandidateInterface> native_candidate( 889 scoped_ptr<webrtc::IceCandidateInterface> native_candidate(
653 dependency_factory_->CreateIceCandidate( 890 dependency_factory_->CreateIceCandidate(
654 base::UTF16ToUTF8(candidate.sdpMid()), 891 base::UTF16ToUTF8(candidate.sdpMid()),
655 candidate.sdpMLineIndex(), 892 candidate.sdpMLineIndex(),
656 base::UTF16ToUTF8(candidate.candidate()))); 893 base::UTF16ToUTF8(candidate.candidate())));
657 bool return_value = false; 894 bool return_value = false;
658 895
659 if (native_candidate) { 896 if (native_candidate) {
660 return_value = 897 return_value =
661 native_peer_connection_->AddIceCandidate(native_candidate.get()); 898 native_peer_connection_->AddIceCandidate(native_candidate.get());
662 LOG_IF(ERROR, !return_value) << "Error processing ICE candidate."; 899 LOG_IF(ERROR, !return_value) << "Error processing ICE candidate.";
663 } else { 900 } else {
664 LOG(ERROR) << "Could not create native ICE candidate."; 901 LOG(ERROR) << "Could not create native ICE candidate.";
665 } 902 }
666 903
667 if (peer_connection_tracker_) { 904 if (peer_connection_tracker_) {
668 peer_connection_tracker_->TrackAddIceCandidate( 905 peer_connection_tracker_->TrackAddIceCandidate(
669 this, candidate, PeerConnectionTracker::SOURCE_REMOTE, return_value); 906 this, candidate, PeerConnectionTracker::SOURCE_REMOTE, return_value);
670 } 907 }
671 return return_value; 908 return return_value;
672 } 909 }
673 910
674 void RTCPeerConnectionHandler::OnaddICECandidateResult( 911 void RTCPeerConnectionHandler::OnaddICECandidateResult(
675 const blink::WebRTCVoidRequest& webkit_request, bool result) { 912 const blink::WebRTCVoidRequest& webkit_request, bool result) {
913 DCHECK(thread_checker_.CalledOnValidThread());
676 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnaddICECandidateResult"); 914 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnaddICECandidateResult");
677 if (!result) { 915 if (!result) {
678 // We don't have the actual error code from the libjingle, so for now 916 // We don't have the actual error code from the libjingle, so for now
679 // using a generic error string. 917 // using a generic error string.
680 return webkit_request.requestFailed( 918 return webkit_request.requestFailed(
681 base::UTF8ToUTF16("Error processing ICE candidate")); 919 base::UTF8ToUTF16("Error processing ICE candidate"));
682 } 920 }
683 921
684 return webkit_request.requestSucceeded(); 922 return webkit_request.requestSucceeded();
685 } 923 }
686 924
687 bool RTCPeerConnectionHandler::addStream( 925 bool RTCPeerConnectionHandler::addStream(
688 const blink::WebMediaStream& stream, 926 const blink::WebMediaStream& stream,
689 const blink::WebMediaConstraints& options) { 927 const blink::WebMediaConstraints& options) {
928 DCHECK(thread_checker_.CalledOnValidThread());
690 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addStream"); 929 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::addStream");
691 for (ScopedVector<WebRtcMediaStreamAdapter>::iterator adapter_it = 930 for (ScopedVector<WebRtcMediaStreamAdapter>::iterator adapter_it =
692 local_streams_.begin(); adapter_it != local_streams_.end(); 931 local_streams_.begin(); adapter_it != local_streams_.end();
693 ++adapter_it) { 932 ++adapter_it) {
694 if ((*adapter_it)->IsEqual(stream)) { 933 if ((*adapter_it)->IsEqual(stream)) {
695 DVLOG(1) << "RTCPeerConnectionHandler::addStream called with the same " 934 DVLOG(1) << "RTCPeerConnectionHandler::addStream called with the same "
696 << "stream twice. id=" << stream.id().utf8(); 935 << "stream twice. id=" << stream.id().utf8();
697 return false; 936 return false;
698 } 937 }
699 } 938 }
700 939
701 if (peer_connection_tracker_) 940 if (peer_connection_tracker_) {
702 peer_connection_tracker_->TrackAddStream( 941 peer_connection_tracker_->TrackAddStream(
703 this, stream, PeerConnectionTracker::SOURCE_LOCAL); 942 this, stream, PeerConnectionTracker::SOURCE_LOCAL);
943 }
704 944
705 PerSessionWebRTCAPIMetrics::GetInstance()->IncrementStreamCounter(); 945 PerSessionWebRTCAPIMetrics::GetInstance()->IncrementStreamCounter();
706 946
707 WebRtcMediaStreamAdapter* adapter = 947 WebRtcMediaStreamAdapter* adapter =
708 new WebRtcMediaStreamAdapter(stream, dependency_factory_); 948 new WebRtcMediaStreamAdapter(stream, dependency_factory_);
709 local_streams_.push_back(adapter); 949 local_streams_.push_back(adapter);
710 950
711 webrtc::MediaStreamInterface* webrtc_stream = adapter->webrtc_media_stream(); 951 webrtc::MediaStreamInterface* webrtc_stream = adapter->webrtc_media_stream();
712 track_metrics_.AddStream(MediaStreamTrackMetrics::SENT_STREAM, 952 track_metrics_.AddStream(MediaStreamTrackMetrics::SENT_STREAM,
713 webrtc_stream); 953 webrtc_stream);
714 954
715 RTCMediaConstraints constraints(options); 955 RTCMediaConstraints constraints(options);
716 return native_peer_connection_->AddStream(webrtc_stream, &constraints); 956 return native_peer_connection_->AddStream(webrtc_stream, &constraints);
717 } 957 }
718 958
719 void RTCPeerConnectionHandler::removeStream( 959 void RTCPeerConnectionHandler::removeStream(
720 const blink::WebMediaStream& stream) { 960 const blink::WebMediaStream& stream) {
961 DCHECK(thread_checker_.CalledOnValidThread());
721 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::removeStream"); 962 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::removeStream");
722 // Find the webrtc stream. 963 // Find the webrtc stream.
723 scoped_refptr<webrtc::MediaStreamInterface> webrtc_stream; 964 scoped_refptr<webrtc::MediaStreamInterface> webrtc_stream;
724 for (ScopedVector<WebRtcMediaStreamAdapter>::iterator adapter_it = 965 for (ScopedVector<WebRtcMediaStreamAdapter>::iterator adapter_it =
725 local_streams_.begin(); adapter_it != local_streams_.end(); 966 local_streams_.begin(); adapter_it != local_streams_.end();
726 ++adapter_it) { 967 ++adapter_it) {
727 if ((*adapter_it)->IsEqual(stream)) { 968 if ((*adapter_it)->IsEqual(stream)) {
728 webrtc_stream = (*adapter_it)->webrtc_media_stream(); 969 webrtc_stream = (*adapter_it)->webrtc_media_stream();
729 local_streams_.erase(adapter_it); 970 local_streams_.erase(adapter_it);
730 break; 971 break;
731 } 972 }
732 } 973 }
733 DCHECK(webrtc_stream.get()); 974 DCHECK(webrtc_stream.get());
734 native_peer_connection_->RemoveStream(webrtc_stream.get()); 975 native_peer_connection_->RemoveStream(webrtc_stream.get());
735 976
736 if (peer_connection_tracker_) 977 if (peer_connection_tracker_) {
737 peer_connection_tracker_->TrackRemoveStream( 978 peer_connection_tracker_->TrackRemoveStream(
738 this, stream, PeerConnectionTracker::SOURCE_LOCAL); 979 this, stream, PeerConnectionTracker::SOURCE_LOCAL);
980 }
739 PerSessionWebRTCAPIMetrics::GetInstance()->DecrementStreamCounter(); 981 PerSessionWebRTCAPIMetrics::GetInstance()->DecrementStreamCounter();
740 track_metrics_.RemoveStream(MediaStreamTrackMetrics::SENT_STREAM, 982 track_metrics_.RemoveStream(MediaStreamTrackMetrics::SENT_STREAM,
741 webrtc_stream.get()); 983 webrtc_stream.get());
742 } 984 }
743 985
744 void RTCPeerConnectionHandler::getStats( 986 void RTCPeerConnectionHandler::getStats(
745 const blink::WebRTCStatsRequest& request) { 987 const blink::WebRTCStatsRequest& request) {
988 DCHECK(thread_checker_.CalledOnValidThread());
746 scoped_refptr<LocalRTCStatsRequest> inner_request( 989 scoped_refptr<LocalRTCStatsRequest> inner_request(
747 new rtc::RefCountedObject<LocalRTCStatsRequest>(request)); 990 new rtc::RefCountedObject<LocalRTCStatsRequest>(request));
748 getStats(inner_request.get()); 991 getStats(inner_request);
749 } 992 }
750 993
751 void RTCPeerConnectionHandler::getStats(LocalRTCStatsRequest* request) { 994 void RTCPeerConnectionHandler::getStats(
995 const scoped_refptr<LocalRTCStatsRequest>& request) {
996 DCHECK(thread_checker_.CalledOnValidThread());
997 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::getStats");
752 rtc::scoped_refptr<webrtc::StatsObserver> observer( 998 rtc::scoped_refptr<webrtc::StatsObserver> observer(
753 new rtc::RefCountedObject<StatsResponse>(request)); 999 new rtc::RefCountedObject<StatsResponse>(request));
754 webrtc::MediaStreamTrackInterface* track = NULL; 1000 webrtc::MediaStreamTrackInterface* track = NULL;
755 if (request->hasSelector()) { 1001 if (request->hasSelector()) {
756 blink::WebMediaStreamSource::Type type = 1002 blink::WebMediaStreamSource::Type type =
757 request->component().source().type(); 1003 request->component().source().type();
758 std::string track_id = request->component().id().utf8(); 1004 std::string track_id = request->component().id().utf8();
759 if (type == blink::WebMediaStreamSource::TypeAudio) { 1005 if (type == blink::WebMediaStreamSource::TypeAudio) {
1006 // TODO: native_peer_connection_ will introduce a thread hop. Do this
perkj_chrome 2014/10/30 12:42:35 What is the status of this?
tommi (sloooow) - chröme 2014/10/30 20:37:36 Move all of this over to the signaling thread as d
1007 // on the signaling thread.
760 track = 1008 track =
761 native_peer_connection_->local_streams()->FindAudioTrack(track_id); 1009 native_peer_connection_->local_streams()->FindAudioTrack(track_id);
762 if (!track) { 1010 if (!track) {
763 track = 1011 track =
764 native_peer_connection_->remote_streams()->FindAudioTrack(track_id); 1012 native_peer_connection_->remote_streams()->FindAudioTrack(track_id);
765 } 1013 }
766 } else { 1014 } else {
767 DCHECK_EQ(blink::WebMediaStreamSource::TypeVideo, type); 1015 DCHECK_EQ(blink::WebMediaStreamSource::TypeVideo, type);
768 track = 1016 track =
769 native_peer_connection_->local_streams()->FindVideoTrack(track_id); 1017 native_peer_connection_->local_streams()->FindVideoTrack(track_id);
(...skipping 11 matching lines...) Expand all
781 } 1029 }
782 GetStats(observer, 1030 GetStats(observer,
783 track, 1031 track,
784 webrtc::PeerConnectionInterface::kStatsOutputLevelStandard); 1032 webrtc::PeerConnectionInterface::kStatsOutputLevelStandard);
785 } 1033 }
786 1034
787 void RTCPeerConnectionHandler::GetStats( 1035 void RTCPeerConnectionHandler::GetStats(
788 webrtc::StatsObserver* observer, 1036 webrtc::StatsObserver* observer,
789 webrtc::MediaStreamTrackInterface* track, 1037 webrtc::MediaStreamTrackInterface* track,
790 webrtc::PeerConnectionInterface::StatsOutputLevel level) { 1038 webrtc::PeerConnectionInterface::StatsOutputLevel level) {
791 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::GetStats"); 1039 DCHECK(thread_checker_.CalledOnValidThread());
792 if (!native_peer_connection_->GetStats(observer, track, level)) { 1040 scoped_refptr<webrtc::StatsObserver> ref_observer(observer);
793 DVLOG(1) << "GetStats failed."; 1041 scoped_refptr<webrtc::MediaStreamTrackInterface> ref_track(track);
794 // TODO(hta): Consider how to get an error back. 1042
795 observer->OnComplete(StatsReports()); 1043 // TODO(tommi): Make other methods run from the signaling thread too.
796 return; 1044 signaling_thread_->PostTask(FROM_HERE,
797 } 1045 base::Bind(&GetStatsOnSignalingThread, native_peer_connection_, level,
1046 ref_observer, ref_track));
798 } 1047 }
799 1048
800 void RTCPeerConnectionHandler::CloseClientPeerConnection() { 1049 void RTCPeerConnectionHandler::CloseClientPeerConnection() {
801 client_->closePeerConnection(); 1050 DCHECK(thread_checker_.CalledOnValidThread());
1051 if (client_)
1052 client_->closePeerConnection();
802 } 1053 }
803 1054
804 blink::WebRTCDataChannelHandler* RTCPeerConnectionHandler::createDataChannel( 1055 blink::WebRTCDataChannelHandler* RTCPeerConnectionHandler::createDataChannel(
805 const blink::WebString& label, const blink::WebRTCDataChannelInit& init) { 1056 const blink::WebString& label, const blink::WebRTCDataChannelInit& init) {
1057 DCHECK(thread_checker_.CalledOnValidThread());
806 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createDataChannel"); 1058 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createDataChannel");
807 DVLOG(1) << "createDataChannel label " << base::UTF16ToUTF8(label); 1059 DVLOG(1) << "createDataChannel label " << base::UTF16ToUTF8(label);
808 1060
809 webrtc::DataChannelInit config; 1061 webrtc::DataChannelInit config;
810 // TODO(jiayl): remove the deprecated reliable field once Libjingle is updated 1062 // TODO(jiayl): remove the deprecated reliable field once Libjingle is updated
811 // to handle that. 1063 // to handle that.
812 config.reliable = false; 1064 config.reliable = false;
813 config.id = init.id; 1065 config.id = init.id;
814 config.ordered = init.ordered; 1066 config.ordered = init.ordered;
815 config.negotiated = init.negotiated; 1067 config.negotiated = init.negotiated;
816 config.maxRetransmits = init.maxRetransmits; 1068 config.maxRetransmits = init.maxRetransmits;
817 config.maxRetransmitTime = init.maxRetransmitTime; 1069 config.maxRetransmitTime = init.maxRetransmitTime;
818 config.protocol = base::UTF16ToUTF8(init.protocol); 1070 config.protocol = base::UTF16ToUTF8(init.protocol);
819 1071
820 rtc::scoped_refptr<webrtc::DataChannelInterface> webrtc_channel( 1072 rtc::scoped_refptr<webrtc::DataChannelInterface> webrtc_channel(
821 native_peer_connection_->CreateDataChannel(base::UTF16ToUTF8(label), 1073 native_peer_connection_->CreateDataChannel(base::UTF16ToUTF8(label),
822 &config)); 1074 &config));
823 if (!webrtc_channel) { 1075 if (!webrtc_channel) {
824 DLOG(ERROR) << "Could not create native data channel."; 1076 DLOG(ERROR) << "Could not create native data channel.";
825 return NULL; 1077 return NULL;
826 } 1078 }
827 if (peer_connection_tracker_) 1079 if (peer_connection_tracker_) {
828 peer_connection_tracker_->TrackCreateDataChannel( 1080 peer_connection_tracker_->TrackCreateDataChannel(
829 this, webrtc_channel.get(), PeerConnectionTracker::SOURCE_LOCAL); 1081 this, webrtc_channel.get(), PeerConnectionTracker::SOURCE_LOCAL);
1082 }
830 1083
831 ++num_data_channels_created_; 1084 ++num_data_channels_created_;
832 1085
833 return new RtcDataChannelHandler(base::ThreadTaskRunnerHandle::Get(), 1086 return new RtcDataChannelHandler(base::ThreadTaskRunnerHandle::Get(),
834 webrtc_channel); 1087 webrtc_channel);
835 } 1088 }
836 1089
837 blink::WebRTCDTMFSenderHandler* RTCPeerConnectionHandler::createDTMFSender( 1090 blink::WebRTCDTMFSenderHandler* RTCPeerConnectionHandler::createDTMFSender(
838 const blink::WebMediaStreamTrack& track) { 1091 const blink::WebMediaStreamTrack& track) {
1092 DCHECK(thread_checker_.CalledOnValidThread());
839 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createDTMFSender"); 1093 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::createDTMFSender");
840 DVLOG(1) << "createDTMFSender."; 1094 DVLOG(1) << "createDTMFSender.";
841 1095
842 MediaStreamTrack* native_track = MediaStreamTrack::GetTrack(track); 1096 MediaStreamTrack* native_track = MediaStreamTrack::GetTrack(track);
843 if (!native_track || 1097 if (!native_track || !native_track->is_local_track() ||
844 track.source().type() != blink::WebMediaStreamSource::TypeAudio) { 1098 track.source().type() != blink::WebMediaStreamSource::TypeAudio) {
845 DLOG(ERROR) << "Could not create DTMF sender from a non-audio track."; 1099 DLOG(ERROR) << "The DTMF sender requires a local audio track.";
846 return NULL; 1100 return nullptr;
847 } 1101 }
848 1102
849 webrtc::AudioTrackInterface* audio_track = native_track->GetAudioAdapter(); 1103 webrtc::AudioTrackInterface* audio_track = native_track->GetAudioAdapter();
850 rtc::scoped_refptr<webrtc::DtmfSenderInterface> sender( 1104 rtc::scoped_refptr<webrtc::DtmfSenderInterface> sender(
851 native_peer_connection_->CreateDtmfSender(audio_track)); 1105 native_peer_connection_->CreateDtmfSender(audio_track));
852 if (!sender) { 1106 if (!sender) {
853 DLOG(ERROR) << "Could not create native DTMF sender."; 1107 DLOG(ERROR) << "Could not create native DTMF sender.";
854 return NULL; 1108 return nullptr;
855 } 1109 }
856 if (peer_connection_tracker_) 1110 if (peer_connection_tracker_)
857 peer_connection_tracker_->TrackCreateDTMFSender(this, track); 1111 peer_connection_tracker_->TrackCreateDTMFSender(this, track);
858 1112
859 return new RtcDtmfSenderHandler(sender); 1113 return new RtcDtmfSenderHandler(sender);
860 } 1114 }
861 1115
862 void RTCPeerConnectionHandler::stop() { 1116 void RTCPeerConnectionHandler::stop() {
1117 DCHECK(thread_checker_.CalledOnValidThread());
863 DVLOG(1) << "RTCPeerConnectionHandler::stop"; 1118 DVLOG(1) << "RTCPeerConnectionHandler::stop";
864 1119
865 if (peer_connection_tracker_) 1120 if (peer_connection_tracker_)
866 peer_connection_tracker_->TrackStop(this); 1121 peer_connection_tracker_->TrackStop(this);
867 native_peer_connection_->Close(); 1122 native_peer_connection_->Close();
868 } 1123 // The client_ pointer is not considered valid after this point and no further
869 1124 // callbacks must be made.
870 void RTCPeerConnectionHandler::OnError() { 1125 client_ = nullptr;
871 // TODO(perkj): Implement.
872 NOTIMPLEMENTED();
873 } 1126 }
874 1127
875 void RTCPeerConnectionHandler::OnSignalingChange( 1128 void RTCPeerConnectionHandler::OnSignalingChange(
876 webrtc::PeerConnectionInterface::SignalingState new_state) { 1129 webrtc::PeerConnectionInterface::SignalingState new_state) {
1130 DCHECK(thread_checker_.CalledOnValidThread());
877 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnSignalingChange"); 1131 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnSignalingChange");
1132
878 blink::WebRTCPeerConnectionHandlerClient::SignalingState state = 1133 blink::WebRTCPeerConnectionHandlerClient::SignalingState state =
879 GetWebKitSignalingState(new_state); 1134 GetWebKitSignalingState(new_state);
880 if (peer_connection_tracker_) 1135 if (peer_connection_tracker_)
881 peer_connection_tracker_->TrackSignalingStateChange(this, state); 1136 peer_connection_tracker_->TrackSignalingStateChange(this, state);
882 client_->didChangeSignalingState(state); 1137 if (client_)
1138 client_->didChangeSignalingState(state);
883 } 1139 }
884 1140
885 // Called any time the IceConnectionState changes 1141 // Called any time the IceConnectionState changes
886 void RTCPeerConnectionHandler::OnIceConnectionChange( 1142 void RTCPeerConnectionHandler::OnIceConnectionChange(
887 webrtc::PeerConnectionInterface::IceConnectionState new_state) { 1143 webrtc::PeerConnectionInterface::IceConnectionState new_state) {
888 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceConnectionChange"); 1144 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceConnectionChange");
1145 DCHECK(thread_checker_.CalledOnValidThread());
889 if (new_state == webrtc::PeerConnectionInterface::kIceConnectionChecking) { 1146 if (new_state == webrtc::PeerConnectionInterface::kIceConnectionChecking) {
890 ice_connection_checking_start_ = base::TimeTicks::Now(); 1147 ice_connection_checking_start_ = base::TimeTicks::Now();
891 } else if (new_state == 1148 } else if (new_state ==
892 webrtc::PeerConnectionInterface::kIceConnectionConnected) { 1149 webrtc::PeerConnectionInterface::kIceConnectionConnected) {
893 // If the state becomes connected, send the time needed for PC to become 1150 // If the state becomes connected, send the time needed for PC to become
894 // connected from checking to UMA. UMA data will help to know how much 1151 // connected from checking to UMA. UMA data will help to know how much
895 // time needed for PC to connect with remote peer. 1152 // time needed for PC to connect with remote peer.
896 UMA_HISTOGRAM_MEDIUM_TIMES( 1153 UMA_HISTOGRAM_MEDIUM_TIMES(
897 "WebRTC.PeerConnection.TimeToConnect", 1154 "WebRTC.PeerConnection.TimeToConnect",
898 base::TimeTicks::Now() - ice_connection_checking_start_); 1155 base::TimeTicks::Now() - ice_connection_checking_start_);
899 } 1156 }
900 1157
901 track_metrics_.IceConnectionChange(new_state); 1158 track_metrics_.IceConnectionChange(new_state);
902 blink::WebRTCPeerConnectionHandlerClient::ICEConnectionState state = 1159 blink::WebRTCPeerConnectionHandlerClient::ICEConnectionState state =
903 GetWebKitIceConnectionState(new_state); 1160 GetWebKitIceConnectionState(new_state);
904 if (peer_connection_tracker_) 1161 if (peer_connection_tracker_)
905 peer_connection_tracker_->TrackIceConnectionStateChange(this, state); 1162 peer_connection_tracker_->TrackIceConnectionStateChange(this, state);
906 client_->didChangeICEConnectionState(state); 1163 if(client_)
1164 client_->didChangeICEConnectionState(state);
907 } 1165 }
908 1166
909 // Called any time the IceGatheringState changes 1167 // Called any time the IceGatheringState changes
910 void RTCPeerConnectionHandler::OnIceGatheringChange( 1168 void RTCPeerConnectionHandler::OnIceGatheringChange(
911 webrtc::PeerConnectionInterface::IceGatheringState new_state) { 1169 webrtc::PeerConnectionInterface::IceGatheringState new_state) {
1170 DCHECK(thread_checker_.CalledOnValidThread());
912 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceGatheringChange"); 1171 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceGatheringChange");
1172
913 if (new_state == webrtc::PeerConnectionInterface::kIceGatheringComplete) { 1173 if (new_state == webrtc::PeerConnectionInterface::kIceGatheringComplete) {
914 // If ICE gathering is completed, generate a NULL ICE candidate, 1174 // If ICE gathering is completed, generate a NULL ICE candidate,
915 // to signal end of candidates. 1175 // to signal end of candidates.
916 blink::WebRTCICECandidate null_candidate; 1176 if (client_) {
917 client_->didGenerateICECandidate(null_candidate); 1177 blink::WebRTCICECandidate null_candidate;
1178 client_->didGenerateICECandidate(null_candidate);
1179 }
918 1180
919 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4LocalCandidates", 1181 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv4LocalCandidates",
920 num_local_candidates_ipv4_); 1182 num_local_candidates_ipv4_);
921 1183
922 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6LocalCandidates", 1184 UMA_HISTOGRAM_COUNTS_100("WebRTC.PeerConnection.IPv6LocalCandidates",
923 num_local_candidates_ipv6_); 1185 num_local_candidates_ipv6_);
924 } else if (new_state == 1186 } else if (new_state ==
925 webrtc::PeerConnectionInterface::kIceGatheringGathering) { 1187 webrtc::PeerConnectionInterface::kIceGatheringGathering) {
926 // ICE restarts will change gathering state back to "gathering", 1188 // ICE restarts will change gathering state back to "gathering",
927 // reset the counter. 1189 // reset the counter.
928 num_local_candidates_ipv6_ = 0; 1190 num_local_candidates_ipv6_ = 0;
929 num_local_candidates_ipv4_ = 0; 1191 num_local_candidates_ipv4_ = 0;
930 } 1192 }
931 1193
932 blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState state = 1194 blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState state =
933 GetWebKitIceGatheringState(new_state); 1195 GetWebKitIceGatheringState(new_state);
934 if (peer_connection_tracker_) 1196 if (peer_connection_tracker_)
935 peer_connection_tracker_->TrackIceGatheringStateChange(this, state); 1197 peer_connection_tracker_->TrackIceGatheringStateChange(this, state);
936 client_->didChangeICEGatheringState(state); 1198 if (client_)
1199 client_->didChangeICEGatheringState(state);
1200 }
1201
1202 void RTCPeerConnectionHandler::OnRenegotiationNeeded() {
1203 DCHECK(thread_checker_.CalledOnValidThread());
1204 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnRenegotiationNeeded");
1205 if (peer_connection_tracker_)
1206 peer_connection_tracker_->TrackOnRenegotiationNeeded(this);
1207 if (client_)
1208 client_->negotiationNeeded();
1209 }
1210
1211 PeerConnectionTracker* RTCPeerConnectionHandler::peer_connection_tracker() {
1212 DCHECK(thread_checker_.CalledOnValidThread());
1213 return peer_connection_tracker_;
937 } 1214 }
938 1215
939 void RTCPeerConnectionHandler::OnAddStream( 1216 void RTCPeerConnectionHandler::OnAddStream(
940 webrtc::MediaStreamInterface* stream_interface) { 1217 scoped_ptr<RemoteMediaStreamImpl> stream) {
941 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnAddStream"); 1218 DCHECK(thread_checker_.CalledOnValidThread());
942 DCHECK(stream_interface); 1219 DCHECK(remote_streams_.find(stream->webrtc_stream().get()) ==
943 DCHECK(remote_streams_.find(stream_interface) == remote_streams_.end()); 1220 remote_streams_.end());
1221 DCHECK(stream->webkit_stream().extraData()) << "Initialization not done";
1222 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnAddStreamImpl");
944 1223
945 RemoteMediaStreamImpl* remote_stream = 1224 // Ownership is with remote_streams_ now.
946 new RemoteMediaStreamImpl(stream_interface); 1225 RemoteMediaStreamImpl* s = stream.release();
947 remote_streams_.insert( 1226 remote_streams_.insert(
948 std::pair<webrtc::MediaStreamInterface*, RemoteMediaStreamImpl*> ( 1227 std::pair<webrtc::MediaStreamInterface*, RemoteMediaStreamImpl*> (
949 stream_interface, remote_stream)); 1228 s->webrtc_stream().get(), s));
950 1229
951 if (peer_connection_tracker_) 1230 if (peer_connection_tracker_) {
952 peer_connection_tracker_->TrackAddStream( 1231 peer_connection_tracker_->TrackAddStream(
953 this, remote_stream->webkit_stream(), 1232 this, s->webkit_stream(), PeerConnectionTracker::SOURCE_REMOTE);
954 PeerConnectionTracker::SOURCE_REMOTE); 1233 }
955 1234
956 PerSessionWebRTCAPIMetrics::GetInstance()->IncrementStreamCounter(); 1235 PerSessionWebRTCAPIMetrics::GetInstance()->IncrementStreamCounter();
957 1236
958 track_metrics_.AddStream(MediaStreamTrackMetrics::RECEIVED_STREAM, 1237 track_metrics_.AddStream(MediaStreamTrackMetrics::RECEIVED_STREAM,
959 stream_interface); 1238 s->webrtc_stream().get());
960 1239 if (client_)
961 client_->didAddRemoteStream(remote_stream->webkit_stream()); 1240 client_->didAddRemoteStream(s->webkit_stream());
962 } 1241 }
963 1242
964 void RTCPeerConnectionHandler::OnRemoveStream( 1243 void RTCPeerConnectionHandler::OnRemoveStream(
965 webrtc::MediaStreamInterface* stream_interface) { 1244 const scoped_refptr<webrtc::MediaStreamInterface>& stream) {
966 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnRemoveStream"); 1245 DCHECK(thread_checker_.CalledOnValidThread());
967 DCHECK(stream_interface); 1246 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnRemoveStreamImpl");
968 RemoteStreamMap::iterator it = remote_streams_.find(stream_interface); 1247 RemoteStreamMap::iterator it = remote_streams_.find(stream.get());
969 if (it == remote_streams_.end()) { 1248 if (it == remote_streams_.end()) {
970 NOTREACHED() << "Stream not found"; 1249 NOTREACHED() << "Stream not found";
971 return; 1250 return;
972 } 1251 }
973 1252
974 track_metrics_.RemoveStream(MediaStreamTrackMetrics::RECEIVED_STREAM, 1253 track_metrics_.RemoveStream(MediaStreamTrackMetrics::RECEIVED_STREAM,
975 stream_interface); 1254 stream.get());
976 PerSessionWebRTCAPIMetrics::GetInstance()->DecrementStreamCounter(); 1255 PerSessionWebRTCAPIMetrics::GetInstance()->DecrementStreamCounter();
977 1256
978 scoped_ptr<RemoteMediaStreamImpl> remote_stream(it->second); 1257 scoped_ptr<RemoteMediaStreamImpl> remote_stream(it->second);
979 const blink::WebMediaStream& webkit_stream = remote_stream->webkit_stream(); 1258 const blink::WebMediaStream& webkit_stream = remote_stream->webkit_stream();
980 DCHECK(!webkit_stream.isNull()); 1259 DCHECK(!webkit_stream.isNull());
981 remote_streams_.erase(it); 1260 remote_streams_.erase(it);
982 1261
983 if (peer_connection_tracker_) 1262 if (peer_connection_tracker_) {
984 peer_connection_tracker_->TrackRemoveStream( 1263 peer_connection_tracker_->TrackRemoveStream(
985 this, webkit_stream, PeerConnectionTracker::SOURCE_REMOTE); 1264 this, webkit_stream, PeerConnectionTracker::SOURCE_REMOTE);
1265 }
986 1266
987 client_->didRemoveRemoteStream(webkit_stream); 1267 if (client_)
1268 client_->didRemoveRemoteStream(webkit_stream);
1269 }
1270
1271 void RTCPeerConnectionHandler::OnDataChannel(
1272 scoped_ptr<RtcDataChannelHandler> handler) {
1273 DCHECK(thread_checker_.CalledOnValidThread());
1274 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnDataChannelImpl");
1275
1276 if (peer_connection_tracker_) {
1277 peer_connection_tracker_->TrackCreateDataChannel(
1278 this, handler->channel().get(), PeerConnectionTracker::SOURCE_REMOTE);
1279 }
1280
1281 if (client_)
1282 client_->didAddRemoteDataChannel(handler.release());
988 } 1283 }
989 1284
990 void RTCPeerConnectionHandler::OnIceCandidate( 1285 void RTCPeerConnectionHandler::OnIceCandidate(
991 const webrtc::IceCandidateInterface* candidate) { 1286 const std::string& sdp, const std::string& sdp_mid, int sdp_mline_index,
992 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceCandidate"); 1287 int component, int address_family) {
993 DCHECK(candidate); 1288 DCHECK(thread_checker_.CalledOnValidThread());
994 std::string sdp; 1289 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnIceCandidateImpl");
995 if (!candidate->ToString(&sdp)) {
996 NOTREACHED() << "OnIceCandidate: Could not get SDP string.";
997 return;
998 }
999 blink::WebRTCICECandidate web_candidate; 1290 blink::WebRTCICECandidate web_candidate;
1000 web_candidate.initialize(base::UTF8ToUTF16(sdp), 1291 web_candidate.initialize(base::UTF8ToUTF16(sdp),
1001 base::UTF8ToUTF16(candidate->sdp_mid()), 1292 base::UTF8ToUTF16(sdp_mid),
1002 candidate->sdp_mline_index()); 1293 sdp_mline_index);
1003 if (peer_connection_tracker_) 1294 if (peer_connection_tracker_) {
1004 peer_connection_tracker_->TrackAddIceCandidate( 1295 peer_connection_tracker_->TrackAddIceCandidate(
1005 this, web_candidate, PeerConnectionTracker::SOURCE_LOCAL, true); 1296 this, web_candidate, PeerConnectionTracker::SOURCE_LOCAL, true);
1297 }
1006 1298
1007 // Only the first m line's first component is tracked to avoid 1299 // Only the first m line's first component is tracked to avoid
1008 // miscounting when doing BUNDLE or rtcp mux. 1300 // miscounting when doing BUNDLE or rtcp mux.
1009 if (candidate->sdp_mline_index() == 0 && 1301 if (sdp_mline_index == 0 && component == 1) {
1010 candidate->candidate().component() == 1) { 1302 if (address_family == AF_INET) {
1011 if (candidate->candidate().address().family() == AF_INET) { 1303 ++num_local_candidates_ipv4_;
1012 num_local_candidates_ipv4_++; 1304 } else if (address_family == AF_INET6) {
1013 } else if (candidate->candidate().address().family() == AF_INET6) { 1305 ++num_local_candidates_ipv6_;
1014 num_local_candidates_ipv6_++;
1015 } else { 1306 } else {
1016 NOTREACHED(); 1307 NOTREACHED();
1017 } 1308 }
1018 } 1309 }
1019 client_->didGenerateICECandidate(web_candidate); 1310 if (client_)
1020 } 1311 client_->didGenerateICECandidate(web_candidate);
1021
1022 void RTCPeerConnectionHandler::OnDataChannel(
1023 webrtc::DataChannelInterface* data_channel) {
1024 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnDataChannel");
1025 if (peer_connection_tracker_)
1026 peer_connection_tracker_->TrackCreateDataChannel(
1027 this, data_channel, PeerConnectionTracker::SOURCE_REMOTE);
1028
1029 DVLOG(1) << "RTCPeerConnectionHandler::OnDataChannel "
1030 << data_channel->label();
1031 client_->didAddRemoteDataChannel(new RtcDataChannelHandler(
1032 base::ThreadTaskRunnerHandle::Get(), data_channel));
1033 }
1034
1035 void RTCPeerConnectionHandler::OnRenegotiationNeeded() {
1036 TRACE_EVENT0("webrtc", "RTCPeerConnectionHandler::OnRenegotiationNeeded");
1037 if (peer_connection_tracker_)
1038 peer_connection_tracker_->TrackOnRenegotiationNeeded(this);
1039 client_->negotiationNeeded();
1040 }
1041
1042 PeerConnectionTracker* RTCPeerConnectionHandler::peer_connection_tracker() {
1043 return peer_connection_tracker_;
1044 } 1312 }
1045 1313
1046 webrtc::SessionDescriptionInterface* 1314 webrtc::SessionDescriptionInterface*
1047 RTCPeerConnectionHandler::CreateNativeSessionDescription( 1315 RTCPeerConnectionHandler::CreateNativeSessionDescription(
1048 const blink::WebRTCSessionDescription& description, 1316 const blink::WebRTCSessionDescription& description,
1049 webrtc::SdpParseError* error) { 1317 webrtc::SdpParseError* error) {
1050 std::string sdp = base::UTF16ToUTF8(description.sdp()); 1318 std::string sdp = base::UTF16ToUTF8(description.sdp());
1051 std::string type = base::UTF16ToUTF8(description.type()); 1319 std::string type = base::UTF16ToUTF8(description.type());
1052 webrtc::SessionDescriptionInterface* native_desc = 1320 webrtc::SessionDescriptionInterface* native_desc =
1053 dependency_factory_->CreateSessionDescription(type, sdp, error); 1321 dependency_factory_->CreateSessionDescription(type, sdp, error);
1054 1322
1055 LOG_IF(ERROR, !native_desc) << "Failed to create native session description." 1323 LOG_IF(ERROR, !native_desc) << "Failed to create native session description."
1056 << " Type: " << type << " SDP: " << sdp; 1324 << " Type: " << type << " SDP: " << sdp;
1057 1325
1058 return native_desc; 1326 return native_desc;
1059 } 1327 }
1060 1328
1061 } // namespace content 1329 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698