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 ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::CoreUtils; 48 using ::android::hardware::audio::CORE_TYPES_CPP_VERSION::implementation::ParametersUtil; 49 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION; 50 using namespace ::android::hardware::audio::CORE_TYPES_CPP_VERSION; 51 using namespace ::android::hardware::audio::CPP_VERSION; 52 using AudioInputFlags = CoreUtils::AudioInputFlags; 53 using AudioOutputFlags = CoreUtils::AudioOutputFlags; 54 55 struct Device : public IDevice, public ParametersUtil { 56 explicit Device(audio_hw_device_t* device); 57 58 // Methods from ::android::hardware::audio::CPP_VERSION::IDevice follow. 59 Return<Result> initCheck() override; 60 Return<Result> setMasterVolume(float volume) override; 61 Return<void> getMasterVolume(getMasterVolume_cb _hidl_cb) override; 62 Return<Result> setMicMute(bool mute) override; 63 Return<void> getMicMute(getMicMute_cb _hidl_cb) override; 64 Return<Result> setMasterMute(bool mute) override; 65 Return<void> getMasterMute(getMasterMute_cb _hidl_cb) override; 66 Return<void> getInputBufferSize(const AudioConfig& config, 67 getInputBufferSize_cb _hidl_cb) override; 68 69 std::tuple<Result, sp<IStreamOut>> openOutputStreamCore(int32_t ioHandle, 70 const DeviceAddress& device, 71 const AudioConfig& config, 72 const AudioOutputFlags& flags, 73 AudioConfig* suggestedConfig); 74 std::tuple<Result, sp<IStreamIn>> openInputStreamCore( 75 int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config, 76 const AudioInputFlags& flags, AudioSource source, AudioConfig* suggestedConfig); 77 #if MAJOR_VERSION >= 4 78 std::tuple<Result, sp<IStreamOut>, AudioConfig> openOutputStreamImpl( 79 int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config, 80 const SourceMetadata& sourceMetadata, 81 #if MAJOR_VERSION <= 6 82 AudioOutputFlags flags); 83 #else 84 const AudioOutputFlags& flags); 85 #endif 86 std::tuple<Result, sp<IStreamIn>, AudioConfig> openInputStreamImpl( 87 int32_t ioHandle, const DeviceAddress& device, const AudioConfig& config, 88 #if MAJOR_VERSION <= 6 89 AudioInputFlags flags, 90 #else 91 const AudioInputFlags& flags, 92 #endif 93 const SinkMetadata& sinkMetadata); 94 #endif // MAJOR_VERSION >= 4 95 96 Return<void> openOutputStream(int32_t ioHandle, const DeviceAddress& device, 97 const AudioConfig& config, 98 #if MAJOR_VERSION <= 6 99 AudioOutputFlags flags, 100 #else 101 const AudioOutputFlags& flags, 102 #endif 103 #if MAJOR_VERSION >= 4 104 const SourceMetadata& sourceMetadata, 105 #endif 106 openOutputStream_cb _hidl_cb) override; 107 Return<void> openInputStream(int32_t ioHandle, const DeviceAddress& device, 108 const AudioConfig& config, 109 #if MAJOR_VERSION <= 6 110 AudioInputFlags flags, 111 #else 112 const AudioInputFlags& flags, 113 #endif 114 #if MAJOR_VERSION == 2 115 AudioSource source, 116 #elif MAJOR_VERSION >= 4 117 const SinkMetadata& sinkMetadata, 118 #endif 119 openInputStream_cb _hidl_cb) override; 120 121 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 122 Return<void> openOutputStream_7_1(int32_t ioHandle, const DeviceAddress& device, 123 const AudioConfig& config, const AudioOutputFlags& flags, 124 const SourceMetadata& sourceMetadata, 125 openOutputStream_7_1_cb _hidl_cb) override; 126 #endif 127 128 Return<bool> supportsAudioPatches() override; 129 Return<void> createAudioPatch(const hidl_vec<AudioPortConfig>& sources, 130 const hidl_vec<AudioPortConfig>& sinks, 131 createAudioPatch_cb _hidl_cb) override; 132 Return<Result> releaseAudioPatch(int32_t patch) override; 133 Return<void> getAudioPort(const AudioPort& port, getAudioPort_cb _hidl_cb) override; 134 Return<Result> setAudioPortConfig(const AudioPortConfig& config) override; 135 136 Return<Result> setScreenState(bool turnedOn) override; 137 138 #if MAJOR_VERSION == 2 139 Return<AudioHwSync> getHwAvSync() override; 140 Return<void> getParameters(const hidl_vec<hidl_string>& keys, 141 getParameters_cb _hidl_cb) override; 142 Return<Result> setParameters(const hidl_vec<ParameterValue>& parameters) override; 143 Return<void> debugDump(const hidl_handle& fd) override; 144 #elif MAJOR_VERSION >= 4 145 Return<void> getHwAvSync(getHwAvSync_cb _hidl_cb) override; 146 Return<void> getParameters(const hidl_vec<ParameterValue>& context, 147 const hidl_vec<hidl_string>& keys, 148 getParameters_cb _hidl_cb) override; 149 Return<Result> setParameters(const hidl_vec<ParameterValue>& context, 150 const hidl_vec<ParameterValue>& parameters) override; 151 Return<void> getMicrophones(getMicrophones_cb _hidl_cb) override; 152 Return<Result> setConnectedState(const DeviceAddress& address, bool connected) override; 153 #endif 154 #if MAJOR_VERSION >= 6 155 Return<Result> close() override; 156 Return<Result> addDeviceEffect(AudioPortHandle device, uint64_t effectId) override; 157 Return<Result> removeDeviceEffect(AudioPortHandle device, uint64_t effectId) override; 158 Return<void> updateAudioPatch(int32_t previousPatch, const hidl_vec<AudioPortConfig>& sources, 159 const hidl_vec<AudioPortConfig>& sinks, 160 createAudioPatch_cb _hidl_cb) override; 161 #endif 162 #if MAJOR_VERSION == 7 && MINOR_VERSION == 1 163 Return<Result> setConnectedState_7_1(const AudioPort& devicePort, bool connected) override; 164 #endif 165 Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& options) override; 166 167 // Utility methods for extending interfaces. 168 Result analyzeStatus(const char* funcName, int status, 169 const std::vector<int>& ignoreErrors = {}); 170 void closeInputStream(audio_stream_in_t* stream); 171 void closeOutputStream(audio_stream_out_t* stream); deviceDevice172 audio_hw_device_t* device() const { return mDevice; } 173 versionDevice174 uint32_t version() const { return mDevice->common.version; } 175 176 private: 177 bool mIsClosed; 178 audio_hw_device_t* mDevice; 179 int mOpenedStreamsCount = 0; 180 181 virtual ~Device(); 182 183 Result doClose(); 184 std::tuple<Result, AudioPatchHandle> createOrUpdateAudioPatch( 185 AudioPatchHandle patch, const hidl_vec<AudioPortConfig>& sources, 186 const hidl_vec<AudioPortConfig>& sinks); 187 template <typename HalPort> 188 Return<void> getAudioPortImpl(const AudioPort& port, getAudioPort_cb _hidl_cb, 189 int (*halGetter)(audio_hw_device_t*, HalPort*), 190 const char* halGetterName); 191 192 // Methods from ParametersUtil. 193 char* halGetParameters(const char* keys) override; 194 int halSetParameters(const char* keysAndValues) override; 195 }; 196 197 } // namespace implementation 198 } // namespace CPP_VERSION 199 } // namespace audio 200 } // namespace hardware 201 } // namespace android 202 203 #endif // ANDROID_HARDWARE_AUDIO_DEVICE_H 204