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 <android/hardware/audio/6.0/IPrimaryDevice.h>
19 #include <mutex>
20 #include <unordered_map>
21 #include <unordered_set>
22 
23 namespace android {
24 namespace hardware {
25 namespace audio {
26 namespace V6_0 {
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::V6_0;
36 using namespace ::android::hardware::audio::V6_0;
37 
38 struct StreamIn;
39 struct StreamOut;
40 
41 struct PrimaryDevice : public IPrimaryDevice {
42     PrimaryDevice();
43 
44     Return<Result> initCheck() override;
45     Return<Result> setMasterVolume(float volume) override;
46     Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override;
47     Return<Result> setMicMute(bool mute) override;
48     Return<void> getMicMute(getMicMute_cb _hidl_cb) override;
49     Return<Result> setMasterMute(bool mute) override;
50     Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override;
51     Return<void> getInputBufferSize(const AudioConfig& config,
52                                     getInputBufferSize_cb _hidl_cb) override;
53     Return<void> openOutputStream(int32_t ioHandle,
54                                   const DeviceAddress& device,
55                                   const AudioConfig& config,
56                                   hidl_bitfield<AudioOutputFlag> flags,
57                                   const SourceMetadata& sourceMetadata,
58                                   openOutputStream_cb _hidl_cb) override;
59     Return<void> openInputStream(int32_t ioHandle,
60                                  const DeviceAddress& device,
61                                  const AudioConfig& config,
62                                  hidl_bitfield<AudioInputFlag> flags,
63                                  const SinkMetadata& sinkMetadata,
64                                  openInputStream_cb _hidl_cb) override;
65     Return<bool> supportsAudioPatches() override;
66     Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
67                                   const hidl_vec<AudioPortConfig>& sinks,
68                                   createAudioPatch_cb _hidl_cb) override;
69     Return<Result> releaseAudioPatch(AudioPatchHandle patch) override;
70     Return<void> getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) override;
71     Return<Result> setAudioPortConfig(const AudioPortConfig& config) override;
72     Return<Result> setScreenState(bool turnedOn) override;
73     Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override;
74     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
75                                const hidl_vec<hidl_string>& keys,
76                                getParameters_cb _hidl_cb) override;
77     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
78                                  const hidl_vec<ParameterValue>& parameters) override;
79     Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override;
80     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
81     Return<Result> close() override;
82     Return<Result> addDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
83     Return<Result> removeDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
84     Return<Result> setVoiceVolume(float volume) override;
85     Return<Result> setMode(AudioMode mode) override;
86     Return<Result> setBtScoHeadsetDebugName(const hidl_string& name) override;
87     Return<void> getBtScoNrecEnabled(getBtScoNrecEnabled_cb _hidl_cb) override;
88     Return<Result> setBtScoNrecEnabled(bool enabled) override;
89     Return<void> getBtScoWidebandEnabled(getBtScoWidebandEnabled_cb _hidl_cb) override;
90     Return<Result> setBtScoWidebandEnabled(bool enabled) override;
91     Return<void> getTtyMode(getTtyMode_cb _hidl_cb) override;
92     Return<Result> setTtyMode(IPrimaryDevice::TtyMode mode) override;
93     Return<void> getHacEnabled(getHacEnabled_cb _hidl_cb) override;
94     Return<Result> setHacEnabled(bool enabled) override;
95     Return<void> getBtHfpEnabled(getBtHfpEnabled_cb _hidl_cb) override;
96     Return<Result> setBtHfpEnabled(bool enabled) override;
97     Return<Result> setBtHfpSampleRate(uint32_t sampleRateHz) override;
98     Return<Result> setBtHfpVolume(float volume) override;
99     Return<Result> updateRotation(IPrimaryDevice::Rotation rotation) override;
100 
101 private:
102     friend StreamIn;
103     friend StreamOut;
104 
105     void unrefDevice(StreamIn *);
106     void unrefDevice(StreamOut *);
107     void updateOutputStreamVolume(float masterVolume) const;
108     void updateInputStreamMicMute(bool micMute) const;
109 
110     struct AudioPatch {
111         AudioPortConfig source;
112         AudioPortConfig sink;
113     };
114 
115     AudioPatchHandle    mNextAudioPatchHandle = 0;
116     std::unordered_map<AudioPatchHandle, AudioPatch> mAudioPatches;
117 
118     std::unordered_set<StreamIn *>  mInputStreams;  // requires mMutex
119     std::unordered_set<StreamOut *> mOutputStreams; // requires mMutex
120     mutable std::mutex mMutex;
121 
122     float  mMasterVolume = 1.0f;
123     bool   mMasterMute = false;
124     bool   mMicMute = false;
125 };
126 
127 }  // namespace implementation
128 }  // namespace V6_0
129 }  // namespace audio
130 }  // namespace hardware
131 }  // namespace android
132