Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / chromeos / dbus / fake_cras_audio_client.cc
blobe28852bb201e2f23a41b083a04b7bcdcfbf9bb6e
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.
5 #include "chromeos/dbus/fake_cras_audio_client.h"
7 namespace chromeos {
9 FakeCrasAudioClient::FakeCrasAudioClient()
10 : active_input_node_id_(0),
11 active_output_node_id_(0) {
14 FakeCrasAudioClient::~FakeCrasAudioClient() {
17 void FakeCrasAudioClient::Init(dbus::Bus* bus) {
18 VLOG(1) << "FakeCrasAudioClient is created";
20 // Fake audio output nodes.
21 AudioNode output_1;
22 output_1.is_input = false;
23 output_1.id = 10001;
24 output_1.device_name = "Fake Speaker";
25 output_1.type = "INTERNAL_SPEAKER";
26 output_1.name = "Speaker";
27 node_list_.push_back(output_1);
29 AudioNode output_2;
30 output_2.is_input = false;
31 output_2.id = 10002;
32 output_2.device_name = "Fake Headphone";
33 output_2.type = "HEADPHONE";
34 output_2.name = "Headphone";
35 node_list_.push_back(output_2);
37 AudioNode output_3;
38 output_3.is_input = false;
39 output_3.id = 10003;
40 output_3.device_name = "Fake Bluetooth Headphone";
41 output_3.type = "BLUETOOTH";
42 output_3.name = "Headphone";
43 node_list_.push_back(output_3);
45 AudioNode output_4;
46 output_4.is_input = false;
47 output_4.id = 10004;
48 output_4.device_name = "Fake HDMI Speaker";
49 output_4.type = "HDMI";
50 output_4.name = "HDMI Speaker";
51 node_list_.push_back(output_4);
53 // Fake audio input nodes
54 AudioNode input_1;
55 input_1.is_input = true;
56 input_1.id = 20001;
57 input_1.device_name = "Fake Internal Mic";
58 input_1.type = "INTERNAL_MIC";
59 input_1.name = "Internal Mic";
60 node_list_.push_back(input_1);
62 AudioNode input_2;
63 input_2.is_input = true;
64 input_2.id = 20002;
65 input_2.device_name = "Fake USB Mic";
66 input_2.type = "USB";
67 input_2.name = "Mic";
68 node_list_.push_back(input_2);
70 AudioNode input_3;
71 input_3.is_input = true;
72 input_3.id = 20003;
73 input_3.device_name = "Fake Mick Jack";
74 input_3.type = "MIC";
75 input_3.name = "Some type of Mic";
76 node_list_.push_back(input_3);
79 void FakeCrasAudioClient::AddObserver(Observer* observer) {
80 observers_.AddObserver(observer);
83 void FakeCrasAudioClient::RemoveObserver(Observer* observer) {
84 observers_.RemoveObserver(observer);
87 bool FakeCrasAudioClient::HasObserver(const Observer* observer) const {
88 return observers_.HasObserver(observer);
91 void FakeCrasAudioClient::GetVolumeState(
92 const GetVolumeStateCallback& callback) {
93 callback.Run(volume_state_, true);
96 void FakeCrasAudioClient::GetNodes(const GetNodesCallback& callback,
97 const ErrorCallback& error_callback) {
98 callback.Run(node_list_, true);
101 void FakeCrasAudioClient::SetOutputNodeVolume(uint64 node_id, int32 volume) {}
103 void FakeCrasAudioClient::SetOutputUserMute(bool mute_on) {
104 volume_state_.output_user_mute = mute_on;
105 FOR_EACH_OBSERVER(Observer, observers_,
106 OutputMuteChanged(volume_state_.output_user_mute));
109 void FakeCrasAudioClient::SetInputNodeGain(uint64 node_id, int32 input_gain) {}
111 void FakeCrasAudioClient::SetInputMute(bool mute_on) {
112 volume_state_.input_mute = mute_on;
113 FOR_EACH_OBSERVER(Observer, observers_,
114 InputMuteChanged(volume_state_.input_mute));
117 void FakeCrasAudioClient::SetActiveOutputNode(uint64 node_id) {
118 if (active_output_node_id_ == node_id)
119 return;
121 for (size_t i = 0; i < node_list_.size(); ++i) {
122 if (node_list_[i].id == active_output_node_id_)
123 node_list_[i].active = false;
124 else if (node_list_[i].id == node_id)
125 node_list_[i].active = true;
127 active_output_node_id_ = node_id;
128 FOR_EACH_OBSERVER(Observer, observers_, ActiveOutputNodeChanged(node_id));
131 void FakeCrasAudioClient::SetActiveInputNode(uint64 node_id) {
132 if (active_input_node_id_ == node_id)
133 return;
135 for (size_t i = 0; i < node_list_.size(); ++i) {
136 if (node_list_[i].id == active_input_node_id_)
137 node_list_[i].active = false;
138 else if (node_list_[i].id == node_id)
139 node_list_[i].active = true;
141 active_input_node_id_ = node_id;
142 FOR_EACH_OBSERVER(Observer, observers_, ActiveInputNodeChanged(node_id));
145 void FakeCrasAudioClient::AddActiveInputNode(uint64 node_id) {
146 for (size_t i = 0; i < node_list_.size(); ++i) {
147 if (node_list_[i].id == node_id)
148 node_list_[i].active = true;
152 void FakeCrasAudioClient::RemoveActiveInputNode(uint64 node_id) {
153 for (size_t i = 0; i < node_list_.size(); ++i) {
154 if (node_list_[i].id == node_id)
155 node_list_[i].active = false;
159 void FakeCrasAudioClient::SwapLeftRight(uint64 node_id, bool swap) {
162 void FakeCrasAudioClient::AddActiveOutputNode(uint64 node_id) {
163 for (size_t i = 0; i < node_list_.size(); ++i) {
164 if (node_list_[i].id == node_id)
165 node_list_[i].active = true;
169 void FakeCrasAudioClient::RemoveActiveOutputNode(uint64 node_id) {
170 for (size_t i = 0; i < node_list_.size(); ++i) {
171 if (node_list_[i].id == node_id)
172 node_list_[i].active = false;
176 void FakeCrasAudioClient::InsertAudioNodeToList(const AudioNode& audio_node) {
177 auto iter = FindNode(audio_node.id);
178 if (iter != node_list_.end())
179 (*iter) = audio_node;
180 else
181 node_list_.push_back(audio_node);
182 FOR_EACH_OBSERVER(Observer, observers_, NodesChanged());
185 void FakeCrasAudioClient::RemoveAudioNodeFromList(const uint64& node_id) {
186 auto iter = FindNode(node_id);
187 if (iter != node_list_.end()) {
188 node_list_.erase(iter);
189 FOR_EACH_OBSERVER(Observer, observers_, NodesChanged());
193 void FakeCrasAudioClient::SetAudioNodesForTesting(
194 const AudioNodeList& audio_nodes) {
195 node_list_ = audio_nodes;
198 void FakeCrasAudioClient::SetAudioNodesAndNotifyObserversForTesting(
199 const AudioNodeList& new_nodes) {
200 SetAudioNodesForTesting(new_nodes);
201 FOR_EACH_OBSERVER(Observer, observers_, NodesChanged());
204 AudioNodeList::iterator FakeCrasAudioClient::FindNode(uint64 node_id) {
205 return std::find_if(
206 node_list_.begin(), node_list_.end(),
207 [node_id](const AudioNode& node) { return node_id == node.id; });
210 } // namespace chromeos