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 | |
| 21 | #include <hardware/audio.h> |
| 22 | #include <utils/Log.h> |
| 23 | |
| 24 | #include "AudioHwDevice.h" |
| 25 | #include "AudioStreamOut.h" |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 26 | #include "DeviceHalInterface.h" |
| 27 | |
| 28 | // FIXME: Remove after streams HAL is componentized |
| 29 | #include "DeviceHalLocal.h" |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // ---------------------------------------------------------------------------- |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 34 | AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags) |
| 35 | : audioHwDev(dev) |
| 36 | , stream(NULL) |
| 37 | , flags(flags) |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 38 | , mFramesWritten(0) |
| 39 | , mFramesWrittenAtStandby(0) |
| 40 | , mRenderPosition(0) |
| 41 | , mRateMultiplier(1) |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 42 | , mHalFormatHasProportionalFrames(false) |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 43 | , mHalFrameSize(0) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 47 | sp<DeviceHalInterface> AudioStreamOut::hwDev() const |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 48 | { |
| 49 | return audioHwDev->hwDevice(); |
| 50 | } |
| 51 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 52 | status_t AudioStreamOut::getRenderPosition(uint64_t *frames) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 53 | { |
| 54 | if (stream == NULL) { |
| 55 | return NO_INIT; |
| 56 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 57 | |
| 58 | uint32_t halPosition = 0; |
| 59 | status_t status = stream->get_render_position(stream, &halPosition); |
| 60 | if (status != NO_ERROR) { |
| 61 | return status; |
| 62 | } |
| 63 | |
| 64 | // Maintain a 64-bit render position using the 32-bit result from the HAL. |
| 65 | // This delta calculation relies on the arithmetic overflow behavior |
| 66 | // of integers. For example (100 - 0xFFFFFFF0) = 116. |
| 67 | uint32_t truncatedPosition = (uint32_t)mRenderPosition; |
| 68 | int32_t deltaHalPosition = (int32_t)(halPosition - truncatedPosition); |
| 69 | if (deltaHalPosition > 0) { |
| 70 | mRenderPosition += deltaHalPosition; |
| 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; |
| 82 | status_t status = getRenderPosition(&position64); |
| 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 | { |
| 91 | if (stream == NULL) { |
| 92 | return NO_INIT; |
| 93 | } |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 94 | |
| 95 | uint64_t halPosition = 0; |
| 96 | status_t status = stream->get_presentation_position(stream, &halPosition, timestamp); |
| 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) { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 104 | uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ? |
| 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, |
| 118 | audio_devices_t devices, |
| 119 | struct audio_config *config, |
| 120 | const char *address) |
| 121 | { |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 122 | audio_stream_out_t *outStream; |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 123 | |
| 124 | audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937) |
| 125 | ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO) |
| 126 | : flags; |
| 127 | |
Mikhail Naganov | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 128 | int status = static_cast<DeviceHalLocal*>(hwDev().get())->openOutputStream( |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 129 | handle, |
| 130 | devices, |
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", |
| 138 | outStream, |
| 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 | e4f1f63 | 2016-08-31 11:35:10 -0700 | [diff] [blame] | 150 | status = static_cast<DeviceHalLocal*>(hwDev().get())->openOutputStream( |
Phil Burk | fdb3c07 | 2016-02-09 10:47:02 -0800 | [diff] [blame] | 151 | handle, |
| 152 | devices, |
| 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); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 163 | mHalFrameSize = audio_stream_out_frame_size(stream); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return status; |
| 167 | } |
| 168 | |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 169 | audio_format_t AudioStreamOut::getFormat() const |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 170 | { |
Phil Burk | ca5e614 | 2015-07-14 09:42:29 -0700 | [diff] [blame] | 171 | return stream->common.get_format(&stream->common); |
| 172 | } |
| 173 | |
| 174 | uint32_t AudioStreamOut::getSampleRate() const |
| 175 | { |
| 176 | return stream->common.get_sample_rate(&stream->common); |
| 177 | } |
| 178 | |
| 179 | audio_channel_mask_t AudioStreamOut::getChannelMask() const |
| 180 | { |
| 181 | return stream->common.get_channels(&stream->common); |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | int AudioStreamOut::flush() |
| 185 | { |
| 186 | ALOG_ASSERT(stream != NULL); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 187 | mRenderPosition = 0; |
| 188 | mFramesWritten = 0; |
| 189 | mFramesWrittenAtStandby = 0; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 190 | if (stream->flush != NULL) { |
| 191 | return stream->flush(stream); |
| 192 | } |
| 193 | return NO_ERROR; |
| 194 | } |
| 195 | |
| 196 | int AudioStreamOut::standby() |
| 197 | { |
| 198 | ALOG_ASSERT(stream != NULL); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 199 | mRenderPosition = 0; |
| 200 | mFramesWrittenAtStandby = mFramesWritten; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 201 | return stream->common.standby(&stream->common); |
| 202 | } |
| 203 | |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 204 | ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes) |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 205 | { |
| 206 | ALOG_ASSERT(stream != NULL); |
Phil Burk | 90eea76 | 2015-07-06 16:24:14 -0700 | [diff] [blame] | 207 | ssize_t bytesWritten = stream->write(stream, buffer, numBytes); |
| 208 | if (bytesWritten > 0 && mHalFrameSize > 0) { |
| 209 | mFramesWritten += bytesWritten / mHalFrameSize; |
| 210 | } |
| 211 | return bytesWritten; |
Phil Burk | 062e67a | 2015-02-11 13:40:50 -0800 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | } // namespace android |