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 #define LOG_TAG "DeviceDescriptorBase"
18 //#define LOG_NDEBUG 0
19 
20 #include <android-base/stringprintf.h>
21 #include <audio_utils/string.h>
22 #include <media/DeviceDescriptorBase.h>
23 #include <media/TypeConverter.h>
24 
25 namespace android {
26 
DeviceDescriptorBase(audio_devices_t type)27 DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) :
28         DeviceDescriptorBase(type, "")
29 {
30 }
31 
DeviceDescriptorBase(audio_devices_t type,const std::string & address)32 DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type, const std::string& address) :
33         DeviceDescriptorBase(AudioDeviceTypeAddr(type, address))
34 {
35 }
36 
DeviceDescriptorBase(const AudioDeviceTypeAddr & deviceTypeAddr)37 DeviceDescriptorBase::DeviceDescriptorBase(const AudioDeviceTypeAddr &deviceTypeAddr) :
38         AudioPort("", AUDIO_PORT_TYPE_DEVICE,
39                   audio_is_output_device(deviceTypeAddr.mType) ? AUDIO_PORT_ROLE_SINK :
40                                          AUDIO_PORT_ROLE_SOURCE),
41         mDeviceTypeAddr(deviceTypeAddr)
42 {
43     if (mDeviceTypeAddr.mAddress.empty() && audio_is_remote_submix_device(mDeviceTypeAddr.mType)) {
44         mDeviceTypeAddr.mAddress = "0";
45     }
46 }
47 
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const48 void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig,
49                                              const struct audio_port_config *srcConfig) const
50 {
51     dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN;
52     if (mSamplingRate != 0) {
53         dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
54     }
55     if (mChannelMask != AUDIO_CHANNEL_NONE) {
56         dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
57     }
58     if (mFormat != AUDIO_FORMAT_INVALID) {
59         dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
60     }
61 
62     if (srcConfig != NULL) {
63         dstConfig->config_mask |= srcConfig->config_mask;
64     }
65 
66     AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
67 
68     dstConfig->role = audio_is_output_device(mDeviceTypeAddr.mType) ?
69                         AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
70     dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
71     dstConfig->ext.device.type = mDeviceTypeAddr.mType;
72 
73     (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mDeviceTypeAddr.getAddress());
74 }
75 
toAudioPort(struct audio_port * port) const76 void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const
77 {
78     ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceTypeAddr.mType);
79     AudioPort::toAudioPort(port);
80     toAudioPortConfig(&port->active_config);
81     port->id = mId;
82     port->ext.device.type = mDeviceTypeAddr.mType;
83     port->ext.device.encapsulation_modes = mEncapsulationModes;
84     port->ext.device.encapsulation_metadata_types = mEncapsulationMetadataTypes;
85     (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mDeviceTypeAddr.getAddress());
86 }
87 
setEncapsulationModes(uint32_t encapsulationModes)88 status_t DeviceDescriptorBase::setEncapsulationModes(uint32_t encapsulationModes) {
89     if ((encapsulationModes & ~AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS) != 0) {
90         return BAD_VALUE;
91     }
92     mEncapsulationModes = encapsulationModes & ~(1 << AUDIO_ENCAPSULATION_MODE_NONE);
93     return NO_ERROR;
94 }
95 
setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes)96 status_t DeviceDescriptorBase::setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes) {
97     if ((encapsulationMetadataTypes & ~AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS) != 0) {
98         return BAD_VALUE;
99     }
100     mEncapsulationMetadataTypes =
101             encapsulationMetadataTypes & ~(1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE);
102     return NO_ERROR;
103 }
104 
dump(std::string * dst,int spaces,int index,const char * extraInfo,bool verbose) const105 void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index,
106                                 const char* extraInfo, bool verbose) const
107 {
108     dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1));
109     if (mId != 0) {
110         dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId));
111     }
112 
113     if (extraInfo != nullptr) {
114         dst->append(extraInfo);
115     }
116 
117     dst->append(base::StringPrintf("%*s- type: %-48s\n",
118             spaces, "", ::android::toString(mDeviceTypeAddr.mType).c_str()));
119 
120     dst->append(base::StringPrintf(
121             "%*s- supported encapsulation modes: %u", spaces, "", mEncapsulationModes));
122     dst->append(base::StringPrintf(
123             "%*s- supported encapsulation metadata types: %u",
124             spaces, "", mEncapsulationMetadataTypes));
125 
126     if (mDeviceTypeAddr.mAddress.size() != 0) {
127         dst->append(base::StringPrintf(
128                 "%*s- address: %-32s\n", spaces, "", mDeviceTypeAddr.getAddress()));
129     }
130     AudioPort::dump(dst, spaces, verbose);
131 }
132 
toString() const133 std::string DeviceDescriptorBase::toString() const
134 {
135     std::stringstream sstream;
136     sstream << "type:0x" << std::hex << type() << ",@:" << mDeviceTypeAddr.mAddress;
137     return sstream.str();
138 }
139 
log() const140 void DeviceDescriptorBase::log() const
141 {
142     ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId,  mDeviceTypeAddr.mType,
143           ::android::toString(mDeviceTypeAddr.mType).c_str(),
144           mDeviceTypeAddr.getAddress());
145 
146     AudioPort::log("  ");
147 }
148 
equals(const sp<DeviceDescriptorBase> & other) const149 bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
150 {
151     return other != nullptr &&
152            static_cast<const AudioPort*>(this)->equals(other) &&
153            static_cast<const AudioPortConfig*>(this)->equals(other) &&
154            mDeviceTypeAddr.equals(other->mDeviceTypeAddr);
155 }
156 
writeToParcel(Parcel * parcel) const157 status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const
158 {
159     status_t status = NO_ERROR;
160     if ((status = AudioPort::writeToParcel(parcel)) != NO_ERROR) return status;
161     if ((status = AudioPortConfig::writeToParcel(parcel)) != NO_ERROR) return status;
162     if ((status = parcel->writeParcelable(mDeviceTypeAddr)) != NO_ERROR) return status;
163     if ((status = parcel->writeUint32(mEncapsulationModes)) != NO_ERROR) return status;
164     if ((status = parcel->writeUint32(mEncapsulationMetadataTypes)) != NO_ERROR) return status;
165     return status;
166 }
167 
readFromParcel(const Parcel * parcel)168 status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel)
169 {
170     status_t status = NO_ERROR;
171     if ((status = AudioPort::readFromParcel(parcel)) != NO_ERROR) return status;
172     if ((status = AudioPortConfig::readFromParcel(parcel)) != NO_ERROR) return status;
173     if ((status = parcel->readParcelable(&mDeviceTypeAddr)) != NO_ERROR) return status;
174     if ((status = parcel->readUint32(&mEncapsulationModes)) != NO_ERROR) return status;
175     if ((status = parcel->readUint32(&mEncapsulationMetadataTypes)) != NO_ERROR) return status;
176     return status;
177 }
178 
toString(const DeviceDescriptorBaseVector & devices)179 std::string toString(const DeviceDescriptorBaseVector& devices)
180 {
181     std::string ret;
182     for (const auto& device : devices) {
183         if (device != *devices.begin()) {
184             ret += ";";
185         }
186         ret += device->toString();
187     }
188     return ret;
189 }
190 
deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector & devices)191 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices)
192 {
193     AudioDeviceTypeAddrVector deviceTypeAddrs;
194     for (const auto& device : devices) {
195         deviceTypeAddrs.push_back(device->getDeviceTypeAddr());
196     }
197     return deviceTypeAddrs;
198 }
199 
200 } // namespace android
201