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 const char kExtensionScheme[] = "chrome-extension"; | |
41 | |
42 std::string GetMandatoryStreamConstraint( | |
43 const WebKit::WebMediaConstraints& constraints, const std::string& key) { | |
44 if (constraints.isNull()) | |
45 return ""; | |
no longer working on chromium
2012/10/11 09:07:25
I know both ways work, but what about using std::s
justinlin
2012/10/11 19:41:51
Done.
| |
46 | |
47 WebKit::WebString value; | |
48 constraints.getMandatoryConstraintValue(UTF8ToUTF16(key), value); | |
49 return UTF16ToUTF8(value); | |
50 } | |
51 | |
52 bool CheckAndUpdateAsTabMediaRequest( | |
53 const WebKit::WebUserMediaRequest& user_media_request, | |
54 content::MediaStreamDeviceType* audio, | |
no longer working on chromium
2012/10/11 09:07:25
pass a StreamOptions* as input param here.
justinlin
2012/10/11 19:41:51
Done.
| |
55 std::string* audio_device_id, | |
56 content::MediaStreamDeviceType* video, | |
57 std::string* video_device_id) { | |
58 if (*audio != content::MEDIA_NO_SERVICE && | |
59 GetMandatoryStreamConstraint(user_media_request.audioConstraints(), | |
60 media_stream::kMediaStreamSource) == | |
no longer working on chromium
2012/10/11 09:07:25
odd indentation. if it fits 80 chars, it should be
justinlin
2012/10/11 19:41:51
Done.
| |
61 media_stream::kMediaStreamSourceTab) { | |
62 *audio = content::MEDIA_TAB_AUDIO_CAPTURE; | |
63 *audio_device_id = GetMandatoryStreamConstraint( | |
64 user_media_request.audioConstraints(), | |
65 media_stream::kMediaStreamSourceId); | |
66 } | |
67 | |
68 if (*video != content::MEDIA_NO_SERVICE && | |
69 GetMandatoryStreamConstraint(user_media_request.videoConstraints(), | |
70 media_stream::kMediaStreamSource) == | |
no longer working on chromium
2012/10/11 09:07:25
ditto
justinlin
2012/10/11 19:41:51
Done.
| |
71 media_stream::kMediaStreamSourceTab) { | |
72 *video = content::MEDIA_TAB_VIDEO_CAPTURE; | |
73 *video_device_id = GetMandatoryStreamConstraint( | |
74 user_media_request.videoConstraints(), | |
75 media_stream::kMediaStreamSourceId); | |
76 } | |
77 | |
78 return *audio == content::MEDIA_TAB_VIDEO_CAPTURE || | |
79 *video == content::MEDIA_TAB_AUDIO_CAPTURE; | |
80 } | |
81 | |
37 } // namespace | 82 } // namespace |
38 | 83 |
39 static int g_next_request_id = 0; | 84 static int g_next_request_id = 0; |
40 | 85 |
41 // Creates a WebKit representation of a stream sources based on | 86 // Creates a WebKit representation of a stream sources based on |
42 // |devices| from the MediaStreamDispatcher. | 87 // |devices| from the MediaStreamDispatcher. |
43 static void CreateWebKitSourceVector( | 88 static void CreateWebKitSourceVector( |
44 const std::string& label, | 89 const std::string& label, |
45 const media_stream::StreamDeviceInfoArray& devices, | 90 const media_stream::StreamDeviceInfoArray& devices, |
46 WebKit::WebMediaStreamSource::Type type, | 91 WebKit::WebMediaStreamSource::Type type, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
80 } | 125 } |
81 | 126 |
82 void MediaStreamImpl::requestUserMedia( | 127 void MediaStreamImpl::requestUserMedia( |
83 const WebKit::WebUserMediaRequest& user_media_request, | 128 const WebKit::WebUserMediaRequest& user_media_request, |
84 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, | 129 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, |
85 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { | 130 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { |
86 // Save histogram data so we can see how much GetUserMedia is used. | 131 // Save histogram data so we can see how much GetUserMedia is used. |
87 // The histogram counts the number of calls to the JS API | 132 // The histogram counts the number of calls to the JS API |
88 // webGetUserMedia. | 133 // webGetUserMedia. |
89 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 134 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
135 | |
90 DCHECK(CalledOnValidThread()); | 136 DCHECK(CalledOnValidThread()); |
91 int request_id = g_next_request_id++; | 137 int request_id = g_next_request_id++; |
92 bool audio = false; | 138 content::MediaStreamDeviceType audio = content::MEDIA_NO_SERVICE; |
no longer working on chromium
2012/10/11 09:07:25
we should simply use a media_stream::StreamOptions
justinlin
2012/10/11 19:41:51
Done. Thanks, looks better.
| |
93 bool video = false; | 139 content::MediaStreamDeviceType video = content::MEDIA_NO_SERVICE; |
94 WebKit::WebFrame* frame = NULL; | 140 WebKit::WebFrame* frame = NULL; |
95 GURL security_origin; | 141 GURL security_origin; |
96 | 142 |
97 // |user_media_request| can't be mocked. So in order to test at all we check | 143 // |user_media_request| can't be mocked. So in order to test at all we check |
98 // if it isNull. | 144 // if it isNull. |
99 if (user_media_request.isNull()) { | 145 if (user_media_request.isNull()) { |
100 // We are in a test. | 146 // We are in a test. |
101 audio = audio_sources.size() > 0; | 147 if (audio_sources.size() > 0) |
102 video = video_sources.size() > 0; | 148 audio = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
149 if (video_sources.size() > 0) | |
150 video = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
103 } else { | 151 } else { |
104 audio = user_media_request.audio(); | 152 if (user_media_request.audio()) |
105 video = user_media_request.video(); | 153 audio = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
154 if (user_media_request.video()) | |
155 video = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
156 | |
106 security_origin = GURL(user_media_request.securityOrigin().toString()); | 157 security_origin = GURL(user_media_request.securityOrigin().toString()); |
107 // Get the WebFrame that requested a MediaStream. | 158 // Get the WebFrame that requested a MediaStream. |
108 // The frame is needed to tell the MediaStreamDispatcher when a stream goes | 159 // The frame is needed to tell the MediaStreamDispatcher when a stream goes |
109 // out of scope. | 160 // out of scope. |
110 frame = user_media_request.ownerDocument().frame(); | 161 frame = user_media_request.ownerDocument().frame(); |
111 DCHECK(frame); | 162 DCHECK(frame); |
112 } | 163 } |
113 | 164 |
165 std::string audio_device_id, video_device_id; | |
166 bool isTabMediaRequest = CheckAndUpdateAsTabMediaRequest(user_media_request, | |
perkj_chrome
2012/10/11 08:01:54
camel case is not used in chrome. change to is_tab
justinlin
2012/10/11 19:41:51
Done.
| |
167 &audio, &audio_device_id, &video, &video_device_id); | |
168 | |
169 if (isTabMediaRequest) { | |
170 const GURL& url = user_media_request.ownerDocument().url(); | |
171 if (!url.SchemeIs(kExtensionScheme)) { | |
172 LOG(ERROR) << "Tried to use tab capture outside extension API."; | |
173 return; | |
perkj_chrome
2012/10/11 08:01:54
I realized you can not just return here. You need
justinlin
2012/10/11 19:41:51
Done. Thanks, this is cleaner.
| |
174 } | |
175 } | |
176 | |
114 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | 177 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " |
115 << (audio ? "audio" : "") | 178 << (audio ? "audio" : "") |
116 << (user_media_request.video() ? " video" : "") << "], " | 179 << (video ? " video" : "") << "], " |
117 << security_origin.spec() << ")"; | 180 << security_origin.spec() << ")"; |
118 | 181 |
119 user_media_requests_[request_id] = | 182 user_media_requests_[request_id] = |
120 UserMediaRequestInfo(frame, user_media_request); | 183 UserMediaRequestInfo(frame, user_media_request); |
121 | 184 |
122 media_stream_dispatcher_->GenerateStream( | 185 media_stream_dispatcher_->GenerateStream( |
perkj_chrome
2012/10/11 08:01:54
nice
| |
123 request_id, | 186 request_id, |
124 AsWeakPtr(), | 187 AsWeakPtr(), |
125 media_stream::StreamOptions(audio, video), | 188 media_stream::StreamOptions( |
189 audio, audio_device_id, video, video_device_id), | |
126 security_origin); | 190 security_origin); |
127 } | 191 } |
128 | 192 |
129 void MediaStreamImpl::cancelUserMediaRequest( | 193 void MediaStreamImpl::cancelUserMediaRequest( |
130 const WebKit::WebUserMediaRequest& user_media_request) { | 194 const WebKit::WebUserMediaRequest& user_media_request) { |
131 DCHECK(CalledOnValidThread()); | 195 DCHECK(CalledOnValidThread()); |
132 MediaRequestMap::iterator it = user_media_requests_.begin(); | 196 MediaRequestMap::iterator it = user_media_requests_.begin(); |
133 for (; it != user_media_requests_.end(); ++it) { | 197 for (; it != user_media_requests_.end(); ++it) { |
134 if (it->second.request_ == user_media_request) | 198 if (it->second.request_ == user_media_request) |
135 break; | 199 break; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
456 | 520 |
457 void MediaStreamExtraData::SetLocalStreamStopCallback( | 521 void MediaStreamExtraData::SetLocalStreamStopCallback( |
458 const StreamStopCallback& stop_callback) { | 522 const StreamStopCallback& stop_callback) { |
459 stream_stop_callback_ = stop_callback; | 523 stream_stop_callback_ = stop_callback; |
460 } | 524 } |
461 | 525 |
462 void MediaStreamExtraData::OnLocalStreamStop() { | 526 void MediaStreamExtraData::OnLocalStreamStop() { |
463 if (!stream_stop_callback_.is_null()) | 527 if (!stream_stop_callback_.is_null()) |
464 stream_stop_callback_.Run(local_stream_->label()); | 528 stream_stop_callback_.Run(local_stream_->label()); |
465 } | 529 } |
OLD | NEW |