OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_video_source.h" | 5 #include "content/renderer/media/media_stream_video_source.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "content/renderer/media/media_stream_constraints_util.h" |
13 #include "content/renderer/media/media_stream_dependency_factory.h" | 14 #include "content/renderer/media/media_stream_dependency_factory.h" |
14 #include "content/renderer/media/media_stream_video_track.h" | 15 #include "content/renderer/media/media_stream_video_track.h" |
15 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" | 16 #include "content/renderer/media/webrtc/webrtc_video_capturer_adapter.h" |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 // Constraint keys. Specified by draft-alvestrand-constraints-resolution-00b | 20 // Constraint keys. Specified by draft-alvestrand-constraints-resolution-00b |
20 const char MediaStreamVideoSource::kMinAspectRatio[] = "minAspectRatio"; | 21 const char MediaStreamVideoSource::kMinAspectRatio[] = "minAspectRatio"; |
21 const char MediaStreamVideoSource::kMaxAspectRatio[] = "maxAspectRatio"; | 22 const char MediaStreamVideoSource::kMaxAspectRatio[] = "maxAspectRatio"; |
22 const char MediaStreamVideoSource::kMaxWidth[] = "maxWidth"; | 23 const char MediaStreamVideoSource::kMaxWidth[] = "maxWidth"; |
(...skipping 12 matching lines...) Expand all Loading... |
35 MediaStreamVideoSource::kMinHeight, | 36 MediaStreamVideoSource::kMinHeight, |
36 MediaStreamVideoSource::kMaxFrameRate, | 37 MediaStreamVideoSource::kMaxFrameRate, |
37 MediaStreamVideoSource::kMinFrameRate, | 38 MediaStreamVideoSource::kMinFrameRate, |
38 }; | 39 }; |
39 | 40 |
40 const int MediaStreamVideoSource::kDefaultWidth = 640; | 41 const int MediaStreamVideoSource::kDefaultWidth = 640; |
41 const int MediaStreamVideoSource::kDefaultHeight = 480; | 42 const int MediaStreamVideoSource::kDefaultHeight = 480; |
42 const int MediaStreamVideoSource::kDefaultFrameRate = 30; | 43 const int MediaStreamVideoSource::kDefaultFrameRate = 30; |
43 | 44 |
44 namespace { | 45 namespace { |
45 // Constraints keys for http://dev.w3.org/2011/webrtc/editor/getusermedia.html | |
46 const char kSourceId[] = "sourceId"; | |
47 | 46 |
48 // Google-specific key prefix. Constraints with this prefix are ignored if they | 47 // Google-specific key prefix. Constraints with this prefix are ignored if they |
49 // are unknown. | 48 // are unknown. |
50 const char kGooglePrefix[] = "goog"; | 49 const char kGooglePrefix[] = "goog"; |
51 | 50 |
52 // MediaStreamVideoSource supports cropping of video frames but only up to | 51 // MediaStreamVideoSource supports cropping of video frames but only up to |
53 // kMaxCropFactor. Ie - if a constraint is set to maxHeight 360, an original | 52 // kMaxCropFactor. Ie - if a constraint is set to maxHeight 360, an original |
54 // input frame height of max 360 * kMaxCropFactor pixels is accepted. | 53 // input frame height of max 360 * kMaxCropFactor pixels is accepted. |
55 const int kMaxCropFactor = 2; | 54 const int kMaxCropFactor = 2; |
56 | 55 |
(...skipping 11 matching lines...) Expand all Loading... |
68 | 67 |
69 std::string constraint_name = constraint.m_name.utf8(); | 68 std::string constraint_name = constraint.m_name.utf8(); |
70 std::string constraint_value = constraint.m_value.utf8(); | 69 std::string constraint_value = constraint.m_value.utf8(); |
71 | 70 |
72 if (constraint_name.find(kGooglePrefix) == 0) { | 71 if (constraint_name.find(kGooglePrefix) == 0) { |
73 // These are actually options, not constraints, so they can be satisfied | 72 // These are actually options, not constraints, so they can be satisfied |
74 // regardless of the format. | 73 // regardless of the format. |
75 return true; | 74 return true; |
76 } | 75 } |
77 | 76 |
78 if (constraint_name == kSourceId) { | 77 if (constraint_name == MediaStreamSource::kSourceId) { |
79 // This is a constraint that doesn't affect the format. | 78 // This is a constraint that doesn't affect the format. |
80 return true; | 79 return true; |
81 } | 80 } |
82 | 81 |
83 // Ignore Chrome specific Tab capture constraints. | 82 // Ignore Chrome specific Tab capture constraints. |
84 if (constraint_name == kMediaStreamSource || | 83 if (constraint_name == kMediaStreamSource || |
85 constraint_name == kMediaStreamSourceId) | 84 constraint_name == kMediaStreamSourceId) |
86 return true; | 85 return true; |
87 | 86 |
88 if (constraint_name == MediaStreamVideoSource::kMinAspectRatio || | 87 if (constraint_name == MediaStreamVideoSource::kMinAspectRatio || |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 FilterFormatsByConstraint(optional[i], false, ¤t_candidates); | 195 FilterFormatsByConstraint(optional[i], false, ¤t_candidates); |
197 if (!current_candidates.empty()) { | 196 if (!current_candidates.empty()) { |
198 candidates = current_candidates; | 197 candidates = current_candidates; |
199 } | 198 } |
200 } | 199 } |
201 | 200 |
202 // We have done as good as we can to filter the supported resolutions. | 201 // We have done as good as we can to filter the supported resolutions. |
203 return candidates; | 202 return candidates; |
204 } | 203 } |
205 | 204 |
206 bool GetConstraintValue(const blink::WebMediaConstraints& constraints, | |
207 bool mandatory, const blink::WebString& name, | |
208 int* value) { | |
209 blink::WebString value_str; | |
210 bool ret = mandatory ? | |
211 constraints.getMandatoryConstraintValue(name, value_str) : | |
212 constraints.getOptionalConstraintValue(name, value_str); | |
213 if (ret) | |
214 base::StringToInt(value_str.utf8(), value); | |
215 return ret; | |
216 } | |
217 | |
218 // Retrieve the desired max width and height from |constraints|. | 205 // Retrieve the desired max width and height from |constraints|. |
219 void GetDesiredMaxWidthAndHeight(const blink::WebMediaConstraints& constraints, | 206 void GetDesiredMaxWidthAndHeight(const blink::WebMediaConstraints& constraints, |
220 int* desired_width, int* desired_height) { | 207 int* desired_width, int* desired_height) { |
221 bool mandatory = GetConstraintValue(constraints, true, | 208 bool mandatory = GetMandatoryConstraintValueAsInteger( |
222 MediaStreamVideoSource::kMaxWidth, | 209 constraints, MediaStreamVideoSource::kMaxWidth, desired_width); |
223 desired_width); | 210 mandatory |= GetMandatoryConstraintValueAsInteger( |
224 mandatory |= GetConstraintValue(constraints, true, | 211 constraints, MediaStreamVideoSource::kMaxHeight, desired_height); |
225 MediaStreamVideoSource::kMaxHeight, | 212 // Skip the optional constraints if any of the mandatory constraint is |
226 desired_height); | 213 // specified. |
227 if (mandatory) | 214 if (mandatory) |
228 return; | 215 return; |
229 | 216 |
230 GetConstraintValue(constraints, false, MediaStreamVideoSource::kMaxWidth, | 217 GetOptionalConstraintValueAsInteger(constraints, |
231 desired_width); | 218 MediaStreamVideoSource::kMaxWidth, |
232 GetConstraintValue(constraints, false, MediaStreamVideoSource::kMaxHeight, | 219 desired_width); |
233 desired_height); | 220 GetOptionalConstraintValueAsInteger(constraints, |
| 221 MediaStreamVideoSource::kMaxHeight, |
| 222 desired_height); |
234 } | 223 } |
235 | 224 |
236 const media::VideoCaptureFormat& GetBestFormatBasedOnArea( | 225 const media::VideoCaptureFormat& GetBestFormatBasedOnArea( |
237 const media::VideoCaptureFormats& formats, | 226 const media::VideoCaptureFormats& formats, |
238 int area) { | 227 int area) { |
239 media::VideoCaptureFormats::const_iterator it = formats.begin(); | 228 media::VideoCaptureFormats::const_iterator it = formats.begin(); |
240 media::VideoCaptureFormats::const_iterator best_it = formats.begin(); | 229 media::VideoCaptureFormats::const_iterator best_it = formats.begin(); |
241 int best_diff = std::numeric_limits<int>::max(); | 230 int best_diff = std::numeric_limits<int>::max(); |
242 for (; it != formats.end(); ++it) { | 231 for (; it != formats.end(); ++it) { |
243 int diff = abs(area - it->frame_size.width() * it->frame_size.height()); | 232 int diff = abs(area - it->frame_size.width() * it->frame_size.height()); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 tracks_.push_back(track); | 306 tracks_.push_back(track); |
318 | 307 |
319 requested_constraints_.push_back( | 308 requested_constraints_.push_back( |
320 RequestedConstraints(constraints, callback)); | 309 RequestedConstraints(constraints, callback)); |
321 | 310 |
322 switch (state_) { | 311 switch (state_) { |
323 case NEW: { | 312 case NEW: { |
324 // Tab capture and Screen capture needs the maximum requested height | 313 // Tab capture and Screen capture needs the maximum requested height |
325 // and width to decide on the resolution. | 314 // and width to decide on the resolution. |
326 int max_requested_width = 0; | 315 int max_requested_width = 0; |
327 GetConstraintValue(constraints, true, kMaxWidth, &max_requested_width); | 316 GetMandatoryConstraintValueAsInteger(constraints, kMaxWidth, |
| 317 &max_requested_width); |
328 | 318 |
329 int max_requested_height = 0; | 319 int max_requested_height = 0; |
330 GetConstraintValue(constraints, true, kMaxHeight, &max_requested_height); | 320 GetMandatoryConstraintValueAsInteger(constraints, kMaxHeight, |
| 321 &max_requested_height); |
331 | 322 |
332 state_ = RETRIEVING_CAPABILITIES; | 323 state_ = RETRIEVING_CAPABILITIES; |
333 GetCurrentSupportedFormats(max_requested_width, | 324 GetCurrentSupportedFormats(max_requested_width, |
334 max_requested_height); | 325 max_requested_height); |
335 | 326 |
336 break; | 327 break; |
337 } | 328 } |
338 case STARTING: | 329 case STARTING: |
339 case RETRIEVING_CAPABILITIES: { | 330 case RETRIEVING_CAPABILITIES: { |
340 // The |callback| will be triggered once the source has started or | 331 // The |callback| will be triggered once the source has started or |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
502 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( | 493 MediaStreamVideoSource::RequestedConstraints::RequestedConstraints( |
503 const blink::WebMediaConstraints& constraints, | 494 const blink::WebMediaConstraints& constraints, |
504 const ConstraintsCallback& callback) | 495 const ConstraintsCallback& callback) |
505 : constraints(constraints), callback(callback) { | 496 : constraints(constraints), callback(callback) { |
506 } | 497 } |
507 | 498 |
508 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { | 499 MediaStreamVideoSource::RequestedConstraints::~RequestedConstraints() { |
509 } | 500 } |
510 | 501 |
511 } // namespace content | 502 } // namespace content |
OLD | NEW |