1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ANDROID_HARDWARE_DEVICES_FACTORY_HAL_HIDL_H
18 #define ANDROID_HARDWARE_DEVICES_FACTORY_HAL_HIDL_H
19 
20 #include <mutex>
21 #include <vector>
22 
23 #include PATH(android/hardware/audio/FILE_VERSION/IDevicesFactory.h)
24 #include <media/audiohal/DevicesFactoryHalInterface.h>
25 #include <utils/Errors.h>
26 #include <utils/RefBase.h>
27 
28 #include "DeviceHalHidl.h"
29 
30 using ::android::hardware::audio::CPP_VERSION::IDevicesFactory;
31 
32 namespace android {
33 
34 class DevicesFactoryHalHidl : public DevicesFactoryHalInterface
35 {
36   public:
37     explicit DevicesFactoryHalHidl(sp<IDevicesFactory> devicesFactory);
38     void onFirstRef() override;
39 
40     status_t getDeviceNames(std::vector<std::string> *names) override;
41 
42     // Opens a device with the specified name. To close the device, it is
43     // necessary to release references to the returned object.
44     status_t openDevice(const char *name, sp<DeviceHalInterface> *device) override;
45 
46     status_t setCallbackOnce(sp<DevicesFactoryHalCallback> callback) override;
47 
48     android::detail::AudioHalVersionInfo getHalVersion() const override;
49 
50     status_t getSurroundSoundConfig(media::SurroundSoundConfig *config) override;
51 
52     status_t getEngineConfig(media::audio::common::AudioHalEngineConfig *config) override;
53 
54   private:
55     friend class ServiceNotificationListener;
56     void addDeviceFactory(sp<IDevicesFactory> factory, bool needToNotify);
57     std::vector<sp<IDevicesFactory>> copyDeviceFactories();
58 
59     std::mutex mLock;
60     std::vector<sp<IDevicesFactory>> mDeviceFactories;  // GUARDED_BY(mLock)
61     wp<DevicesFactoryHalCallback> mCallback;  // GUARDED_BY(mLock)
62     bool mHaveUndeliveredNotifications = false;  // GUARDED_BY(mLock)
63 
64     virtual ~DevicesFactoryHalHidl() = default;
65 };
66 
67 } // namespace android
68 
69 #endif // ANDROID_HARDWARE_DEVICES_FACTORY_HAL_HIDL_H
70