Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 1 | /* |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 2 | * |
| 3 | * Copyright 2007, The Android Open Source Project |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 17 | |
| 18 | #define LOG_TAG "AudioHwDevice" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 21 | #include <system/audio.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 22 | #include <utils/Log.h> |
| 23 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 24 | #include <audio_utils/spdif/SPDIFDecoder.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 25 | #include <audio_utils/spdif/SPDIFEncoder.h> |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 26 | #include <media/AudioResamplerPublic.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 27 | |
| 28 | #include "AudioHwDevice.h" |
| 29 | #include "AudioStreamOut.h" |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 30 | #include "SpdifStreamIn.h" |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 31 | #include "SpdifStreamOut.h" |
| 32 | |
| 33 | namespace android { |
| 34 | |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 35 | using media::audio::common::AudioMMapPolicyInfo; |
| 36 | using media::audio::common::AudioMMapPolicyType; |
| 37 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | status_t AudioHwDevice::openOutputStream( |
| 41 | AudioStreamOut **ppStreamOut, |
| 42 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 43 | audio_devices_t deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 44 | audio_output_flags_t flags, |
| 45 | struct audio_config *config, |
| 46 | const char *address) |
| 47 | { |
| 48 | |
| 49 | struct audio_config originalConfig = *config; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 50 | auto outputStream = new AudioStreamOut(this, flags); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 51 | |
| 52 | // Try to open the HAL first using the current format. |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 53 | ALOGV("openOutputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate, |
| 54 | config->format, config->channel_mask); |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 55 | status_t status = outputStream->open(handle, deviceType, config, address); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 56 | |
| 57 | if (status != NO_ERROR) { |
| 58 | delete outputStream; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 59 | outputStream = nullptr; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 60 | |
| 61 | // FIXME Look at any modification to the config. |
| 62 | // The HAL might modify the config to suggest a wrapped format. |
| 63 | // Log this so we can see what the HALs are doing. |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 64 | ALOGI("openOutputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x," |
| 65 | " status %d", config->sample_rate, config->format, config->channel_mask, status); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 66 | |
| 67 | // If the data is encoded then try again using wrapped PCM. |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 68 | const bool wrapperNeeded = !audio_has_proportional_frames(originalConfig.format) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 69 | && ((flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) |
| 70 | && ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0); |
| 71 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 72 | if (wrapperNeeded) { |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 73 | if (SPDIFEncoder::isFormatSupported(originalConfig.format)) { |
| 74 | outputStream = new SpdifStreamOut(this, flags, originalConfig.format); |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 75 | status = outputStream->open(handle, deviceType, &originalConfig, address); |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 76 | if (status != NO_ERROR) { |
| 77 | ALOGE("ERROR - openOutputStream(), SPDIF open returned %d", |
| 78 | status); |
| 79 | delete outputStream; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 80 | outputStream = nullptr; |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 81 | } |
| 82 | } else { |
| 83 | ALOGE("ERROR - openOutputStream(), SPDIFEncoder does not support format 0x%08x", |
| 84 | originalConfig.format); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | *ppStreamOut = outputStream; |
| 90 | return status; |
| 91 | } |
| 92 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame^] | 93 | status_t AudioHwDevice::openInputStream( |
| 94 | AudioStreamIn **ppStreamIn, |
| 95 | audio_io_handle_t handle, |
| 96 | audio_devices_t deviceType, |
| 97 | audio_input_flags_t flags, |
| 98 | struct audio_config *config, |
| 99 | const char *address, |
| 100 | audio_source_t source, |
| 101 | audio_devices_t outputDevice, |
| 102 | const char *outputDeviceAddress) { |
| 103 | |
| 104 | struct audio_config originalConfig = *config; |
| 105 | auto inputStream = new AudioStreamIn(this, flags); |
| 106 | |
| 107 | // Try to open the HAL first using the current format. |
| 108 | ALOGV("openInputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate, |
| 109 | config->format, config->channel_mask); |
| 110 | status_t status = inputStream->open(handle, deviceType, config, address, source, outputDevice, |
| 111 | outputDeviceAddress); |
| 112 | |
| 113 | // If the input could not be opened with the requested parameters and we can handle the |
| 114 | // conversion internally, try to open again with the proposed parameters. |
| 115 | if (status == BAD_VALUE && |
| 116 | audio_is_linear_pcm(originalConfig.format) && |
| 117 | audio_is_linear_pcm(config->format) && |
| 118 | (config->sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
| 119 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_LIMIT) && |
| 120 | (audio_channel_count_from_in_mask(originalConfig.channel_mask) <= FCC_LIMIT)) { |
| 121 | // FIXME describe the change proposed by HAL (save old values so we can log them here) |
| 122 | ALOGV("openInputStream() reopening with proposed sampling rate and channel mask"); |
| 123 | status = inputStream->open(handle, deviceType, config, address, source, |
| 124 | outputDevice, outputDeviceAddress); |
| 125 | // FIXME log this new status; HAL should not propose any further changes |
| 126 | if (status != NO_ERROR) { |
| 127 | delete inputStream; |
| 128 | inputStream = nullptr; |
| 129 | } |
| 130 | } else if (status != NO_ERROR) { |
| 131 | delete inputStream; |
| 132 | inputStream = nullptr; |
| 133 | |
| 134 | // FIXME Look at any modification to the config. |
| 135 | // The HAL might modify the config to suggest a wrapped format. |
| 136 | // Log this so we can see what the HALs are doing. |
| 137 | ALOGI("openInputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x," |
| 138 | " status %d", config->sample_rate, config->format, config->channel_mask, status); |
| 139 | |
| 140 | // If the data is encoded then try again using wrapped PCM. |
| 141 | const bool unwrapperNeeded = !audio_has_proportional_frames(originalConfig.format) |
| 142 | && ((flags & AUDIO_INPUT_FLAG_DIRECT) != 0); |
| 143 | |
| 144 | if (unwrapperNeeded) { |
| 145 | if (SPDIFDecoder::isFormatSupported(originalConfig.format)) { |
| 146 | inputStream = new SpdifStreamIn(this, flags, originalConfig.format); |
| 147 | status = inputStream->open(handle, deviceType, &originalConfig, address, source, |
| 148 | outputDevice, outputDeviceAddress); |
| 149 | if (status != NO_ERROR) { |
| 150 | ALOGE("ERROR - openInputStream(), SPDIF open returned %d", |
| 151 | status); |
| 152 | delete inputStream; |
| 153 | inputStream = nullptr; |
| 154 | } |
| 155 | } else { |
| 156 | ALOGE("ERROR - openInputStream(), SPDIFDecoder does not support format 0x%08x", |
| 157 | originalConfig.format); |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | *ppStreamIn = inputStream; |
| 163 | return status; |
| 164 | } |
| 165 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 166 | bool AudioHwDevice::supportsAudioPatches() const { |
| 167 | bool result; |
| 168 | return mHwDevice->supportsAudioPatches(&result) == OK ? result : false; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 169 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 170 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 171 | status_t AudioHwDevice::getAudioPort(struct audio_port_v7 *port) const { |
| 172 | return mHwDevice->getAudioPort(port); |
| 173 | } |
| 174 | |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 175 | status_t AudioHwDevice::getMmapPolicyInfos( |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 176 | AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) const { |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 177 | return mHwDevice->getMmapPolicyInfos(policyType, policyInfos); |
| 178 | } |
| 179 | |
jiabin | e504e7b | 2021-09-18 00:27:08 +0000 | [diff] [blame] | 180 | int32_t AudioHwDevice::getAAudioMixerBurstCount() const { |
| 181 | return mHwDevice->getAAudioMixerBurstCount(); |
| 182 | } |
| 183 | |
| 184 | int32_t AudioHwDevice::getAAudioHardwareBurstMinUsec() const { |
| 185 | return mHwDevice->getAAudioHardwareBurstMinUsec(); |
| 186 | } |
| 187 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 188 | status_t AudioHwDevice::getAudioMixPort(const struct audio_port_v7 *devicePort, |
| 189 | struct audio_port_v7 *mixPort) const { |
| 190 | return mHwDevice->getAudioMixPort(devicePort, mixPort); |
| 191 | } |
| 192 | |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 193 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 194 | }; // namespace android |