blob: a686ff625b8020e21c2bf58806b9f5399dc43f19 [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,
96 const char *address)
97{
Mikhail Naganov1dc98672016-08-18 17:50:29 -070098 sp<StreamOutHalInterface> outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -080099
Andy Hung1ef77382023-06-15 14:50:18 -0700100 const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
Phil Burkfdb3c072016-02-09 10:47:02 -0800101 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
102 : flags;
103
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700104 int status = hwDev()->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800105 handle,
jiabin43810402019-10-24 14:58:31 -0700106 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800107 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800108 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700109 address,
110 &outStream);
Dean Wheatley6c009512023-10-23 09:34:14 +1100111 ALOGV("AudioStreamOut::open(), HAL returned stream %p, sampleRate %d, format %#x,"
112 " channelMask %#x, status %d", outStream.get(), config->sample_rate, config->format,
113 config->channel_mask, status);
Phil Burk062e67a2015-02-11 13:40:50 -0800114
Phil Burkfdb3c072016-02-09 10:47:02 -0800115 // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
116 // it as PCM then it will probably work.
117 if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
118 struct audio_config customConfig = *config;
119 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
120
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700121 status = hwDev()->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800122 handle,
jiabin43810402019-10-24 14:58:31 -0700123 deviceType,
Phil Burkfdb3c072016-02-09 10:47:02 -0800124 customFlags,
125 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700126 address,
127 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800128 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
129 }
130
Phil Burk062e67a2015-02-11 13:40:50 -0800131 if (status == NO_ERROR) {
132 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800133 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700134 status = stream->getFrameSize(&mHalFrameSize);
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700135 LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
Dean Wheatley6c009512023-10-23 09:34:14 +1100136 LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
Hayden Gomes1a89ab32020-06-12 11:05:47 -0700137 " zero", mHalFrameSize);
138
Phil Burk062e67a2015-02-11 13:40:50 -0800139 }
140
141 return status;
142}
143
Mikhail Naganov560637e2021-03-31 22:40:13 +0000144audio_config_base_t AudioStreamOut::getAudioProperties() const
Phil Burk062e67a2015-02-11 13:40:50 -0800145{
Mikhail Naganov560637e2021-03-31 22:40:13 +0000146 audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
147 if (stream->getAudioProperties(&result) != OK) {
148 result.sample_rate = 0;
149 result.channel_mask = AUDIO_CHANNEL_INVALID;
150 result.format = AUDIO_FORMAT_INVALID;
151 }
152 return result;
Phil Burk062e67a2015-02-11 13:40:50 -0800153}
154
155int AudioStreamOut::flush()
156{
Phil Burk90eea762015-07-06 16:24:14 -0700157 mFramesWritten = 0;
158 mFramesWrittenAtStandby = 0;
Andy Hung1ef77382023-06-15 14:50:18 -0700159 const status_t result = stream->flush();
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700160 return result != INVALID_OPERATION ? result : NO_ERROR;
Phil Burk062e67a2015-02-11 13:40:50 -0800161}
162
163int AudioStreamOut::standby()
164{
Phil Burk90eea762015-07-06 16:24:14 -0700165 mFramesWrittenAtStandby = mFramesWritten;
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700166 return stream->standby();
Phil Burk062e67a2015-02-11 13:40:50 -0800167}
168
Mikhail Naganov0ea58fe2024-05-10 13:30:40 -0700169void AudioStreamOut::presentationComplete() {
170 stream->presentationComplete();
171}
172
Phil Burk90eea762015-07-06 16:24:14 -0700173ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800174{
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700175 size_t bytesWritten;
Andy Hung1ef77382023-06-15 14:50:18 -0700176 const status_t result = stream->write(buffer, numBytes, &bytesWritten);
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700177 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
Phil Burk90eea762015-07-06 16:24:14 -0700178 mFramesWritten += bytesWritten / mHalFrameSize;
179 }
Mikhail Naganov1dc98672016-08-18 17:50:29 -0700180 return result == OK ? bytesWritten : result;
Phil Burk062e67a2015-02-11 13:40:50 -0800181}
182
183} // namespace android