blob: 7aadda3953dbdac877c4e28e4071e7f433e09133 [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// ----------------------------------------------------------------------------
Dean Wheatleydfb67b82024-01-23 09:36:29 +110033AudioStreamOut::AudioStreamOut(AudioHwDevice *dev)
Phil Burk062e67a2015-02-11 13:40:50 -080034 : audioHwDev(dev)
Phil Burk062e67a2015-02-11 13:40:50 -080035{
36}
37
Andy Hung1ef77382023-06-15 14:50:18 -070038// This must be defined here together with the HAL includes above and
39// not solely in the header.
40AudioStreamOut::~AudioStreamOut() = default;
Mikhail Naganov1dc98672016-08-18 17:50:29 -070041
Mikhail Naganove4f1f632016-08-31 11:35:10 -070042sp<DeviceHalInterface> AudioStreamOut::hwDev() const
Phil Burk062e67a2015-02-11 13:40:50 -080043{
44 return audioHwDev->hwDevice();
45}
46
Phil Burk90eea762015-07-06 16:24:14 -070047status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
Phil Burk062e67a2015-02-11 13:40:50 -080048{
Andy Hung1ef77382023-06-15 14:50:18 -070049 if (stream == nullptr) {
Phil Burk062e67a2015-02-11 13:40:50 -080050 return NO_INIT;
51 }
Phil Burk90eea762015-07-06 16:24:14 -070052
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070053 uint64_t halPosition = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070054 const status_t status = stream->getRenderPosition(&halPosition);
Phil Burk90eea762015-07-06 16:24:14 -070055 if (status != NO_ERROR) {
56 return status;
57 }
Phil Burk90eea762015-07-06 16:24:14 -070058 // Scale from HAL sample rate to application rate.
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070059 *frames = halPosition / mRateMultiplier;
Phil Burk90eea762015-07-06 16:24:14 -070060
61 return status;
62}
63
Phil Burk062e67a2015-02-11 13:40:50 -080064status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
65{
Andy Hung1ef77382023-06-15 14:50:18 -070066 if (stream == nullptr) {
Phil Burk062e67a2015-02-11 13:40:50 -080067 return NO_INIT;
68 }
Phil Burk90eea762015-07-06 16:24:14 -070069
70 uint64_t halPosition = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070071 const status_t status = stream->getPresentationPosition(&halPosition, timestamp);
Phil Burk90eea762015-07-06 16:24:14 -070072 if (status != NO_ERROR) {
73 return status;
74 }
75
Andy Hung5bbd8ed2024-02-20 13:57:02 -080076 if (mHalFormatHasProportionalFrames &&
77 (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) {
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070078 // For DirectTrack reset position to 0 on standby.
Andy Hung1ef77382023-06-15 14:50:18 -070079 const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
Phil Burk90eea762015-07-06 16:24:14 -070080 0 : (halPosition - mFramesWrittenAtStandby);
81 // Scale from HAL sample rate to application rate.
82 *frames = adjustedPosition / mRateMultiplier;
83 } else {
Andy Hung5bbd8ed2024-02-20 13:57:02 -080084 // For offloaded MP3 and other compressed formats, and linear PCM.
Phil Burk90eea762015-07-06 16:24:14 -070085 *frames = halPosition;
86 }
87
88 return status;
Phil Burk062e67a2015-02-11 13:40:50 -080089}
90
91status_t AudioStreamOut::open(
92 audio_io_handle_t handle,
jiabin43810402019-10-24 14:58:31 -070093 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -080094 struct audio_config *config,
Dean Wheatleydfb67b82024-01-23 09:36:29 +110095 audio_output_flags_t *flagsPtr,
Haofan Wangb75aa6a2024-07-09 23:06:58 -070096 const char *address,
97 const std::vector<playback_track_metadata_v7_t>& sourceMetadata)
Phil Burk062e67a2015-02-11 13:40:50 -080098{
Mikhail Naganov1dc98672016-08-18 17:50:29 -070099 sp<StreamOutHalInterface> outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800100
Dean Wheatleydfb67b82024-01-23 09:36:29 +1100101 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 Burkfdb3c072016-02-09 10:47:02 -0800105
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700106 int status = hwDev()->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800107 handle,
jiabin43810402019-10-24 14:58:31 -0700108 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800109 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800110 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700111 address,
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700112 &outStream,
113 sourceMetadata);
Dean Wheatley6c009512023-10-23 09:34:14 +1100114 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 Burk062e67a2015-02-11 13:40:50 -0800117
Phil Burkfdb3c072016-02-09 10:47:02 -0800118 // 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 Naganov1dc98672016-08-18 17:50:29 -0700124 status = hwDev()->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800125 handle,
jiabin43810402019-10-24 14:58:31 -0700126 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800127 customFlags,
128 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700129 address,
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700130 &outStream,
131 sourceMetadata);
Phil Burkfdb3c072016-02-09 10:47:02 -0800132 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
133 }
134
Phil Burk062e67a2015-02-11 13:40:50 -0800135 if (status == NO_ERROR) {
136 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800137 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700138 status = stream->getFrameSize(&mHalFrameSize);
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700139 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
Dean Wheatley6c009512023-10-23 09:34:14 +1100140 LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700141 " zero", mHalFrameSize);
142
Phil Burk062e67a2015-02-11 13:40:50 -0800143 }
144
145 return status;
146}
147
Mikhail Naganov560637e2021-03-31 22:40:13 +0000148audio_config_base_t AudioStreamOut::getAudioProperties() const
Phil Burk062e67a2015-02-11 13:40:50 -0800149{
Mikhail Naganov560637e2021-03-31 22:40:13 +0000150 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 Burk062e67a2015-02-11 13:40:50 -0800157}
158
159int AudioStreamOut::flush()
160{
Phil Burk90eea762015-07-06 16:24:14 -0700161 mFramesWritten = 0;
162 mFramesWrittenAtStandby = 0;
Andy Hung1ef77382023-06-15 14:50:18 -0700163 const status_t result = stream->flush();
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700164 return result != INVALID_OPERATION ? result : NO_ERROR;
Phil Burk062e67a2015-02-11 13:40:50 -0800165}
166
167int AudioStreamOut::standby()
168{
Phil Burk90eea762015-07-06 16:24:14 -0700169 mFramesWrittenAtStandby = mFramesWritten;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700170 return stream->standby();
Phil Burk062e67a2015-02-11 13:40:50 -0800171}
172
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700173void AudioStreamOut::presentationComplete() {
174 stream->presentationComplete();
175}
176
Phil Burk90eea762015-07-06 16:24:14 -0700177ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800178{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700179 size_t bytesWritten;
Andy Hung1ef77382023-06-15 14:50:18 -0700180 const status_t result = stream->write(buffer, numBytes, &bytesWritten);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700181 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
Phil Burk90eea762015-07-06 16:24:14 -0700182 mFramesWritten += bytesWritten / mHalFrameSize;
183 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700184 return result == OK ? bytesWritten : result;
Phil Burk062e67a2015-02-11 13:40:50 -0800185}
186
187} // namespace android