Home | History | Annotate | Download | only in media
      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
      3 // found in the LICENSE file.
      4 
      5 #include "content/renderer/media/media_stream_constraints_util.h"
      6 
      7 #include "base/strings/string_number_conversions.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "third_party/WebKit/public/platform/WebMediaConstraints.h"
     10 #include "third_party/WebKit/public/platform/WebString.h"
     11 
     12 namespace content {
     13 
     14 namespace {
     15 
     16 // Convert a string ("true", "false") to a boolean.
     17 bool ConvertStringToBoolean(const std::string& string, bool* value) {
     18   static const char kValueTrue[] = "true";
     19   static const char kValueFalse[] = "false";
     20 
     21   *value = (string == kValueTrue);
     22   return *value || (string == kValueFalse);
     23 }
     24 
     25 }  // namespace
     26 
     27 bool GetConstraintValueAsBoolean(const blink::WebMediaConstraints& constraints,
     28                                  const std::string& name,
     29                                  bool* value) {
     30   return GetMandatoryConstraintValueAsBoolean(constraints, name, value) ||
     31          GetOptionalConstraintValueAsBoolean(constraints, name, value);
     32 }
     33 
     34 bool GetConstraintValueAsInteger(const blink::WebMediaConstraints& constraints,
     35                                  const std::string& name,
     36                                  int* value) {
     37   return GetMandatoryConstraintValueAsInteger(constraints, name, value) ||
     38          GetOptionalConstraintValueAsInteger(constraints, name, value);
     39 }
     40 
     41 bool GetConstraintValueAsString(const blink::WebMediaConstraints& constraints,
     42                                 const std::string& name,
     43                                 std::string* value) {
     44   blink::WebString value_str;
     45   base::string16 name_16 = base::UTF8ToUTF16(name);
     46   if (!constraints.getMandatoryConstraintValue(name_16, value_str) &&
     47       !constraints.getOptionalConstraintValue(name_16, value_str)) {
     48     return false;
     49   }
     50 
     51   *value = value_str.utf8();
     52   return true;
     53 }
     54 
     55 bool GetMandatoryConstraintValueAsBoolean(
     56     const blink::WebMediaConstraints& constraints,
     57     const std::string& name,
     58     bool* value) {
     59   blink::WebString value_str;
     60   if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
     61                                                value_str)) {
     62     return false;
     63   }
     64 
     65   return ConvertStringToBoolean(value_str.utf8(), value);
     66 }
     67 
     68 bool GetMandatoryConstraintValueAsInteger(
     69     const blink::WebMediaConstraints& constraints,
     70     const std::string& name,
     71     int* value) {
     72   blink::WebString value_str;
     73   if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
     74                                                value_str)) {
     75     return false;
     76   }
     77 
     78   return base::StringToInt(value_str.utf8(), value);
     79 }
     80 
     81 bool GetMandatoryConstraintValueAsDouble(
     82     const blink::WebMediaConstraints& constraints,
     83     const std::string& name,
     84     double* value) {
     85   blink::WebString value_str;
     86   if (!constraints.getMandatoryConstraintValue(base::UTF8ToUTF16(name),
     87                                                value_str)) {
     88     return false;
     89   }
     90   return base::StringToDouble(value_str.utf8(), value);
     91 }
     92 
     93 bool GetOptionalConstraintValueAsBoolean(
     94     const blink::WebMediaConstraints& constraints,
     95     const std::string& name,
     96     bool* value) {
     97   blink::WebString value_str;
     98   if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
     99                                               value_str)) {
    100     return false;
    101   }
    102 
    103   return ConvertStringToBoolean(value_str.utf8(), value);
    104 }
    105 
    106 bool GetOptionalConstraintValueAsInteger(
    107     const blink::WebMediaConstraints& constraints,
    108     const std::string& name,
    109     int* value) {
    110   blink::WebString value_str;
    111   if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
    112                                               value_str)) {
    113     return false;
    114   }
    115 
    116   return base::StringToInt(value_str.utf8(), value);
    117 }
    118 
    119 bool GetOptionalConstraintValueAsDouble(
    120     const blink::WebMediaConstraints& constraints,
    121     const std::string& name,
    122     double* value) {
    123   blink::WebString value_str;
    124   if (!constraints.getOptionalConstraintValue(base::UTF8ToUTF16(name),
    125                                               value_str)) {
    126     return false;
    127   }
    128 
    129   return base::StringToDouble(value_str.utf8(), value);
    130 }
    131 
    132 }  // namespace content
    133