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 "AudioStreamOut.h"
22 
23 #include <media/audiohal/DeviceHalInterface.h>
24 #include <media/audiohal/StreamHalInterface.h>
25 #include <system/audio.h>
26 #include <utils/Log.h>
27 
28 #include "AudioHwDevice.h"
29 
30 namespace android {
31 
32 // ----------------------------------------------------------------------------
AudioStreamOut(AudioHwDevice * dev,audio_output_flags_t flags)33 AudioStreamOut::AudioStreamOut(AudioHwDevice *dev, audio_output_flags_t flags)
34         : audioHwDev(dev)
35         , flags(flags)
36 {
37 }
38 
39 // This must be defined here together with the HAL includes above and
40 // not solely in the header.
41 AudioStreamOut::~AudioStreamOut() = default;
42 
hwDev() const43 sp<DeviceHalInterface> AudioStreamOut::hwDev() const
44 {
45     return audioHwDev->hwDevice();
46 }
47 
getRenderPosition(uint64_t * frames)48 status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
49 {
50     if (stream == nullptr) {
51         return NO_INIT;
52     }
53 
54     uint64_t halPosition = 0;
55     const status_t status = stream->getRenderPosition(&halPosition);
56     if (status != NO_ERROR) {
57         return status;
58     }
59     // Scale from HAL sample rate to application rate.
60     *frames = halPosition / mRateMultiplier;
61 
62     return status;
63 }
64 
getPresentationPosition(uint64_t * frames,struct timespec * timestamp)65 status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
66 {
67     if (stream == nullptr) {
68         return NO_INIT;
69     }
70 
71     uint64_t halPosition = 0;
72     const status_t status = stream->getPresentationPosition(&halPosition, timestamp);
73     if (status != NO_ERROR) {
74         return status;
75     }
76 
77     if (mHalFormatHasProportionalFrames &&
78             (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) {
79         // For DirectTrack reset position to 0 on standby.
80         const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
81                 0 : (halPosition - mFramesWrittenAtStandby);
82         // Scale from HAL sample rate to application rate.
83         *frames = adjustedPosition / mRateMultiplier;
84     } else {
85         // For offloaded MP3 and other compressed formats, and linear PCM.
86         *frames = halPosition;
87     }
88 
89     return status;
90 }
91 
open(audio_io_handle_t handle,audio_devices_t deviceType,struct audio_config * config,const char * address)92 status_t AudioStreamOut::open(
93         audio_io_handle_t handle,
94         audio_devices_t deviceType,
95         struct audio_config *config,
96         const char *address)
97 {
98     sp<StreamOutHalInterface> outStream;
99 
100     const audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
101                 ? (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
102                 : flags;
103 
104     int status = hwDev()->openOutputStream(
105             handle,
106             deviceType,
107             customFlags,
108             config,
109             address,
110             &outStream);
111     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);
114 
115     // 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 
121         status = hwDev()->openOutputStream(
122                 handle,
123                 deviceType,
124                 customFlags,
125                 &customConfig,
126                 address,
127                 &outStream);
128         ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
129     }
130 
131     if (status == NO_ERROR) {
132         stream = outStream;
133         mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
134         status = stream->getFrameSize(&mHalFrameSize);
135         LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
136         LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
137                 " zero", mHalFrameSize);
138 
139     }
140 
141     return status;
142 }
143 
getAudioProperties() const144 audio_config_base_t AudioStreamOut::getAudioProperties() const
145 {
146     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;
153 }
154 
flush()155 int AudioStreamOut::flush()
156 {
157     mFramesWritten = 0;
158     mFramesWrittenAtStandby = 0;
159     const status_t result = stream->flush();
160     return result != INVALID_OPERATION ? result : NO_ERROR;
161 }
162 
standby()163 int AudioStreamOut::standby()
164 {
165     mFramesWrittenAtStandby = mFramesWritten;
166     return stream->standby();
167 }
168 
presentationComplete()169 void AudioStreamOut::presentationComplete() {
170     stream->presentationComplete();
171 }
172 
write(const void * buffer,size_t numBytes)173 ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
174 {
175     size_t bytesWritten;
176     const status_t result = stream->write(buffer, numBytes, &bytesWritten);
177     if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
178         mFramesWritten += bytesWritten / mHalFrameSize;
179     }
180     return result == OK ? bytesWritten : result;
181 }
182 
183 } // namespace android
184