Home | History | Annotate | Download | only in audio
      1 // Copyright (c) 2013 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 "chromeos/audio/audio_device.h"
      6 
      7 #include "base/format_macros.h"
      8 #include "base/strings/string_number_conversions.h"
      9 #include "base/strings/stringprintf.h"
     10 #include "base/strings/utf_string_conversions.h"
     11 
     12 namespace {
     13 
     14 // Get the priority for a particular device type. The priority returned
     15 // will be between 0 to 3, the higher number meaning a higher priority.
     16 uint8 GetDevicePriority(chromeos::AudioDeviceType type) {
     17   switch (type) {
     18     // Fall through.
     19     case chromeos::AUDIO_TYPE_HEADPHONE:
     20     case chromeos::AUDIO_TYPE_MIC:
     21     case chromeos::AUDIO_TYPE_USB:
     22     case chromeos::AUDIO_TYPE_BLUETOOTH:
     23       return 3;
     24     case chromeos::AUDIO_TYPE_HDMI:
     25       return 2;
     26       // Fall through.
     27     case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER:
     28     case chromeos::AUDIO_TYPE_INTERNAL_MIC:
     29       return 1;
     30       // Fall through.
     31     case chromeos::AUDIO_TYPE_OTHER:
     32     default:
     33       return 0;
     34   }
     35 }
     36 
     37 std::string GetTypeString(chromeos::AudioDeviceType type) {
     38   switch (type) {
     39     case chromeos::AUDIO_TYPE_HEADPHONE:
     40       return "HEADPHONE";
     41     case chromeos::AUDIO_TYPE_MIC:
     42       return "MIC";
     43     case chromeos::AUDIO_TYPE_USB:
     44       return "USB";
     45     case chromeos::AUDIO_TYPE_BLUETOOTH:
     46       return "BLUETOOTH";
     47     case chromeos::AUDIO_TYPE_HDMI:
     48       return "HDMI";
     49     case chromeos::AUDIO_TYPE_INTERNAL_SPEAKER:
     50       return "INTERNAL_SPEAKER";
     51     case chromeos::AUDIO_TYPE_INTERNAL_MIC:
     52       return "INTERNAL_MIC";
     53     case chromeos::AUDIO_TYPE_OTHER:
     54     default:
     55       return "OTHER";
     56   }
     57 }
     58 
     59 chromeos::AudioDeviceType GetAudioType(const std::string& node_type) {
     60   if (node_type.find("HEADPHONE") != std::string::npos)
     61     return chromeos::AUDIO_TYPE_HEADPHONE;
     62   else if (node_type.find("INTERNAL_MIC") != std::string::npos)
     63     return chromeos::AUDIO_TYPE_INTERNAL_MIC;
     64   else if (node_type.find("MIC") != std::string::npos)
     65     return chromeos::AUDIO_TYPE_MIC;
     66   else if (node_type.find("USB") != std::string::npos)
     67     return chromeos::AUDIO_TYPE_USB;
     68   else if (node_type.find("BLUETOOTH") != std::string::npos)
     69     return chromeos::AUDIO_TYPE_BLUETOOTH;
     70   else if (node_type.find("HDMI") != std::string::npos)
     71     return chromeos::AUDIO_TYPE_HDMI;
     72   else if (node_type.find("INTERNAL_SPEAKER") != std::string::npos)
     73     return chromeos::AUDIO_TYPE_INTERNAL_SPEAKER;
     74   else
     75     return chromeos::AUDIO_TYPE_OTHER;
     76 }
     77 
     78 }  // namespace
     79 
     80 namespace chromeos {
     81 
     82 AudioDevice::AudioDevice()
     83     : is_input(false),
     84       id(0),
     85       display_name(""),
     86       type(AUDIO_TYPE_OTHER),
     87       priority(0),
     88       active(false),
     89       plugged_time(0) {
     90 }
     91 
     92 AudioDevice::AudioDevice(const AudioNode& node) {
     93   is_input = node.is_input;
     94   id = node.id;
     95   type = GetAudioType(node.type);
     96   if (!node.name.empty() && node.name != "(default)")
     97     display_name = node.name;
     98   else
     99     display_name = node.device_name;
    100   device_name = node.device_name;
    101   priority = GetDevicePriority(type);
    102   active = node.active;
    103   plugged_time = node.plugged_time;
    104 }
    105 
    106 std::string AudioDevice::ToString() const {
    107   std::string result;
    108   base::StringAppendF(&result,
    109                       "is_input = %s ",
    110                       is_input ? "true" : "false");
    111   base::StringAppendF(&result,
    112                       "id = 0x%" PRIx64 " ",
    113                       id);
    114   base::StringAppendF(&result,
    115                       "display_name = %s ",
    116                       display_name.c_str());
    117   base::StringAppendF(&result,
    118                       "device_name = %s ",
    119                       device_name.c_str());
    120   base::StringAppendF(&result,
    121                       "type = %s ",
    122                       GetTypeString(type).c_str());
    123   base::StringAppendF(&result,
    124                       "active = %s ",
    125                       active ? "true" : "false");
    126   base::StringAppendF(&result,
    127                       "plugged_time= %s ",
    128                       base::Uint64ToString(plugged_time).c_str());
    129 
    130   return result;
    131 }
    132 
    133 }  // namespace chromeos
    134