• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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)37 ndk::ScopedAStatus ModuleAlsa::populateConnectedDevicePort(AudioPort* audioPort) {
38     auto deviceProfile = alsa::getDeviceProfile(*audioPort);
39     if (!deviceProfile.has_value()) {
40         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_ARGUMENT);
41     }
42     auto profile = alsa::readAlsaDeviceInfo(*deviceProfile);
43     if (!profile.has_value()) {
44         return ndk::ScopedAStatus::fromExceptionCode(EX_ILLEGAL_STATE);
45     }
46 
47     std::vector<AudioChannelLayout> channels = alsa::getChannelMasksFromProfile(&profile.value());
48     std::vector<int> sampleRates = alsa::getSampleRatesFromProfile(&profile.value());
49 
50     for (size_t i = 0; i < std::min(MAX_PROFILE_FORMATS, AUDIO_PORT_MAX_AUDIO_PROFILES) &&
51                        profile->formats[i] != PCM_FORMAT_INVALID;
52          ++i) {
53         auto audioFormatDescription =
54                 alsa::c2aidl_pcm_format_AudioFormatDescription(profile->formats[i]);
55         if (audioFormatDescription.type == AudioFormatType::DEFAULT) {
56             LOG(WARNING) << __func__ << ": unknown pcm type=" << profile->formats[i];
57             continue;
58         }
59         AudioProfile audioProfile = {.format = audioFormatDescription,
60                                      .channelMasks = channels,
61                                      .sampleRates = sampleRates};
62         audioPort->profiles.push_back(std::move(audioProfile));
63     }
64     return ndk::ScopedAStatus::ok();
65 }
66 
67 }  // namespace aidl::android::hardware::audio::core
68