1 // Copyright (C) 2020 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 #include "AudioProxyDevicesManagerImpl.h"
16
17 #include <utils/Log.h>
18
19 using ::android::OK;
20 using ::android::status_t;
21 using ::device::google::atv::audio_proxy::AUDIO_PROXY_CPP_VERSION::IBusDevice;
22
23 namespace audio_proxy {
24 namespace service {
25 namespace {
26 template<typename D>
27 class BusDeviceVersionWrapper : public IBusDevice {
28 public:
BusDeviceVersionWrapper(const sp<D> & device)29 explicit BusDeviceVersionWrapper(const sp<D>& device) : mDevice(device) {}
30 ~BusDeviceVersionWrapper() override = default;
31
openOutputStream(int32_t ioHandle,const DeviceAddress & device,const AudioConfig & config,hidl_bitfield<AudioOutputFlag> flags,const SourceMetadata & sourceMetadata,openOutputStream_cb _hidl_cb)32 Return<void> openOutputStream(int32_t ioHandle, const DeviceAddress& device,
33 const AudioConfig& config,
34 hidl_bitfield<AudioOutputFlag> flags,
35 const SourceMetadata& sourceMetadata,
36 openOutputStream_cb _hidl_cb) override {
37 return mDevice->openOutputStream(ioHandle, device, config, flags,
38 sourceMetadata, std::move(_hidl_cb));
39 }
40
41 private:
42 const sp<D> mDevice;
43 };
44 } // namespace
45
AudioProxyDevicesManagerImpl()46 AudioProxyDevicesManagerImpl::AudioProxyDevicesManagerImpl() {
47 // We need to register the factory service when this is instantiated,
48 // rather than when the first client registers in order to not break VTS.
49 ensureDevicesFactory();
50 }
51
52 AudioProxyDevicesManagerImpl::~AudioProxyDevicesManagerImpl() = default;
53
registerDevice(const hidl_string & address,const sp<::device::google::atv::audio_proxy::CPP_VERSION::IBusDevice> & device)54 Return<bool> AudioProxyDevicesManagerImpl::registerDevice(
55 const hidl_string& address,
56 const sp<::device::google::atv::audio_proxy::CPP_VERSION::IBusDevice>&
57 device) {
58 if (address.empty() || !device) {
59 return false;
60 }
61
62 sp<IBusDevice> busDevice = IBusDevice::castFrom(device);
63 if (!busDevice) {
64 ALOGW("Client registers lower version bus device at %s", address.c_str());
65 busDevice = new BusDeviceVersionWrapper(device);
66 }
67
68 if (!mBusDeviceProvider.add(address, busDevice)) {
69 ALOGE("Failed to register bus device with addr %s", address.c_str());
70 return false;
71 }
72
73 if (!ensureDevicesFactory()) {
74 ALOGE("Failed to register audio devices factory.");
75 mBusDeviceProvider.removeAll();
76 return false;
77 }
78
79 return true;
80 }
81
ensureDevicesFactory()82 bool AudioProxyDevicesManagerImpl::ensureDevicesFactory() {
83 sp<DevicesFactoryImpl> factory = mDevicesFactory.promote();
84 if (factory) {
85 return true;
86 }
87
88 factory = new DevicesFactoryImpl(mBusDeviceProvider);
89 status_t status = factory->registerAsService("audio_proxy");
90 if (status != OK) {
91 return false;
92 }
93
94 mDevicesFactory = factory;
95 return true;
96 }
97
98 } // namespace service
99 } // namespace audio_proxy
100