blob: aad538ff9e33eedd13616c981a1d3e63526f32be [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
Dean Wheatley6c009512023-10-23 09:34:14 +11002 *
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 Burk062e67a2015-02-11 13:40:50 -080017
18#define LOG_TAG "AudioFlinger"
19//#define LOG_NDEBUG 0
20
Dean Wheatley6c009512023-10-23 09:34:14 +110021#include "AudioStreamOut.h"
22
Mikhail Naganova0c91332016-09-19 10:01:12 -070023#include <media/audiohal/DeviceHalInterface.h>
24#include <media/audiohal/StreamHalInterface.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070025#include <system/audio.h>
Phil Burk062e67a2015-02-11 13:40:50 -080026#include <utils/Log.h>
27
28#include "AudioHwDevice.h"
Phil Burk062e67a2015-02-11 13:40:50 -080029
30namespace android {
31
32// ----------------------------------------------------------------------------
Phil Burk062e67a2015-02-11 13:40:50 -080033AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
34 : audioHwDev(dev)
Phil Burk062e67a2015-02-11 13:40:50 -080035 , flags(flags)
36{
37}
38
Andy Hung1ef77382023-06-15 14:50:18 -070039// This must be defined here together with the HAL includes above and
40// not solely in the header.
41AudioStreamOut::~AudioStreamOut() = default;
Mikhail Naganov1dc98672016-08-18 17:50:29 -070042
Mikhail Naganove4f1f632016-08-31 11:35:10 -070043sp<DeviceHalInterface> AudioStreamOut::hwDev() const
Phil Burk062e67a2015-02-11 13:40:50 -080044{
45 return audioHwDev->hwDevice();
46}
47
Phil Burk90eea762015-07-06 16:24:14 -070048status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
Phil Burk062e67a2015-02-11 13:40:50 -080049{
Andy Hung1ef77382023-06-15 14:50:18 -070050 if (stream == nullptr) {
Phil Burk062e67a2015-02-11 13:40:50 -080051 return NO_INIT;
52 }
Phil Burk90eea762015-07-06 16:24:14 -070053
54 uint32_t halPosition = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070055 const status_t status = stream->getRenderPosition(&halPosition);
Phil Burk90eea762015-07-06 16:24:14 -070056 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 Hung1ef77382023-06-15 14:50:18 -070063 const auto truncatedPosition = (uint32_t)mRenderPosition;
Phil Burkab3c6a52019-02-01 09:52:15 -080064 int32_t deltaHalPosition; // initialization not needed, overwitten by __builtin_sub_overflow()
65 (void) __builtin_sub_overflow(halPosition, truncatedPosition, &deltaHalPosition);
Atneya Nair0cae0432022-05-10 18:12:12 -040066
David Lidafa7232024-04-11 06:08:45 +080067 if (deltaHalPosition >= 0) {
Phil Burk90eea762015-07-06 16:24:14 -070068 mRenderPosition += deltaHalPosition;
Atneya Nair0cae0432022-05-10 18:12:12 -040069 } else if (mExpectRetrograde) {
70 mExpectRetrograde = false;
71 mRenderPosition -= static_cast<uint64_t>(-deltaHalPosition);
Phil Burk90eea762015-07-06 16:24:14 -070072 }
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
80status_t AudioStreamOut::getRenderPosition(uint32_t *frames)
81{
82 uint64_t position64 = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070083 const status_t status = getRenderPosition(&position64);
Phil Burk90eea762015-07-06 16:24:14 -070084 if (status == NO_ERROR) {
85 *frames = (uint32_t)position64;
86 }
87 return status;
Phil Burk062e67a2015-02-11 13:40:50 -080088}
89
90status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
91{
Andy Hung1ef77382023-06-15 14:50:18 -070092 if (stream == nullptr) {
Phil Burk062e67a2015-02-11 13:40:50 -080093 return NO_INIT;
94 }
Phil Burk90eea762015-07-06 16:24:14 -070095
96 uint64_t halPosition = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070097 const status_t status = stream->getPresentationPosition(&halPosition, timestamp);
Phil Burk90eea762015-07-06 16:24:14 -070098 if (status != NO_ERROR) {
99 return status;
100 }
101
Andy Hung5bbd8ed2024-02-20 13:57:02 -0800102 if (mHalFormatHasProportionalFrames &&
103 (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) {
104 // For DirectTrack reset timestamp to 0 on standby.
Andy Hung1ef77382023-06-15 14:50:18 -0700105 const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
Phil Burk90eea762015-07-06 16:24:14 -0700106 0 : (halPosition - mFramesWrittenAtStandby);
107 // Scale from HAL sample rate to application rate.
108 *frames = adjustedPosition / mRateMultiplier;
109 } else {
Andy Hung5bbd8ed2024-02-20 13:57:02 -0800110 // For offloaded MP3 and other compressed formats, and linear PCM.
Phil Burk90eea762015-07-06 16:24:14 -0700111 *frames = halPosition;
112 }
113
114 return status;
Phil Burk062e67a2015-02-11 13:40:50 -0800115}
116
117status_t AudioStreamOut::open(
118 audio_io_handle_t handle,
jiabin43810402019-10-24 14:58:31 -0700119 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -0800120 struct audio_config *config,
121 const char *address)
122{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700123 sp<StreamOutHalInterface> outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800124
Andy Hung1ef77382023-06-15 14:50:18 -0700125 const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
Phil Burkfdb3c072016-02-09 10:47:02 -0800126 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
127 : flags;
128
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700129 int status = hwDev()->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800130 handle,
jiabin43810402019-10-24 14:58:31 -0700131 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800132 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800133 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700134 address,
135 &outStream);
Dean Wheatley6c009512023-10-23 09:34:14 +1100136 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 Burk062e67a2015-02-11 13:40:50 -0800139
Phil Burkfdb3c072016-02-09 10:47:02 -0800140 // 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 Naganov1dc98672016-08-18 17:50:29 -0700146 status = hwDev()->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800147 handle,
jiabin43810402019-10-24 14:58:31 -0700148 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800149 customFlags,
150 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700151 address,
152 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800153 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
154 }
155
Phil Burk062e67a2015-02-11 13:40:50 -0800156 if (status == NO_ERROR) {
157 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800158 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700159 status = stream->getFrameSize(&mHalFrameSize);
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700160 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
Dean Wheatley6c009512023-10-23 09:34:14 +1100161 LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700162 " zero", mHalFrameSize);
163
Phil Burk062e67a2015-02-11 13:40:50 -0800164 }
165
166 return status;
167}
168
Mikhail Naganov560637e2021-03-31 22:40:13 +0000169audio_config_base_t AudioStreamOut::getAudioProperties() const
Phil Burk062e67a2015-02-11 13:40:50 -0800170{
Mikhail Naganov560637e2021-03-31 22:40:13 +0000171 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 Burk062e67a2015-02-11 13:40:50 -0800178}
179
180int AudioStreamOut::flush()
181{
Phil Burk90eea762015-07-06 16:24:14 -0700182 mRenderPosition = 0;
Atneya Nair0cae0432022-05-10 18:12:12 -0400183 mExpectRetrograde = false;
Phil Burk90eea762015-07-06 16:24:14 -0700184 mFramesWritten = 0;
185 mFramesWrittenAtStandby = 0;
Andy Hung1ef77382023-06-15 14:50:18 -0700186 const status_t result = stream->flush();
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700187 return result != INVALID_OPERATION ? result : NO_ERROR;
Phil Burk062e67a2015-02-11 13:40:50 -0800188}
189
190int AudioStreamOut::standby()
191{
Phil Burk90eea762015-07-06 16:24:14 -0700192 mRenderPosition = 0;
Atneya Nair0cae0432022-05-10 18:12:12 -0400193 mExpectRetrograde = false;
Phil Burk90eea762015-07-06 16:24:14 -0700194 mFramesWrittenAtStandby = mFramesWritten;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700195 return stream->standby();
Phil Burk062e67a2015-02-11 13:40:50 -0800196}
197
Phil Burk90eea762015-07-06 16:24:14 -0700198ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800199{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700200 size_t bytesWritten;
Andy Hung1ef77382023-06-15 14:50:18 -0700201 const status_t result = stream->write(buffer, numBytes, &bytesWritten);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700202 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
Phil Burk90eea762015-07-06 16:24:14 -0700203 mFramesWritten += bytesWritten / mHalFrameSize;
204 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700205 return result == OK ? bytesWritten : result;
Phil Burk062e67a2015-02-11 13:40:50 -0800206}
207
208} // namespace android