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