blob: 4839480529e258ccb724263ae21c29fbb39bdd55 [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
21#include <hardware/audio.h>
22#include <utils/Log.h>
23
24#include "AudioHwDevice.h"
25#include "AudioStreamOut.h"
Mikhail Naganove4f1f632016-08-31 11:35:10 -070026#include "DeviceHalInterface.h"
27
28// FIXME: Remove after streams HAL is componentized
29#include "DeviceHalLocal.h"
Phil Burk062e67a2015-02-11 13:40:50 -080030
31namespace android {
32
33// ----------------------------------------------------------------------------
Phil Burk062e67a2015-02-11 13:40:50 -080034AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
35 : audioHwDev(dev)
36 , stream(NULL)
37 , flags(flags)
Phil Burk90eea762015-07-06 16:24:14 -070038 , mFramesWritten(0)
39 , mFramesWrittenAtStandby(0)
40 , mRenderPosition(0)
41 , mRateMultiplier(1)
Phil Burkfdb3c072016-02-09 10:47:02 -080042 , mHalFormatHasProportionalFrames(false)
Phil Burk90eea762015-07-06 16:24:14 -070043 , mHalFrameSize(0)
Phil Burk062e67a2015-02-11 13:40:50 -080044{
45}
46
Mikhail Naganove4f1f632016-08-31 11:35:10 -070047sp<DeviceHalInterface> AudioStreamOut::hwDev() const
Phil Burk062e67a2015-02-11 13:40:50 -080048{
49 return audioHwDev->hwDevice();
50}
51
Phil Burk90eea762015-07-06 16:24:14 -070052status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
Phil Burk062e67a2015-02-11 13:40:50 -080053{
54 if (stream == NULL) {
55 return NO_INIT;
56 }
Phil Burk90eea762015-07-06 16:24:14 -070057
58 uint32_t halPosition = 0;
59 status_t status = stream->get_render_position(stream, &halPosition);
60 if (status != NO_ERROR) {
61 return status;
62 }
63
64 // Maintain a 64-bit render position using the 32-bit result from the HAL.
65 // This delta calculation relies on the arithmetic overflow behavior
66 // of integers. For example (100 - 0xFFFFFFF0) = 116.
67 uint32_t truncatedPosition = (uint32_t)mRenderPosition;
68 int32_t deltaHalPosition = (int32_t)(halPosition - truncatedPosition);
69 if (deltaHalPosition > 0) {
70 mRenderPosition += deltaHalPosition;
71 }
72 // Scale from HAL sample rate to application rate.
73 *frames = mRenderPosition / mRateMultiplier;
74
75 return status;
76}
77
78// return bottom 32-bits of the render position
79status_t AudioStreamOut::getRenderPosition(uint32_t *frames)
80{
81 uint64_t position64 = 0;
82 status_t status = getRenderPosition(&position64);
83 if (status == NO_ERROR) {
84 *frames = (uint32_t)position64;
85 }
86 return status;
Phil Burk062e67a2015-02-11 13:40:50 -080087}
88
89status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
90{
91 if (stream == NULL) {
92 return NO_INIT;
93 }
Phil Burk90eea762015-07-06 16:24:14 -070094
95 uint64_t halPosition = 0;
96 status_t status = stream->get_presentation_position(stream, &halPosition, timestamp);
97 if (status != NO_ERROR) {
98 return status;
99 }
100
101 // Adjust for standby using HAL rate frames.
102 // Only apply this correction if the HAL is getting PCM frames.
Phil Burkfdb3c072016-02-09 10:47:02 -0800103 if (mHalFormatHasProportionalFrames) {
Phil Burk90eea762015-07-06 16:24:14 -0700104 uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
105 0 : (halPosition - mFramesWrittenAtStandby);
106 // Scale from HAL sample rate to application rate.
107 *frames = adjustedPosition / mRateMultiplier;
108 } else {
109 // For offloaded MP3 and other compressed formats.
110 *frames = halPosition;
111 }
112
113 return status;
Phil Burk062e67a2015-02-11 13:40:50 -0800114}
115
116status_t AudioStreamOut::open(
117 audio_io_handle_t handle,
118 audio_devices_t devices,
119 struct audio_config *config,
120 const char *address)
121{
Phil Burk90eea762015-07-06 16:24:14 -0700122 audio_stream_out_t *outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800123
124 audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
125 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
126 : flags;
127
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700128 int status = static_cast<DeviceHalLocal*>(hwDev().get())->openOutputStream(
Phil Burk062e67a2015-02-11 13:40:50 -0800129 handle,
130 devices,
Phil Burkfdb3c072016-02-09 10:47:02 -0800131 customFlags,
Phil Burk062e67a2015-02-11 13:40:50 -0800132 config,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700133 address,
134 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800135 ALOGV("AudioStreamOut::open(), HAL returned "
136 " stream %p, sampleRate %d, Format %#x, "
Phil Burk062e67a2015-02-11 13:40:50 -0800137 "channelMask %#x, status %d",
138 outStream,
139 config->sample_rate,
140 config->format,
141 config->channel_mask,
142 status);
143
Phil Burkfdb3c072016-02-09 10:47:02 -0800144 // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
145 // it as PCM then it will probably work.
146 if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
147 struct audio_config customConfig = *config;
148 customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
149
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700150 status = static_cast<DeviceHalLocal*>(hwDev().get())->openOutputStream(
Phil Burkfdb3c072016-02-09 10:47:02 -0800151 handle,
152 devices,
153 customFlags,
154 &customConfig,
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700155 address,
156 &outStream);
Phil Burkfdb3c072016-02-09 10:47:02 -0800157 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
158 }
159
Phil Burk062e67a2015-02-11 13:40:50 -0800160 if (status == NO_ERROR) {
161 stream = outStream;
Phil Burkfdb3c072016-02-09 10:47:02 -0800162 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
Phil Burk90eea762015-07-06 16:24:14 -0700163 mHalFrameSize = audio_stream_out_frame_size(stream);
Phil Burk062e67a2015-02-11 13:40:50 -0800164 }
165
166 return status;
167}
168
Phil Burkca5e6142015-07-14 09:42:29 -0700169audio_format_t AudioStreamOut::getFormat() const
Phil Burk062e67a2015-02-11 13:40:50 -0800170{
Phil Burkca5e6142015-07-14 09:42:29 -0700171 return stream->common.get_format(&stream->common);
172}
173
174uint32_t AudioStreamOut::getSampleRate() const
175{
176 return stream->common.get_sample_rate(&stream->common);
177}
178
179audio_channel_mask_t AudioStreamOut::getChannelMask() const
180{
181 return stream->common.get_channels(&stream->common);
Phil Burk062e67a2015-02-11 13:40:50 -0800182}
183
184int AudioStreamOut::flush()
185{
186 ALOG_ASSERT(stream != NULL);
Phil Burk90eea762015-07-06 16:24:14 -0700187 mRenderPosition = 0;
188 mFramesWritten = 0;
189 mFramesWrittenAtStandby = 0;
Phil Burk062e67a2015-02-11 13:40:50 -0800190 if (stream->flush != NULL) {
191 return stream->flush(stream);
192 }
193 return NO_ERROR;
194}
195
196int AudioStreamOut::standby()
197{
198 ALOG_ASSERT(stream != NULL);
Phil Burk90eea762015-07-06 16:24:14 -0700199 mRenderPosition = 0;
200 mFramesWrittenAtStandby = mFramesWritten;
Phil Burk062e67a2015-02-11 13:40:50 -0800201 return stream->common.standby(&stream->common);
202}
203
Phil Burk90eea762015-07-06 16:24:14 -0700204ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
Phil Burk062e67a2015-02-11 13:40:50 -0800205{
206 ALOG_ASSERT(stream != NULL);
Phil Burk90eea762015-07-06 16:24:14 -0700207 ssize_t bytesWritten = stream->write(stream, buffer, numBytes);
208 if (bytesWritten > 0 && mHalFrameSize > 0) {
209 mFramesWritten += bytesWritten / mHalFrameSize;
210 }
211 return bytesWritten;
Phil Burk062e67a2015-02-11 13:40:50 -0800212}
213
214} // namespace android