summaryrefslogtreecommitdiffstats
path: root/extensions/shell/browser/shell_audio_controller_chromeos.cc
blob: 1e89065861b27b7ecaa7c86afd088b541466fa77 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/shell/browser/shell_audio_controller_chromeos.h"

#include <algorithm>

#include "chromeos/audio/audio_device.h"

namespace extensions {

namespace {

// Returns a pointer to the device in |devices| with ID |node_id|, or NULL if it
// isn't present.
const chromeos::AudioDevice* GetDevice(const chromeos::AudioDeviceList& devices,
                                       uint64_t node_id) {
  for (chromeos::AudioDeviceList::const_iterator it = devices.begin();
       it != devices.end(); ++it) {
    if (it->id == node_id)
      return &(*it);
  }
  return NULL;
}

}  // namespace

ShellAudioController::ShellAudioController() {
  chromeos::CrasAudioHandler::Get()->AddAudioObserver(this);
  ActivateDevices();
}

ShellAudioController::~ShellAudioController() {
  chromeos::CrasAudioHandler::Get()->RemoveAudioObserver(this);
}

void ShellAudioController::OnOutputNodeVolumeChanged(uint64_t /* node_id */,
                                                     int /* volume */) {
}

void ShellAudioController::OnOutputMuteChanged(bool /* mute_on */,
                                               bool /* system_adjust */) {}

void ShellAudioController::OnInputNodeGainChanged(uint64_t /* node_id */,
                                                  int /* gain */) {
}

void ShellAudioController::OnInputMuteChanged(bool /* mute_on */) {
}

void ShellAudioController::OnAudioNodesChanged() {
  VLOG(1) << "Audio nodes changed";
  ActivateDevices();
}

void ShellAudioController::OnActiveOutputNodeChanged() {}

void ShellAudioController::OnActiveInputNodeChanged() {}

void ShellAudioController::ActivateDevices() {
  chromeos::CrasAudioHandler* handler = chromeos::CrasAudioHandler::Get();
  chromeos::AudioDeviceList devices;
  handler->GetAudioDevices(&devices);
  sort(devices.begin(), devices.end(), chromeos::AudioDeviceCompare());

  uint64_t best_input = 0, best_output = 0;
  for (chromeos::AudioDeviceList::const_reverse_iterator it = devices.rbegin();
       it != devices.rend() && (!best_input || !best_output); ++it) {
    // TODO(derat): Need to check |plugged_time|?
    if (it->is_input && !best_input)
      best_input = it->id;
    else if (!it->is_input && !best_output)
      best_output = it->id;
  }

  if (best_input && best_input != handler->GetPrimaryActiveInputNode()) {
    const chromeos::AudioDevice* device = GetDevice(devices, best_input);
    DCHECK(device);
    VLOG(1) << "Activating input device: " << device->ToString();
    handler->SwitchToDevice(*device, true,
                            chromeos::CrasAudioHandler::ACTIVATE_BY_USER);
  }
  if (best_output && best_output != handler->GetPrimaryActiveOutputNode()) {
    const chromeos::AudioDevice* device = GetDevice(devices, best_output);
    DCHECK(device);
    VLOG(1) << "Activating output device: " << device->ToString();
    handler->SwitchToDevice(*device, true,
                            chromeos::CrasAudioHandler::ACTIVATE_BY_USER);
  }
}

}  // namespace extensions