1 /*
2  * Copyright (C) 2020 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 #pragma once
18 #include <mutex>
19 #include <unordered_map>
20 #include <unordered_set>
21 #include PATH(android/hardware/audio/FILE_VERSION/IPrimaryDevice.h)
22 
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace CPP_VERSION {
27 namespace implementation {
28 
29 using ::android::sp;
30 using ::android::hardware::hidl_bitfield;
31 using ::android::hardware::hidl_string;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::Return;
34 
35 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
36 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION;
37 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::IStreamIn;
38 using ::android::hardware::audio::CPP_VERSION::IPrimaryDevice;
39 using ::android::hardware::audio::CPP_VERSION::IStreamOut;
40 
41 struct StreamIn;
42 struct StreamOut;
43 
44 struct Device : public IDevice {
45     Device();
46     Return<Result> initCheck() override;
47     Return<Result> setMasterVolume(float volume) override;
48     Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override;
49     Return<Result> setMicMute(bool mute) override;
50     Return<void> getMicMute(getMicMute_cb _hidl_cb) override;
51     Return<Result> setMasterMute(bool mute) override;
52     Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override;
53     Return<void> getInputBufferSize(const AudioConfig& config,
54                                     getInputBufferSize_cb _hidl_cb) override;
55     Return<void> openOutputStream(int32_t ioHandle,
56                                   const DeviceAddress& device,
57                                   const AudioConfig& config,
58                                   const hidl_vec<AudioInOutFlag>& flags,
59                                   const SourceMetadata& sourceMetadata,
60                                   openOutputStream_cb _hidl_cb) override;
61     Return<void> openInputStream(int32_t ioHandle,
62                                  const DeviceAddress& device,
63                                  const AudioConfig& config,
64                                  const hidl_vec<AudioInOutFlag>& flags,
65                                  const SinkMetadata& sinkMetadata,
66                                  openInputStream_cb _hidl_cb) override;
67     Return<bool> supportsAudioPatches() override;
68     Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
69                                   const hidl_vec<AudioPortConfig>& sinks,
70                                   createAudioPatch_cb _hidl_cb) override;
71     Return<void> updateAudioPatch(AudioPatchHandle previousPatch,
72                                   const hidl_vec<AudioPortConfig>& sources,
73                                   const hidl_vec<AudioPortConfig>& sinks,
74                                   updateAudioPatch_cb _hidl_cb) override;
75     Return<Result> releaseAudioPatch(AudioPatchHandle patch) override;
76     Return<void> getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) override;
77     Return<Result> setAudioPortConfig(const AudioPortConfig& config) override;
78     Return<Result> setScreenState(bool turnedOn) override;
79     Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override;
80     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
81                                const hidl_vec<hidl_string>& keys,
82                                getParameters_cb _hidl_cb) override;
83     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
84                                  const hidl_vec<ParameterValue>& parameters) override;
85     Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override;
86     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
87     Return<Result> close() override;
88     Return<Result> addDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
89     Return<Result> removeDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
90 
91 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
92     Return<void> openOutputStream_7_1(int32_t ioHandle, const DeviceAddress& device,
93                                       const AudioConfig& config,
94                                       const hidl_vec<AudioInOutFlag>& flags,
95                                       const SourceMetadata& sourceMetadata,
96                                       openOutputStream_7_1_cb _hidl_cb) override;
97     Return<Result> setConnectedState_7_1(const AudioPort& devicePort,
98                                          bool connected) override;
99 #endif
100 
101   private:
102     friend StreamIn;
103     friend StreamOut;
104 
105     std::tuple<Result, sp<IStreamOut>, AudioConfig> openOutputStreamImpl(
106             int32_t ioHandle, const DeviceAddress& device,
107             const AudioConfig& config, const hidl_vec<AudioInOutFlag>& flags,
108             const SourceMetadata& sourceMetadata);
109     std::tuple<Result, sp<IStreamIn>, AudioConfig> openInputStreamImpl(
110             int32_t ioHandle, const DeviceAddress& device,
111             const AudioConfig& config, const hidl_vec<AudioInOutFlag>& flags,
112             const SinkMetadata& sinkMetadata);
113     void unrefDevice(StreamIn *);
114     void unrefDevice(StreamOut *);
115     void updateOutputStreamVolume(float masterVolume) const;
116     void updateInputStreamMicMute(bool micMute) const;
117 
118     struct AudioPatch {
119         AudioPortConfig source;
120         AudioPortConfig sink;
121     };
122 
123     AudioPatchHandle    mNextAudioPatchHandle = 0;
124     std::unordered_map<AudioPatchHandle, AudioPatch> mAudioPatches;
125 
126     std::unordered_set<StreamIn *>  mInputStreams;  // requires mMutex
127     std::unordered_set<StreamOut *> mOutputStreams; // requires mMutex
128     mutable std::mutex mMutex;
129 
130     float  mMasterVolume = 1.0f;
131     bool   mMasterMute = false;
132     bool   mMicMute = false;
133 };
134 
135 struct PrimaryDevice : public IPrimaryDevice {
136     PrimaryDevice();
137 
138     // Implementation of IDevice.
139     Return<Result> initCheck() override;
140     Return<Result> setMasterVolume(float volume) override;
141     Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override;
142     Return<Result> setMicMute(bool mute) override;
143     Return<void> getMicMute(getMicMute_cb _hidl_cb) override;
144     Return<Result> setMasterMute(bool mute) override;
145     Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override;
146     Return<void> getInputBufferSize(const AudioConfig& config,
147                                     getInputBufferSize_cb _hidl_cb) override;
148     Return<void> openOutputStream(int32_t ioHandle,
149                                   const DeviceAddress& device,
150                                   const AudioConfig& config,
151                                   const hidl_vec<AudioInOutFlag>& flags,
152                                   const SourceMetadata& sourceMetadata,
153                                   openOutputStream_cb _hidl_cb) override;
154     Return<void> openInputStream(int32_t ioHandle,
155                                  const DeviceAddress& device,
156                                  const AudioConfig& config,
157                                  const hidl_vec<AudioInOutFlag>& flags,
158                                  const SinkMetadata& sinkMetadata,
159                                  openInputStream_cb _hidl_cb) override;
160     Return<bool> supportsAudioPatches() override;
161     Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
162                                   const hidl_vec<AudioPortConfig>& sinks,
163                                   createAudioPatch_cb _hidl_cb) override;
164     Return<void> updateAudioPatch(AudioPatchHandle previousPatch,
165                                   const hidl_vec<AudioPortConfig>& sources,
166                                   const hidl_vec<AudioPortConfig>& sinks,
167                                   updateAudioPatch_cb _hidl_cb) override;
168     Return<Result> releaseAudioPatch(AudioPatchHandle patch) override;
169     Return<void> getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) override;
170     Return<Result> setAudioPortConfig(const AudioPortConfig& config) override;
171     Return<Result> setScreenState(bool turnedOn) override;
172     Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override;
173     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
174                                const hidl_vec<hidl_string>& keys,
175                                getParameters_cb _hidl_cb) override;
176     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
177                                  const hidl_vec<ParameterValue>& parameters) override;
178     Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override;
179     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
180     Return<Result> close() override;
181     Return<Result> addDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
182     Return<Result> removeDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
183 
184     // Implementation of IPrimaryDevice.
185     Return<Result> setVoiceVolume(float volume) override;
186     Return<Result> setMode(AudioMode mode) override;
187     Return<Result> setBtScoHeadsetDebugName(const hidl_string& name) override;
188     Return<void> getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb) override;
189     Return<Result> setBtScoNrecEnabled(bool enabled) override;
190     Return<void> getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb) override;
191     Return<Result> setBtScoWidebandEnabled(bool enabled) override;
192     Return<void> getTtyMode(getTtyMode_cb _hidl_cb) override;
193     Return<Result> setTtyMode(IPrimaryDevice::TtyMode mode) override;
194     Return<void> getHacEnabled(getHacEnabled_cb _hidl_cb) override;
195     Return<Result> setHacEnabled(bool enabled) override;
196     Return<void> getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb) override;
197     Return<Result> setBtHfpEnabled(bool enabled) override;
198     Return<Result> setBtHfpSampleRate(uint32_t sampleRateHz) override;
199     Return<Result> setBtHfpVolume(float volume) override;
200     Return<Result> updateRotation(IPrimaryDevice::Rotation rotation) override;
201 
202 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1
getDevicePrimaryDevice203     Return<sp<::android::hardware::audio::V7_1::IDevice>> getDevice() override {
204         return mDevice;
205     }
206 #endif
207 
208 private:
209     sp<Device> mDevice;
210 };
211 
212 }  // namespace implementation
213 }  // namespace CPP_VERSION
214 }  // namespace audio
215 }  // namespace hardware
216 }  // namespace android
217