blob: 65ec0e825a0a07434c5ae986cd1471a2db4f7d33 [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
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 Naganova0c91332016-09-19 10:01:12 -070021#include <media/audiohal/DeviceHalInterface.h>
22#include <media/audiohal/StreamHalInterface.h>
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070023#include <system/audio.h>
Phil Burk062e67a2015-02-11 13:40:50 -080024#include <utils/Log.h>
25
26#include "AudioHwDevice.h"
27#include "AudioStreamOut.h"
28
29namespace android {
30
31// ----------------------------------------------------------------------------
Phil Burk062e67a2015-02-11 13:40:50 -080032AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
33 : audioHwDev(dev)
34 , stream(NULL)
35 , flags(flags)
Phil Burk90eea762015-07-06 16:24:14 -070036 , mFramesWritten(0)
37 , mFramesWrittenAtStandby(0)
38 , mRenderPosition(0)
39 , mRateMultiplier(1)
Phil Burkfdb3c072016-02-09 10:47:02 -080040 , mHalFormatHasProportionalFrames(false)
Phil Burk90eea762015-07-06 16:24:14 -070041 , mHalFrameSize(0)
Atneya Nair0cae0432022-05-10 18:12:12 -040042 , mExpectRetrograde(false)
Phil Burk062e67a2015-02-11 13:40:50 -080043{
44}
45
Mikhail Naganov1dc98672016-08-18 17:50:29 -070046AudioStreamOut::~AudioStreamOut()
47{
48}
49
Mikhail Naganove4f1f632016-08-31 11:35:10 -070050sp<DeviceHalInterface> AudioStreamOut::hwDev() const
Phil Burk062e67a2015-02-11 13:40:50 -080051{
52 return audioHwDev->hwDevice();
53}
54
Phil Burk90eea762015-07-06 16:24:14 -070055status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
Phil Burk062e67a2015-02-11 13:40:50 -080056{
Mikhail Naganov1dc98672016-08-18 17:50:29 -070057 if (stream == 0) {
Phil Burk062e67a2015-02-11 13:40:50 -080058 return NO_INIT;
59 }
Phil Burk90eea762015-07-06 16:24:14 -070060
61 uint32_t halPosition = 0;
Mikhail Naganov1dc98672016-08-18 17:50:29 -070062 status_t status = stream->getRenderPosition(&halPosition);
Phil Burk90eea762015-07-06 16:24:14 -070063 if (status != NO_ERROR) {
64 return status;
65 }
66
67 // Maintain a 64-bit render position using the 32-bit result from the HAL.
68 // This delta calculation relies on the arithmetic overflow behavior
69 // of integers. For example (100 - 0xFFFFFFF0) = 116.
Phil Burkab3c6a52019-02-01 09:52:15 -080070 const uint32_t truncatedPosition = (uint32_t)mRenderPosition;
71 int32_t deltaHalPosition; // initialization not needed, overwitten by __builtin_sub_overflow()
72 (void) __builtin_sub_overflow(halPosition, truncatedPosition, &deltaHalPosition);
Atneya Nair0cae0432022-05-10 18:12:12 -040073
Phil Burk90eea762015-07-06 16:24:14 -070074 if (deltaHalPosition > 0) {
75 mRenderPosition += deltaHalPosition;
Atneya Nair0cae0432022-05-10 18:12:12 -040076 } else if (mExpectRetrograde) {
77 mExpectRetrograde = false;
78 mRenderPosition -= static_cast<uint64_t>(-deltaHalPosition);
Phil Burk90eea762015-07-06 16:24:14 -070079 }
80 // Scale from HAL sample rate to application rate.
81 *frames = mRenderPosition / mRateMultiplier;
82
83 return status;
84}
85
86// return bottom 32-bits of the render position
87status_t AudioStreamOut::getRenderPosition(uint32_t *frames)
88{
89 uint64_t position64 = 0;
90 status_t status = getRenderPosition(&position64);
91 if (status == NO_ERROR) {
92 *frames = (uint32_t)position64;
93 }
94 return status;
Phil Burk062e67a2015-02-11 13:40:50 -080095}
96
97status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
98{
Mikhail Naganov1dc98672016-08-18 17:50:29 -070099 if (stream == 0) {
Phil Burk062e67a2015-02-11 13:40:50 -0800100 return NO_INIT;
101 }
Phil Burk90eea762015-07-06 16:24:14 -0700102
103 uint64_t halPosition = 0;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700104 status_t status = stream->getPresentationPosition(&halPosition, timestamp);
Phil Burk90eea762015-07-06 16:24:14 -0700105 if (status != NO_ERROR) {
106 return status;
107 }
108
109 // Adjust for standby using HAL rate frames.
110 // Only apply this correction if the HAL is getting PCM frames.
Phil Burkfdb3c072016-02-09 10:47:02 -0800111 if (mHalFormatHasProportionalFrames) {
Phil Burk90eea762015-07-06 16:24:14 -0700112 uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
113 0 : (halPosition - mFramesWrittenAtStandby);
114 // Scale from HAL sample rate to application rate.
115 *frames = adjustedPosition / mRateMultiplier;
116 } else {
117 // For offloaded MP3 and other compressed formats.
118 *frames = halPosition;
119 }
120
121 return status;
Phil Burk062e67a2015-02-11 13:40:50 -0800122}
123
124status_t AudioStreamOut::open(
125 audio_io_handle_t handle,
jiabin43810402019-10-24 14:58:31 -0700126 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -0800127 struct audio_config *config,
128 const char *address)
129{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700130 sp<StreamOutHalInterface> outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800131
132 audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
133 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
134 : flags;
135
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700136 int status = hwDev()->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800137 handle,
jiabin43810402019-10-24 14:58:31 -0700138 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800139 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800140 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700141 address,
142 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800143 ALOGV("AudioStreamOut::open(), HAL returned "
144 " stream %p, sampleRate %d, Format %#x, "
Phil Burk062e67a2015-02-11 13:40:50 -0800145 "channelMask %#x, status %d",
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700146 outStream.get(),
Phil Burk062e67a2015-02-11 13:40:50 -0800147 config->sample_rate,
148 config->format,
149 config->channel_mask,
150 status);
151
Phil Burkfdb3c072016-02-09 10:47:02 -0800152 // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
153 // it as PCM then it will probably work.
154 if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
155 struct audio_config customConfig = *config;
156 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
157
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700158 status = hwDev()->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800159 handle,
jiabin43810402019-10-24 14:58:31 -0700160 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800161 customFlags,
162 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700163 address,
164 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800165 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
166 }
167
Phil Burk062e67a2015-02-11 13:40:50 -0800168 if (status == NO_ERROR) {
169 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800170 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700171 status = stream->getFrameSize(&mHalFrameSize);
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700172 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
173 LOG_ALWAYS_FATAL_IF(mHalFrameSize <= 0, "Error frame size was %zu but must be greater than"
174 " zero", mHalFrameSize);
175
Phil Burk062e67a2015-02-11 13:40:50 -0800176 }
177
178 return status;
179}
180
Mikhail Naganov560637e2021-03-31 22:40:13 +0000181audio_config_base_t AudioStreamOut::getAudioProperties() const
Phil Burk062e67a2015-02-11 13:40:50 -0800182{
Mikhail Naganov560637e2021-03-31 22:40:13 +0000183 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
184 if (stream->getAudioProperties(&result) != OK) {
185 result.sample_rate = 0;
186 result.channel_mask = AUDIO_CHANNEL_INVALID;
187 result.format = AUDIO_FORMAT_INVALID;
188 }
189 return result;
Phil Burk062e67a2015-02-11 13:40:50 -0800190}
191
192int AudioStreamOut::flush()
193{
Phil Burk90eea762015-07-06 16:24:14 -0700194 mRenderPosition = 0;
Atneya Nair0cae0432022-05-10 18:12:12 -0400195 mExpectRetrograde = false;
Phil Burk90eea762015-07-06 16:24:14 -0700196 mFramesWritten = 0;
197 mFramesWrittenAtStandby = 0;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700198 status_t result = stream->flush();
199 return result != INVALID_OPERATION ? result : NO_ERROR;
Phil Burk062e67a2015-02-11 13:40:50 -0800200}
201
202int AudioStreamOut::standby()
203{
Phil Burk90eea762015-07-06 16:24:14 -0700204 mRenderPosition = 0;
Atneya Nair0cae0432022-05-10 18:12:12 -0400205 mExpectRetrograde = false;
Phil Burk90eea762015-07-06 16:24:14 -0700206 mFramesWrittenAtStandby = mFramesWritten;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700207 return stream->standby();
Phil Burk062e67a2015-02-11 13:40:50 -0800208}
209
Phil Burk90eea762015-07-06 16:24:14 -0700210ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800211{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700212 size_t bytesWritten;
213 status_t result = stream->write(buffer, numBytes, &bytesWritten);
214 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
Phil Burk90eea762015-07-06 16:24:14 -0700215 mFramesWritten += bytesWritten / mHalFrameSize;
216 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700217 return result == OK ? bytesWritten : result;
Phil Burk062e67a2015-02-11 13:40:50 -0800218}
219
220} // namespace android