• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/AidlConversion.h>
23 #include <media/DeviceDescriptorBase.h>
24 #include <media/TypeConverter.h>
25 
26 namespace android {
27 
DeviceDescriptorBase(audio_devices_t type)28 DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) :
29         DeviceDescriptorBase(type, "")
30 {
31 }
32 
DeviceDescriptorBase(audio_devices_t type,const std::string & address)33 DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type, const std::string& address) :
34         DeviceDescriptorBase(AudioDeviceTypeAddr(type, address))
35 {
36 }
37 
DeviceDescriptorBase(const AudioDeviceTypeAddr & deviceTypeAddr)38 DeviceDescriptorBase::DeviceDescriptorBase(const AudioDeviceTypeAddr &deviceTypeAddr) :
39         AudioPort("", AUDIO_PORT_TYPE_DEVICE,
40                   audio_is_output_device(deviceTypeAddr.mType) ? AUDIO_PORT_ROLE_SINK :
41                                          AUDIO_PORT_ROLE_SOURCE),
42         mDeviceTypeAddr(deviceTypeAddr)
43 {
44     if (mDeviceTypeAddr.address().empty() && audio_is_remote_submix_device(mDeviceTypeAddr.mType)) {
45         mDeviceTypeAddr.setAddress("0");
46     }
47 }
48 
setAddress(const std::string & address)49 void DeviceDescriptorBase::setAddress(const std::string &address) {
50     mDeviceTypeAddr.setAddress(address);
51 }
52 
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const53 void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig,
54                                              const struct audio_port_config *srcConfig) const
55 {
56     dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN;
57     if (mSamplingRate != 0) {
58         dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
59     }
60     if (mChannelMask != AUDIO_CHANNEL_NONE) {
61         dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
62     }
63     if (mFormat != AUDIO_FORMAT_INVALID) {
64         dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
65     }
66 
67     if (srcConfig != NULL) {
68         dstConfig->config_mask |= srcConfig->config_mask;
69     }
70 
71     AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
72 
73     dstConfig->role = audio_is_output_device(mDeviceTypeAddr.mType) ?
74                         AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
75     dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
76     dstConfig->ext.device.type = mDeviceTypeAddr.mType;
77 
78     (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mDeviceTypeAddr.getAddress());
79 }
80 
toAudioPort(struct audio_port * port) const81 void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const
82 {
83     ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceTypeAddr.mType);
84     toAudioPortInternal(port);
85 }
86 
toAudioPort(struct audio_port_v7 * port) const87 void DeviceDescriptorBase::toAudioPort(struct audio_port_v7 *port) const {
88     ALOGV("DeviceDescriptorBase::toAudioPort() v7 handle %d type %08x", mId, mDeviceTypeAddr.mType);
89     toAudioPortInternal(port);
90 }
91 
setEncapsulationModes(uint32_t encapsulationModes)92 status_t DeviceDescriptorBase::setEncapsulationModes(uint32_t encapsulationModes) {
93     if ((encapsulationModes & ~AUDIO_ENCAPSULATION_MODE_ALL_POSITION_BITS) != 0) {
94         return BAD_VALUE;
95     }
96     mEncapsulationModes = encapsulationModes & ~(1 << AUDIO_ENCAPSULATION_MODE_NONE);
97     return NO_ERROR;
98 }
99 
setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes)100 status_t DeviceDescriptorBase::setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes) {
101     if ((encapsulationMetadataTypes & ~AUDIO_ENCAPSULATION_METADATA_TYPE_ALL_POSITION_BITS) != 0) {
102         return BAD_VALUE;
103     }
104     mEncapsulationMetadataTypes =
105             encapsulationMetadataTypes & ~(1 << AUDIO_ENCAPSULATION_METADATA_TYPE_NONE);
106     return NO_ERROR;
107 }
108 
dump(std::string * dst,int spaces,int index,const char * extraInfo,bool verbose) const109 void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index,
110                                 const char* extraInfo, bool verbose) const
111 {
112     dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1));
113     if (mId != 0) {
114         dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId));
115     }
116 
117     if (extraInfo != nullptr) {
118         dst->append(extraInfo);
119     }
120 
121     dst->append(base::StringPrintf("%*s- type: %-48s\n",
122             spaces, "", ::android::toString(mDeviceTypeAddr.mType).c_str()));
123 
124     dst->append(base::StringPrintf(
125             "%*s- supported encapsulation modes: %u\n", spaces, "", mEncapsulationModes));
126     dst->append(base::StringPrintf(
127             "%*s- supported encapsulation metadata types: %u\n",
128             spaces, "", mEncapsulationMetadataTypes));
129 
130     if (mDeviceTypeAddr.address().size() != 0) {
131         dst->append(base::StringPrintf(
132                 "%*s- address: %-32s\n", spaces, "", mDeviceTypeAddr.getAddress()));
133     }
134     AudioPort::dump(dst, spaces, verbose);
135 }
136 
toString(bool includeSensitiveInfo) const137 std::string DeviceDescriptorBase::toString(bool includeSensitiveInfo) const
138 {
139     return mDeviceTypeAddr.toString(includeSensitiveInfo);
140 }
141 
log() const142 void DeviceDescriptorBase::log() const
143 {
144     ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId,  mDeviceTypeAddr.mType,
145           ::android::toString(mDeviceTypeAddr.mType).c_str(),
146           mDeviceTypeAddr.getAddress());
147 
148     AudioPort::log("  ");
149 }
150 
equals(const sp<DeviceDescriptorBase> & other) const151 bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
152 {
153     return other != nullptr &&
154            static_cast<const AudioPort*>(this)->equals(other) &&
155            static_cast<const AudioPortConfig*>(this)->equals(other) &&
156            mDeviceTypeAddr.equals(other->mDeviceTypeAddr);
157 }
158 
159 
writeToParcel(Parcel * parcel) const160 status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const
161 {
162     media::AudioPort parcelable;
163     return writeToParcelable(&parcelable)
164         ?: parcelable.writeToParcel(parcel);
165 }
166 
writeToParcelable(media::AudioPort * parcelable) const167 status_t DeviceDescriptorBase::writeToParcelable(media::AudioPort* parcelable) const {
168     AudioPort::writeToParcelable(parcelable);
169     AudioPortConfig::writeToParcelable(&parcelable->activeConfig);
170     parcelable->id = VALUE_OR_RETURN_STATUS(legacy2aidl_audio_port_handle_t_int32_t(mId));
171 
172     media::AudioPortDeviceExt ext;
173     ext.device = VALUE_OR_RETURN_STATUS(legacy2aidl_AudioDeviceTypeAddress(mDeviceTypeAddr));
174     ext.encapsulationModes = VALUE_OR_RETURN_STATUS(
175             legacy2aidl_AudioEncapsulationMode_mask(mEncapsulationModes));
176     ext.encapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
177             legacy2aidl_AudioEncapsulationMetadataType_mask(mEncapsulationMetadataTypes));
178     UNION_SET(parcelable->ext, device, std::move(ext));
179     return OK;
180 }
181 
readFromParcel(const Parcel * parcel)182 status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel) {
183     media::AudioPort parcelable;
184     return parcelable.readFromParcel(parcel)
185         ?: readFromParcelable(parcelable);
186 }
187 
readFromParcelable(const media::AudioPort & parcelable)188 status_t DeviceDescriptorBase::readFromParcelable(const media::AudioPort& parcelable) {
189     if (parcelable.type != media::AudioPortType::DEVICE) {
190         return BAD_VALUE;
191     }
192     status_t status = AudioPort::readFromParcelable(parcelable)
193                       ?: AudioPortConfig::readFromParcelable(parcelable.activeConfig);
194     if (status != OK) {
195         return status;
196     }
197 
198     media::AudioPortDeviceExt ext = VALUE_OR_RETURN_STATUS(UNION_GET(parcelable.ext, device));
199     mDeviceTypeAddr = VALUE_OR_RETURN_STATUS(
200             aidl2legacy_AudioDeviceTypeAddress(ext.device));
201     mEncapsulationModes = VALUE_OR_RETURN_STATUS(
202             aidl2legacy_AudioEncapsulationMode_mask(ext.encapsulationModes));
203     mEncapsulationMetadataTypes = VALUE_OR_RETURN_STATUS(
204             aidl2legacy_AudioEncapsulationMetadataType_mask(ext.encapsulationMetadataTypes));
205     return OK;
206 }
207 
toString(const DeviceDescriptorBaseVector & devices)208 std::string toString(const DeviceDescriptorBaseVector& devices)
209 {
210     std::string ret;
211     for (const auto& device : devices) {
212         if (device != *devices.begin()) {
213             ret += ";";
214         }
215         ret += device->toString();
216     }
217     return ret;
218 }
219 
deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector & devices)220 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices)
221 {
222     AudioDeviceTypeAddrVector deviceTypeAddrs;
223     for (const auto& device : devices) {
224         deviceTypeAddrs.push_back(device->getDeviceTypeAddr());
225     }
226     return deviceTypeAddrs;
227 }
228 
229 ConversionResult<sp<DeviceDescriptorBase>>
aidl2legacy_DeviceDescriptorBase(const media::AudioPort & aidl)230 aidl2legacy_DeviceDescriptorBase(const media::AudioPort& aidl) {
231     sp<DeviceDescriptorBase> result = new DeviceDescriptorBase(AUDIO_DEVICE_NONE);
232     status_t status = result->readFromParcelable(aidl);
233     if (status != OK) {
234         return base::unexpected(status);
235     }
236     return result;
237 }
238 
239 ConversionResult<media::AudioPort>
legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase> & legacy)240 legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy) {
241     media::AudioPort aidl;
242     status_t status = legacy->writeToParcelable(&aidl);
243     if (status != OK) {
244         return base::unexpected(status);
245     }
246     return aidl;
247 }
248 
249 } // namespace android
250