OLD | NEW |
---|---|
(Empty) | |
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 "chrome/browser/extensions/api/audio/audio_service.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "chromeos/dbus/audio_node.h" | |
10 #include "chromeos/dbus/cras_audio_client.h" | |
11 #include "chromeos/dbus/dbus_thread_manager.h" | |
12 #include "content/public/browser/browser_thread.h" | |
13 | |
14 using content::BrowserThread; | |
15 | |
16 namespace extensions { | |
17 | |
18 using api::audio::OutputDeviceInfo; | |
19 using api::audio::InputDeviceInfo; | |
20 | |
21 class AudioServiceImpl : public AudioService, | |
22 public chromeos::CrasAudioClient::Observer { | |
23 public: | |
24 AudioServiceImpl(); | |
25 virtual ~AudioServiceImpl(); | |
26 | |
27 // Called by listeners to this service to add/remove themselves as observers. | |
28 virtual void AddObserver(AudioService::Observer* observer); | |
29 virtual void RemoveObserver(AudioService::Observer* observer); | |
30 | |
31 // Start to query audio device information. | |
32 virtual void StartGetInfo(const GetInfoCallback& callback); | |
33 | |
34 protected: | |
35 // chromeos::CrasAudioClient::Observer overrides. | |
36 virtual void OutputVolumeChanged(int volume) OVERRIDE; | |
37 virtual void OutputMuteChanged(bool mute_on) OVERRIDE; | |
38 virtual void InputGainChanged(int gain) OVERRIDE; | |
39 virtual void InputMuteChanged(bool mute_on) OVERRIDE; | |
40 virtual void NodesChanged() OVERRIDE; | |
41 virtual void ActiveOutputNodeChanged(uint64 node_id) OVERRIDE; | |
42 virtual void ActiveInputNodeChanged(uint64 node_id) OVERRIDE; | |
43 | |
44 private: | |
45 void NotifyDeviceChanged(); | |
46 | |
47 // Callback for CrasAudioClient::GetNodes(). | |
48 void OnGetNodes(const GetInfoCallback& callback, | |
49 const chromeos::AudioNodeList& audio_nodes, | |
50 bool success); | |
51 | |
52 // List of observers. | |
53 ObserverList<AudioService::Observer> observer_list_; | |
54 | |
55 chromeos::CrasAudioClient* cras_audio_client_; | |
56 | |
57 // Note: This should remain the last member so it'll be destroyed and | |
58 // invalidate the weak pointers before any other members are destroyed. | |
59 base::WeakPtrFactory<AudioServiceImpl> weak_ptr_factory_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(AudioServiceImpl); | |
62 }; | |
63 | |
64 AudioServiceImpl::AudioServiceImpl() | |
65 : ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { | |
66 if (chromeos::DBusThreadManager::Get()) { | |
67 cras_audio_client_ = | |
68 chromeos::DBusThreadManager::Get()->GetCrasAudioClient(); | |
69 DCHECK(cras_audio_client_); | |
70 | |
71 if (cras_audio_client_) | |
72 cras_audio_client_->AddObserver(this); | |
73 } | |
74 } | |
75 | |
76 AudioServiceImpl::~AudioServiceImpl() { | |
77 if (cras_audio_client_) | |
78 cras_audio_client_->RemoveObserver(this); | |
79 } | |
80 | |
81 void AudioServiceImpl::AddObserver(AudioService::Observer* observer) { | |
82 observer_list_.AddObserver(observer); | |
83 } | |
84 | |
85 void AudioServiceImpl::RemoveObserver(AudioService::Observer* observer) { | |
86 observer_list_.RemoveObserver(observer); | |
87 } | |
88 | |
89 void AudioServiceImpl::StartGetInfo(const GetInfoCallback& callback) { | |
90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
91 DCHECK(cras_audio_client_); | |
92 if (cras_audio_client_) | |
93 cras_audio_client_->GetNodes(base::Bind(&AudioServiceImpl::OnGetNodes, | |
94 weak_ptr_factory_.GetWeakPtr(), | |
95 callback)); | |
96 } | |
97 | |
98 void AudioServiceImpl::OnGetNodes(const GetInfoCallback& callback, | |
99 const chromeos::AudioNodeList& audio_nodes, | |
100 bool success) { | |
101 OutputInfo output_info; | |
102 InputInfo input_info; | |
103 if (success) { | |
104 for (chromeos::AudioNodeList::const_iterator i = audio_nodes.begin(); | |
105 i != audio_nodes.end(); i++) { | |
rkc
2013/04/22 22:54:19
s/i/iter
s/i++/++iter
hshi1
2013/04/22 23:02:16
Done.
| |
106 if (!i->is_input) { | |
107 linked_ptr<OutputDeviceInfo> info(new OutputDeviceInfo()); | |
108 info->id = i->id; | |
109 info->name = i->name; | |
110 info->is_active = i->active; | |
111 output_info.push_back(info); | |
112 } else { | |
113 linked_ptr<InputDeviceInfo> info(new InputDeviceInfo()); | |
114 info->id = i->id; | |
115 info->name = i->name; | |
116 info->is_active = i->active; | |
117 input_info.push_back(info); | |
118 } | |
119 } | |
120 } | |
121 | |
122 DCHECK(!callback.is_null()); | |
123 if (!callback.is_null()) | |
124 callback.Run(output_info, input_info, success); | |
125 } | |
126 | |
127 void AudioServiceImpl::OutputVolumeChanged(int volume) { | |
128 NotifyDeviceChanged(); | |
129 } | |
130 | |
131 void AudioServiceImpl::OutputMuteChanged(bool mute_on) { | |
132 NotifyDeviceChanged(); | |
133 } | |
134 | |
135 void AudioServiceImpl::InputGainChanged(int gain) { | |
136 NotifyDeviceChanged(); | |
137 } | |
138 | |
139 void AudioServiceImpl::InputMuteChanged(bool mute_on) { | |
140 NotifyDeviceChanged(); | |
141 } | |
142 | |
143 void AudioServiceImpl::NodesChanged() { | |
144 NotifyDeviceChanged(); | |
145 } | |
146 | |
147 void AudioServiceImpl::ActiveOutputNodeChanged(uint64 node_id) { | |
148 NotifyDeviceChanged(); | |
149 } | |
150 | |
151 void AudioServiceImpl::ActiveInputNodeChanged(uint64 node_id) { | |
152 NotifyDeviceChanged(); | |
153 } | |
154 | |
155 void AudioServiceImpl::NotifyDeviceChanged() { | |
156 FOR_EACH_OBSERVER(AudioService::Observer, observer_list_, OnDeviceChanged()); | |
157 } | |
158 | |
159 AudioService* AudioService::CreateInstance() { | |
160 return new AudioServiceImpl; | |
161 } | |
162 | |
163 } // namespace extensions | |
OLD | NEW |