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_renderer.h" | 14 #include "content/renderer/media/media_stream_audio_renderer.h" |
15 #include "content/renderer/media/media_stream_audio_source.h" | 15 #include "content/renderer/media/media_stream_audio_source.h" |
16 #include "content/renderer/media/media_stream_dispatcher.h" | 16 #include "content/renderer/media/media_stream_dispatcher.h" |
17 #include "content/renderer/media/media_stream_video_capturer_source.h" | 17 #include "content/renderer/media/media_stream_video_capturer_source.h" |
18 #include "content/renderer/media/media_stream_video_track.h" | 18 #include "content/renderer/media/media_stream_video_track.h" |
19 #include "content/renderer/media/peer_connection_tracker.h" | 19 #include "content/renderer/media/peer_connection_tracker.h" |
20 #include "content/renderer/media/rtc_video_renderer.h" | 20 #include "content/renderer/media/rtc_video_renderer.h" |
21 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 21 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
22 #include "content/renderer/media/webrtc_audio_capturer.h" | 22 #include "content/renderer/media/webrtc_audio_capturer.h" |
23 #include "content/renderer/media/webrtc_audio_renderer.h" | 23 #include "content/renderer/media/webrtc_audio_renderer.h" |
24 #include "content/renderer/media/webrtc_local_audio_renderer.h" | 24 #include "content/renderer/media/webrtc_local_audio_renderer.h" |
25 #include "content/renderer/media/webrtc_logging.h" | 25 #include "content/renderer/media/webrtc_logging.h" |
26 #include "content/renderer/media/webrtc_uma_histograms.h" | 26 #include "content/renderer/media/webrtc_uma_histograms.h" |
27 #include "content/renderer/render_thread_impl.h" | 27 #include "content/renderer/render_thread_impl.h" |
28 #include "media/base/audio_hardware_config.h" | 28 #include "media/base/audio_hardware_config.h" |
29 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" | 29 #include "third_party/WebKit/public/platform/WebMediaConstraints.h" |
30 #include "third_party/WebKit/public/platform/WebMediaDeviceInfo.h" | |
30 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 31 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
31 #include "third_party/WebKit/public/web/WebDocument.h" | 32 #include "third_party/WebKit/public/web/WebDocument.h" |
32 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 33 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
33 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" | 34 #include "third_party/WebKit/public/web/WebMediaStreamRegistry.h" |
34 | 35 |
35 namespace content { | 36 namespace content { |
36 namespace { | 37 namespace { |
37 | 38 |
38 void CopyStreamConstraints(const blink::WebMediaConstraints& constraints, | 39 void CopyStreamConstraints(const blink::WebMediaConstraints& constraints, |
39 StreamOptions::Constraints* mandatory, | 40 StreamOptions::Constraints* mandatory, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
73 MediaStreamDispatcher* media_stream_dispatcher, | 74 MediaStreamDispatcher* media_stream_dispatcher, |
74 PeerConnectionDependencyFactory* dependency_factory) | 75 PeerConnectionDependencyFactory* dependency_factory) |
75 : RenderViewObserver(render_view), | 76 : RenderViewObserver(render_view), |
76 dependency_factory_(dependency_factory), | 77 dependency_factory_(dependency_factory), |
77 media_stream_dispatcher_(media_stream_dispatcher) { | 78 media_stream_dispatcher_(media_stream_dispatcher) { |
78 } | 79 } |
79 | 80 |
80 MediaStreamImpl::~MediaStreamImpl() { | 81 MediaStreamImpl::~MediaStreamImpl() { |
81 } | 82 } |
82 | 83 |
84 // TODO(grunell): Device enumeration should probably be moved to its own class | |
perkj_chrome
2014/05/28 14:49:14
I think you should remove this todo. Its fine here
perkj_chrome
2014/05/28 14:49:14
nit: Move to before MediaStreamImpl ctor.
Henrik Grunell
2014/06/02 08:19:48
Done.
Henrik Grunell
2014/06/02 08:19:48
Done.
| |
85 // here or at other places in the code. | |
86 struct MediaStreamImpl::MediaDevicesRequestInfo { | |
87 MediaDevicesRequestInfo(blink::WebMediaDevicesRequest request, | |
88 int audio_input_request_id, | |
89 int video_input_request_id) | |
90 : request(request), | |
91 audio_input_request_id(audio_input_request_id), | |
92 video_input_request_id(video_input_request_id), | |
93 has_audio_input_returned(false), | |
94 has_video_input_returned(false) {} | |
95 | |
96 blink::WebMediaDevicesRequest request; | |
97 int audio_input_request_id; | |
98 int video_input_request_id; | |
99 bool has_audio_input_returned; | |
100 bool has_video_input_returned; | |
101 StreamDeviceInfoArray audio_input_devices; | |
102 StreamDeviceInfoArray video_input_devices; | |
103 }; | |
104 | |
83 void MediaStreamImpl::requestUserMedia( | 105 void MediaStreamImpl::requestUserMedia( |
84 const blink::WebUserMediaRequest& user_media_request) { | 106 const blink::WebUserMediaRequest& user_media_request) { |
85 // Save histogram data so we can see how much GetUserMedia is used. | 107 // Save histogram data so we can see how much GetUserMedia is used. |
86 // The histogram counts the number of calls to the JS API | 108 // The histogram counts the number of calls to the JS API |
87 // webGetUserMedia. | 109 // webGetUserMedia. |
88 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 110 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
89 DCHECK(CalledOnValidThread()); | 111 DCHECK(CalledOnValidThread()); |
90 | 112 |
91 if (RenderThreadImpl::current()) { | 113 if (RenderThreadImpl::current()) { |
92 RenderThreadImpl::current()->peer_connection_tracker()->TrackGetUserMedia( | 114 RenderThreadImpl::current()->peer_connection_tracker()->TrackGetUserMedia( |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 DCHECK(CalledOnValidThread()); | 200 DCHECK(CalledOnValidThread()); |
179 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); | 201 UserMediaRequestInfo* request = FindUserMediaRequestInfo(user_media_request); |
180 if (request) { | 202 if (request) { |
181 // We can't abort the stream generation process. | 203 // We can't abort the stream generation process. |
182 // Instead, erase the request. Once the stream is generated we will stop the | 204 // Instead, erase the request. Once the stream is generated we will stop the |
183 // stream if the request does not exist. | 205 // stream if the request does not exist. |
184 DeleteUserMediaRequestInfo(request); | 206 DeleteUserMediaRequestInfo(request); |
185 } | 207 } |
186 } | 208 } |
187 | 209 |
210 void MediaStreamImpl::requestMediaDevices( | |
211 const blink::WebMediaDevicesRequest& media_devices_request) { | |
212 // TODO(grunell): UMA stats? | |
213 DCHECK(CalledOnValidThread()); | |
214 | |
215 int audio_input_request_id = g_next_request_id++; | |
216 int video_input_request_id = g_next_request_id++; | |
217 GURL security_origin = | |
218 GURL(media_devices_request.securityOrigin().toString()); | |
219 | |
220 media_devices_requests_.push_back(new MediaDevicesRequestInfo( | |
221 media_devices_request, audio_input_request_id, video_input_request_id)); | |
222 | |
223 media_stream_dispatcher_->EnumerateDevices( | |
224 audio_input_request_id, | |
225 AsWeakPtr(), | |
226 MEDIA_DEVICE_AUDIO_CAPTURE, | |
227 security_origin); | |
228 | |
229 media_stream_dispatcher_->EnumerateDevices( | |
230 video_input_request_id, | |
231 AsWeakPtr(), | |
232 MEDIA_DEVICE_VIDEO_CAPTURE, | |
233 security_origin); | |
234 } | |
235 | |
236 void MediaStreamImpl::cancelMediaDevicesRequest( | |
237 const blink::WebMediaDevicesRequest& media_devices_request) { | |
238 DCHECK(CalledOnValidThread()); | |
239 MediaDevicesRequestInfo* request = | |
240 FindMediaDevicesRequestInfo(media_devices_request); | |
241 if (!request) | |
242 return; | |
243 | |
244 // Cancel device enumeration. | |
245 media_stream_dispatcher_->StopEnumerateDevices( | |
246 request->audio_input_request_id, | |
247 AsWeakPtr()); | |
248 media_stream_dispatcher_->StopEnumerateDevices( | |
249 request->video_input_request_id, | |
250 AsWeakPtr()); | |
251 DeleteMediaDevicesRequestInfo(request); | |
252 } | |
253 | |
188 blink::WebMediaStream MediaStreamImpl::GetMediaStream( | 254 blink::WebMediaStream MediaStreamImpl::GetMediaStream( |
189 const GURL& url) { | 255 const GURL& url) { |
190 return blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url); | 256 return blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url); |
191 } | 257 } |
192 | 258 |
193 bool MediaStreamImpl::IsMediaStream(const GURL& url) { | 259 bool MediaStreamImpl::IsMediaStream(const GURL& url) { |
194 blink::WebMediaStream web_stream( | 260 blink::WebMediaStream web_stream( |
195 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url)); | 261 blink::WebMediaStreamRegistry::lookupMediaStreamDescriptor(url)); |
196 | 262 |
197 return (!web_stream.isNull() && | 263 return (!web_stream.isNull() && |
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
519 GetUserMediaRequestSucceeded(request->web_stream, &request->request); | 585 GetUserMediaRequestSucceeded(request->web_stream, &request->request); |
520 else | 586 else |
521 GetUserMediaRequestFailed(&request->request, result); | 587 GetUserMediaRequestFailed(&request->request, result); |
522 | 588 |
523 DeleteUserMediaRequestInfo(request); | 589 DeleteUserMediaRequestInfo(request); |
524 } | 590 } |
525 | 591 |
526 void MediaStreamImpl::OnDevicesEnumerated( | 592 void MediaStreamImpl::OnDevicesEnumerated( |
527 int request_id, | 593 int request_id, |
528 const StreamDeviceInfoArray& device_array) { | 594 const StreamDeviceInfoArray& device_array) { |
529 DVLOG(1) << "MediaStreamImpl::OnDevicesEnumerated(" | 595 DVLOG(1) << "MediaStreamImpl::OnDevicesEnumerated(" << request_id << ")"; |
530 << request_id << ")"; | 596 |
531 NOTIMPLEMENTED(); | 597 MediaDevicesRequestInfo* request = FindMediaDevicesRequestInfo(request_id); |
598 DCHECK(request); | |
599 | |
600 if (request_id == request->audio_input_request_id) { | |
601 request->has_audio_input_returned = true; | |
602 DCHECK(request->audio_input_devices.empty()); | |
603 request->audio_input_devices = device_array; | |
604 } else { | |
605 DCHECK(request_id == request->video_input_request_id); | |
606 request->has_video_input_returned = true; | |
607 DCHECK(request->video_input_devices.empty()); | |
608 request->video_input_devices = device_array; | |
609 } | |
610 | |
611 if (!request->has_audio_input_returned || | |
612 !request->has_video_input_returned) { | |
613 // Wait for the rest of the devices to complete. | |
614 return; | |
615 } | |
616 | |
617 // Both audio and video devices are ready for copying. | |
618 // TODO(grunell): Add support for output devices and group id. | |
619 blink::WebVector<blink::WebMediaDeviceInfo> | |
620 devices(request->audio_input_devices.size() + | |
621 request->video_input_devices.size()); | |
622 for (size_t i = 0; i < request->audio_input_devices.size(); ++i) { | |
623 const MediaStreamDevice& device = request->audio_input_devices[i].device; | |
624 DCHECK_EQ(device.type, MEDIA_DEVICE_AUDIO_CAPTURE); | |
625 devices[i].initialize(blink::WebString::fromUTF8(device.id), | |
626 blink::WebMediaDeviceInfo::MediaDeviceKindAudioInput, | |
627 blink::WebString::fromUTF8(device.name), | |
628 blink::WebString()); | |
629 } | |
630 size_t audio_size = request->audio_input_devices.size(); | |
631 for (size_t i = 0; i < request->video_input_devices.size(); ++i) { | |
632 const MediaStreamDevice& device = request->video_input_devices[i].device; | |
633 DCHECK_EQ(device.type, MEDIA_DEVICE_VIDEO_CAPTURE); | |
634 devices[audio_size + i].initialize( | |
635 blink::WebString::fromUTF8(device.id), | |
636 blink::WebMediaDeviceInfo::MediaDeviceKindVideoInput, | |
637 blink::WebString::fromUTF8(device.name), | |
638 blink::WebString()); | |
639 } | |
640 request->request.requestSucceeded(devices); | |
641 | |
642 DeleteMediaDevicesRequestInfo(request); | |
532 } | 643 } |
533 | 644 |
534 void MediaStreamImpl::OnDeviceOpened( | 645 void MediaStreamImpl::OnDeviceOpened( |
535 int request_id, | 646 int request_id, |
536 const std::string& label, | 647 const std::string& label, |
537 const StreamDeviceInfo& video_device) { | 648 const StreamDeviceInfo& video_device) { |
538 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" | 649 DVLOG(1) << "MediaStreamImpl::OnDeviceOpened(" |
539 << request_id << ", " << label << ")"; | 650 << request_id << ", " << label << ")"; |
540 NOTIMPLEMENTED(); | 651 NOTIMPLEMENTED(); |
541 } | 652 } |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 UserMediaRequests::iterator it = user_media_requests_.begin(); | 746 UserMediaRequests::iterator it = user_media_requests_.begin(); |
636 for (; it != user_media_requests_.end(); ++it) { | 747 for (; it != user_media_requests_.end(); ++it) { |
637 if ((*it) == request) { | 748 if ((*it) == request) { |
638 user_media_requests_.erase(it); | 749 user_media_requests_.erase(it); |
639 return; | 750 return; |
640 } | 751 } |
641 } | 752 } |
642 NOTREACHED(); | 753 NOTREACHED(); |
643 } | 754 } |
644 | 755 |
756 MediaStreamImpl::MediaDevicesRequestInfo* | |
757 MediaStreamImpl::FindMediaDevicesRequestInfo( | |
758 int request_id) { | |
759 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
760 for (; it != media_devices_requests_.end(); ++it) { | |
761 if ((*it)->audio_input_request_id == request_id || | |
762 (*it)->video_input_request_id == request_id) { | |
763 return (*it); | |
764 } | |
765 } | |
766 return NULL; | |
767 } | |
768 | |
769 MediaStreamImpl::MediaDevicesRequestInfo* | |
770 MediaStreamImpl::FindMediaDevicesRequestInfo( | |
771 const blink::WebMediaDevicesRequest& request) { | |
772 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
773 for (; it != media_devices_requests_.end(); ++it) { | |
774 if ((*it)->request == request) | |
775 return (*it); | |
776 } | |
777 return NULL; | |
778 } | |
779 | |
780 void MediaStreamImpl::DeleteMediaDevicesRequestInfo( | |
781 MediaDevicesRequestInfo* request) { | |
782 MediaDevicesRequests::iterator it = media_devices_requests_.begin(); | |
783 for (; it != media_devices_requests_.end(); ++it) { | |
784 if ((*it) == request) { | |
785 media_devices_requests_.erase(it); | |
786 return; | |
787 } | |
788 } | |
789 NOTREACHED(); | |
790 } | |
791 | |
645 void MediaStreamImpl::FrameDetached(blink::WebFrame* frame) { | 792 void MediaStreamImpl::FrameDetached(blink::WebFrame* frame) { |
646 // Do same thing as FrameWillClose. | 793 // Do same thing as FrameWillClose. |
647 FrameWillClose(frame); | 794 FrameWillClose(frame); |
648 } | 795 } |
649 | 796 |
650 void MediaStreamImpl::FrameWillClose(blink::WebFrame* frame) { | 797 void MediaStreamImpl::FrameWillClose(blink::WebFrame* frame) { |
651 // Loop through all UserMediaRequests and find the requests that belong to the | 798 // Loop through all UserMediaRequests and find the requests that belong to the |
652 // frame that is being closed. | 799 // frame that is being closed. |
653 UserMediaRequests::iterator request_it = user_media_requests_.begin(); | 800 UserMediaRequests::iterator request_it = user_media_requests_.begin(); |
654 while (request_it != user_media_requests_.end()) { | 801 while (request_it != user_media_requests_.end()) { |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
877 sources_.begin(); | 1024 sources_.begin(); |
878 it != sources_.end(); ++it) { | 1025 it != sources_.end(); ++it) { |
879 if (source.id() == it->id()) { | 1026 if (source.id() == it->id()) { |
880 sources_.erase(it); | 1027 sources_.erase(it); |
881 return; | 1028 return; |
882 } | 1029 } |
883 } | 1030 } |
884 } | 1031 } |
885 | 1032 |
886 } // namespace content | 1033 } // namespace content |
OLD | NEW |