blob: da0ad11be2e1f401dbe05963e960af8fff06e0d8 [file] [log] [blame]
jiabin253bd322023-01-25 23:57:31 +00001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mikhail Naganov49712b52023-06-27 16:39:33 -070017#include <limits>
18
jiabin253bd322023-01-25 23:57:31 +000019#define LOG_TAG "AHAL_StreamUsb"
20#include <android-base/logging.h>
21
jiabin783c48b2023-02-28 18:28:06 +000022#include <Utils.h>
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070023#include <error/expected_utils.h>
jiabin783c48b2023-02-28 18:28:06 +000024
25#include "UsbAlsaMixerControl.h"
jiabin253bd322023-01-25 23:57:31 +000026#include "core-impl/StreamUsb.h"
27
Mikhail Naganov872d4a62023-03-09 18:19:01 -080028using aidl::android::hardware::audio::common::getChannelCount;
jiabin253bd322023-01-25 23:57:31 +000029using aidl::android::hardware::audio::common::SinkMetadata;
30using aidl::android::hardware::audio::common::SourceMetadata;
31using aidl::android::media::audio::common::AudioDevice;
jiabin253bd322023-01-25 23:57:31 +000032using aidl::android::media::audio::common::AudioOffloadInfo;
Mikhail Naganov6725ef52023-02-09 17:52:50 -080033using aidl::android::media::audio::common::MicrophoneDynamicInfo;
34using aidl::android::media::audio::common::MicrophoneInfo;
jiabin253bd322023-01-25 23:57:31 +000035
36namespace aidl::android::hardware::audio::core {
37
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070038StreamUsb::StreamUsb(const Metadata& metadata, StreamContext&& context)
Mikhail Naganovc337a872023-07-07 12:01:17 -070039 : StreamAlsa(metadata, std::move(context)) {}
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070040
41ndk::ScopedAStatus StreamUsb::setConnectedDevices(
jiabin253bd322023-01-25 23:57:31 +000042 const std::vector<AudioDevice>& connectedDevices) {
43 if (mIsInput && connectedDevices.size() > 1) {
44 LOG(ERROR) << __func__ << ": wrong device size(" << connectedDevices.size()
45 << ") for input stream";
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070046 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
jiabin253bd322023-01-25 23:57:31 +000047 }
Mikhail Naganovc337a872023-07-07 12:01:17 -070048 std::vector<alsa::DeviceProfile> connectedDeviceProfiles;
jiabin253bd322023-01-25 23:57:31 +000049 for (const auto& connectedDevice : connectedDevices) {
Mikhail Naganovc337a872023-07-07 12:01:17 -070050 auto profile = alsa::getDeviceProfile(connectedDevice, mIsInput);
51 if (!profile.has_value()) {
52 LOG(ERROR) << __func__
53 << ": unsupported device address=" << connectedDevice.address.toString();
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070054 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
jiabin253bd322023-01-25 23:57:31 +000055 }
Mikhail Naganovc337a872023-07-07 12:01:17 -070056 connectedDeviceProfiles.push_back(*profile);
jiabin253bd322023-01-25 23:57:31 +000057 }
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070058 RETURN_STATUS_IF_ERROR(StreamCommonImpl::setConnectedDevices(connectedDevices));
Mikhail Naganovc337a872023-07-07 12:01:17 -070059 std::lock_guard guard(mLock);
60 mConnectedDeviceProfiles = std::move(connectedDeviceProfiles);
Mikhail Naganov49712b52023-06-27 16:39:33 -070061 mConnectedDevicesUpdated.store(true, std::memory_order_release);
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070062 return ndk::ScopedAStatus::ok();
jiabin253bd322023-01-25 23:57:31 +000063}
64
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070065::android::status_t StreamUsb::drain(StreamDescriptor::DrainMode) {
jiabin253bd322023-01-25 23:57:31 +000066 usleep(1000);
67 return ::android::OK;
68}
69
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070070::android::status_t StreamUsb::flush() {
jiabin253bd322023-01-25 23:57:31 +000071 usleep(1000);
72 return ::android::OK;
73}
74
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070075::android::status_t StreamUsb::pause() {
jiabin253bd322023-01-25 23:57:31 +000076 usleep(1000);
77 return ::android::OK;
78}
79
Mikhail Naganovd5554cf2023-06-21 15:20:31 -070080::android::status_t StreamUsb::transfer(void* buffer, size_t frameCount, size_t* actualFrameCount,
jiabin253bd322023-01-25 23:57:31 +000081 int32_t* latencyMs) {
Mikhail Naganov49712b52023-06-27 16:39:33 -070082 if (mConnectedDevicesUpdated.load(std::memory_order_acquire)) {
Mikhail Naganovc337a872023-07-07 12:01:17 -070083 // 'setConnectedDevices' was called. I/O will be restarted.
Mikhail Naganov49712b52023-06-27 16:39:33 -070084 *actualFrameCount = 0;
85 *latencyMs = StreamDescriptor::LATENCY_UNKNOWN;
86 return ::android::OK;
jiabin253bd322023-01-25 23:57:31 +000087 }
Mikhail Naganovc337a872023-07-07 12:01:17 -070088 return StreamAlsa::transfer(buffer, frameCount, actualFrameCount, latencyMs);
jiabin253bd322023-01-25 23:57:31 +000089}
90
Mikhail Naganovc337a872023-07-07 12:01:17 -070091std::vector<alsa::DeviceProfile> StreamUsb::getDeviceProfiles() {
92 std::vector<alsa::DeviceProfile> connectedDevices;
jiabin253bd322023-01-25 23:57:31 +000093 {
94 std::lock_guard guard(mLock);
Mikhail Naganovc337a872023-07-07 12:01:17 -070095 connectedDevices = mConnectedDeviceProfiles;
Mikhail Naganov49712b52023-06-27 16:39:33 -070096 mConnectedDevicesUpdated.store(false, std::memory_order_release);
jiabin253bd322023-01-25 23:57:31 +000097 }
Mikhail Naganovc337a872023-07-07 12:01:17 -070098 return connectedDevices;
jiabin253bd322023-01-25 23:57:31 +000099}
100
jiabin253bd322023-01-25 23:57:31 +0000101StreamInUsb::StreamInUsb(const SinkMetadata& sinkMetadata, StreamContext&& context,
102 const std::vector<MicrophoneInfo>& microphones)
Mikhail Naganovd5554cf2023-06-21 15:20:31 -0700103 : StreamUsb(sinkMetadata, std::move(context)), StreamIn(microphones) {}
jiabin253bd322023-01-25 23:57:31 +0000104
105ndk::ScopedAStatus StreamInUsb::getActiveMicrophones(
106 std::vector<MicrophoneDynamicInfo>* _aidl_return __unused) {
107 LOG(DEBUG) << __func__ << ": not supported";
108 return ndk::ScopedAStatus::fromExceptionCode(EX_UNSUPPORTED_OPERATION);
109}
110
jiabin253bd322023-01-25 23:57:31 +0000111StreamOutUsb::StreamOutUsb(const SourceMetadata& sourceMetadata, StreamContext&& context,
112 const std::optional<AudioOffloadInfo>& offloadInfo)
Mikhail Naganovc337a872023-07-07 12:01:17 -0700113 : StreamUsb(sourceMetadata, std::move(context)),
114 StreamOut(offloadInfo),
115 mChannelCount(getChannelCount(getContext().getChannelLayout())) {}
jiabin783c48b2023-02-28 18:28:06 +0000116
117ndk::ScopedAStatus StreamOutUsb::getHwVolume(std::vector<float>* _aidl_return) {
118 *_aidl_return = mHwVolumes;
119 return ndk::ScopedAStatus::ok();
120}
121
122ndk::ScopedAStatus StreamOutUsb::setHwVolume(const std::vector<float>& in_channelVolumes) {
Mikhail Naganovc337a872023-07-07 12:01:17 -0700123 // Avoid using mConnectedDeviceProfiles because it requires a lock.
Mikhail Naganovd5554cf2023-06-21 15:20:31 -0700124 for (const auto& device : getConnectedDevices()) {
Mikhail Naganovc337a872023-07-07 12:01:17 -0700125 if (auto deviceProfile = alsa::getDeviceProfile(device, mIsInput);
126 deviceProfile.has_value()) {
127 if (auto result = usb::UsbAlsaMixerControl::getInstance().setVolumes(
128 deviceProfile->card, in_channelVolumes);
129 !result.isOk()) {
130 LOG(ERROR) << __func__
131 << ": failed to set volume for device address=" << *deviceProfile;
132 return result;
133 }
jiabin783c48b2023-02-28 18:28:06 +0000134 }
135 }
136 mHwVolumes = in_channelVolumes;
137 return ndk::ScopedAStatus::ok();
138}
jiabin253bd322023-01-25 23:57:31 +0000139
Mikhail Naganov6725ef52023-02-09 17:52:50 -0800140} // namespace aidl::android::hardware::audio::core