2a81b660ff6d16e0ca17e3a4dccbb17853311fad
[platform/framework/web/crosswalk.git] / src / content / renderer / media / rtc_media_constraints.cc
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"
5
6 #include <string>
7
8 #include "base/logging.h"
9 #include "base/strings/string_util.h"
10 #include "content/common/media/media_stream_options.h"
11 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
12 #include "third_party/WebKit/public/platform/WebCString.h"
13 #include "third_party/WebKit/public/platform/WebString.h"
14
15 namespace content {
16 namespace {
17
18 void GetNativeMediaConstraints(
19     const blink::WebVector<blink::WebMediaConstraint>& constraints,
20     webrtc::MediaConstraintsInterface::Constraints* native_constraints) {
21   DCHECK(native_constraints);
22   for (size_t i = 0; i < constraints.size(); ++i) {
23     webrtc::MediaConstraintsInterface::Constraint new_constraint;
24     new_constraint.key = constraints[i].m_name.utf8();
25     new_constraint.value = constraints[i].m_value.utf8();
26
27     // Ignore Chrome specific Tab capture constraints.
28     if (new_constraint.key == kMediaStreamSource ||
29         new_constraint.key == kMediaStreamSourceId)
30       continue;
31
32     // Ignore sourceId constraint since that has nothing to do with webrtc.
33     if (new_constraint.key == kMediaStreamSourceInfoId)
34       continue;
35
36     DVLOG(3) << "MediaStreamConstraints:" << new_constraint.key
37              << " : " <<  new_constraint.value;
38     native_constraints->push_back(new_constraint);
39   }
40 }
41
42 }  // namespace
43
44 RTCMediaConstraints::RTCMediaConstraints() {}
45
46 RTCMediaConstraints::RTCMediaConstraints(
47       const blink::WebMediaConstraints& constraints) {
48   if (constraints.isNull())
49     return;  // Will happen in unit tests.
50   blink::WebVector<blink::WebMediaConstraint> mandatory;
51   constraints.getMandatoryConstraints(mandatory);
52   GetNativeMediaConstraints(mandatory, &mandatory_);
53   blink::WebVector<blink::WebMediaConstraint> optional;
54   constraints.getOptionalConstraints(optional);
55   GetNativeMediaConstraints(optional, &optional_);
56 }
57
58 RTCMediaConstraints::~RTCMediaConstraints() {}
59
60 const webrtc::MediaConstraintsInterface::Constraints&
61 RTCMediaConstraints::GetMandatory() const  {
62   return mandatory_;
63 }
64
65 const webrtc::MediaConstraintsInterface::Constraints&
66 RTCMediaConstraints::GetOptional() const {
67   return optional_;
68 }
69
70 void RTCMediaConstraints::AddOptional(const std::string& key,
71                                       const std::string& value) {
72   optional_.push_back(Constraint(key, value));
73 }
74
75 bool RTCMediaConstraints::AddMandatory(const std::string& key,
76                                        const std::string& value,
77                                        bool override_if_exists) {
78   for (Constraints::iterator iter = mandatory_.begin();
79        iter != mandatory_.end();
80        ++iter) {
81     if (iter->key == key) {
82       if (override_if_exists)
83         iter->value = value;
84       return override_if_exists;
85     }
86   }
87   // The key wasn't found, add it.
88   mandatory_.push_back(Constraint(key, value));
89   return true;
90 }
91
92 }  // namespace content