1 // Copyright 2016 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 
16 // Implementation of brillo_audio_service_impl.h
17 
18 #include "brillo_audio_service_impl.h"
19 
20 using android::binder::Status;
21 
22 namespace brillo {
23 
GetDevices(int flag,std::vector<int> * _aidl_return)24 Status BrilloAudioServiceImpl::GetDevices(int flag,
25                                           std::vector<int>* _aidl_return) {
26   auto device_handler = audio_device_handler_.lock();
27   if (!device_handler) {
28     return Status::fromServiceSpecificError(
29         EREMOTEIO, android::String8("The audio device handler died."));
30   }
31   if (flag == BrilloAudioService::GET_DEVICES_INPUTS) {
32     device_handler->GetInputDevices(_aidl_return);
33   } else if (flag == BrilloAudioService::GET_DEVICES_OUTPUTS) {
34     device_handler->GetOutputDevices(_aidl_return);
35   } else {
36     return Status::fromServiceSpecificError(EINVAL,
37                                             android::String8("Invalid flag."));
38   }
39   return Status::ok();
40 }
41 
SetDevice(int usage,int config)42 Status BrilloAudioServiceImpl::SetDevice(int usage, int config) {
43   auto device_handler = audio_device_handler_.lock();
44   if (!device_handler) {
45     return Status::fromServiceSpecificError(
46         EREMOTEIO, android::String8("The audio device handler died."));
47   }
48   int rc =
49       device_handler->SetDevice(static_cast<audio_policy_force_use_t>(usage),
50                                 static_cast<audio_policy_forced_cfg_t>(config));
51   if (rc) return Status::fromServiceSpecificError(rc);
52   return Status::ok();
53 }
54 
RegisterServiceCallback(const android::sp<IAudioServiceCallback> & callback)55 Status BrilloAudioServiceImpl::RegisterServiceCallback(
56     const android::sp<IAudioServiceCallback>& callback) {
57   callbacks_set_.insert(callback);
58   return Status::ok();
59 }
60 
UnregisterServiceCallback(const android::sp<IAudioServiceCallback> & callback)61 Status BrilloAudioServiceImpl::UnregisterServiceCallback(
62     const android::sp<IAudioServiceCallback>& callback) {
63   callbacks_set_.erase(callback);
64   return Status::ok();
65 }
66 
RegisterHandlers(std::weak_ptr<AudioDeviceHandler> audio_device_handler,std::weak_ptr<AudioVolumeHandler> audio_volume_handler)67 void BrilloAudioServiceImpl::RegisterHandlers(
68     std::weak_ptr<AudioDeviceHandler> audio_device_handler,
69     std::weak_ptr<AudioVolumeHandler> audio_volume_handler) {
70   audio_device_handler_ = audio_device_handler;
71   audio_volume_handler_ = audio_volume_handler;
72 }
73 
GetMaxVolumeSteps(int stream,int * _aidl_return)74 Status BrilloAudioServiceImpl::GetMaxVolumeSteps(int stream,
75                                                  int* _aidl_return) {
76   auto volume_handler = audio_volume_handler_.lock();
77   if (!volume_handler) {
78     return Status::fromServiceSpecificError(
79         EREMOTEIO, android::String8("The audio volume handler died."));
80   }
81   *_aidl_return = volume_handler->GetVolumeMaxSteps(
82       static_cast<audio_stream_type_t>(stream));
83   return Status::ok();
84 }
85 
SetMaxVolumeSteps(int stream,int max_steps)86 Status BrilloAudioServiceImpl::SetMaxVolumeSteps(int stream, int max_steps) {
87   auto volume_handler = audio_volume_handler_.lock();
88   if (!volume_handler) {
89     return Status::fromServiceSpecificError(
90         EREMOTEIO, android::String8("The audio volume handler died."));
91   }
92   int rc = volume_handler->SetVolumeMaxSteps(
93       static_cast<audio_stream_type_t>(stream), max_steps);
94   if (rc)
95     return Status::fromServiceSpecificError(rc);
96   return Status::ok();
97 }
98 
SetVolumeIndex(int stream,int device,int index)99 Status BrilloAudioServiceImpl::SetVolumeIndex(int stream,
100                                               int device,
101                                               int index) {
102   auto volume_handler = audio_volume_handler_.lock();
103   if (!volume_handler) {
104     return Status::fromServiceSpecificError(
105         EREMOTEIO, android::String8("The audio volume handler died."));
106   }
107   int rc =
108       volume_handler->SetVolumeIndex(static_cast<audio_stream_type_t>(stream),
109                                      static_cast<audio_devices_t>(device),
110                                      index);
111   if (rc)
112     return Status::fromServiceSpecificError(rc);
113   return Status::ok();
114 }
115 
GetVolumeIndex(int stream,int device,int * _aidl_return)116 Status BrilloAudioServiceImpl::GetVolumeIndex(int stream,
117                                               int device,
118                                               int* _aidl_return) {
119   auto volume_handler = audio_volume_handler_.lock();
120   if (!volume_handler) {
121     return Status::fromServiceSpecificError(
122         EREMOTEIO, android::String8("The audio volume handler died."));
123   }
124   *_aidl_return =
125       volume_handler->GetVolumeIndex(static_cast<audio_stream_type_t>(stream),
126                                      static_cast<audio_devices_t>(device));
127   return Status::ok();
128 }
129 
IncrementVolume()130 Status BrilloAudioServiceImpl::IncrementVolume() {
131   auto volume_handler = audio_volume_handler_.lock();
132   if (!volume_handler) {
133     return Status::fromServiceSpecificError(
134         EREMOTEIO, android::String8("The audio volume handler died."));
135   }
136   volume_handler->AdjustVolumeActiveStreams(1);
137   return Status::ok();
138 }
139 
GetVolumeControlStream(int * _aidl_return)140 Status BrilloAudioServiceImpl::GetVolumeControlStream(int* _aidl_return) {
141   auto volume_handler = audio_volume_handler_.lock();
142   if (!volume_handler) {
143     return Status::fromServiceSpecificError(
144         EREMOTEIO, android::String8("The audio volume handler died."));
145   }
146   *_aidl_return = volume_handler->GetVolumeControlStream();
147   return Status::ok();
148 }
149 
SetVolumeControlStream(int stream)150 Status BrilloAudioServiceImpl::SetVolumeControlStream(int stream) {
151   auto volume_handler = audio_volume_handler_.lock();
152   if (!volume_handler) {
153     return Status::fromServiceSpecificError(
154         EREMOTEIO, android::String8("The audio volume handler died."));
155   }
156   volume_handler->SetVolumeControlStream(
157       static_cast<audio_stream_type_t>(stream));
158   return Status::ok();
159 }
160 
DecrementVolume()161 Status BrilloAudioServiceImpl::DecrementVolume() {
162   auto volume_handler = audio_volume_handler_.lock();
163   if (!volume_handler) {
164     return Status::fromServiceSpecificError(
165         EREMOTEIO, android::String8("The audio volume handler died."));
166   }
167   volume_handler->AdjustVolumeActiveStreams(-1);
168   return Status::ok();
169 }
170 
OnDevicesConnected(const std::vector<int> & devices)171 void BrilloAudioServiceImpl::OnDevicesConnected(
172     const std::vector<int>& devices) {
173   for (const auto& callback : callbacks_set_) {
174     callback->OnAudioDevicesConnected(devices);
175   }
176 }
177 
OnDevicesDisconnected(const std::vector<int> & devices)178 void BrilloAudioServiceImpl::OnDevicesDisconnected(
179     const std::vector<int>& devices) {
180   for (const auto& callback : callbacks_set_) {
181     callback->OnAudioDevicesDisconnected(devices);
182   }
183 }
184 
OnVolumeChanged(audio_stream_type_t stream,int previous_index,int current_index)185 void BrilloAudioServiceImpl::OnVolumeChanged(audio_stream_type_t stream,
186                                              int previous_index,
187                                              int current_index) {
188   for (const auto& callback : callbacks_set_) {
189     callback->OnVolumeChanged(stream, previous_index, current_index);
190   }
191 }
192 
193 }  // namespace brillo
194