blob: 5314e9e5e00816901f99d73aeb6048c06cddb055 [file] [log] [blame]
Phil Burk062e67a2015-02-11 13:40:50 -08001/*
Dean Wheatley6c009512023-10-23 09:34:14 +11002 *
3 * Copyright 2007, 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 */
Phil Burk062e67a2015-02-11 13:40:50 -080017
18#define LOG_TAG "AudioHwDevice"
19//#define LOG_NDEBUG 0
20
Mikhail Naganovcbc8f612016-10-11 18:05:13 -070021#include <system/audio.h>
Phil Burk062e67a2015-02-11 13:40:50 -080022#include <utils/Log.h>
23
Dean Wheatley6c009512023-10-23 09:34:14 +110024#include <audio_utils/spdif/SPDIFDecoder.h>
Phil Burk062e67a2015-02-11 13:40:50 -080025#include <audio_utils/spdif/SPDIFEncoder.h>
Dean Wheatley6c009512023-10-23 09:34:14 +110026#include <media/AudioResamplerPublic.h>
Phil Burk062e67a2015-02-11 13:40:50 -080027
28#include "AudioHwDevice.h"
29#include "AudioStreamOut.h"
Dean Wheatley6c009512023-10-23 09:34:14 +110030#include "SpdifStreamIn.h"
Phil Burk062e67a2015-02-11 13:40:50 -080031#include "SpdifStreamOut.h"
32
33namespace android {
34
jiabine99d0882021-09-17 05:21:25 +000035using media::audio::common::AudioMMapPolicyInfo;
36using media::audio::common::AudioMMapPolicyType;
37
Phil Burk062e67a2015-02-11 13:40:50 -080038// ----------------------------------------------------------------------------
39
40status_t AudioHwDevice::openOutputStream(
41 AudioStreamOut **ppStreamOut,
42 audio_io_handle_t handle,
jiabin43810402019-10-24 14:58:31 -070043 audio_devices_t deviceType,
Phil Burk062e67a2015-02-11 13:40:50 -080044 audio_output_flags_t flags,
45 struct audio_config *config,
Haofan Wangb75aa6a2024-07-09 23:06:58 -070046 const char *address,
47 const std::vector<playback_track_metadata_v7_t>& sourceMetadata)
Phil Burk062e67a2015-02-11 13:40:50 -080048{
49
50 struct audio_config originalConfig = *config;
Andy Hung1ef77382023-06-15 14:50:18 -070051 auto outputStream = new AudioStreamOut(this, flags);
Phil Burk062e67a2015-02-11 13:40:50 -080052
53 // Try to open the HAL first using the current format.
Dean Wheatley6c009512023-10-23 09:34:14 +110054 ALOGV("openOutputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate,
55 config->format, config->channel_mask);
Haofan Wangb75aa6a2024-07-09 23:06:58 -070056 status_t status = outputStream->open(handle, deviceType, config, address, sourceMetadata);
Phil Burk062e67a2015-02-11 13:40:50 -080057
58 if (status != NO_ERROR) {
59 delete outputStream;
Andy Hung1ef77382023-06-15 14:50:18 -070060 outputStream = nullptr;
Phil Burk062e67a2015-02-11 13:40:50 -080061
62 // FIXME Look at any modification to the config.
63 // The HAL might modify the config to suggest a wrapped format.
64 // Log this so we can see what the HALs are doing.
Dean Wheatley6c009512023-10-23 09:34:14 +110065 ALOGI("openOutputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x,"
66 " status %d", config->sample_rate, config->format, config->channel_mask, status);
Phil Burk062e67a2015-02-11 13:40:50 -080067
68 // If the data is encoded then try again using wrapped PCM.
Andy Hung1ef77382023-06-15 14:50:18 -070069 const bool wrapperNeeded = !audio_has_proportional_frames(originalConfig.format)
Phil Burk062e67a2015-02-11 13:40:50 -080070 && ((flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)
71 && ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0);
72
Phil Burk062e67a2015-02-11 13:40:50 -080073 if (wrapperNeeded) {
Phil Burk23d89972015-04-06 16:22:23 -070074 if (SPDIFEncoder::isFormatSupported(originalConfig.format)) {
75 outputStream = new SpdifStreamOut(this, flags, originalConfig.format);
Haofan Wangb75aa6a2024-07-09 23:06:58 -070076 status = outputStream->open(handle, deviceType, &originalConfig, address,
77 sourceMetadata);
Phil Burk23d89972015-04-06 16:22:23 -070078 if (status != NO_ERROR) {
79 ALOGE("ERROR - openOutputStream(), SPDIF open returned %d",
80 status);
81 delete outputStream;
Andy Hung1ef77382023-06-15 14:50:18 -070082 outputStream = nullptr;
Phil Burk23d89972015-04-06 16:22:23 -070083 }
84 } else {
85 ALOGE("ERROR - openOutputStream(), SPDIFEncoder does not support format 0x%08x",
86 originalConfig.format);
Phil Burk062e67a2015-02-11 13:40:50 -080087 }
88 }
89 }
90
91 *ppStreamOut = outputStream;
92 return status;
93}
94
Dean Wheatley6c009512023-10-23 09:34:14 +110095status_t AudioHwDevice::openInputStream(
96 AudioStreamIn **ppStreamIn,
97 audio_io_handle_t handle,
98 audio_devices_t deviceType,
99 audio_input_flags_t flags,
100 struct audio_config *config,
101 const char *address,
102 audio_source_t source,
103 audio_devices_t outputDevice,
104 const char *outputDeviceAddress) {
105
106 struct audio_config originalConfig = *config;
107 auto inputStream = new AudioStreamIn(this, flags);
108
109 // Try to open the HAL first using the current format.
110 ALOGV("openInputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate,
111 config->format, config->channel_mask);
112 status_t status = inputStream->open(handle, deviceType, config, address, source, outputDevice,
113 outputDeviceAddress);
114
115 // If the input could not be opened with the requested parameters and we can handle the
116 // conversion internally, try to open again with the proposed parameters.
117 if (status == BAD_VALUE &&
118 audio_is_linear_pcm(originalConfig.format) &&
119 audio_is_linear_pcm(config->format) &&
120 (config->sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
121 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_LIMIT) &&
122 (audio_channel_count_from_in_mask(originalConfig.channel_mask) <= FCC_LIMIT)) {
123 // FIXME describe the change proposed by HAL (save old values so we can log them here)
124 ALOGV("openInputStream() reopening with proposed sampling rate and channel mask");
125 status = inputStream->open(handle, deviceType, config, address, source,
126 outputDevice, outputDeviceAddress);
127 // FIXME log this new status; HAL should not propose any further changes
128 if (status != NO_ERROR) {
129 delete inputStream;
130 inputStream = nullptr;
131 }
132 } else if (status != NO_ERROR) {
133 delete inputStream;
134 inputStream = nullptr;
135
136 // FIXME Look at any modification to the config.
137 // The HAL might modify the config to suggest a wrapped format.
138 // Log this so we can see what the HALs are doing.
139 ALOGI("openInputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x,"
140 " status %d", config->sample_rate, config->format, config->channel_mask, status);
141
142 // If the data is encoded then try again using wrapped PCM.
143 const bool unwrapperNeeded = !audio_has_proportional_frames(originalConfig.format)
144 && ((flags & AUDIO_INPUT_FLAG_DIRECT) != 0);
145
146 if (unwrapperNeeded) {
147 if (SPDIFDecoder::isFormatSupported(originalConfig.format)) {
148 inputStream = new SpdifStreamIn(this, flags, originalConfig.format);
149 status = inputStream->open(handle, deviceType, &originalConfig, address, source,
150 outputDevice, outputDeviceAddress);
151 if (status != NO_ERROR) {
152 ALOGE("ERROR - openInputStream(), SPDIF open returned %d",
153 status);
154 delete inputStream;
155 inputStream = nullptr;
156 }
157 } else {
158 ALOGE("ERROR - openInputStream(), SPDIFDecoder does not support format 0x%08x",
159 originalConfig.format);
160 }
161 }
162 }
163
164 *ppStreamIn = inputStream;
165 return status;
166}
167
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700168bool AudioHwDevice::supportsAudioPatches() const {
169 bool result;
170 return mHwDevice->supportsAudioPatches(&result) == OK ? result : false;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700171}
Phil Burk062e67a2015-02-11 13:40:50 -0800172
jiabinb4fed192020-09-22 14:45:40 -0700173status_t AudioHwDevice::getAudioPort(struct audio_port_v7 *port) const {
174 return mHwDevice->getAudioPort(port);
175}
176
Jiabin Huangebe64102021-09-07 20:01:07 +0000177status_t AudioHwDevice::getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +0000178 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) const {
Jiabin Huangebe64102021-09-07 20:01:07 +0000179 return mHwDevice->getMmapPolicyInfos(policyType, policyInfos);
180}
181
jiabine504e7b2021-09-18 00:27:08 +0000182int32_t AudioHwDevice::getAAudioMixerBurstCount() const {
183 return mHwDevice->getAAudioMixerBurstCount();
184}
185
186int32_t AudioHwDevice::getAAudioHardwareBurstMinUsec() const {
187 return mHwDevice->getAAudioHardwareBurstMinUsec();
188}
189
jiabin12537fc2023-10-12 17:56:08 +0000190status_t AudioHwDevice::getAudioMixPort(const struct audio_port_v7 *devicePort,
191 struct audio_port_v7 *mixPort) const {
192 return mHwDevice->getAudioMixPort(devicePort, mixPort);
193}
194
Jiabin Huangebe64102021-09-07 20:01:07 +0000195
Phil Burk062e67a2015-02-11 13:40:50 -0800196}; // namespace android