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 audio_service_callback.
17 
18 #include "audio_service_callback.h"
19 
20 #include <base/bind.h>
21 #include <base/logging.h>
22 
23 #include "brillo_audio_client_helpers.h"
24 #include "brillo_audio_device_info_def.h"
25 
26 using android::binder::Status;
27 
28 namespace brillo {
29 
AudioServiceCallback(const BAudioCallback * callback,void * user_data)30 AudioServiceCallback::AudioServiceCallback(const BAudioCallback* callback,
31                                            void* user_data) {
32   connected_callback_ = base::Bind(callback->OnAudioDeviceAdded);
33   disconnected_callback_ = base::Bind(callback->OnAudioDeviceRemoved);
34   volume_callback_ = base::Bind(callback->OnVolumeChanged);
35   user_data_ = user_data;
36 }
37 
OnAudioDevicesConnected(const std::vector<int> & devices)38 Status AudioServiceCallback::OnAudioDevicesConnected(
39     const std::vector<int>& devices) {
40   for (auto device : devices) {
41     BAudioDeviceInfo device_info;
42     device_info.internal_ = std::unique_ptr<BAudioDeviceInfoInternal>(
43         BAudioDeviceInfoInternal::CreateFromAudioDevicesT(device));
44     connected_callback_.Run(&device_info, user_data_);
45   }
46   return Status::ok();
47 }
48 
OnAudioDevicesDisconnected(const std::vector<int> & devices)49 Status AudioServiceCallback::OnAudioDevicesDisconnected(
50     const std::vector<int>& devices) {
51   for (auto device : devices) {
52     BAudioDeviceInfo device_info;
53     device_info.internal_ = std::unique_ptr<BAudioDeviceInfoInternal>(
54         BAudioDeviceInfoInternal::CreateFromAudioDevicesT(device));
55     disconnected_callback_.Run(&device_info, user_data_);
56   }
57   return Status::ok();
58 }
59 
OnVolumeChanged(int stream,int previous_index,int current_index)60 Status AudioServiceCallback::OnVolumeChanged(int stream,
61                                              int previous_index,
62                                              int current_index) {
63   auto usage = BrilloAudioClientHelpers::GetBAudioUsage(
64       static_cast<audio_stream_type_t>(stream));
65   volume_callback_.Run(usage, previous_index, current_index, user_data_);
66   return Status::ok();
67 }
68 
Equals(const android::sp<AudioServiceCallback> & callback)69 bool AudioServiceCallback::Equals(const android::sp<AudioServiceCallback>& callback) {
70   if (callback->connected_callback_.Equals(connected_callback_) &&
71       callback->disconnected_callback_.Equals(disconnected_callback_) &&
72       callback->volume_callback_.Equals(volume_callback_) &&
73       callback->user_data_ == user_data_)
74     return true;
75   return false;
76 }
77 
78 }  // namespace brillo
79