blob: 8b41be7ec828ac651166fc659bc12be970e01e0f [file] [log] [blame]
jiabin5740f082019-08-19 15:08:30 -07001/*
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
23namespace android {
24
jiabin3e277cc2019-09-10 14:27:34 -070025void AudioPortBase::toAudioPort(struct audio_port *port) const {
jiabin5740f082019-08-19 15:08:30 -070026 // 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;
jiabin3e277cc2019-09-10 14:27:34 -070031 for (const auto& profile : mProfiles) {
jiabin5740f082019-08-19 15:08:30 -070032 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
jiabin3e277cc2019-09-10 14:27:34 -070067void AudioPortBase::dump(std::string *dst, int spaces, bool verbose) const {
jiabin5740f082019-08-19 15:08:30 -070068 if (!mName.empty()) {
69 dst->append(base::StringPrintf("%*s- name: %s\n", spaces, "", mName.c_str()));
70 }
71 if (verbose) {
72 std::string profilesStr;
jiabin3e277cc2019-09-10 14:27:34 -070073 mProfiles.dump(&profilesStr, spaces);
jiabin5740f082019-08-19 15:08:30 -070074 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}