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 2015, 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 "AudioFlinger" |
| 19 | //#define LOG_NDEBUG 0 |
| 20 | |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 21 | #include "AudioStreamOut.h" |
| 22 | |
Mikhail Naganov | a0c9133 | 2016-09-19 10:01:12 -0700 | [diff] [blame] | 23 | #include <media/audiohal/DeviceHalInterface.h> |
| 24 | #include <media/audiohal/StreamHalInterface.h> |
Mikhail Naganov | cbc8f61 | 2016-10-11 18:05:13 -0700 | [diff] [blame] | 25 | #include <system/audio.h> |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 26 | #include <utils/Log.h> |
| 27 | |
| 28 | #include "AudioHwDevice.h" |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | // ---------------------------------------------------------------------------- |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 33 | AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags) |
| 34 | : audioHwDev(dev) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 35 | , flags(flags) |
| 36 | { |
| 37 | } |
| 38 | |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 39 | // This must be defined here together with the HAL includes above and |
| 40 | // not solely in the header. |
| 41 | AudioStreamOut::~AudioStreamOut() = default; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 42 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 43 | sp<DeviceHalInterface> AudioStreamOut::hwDev() const |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 44 | { |
| 45 | return audioHwDev->hwDevice(); |
| 46 | } |
| 47 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 48 | status_t AudioStreamOut::getRenderPosition(uint64_t *frames) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 49 | { |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 50 | if (stream == nullptr) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 51 | return NO_INIT; |
| 52 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 53 | |
| 54 | uint32_t halPosition = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 55 | const status_t status = stream->getRenderPosition(&halPosition); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 56 | if (status != NO_ERROR) { |
| 57 | return status; |
| 58 | } |
| 59 | |
| 60 | // Maintain a 64-bit render position using the 32-bit result from the HAL. |
| 61 | // This delta calculation relies on the arithmetic overflow behavior |
| 62 | // of integers. For example (100 - 0xFFFFFFF0) = 116. |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 63 | const auto truncatedPosition = (uint32_t)mRenderPosition; |
Phil Burk | ab3c6a5 | 2019-02-01 09:52:15 -0800 | [diff] [blame] | 64 | int32_t deltaHalPosition; // initialization not needed, overwitten by __builtin_sub_overflow() |
| 65 | (void) __builtin_sub_overflow(halPosition, truncatedPosition, &deltaHalPosition); |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 66 | |
David Li | dafa723 | 2024-04-11 06:08:45 +0800 | [diff] [blame^] | 67 | if (deltaHalPosition >= 0) { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 68 | mRenderPosition += deltaHalPosition; |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 69 | } else if (mExpectRetrograde) { |
| 70 | mExpectRetrograde = false; |
| 71 | mRenderPosition -= static_cast<uint64_t>(-deltaHalPosition); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 72 | } |
| 73 | // Scale from HAL sample rate to application rate. |
| 74 | *frames = mRenderPosition / mRateMultiplier; |
| 75 | |
| 76 | return status; |
| 77 | } |
| 78 | |
| 79 | // return bottom 32-bits of the render position |
| 80 | status_t AudioStreamOut::getRenderPosition(uint32_t *frames) |
| 81 | { |
| 82 | uint64_t position64 = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 83 | const status_t status = getRenderPosition(&position64); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 84 | if (status == NO_ERROR) { |
| 85 | *frames = (uint32_t)position64; |
| 86 | } |
| 87 | return status; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp) |
| 91 | { |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 92 | if (stream == nullptr) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 93 | return NO_INIT; |
| 94 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 95 | |
| 96 | uint64_t halPosition = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 97 | const status_t status = stream->getPresentationPosition(&halPosition, timestamp); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 98 | if (status != NO_ERROR) { |
| 99 | return status; |
| 100 | } |
| 101 | |
Andy Hung | 5bbd8ed | 2024-02-20 13:57:02 -0800 | [diff] [blame] | 102 | if (mHalFormatHasProportionalFrames && |
| 103 | (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) { |
| 104 | // For DirectTrack reset timestamp to 0 on standby. |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 105 | const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ? |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 106 | 0 : (halPosition - mFramesWrittenAtStandby); |
| 107 | // Scale from HAL sample rate to application rate. |
| 108 | *frames = adjustedPosition / mRateMultiplier; |
| 109 | } else { |
Andy Hung | 5bbd8ed | 2024-02-20 13:57:02 -0800 | [diff] [blame] | 110 | // For offloaded MP3 and other compressed formats, and linear PCM. |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 111 | *frames = halPosition; |
| 112 | } |
| 113 | |
| 114 | return status; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | status_t AudioStreamOut::open( |
| 118 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 119 | audio_devices_t deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 120 | struct audio_config *config, |
| 121 | const char *address) |
| 122 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 123 | sp<StreamOutHalInterface> outStream; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 124 | |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 125 | const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 126 | ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO) |
| 127 | : flags; |
| 128 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 129 | int status = hwDev()->openOutputStream( |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 130 | handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 131 | deviceType, |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 132 | customFlags, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 133 | config, |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 134 | address, |
| 135 | &outStream); |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 136 | ALOGV("AudioStreamOut::open(), HAL returned stream %p, sampleRate %d, format %#x," |
| 137 | " channelMask %#x, status %d", outStream.get(), config->sample_rate, config->format, |
| 138 | config->channel_mask, status); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 139 | |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 140 | // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare |
| 141 | // it as PCM then it will probably work. |
| 142 | if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) { |
| 143 | struct audio_config customConfig = *config; |
| 144 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 145 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 146 | status = hwDev()->openOutputStream( |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 147 | handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 148 | deviceType, |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 149 | customFlags, |
| 150 | &customConfig, |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 151 | address, |
| 152 | &outStream); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 153 | ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status); |
| 154 | } |
| 155 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 156 | if (status == NO_ERROR) { |
| 157 | stream = outStream; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 158 | mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 159 | status = stream->getFrameSize(&mHalFrameSize); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 160 | LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status); |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 161 | LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than" |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 162 | " zero", mHalFrameSize); |
| 163 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return status; |
| 167 | } |
| 168 | |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 169 | audio_config_base_t AudioStreamOut::getAudioProperties() const |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 170 | { |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 171 | audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER; |
| 172 | if (stream->getAudioProperties(&result) != OK) { |
| 173 | result.sample_rate = 0; |
| 174 | result.channel_mask = AUDIO_CHANNEL_INVALID; |
| 175 | result.format = AUDIO_FORMAT_INVALID; |
| 176 | } |
| 177 | return result; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | int AudioStreamOut::flush() |
| 181 | { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 182 | mRenderPosition = 0; |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 183 | mExpectRetrograde = false; |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 184 | mFramesWritten = 0; |
| 185 | mFramesWrittenAtStandby = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 186 | const status_t result = stream->flush(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 187 | return result != INVALID_OPERATION ? result : NO_ERROR; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | int AudioStreamOut::standby() |
| 191 | { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 192 | mRenderPosition = 0; |
Atneya Nair | 0cae043 | 2022-05-10 18:12:12 -0400 | [diff] [blame] | 193 | mExpectRetrograde = false; |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 194 | mFramesWrittenAtStandby = mFramesWritten; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 195 | return stream->standby(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 198 | ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 199 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 200 | size_t bytesWritten; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 201 | const status_t result = stream->write(buffer, numBytes, &bytesWritten); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 202 | if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 203 | mFramesWritten += bytesWritten / mHalFrameSize; |
| 204 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 205 | return result == OK ? bytesWritten : result; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | } // namespace android |