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, |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 44 | audio_output_flags_t *flags, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 45 | struct audio_config *config, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 46 | const char *address, |
| 47 | const std::vector<playback_track_metadata_v7_t>& sourceMetadata) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 48 | { |
| 49 | |
| 50 | struct audio_config originalConfig = *config; |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 51 | auto outputStream = new AudioStreamOut(this); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 52 | |
| 53 | // Try to open the HAL first using the current format. |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 54 | ALOGV("openOutputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate, |
| 55 | config->format, config->channel_mask); |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 56 | status_t status = outputStream->open(handle, deviceType, config, flags, address, |
| 57 | sourceMetadata); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 58 | |
| 59 | if (status != NO_ERROR) { |
| 60 | delete outputStream; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 61 | outputStream = nullptr; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 62 | |
| 63 | // FIXME Look at any modification to the config. |
| 64 | // The HAL might modify the config to suggest a wrapped format. |
| 65 | // Log this so we can see what the HALs are doing. |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 66 | ALOGI("openOutputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x," |
| 67 | " status %d", config->sample_rate, config->format, config->channel_mask, status); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 68 | |
| 69 | // If the data is encoded then try again using wrapped PCM. |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 70 | const bool wrapperNeeded = !audio_has_proportional_frames(originalConfig.format) |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 71 | && ((*flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) |
| 72 | && ((*flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 73 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 74 | if (wrapperNeeded) { |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 75 | if (SPDIFEncoder::isFormatSupported(originalConfig.format)) { |
Dean Wheatley | dfb67b8 | 2024-01-23 09:36:29 +1100 | [diff] [blame^] | 76 | outputStream = new SpdifStreamOut(this, originalConfig.format); |
| 77 | status = outputStream->open(handle, deviceType, &originalConfig, flags, address, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 78 | sourceMetadata); |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 79 | if (status != NO_ERROR) { |
| 80 | ALOGE("ERROR - openOutputStream(), SPDIF open returned %d", |
| 81 | status); |
| 82 | delete outputStream; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 83 | outputStream = nullptr; |
Phil Burk | 23d8997 | 2015-04-06 16:22:23 -0700 | [diff] [blame] | 84 | } |
| 85 | } else { |
| 86 | ALOGE("ERROR - openOutputStream(), SPDIFEncoder does not support format 0x%08x", |
| 87 | originalConfig.format); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | *ppStreamOut = outputStream; |
| 93 | return status; |
| 94 | } |
| 95 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 96 | status_t AudioHwDevice::openInputStream( |
| 97 | AudioStreamIn **ppStreamIn, |
| 98 | audio_io_handle_t handle, |
| 99 | audio_devices_t deviceType, |
| 100 | audio_input_flags_t flags, |
| 101 | struct audio_config *config, |
| 102 | const char *address, |
| 103 | audio_source_t source, |
| 104 | audio_devices_t outputDevice, |
| 105 | const char *outputDeviceAddress) { |
| 106 | |
| 107 | struct audio_config originalConfig = *config; |
| 108 | auto inputStream = new AudioStreamIn(this, flags); |
| 109 | |
| 110 | // Try to open the HAL first using the current format. |
| 111 | ALOGV("openInputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate, |
| 112 | config->format, config->channel_mask); |
| 113 | status_t status = inputStream->open(handle, deviceType, config, address, source, outputDevice, |
| 114 | outputDeviceAddress); |
| 115 | |
| 116 | // If the input could not be opened with the requested parameters and we can handle the |
| 117 | // conversion internally, try to open again with the proposed parameters. |
| 118 | if (status == BAD_VALUE && |
| 119 | audio_is_linear_pcm(originalConfig.format) && |
| 120 | audio_is_linear_pcm(config->format) && |
| 121 | (config->sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) && |
| 122 | (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_LIMIT) && |
| 123 | (audio_channel_count_from_in_mask(originalConfig.channel_mask) <= FCC_LIMIT)) { |
| 124 | // FIXME describe the change proposed by HAL (save old values so we can log them here) |
| 125 | ALOGV("openInputStream() reopening with proposed sampling rate and channel mask"); |
| 126 | status = inputStream->open(handle, deviceType, config, address, source, |
| 127 | outputDevice, outputDeviceAddress); |
| 128 | // FIXME log this new status; HAL should not propose any further changes |
| 129 | if (status != NO_ERROR) { |
| 130 | delete inputStream; |
| 131 | inputStream = nullptr; |
| 132 | } |
| 133 | } else if (status != NO_ERROR) { |
| 134 | delete inputStream; |
| 135 | inputStream = nullptr; |
| 136 | |
| 137 | // FIXME Look at any modification to the config. |
| 138 | // The HAL might modify the config to suggest a wrapped format. |
| 139 | // Log this so we can see what the HALs are doing. |
| 140 | ALOGI("openInputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x," |
| 141 | " status %d", config->sample_rate, config->format, config->channel_mask, status); |
| 142 | |
| 143 | // If the data is encoded then try again using wrapped PCM. |
| 144 | const bool unwrapperNeeded = !audio_has_proportional_frames(originalConfig.format) |
| 145 | && ((flags & AUDIO_INPUT_FLAG_DIRECT) != 0); |
| 146 | |
| 147 | if (unwrapperNeeded) { |
| 148 | if (SPDIFDecoder::isFormatSupported(originalConfig.format)) { |
| 149 | inputStream = new SpdifStreamIn(this, flags, originalConfig.format); |
| 150 | status = inputStream->open(handle, deviceType, &originalConfig, address, source, |
| 151 | outputDevice, outputDeviceAddress); |
| 152 | if (status != NO_ERROR) { |
| 153 | ALOGE("ERROR - openInputStream(), SPDIF open returned %d", |
| 154 | status); |
| 155 | delete inputStream; |
| 156 | inputStream = nullptr; |
| 157 | } |
| 158 | } else { |
| 159 | ALOGE("ERROR - openInputStream(), SPDIFDecoder does not support format 0x%08x", |
| 160 | originalConfig.format); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | *ppStreamIn = inputStream; |
| 166 | return status; |
| 167 | } |
| 168 | |
Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 169 | bool AudioHwDevice::supportsAudioPatches() const { |
| 170 | bool result; |
| 171 | return mHwDevice->supportsAudioPatches(&result) == OK ? result : false; |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 172 | } |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 173 | |
jiabin | b4fed19 | 2020-09-22 14:45:40 -0700 | [diff] [blame] | 174 | status_t AudioHwDevice::getAudioPort(struct audio_port_v7 *port) const { |
| 175 | return mHwDevice->getAudioPort(port); |
| 176 | } |
| 177 | |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 178 | status_t AudioHwDevice::getMmapPolicyInfos( |
jiabin | e99d088 | 2021-09-17 05:21:25 +0000 | [diff] [blame] | 179 | AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) const { |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 180 | return mHwDevice->getMmapPolicyInfos(policyType, policyInfos); |
| 181 | } |
| 182 | |
jiabin | e504e7b | 2021-09-18 00:27:08 +0000 | [diff] [blame] | 183 | int32_t AudioHwDevice::getAAudioMixerBurstCount() const { |
| 184 | return mHwDevice->getAAudioMixerBurstCount(); |
| 185 | } |
| 186 | |
| 187 | int32_t AudioHwDevice::getAAudioHardwareBurstMinUsec() const { |
| 188 | return mHwDevice->getAAudioHardwareBurstMinUsec(); |
| 189 | } |
| 190 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 191 | status_t AudioHwDevice::getAudioMixPort(const struct audio_port_v7 *devicePort, |
| 192 | struct audio_port_v7 *mixPort) const { |
| 193 | return mHwDevice->getAudioMixPort(devicePort, mixPort); |
| 194 | } |
| 195 | |
Jiabin Huang | ebe6410 | 2021-09-07 20:01:07 +0000 | [diff] [blame] | 196 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 197 | }; // namespace android |