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 {
43 }
44 
~AudioStreamOut()45 AudioStreamOut::~AudioStreamOut()
46 {
47 }
48 
hwDev() const49 sp<DeviceHalInterface> AudioStreamOut::hwDev() const
50 {
51     return audioHwDev->hwDevice();
52 }
53 
getRenderPosition(uint64_t * frames)54 status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
55 {
56     if (stream == 0) {
57         return NO_INIT;
58     }
59 
60     uint32_t halPosition = 0;
61     status_t status = stream->getRenderPosition(&halPosition);
62     if (status != NO_ERROR) {
63         return status;
64     }
65 
66     // Maintain a 64-bit render position using the 32-bit result from the HAL.
67     // This delta calculation relies on the arithmetic overflow behavior
68     // of integers. For example (100 - 0xFFFFFFF0) = 116.
69     const uint32_t truncatedPosition = (uint32_t)mRenderPosition;
70     int32_t deltaHalPosition; // initialization not needed, overwitten by __builtin_sub_overflow()
71     (void) __builtin_sub_overflow(halPosition, truncatedPosition, &deltaHalPosition);
72     if (deltaHalPosition > 0) {
73         mRenderPosition += deltaHalPosition;
74     }
75     // Scale from HAL sample rate to application rate.
76     *frames = mRenderPosition / mRateMultiplier;
77 
78     return status;
79 }
80 
81 // return bottom 32-bits of the render position
getRenderPosition(uint32_t * frames)82 status_t AudioStreamOut::getRenderPosition(uint32_t *frames)
83 {
84     uint64_t position64 = 0;
85     status_t status = getRenderPosition(&position64);
86     if (status == NO_ERROR) {
87         *frames = (uint32_t)position64;
88     }
89     return status;
90 }
91 
getPresentationPosition(uint64_t * frames,struct timespec * timestamp)92 status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
93 {
94     if (stream == 0) {
95         return NO_INIT;
96     }
97 
98     uint64_t halPosition = 0;
99     status_t status = stream->getPresentationPosition(&halPosition, timestamp);
100     if (status != NO_ERROR) {
101         return status;
102     }
103 
104     // Adjust for standby using HAL rate frames.
105     // Only apply this correction if the HAL is getting PCM frames.
106     if (mHalFormatHasProportionalFrames) {
107         uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
108                 0 : (halPosition - mFramesWrittenAtStandby);
109         // Scale from HAL sample rate to application rate.
110         *frames = adjustedPosition / mRateMultiplier;
111     } else {
112         // For offloaded MP3 and other compressed formats.
113         *frames = halPosition;
114     }
115 
116     return status;
117 }
118 
open(audio_io_handle_t handle,audio_devices_t deviceType,struct audio_config * config,const char * address)119 status_t AudioStreamOut::open(
120         audio_io_handle_t handle,
121         audio_devices_t deviceType,
122         struct audio_config *config,
123         const char *address)
124 {
125     sp<StreamOutHalInterface> outStream;
126 
127     audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
128                 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
129                 : flags;
130 
131     int status = hwDev()->openOutputStream(
132             handle,
133             deviceType,
134             customFlags,
135             config,
136             address,
137             &outStream);
138     ALOGV("AudioStreamOut::open(), HAL returned "
139             " stream %p, sampleRate %d, Format %#x, "
140             "channelMask %#x, status %d",
141             outStream.get(),
142             config->sample_rate,
143             config->format,
144             config->channel_mask,
145             status);
146 
147     // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
148     // it as PCM then it will probably work.
149     if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
150         struct audio_config customConfig = *config;
151         customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
152 
153         status = hwDev()->openOutputStream(
154                 handle,
155                 deviceType,
156                 customFlags,
157                 &customConfig,
158                 address,
159                 &outStream);
160         ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
161     }
162 
163     if (status == NO_ERROR) {
164         stream = outStream;
165         mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
166         status = stream->getFrameSize(&mHalFrameSize);
167         LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
168         LOG_ALWAYS_FATAL_IF(mHalFrameSize <= 0, "Error frame size was %zu but must be greater than"
169                 " zero", mHalFrameSize);
170 
171     }
172 
173     return status;
174 }
175 
getAudioProperties() const176 audio_config_base_t AudioStreamOut::getAudioProperties() const
177 {
178     audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
179     if (stream->getAudioProperties(&result) != OK) {
180         result.sample_rate = 0;
181         result.channel_mask = AUDIO_CHANNEL_INVALID;
182         result.format = AUDIO_FORMAT_INVALID;
183     }
184     return result;
185 }
186 
flush()187 int AudioStreamOut::flush()
188 {
189     mRenderPosition = 0;
190     mFramesWritten = 0;
191     mFramesWrittenAtStandby = 0;
192     status_t result = stream->flush();
193     return result != INVALID_OPERATION ? result : NO_ERROR;
194 }
195 
standby()196 int AudioStreamOut::standby()
197 {
198     mRenderPosition = 0;
199     mFramesWrittenAtStandby = mFramesWritten;
200     return stream->standby();
201 }
202 
write(const void * buffer,size_t numBytes)203 ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
204 {
205     size_t bytesWritten;
206     status_t result = stream->write(buffer, numBytes, &bytesWritten);
207     if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
208         mFramesWritten += bytesWritten / mHalFrameSize;
209     }
210     return result == OK ? bytesWritten : result;
211 }
212 
213 } // namespace android
214