xref: /aosp_15_r20/frameworks/av/services/audioflinger/datapath/AudioStreamOut.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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)33 AudioStreamOut::AudioStreamOut(AudioHwDevice *dev)
34         : audioHwDev(dev)
35 {
36 }
37 
38 // This must be defined here together with the HAL includes above and
39 // not solely in the header.
40 AudioStreamOut::~AudioStreamOut() = default;
41 
hwDev() const42 sp<DeviceHalInterface> AudioStreamOut::hwDev() const
43 {
44     return audioHwDev->hwDevice();
45 }
46 
getRenderPosition(uint64_t * frames)47 status_t AudioStreamOut::getRenderPosition(uint64_t *frames)
48 {
49     if (stream == nullptr) {
50         return NO_INIT;
51     }
52 
53     uint64_t halPosition = 0;
54     const status_t status = stream->getRenderPosition(&halPosition);
55     if (status != NO_ERROR) {
56         return status;
57     }
58     // Scale from HAL sample rate to application rate.
59     *frames = halPosition / mRateMultiplier;
60 
61     return status;
62 }
63 
getPresentationPosition(uint64_t * frames,struct timespec * timestamp)64 status_t AudioStreamOut::getPresentationPosition(uint64_t *frames, struct timespec *timestamp)
65 {
66     if (stream == nullptr) {
67         return NO_INIT;
68     }
69 
70     uint64_t halPosition = 0;
71     const status_t status = stream->getPresentationPosition(&halPosition, timestamp);
72     if (status != NO_ERROR) {
73         return status;
74     }
75 
76     if (mHalFormatHasProportionalFrames &&
77             (flags & AUDIO_OUTPUT_FLAG_DIRECT) == AUDIO_OUTPUT_FLAG_DIRECT) {
78         // For DirectTrack reset position to 0 on standby.
79         const uint64_t adjustedPosition = (halPosition <= mFramesWrittenAtStandby) ?
80                 0 : (halPosition - mFramesWrittenAtStandby);
81         // Scale from HAL sample rate to application rate.
82         *frames = adjustedPosition / mRateMultiplier;
83     } else {
84         // For offloaded MP3 and other compressed formats, and linear PCM.
85         *frames = halPosition;
86     }
87 
88     return status;
89 }
90 
open(audio_io_handle_t handle,audio_devices_t deviceType,struct audio_config * config,audio_output_flags_t * flagsPtr,const char * address,const std::vector<playback_track_metadata_v7_t> & sourceMetadata)91 status_t AudioStreamOut::open(
92         audio_io_handle_t handle,
93         audio_devices_t deviceType,
94         struct audio_config *config,
95         audio_output_flags_t *flagsPtr,
96         const char *address,
97         const std::vector<playback_track_metadata_v7_t>& sourceMetadata)
98 {
99     sp<StreamOutHalInterface> outStream;
100 
101     audio_output_flags_t customFlags = (config->format == AUDIO_FORMAT_IEC61937)
102                 ? (audio_output_flags_t)(*flagsPtr | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO)
103                 : *flagsPtr;
104     *flagsPtr = flags = customFlags;
105 
106     int status = hwDev()->openOutputStream(
107             handle,
108             deviceType,
109             customFlags,
110             config,
111             address,
112             &outStream,
113             sourceMetadata);
114     ALOGV("AudioStreamOut::open(), HAL returned stream %p, sampleRate %d, format %#x,"
115             " channelMask %#x, status %d", outStream.get(), config->sample_rate, config->format,
116             config->channel_mask, status);
117 
118     // Some HALs may not recognize AUDIO_FORMAT_IEC61937. But if we declare
119     // it as PCM then it will probably work.
120     if (status != NO_ERROR && config->format == AUDIO_FORMAT_IEC61937) {
121         struct audio_config customConfig = *config;
122         customConfig.format = AUDIO_FORMAT_PCM_16_BIT;
123 
124         status = hwDev()->openOutputStream(
125                 handle,
126                 deviceType,
127                 customFlags,
128                 &customConfig,
129                 address,
130                 &outStream,
131                 sourceMetadata);
132         ALOGV("AudioStreamOut::open(), treat IEC61937 as PCM, status = %d", status);
133     }
134 
135     if (status == NO_ERROR) {
136         stream = outStream;
137         mHalFormatHasProportionalFrames = audio_has_proportional_frames(config->format);
138         status = stream->getFrameSize(&mHalFrameSize);
139         LOG_ALWAYS_FATAL_IF(status != OK, "Error retrieving frame size from HAL: %d", status);
140         LOG_ALWAYS_FATAL_IF(mHalFrameSize == 0, "Error frame size was %zu but must be greater than"
141                 " zero", mHalFrameSize);
142 
143     }
144 
145     return status;
146 }
147 
getAudioProperties() const148 audio_config_base_t AudioStreamOut::getAudioProperties() const
149 {
150     audio_config_base_t result = AUDIO_CONFIG_BASE_INITIALIZER;
151     if (stream->getAudioProperties(&result) != OK) {
152         result.sample_rate = 0;
153         result.channel_mask = AUDIO_CHANNEL_INVALID;
154         result.format = AUDIO_FORMAT_INVALID;
155     }
156     return result;
157 }
158 
flush()159 int AudioStreamOut::flush()
160 {
161     mFramesWritten = 0;
162     mFramesWrittenAtStandby = 0;
163     const status_t result = stream->flush();
164     return result != INVALID_OPERATION ? result : NO_ERROR;
165 }
166 
standby()167 int AudioStreamOut::standby()
168 {
169     mFramesWrittenAtStandby = mFramesWritten;
170     return stream->standby();
171 }
172 
presentationComplete()173 void AudioStreamOut::presentationComplete() {
174     stream->presentationComplete();
175 }
176 
write(const void * buffer,size_t numBytes)177 ssize_t AudioStreamOut::write(const void *buffer, size_t numBytes)
178 {
179     size_t bytesWritten;
180     const status_t result = stream->write(buffer, numBytes, &bytesWritten);
181     if (result == OK && bytesWritten > 0 && mHalFrameSize > 0) {
182         mFramesWritten += bytesWritten / mHalFrameSize;
183     }
184     return result == OK ? bytesWritten : result;
185 }
186 
187 } // namespace android
188