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/string_number_conversions.h" | 10 #include "base/string_number_conversions.h" |
11 #include "base/stringprintf.h" | 11 #include "base/stringprintf.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "content/renderer/media/capture_video_decoder.h" | 13 #include "content/renderer/media/capture_video_decoder.h" |
14 #include "content/renderer/media/local_video_capture.h" | 14 #include "content/renderer/media/local_video_capture.h" |
15 #include "content/renderer/media/media_stream_extra_data.h" | 15 #include "content/renderer/media/media_stream_extra_data.h" |
16 #include "content/renderer/media/media_stream_source_extra_data.h" | 16 #include "content/renderer/media/media_stream_source_extra_data.h" |
17 #include "content/renderer/media/media_stream_dependency_factory.h" | 17 #include "content/renderer/media/media_stream_dependency_factory.h" |
18 #include "content/renderer/media/media_stream_dispatcher.h" | 18 #include "content/renderer/media/media_stream_dispatcher.h" |
19 #include "content/renderer/media/rtc_video_decoder.h" | 19 #include "content/renderer/media/rtc_video_decoder.h" |
20 #include "content/renderer/media/rtc_video_renderer.h" | 20 #include "content/renderer/media/rtc_video_renderer.h" |
21 #include "content/renderer/media/video_capture_impl_manager.h" | 21 #include "content/renderer/media/video_capture_impl_manager.h" |
22 #include "content/renderer/media/webrtc_uma_histograms.h" | 22 #include "content/renderer/media/webrtc_uma_histograms.h" |
23 #include "media/base/message_loop_factory.h" | 23 #include "media/base/message_loop_factory.h" |
| 24 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints
.h" |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr
y.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr
y.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amComponent.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amComponent.h" |
29 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" | 30 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amDescriptor.h" |
30 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" | 31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre
amSource.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
32 | 33 |
33 namespace { | 34 namespace { |
| 35 |
34 const int kVideoCaptureWidth = 640; | 36 const int kVideoCaptureWidth = 640; |
35 const int kVideoCaptureHeight = 480; | 37 const int kVideoCaptureHeight = 480; |
36 const int kVideoCaptureFramePerSecond = 30; | 38 const int kVideoCaptureFramePerSecond = 30; |
| 39 |
| 40 std::string GetMandatoryStreamConstraint( |
| 41 const WebKit::WebMediaConstraints& constraints, const std::string& key) { |
| 42 if (constraints.isNull()) |
| 43 return std::string(); |
| 44 |
| 45 WebKit::WebString value; |
| 46 constraints.getMandatoryConstraintValue(UTF8ToUTF16(key), value); |
| 47 return UTF16ToUTF8(value); |
| 48 } |
| 49 |
| 50 void UpdateOptionsIfTabMediaRequest( |
| 51 const WebKit::WebUserMediaRequest& user_media_request, |
| 52 media_stream::StreamOptions* options) { |
| 53 if (options->audio_type != content::MEDIA_NO_SERVICE && |
| 54 GetMandatoryStreamConstraint(user_media_request.audioConstraints(), |
| 55 media_stream::kMediaStreamSource) == |
| 56 media_stream::kMediaStreamSourceTab) { |
| 57 options->audio_type = content::MEDIA_TAB_AUDIO_CAPTURE; |
| 58 options->audio_device_id = GetMandatoryStreamConstraint( |
| 59 user_media_request.audioConstraints(), |
| 60 media_stream::kMediaStreamSourceId); |
| 61 } |
| 62 |
| 63 if (options->video_type != content::MEDIA_NO_SERVICE && |
| 64 GetMandatoryStreamConstraint(user_media_request.videoConstraints(), |
| 65 media_stream::kMediaStreamSource) == |
| 66 media_stream::kMediaStreamSourceTab) { |
| 67 options->video_type = content::MEDIA_TAB_VIDEO_CAPTURE; |
| 68 options->video_device_id = GetMandatoryStreamConstraint( |
| 69 user_media_request.videoConstraints(), |
| 70 media_stream::kMediaStreamSourceId); |
| 71 } |
| 72 } |
| 73 |
37 } // namespace | 74 } // namespace |
38 | 75 |
39 static int g_next_request_id = 0; | 76 static int g_next_request_id = 0; |
40 | 77 |
41 // Creates a WebKit representation of a stream sources based on | 78 // Creates a WebKit representation of a stream sources based on |
42 // |devices| from the MediaStreamDispatcher. | 79 // |devices| from the MediaStreamDispatcher. |
43 static void CreateWebKitSourceVector( | 80 static void CreateWebKitSourceVector( |
44 const std::string& label, | 81 const std::string& label, |
45 const media_stream::StreamDeviceInfoArray& devices, | 82 const media_stream::StreamDeviceInfoArray& devices, |
46 WebKit::WebMediaStreamSource::Type type, | 83 WebKit::WebMediaStreamSource::Type type, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 void MediaStreamImpl::requestUserMedia( | 119 void MediaStreamImpl::requestUserMedia( |
83 const WebKit::WebUserMediaRequest& user_media_request, | 120 const WebKit::WebUserMediaRequest& user_media_request, |
84 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, | 121 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, |
85 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { | 122 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { |
86 // Save histogram data so we can see how much GetUserMedia is used. | 123 // Save histogram data so we can see how much GetUserMedia is used. |
87 // The histogram counts the number of calls to the JS API | 124 // The histogram counts the number of calls to the JS API |
88 // webGetUserMedia. | 125 // webGetUserMedia. |
89 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 126 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
90 DCHECK(CalledOnValidThread()); | 127 DCHECK(CalledOnValidThread()); |
91 int request_id = g_next_request_id++; | 128 int request_id = g_next_request_id++; |
92 bool audio = false; | 129 media_stream::StreamOptions options(content::MEDIA_NO_SERVICE, |
93 bool video = false; | 130 content::MEDIA_NO_SERVICE); |
94 WebKit::WebFrame* frame = NULL; | 131 WebKit::WebFrame* frame = NULL; |
95 GURL security_origin; | 132 GURL security_origin; |
96 | 133 |
97 // |user_media_request| can't be mocked. So in order to test at all we check | 134 // |user_media_request| can't be mocked. So in order to test at all we check |
98 // if it isNull. | 135 // if it isNull. |
99 if (user_media_request.isNull()) { | 136 if (user_media_request.isNull()) { |
100 // We are in a test. | 137 // We are in a test. |
101 audio = audio_sources.size() > 0; | 138 if (audio_sources.size() > 0) |
102 video = video_sources.size() > 0; | 139 options.audio_type = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
| 140 if (video_sources.size() > 0) |
| 141 options.video_type = content::MEDIA_DEVICE_VIDEO_CAPTURE; |
103 } else { | 142 } else { |
104 audio = user_media_request.audio(); | 143 if (user_media_request.audio()) |
105 video = user_media_request.video(); | 144 options.audio_type = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
| 145 if (user_media_request.video()) |
| 146 options.video_type = content::MEDIA_DEVICE_VIDEO_CAPTURE; |
| 147 |
106 security_origin = GURL(user_media_request.securityOrigin().toString()); | 148 security_origin = GURL(user_media_request.securityOrigin().toString()); |
107 // Get the WebFrame that requested a MediaStream. | 149 // Get the WebFrame that requested a MediaStream. |
108 // The frame is needed to tell the MediaStreamDispatcher when a stream goes | 150 // The frame is needed to tell the MediaStreamDispatcher when a stream goes |
109 // out of scope. | 151 // out of scope. |
110 frame = user_media_request.ownerDocument().frame(); | 152 frame = user_media_request.ownerDocument().frame(); |
111 DCHECK(frame); | 153 DCHECK(frame); |
| 154 |
| 155 UpdateOptionsIfTabMediaRequest(user_media_request, &options); |
112 } | 156 } |
113 | 157 |
114 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | 158 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " |
115 << (audio ? "audio" : "") | 159 << "audio=" << (options.audio_type) |
116 << (user_media_request.video() ? " video" : "") << "], " | 160 << ", video=" << (options.video_type) << " ], " |
117 << security_origin.spec() << ")"; | 161 << security_origin.spec() << ")"; |
118 | 162 |
119 user_media_requests_[request_id] = | 163 user_media_requests_[request_id] = |
120 UserMediaRequestInfo(frame, user_media_request); | 164 UserMediaRequestInfo(frame, user_media_request); |
121 | 165 |
122 media_stream_dispatcher_->GenerateStream( | 166 media_stream_dispatcher_->GenerateStream( |
123 request_id, | 167 request_id, |
124 AsWeakPtr(), | 168 AsWeakPtr(), |
125 media_stream::StreamOptions(audio, video), | 169 options, |
126 security_origin); | 170 security_origin); |
127 } | 171 } |
128 | 172 |
129 void MediaStreamImpl::cancelUserMediaRequest( | 173 void MediaStreamImpl::cancelUserMediaRequest( |
130 const WebKit::WebUserMediaRequest& user_media_request) { | 174 const WebKit::WebUserMediaRequest& user_media_request) { |
131 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
132 MediaRequestMap::iterator it = user_media_requests_.begin(); | 176 MediaRequestMap::iterator it = user_media_requests_.begin(); |
133 for (; it != user_media_requests_.end(); ++it) { | 177 for (; it != user_media_requests_.end(); ++it) { |
134 if (it->second.request_ == user_media_request) | 178 if (it->second.request_ == user_media_request) |
135 break; | 179 break; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
461 | 505 |
462 void MediaStreamExtraData::SetLocalStreamStopCallback( | 506 void MediaStreamExtraData::SetLocalStreamStopCallback( |
463 const StreamStopCallback& stop_callback) { | 507 const StreamStopCallback& stop_callback) { |
464 stream_stop_callback_ = stop_callback; | 508 stream_stop_callback_ = stop_callback; |
465 } | 509 } |
466 | 510 |
467 void MediaStreamExtraData::OnLocalStreamStop() { | 511 void MediaStreamExtraData::OnLocalStreamStop() { |
468 if (!stream_stop_callback_.is_null()) | 512 if (!stream_stop_callback_.is_null()) |
469 stream_stop_callback_.Run(local_stream_->label()); | 513 stream_stop_callback_.Run(local_stream_->label()); |
470 } | 514 } |
OLD | NEW |