1 // Copyright 2016 The Android Open Source Project
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
16 // Implementation of brillo_audio_client.h
18 #include "brillo_audio_client.h"
20 #include <base/logging.h>
21 #include <binder/Status.h>
22 #include <binderwrapper/binder_wrapper.h>
24 #include "brillo_audio_client_helpers.h"
25 #include "brillo_audio_device_info_def.h"
26 #include "brillo_audio_device_info_internal.h"
28 using android::binder::Status;
32 static const char kBrilloAudioServiceName[] =
33 "android.brillo.brilloaudioservice.BrilloAudioService";
35 std::shared_ptr<BrilloAudioClient> BrilloAudioClient::instance_ = nullptr;
37 int BrilloAudioClient::callback_id_counter_ = 1;
39 BrilloAudioClient::~BrilloAudioClient() {}
41 std::weak_ptr<BrilloAudioClient> BrilloAudioClient::GetClientInstance() {
43 instance_ = std::shared_ptr<BrilloAudioClient>(new BrilloAudioClient());
44 if (!instance_->Initialize()) {
45 LOG(ERROR) << "Could not Initialize the brillo audio client.";
53 android::sp<android::IBinder> BrilloAudioClient::ConnectToService(
54 const std::string& service_name, const base::Closure& callback) {
55 android::BinderWrapper* binder_wrapper =
56 android::BinderWrapper::GetOrCreateInstance();
57 auto service = binder_wrapper->GetService(service_name);
61 binder_wrapper->RegisterForDeathNotifications(service, callback);
65 void BrilloAudioClient::OnBASDisconnect() {
66 LOG(WARNING) << "The brillo audio service died! Please reset the "
71 bool BrilloAudioClient::Initialize() {
72 auto service = ConnectToService(
73 kBrilloAudioServiceName, base::Bind(&BrilloAudioClient::OnBASDisconnect,
74 weak_ptr_factory_.GetWeakPtr()));
76 LOG(ERROR) << "Could not connect to brillo audio service.";
79 brillo_audio_service_ = android::interface_cast<IBrilloAudioService>(service);
83 int BrilloAudioClient::GetDevices(int flag, std::vector<int>& devices) {
84 if (!brillo_audio_service_.get()) {
88 auto status = brillo_audio_service_->GetDevices(flag, &devices);
89 return status.serviceSpecificErrorCode();
92 int BrilloAudioClient::SetDevice(audio_policy_force_use_t usage,
93 audio_policy_forced_cfg_t config) {
94 if (!brillo_audio_service_.get()) {
98 auto status = brillo_audio_service_->SetDevice(usage, config);
99 return status.serviceSpecificErrorCode();
102 int BrilloAudioClient::GetMaxVolumeSteps(BAudioUsage usage, int* max_steps) {
103 if (!brillo_audio_service_.get()) {
107 auto status = brillo_audio_service_->GetMaxVolumeSteps(
108 BrilloAudioClientHelpers::GetStreamType(usage), max_steps);
109 return status.serviceSpecificErrorCode();
112 int BrilloAudioClient::SetMaxVolumeSteps(BAudioUsage usage, int max_steps) {
113 if (!brillo_audio_service_.get()) {
117 auto status = brillo_audio_service_->SetMaxVolumeSteps(
118 BrilloAudioClientHelpers::GetStreamType(usage), max_steps);
119 return status.serviceSpecificErrorCode();
122 int BrilloAudioClient::SetVolumeIndex(BAudioUsage usage,
123 audio_devices_t device,
125 if (!brillo_audio_service_.get()) {
129 auto status = brillo_audio_service_->SetVolumeIndex(
130 BrilloAudioClientHelpers::GetStreamType(usage), device, index);
131 return status.serviceSpecificErrorCode();
134 int BrilloAudioClient::GetVolumeIndex(BAudioUsage usage,
135 audio_devices_t device,
137 if (!brillo_audio_service_.get()) {
141 auto status = brillo_audio_service_->GetVolumeIndex(
142 BrilloAudioClientHelpers::GetStreamType(usage), device, index);
143 return status.serviceSpecificErrorCode();
146 int BrilloAudioClient::GetVolumeControlStream(BAudioUsage* usage) {
147 if (!brillo_audio_service_.get()) {
152 auto status = brillo_audio_service_->GetVolumeControlStream(&stream);
153 *usage = BrilloAudioClientHelpers::GetBAudioUsage(
154 static_cast<audio_stream_type_t>(stream));
155 return status.serviceSpecificErrorCode();
158 int BrilloAudioClient::SetVolumeControlStream(BAudioUsage usage) {
159 if (!brillo_audio_service_.get()) {
163 auto status = brillo_audio_service_->SetVolumeControlStream(
164 BrilloAudioClientHelpers::GetStreamType(usage));
165 return status.serviceSpecificErrorCode();
168 int BrilloAudioClient::IncrementVolume() {
169 if (!brillo_audio_service_.get()) {
173 auto status = brillo_audio_service_->IncrementVolume();
174 return status.serviceSpecificErrorCode();
177 int BrilloAudioClient::DecrementVolume() {
178 if (!brillo_audio_service_.get()) {
182 auto status = brillo_audio_service_->DecrementVolume();
183 return status.serviceSpecificErrorCode();
186 int BrilloAudioClient::RegisterAudioCallback(
187 android::sp<AudioServiceCallback> callback, int* callback_id) {
188 if (!brillo_audio_service_.get()) {
192 if (!brillo_audio_service_->RegisterServiceCallback(callback).isOk()) {
196 for (auto& entry : callback_map_) {
197 if (entry.second->Equals(callback)) {
198 LOG(ERROR) << "Callback has already been registered.";
203 *callback_id = callback_id_counter_++;
204 callback_map_.emplace(*callback_id, callback);
208 int BrilloAudioClient::UnregisterAudioCallback(int callback_id) {
209 if (!brillo_audio_service_.get()) {
213 auto callback_elem = callback_map_.find(callback_id);
214 if (callback_elem == callback_map_.end()) {
215 // If we were passed an invalid callback_id, do nothing.
216 LOG(ERROR) << "Unregister called with invalid callback ID.";
219 brillo_audio_service_->UnregisterServiceCallback(callback_elem->second.get());
220 callback_map_.erase(callback_elem);
224 } // namespace brillo