blob: 95e9ecc4f5e82e83385d4597cc3ac0cea5572d33 [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,
46 const char *address)
47{
48
49 struct audio_config originalConfig = *config;
Andy Hung1ef77382023-06-15 14:50:18 -070050 auto outputStream = new AudioStreamOut(this, flags);
Phil Burk062e67a2015-02-11 13:40:50 -080051
52 // Try to open the HAL first using the current format.
Dean Wheatley6c009512023-10-23 09:34:14 +110053 ALOGV("openOutputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate,
54 config->format, config->channel_mask);
jiabin43810402019-10-24 14:58:31 -070055 status_t status = outputStream->open(handle, deviceType, config, address);
Phil Burk062e67a2015-02-11 13:40:50 -080056
57 if (status != NO_ERROR) {
58 delete outputStream;
Andy Hung1ef77382023-06-15 14:50:18 -070059 outputStream = nullptr;
Phil Burk062e67a2015-02-11 13:40:50 -080060
61 // FIXME Look at any modification to the config.
62 // The HAL might modify the config to suggest a wrapped format.
63 // Log this so we can see what the HALs are doing.
Dean Wheatley6c009512023-10-23 09:34:14 +110064 ALOGI("openOutputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x,"
65 " status %d", config->sample_rate, config->format, config->channel_mask, status);
Phil Burk062e67a2015-02-11 13:40:50 -080066
67 // If the data is encoded then try again using wrapped PCM.
Andy Hung1ef77382023-06-15 14:50:18 -070068 const bool wrapperNeeded = !audio_has_proportional_frames(originalConfig.format)
Phil Burk062e67a2015-02-11 13:40:50 -080069 && ((flags & AUDIO_OUTPUT_FLAG_DIRECT) != 0)
70 && ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0);
71
Phil Burk062e67a2015-02-11 13:40:50 -080072 if (wrapperNeeded) {
Phil Burk23d89972015-04-06 16:22:23 -070073 if (SPDIFEncoder::isFormatSupported(originalConfig.format)) {
74 outputStream = new SpdifStreamOut(this, flags, originalConfig.format);
jiabin43810402019-10-24 14:58:31 -070075 status = outputStream->open(handle, deviceType, &originalConfig, address);
Phil Burk23d89972015-04-06 16:22:23 -070076 if (status != NO_ERROR) {
77 ALOGE("ERROR - openOutputStream(), SPDIF open returned %d",
78 status);
79 delete outputStream;
Andy Hung1ef77382023-06-15 14:50:18 -070080 outputStream = nullptr;
Phil Burk23d89972015-04-06 16:22:23 -070081 }
82 } else {
83 ALOGE("ERROR - openOutputStream(), SPDIFEncoder does not support format 0x%08x",
84 originalConfig.format);
Phil Burk062e67a2015-02-11 13:40:50 -080085 }
86 }
87 }
88
89 *ppStreamOut = outputStream;
90 return status;
91}
92
Dean Wheatley6c009512023-10-23 09:34:14 +110093status_t AudioHwDevice::openInputStream(
94 AudioStreamIn **ppStreamIn,
95 audio_io_handle_t handle,
96 audio_devices_t deviceType,
97 audio_input_flags_t flags,
98 struct audio_config *config,
99 const char *address,
100 audio_source_t source,
101 audio_devices_t outputDevice,
102 const char *outputDeviceAddress) {
103
104 struct audio_config originalConfig = *config;
105 auto inputStream = new AudioStreamIn(this, flags);
106
107 // Try to open the HAL first using the current format.
108 ALOGV("openInputStream(), try sampleRate %d, format %#x, channelMask %#x", config->sample_rate,
109 config->format, config->channel_mask);
110 status_t status = inputStream->open(handle, deviceType, config, address, source, outputDevice,
111 outputDeviceAddress);
112
113 // If the input could not be opened with the requested parameters and we can handle the
114 // conversion internally, try to open again with the proposed parameters.
115 if (status == BAD_VALUE &&
116 audio_is_linear_pcm(originalConfig.format) &&
117 audio_is_linear_pcm(config->format) &&
118 (config->sample_rate <= AUDIO_RESAMPLER_DOWN_RATIO_MAX * config->sample_rate) &&
119 (audio_channel_count_from_in_mask(config->channel_mask) <= FCC_LIMIT) &&
120 (audio_channel_count_from_in_mask(originalConfig.channel_mask) <= FCC_LIMIT)) {
121 // FIXME describe the change proposed by HAL (save old values so we can log them here)
122 ALOGV("openInputStream() reopening with proposed sampling rate and channel mask");
123 status = inputStream->open(handle, deviceType, config, address, source,
124 outputDevice, outputDeviceAddress);
125 // FIXME log this new status; HAL should not propose any further changes
126 if (status != NO_ERROR) {
127 delete inputStream;
128 inputStream = nullptr;
129 }
130 } else if (status != NO_ERROR) {
131 delete inputStream;
132 inputStream = nullptr;
133
134 // FIXME Look at any modification to the config.
135 // The HAL might modify the config to suggest a wrapped format.
136 // Log this so we can see what the HALs are doing.
137 ALOGI("openInputStream(), HAL returned sampleRate %d, format %#x, channelMask %#x,"
138 " status %d", config->sample_rate, config->format, config->channel_mask, status);
139
140 // If the data is encoded then try again using wrapped PCM.
141 const bool unwrapperNeeded = !audio_has_proportional_frames(originalConfig.format)
142 && ((flags & AUDIO_INPUT_FLAG_DIRECT) != 0);
143
144 if (unwrapperNeeded) {
145 if (SPDIFDecoder::isFormatSupported(originalConfig.format)) {
146 inputStream = new SpdifStreamIn(this, flags, originalConfig.format);
147 status = inputStream->open(handle, deviceType, &originalConfig, address, source,
148 outputDevice, outputDeviceAddress);
149 if (status != NO_ERROR) {
150 ALOGE("ERROR - openInputStream(), SPDIF open returned %d",
151 status);
152 delete inputStream;
153 inputStream = nullptr;
154 }
155 } else {
156 ALOGE("ERROR - openInputStream(), SPDIFDecoder does not support format 0x%08x",
157 originalConfig.format);
158 }
159 }
160 }
161
162 *ppStreamIn = inputStream;
163 return status;
164}
165
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700166bool AudioHwDevice::supportsAudioPatches() const {
167 bool result;
168 return mHwDevice->supportsAudioPatches(&result) == OK ? result : false;
Mikhail Naganove4f1f632016-08-31 11:35:10 -0700169}
Phil Burk062e67a2015-02-11 13:40:50 -0800170
jiabinb4fed192020-09-22 14:45:40 -0700171status_t AudioHwDevice::getAudioPort(struct audio_port_v7 *port) const {
172 return mHwDevice->getAudioPort(port);
173}
174
Jiabin Huangebe64102021-09-07 20:01:07 +0000175status_t AudioHwDevice::getMmapPolicyInfos(
jiabine99d0882021-09-17 05:21:25 +0000176 AudioMMapPolicyType policyType, std::vector<AudioMMapPolicyInfo> *policyInfos) const {
Jiabin Huangebe64102021-09-07 20:01:07 +0000177 return mHwDevice->getMmapPolicyInfos(policyType, policyInfos);
178}
179
jiabine504e7b2021-09-18 00:27:08 +0000180int32_t AudioHwDevice::getAAudioMixerBurstCount() const {
181 return mHwDevice->getAAudioMixerBurstCount();
182}
183
184int32_t AudioHwDevice::getAAudioHardwareBurstMinUsec() const {
185 return mHwDevice->getAAudioHardwareBurstMinUsec();
186}
187
jiabin12537fc2023-10-12 17:56:08 +0000188status_t AudioHwDevice::getAudioMixPort(const struct audio_port_v7 *devicePort,
189 struct audio_port_v7 *mixPort) const {
190 return mHwDevice->getAudioMixPort(devicePort, mixPort);
191}
192
Jiabin Huangebe64102021-09-07 20:01:07 +0000193
Phil Burk062e67a2015-02-11 13:40:50 -0800194}; // namespace android