| OLD | NEW |
| 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/user_media_client_impl.h" | 5 #include "content/renderer/media/user_media_client_impl.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 #include "content/renderer/media/peer_connection_tracker.h" | 29 #include "content/renderer/media/peer_connection_tracker.h" |
| 30 #include "content/renderer/media/webrtc/processed_local_audio_source.h" | 30 #include "content/renderer/media/webrtc/processed_local_audio_source.h" |
| 31 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 31 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
| 32 #include "content/renderer/media/webrtc_logging.h" | 32 #include "content/renderer/media/webrtc_logging.h" |
| 33 #include "content/renderer/media/webrtc_uma_histograms.h" | 33 #include "content/renderer/media/webrtc_uma_histograms.h" |
| 34 #include "content/renderer/render_thread_impl.h" | 34 #include "content/renderer/render_thread_impl.h" |
| 35 #include "third_party/WebKit/public/platform/URLConversion.h" | 35 #include "third_party/WebKit/public/platform/URLConversion.h" |
| 36 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 36 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 37 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h" | 37 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h" |
| 38 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 38 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 39 #include "third_party/WebKit/public/platform/WebMediaStreamTrackSourcesRequest.h
" |
| 39 #include "third_party/WebKit/public/web/WebDocument.h" | 40 #include "third_party/WebKit/public/web/WebDocument.h" |
| 40 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 41 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 41 | 42 |
| 42 namespace content { | 43 namespace content { |
| 43 namespace { | 44 namespace { |
| 44 | 45 |
| 45 void CopyVector(const blink::WebVector<blink::WebString>& source, | 46 void CopyVector(const blink::WebVector<blink::WebString>& source, |
| 46 std::vector<std::string>* destination) { | 47 std::vector<std::string>* destination) { |
| 47 for (const auto& web_string : source) { | 48 for (const auto& web_string : source) { |
| 48 destination->push_back(web_string.utf8()); | 49 destination->push_back(web_string.utf8()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 139 case MEDIA_DEVICE_TYPE_VIDEO_INPUT: | 140 case MEDIA_DEVICE_TYPE_VIDEO_INPUT: |
| 140 return blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput; | 141 return blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput; |
| 141 case MEDIA_DEVICE_TYPE_AUDIO_OUTPUT: | 142 case MEDIA_DEVICE_TYPE_AUDIO_OUTPUT: |
| 142 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput; | 143 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput; |
| 143 default: | 144 default: |
| 144 NOTREACHED(); | 145 NOTREACHED(); |
| 145 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput; | 146 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput; |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 150 blink::WebSourceInfo::VideoFacingMode ToVideoFacingMode( |
| 151 const std::string& device_label) { |
| 152 #if defined(OS_ANDROID) |
| 153 if (device_label.find("front") != std::string::npos) { |
| 154 return blink::WebSourceInfo::VideoFacingModeUser; |
| 155 } else if (device_label.find("back") != std::string::npos) { |
| 156 return blink::WebSourceInfo::VideoFacingModeEnvironment; |
| 157 } |
| 158 #endif |
| 159 return blink::WebSourceInfo::VideoFacingModeNone; |
| 160 } |
| 161 |
| 149 static int g_next_request_id = 0; | 162 static int g_next_request_id = 0; |
| 150 | 163 |
| 151 } // namespace | 164 } // namespace |
| 152 | 165 |
| 153 UserMediaClientImpl::UserMediaClientImpl( | 166 UserMediaClientImpl::UserMediaClientImpl( |
| 154 RenderFrame* render_frame, | 167 RenderFrame* render_frame, |
| 155 PeerConnectionDependencyFactory* dependency_factory, | 168 PeerConnectionDependencyFactory* dependency_factory, |
| 156 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) | 169 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) |
| 157 : RenderFrameObserver(render_frame), | 170 : RenderFrameObserver(render_frame), |
| 158 dependency_factory_(dependency_factory), | 171 dependency_factory_(dependency_factory), |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 if (!media_devices_request.isNull()) | 289 if (!media_devices_request.isNull()) |
| 277 security_origin = media_devices_request.getSecurityOrigin(); | 290 security_origin = media_devices_request.getSecurityOrigin(); |
| 278 | 291 |
| 279 GetMediaDevicesDispatcher()->EnumerateDevices( | 292 GetMediaDevicesDispatcher()->EnumerateDevices( |
| 280 true /* audio input */, true /* video input */, true /* audio output */, | 293 true /* audio input */, true /* video input */, true /* audio output */, |
| 281 security_origin, | 294 security_origin, |
| 282 base::Bind(&UserMediaClientImpl::FinalizeEnumerateDevices, | 295 base::Bind(&UserMediaClientImpl::FinalizeEnumerateDevices, |
| 283 weak_factory_.GetWeakPtr(), media_devices_request)); | 296 weak_factory_.GetWeakPtr(), media_devices_request)); |
| 284 } | 297 } |
| 285 | 298 |
| 299 void UserMediaClientImpl::requestSources( |
| 300 const blink::WebMediaStreamTrackSourcesRequest& sources_request) { |
| 301 // We don't call UpdateWebRTCMethodCount() here to track the API count in UMA |
| 302 // stats. This is instead counted in MediaStreamTrack::getSources in blink. |
| 303 DCHECK(CalledOnValidThread()); |
| 304 |
| 305 // |sources_request| can't be mocked, so in tests it will be empty (the |
| 306 // underlying pointer is null). In order to use this function in a test we |
| 307 // need to check if it isNull. |
| 308 url::Origin security_origin; |
| 309 if (!sources_request.isNull()) |
| 310 security_origin = sources_request.origin(); |
| 311 |
| 312 GetMediaDevicesDispatcher()->EnumerateDevices( |
| 313 true /* audio input */, true /* video input */, false /* audio output */, |
| 314 security_origin, base::Bind(&UserMediaClientImpl::FinalizeGetSources, |
| 315 weak_factory_.GetWeakPtr(), sources_request)); |
| 316 } |
| 317 |
| 286 void UserMediaClientImpl::setMediaDeviceChangeObserver( | 318 void UserMediaClientImpl::setMediaDeviceChangeObserver( |
| 287 const blink::WebMediaDeviceChangeObserver& observer) { | 319 const blink::WebMediaDeviceChangeObserver& observer) { |
| 288 media_device_change_observer_ = observer; | 320 media_device_change_observer_ = observer; |
| 289 | 321 |
| 290 // Do nothing if setting a valid observer while already subscribed or setting | 322 // Do nothing if setting a valid observer while already subscribed or setting |
| 291 // no observer while unsubscribed. | 323 // no observer while unsubscribed. |
| 292 if (media_device_change_observer_.isNull() == | 324 if (media_device_change_observer_.isNull() == |
| 293 device_change_subscription_ids_.empty()) | 325 device_change_subscription_ids_.empty()) |
| 294 return; | 326 return; |
| 295 | 327 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 devices[index++].initialize( | 433 devices[index++].initialize( |
| 402 blink::WebString::fromUTF8(device_info.device_id), device_kind, | 434 blink::WebString::fromUTF8(device_info.device_id), device_kind, |
| 403 blink::WebString::fromUTF8(device_info.label), | 435 blink::WebString::fromUTF8(device_info.label), |
| 404 blink::WebString::fromUTF8(device_info.group_id)); | 436 blink::WebString::fromUTF8(device_info.group_id)); |
| 405 } | 437 } |
| 406 } | 438 } |
| 407 | 439 |
| 408 EnumerateDevicesSucceded(&request, devices); | 440 EnumerateDevicesSucceded(&request, devices); |
| 409 } | 441 } |
| 410 | 442 |
| 443 void UserMediaClientImpl::FinalizeGetSources( |
| 444 blink::WebMediaStreamTrackSourcesRequest request, |
| 445 const EnumerationResult& result) { |
| 446 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); |
| 447 |
| 448 blink::WebVector<blink::WebSourceInfo> sources( |
| 449 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + |
| 450 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size()); |
| 451 size_t index = 0; |
| 452 for (const auto& device_info : result[MEDIA_DEVICE_TYPE_AUDIO_INPUT]) { |
| 453 sources[index++].initialize( |
| 454 blink::WebString::fromUTF8(device_info.device_id), |
| 455 blink::WebSourceInfo::SourceKindAudio, |
| 456 blink::WebString::fromUTF8(device_info.label), |
| 457 blink::WebSourceInfo::VideoFacingModeNone); |
| 458 } |
| 459 |
| 460 for (const auto& device_info : result[MEDIA_DEVICE_TYPE_VIDEO_INPUT]) { |
| 461 sources[index++].initialize( |
| 462 blink::WebString::fromUTF8(device_info.device_id), |
| 463 blink::WebSourceInfo::SourceKindVideo, |
| 464 blink::WebString::fromUTF8(device_info.label), |
| 465 ToVideoFacingMode(device_info.label)); |
| 466 } |
| 467 |
| 468 EnumerateSourcesSucceded(&request, sources); |
| 469 } |
| 470 |
| 411 // Callback from MediaStreamDispatcher. | 471 // Callback from MediaStreamDispatcher. |
| 412 // The requested stream failed to be generated. | 472 // The requested stream failed to be generated. |
| 413 void UserMediaClientImpl::OnStreamGenerationFailed( | 473 void UserMediaClientImpl::OnStreamGenerationFailed( |
| 414 int request_id, | 474 int request_id, |
| 415 MediaStreamRequestResult result) { | 475 MediaStreamRequestResult result) { |
| 416 DCHECK(CalledOnValidThread()); | 476 DCHECK(CalledOnValidThread()); |
| 417 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" | 477 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" |
| 418 << request_id << ")"; | 478 << request_id << ")"; |
| 419 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); | 479 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); |
| 420 if (!request_info) { | 480 if (!request_info) { |
| (...skipping 316 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 NOTREACHED(); | 797 NOTREACHED(); |
| 738 request_info.requestFailed(); | 798 request_info.requestFailed(); |
| 739 } | 799 } |
| 740 | 800 |
| 741 void UserMediaClientImpl::EnumerateDevicesSucceded( | 801 void UserMediaClientImpl::EnumerateDevicesSucceded( |
| 742 blink::WebMediaDevicesRequest* request, | 802 blink::WebMediaDevicesRequest* request, |
| 743 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 803 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
| 744 request->requestSucceeded(devices); | 804 request->requestSucceeded(devices); |
| 745 } | 805 } |
| 746 | 806 |
| 807 void UserMediaClientImpl::EnumerateSourcesSucceded( |
| 808 blink::WebMediaStreamTrackSourcesRequest* request, |
| 809 blink::WebVector<blink::WebSourceInfo>& sources) { |
| 810 request->requestSucceeded(sources); |
| 811 } |
| 812 |
| 747 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 813 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
| 748 const StreamDeviceInfo& device) const { | 814 const StreamDeviceInfo& device) const { |
| 749 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 815 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
| 750 it != local_sources_.end(); ++it) { | 816 it != local_sources_.end(); ++it) { |
| 751 MediaStreamSource* const source = | 817 MediaStreamSource* const source = |
| 752 static_cast<MediaStreamSource*>(it->getExtraData()); | 818 static_cast<MediaStreamSource*>(it->getExtraData()); |
| 753 const StreamDeviceInfo& active_device = source->device_info(); | 819 const StreamDeviceInfo& active_device = source->device_info(); |
| 754 if (IsSameDevice(active_device, device)) { | 820 if (IsSameDevice(active_device, device)) { |
| 755 return &(*it); | 821 return &(*it); |
| 756 } | 822 } |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 992 | 1058 |
| 993 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { | 1059 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
| 994 return !sources_waiting_for_callback_.empty(); | 1060 return !sources_waiting_for_callback_.empty(); |
| 995 } | 1061 } |
| 996 | 1062 |
| 997 void UserMediaClientImpl::OnDestruct() { | 1063 void UserMediaClientImpl::OnDestruct() { |
| 998 delete this; | 1064 delete this; |
| 999 } | 1065 } |
| 1000 | 1066 |
| 1001 } // namespace content | 1067 } // namespace content |
| OLD | NEW |