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 std::string(); | |
46 | |
47 WebKit::WebString value; | |
48 constraints.getMandatoryConstraintValue(UTF8ToUTF16(key), value); | |
49 return UTF16ToUTF8(value); | |
50 } | |
51 | |
52 void UpdateOptionsIfTabMediaRequest( | |
53 const WebKit::WebUserMediaRequest& user_media_request, | |
54 media_stream::StreamOptions* options) { | |
55 if (options->audio_type != content::MEDIA_NO_SERVICE && | |
56 GetMandatoryStreamConstraint(user_media_request.audioConstraints(), | |
57 media_stream::kMediaStreamSource) == | |
58 media_stream::kMediaStreamSourceTab) { | |
miu
2012/10/12 01:10:50
nit: Indentation. Consider putting this 4 chars t
justinlin
2012/10/12 03:02:31
Done.
| |
59 options->audio_type = content::MEDIA_TAB_AUDIO_CAPTURE; | |
60 options->audio_device_id = GetMandatoryStreamConstraint( | |
61 user_media_request.audioConstraints(), | |
62 media_stream::kMediaStreamSourceId); | |
63 } | |
64 | |
65 if (options->video_type != content::MEDIA_NO_SERVICE && | |
66 GetMandatoryStreamConstraint(user_media_request.videoConstraints(), | |
67 media_stream::kMediaStreamSource) == | |
68 media_stream::kMediaStreamSourceTab) { | |
miu
2012/10/12 01:10:50
nit: ditto on indentation.
justinlin
2012/10/12 03:02:31
Done.
| |
69 options->video_type = content::MEDIA_TAB_VIDEO_CAPTURE; | |
70 options->video_device_id = GetMandatoryStreamConstraint( | |
71 user_media_request.videoConstraints(), | |
72 media_stream::kMediaStreamSourceId); | |
73 } | |
74 } | |
75 | |
37 } // namespace | 76 } // namespace |
38 | 77 |
39 static int g_next_request_id = 0; | 78 static int g_next_request_id = 0; |
40 | 79 |
41 // Creates a WebKit representation of a stream sources based on | 80 // Creates a WebKit representation of a stream sources based on |
42 // |devices| from the MediaStreamDispatcher. | 81 // |devices| from the MediaStreamDispatcher. |
43 static void CreateWebKitSourceVector( | 82 static void CreateWebKitSourceVector( |
44 const std::string& label, | 83 const std::string& label, |
45 const media_stream::StreamDeviceInfoArray& devices, | 84 const media_stream::StreamDeviceInfoArray& devices, |
46 WebKit::WebMediaStreamSource::Type type, | 85 WebKit::WebMediaStreamSource::Type type, |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
82 void MediaStreamImpl::requestUserMedia( | 121 void MediaStreamImpl::requestUserMedia( |
83 const WebKit::WebUserMediaRequest& user_media_request, | 122 const WebKit::WebUserMediaRequest& user_media_request, |
84 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, | 123 const WebKit::WebVector<WebKit::WebMediaStreamSource>& audio_sources, |
85 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { | 124 const WebKit::WebVector<WebKit::WebMediaStreamSource>& video_sources) { |
86 // Save histogram data so we can see how much GetUserMedia is used. | 125 // Save histogram data so we can see how much GetUserMedia is used. |
87 // The histogram counts the number of calls to the JS API | 126 // The histogram counts the number of calls to the JS API |
88 // webGetUserMedia. | 127 // webGetUserMedia. |
89 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); | 128 UpdateWebRTCMethodCount(WEBKIT_GET_USER_MEDIA); |
90 DCHECK(CalledOnValidThread()); | 129 DCHECK(CalledOnValidThread()); |
91 int request_id = g_next_request_id++; | 130 int request_id = g_next_request_id++; |
92 bool audio = false; | 131 media_stream::StreamOptions options(content::MEDIA_NO_SERVICE, |
93 bool video = false; | 132 content::MEDIA_NO_SERVICE); |
94 WebKit::WebFrame* frame = NULL; | 133 WebKit::WebFrame* frame = NULL; |
95 GURL security_origin; | 134 GURL security_origin; |
96 | 135 |
97 // |user_media_request| can't be mocked. So in order to test at all we check | 136 // |user_media_request| can't be mocked. So in order to test at all we check |
98 // if it isNull. | 137 // if it isNull. |
99 if (user_media_request.isNull()) { | 138 if (user_media_request.isNull()) { |
100 // We are in a test. | 139 // We are in a test. |
101 audio = audio_sources.size() > 0; | 140 if (audio_sources.size() > 0) |
102 video = video_sources.size() > 0; | 141 options.audio_type = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
142 if (video_sources.size() > 0) | |
143 options.video_type = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
103 } else { | 144 } else { |
104 audio = user_media_request.audio(); | 145 if (user_media_request.audio()) |
105 video = user_media_request.video(); | 146 options.audio_type = content::MEDIA_DEVICE_AUDIO_CAPTURE; |
147 if (user_media_request.video()) | |
148 options.video_type = content::MEDIA_DEVICE_VIDEO_CAPTURE; | |
149 | |
106 security_origin = GURL(user_media_request.securityOrigin().toString()); | 150 security_origin = GURL(user_media_request.securityOrigin().toString()); |
107 // Get the WebFrame that requested a MediaStream. | 151 // Get the WebFrame that requested a MediaStream. |
108 // The frame is needed to tell the MediaStreamDispatcher when a stream goes | 152 // The frame is needed to tell the MediaStreamDispatcher when a stream goes |
109 // out of scope. | 153 // out of scope. |
110 frame = user_media_request.ownerDocument().frame(); | 154 frame = user_media_request.ownerDocument().frame(); |
111 DCHECK(frame); | 155 DCHECK(frame); |
112 } | 156 } |
113 | 157 |
158 UpdateOptionsIfTabMediaRequest(user_media_request, &options); | |
wjia(left Chromium)
2012/10/11 22:13:27
Do we need any check after this function returns?
justinlin
2012/10/12 00:50:22
Good observation! Right now, this can't really hap
| |
159 | |
114 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " | 160 DVLOG(1) << "MediaStreamImpl::generateStream(" << request_id << ", [ " |
115 << (audio ? "audio" : "") | 161 << (options.audio_type ? "audio" : "") |
miu
2012/10/12 01:10:50
nit: Consider replacing these with the following s
justinlin
2012/10/12 03:02:31
Done. Thanks.
| |
116 << (user_media_request.video() ? " video" : "") << "], " | 162 << (options.video_type ? " video" : "") << "], " |
117 << security_origin.spec() << ")"; | 163 << security_origin.spec() << ")"; |
118 | 164 |
119 user_media_requests_[request_id] = | 165 user_media_requests_[request_id] = |
120 UserMediaRequestInfo(frame, user_media_request); | 166 UserMediaRequestInfo(frame, user_media_request); |
121 | 167 |
122 media_stream_dispatcher_->GenerateStream( | 168 media_stream_dispatcher_->GenerateStream( |
123 request_id, | 169 request_id, |
124 AsWeakPtr(), | 170 AsWeakPtr(), |
125 media_stream::StreamOptions(audio, video), | 171 options, |
126 security_origin); | 172 security_origin); |
127 } | 173 } |
128 | 174 |
129 void MediaStreamImpl::cancelUserMediaRequest( | 175 void MediaStreamImpl::cancelUserMediaRequest( |
130 const WebKit::WebUserMediaRequest& user_media_request) { | 176 const WebKit::WebUserMediaRequest& user_media_request) { |
131 DCHECK(CalledOnValidThread()); | 177 DCHECK(CalledOnValidThread()); |
132 MediaRequestMap::iterator it = user_media_requests_.begin(); | 178 MediaRequestMap::iterator it = user_media_requests_.begin(); |
133 for (; it != user_media_requests_.end(); ++it) { | 179 for (; it != user_media_requests_.end(); ++it) { |
134 if (it->second.request_ == user_media_request) | 180 if (it->second.request_ == user_media_request) |
135 break; | 181 break; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
461 | 507 |
462 void MediaStreamExtraData::SetLocalStreamStopCallback( | 508 void MediaStreamExtraData::SetLocalStreamStopCallback( |
463 const StreamStopCallback& stop_callback) { | 509 const StreamStopCallback& stop_callback) { |
464 stream_stop_callback_ = stop_callback; | 510 stream_stop_callback_ = stop_callback; |
465 } | 511 } |
466 | 512 |
467 void MediaStreamExtraData::OnLocalStreamStop() { | 513 void MediaStreamExtraData::OnLocalStreamStop() { |
468 if (!stream_stop_callback_.is_null()) | 514 if (!stream_stop_callback_.is_null()) |
469 stream_stop_callback_.Run(local_stream_->label()); | 515 stream_stop_callback_.Run(local_stream_->label()); |
470 } | 516 } |
OLD | NEW |