jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | #include <algorithm> |
| 18 | |
| 19 | #include <android-base/stringprintf.h> |
| 20 | #include <media/AudioPortBase.h> |
| 21 | #include <utils/Log.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 25 | void AudioPortBase::toAudioPort(struct audio_port *port) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 26 | // TODO: update this function once audio_port structure reflects the new profile definition. |
| 27 | // For compatibility reason: flatening the AudioProfile into audio_port structure. |
| 28 | FormatSet flatenedFormats; |
| 29 | SampleRateSet flatenedRates; |
| 30 | ChannelMaskSet flatenedChannels; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 31 | for (const auto& profile : mProfiles) { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 32 | if (profile->isValid()) { |
| 33 | audio_format_t formatToExport = profile->getFormat(); |
| 34 | const SampleRateSet &ratesToExport = profile->getSampleRates(); |
| 35 | const ChannelMaskSet &channelsToExport = profile->getChannels(); |
| 36 | |
| 37 | flatenedFormats.insert(formatToExport); |
| 38 | flatenedRates.insert(ratesToExport.begin(), ratesToExport.end()); |
| 39 | flatenedChannels.insert(channelsToExport.begin(), channelsToExport.end()); |
| 40 | |
| 41 | if (flatenedRates.size() > AUDIO_PORT_MAX_SAMPLING_RATES || |
| 42 | flatenedChannels.size() > AUDIO_PORT_MAX_CHANNEL_MASKS || |
| 43 | flatenedFormats.size() > AUDIO_PORT_MAX_FORMATS) { |
| 44 | ALOGE("%s: bailing out: cannot export profiles to port config", __func__); |
| 45 | return; |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | port->role = mRole; |
| 50 | port->type = mType; |
| 51 | strlcpy(port->name, mName.c_str(), AUDIO_PORT_MAX_NAME_LEN); |
| 52 | port->num_sample_rates = flatenedRates.size(); |
| 53 | port->num_channel_masks = flatenedChannels.size(); |
| 54 | port->num_formats = flatenedFormats.size(); |
| 55 | std::copy(flatenedRates.begin(), flatenedRates.end(), port->sample_rates); |
| 56 | std::copy(flatenedChannels.begin(), flatenedChannels.end(), port->channel_masks); |
| 57 | std::copy(flatenedFormats.begin(), flatenedFormats.end(), port->formats); |
| 58 | |
| 59 | ALOGV("AudioPort::toAudioPort() num gains %zu", mGains.size()); |
| 60 | |
| 61 | port->num_gains = std::min(mGains.size(), (size_t) AUDIO_PORT_MAX_GAINS); |
| 62 | for (size_t i = 0; i < port->num_gains; i++) { |
| 63 | port->gains[i] = mGains[i]->getGain(); |
| 64 | } |
| 65 | } |
| 66 | |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 67 | void AudioPortBase::dump(std::string *dst, int spaces, bool verbose) const { |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 68 | if (!mName.empty()) { |
| 69 | dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str())); |
| 70 | } |
| 71 | if (verbose) { |
| 72 | std::string profilesStr; |
jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 73 | mProfiles.dump(&profilesStr, spaces); |
jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 74 | dst->append(profilesStr); |
| 75 | |
| 76 | if (mGains.size() != 0) { |
| 77 | dst->append(base::StringPrintf("%*s- gains:\n", spaces, "")); |
| 78 | for (size_t i = 0; i < mGains.size(); i++) { |
| 79 | std::string gainStr; |
| 80 | mGains[i]->dump(&gainStr, spaces + 2, i); |
| 81 | dst->append(gainStr); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | } |