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