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/public/common/media_stream_request.h" | |
perkj_chrome
2012/10/09 11:10:02
Get rid of this include. It does not belong here.
justinlin
2012/10/11 07:19:21
Done.
| |
13 #include "content/renderer/media/capture_video_decoder.h" | 14 #include "content/renderer/media/capture_video_decoder.h" |
14 #include "content/renderer/media/local_video_capture.h" | 15 #include "content/renderer/media/local_video_capture.h" |
15 #include "content/renderer/media/media_stream_extra_data.h" | 16 #include "content/renderer/media/media_stream_extra_data.h" |
16 #include "content/renderer/media/media_stream_source_extra_data.h" | 17 #include "content/renderer/media/media_stream_source_extra_data.h" |
17 #include "content/renderer/media/media_stream_dependency_factory.h" | 18 #include "content/renderer/media/media_stream_dependency_factory.h" |
18 #include "content/renderer/media/media_stream_dispatcher.h" | 19 #include "content/renderer/media/media_stream_dispatcher.h" |
19 #include "content/renderer/media/rtc_video_decoder.h" | 20 #include "content/renderer/media/rtc_video_decoder.h" |
20 #include "content/renderer/media/rtc_video_renderer.h" | 21 #include "content/renderer/media/rtc_video_renderer.h" |
21 #include "content/renderer/media/video_capture_impl_manager.h" | 22 #include "content/renderer/media/video_capture_impl_manager.h" |
22 #include "content/renderer/media/webrtc_uma_histograms.h" | 23 #include "content/renderer/media/webrtc_uma_histograms.h" |
23 #include "media/base/message_loop_factory.h" | 24 #include "media/base/message_loop_factory.h" |
25 #include "third_party/WebKit/Source/Platform/chromium/public/WebMediaConstraints .h" | |
24 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
25 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
26 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr y.h" | 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebMediaStreamRegistr y.h" |
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
28 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amComponent.h" | 30 #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" | 31 #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" | 32 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebMediaStre amSource.h" |
31 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" | 33 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebVector.h" |
32 | 34 |
33 namespace { | 35 namespace { |
36 | |
34 const int kVideoCaptureWidth = 640; | 37 const int kVideoCaptureWidth = 640; |
35 const int kVideoCaptureHeight = 480; | 38 const int kVideoCaptureHeight = 480; |
36 const int kVideoCaptureFramePerSecond = 30; | 39 const int kVideoCaptureFramePerSecond = 30; |
40 | |
41 const char kExtensionScheme[] = "chrome-extension"; | |
42 | |
43 std::string GetMandatoryStreamConstraint( | |
44 const WebKit::WebMediaConstraints constraints, const std::string& key) { | |
perkj_chrome
2012/10/09 11:10:02
const WebKit::WebMediaConstraints&
justinlin
2012/10/11 07:19:21
Done.
| |
45 if (constraints.isNull()) | |
46 return ""; | |
47 | |
48 WebKit::WebString value; | |
49 constraints.getMandatoryConstraintValue(UTF8ToUTF16(key), value); | |
50 return UTF16ToUTF8(value); | |
51 } | |
52 | |
37 } // namespace | 53 } // namespace |
38 | 54 |
39 static int g_next_request_id = 0; | 55 static int g_next_request_id = 0; |
40 | 56 |
41 // Creates a WebKit representation of a stream sources based on | 57 // Creates a WebKit representation of a stream sources based on |
42 // |devices| from the MediaStreamDispatcher. | 58 // |devices| from the MediaStreamDispatcher. |
43 static void CreateWebKitSourceVector( | 59 static void CreateWebKitSourceVector( |
44 const std::string& label, | 60 const std::string& label, |
45 const media_stream::StreamDeviceInfoArray& devices, | 61 const media_stream::StreamDeviceInfoArray& devices, |
46 WebKit::WebMediaStreamSource::Type type, | 62 WebKit::WebMediaStreamSource::Type type, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 96 } |
81 | 97 |
82 void MediaStreamImpl::requestUserMedia( | 98 void MediaStreamImpl::requestUserMedia( |
83 const WebKit::WebUserMediaRequest& user_media_request, | 99 const WebKit::WebUserMediaRequest& user_media_request, |
84 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, | 100 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, |
85 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { | 101 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { |
86 // Save histogram data so we can see how much GetUserMedia is used. | 102 // Save histogram data so we can see how much GetUserMedia is used. |
87 // The histogram counts the number of calls to the JS API | 103 // The histogram counts the number of calls to the JS API |
88 // webGetUserMedia. | 104 // webGetUserMedia. |
89 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 105 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
106 | |
90 DCHECK(CalledOnValidThread()); | 107 DCHECK(CalledOnValidThread()); |
91 int request_id = g_next_request_id++; | 108 int request_id = g_next_request_id++; |
92 bool audio = false; | 109 content::MediaStreamDeviceType audio = content::MEDIA_NO_SERVICE; |
93 bool video = false; | 110 content::MediaStreamDeviceType video = content::MEDIA_NO_SERVICE; |
94 WebKit::WebFrame* frame = NULL; | 111 WebKit::WebFrame* frame = NULL; |
95 GURL security_origin; | 112 GURL security_origin; |
96 | 113 |
97 // |user_media_request| can't be mocked. So in order to test at all we check | 114 // |user_media_request| can't be mocked. So in order to test at all we check |
98 // if it isNull. | 115 // if it isNull. |
99 if (user_media_request.isNull()) { | 116 if (user_media_request.isNull()) { |
100 // We are in a test. | 117 // We are in a test. |
101 audio = audio_sources.size() > 0; | 118 if (audio_sources.size() > 0) |
102 video = video_sources.size() > 0; | 119 audio = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
120 if (video_sources.size() > 0) | |
121 video = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
103 } else { | 122 } else { |
104 audio = user_media_request.audio(); | 123 if (user_media_request.audio()) |
105 video = user_media_request.video(); | 124 audio = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
125 if (user_media_request.video()) | |
126 video = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
127 | |
106 security_origin = GURL(user_media_request.securityOrigin().toString()); | 128 security_origin = GURL(user_media_request.securityOrigin().toString()); |
107 // Get the WebFrame that requested a MediaStream. | 129 // Get the WebFrame that requested a MediaStream. |
108 // The frame is needed to tell the MediaStreamDispatcher when a stream goes | 130 // The frame is needed to tell the MediaStreamDispatcher when a stream goes |
109 // out of scope. | 131 // out of scope. |
110 frame = user_media_request.ownerDocument().frame(); | 132 frame = user_media_request.ownerDocument().frame(); |
111 DCHECK(frame); | 133 DCHECK(frame); |
112 } | 134 } |
135 if (audio != content::MEDIA_NO_SERVICE && | |
136 GetMandatoryStreamConstraint(user_media_request.audioConstraints(), | |
137 media_stream::kMediaStreamSource) == "tab") { | |
perkj_chrome
2012/10/09 11:10:02
define "tab" as media_stream::kMediaStreamSourceTa
justinlin
2012/10/11 07:19:21
Done.
| |
138 audio = content::MEDIA_TAB_AUDIO_CAPTURE; | |
139 } | |
113 | 140 |
114 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | 141 if (video != content::MEDIA_NO_SERVICE && |
115 << (audio ? "audio" : "") | 142 GetMandatoryStreamConstraint(user_media_request.videoConstraints(), |
116 << (user_media_request.video() ? " video" : "") << "], " | 143 media_stream::kMediaStreamSource) == "tab") { |
117 << security_origin.spec() << ")"; | 144 video = content::MEDIA_TAB_VIDEO_CAPTURE; |
145 } | |
146 | |
147 if (audio == content::MEDIA_TAB_AUDIO_CAPTURE || | |
148 video == content::MEDIA_TAB_VIDEO_CAPTURE) { | |
149 const GURL& url = user_media_request.ownerDocument().url(); | |
150 if (!url.SchemeIs(kExtensionScheme)) { | |
151 DVLOG(1) << "Tried to use tab capture outside extension API."; | |
perkj_chrome
2012/10/09 11:10:02
Change to error log
justinlin
2012/10/11 07:19:21
Done.
| |
152 return; | |
153 } | |
154 } | |
118 | 155 |
119 user_media_requests_[request_id] = | 156 user_media_requests_[request_id] = |
120 UserMediaRequestInfo(frame, user_media_request); | 157 UserMediaRequestInfo(frame, user_media_request); |
121 | 158 |
122 media_stream_dispatcher_->GenerateStream( | 159 if (audio == content::MEDIA_TAB_AUDIO_CAPTURE || |
perkj_chrome
2012/10/09 11:10:02
So it looks like you need video == content::MEDIA
justinlin
2012/10/11 07:19:21
Done.
| |
123 request_id, | 160 video == content::MEDIA_TAB_VIDEO_CAPTURE) { |
124 AsWeakPtr(), | 161 DVLOG(1) << "MediaStreamImpl::generateStreamForDevice(" |
125 media_stream::StreamOptions(audio, video), | 162 << request_id << ", [ " |
126 security_origin); | 163 << (audio ? "audio" : "") |
164 << (video ? " video" : "") << "], " | |
165 << security_origin.spec() << ")"; | |
166 | |
167 // TODO(justinlin): Right now we only use the mediaSourceId of the video. | |
168 media_stream_dispatcher_->GenerateStreamForDevice( | |
169 request_id, | |
170 AsWeakPtr(), | |
171 media_stream::StreamOptions(audio, video), | |
172 GetMandatoryStreamConstraint(user_media_request.videoConstraints(), | |
173 media_stream::kMediaStreamSourceId), | |
174 security_origin); | |
175 } else { | |
176 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | |
177 << (audio ? "audio" : "") | |
178 << (video ? " video" : "") << "], " | |
179 << security_origin.spec() << ")"; | |
180 | |
181 media_stream_dispatcher_->GenerateStream( | |
182 request_id, | |
183 AsWeakPtr(), | |
184 media_stream::StreamOptions(audio, video), | |
185 security_origin); | |
186 } | |
127 } | 187 } |
128 | 188 |
129 void MediaStreamImpl::cancelUserMediaRequest( | 189 void MediaStreamImpl::cancelUserMediaRequest( |
130 const WebKit::WebUserMediaRequest& user_media_request) { | 190 const WebKit::WebUserMediaRequest& user_media_request) { |
131 DCHECK(CalledOnValidThread()); | 191 DCHECK(CalledOnValidThread()); |
132 MediaRequestMap::iterator it = user_media_requests_.begin(); | 192 MediaRequestMap::iterator it = user_media_requests_.begin(); |
133 for (; it != user_media_requests_.end(); ++it) { | 193 for (; it != user_media_requests_.end(); ++it) { |
134 if (it->second.request_ == user_media_request) | 194 if (it->second.request_ == user_media_request) |
135 break; | 195 break; |
136 } | 196 } |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 | 516 |
457 void MediaStreamExtraData::SetLocalStreamStopCallback( | 517 void MediaStreamExtraData::SetLocalStreamStopCallback( |
458 const StreamStopCallback& stop_callback) { | 518 const StreamStopCallback& stop_callback) { |
459 stream_stop_callback_ = stop_callback; | 519 stream_stop_callback_ = stop_callback; |
460 } | 520 } |
461 | 521 |
462 void MediaStreamExtraData::OnLocalStreamStop() { | 522 void MediaStreamExtraData::OnLocalStreamStop() { |
463 if (!stream_stop_callback_.is_null()) | 523 if (!stream_stop_callback_.is_null()) |
464 stream_stop_callback_.Run(local_stream_->label()); | 524 stream_stop_callback_.Run(local_stream_->label()); |
465 } | 525 } |
OLD | NEW |