Chromium Code Reviews| 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/media_stream_impl.h" | 5 #include "content/renderer/media/media_stream_impl.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "content/renderer/media/media_stream.h" | 13 #include "content/renderer/media/media_stream.h" |
| 14 #include "content/renderer/media/media_stream_audio_source.h" | 14 #include "content/renderer/media/media_stream_audio_source.h" |
| 15 #include "content/renderer/media/media_stream_dispatcher.h" | 15 #include "content/renderer/media/media_stream_dispatcher.h" |
| 16 #include "content/renderer/media/media_stream_video_capturer_source.h" | 16 #include "content/renderer/media/media_stream_video_capturer_source.h" |
| 17 #include "content/renderer/media/media_stream_video_track.h" | 17 #include "content/renderer/media/media_stream_video_track.h" |
| 18 #include "content/renderer/media/peer_connection_tracker.h" | 18 #include "content/renderer/media/peer_connection_tracker.h" |
| 19 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 19 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
| 20 #include "content/renderer/media/webrtc_audio_capturer.h" | 20 #include "content/renderer/media/webrtc_audio_capturer.h" |
| 21 #include "content/renderer/media/webrtc_logging.h" | 21 #include "content/renderer/media/webrtc_logging.h" |
| 22 #include "content/renderer/media/webrtc_uma_histograms.h" | 22 #include "content/renderer/media/webrtc_uma_histograms.h" |
| 23 #include "content/renderer/render_thread_impl.h" | 23 #include "content/renderer/render_thread_impl.h" |
| 24 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 24 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
| 25 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h" | |
| 25 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 26 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
| 26 #include "third_party/WebKit/public/web/WebDocument.h" | 27 #include "third_party/WebKit/public/web/WebDocument.h" |
| 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 28 | 29 |
| 29 namespace content { | 30 namespace content { |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 void CopyStreamConstraints(const blink::WebMediaConstraints& constraints, | 33 void CopyStreamConstraints(const blink::WebMediaConstraints& constraints, |
| 33 StreamOptions::Constraints* mandatory, | 34 StreamOptions::Constraints* mandatory, |
| 34 StreamOptions::Constraints* optional) { | 35 StreamOptions::Constraints* optional) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 46 optional->push_back(StreamOptions::Constraint( | 47 optional->push_back(StreamOptions::Constraint( |
| 47 optional_constraints[i].m_name.utf8(), | 48 optional_constraints[i].m_name.utf8(), |
| 48 optional_constraints[i].m_value.utf8())); | 49 optional_constraints[i].m_value.utf8())); |
| 49 } | 50 } |
| 50 } | 51 } |
| 51 | 52 |
| 52 static int g_next_request_id = 0; | 53 static int g_next_request_id = 0; |
| 53 | 54 |
| 54 } // namespace | 55 } // namespace |
| 55 | 56 |
| 57 struct MediaStreamImpl::MediaDevicesRequestInfo { | |
| 58 MediaDevicesRequestInfo(blink::WebMediaDevicesRequest request, | |
|
no longer working on chromium
2014/06/02 11:57:42
const blink::WebMediaDevicesRequest& ?
Henrik Grunell
2014/06/03 07:52:27
Done.
| |
| 59 int audio_input_request_id, | |
| 60 int video_input_request_id) | |
| 61 : request(request), | |
| 62 audio_input_request_id(audio_input_request_id), | |
| 63 video_input_request_id(video_input_request_id), | |
| 64 has_audio_input_returned(false), | |
| 65 has_video_input_returned(false) {} | |
| 66 | |
| 67 blink::WebMediaDevicesRequest request; | |
|
no longer working on chromium
2014/06/02 11:57:42
nit, const, if the request won't be changed afterw
Henrik Grunell
2014/06/03 07:52:27
Can't be const, we're making a call on it later wh
| |
| 68 int audio_input_request_id; | |
| 69 int video_input_request_id; | |
| 70 bool has_audio_input_returned; | |
| 71 bool has_video_input_returned; | |
| 72 StreamDeviceInfoArray audio_input_devices; | |
| 73 StreamDeviceInfoArray video_input_devices; | |
| 74 }; | |
| 75 | |
| 56 MediaStreamImpl::MediaStreamImpl( | 76 MediaStreamImpl::MediaStreamImpl( |
| 57 RenderView* render_view, | 77 RenderView* render_view, |
| 58 MediaStreamDispatcher* media_stream_dispatcher, | 78 MediaStreamDispatcher* media_stream_dispatcher, |
| 59 PeerConnectionDependencyFactory* dependency_factory) | 79 PeerConnectionDependencyFactory* dependency_factory) |
| 60 : RenderViewObserver(render_view), | 80 : RenderViewObserver(render_view), |
| 61 dependency_factory_(dependency_factory), | 81 dependency_factory_(dependency_factory), |
| 62 media_stream_dispatcher_(media_stream_dispatcher) { | 82 media_stream_dispatcher_(media_stream_dispatcher) { |
| 63 } | 83 } |
| 64 | 84 |
| 65 MediaStreamImpl::~MediaStreamImpl() { | 85 MediaStreamImpl::~MediaStreamImpl() { |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 163 DCHECK(CalledOnValidThread()); | 183 DCHECK(CalledOnValidThread()); |
| 164 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); | 184 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); |
| 165 if (request) { | 185 if (request) { |
| 166 // We can't abort the stream generation process. | 186 // We can't abort the stream generation process. |
| 167 // Instead, erase the request. Once the stream is generated we will stop the | 187 // Instead, erase the request. Once the stream is generated we will stop the |
| 168 // stream if the request does not exist. | 188 // stream if the request does not exist. |
| 169 DeleteUserMediaRequestInfo(request); | 189 DeleteUserMediaRequestInfo(request); |
| 170 } | 190 } |
| 171 } | 191 } |
| 172 | 192 |
| 193 void MediaStreamImpl::requestMediaDevices( | |
| 194 const blink::WebMediaDevicesRequest& media_devices_request) { | |
| 195 // TODO(grunell): UMA stats? | |
|
no longer working on chromium
2014/06/02 11:57:42
are you going to address this in this CL?
Henrik Grunell
2014/06/03 07:52:27
Yes, done.
| |
| 196 DCHECK(CalledOnValidThread()); | |
| 197 | |
| 198 int audio_input_request_id = g_next_request_id++; | |
|
no longer working on chromium
2014/06/02 11:57:42
what happen if media_devices_request contains only
Henrik Grunell
2014/06/03 07:52:27
getMediaDevices() always requests enumeration of a
| |
| 199 int video_input_request_id = g_next_request_id++; | |
| 200 | |
| 201 // |media_devices_request| can't be mocked. So in order to test at all we | |
|
no longer working on chromium
2014/06/02 11:57:42
confusing comment, please fix it.
Henrik Grunell
2014/06/03 07:52:27
Done.
| |
| 202 // check if it isNull. | |
| 203 GURL security_origin; | |
| 204 if (!media_devices_request.isNull()) | |
|
no longer working on chromium
2014/06/02 11:57:42
are you saying that media_devices_request.isNull()
Henrik Grunell
2014/06/03 07:52:27
Yes.
| |
| 205 security_origin = GURL(media_devices_request.securityOrigin().toString()); | |
| 206 | |
| 207 DVLOG(1) << "MediaStreamImpl::requestMediaDevices(" << audio_input_request_id | |
| 208 << ", " << video_input_request_id << ", " | |
| 209 << security_origin.spec() << ")"; | |
| 210 | |
| 211 media_devices_requests_.push_back(new MediaDevicesRequestInfo( | |
| 212 media_devices_request, audio_input_request_id, video_input_request_id)); | |
| 213 | |
| 214 media_stream_dispatcher_->EnumerateDevices( | |
| 215 audio_input_request_id, | |
| 216 AsWeakPtr(), | |
| 217 MEDIA_DEVICE_AUDIO_CAPTURE, | |
| 218 security_origin); | |
| 219 | |
| 220 media_stream_dispatcher_->EnumerateDevices( | |
|
no longer working on chromium
2014/06/02 11:57:42
same question here, what happen if media_devices_r
Henrik Grunell
2014/06/03 07:52:27
Same answer. :)
| |
| 221 video_input_request_id, | |
| 222 AsWeakPtr(), | |
| 223 MEDIA_DEVICE_VIDEO_CAPTURE, | |
| 224 security_origin); | |
| 225 } | |
| 226 | |
| 227 void MediaStreamImpl::cancelMediaDevicesRequest( | |
| 228 const blink::WebMediaDevicesRequest& media_devices_request) { | |
| 229 DCHECK(CalledOnValidThread()); | |
| 230 MediaDevicesRequestInfo* request = | |
| 231 FindMediaDevicesRequestInfo(media_devices_request); | |
| 232 if (!request) | |
| 233 return; | |
| 234 | |
| 235 // Cancel device enumeration. | |
| 236 media_stream_dispatcher_->StopEnumerateDevices( | |
| 237 request->audio_input_request_id, | |
| 238 AsWeakPtr()); | |
| 239 media_stream_dispatcher_->StopEnumerateDevices( | |
| 240 request->video_input_request_id, | |
| 241 AsWeakPtr()); | |
| 242 DeleteMediaDevicesRequestInfo(request); | |
| 243 } | |
| 244 | |
| 173 // Callback from MediaStreamDispatcher. | 245 // Callback from MediaStreamDispatcher. |
| 174 // The requested stream have been generated by the MediaStreamDispatcher. | 246 // The requested stream have been generated by the MediaStreamDispatcher. |
| 175 void MediaStreamImpl::OnStreamGenerated( | 247 void MediaStreamImpl::OnStreamGenerated( |
| 176 int request_id, | 248 int request_id, |
| 177 const std::string& label, | 249 const std::string& label, |
| 178 const StreamDeviceInfoArray& audio_array, | 250 const StreamDeviceInfoArray& audio_array, |
| 179 const StreamDeviceInfoArray& video_array) { | 251 const StreamDeviceInfoArray& video_array) { |
| 180 DCHECK(CalledOnValidThread()); | 252 DCHECK(CalledOnValidThread()); |
| 181 DVLOG(1) << "MediaStreamImpl::OnStreamGenerated stream:" << label; | 253 DVLOG(1) << "MediaStreamImpl::OnStreamGenerated stream:" << label; |
| 182 | 254 |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 411 GetUserMediaRequestSucceeded(request->web_stream, &request->request); | 483 GetUserMediaRequestSucceeded(request->web_stream, &request->request); |
| 412 else | 484 else |
| 413 GetUserMediaRequestFailed(&request->request, result); | 485 GetUserMediaRequestFailed(&request->request, result); |
| 414 | 486 |
| 415 DeleteUserMediaRequestInfo(request); | 487 DeleteUserMediaRequestInfo(request); |
| 416 } | 488 } |
| 417 | 489 |
| 418 void MediaStreamImpl::OnDevicesEnumerated( | 490 void MediaStreamImpl::OnDevicesEnumerated( |
| 419 int request_id, | 491 int request_id, |
| 420 const StreamDeviceInfoArray& device_array) { | 492 const StreamDeviceInfoArray& device_array) { |
| 421 DVLOG(1) << "MediaStreamImpl::OnDevicesEnumerated(" | 493 DVLOG(1) << "MediaStreamImpl::OnDevicesEnumerated(" << request_id << ")"; |
| 422 << request_id << ")"; | 494 |
| 423 NOTIMPLEMENTED(); | 495 MediaDevicesRequestInfo* request = FindMediaDevicesRequestInfo(request_id); |
| 496 DCHECK(request); | |
| 497 | |
| 498 if (request_id == request->audio_input_request_id) { | |
| 499 request->has_audio_input_returned = true; | |
| 500 DCHECK(request->audio_input_devices.empty()); | |
| 501 request->audio_input_devices = device_array; | |
| 502 } else { | |
| 503 DCHECK(request_id == request->video_input_request_id); | |
| 504 request->has_video_input_returned = true; | |
| 505 DCHECK(request->video_input_devices.empty()); | |
| 506 request->video_input_devices = device_array; | |
| 507 } | |
| 508 | |
| 509 if (!request->has_audio_input_returned || | |
| 510 !request->has_video_input_returned) { | |
| 511 // Wait for the rest of the devices to complete. | |
| 512 return; | |
| 513 } | |
| 514 | |
| 515 // Both audio and video devices are ready for copying. | |
| 516 // TODO(grunell): Add support for output devices and group id. | |
| 517 blink::WebVector<blink::WebMediaDeviceInfo> | |
| 518 devices(request->audio_input_devices.size() + | |
| 519 request->video_input_devices.size()); | |
| 520 for (size_t i = 0; i < request->audio_input_devices.size(); ++i) { | |
| 521 const MediaStreamDevice& device = request->audio_input_devices[i].device; | |
| 522 DCHECK_EQ(device.type, MEDIA_DEVICE_AUDIO_CAPTURE); | |
| 523 devices[i].initialize(blink::WebString::fromUTF8(device.id), | |
| 524 blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput, | |
| 525 blink::WebString::fromUTF8(device.name), | |
| 526 blink::WebString()); | |
| 527 } | |
| 528 size_t audio_size = request->audio_input_devices.size(); | |
| 529 for (size_t i = 0; i < request->video_input_devices.size(); ++i) { | |
| 530 const MediaStreamDevice& device = request->video_input_devices[i].device; | |
| 531 DCHECK_EQ(device.type, MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 532 devices[audio_size + i].initialize( | |
| 533 blink::WebString::fromUTF8(device.id), | |
| 534 blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput, | |
| 535 blink::WebString::fromUTF8(device.name), | |
| 536 blink::WebString()); | |
| 537 } | |
| 538 | |
| 539 EnumerateDevicesSucceded(&request->request, devices); | |
| 540 | |
| 541 // Cancel device enumeration. | |
| 542 media_stream_dispatcher_->StopEnumerateDevices( | |
| 543 request->audio_input_request_id, | |
| 544 AsWeakPtr()); | |
| 545 media_stream_dispatcher_->StopEnumerateDevices( | |
| 546 request->video_input_request_id, | |
| 547 AsWeakPtr()); | |
| 548 | |
| 549 DeleteMediaDevicesRequestInfo(request); | |
| 424 } | 550 } |
| 425 | 551 |
| 426 void MediaStreamImpl::OnDeviceOpened( | 552 void MediaStreamImpl::OnDeviceOpened( |
| 427 int request_id, | 553 int request_id, |
| 428 const std::string& label, | 554 const std::string& label, |
| 429 const StreamDeviceInfo& video_device) { | 555 const StreamDeviceInfo& video_device) { |
| 430 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" | 556 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" |
| 431 << request_id << ", " << label << ")"; | 557 << request_id << ", " << label << ")"; |
| 432 NOTIMPLEMENTED(); | 558 NOTIMPLEMENTED(); |
| 433 } | 559 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 478 break; | 604 break; |
| 479 case MEDIA_DEVICE_TRACK_START_FAILURE: | 605 case MEDIA_DEVICE_TRACK_START_FAILURE: |
| 480 request_info->requestFailedUASpecific("TrackStartError"); | 606 request_info->requestFailedUASpecific("TrackStartError"); |
| 481 break; | 607 break; |
| 482 default: | 608 default: |
| 483 request_info->requestFailed(); | 609 request_info->requestFailed(); |
| 484 break; | 610 break; |
| 485 } | 611 } |
| 486 } | 612 } |
| 487 | 613 |
| 614 void MediaStreamImpl::EnumerateDevicesSucceded( | |
| 615 blink::WebMediaDevicesRequest* request, | |
| 616 blink::WebVector<blink::WebMediaDeviceInfo>& devices) { | |
| 617 request->requestSucceeded(devices); | |
| 618 } | |
| 619 | |
| 488 const blink::WebMediaStreamSource* MediaStreamImpl::FindLocalSource( | 620 const blink::WebMediaStreamSource* MediaStreamImpl::FindLocalSource( |
| 489 const StreamDeviceInfo& device) const { | 621 const StreamDeviceInfo& device) const { |
| 490 for (LocalStreamSources::const_iterator it = local_sources_.begin(); | 622 for (LocalStreamSources::const_iterator it = local_sources_.begin(); |
| 491 it != local_sources_.end(); ++it) { | 623 it != local_sources_.end(); ++it) { |
| 492 MediaStreamSource* source = | 624 MediaStreamSource* source = |
| 493 static_cast<MediaStreamSource*>(it->source.extraData()); | 625 static_cast<MediaStreamSource*>(it->source.extraData()); |
| 494 const StreamDeviceInfo& active_device = source->device_info(); | 626 const StreamDeviceInfo& active_device = source->device_info(); |
| 495 if (active_device.device.id == device.device.id && | 627 if (active_device.device.id == device.device.id && |
| 496 active_device.device.type == device.device.type && | 628 active_device.device.type == device.device.type && |
| 497 active_device.session_id == device.session_id) { | 629 active_device.session_id == device.session_id) { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 527 UserMediaRequests::iterator it = user_media_requests_.begin(); | 659 UserMediaRequests::iterator it = user_media_requests_.begin(); |
| 528 for (; it != user_media_requests_.end(); ++it) { | 660 for (; it != user_media_requests_.end(); ++it) { |
| 529 if ((*it) == request) { | 661 if ((*it) == request) { |
| 530 user_media_requests_.erase(it); | 662 user_media_requests_.erase(it); |
| 531 return; | 663 return; |
| 532 } | 664 } |
| 533 } | 665 } |
| 534 NOTREACHED(); | 666 NOTREACHED(); |
| 535 } | 667 } |
| 536 | 668 |
| 669 MediaStreamImpl::MediaDevicesRequestInfo* | |
| 670 MediaStreamImpl::FindMediaDevicesRequestInfo( | |
| 671 int request_id) { | |
| 672 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
| 673 for (; it != media_devices_requests_.end(); ++it) { | |
| 674 if ((*it)->audio_input_request_id == request_id || | |
| 675 (*it)->video_input_request_id == request_id) { | |
| 676 return (*it); | |
| 677 } | |
| 678 } | |
| 679 return NULL; | |
| 680 } | |
| 681 | |
| 682 MediaStreamImpl::MediaDevicesRequestInfo* | |
| 683 MediaStreamImpl::FindMediaDevicesRequestInfo( | |
| 684 const blink::WebMediaDevicesRequest& request) { | |
| 685 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
| 686 for (; it != media_devices_requests_.end(); ++it) { | |
| 687 if ((*it)->request == request) | |
| 688 return (*it); | |
| 689 } | |
| 690 return NULL; | |
| 691 } | |
| 692 | |
| 693 void MediaStreamImpl::DeleteMediaDevicesRequestInfo( | |
| 694 MediaDevicesRequestInfo* request) { | |
| 695 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
| 696 for (; it != media_devices_requests_.end(); ++it) { | |
| 697 if ((*it) == request) { | |
| 698 media_devices_requests_.erase(it); | |
| 699 return; | |
| 700 } | |
| 701 } | |
| 702 NOTREACHED(); | |
| 703 } | |
| 704 | |
| 537 void MediaStreamImpl::FrameDetached(blink::WebFrame* frame) { | 705 void MediaStreamImpl::FrameDetached(blink::WebFrame* frame) { |
| 538 // Do same thing as FrameWillClose. | 706 // Do same thing as FrameWillClose. |
| 539 FrameWillClose(frame); | 707 FrameWillClose(frame); |
| 540 } | 708 } |
| 541 | 709 |
| 542 void MediaStreamImpl::FrameWillClose(blink::WebFrame* frame) { | 710 void MediaStreamImpl::FrameWillClose(blink::WebFrame* frame) { |
| 543 // Loop through all UserMediaRequests and find the requests that belong to the | 711 // Loop through all UserMediaRequests and find the requests that belong to the |
| 544 // frame that is being closed. | 712 // frame that is being closed. |
| 545 UserMediaRequests::iterator request_it = user_media_requests_.begin(); | 713 UserMediaRequests::iterator request_it = user_media_requests_.begin(); |
| 546 while (request_it != user_media_requests_.end()) { | 714 while (request_it != user_media_requests_.end()) { |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 709 sources_.begin(); | 877 sources_.begin(); |
| 710 it != sources_.end(); ++it) { | 878 it != sources_.end(); ++it) { |
| 711 if (source.id() == it->id()) { | 879 if (source.id() == it->id()) { |
| 712 sources_.erase(it); | 880 sources_.erase(it); |
| 713 return; | 881 return; |
| 714 } | 882 } |
| 715 } | 883 } |
| 716 } | 884 } |
| 717 | 885 |
| 718 } // namespace content | 886 } // namespace content |
| OLD | NEW |