| 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 if (media_device_change_observer_.isNull()) { | 322 if (media_device_change_observer_.isNull()) { |
| 291 media_stream_dispatcher_->CancelDeviceChangeNotifications( | 323 media_stream_dispatcher_->CancelDeviceChangeNotifications( |
| 292 weak_factory_.GetWeakPtr()); | 324 weak_factory_.GetWeakPtr()); |
| 293 } else { | 325 } else { |
| 294 url::Origin origin = observer.getSecurityOrigin(); | 326 url::Origin origin = observer.getSecurityOrigin(); |
| 295 media_stream_dispatcher_->SubscribeToDeviceChangeNotifications( | 327 media_stream_dispatcher_->SubscribeToDeviceChangeNotifications( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 388 devices[index++].initialize( | 420 devices[index++].initialize( |
| 389 blink::WebString::fromUTF8(device_info.device_id), device_kind, | 421 blink::WebString::fromUTF8(device_info.device_id), device_kind, |
| 390 blink::WebString::fromUTF8(device_info.label), | 422 blink::WebString::fromUTF8(device_info.label), |
| 391 blink::WebString::fromUTF8(device_info.group_id)); | 423 blink::WebString::fromUTF8(device_info.group_id)); |
| 392 } | 424 } |
| 393 } | 425 } |
| 394 | 426 |
| 395 EnumerateDevicesSucceded(&request, devices); | 427 EnumerateDevicesSucceded(&request, devices); |
| 396 } | 428 } |
| 397 | 429 |
| 430 void UserMediaClientImpl::FinalizeGetSources( |
| 431 blink::WebMediaStreamTrackSourcesRequest request, |
| 432 const EnumerationResult& result) { |
| 433 DCHECK_EQ(static_cast<size_t>(NUM_MEDIA_DEVICE_TYPES), result.size()); |
| 434 |
| 435 blink::WebVector<blink::WebSourceInfo> sources( |
| 436 result[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size() + |
| 437 result[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size()); |
| 438 size_t index = 0; |
| 439 for (const auto& device_info : result[MEDIA_DEVICE_TYPE_AUDIO_INPUT]) { |
| 440 sources[index++].initialize( |
| 441 blink::WebString::fromUTF8(device_info.device_id), |
| 442 blink::WebSourceInfo::SourceKindAudio, |
| 443 blink::WebString::fromUTF8(device_info.label), |
| 444 blink::WebSourceInfo::VideoFacingModeNone); |
| 445 } |
| 446 |
| 447 for (const auto& device_info : result[MEDIA_DEVICE_TYPE_VIDEO_INPUT]) { |
| 448 sources[index++].initialize( |
| 449 blink::WebString::fromUTF8(device_info.device_id), |
| 450 blink::WebSourceInfo::SourceKindVideo, |
| 451 blink::WebString::fromUTF8(device_info.label), |
| 452 ToVideoFacingMode(device_info.label)); |
| 453 } |
| 454 |
| 455 EnumerateSourcesSucceded(&request, sources); |
| 456 } |
| 457 |
| 398 // Callback from MediaStreamDispatcher. | 458 // Callback from MediaStreamDispatcher. |
| 399 // The requested stream failed to be generated. | 459 // The requested stream failed to be generated. |
| 400 void UserMediaClientImpl::OnStreamGenerationFailed( | 460 void UserMediaClientImpl::OnStreamGenerationFailed( |
| 401 int request_id, | 461 int request_id, |
| 402 MediaStreamRequestResult result) { | 462 MediaStreamRequestResult result) { |
| 403 DCHECK(CalledOnValidThread()); | 463 DCHECK(CalledOnValidThread()); |
| 404 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" | 464 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" |
| 405 << request_id << ")"; | 465 << request_id << ")"; |
| 406 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); | 466 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); |
| 407 if (!request_info) { | 467 if (!request_info) { |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 715 NOTREACHED(); | 775 NOTREACHED(); |
| 716 request_info.requestFailed(); | 776 request_info.requestFailed(); |
| 717 } | 777 } |
| 718 | 778 |
| 719 void UserMediaClientImpl::EnumerateDevicesSucceded( | 779 void UserMediaClientImpl::EnumerateDevicesSucceded( |
| 720 blink::WebMediaDevicesRequest* request, | 780 blink::WebMediaDevicesRequest* request, |
| 721 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 781 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
| 722 request->requestSucceeded(devices); | 782 request->requestSucceeded(devices); |
| 723 } | 783 } |
| 724 | 784 |
| 785 void UserMediaClientImpl::EnumerateSourcesSucceded( |
| 786 blink::WebMediaStreamTrackSourcesRequest* request, |
| 787 blink::WebVector<blink::WebSourceInfo>& sources) { |
| 788 request->requestSucceeded(sources); |
| 789 } |
| 790 |
| 725 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 791 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
| 726 const StreamDeviceInfo& device) const { | 792 const StreamDeviceInfo& device) const { |
| 727 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 793 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
| 728 it != local_sources_.end(); ++it) { | 794 it != local_sources_.end(); ++it) { |
| 729 MediaStreamSource* const source = | 795 MediaStreamSource* const source = |
| 730 static_cast<MediaStreamSource*>(it->getExtraData()); | 796 static_cast<MediaStreamSource*>(it->getExtraData()); |
| 731 const StreamDeviceInfo& active_device = source->device_info(); | 797 const StreamDeviceInfo& active_device = source->device_info(); |
| 732 if (IsSameDevice(active_device, device)) { | 798 if (IsSameDevice(active_device, device)) { |
| 733 return &(*it); | 799 return &(*it); |
| 734 } | 800 } |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 970 | 1036 |
| 971 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { | 1037 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
| 972 return !sources_waiting_for_callback_.empty(); | 1038 return !sources_waiting_for_callback_.empty(); |
| 973 } | 1039 } |
| 974 | 1040 |
| 975 void UserMediaClientImpl::OnDestruct() { | 1041 void UserMediaClientImpl::OnDestruct() { |
| 976 delete this; | 1042 delete this; |
| 977 } | 1043 } |
| 978 | 1044 |
| 979 } // namespace content | 1045 } // namespace content |
| OLD | NEW |