1 /*
2  * Copyright (C) 2018 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_AUDIO_DEVICE_H
18 #define ANDROID_HARDWARE_AUDIO_DEVICE_H
19 
20 #include PATH(android/hardware/audio/FILE_VERSION/IDevice.h)
21 
22 #include "ParametersUtil.h"
23 
24 #include <memory>
25 
26 #include <hardware/audio.h>
27 #include <media/AudioParameter.h>
28 
29 #include <hidl/Status.h>
30 
31 #include <hidl/MQDescriptor.h>
32 
33 #include <VersionUtils.h>
34 #include <util/CoreUtils.h>
35 
36 namespace android {
37 namespace hardware {
38 namespace audio {
39 namespace CPP_VERSION {
40 namespace implementation {
41 
42 using ::android::sp;
43 using ::android::hardware::hidl_string;
44 using ::android::hardware::hidl_vec;
45 using ::android::hardware::Return;
46 using ::android::hardware::Void;
47 using namespace ::android::hardware::audio::common::CPP_VERSION;
48 using namespace ::android::hardware::audio::CPP_VERSION;
49 using AudioInputFlags = CoreUtils::AudioInputFlags;
50 using AudioOutputFlags = CoreUtils::AudioOutputFlags;
51 
52 struct Device : public IDevice, public ParametersUtil {
53     explicit Device(audio_hw_device_t* device);
54 
55     // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow.
56     Return<Result> initCheck() override;
57     Return<Result> setMasterVolume(float volume) override;
58     Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override;
59     Return<Result> setMicMute(bool mute) override;
60     Return<void> getMicMute(getMicMute_cb _hidl_cb) override;
61     Return<Result> setMasterMute(bool mute) override;
62     Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override;
63     Return<void> getInputBufferSize(const AudioConfig& config,
64                                     getInputBufferSize_cb _hidl_cb) override;
65 
66     std::tuple<Result, sp<IStreamOut>> openOutputStreamImpl(int32_t ioHandle,
67                                                             const DeviceAddress& device,
68                                                             const AudioConfig& config,
69                                                             const AudioOutputFlags& flags,
70                                                             AudioConfig* suggestedConfig);
71     std::tuple<Result, sp<IStreamIn>> openInputStreamImpl(
72             int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config,
73             const AudioInputFlags& flags, AudioSource source, AudioConfig* suggestedConfig);
74 
75     Return<void> openOutputStream(int32_t ioHandle, const DeviceAddress& device,
76                                   const AudioConfig& config,
77 #if MAJOR_VERSION <= 6
78                                   AudioOutputFlags flags,
79 #else
80                                   const AudioOutputFlags& flags,
81 #endif
82 #if MAJOR_VERSION >= 4
83                                   const SourceMetadata& sourceMetadata,
84 #endif
85                                   openOutputStream_cb _hidl_cb) override;
86     Return<void> openInputStream(int32_t ioHandle, const DeviceAddress& device,
87                                  const AudioConfig& config,
88 #if MAJOR_VERSION <= 6
89                                  AudioInputFlags flags,
90 #else
91                                  const AudioInputFlags& flags,
92 #endif
93 #if MAJOR_VERSION == 2
94                                  AudioSource source,
95 #elif MAJOR_VERSION >= 4
96                                  const SinkMetadata& sinkMetadata,
97 #endif
98                                  openInputStream_cb _hidl_cb) override;
99 
100     Return<bool> supportsAudioPatches() override;
101     Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources,
102                                   const hidl_vec<AudioPortConfig>& sinks,
103                                   createAudioPatch_cb _hidl_cb) override;
104     Return<Result> releaseAudioPatch(int32_t patch) override;
105     Return<void> getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) override;
106     Return<Result> setAudioPortConfig(const AudioPortConfig& config) override;
107 
108     Return<Result> setScreenState(bool turnedOn) override;
109 
110 #if MAJOR_VERSION == 2
111     Return<AudioHwSync> getHwAvSync() override;
112     Return<void> getParameters(const hidl_vec<hidl_string>& keys,
113                                getParameters_cb _hidl_cb) override;
114     Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override;
115     Return<void> debugDump(const hidl_handle& fd) override;
116 #elif MAJOR_VERSION >= 4
117     Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override;
118     Return<void> getParameters(const hidl_vec<ParameterValue>& context,
119                                const hidl_vec<hidl_string>& keys,
120                                getParameters_cb _hidl_cb) override;
121     Return<Result> setParameters(const hidl_vec<ParameterValue>& context,
122                                  const hidl_vec<ParameterValue>& parameters) override;
123     Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override;
124     Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override;
125 #endif
126 #if MAJOR_VERSION >= 6
127     Return<Result> close() override;
128     Return<Result> addDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
129     Return<Result> removeDeviceEffect(AudioPortHandle device, uint64_t effectId) override;
130     Return<void> updateAudioPatch(int32_t previousPatch, const hidl_vec<AudioPortConfig>& sources,
131                                   const hidl_vec<AudioPortConfig>& sinks,
132                                   createAudioPatch_cb _hidl_cb) override;
133 #endif
134     Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override;
135 
136     // Utility methods for extending interfaces.
137     Result analyzeStatus(const char* funcName, int status,
138                          const std::vector<int>& ignoreErrors = {});
139     void closeInputStream(audio_stream_in_t* stream);
140     void closeOutputStream(audio_stream_out_t* stream);
deviceDevice141     audio_hw_device_t* device() const { return mDevice; }
142 
versionDevice143     uint32_t version() const { return mDevice->common.version; }
144 
145   private:
146     bool mIsClosed;
147     audio_hw_device_t* mDevice;
148     int mOpenedStreamsCount = 0;
149 
150     virtual ~Device();
151 
152     Result doClose();
153     std::tuple<Result, AudioPatchHandle> createOrUpdateAudioPatch(
154             AudioPatchHandle patch, const hidl_vec<AudioPortConfig>& sources,
155             const hidl_vec<AudioPortConfig>& sinks);
156     template <typename HalPort>
157     Return<void> getAudioPortImpl(const AudioPort& port, getAudioPort_cb _hidl_cb,
158                                   int (*halGetter)(audio_hw_device_t*, HalPort*),
159                                   const char* halGetterName);
160 
161     // Methods from ParametersUtil.
162     char* halGetParameters(const char* keys) override;
163     int halSetParameters(const char* keysAndValues) override;
164 };
165 
166 }  // namespace implementation
167 }  // namespace CPP_VERSION
168 }  // namespace audio
169 }  // namespace hardware
170 }  // namespace android
171 
172 #endif  // ANDROID_HARDWARE_AUDIO_DEVICE_H
173