Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / renderer / media / rtc_media_constraints.cc
blob7f8b79dc136dadb26c3150abbe83f422a1c52559
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
3 // found in the LICENSE file.
4 #include "content/renderer/media/rtc_media_constraints.h"
6 #include <string>
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "content/common/media/media_stream_options.h"
11 #include "content/renderer/media/media_stream_video_source.h"
12 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
13 #include "third_party/WebKit/public/platform/WebCString.h"
14 #include "third_party/WebKit/public/platform/WebString.h"
16 namespace content {
17 namespace {
19 void GetNativeMediaConstraints(
20 const blink::WebVector<blink::WebMediaConstraint>& constraints,
21 webrtc::MediaConstraintsInterface::Constraints* native_constraints) {
22 DCHECK(native_constraints);
23 for (size_t i = 0; i < constraints.size(); ++i) {
24 webrtc::MediaConstraintsInterface::Constraint new_constraint;
25 new_constraint.key = constraints[i].m_name.utf8();
26 new_constraint.value = constraints[i].m_value.utf8();
28 // Ignore Chrome specific Tab capture constraints.
29 if (new_constraint.key == kMediaStreamSource ||
30 new_constraint.key == kMediaStreamSourceId)
31 continue;
33 // Ignore sourceId constraint since that has nothing to do with webrtc.
34 if (new_constraint.key == kMediaStreamSourceInfoId)
35 continue;
37 // Ignore constraints that are handled by Chrome in MediaStreamVideoSource.
38 if (MediaStreamVideoSource::IsConstraintSupported(new_constraint.key))
39 continue;
41 DVLOG(3) << "MediaStreamConstraints:" << new_constraint.key
42 << " : " << new_constraint.value;
43 native_constraints->push_back(new_constraint);
47 } // namespace
49 RTCMediaConstraints::RTCMediaConstraints() {}
51 RTCMediaConstraints::RTCMediaConstraints(
52 const blink::WebMediaConstraints& constraints) {
53 if (constraints.isNull())
54 return; // Will happen in unit tests.
55 blink::WebVector<blink::WebMediaConstraint> mandatory;
56 constraints.getMandatoryConstraints(mandatory);
57 GetNativeMediaConstraints(mandatory, &mandatory_);
58 blink::WebVector<blink::WebMediaConstraint> optional;
59 constraints.getOptionalConstraints(optional);
60 GetNativeMediaConstraints(optional, &optional_);
63 RTCMediaConstraints::~RTCMediaConstraints() {}
65 const webrtc::MediaConstraintsInterface::Constraints&
66 RTCMediaConstraints::GetMandatory() const {
67 return mandatory_;
70 const webrtc::MediaConstraintsInterface::Constraints&
71 RTCMediaConstraints::GetOptional() const {
72 return optional_;
75 bool RTCMediaConstraints::AddOptional(const std::string& key,
76 const std::string& value,
77 bool override_if_exists) {
78 return AddConstraint(&optional_, key, value, override_if_exists);
81 bool RTCMediaConstraints::AddMandatory(const std::string& key,
82 const std::string& value,
83 bool override_if_exists) {
84 return AddConstraint(&mandatory_, key, value, override_if_exists);
87 bool RTCMediaConstraints::AddConstraint(Constraints* constraints,
88 const std::string& key,
89 const std::string& value,
90 bool override_if_exists) {
91 for (Constraints::iterator iter = constraints->begin();
92 iter != constraints->end();
93 ++iter) {
94 if (iter->key == key) {
95 if (override_if_exists)
96 iter->value = value;
97 return override_if_exists;
100 // The key wasn't found, add it.
101 constraints->push_back(Constraint(key, value));
102 return true;
105 } // namespace content