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 | |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 54 | uint64_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 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 59 | // Scale from HAL sample rate to application rate. |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 60 | *frames = halPosition / mRateMultiplier; |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 61 | |
| 62 | return status; |
| 63 | } |
| 64 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 65 | status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp) |
| 66 | { |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 67 | if (stream == nullptr) { |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 68 | return NO_INIT; |
| 69 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 70 | |
| 71 | uint64_t halPosition = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 72 | const status_t status = stream->getPresentationPosition(&halPosition, timestamp); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 73 | if (status != NO_ERROR) { |
| 74 | return status; |
| 75 | } |
| 76 | |
Andy Hung | 5bbd8ed | 2024-02-20 13:57:02 -0800 | [diff] [blame] | 77 | if (mHalFormatHasProportionalFrames && |
| 78 | (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) { |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 79 | // For DirectTrack reset position to 0 on standby. |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 80 | const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ? |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 81 | 0 : (halPosition - mFramesWrittenAtStandby); |
| 82 | // Scale from HAL sample rate to application rate. |
| 83 | *frames = adjustedPosition / mRateMultiplier; |
| 84 | } else { |
Andy Hung | 5bbd8ed | 2024-02-20 13:57:02 -0800 | [diff] [blame] | 85 | // For offloaded MP3 and other compressed formats, and linear PCM. |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 86 | *frames = halPosition; |
| 87 | } |
| 88 | |
| 89 | return status; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | status_t AudioStreamOut::open( |
| 93 | audio_io_handle_t handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 94 | audio_devices_t deviceType, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 95 | struct audio_config *config, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 96 | const char *address, |
| 97 | const std::vector<playback_track_metadata_v7_t>& sourceMetadata) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 98 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 99 | sp<StreamOutHalInterface> outStream; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 100 | |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 101 | const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 102 | ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO) |
| 103 | : flags; |
| 104 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 105 | int status = hwDev()->openOutputStream( |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 106 | handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 107 | deviceType, |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 108 | customFlags, |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 109 | config, |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 110 | address, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 111 | &outStream, |
| 112 | sourceMetadata); |
Dean Wheatley | 6c00951 | 2023-10-23 09:34:14 +1100 | [diff] [blame] | 113 | ALOGV("AudioStreamOut::open(), HAL returned stream %p, sampleRate %d, format %#x," |
| 114 | " channelMask %#x, status %d", outStream.get(), config->sample_rate, config->format, |
| 115 | config->channel_mask, status); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 116 | |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 117 | // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare |
| 118 | // it as PCM then it will probably work. |
| 119 | if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) { |
| 120 | struct audio_config customConfig = *config; |
| 121 | customConfig.format = AUDIO_FORMAT_PCM_16_BIT; |
| 122 | |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 123 | status = hwDev()->openOutputStream( |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 124 | handle, |
jiabin | 4381040 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 125 | deviceType, |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 126 | customFlags, |
| 127 | &customConfig, |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 128 | address, |
Haofan Wang | b75aa6a | 2024-07-09 23:06:58 -0700 | [diff] [blame] | 129 | &outStream, |
| 130 | sourceMetadata); |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 131 | ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status); |
| 132 | } |
| 133 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 134 | if (status == NO_ERROR) { |
| 135 | stream = outStream; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 136 | mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 137 | status = stream->getFrameSize(&mHalFrameSize); |
Hayden Gomes | 1a89ab3 | 2020-06-12 11:05:47 -0700 | [diff] [blame] | 138 | 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] | 139 | 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] | 140 | " zero", mHalFrameSize); |
| 141 | |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | return status; |
| 145 | } |
| 146 | |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 147 | audio_config_base_t AudioStreamOut::getAudioProperties() const |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 148 | { |
Mikhail Naganov | 560637e | 2021-03-31 22:40:13 +0000 | [diff] [blame] | 149 | audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER; |
| 150 | if (stream->getAudioProperties(&result) != OK) { |
| 151 | result.sample_rate = 0; |
| 152 | result.channel_mask = AUDIO_CHANNEL_INVALID; |
| 153 | result.format = AUDIO_FORMAT_INVALID; |
| 154 | } |
| 155 | return result; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | int AudioStreamOut::flush() |
| 159 | { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 160 | mFramesWritten = 0; |
| 161 | mFramesWrittenAtStandby = 0; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 162 | const status_t result = stream->flush(); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 163 | return result != INVALID_OPERATION ? result : NO_ERROR; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | int AudioStreamOut::standby() |
| 167 | { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 168 | mFramesWrittenAtStandby = mFramesWritten; |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 169 | return stream->standby(); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Mikhail Naganov | 0ea58fe | 2024-05-10 13:30:40 -0700 | [diff] [blame] | 172 | void AudioStreamOut::presentationComplete() { |
| 173 | stream->presentationComplete(); |
| 174 | } |
| 175 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 176 | ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 177 | { |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 178 | size_t bytesWritten; |
Andy Hung | 1ef7738 | 2023-06-15 14:50:18 -0700 | [diff] [blame] | 179 | const status_t result = stream->write(buffer, numBytes, &bytesWritten); |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 180 | if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 181 | mFramesWritten += bytesWritten / mHalFrameSize; |
| 182 | } |
Mikhail Naganov | 1dc9867 | 2016-08-18 17:50:29 -0700 | [diff] [blame] | 183 | return result == OK ? bytesWritten : result; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | } // namespace android |