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 #pragma once
16 
17 #include <set>
18 
19 // clang-format off
20 #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
21 // clang-format on
22 #include <hidl/MQDescriptor.h>
23 #include <hidl/Status.h>
24 
25 namespace audio_proxy {
26 namespace service {
27 
28 using ::android::sp;
29 using ::android::hardware::hidl_array;
30 using ::android::hardware::hidl_bitfield;
31 using ::android::hardware::hidl_death_recipient;
32 using ::android::hardware::hidl_memory;
33 using ::android::hardware::hidl_string;
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::Return;
36 using ::android::hardware::Void;
37 using ::android::hardware::audio::common::CPP_VERSION::AudioConfig;
38 using ::android::hardware::audio::common::CPP_VERSION::AudioPatchHandle;
39 using ::android::hardware::audio::common::CPP_VERSION::AudioPort;
40 using ::android::hardware::audio::common::CPP_VERSION::AudioPortConfig;
41 using ::android::hardware::audio::common::CPP_VERSION::AudioPortHandle;
42 using ::android::hardware::audio::common::CPP_VERSION::DeviceAddress;
43 using ::android::hardware::audio::common::CPP_VERSION::SinkMetadata;
44 using ::android::hardware::audio::common::CPP_VERSION::SourceMetadata;
45 using ::android::hardware::audio::CPP_VERSION::IDevice;
46 using ::android::hardware::audio::CPP_VERSION::ParameterValue;
47 using ::android::hardware::audio::CPP_VERSION::Result;
48 
49 #if MAJOR_VERSION >= 7
50 using ::android::hardware::audio::CPP_VERSION::AudioInOutFlag;
51 #else
52 using ::android::hardware::audio::common::CPP_VERSION::AudioInputFlag;
53 using ::android::hardware::audio::common::CPP_VERSION::AudioOutputFlag;
54 #endif
55 
56 class BusStreamProvider;
57 struct ServiceConfig;
58 
59 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
60 class DeviceImpl : public android::hardware::audio::V7_1::IDevice {
61 #else
62 class DeviceImpl : public IDevice {
63 #endif
64  public:
65   DeviceImpl(BusStreamProvider& busStreamProvider, const ServiceConfig& config);
66 
67   // Methods from ::android::hardware::audio::V5_0::IDevice follow.
68   Return<Result> initCheck() override;
69   Return<Result> setMasterVolume(float volume) override;
70   Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override;
71   Return<Result> setMicMute(bool mute) override;
72   Return<void> getMicMute(getMicMute_cb _hidl_cb) override;
73   Return<Result> setMasterMute(bool mute) override;
74   Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override;
75   Return<void> getInputBufferSize(const AudioConfig& config,
76                                   getInputBufferSize_cb _hidl_cb) override;
77 
78 #if MAJOR_VERSION >= 7
79   Return<void> openOutputStream(int32_t ioHandle, const DeviceAddress& device,
80                                 const AudioConfig& config,
81                                 const hidl_vec<AudioInOutFlag>& flags,
82                                 const SourceMetadata& sourceMetadata,
83                                 openOutputStream_cb _hidl_cb) override;
84   Return<void> openInputStream(int32_t ioHandle, const DeviceAddress& device,
85                                const AudioConfig& config,
86                                const hidl_vec<AudioInOutFlag>& flags,
87                                const SinkMetadata& sinkMetadata,
88                                openInputStream_cb _hidl_cb) override;
89 #else
90   Return<void> openOutputStream(int32_t ioHandle, const DeviceAddress& device,
91                                 const AudioConfig& config,
92                                 hidl_bitfield<AudioOutputFlag> flags,
93                                 const SourceMetadata& sourceMetadata,
94                                 openOutputStream_cb _hidl_cb) override;
95   Return<void> openInputStream(int32_t ioHandle, const DeviceAddress& device,
96                                const AudioConfig& config,
97                                hidl_bitfield<AudioInputFlag> flags,
98                                const SinkMetadata& sinkMetadata,
99                                openInputStream_cb _hidl_cb) override;
100 #endif
101 
102   Return<bool> supportsAudioPatches() override;
103   Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
104                                 const hidl_vec<AudioPortConfig>& sinks,
105                                 createAudioPatch_cb _hidl_cb) override;
106   Return<Result> releaseAudioPatch(AudioPatchHandle patch) override;
107   Return<void> getAudioPort(const AudioPort& port,
108                             getAudioPort_cb _hidl_cb) override;
109   Return<Result> setAudioPortConfig(const AudioPortConfig& config) override;
110   Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override;
111   Return<Result> setScreenState(bool turnedOn) override;
112   Return<void> getParameters(const hidl_vec<ParameterValue>& context,
113                              const hidl_vec<hidl_string>& keys,
114                              getParameters_cb _hidl_cb) override;
115   Return<Result> setParameters(
116       const hidl_vec<ParameterValue>& context,
117       const hidl_vec<ParameterValue>& parameters) override;
118   Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override;
119   Return<Result> setConnectedState(const DeviceAddress& address,
120                                    bool connected) override;
121 
122 #if MAJOR_VERSION >= 6
123   Return<void> updateAudioPatch(AudioPatchHandle previousPatch,
124                                 const hidl_vec<AudioPortConfig>& sources,
125                                 const hidl_vec<AudioPortConfig>& sinks,
126                                 updateAudioPatch_cb _hidl_cb) override;
127   Return<Result> close() override;
128   Return<Result> addDeviceEffect(AudioPortHandle device,
129                                  uint64_t effectId) override;
130   Return<Result> removeDeviceEffect(AudioPortHandle device,
131                                     uint64_t effectId) override;
132 #endif
133 
134 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
135   Return<void> openOutputStream_7_1(int32_t ioHandle,
136                                     const DeviceAddress& device,
137                                     const AudioConfig& config,
138                                     const hidl_vec<AudioInOutFlag>& flags,
139                                     const SourceMetadata& sourceMetadata,
140                                     openOutputStream_7_1_cb _hidl_cb) override;
141   Return<Result> setConnectedState_7_1(const AudioPort& devicePort,
142                                        bool connected) override;
143 #endif
144 
145  private:
146 #if MAJOR_VERSION >= 7
147   template<typename CallbackType>
148   Return<void> openOutputStreamImpl(int32_t ioHandle,
149                                     const DeviceAddress& device,
150                                     const AudioConfig& config,
151                                     const hidl_vec<AudioInOutFlag>& flags,
152                                     const SourceMetadata& sourceMetadata,
153                                     CallbackType _hidl_cb);
154 #endif
155 
156   BusStreamProvider& mBusStreamProvider;
157   const ServiceConfig& mServiceConfig;
158   std::set<AudioPatchHandle> mAudioPatchHandles;
159 };
160 
161 }  // namespace service
162 }  // namespace audio_proxy
163