1 /* 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 17 #define LOG_TAG "AHAL_ModuleAlsa" 18 19 #include <vector> 20 21 #include <android-base/logging.h> 22 23 #include "Utils.h" 24 #include "core-impl/ModuleAlsa.h" 25 26 extern "C" { 27 #include "alsa_device_profile.h" 28 } 29 30 using aidl::android::media::audio::common::AudioChannelLayout; 31 using aidl::android::media::audio::common::AudioFormatType; 32 using aidl::android::media::audio::common::AudioPort; 33 using aidl::android::media::audio::common::AudioProfile; 34 35 namespace aidl::android::hardware::audio::core { 36 populateConnectedDevicePort(AudioPort * audioPort,int32_t)37ndk::ScopedAStatus ModuleAlsa::populateConnectedDevicePort(AudioPort* audioPort, int32_t) { 38 auto deviceProfile = alsa::getDeviceProfile(*audioPort); 39 if (!deviceProfile.has_value()) { 40 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT); 41 } 42 auto proxy = alsa::readAlsaDeviceInfo(*deviceProfile); 43 if (proxy.get() == nullptr) { 44 return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE); 45 } 46 47 alsa_device_profile* profile = proxy.getProfile(); 48 std::vector<AudioChannelLayout> channels = alsa::getChannelMasksFromProfile(profile); 49 std::vector<int> sampleRates = alsa::getSampleRatesFromProfile(profile); 50 51 for (size_t i = 0; i < std::min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) && 52 profile->formats[i] != PCM_FORMAT_INVALID; 53 ++i) { 54 auto audioFormatDescription = 55 alsa::c2aidl_pcm_format_AudioFormatDescription(profile->formats[i]); 56 if (audioFormatDescription.type == AudioFormatType::DEFAULT) { 57 LOG(WARNING) << __func__ << ": unknown pcm type=" << profile->formats[i]; 58 continue; 59 } 60 AudioProfile audioProfile = {.format = audioFormatDescription, 61 .channelMasks = channels, 62 .sampleRates = sampleRates}; 63 audioPort->profiles.push_back(std::move(audioProfile)); 64 } 65 return ndk::ScopedAStatus::ok(); 66 } 67 68 } // namespace aidl::android::hardware::audio::core 69