| 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
" | |
| 40 #include "third_party/WebKit/public/web/WebDocument.h" | 39 #include "third_party/WebKit/public/web/WebDocument.h" |
| 41 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 40 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 42 | 41 |
| 43 namespace content { | 42 namespace content { |
| 44 namespace { | 43 namespace { |
| 45 | 44 |
| 46 void CopyVector(const blink::WebVector<blink::WebString>& source, | 45 void CopyVector(const blink::WebVector<blink::WebString>& source, |
| 47 std::vector<std::string>* destination) { | 46 std::vector<std::string>* destination) { |
| 48 for (const auto& web_string : source) { | 47 for (const auto& web_string : source) { |
| 49 destination->push_back(web_string.utf8()); | 48 destination->push_back(web_string.utf8()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 case MEDIA_DEVICE_TYPE_VIDEO_INPUT: | 139 case MEDIA_DEVICE_TYPE_VIDEO_INPUT: |
| 141 return blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput; | 140 return blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput; |
| 142 case MEDIA_DEVICE_TYPE_AUDIO_OUTPUT: | 141 case MEDIA_DEVICE_TYPE_AUDIO_OUTPUT: |
| 143 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput; | 142 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioOutput; |
| 144 default: | 143 default: |
| 145 NOTREACHED(); | 144 NOTREACHED(); |
| 146 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput; | 145 return blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput; |
| 147 } | 146 } |
| 148 } | 147 } |
| 149 | 148 |
| 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 | |
| 162 static int g_next_request_id = 0; | 149 static int g_next_request_id = 0; |
| 163 | 150 |
| 164 } // namespace | 151 } // namespace |
| 165 | 152 |
| 166 UserMediaClientImpl::UserMediaClientImpl( | 153 UserMediaClientImpl::UserMediaClientImpl( |
| 167 RenderFrame* render_frame, | 154 RenderFrame* render_frame, |
| 168 PeerConnectionDependencyFactory* dependency_factory, | 155 PeerConnectionDependencyFactory* dependency_factory, |
| 169 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) | 156 std::unique_ptr<MediaStreamDispatcher> media_stream_dispatcher) |
| 170 : RenderFrameObserver(render_frame), | 157 : RenderFrameObserver(render_frame), |
| 171 dependency_factory_(dependency_factory), | 158 dependency_factory_(dependency_factory), |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 289 if (!media_devices_request.isNull()) | 276 if (!media_devices_request.isNull()) |
| 290 security_origin = media_devices_request.getSecurityOrigin(); | 277 security_origin = media_devices_request.getSecurityOrigin(); |
| 291 | 278 |
| 292 GetMediaDevicesDispatcher()->EnumerateDevices( | 279 GetMediaDevicesDispatcher()->EnumerateDevices( |
| 293 true /* audio input */, true /* video input */, true /* audio output */, | 280 true /* audio input */, true /* video input */, true /* audio output */, |
| 294 security_origin, | 281 security_origin, |
| 295 base::Bind(&UserMediaClientImpl::FinalizeEnumerateDevices, | 282 base::Bind(&UserMediaClientImpl::FinalizeEnumerateDevices, |
| 296 weak_factory_.GetWeakPtr(), media_devices_request)); | 283 weak_factory_.GetWeakPtr(), media_devices_request)); |
| 297 } | 284 } |
| 298 | 285 |
| 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 | |
| 318 void UserMediaClientImpl::setMediaDeviceChangeObserver( | 286 void UserMediaClientImpl::setMediaDeviceChangeObserver( |
| 319 const blink::WebMediaDeviceChangeObserver& observer) { | 287 const blink::WebMediaDeviceChangeObserver& observer) { |
| 320 media_device_change_observer_ = observer; | 288 media_device_change_observer_ = observer; |
| 321 | 289 |
| 322 if (media_device_change_observer_.isNull()) { | 290 if (media_device_change_observer_.isNull()) { |
| 323 media_stream_dispatcher_->CancelDeviceChangeNotifications( | 291 media_stream_dispatcher_->CancelDeviceChangeNotifications( |
| 324 weak_factory_.GetWeakPtr()); | 292 weak_factory_.GetWeakPtr()); |
| 325 } else { | 293 } else { |
| 326 url::Origin origin = observer.getSecurityOrigin(); | 294 url::Origin origin = observer.getSecurityOrigin(); |
| 327 media_stream_dispatcher_->SubscribeToDeviceChangeNotifications( | 295 media_stream_dispatcher_->SubscribeToDeviceChangeNotifications( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 devices[index++].initialize( | 388 devices[index++].initialize( |
| 421 blink::WebString::fromUTF8(device_info.device_id), device_kind, | 389 blink::WebString::fromUTF8(device_info.device_id), device_kind, |
| 422 blink::WebString::fromUTF8(device_info.label), | 390 blink::WebString::fromUTF8(device_info.label), |
| 423 blink::WebString::fromUTF8(device_info.group_id)); | 391 blink::WebString::fromUTF8(device_info.group_id)); |
| 424 } | 392 } |
| 425 } | 393 } |
| 426 | 394 |
| 427 EnumerateDevicesSucceded(&request, devices); | 395 EnumerateDevicesSucceded(&request, devices); |
| 428 } | 396 } |
| 429 | 397 |
| 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 | |
| 458 // Callback from MediaStreamDispatcher. | 398 // Callback from MediaStreamDispatcher. |
| 459 // The requested stream failed to be generated. | 399 // The requested stream failed to be generated. |
| 460 void UserMediaClientImpl::OnStreamGenerationFailed( | 400 void UserMediaClientImpl::OnStreamGenerationFailed( |
| 461 int request_id, | 401 int request_id, |
| 462 MediaStreamRequestResult result) { | 402 MediaStreamRequestResult result) { |
| 463 DCHECK(CalledOnValidThread()); | 403 DCHECK(CalledOnValidThread()); |
| 464 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" | 404 DVLOG(1) << "UserMediaClientImpl::OnStreamGenerationFailed(" |
| 465 << request_id << ")"; | 405 << request_id << ")"; |
| 466 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); | 406 UserMediaRequestInfo* request_info = FindUserMediaRequestInfo(request_id); |
| 467 if (!request_info) { | 407 if (!request_info) { |
| (...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 775 NOTREACHED(); | 715 NOTREACHED(); |
| 776 request_info.requestFailed(); | 716 request_info.requestFailed(); |
| 777 } | 717 } |
| 778 | 718 |
| 779 void UserMediaClientImpl::EnumerateDevicesSucceded( | 719 void UserMediaClientImpl::EnumerateDevicesSucceded( |
| 780 blink::WebMediaDevicesRequest* request, | 720 blink::WebMediaDevicesRequest* request, |
| 781 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | 721 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { |
| 782 request->requestSucceeded(devices); | 722 request->requestSucceeded(devices); |
| 783 } | 723 } |
| 784 | 724 |
| 785 void UserMediaClientImpl::EnumerateSourcesSucceded( | |
| 786 blink::WebMediaStreamTrackSourcesRequest* request, | |
| 787 blink::WebVector<blink::WebSourceInfo>& sources) { | |
| 788 request->requestSucceeded(sources); | |
| 789 } | |
| 790 | |
| 791 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( | 725 const blink::WebMediaStreamSource* UserMediaClientImpl::FindLocalSource( |
| 792 const StreamDeviceInfo& device) const { | 726 const StreamDeviceInfo& device) const { |
| 793 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 727 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
| 794 it != local_sources_.end(); ++it) { | 728 it != local_sources_.end(); ++it) { |
| 795 MediaStreamSource* const source = | 729 MediaStreamSource* const source = |
| 796 static_cast<MediaStreamSource*>(it->getExtraData()); | 730 static_cast<MediaStreamSource*>(it->getExtraData()); |
| 797 const StreamDeviceInfo& active_device = source->device_info(); | 731 const StreamDeviceInfo& active_device = source->device_info(); |
| 798 if (IsSameDevice(active_device, device)) { | 732 if (IsSameDevice(active_device, device)) { |
| 799 return &(*it); | 733 return &(*it); |
| 800 } | 734 } |
| (...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1036 | 970 |
| 1037 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { | 971 bool UserMediaClientImpl::UserMediaRequestInfo::HasPendingSources() const { |
| 1038 return !sources_waiting_for_callback_.empty(); | 972 return !sources_waiting_for_callback_.empty(); |
| 1039 } | 973 } |
| 1040 | 974 |
| 1041 void UserMediaClientImpl::OnDestruct() { | 975 void UserMediaClientImpl::OnDestruct() { |
| 1042 delete this; | 976 delete this; |
| 1043 } | 977 } |
| 1044 | 978 |
| 1045 } // namespace content | 979 } // namespace content |
| OLD | NEW |