blob: c65373eb52440c51040d6683cf1b3b9e50b6f47f [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
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070054 uint64_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 }
Phil Burk90eea762015-07-06 16:24:14 -070059 // Scale from HAL sample rate to application rate.
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070060 *frames = halPosition / mRateMultiplier;
Phil Burk90eea762015-07-06 16:24:14 -070061
62 return status;
63}
64
Phil Burk062e67a2015-02-11 13:40:50 -080065status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
66{
Andy Hung1ef77382023-06-15 14:50:18 -070067 if (stream == nullptr) {
Phil Burk062e67a2015-02-11 13:40:50 -080068 return NO_INIT;
69 }
Phil Burk90eea762015-07-06 16:24:14 -070070
71 uint64_t halPosition = 0;
Andy Hung1ef77382023-06-15 14:50:18 -070072 const status_t status = stream->getPresentationPosition(&halPosition, timestamp);
Phil Burk90eea762015-07-06 16:24:14 -070073 if (status != NO_ERROR) {
74 return status;
75 }
76
Andy Hung5bbd8ed2024-02-20 13:57:02 -080077 if (mHalFormatHasProportionalFrames &&
78 (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) {
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -070079 // For DirectTrack reset position to 0 on standby.
Andy Hung1ef77382023-06-15 14:50:18 -070080 const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
Phil Burk90eea762015-07-06 16:24:14 -070081 0 : (halPosition - mFramesWrittenAtStandby);
82 // Scale from HAL sample rate to application rate.
83 *frames = adjustedPosition / mRateMultiplier;
84 } else {
Andy Hung5bbd8ed2024-02-20 13:57:02 -080085 // For offloaded MP3 and other compressed formats, and linear PCM.
Phil Burk90eea762015-07-06 16:24:14 -070086 *frames = halPosition;
87 }
88
89 return status;
Phil Burk062e67a2015-02-11 13:40:50 -080090}
91
92status_t AudioStreamOut::open(
93 audio_io_handle_t handle,
jiabin43810402019-10-24 14:58:31 -070094 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -080095 struct audio_config *config,
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
Andy Hung1ef77382023-06-15 14:50:18 -0700101 const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
Phil Burkfdb3c072016-02-09 10:47:02 -0800102 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
103 : flags;
104
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700105 int status = hwDev()->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800106 handle,
jiabin43810402019-10-24 14:58:31 -0700107 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800108 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800109 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700110 address,
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700111 &outStream,
112 sourceMetadata);
Dean Wheatley6c009512023-10-23 09:34:14 +1100113 ALOGV("AudioStreamOut::open(), HAL returned stream %p, sampleRate %d, format %#x,"
114 " channelMask %#x, status %d", outStream.get(), config->sample_rate, config->format,
115 config->channel_mask, status);
Phil Burk062e67a2015-02-11 13:40:50 -0800116
Phil Burkfdb3c072016-02-09 10:47:02 -0800117 // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
118 // it as PCM then it will probably work.
119 if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
120 struct audio_config customConfig = *config;
121 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
122
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700123 status = hwDev()->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800124 handle,
jiabin43810402019-10-24 14:58:31 -0700125 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800126 customFlags,
127 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700128 address,
Haofan Wangb75aa6a2024-07-09 23:06:58 -0700129 &outStream,
130 sourceMetadata);
Phil Burkfdb3c072016-02-09 10:47:02 -0800131 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
132 }
133
Phil Burk062e67a2015-02-11 13:40:50 -0800134 if (status == NO_ERROR) {
135 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800136 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700137 status = stream->getFrameSize(&mHalFrameSize);
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700138 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
Dean Wheatley6c009512023-10-23 09:34:14 +1100139 LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700140 " zero", mHalFrameSize);
141
Phil Burk062e67a2015-02-11 13:40:50 -0800142 }
143
144 return status;
145}
146
Mikhail Naganov560637e2021-03-31 22:40:13 +0000147audio_config_base_t AudioStreamOut::getAudioProperties() const
Phil Burk062e67a2015-02-11 13:40:50 -0800148{
Mikhail Naganov560637e2021-03-31 22:40:13 +0000149 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
150 if (stream->getAudioProperties(&result) != OK) {
151 result.sample_rate = 0;
152 result.channel_mask = AUDIO_CHANNEL_INVALID;
153 result.format = AUDIO_FORMAT_INVALID;
154 }
155 return result;
Phil Burk062e67a2015-02-11 13:40:50 -0800156}
157
158int AudioStreamOut::flush()
159{
Phil Burk90eea762015-07-06 16:24:14 -0700160 mFramesWritten = 0;
161 mFramesWrittenAtStandby = 0;
Andy Hung1ef77382023-06-15 14:50:18 -0700162 const status_t result = stream->flush();
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700163 return result != INVALID_OPERATION ? result : NO_ERROR;
Phil Burk062e67a2015-02-11 13:40:50 -0800164}
165
166int AudioStreamOut::standby()
167{
Phil Burk90eea762015-07-06 16:24:14 -0700168 mFramesWrittenAtStandby = mFramesWritten;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700169 return stream->standby();
Phil Burk062e67a2015-02-11 13:40:50 -0800170}
171
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700172void AudioStreamOut::presentationComplete() {
173 stream->presentationComplete();
174}
175
Phil Burk90eea762015-07-06 16:24:14 -0700176ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800177{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700178 size_t bytesWritten;
Andy Hung1ef77382023-06-15 14:50:18 -0700179 const status_t result = stream->write(buffer, numBytes, &bytesWritten);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700180 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
Phil Burk90eea762015-07-06 16:24:14 -0700181 mFramesWritten += bytesWritten / mHalFrameSize;
182 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700183 return result == OK ? bytesWritten : result;
Phil Burk062e67a2015-02-11 13:40:50 -0800184}
185
186} // namespace android