1 /*
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 <media/audiohal/DeviceHalInterface.h>
22 #include <media/audiohal/StreamHalInterface.h>
23 #include <system/audio.h>
24 #include <utils/Log.h>
25
26 #include "AudioHwDevice.h"
27 #include "AudioStreamOut.h"
28
29 namespace android {
30
31 // ----------------------------------------------------------------------------
AudioStreamOut(AudioHwDevice * dev,audio_output_flags_t flags)32 AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
33 : audioHwDev(dev)
34 , stream(NULL)
35 , flags(flags)
36 , mFramesWritten(0)
37 , mFramesWrittenAtStandby(0)
38 , mRenderPosition(0)
39 , mRateMultiplier(1)
40 , mHalFormatHasProportionalFrames(false)
41 , mHalFrameSize(0)
42 , mExpectRetrograde(false)
43 {
44 }
45
~AudioStreamOut()46 AudioStreamOut::~AudioStreamOut()
47 {
48 }
49
hwDev() const50 sp<DeviceHalInterface> AudioStreamOut::hwDev() const
51 {
52 return audioHwDev->hwDevice();
53 }
54
getRenderPosition(uint64_t * frames)55 status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
56 {
57 if (stream == 0) {
58 return NO_INIT;
59 }
60
61 uint32_t halPosition = 0;
62 status_t status = stream->getRenderPosition(&halPosition);
63 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.
70 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);
73
74 if (deltaHalPosition > 0) {
75 mRenderPosition += deltaHalPosition;
76 } else if (mExpectRetrograde) {
77 mExpectRetrograde = false;
78 mRenderPosition -= static_cast<uint64_t>(-deltaHalPosition);
79 }
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
getRenderPosition(uint32_t * frames)87 status_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;
95 }
96
getPresentationPosition(uint64_t * frames,struct timespec * timestamp)97 status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
98 {
99 if (stream == 0) {
100 return NO_INIT;
101 }
102
103 uint64_t halPosition = 0;
104 status_t status = stream->getPresentationPosition(&halPosition, timestamp);
105 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.
111 if (mHalFormatHasProportionalFrames) {
112 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;
122 }
123
open(audio_io_handle_t handle,audio_devices_t deviceType,struct audio_config * config,const char * address)124 status_t AudioStreamOut::open(
125 audio_io_handle_t handle,
126 audio_devices_t deviceType,
127 struct audio_config *config,
128 const char *address)
129 {
130 sp<StreamOutHalInterface> outStream;
131
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
136 int status = hwDev()->openOutputStream(
137 handle,
138 deviceType,
139 customFlags,
140 config,
141 address,
142 &outStream);
143 ALOGV("AudioStreamOut::open(), HAL returned "
144 " stream %p, sampleRate %d, Format %#x, "
145 "channelMask %#x, status %d",
146 outStream.get(),
147 config->sample_rate,
148 config->format,
149 config->channel_mask,
150 status);
151
152 // 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
158 status = hwDev()->openOutputStream(
159 handle,
160 deviceType,
161 customFlags,
162 &customConfig,
163 address,
164 &outStream);
165 ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
166 }
167
168 if (status == NO_ERROR) {
169 stream = outStream;
170 mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
171 status = stream->getFrameSize(&mHalFrameSize);
172 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
176 }
177
178 return status;
179 }
180
getAudioProperties() const181 audio_config_base_t AudioStreamOut::getAudioProperties() const
182 {
183 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;
190 }
191
flush()192 int AudioStreamOut::flush()
193 {
194 mRenderPosition = 0;
195 mExpectRetrograde = false;
196 mFramesWritten = 0;
197 mFramesWrittenAtStandby = 0;
198 status_t result = stream->flush();
199 return result != INVALID_OPERATION ? result : NO_ERROR;
200 }
201
standby()202 int AudioStreamOut::standby()
203 {
204 mRenderPosition = 0;
205 mExpectRetrograde = false;
206 mFramesWrittenAtStandby = mFramesWritten;
207 return stream->standby();
208 }
209
write(const void * buffer,size_t numBytes)210 ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
211 {
212 size_t bytesWritten;
213 status_t result = stream->write(buffer, numBytes, &bytesWritten);
214 if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
215 mFramesWritten += bytesWritten / mHalFrameSize;
216 }
217 return result == OK ? bytesWritten : result;
218 }
219
220 } // namespace android
221