blob: 18fd184c46465aec2c657d72bc53ea1721f18b71 [file] [log] [blame]
jiabin6ae65d82019-09-11 10:16:33 -07001/*
jiabince9f20e2019-09-12 16:29:15 -07002 * Copyright (C) 2019 The Android Open Source Project
jiabin6ae65d82019-09-11 10:16:33 -07003 *
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
jiabince9f20e2019-09-12 16:29:15 -070017#define LOG_TAG "DeviceDescriptorBase"
jiabin6ae65d82019-09-11 10:16:33 -070018//#define LOG_NDEBUG 0
19
jiabince9f20e2019-09-12 16:29:15 -070020#include <android-base/stringprintf.h>
jiabin6ae65d82019-09-11 10:16:33 -070021#include <audio_utils/string.h>
jiabince9f20e2019-09-12 16:29:15 -070022#include <media/DeviceDescriptorBase.h>
jiabin6ae65d82019-09-11 10:16:33 -070023#include <media/TypeConverter.h>
jiabin6ae65d82019-09-11 10:16:33 -070024
25namespace android {
26
jiabince9f20e2019-09-12 16:29:15 -070027DeviceDescriptorBase::DeviceDescriptorBase(audio_devices_t type) :
jiabin6ae65d82019-09-11 10:16:33 -070028 AudioPort("", AUDIO_PORT_TYPE_DEVICE,
29 audio_is_output_device(type) ? AUDIO_PORT_ROLE_SINK :
30 AUDIO_PORT_ROLE_SOURCE),
jiabince9f20e2019-09-12 16:29:15 -070031 mDeviceType(type)
jiabin6ae65d82019-09-11 10:16:33 -070032{
jiabin6ae65d82019-09-11 10:16:33 -070033 if (audio_is_remote_submix_device(type)) {
jiabince9f20e2019-09-12 16:29:15 -070034 mAddress = "0";
jiabin6ae65d82019-09-11 10:16:33 -070035 }
36}
37
jiabince9f20e2019-09-12 16:29:15 -070038void DeviceDescriptorBase::toAudioPortConfig(struct audio_port_config *dstConfig,
39 const struct audio_port_config *srcConfig) const
jiabin6ae65d82019-09-11 10:16:33 -070040{
41 dstConfig->config_mask = AUDIO_PORT_CONFIG_GAIN;
42 if (mSamplingRate != 0) {
43 dstConfig->config_mask |= AUDIO_PORT_CONFIG_SAMPLE_RATE;
44 }
45 if (mChannelMask != AUDIO_CHANNEL_NONE) {
46 dstConfig->config_mask |= AUDIO_PORT_CONFIG_CHANNEL_MASK;
47 }
48 if (mFormat != AUDIO_FORMAT_INVALID) {
49 dstConfig->config_mask |= AUDIO_PORT_CONFIG_FORMAT;
50 }
51
52 if (srcConfig != NULL) {
53 dstConfig->config_mask |= srcConfig->config_mask;
54 }
55
56 AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
jiabin6ae65d82019-09-11 10:16:33 -070057
58 dstConfig->role = audio_is_output_device(mDeviceType) ?
59 AUDIO_PORT_ROLE_SINK : AUDIO_PORT_ROLE_SOURCE;
60 dstConfig->type = AUDIO_PORT_TYPE_DEVICE;
61 dstConfig->ext.device.type = mDeviceType;
62
jiabince9f20e2019-09-12 16:29:15 -070063 (void)audio_utils_strlcpy_zerofill(dstConfig->ext.device.address, mAddress.c_str());
jiabin6ae65d82019-09-11 10:16:33 -070064}
65
jiabince9f20e2019-09-12 16:29:15 -070066void DeviceDescriptorBase::toAudioPort(struct audio_port *port) const
jiabin6ae65d82019-09-11 10:16:33 -070067{
jiabince9f20e2019-09-12 16:29:15 -070068 ALOGV("DeviceDescriptorBase::toAudioPort() handle %d type %08x", mId, mDeviceType);
jiabin6ae65d82019-09-11 10:16:33 -070069 AudioPort::toAudioPort(port);
jiabin6ae65d82019-09-11 10:16:33 -070070 toAudioPortConfig(&port->active_config);
jiabince9f20e2019-09-12 16:29:15 -070071 port->id = mId;
jiabin6ae65d82019-09-11 10:16:33 -070072 port->ext.device.type = mDeviceType;
jiabince9f20e2019-09-12 16:29:15 -070073 (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mAddress.c_str());
jiabin6ae65d82019-09-11 10:16:33 -070074}
75
jiabince9f20e2019-09-12 16:29:15 -070076void DeviceDescriptorBase::dump(std::string *dst, int spaces, int index,
77 const char* extraInfo, bool verbose) const
jiabin6ae65d82019-09-11 10:16:33 -070078{
jiabince9f20e2019-09-12 16:29:15 -070079 dst->append(base::StringPrintf("%*sDevice %d:\n", spaces, "", index + 1));
jiabin6ae65d82019-09-11 10:16:33 -070080 if (mId != 0) {
jiabince9f20e2019-09-12 16:29:15 -070081 dst->append(base::StringPrintf("%*s- id: %2d\n", spaces, "", mId));
jiabin6ae65d82019-09-11 10:16:33 -070082 }
83
jiabince9f20e2019-09-12 16:29:15 -070084 if (extraInfo != nullptr) {
85 dst->append(extraInfo);
86 }
87
88 dst->append(base::StringPrintf("%*s- type: %-48s\n",
89 spaces, "", ::android::toString(mDeviceType).c_str()));
jiabin6ae65d82019-09-11 10:16:33 -070090
91 if (mAddress.size() != 0) {
jiabince9f20e2019-09-12 16:29:15 -070092 dst->append(base::StringPrintf("%*s- address: %-32s\n", spaces, "", mAddress.c_str()));
jiabin6ae65d82019-09-11 10:16:33 -070093 }
jiabince9f20e2019-09-12 16:29:15 -070094 AudioPort::dump(dst, spaces, verbose);
jiabin6ae65d82019-09-11 10:16:33 -070095}
96
jiabince9f20e2019-09-12 16:29:15 -070097std::string DeviceDescriptorBase::toString() const
jiabin6ae65d82019-09-11 10:16:33 -070098{
99 std::stringstream sstream;
100 sstream << "type:0x" << std::hex << type() << ",@:" << mAddress;
101 return sstream.str();
102}
103
jiabince9f20e2019-09-12 16:29:15 -0700104void DeviceDescriptorBase::log() const
jiabin6ae65d82019-09-11 10:16:33 -0700105{
106 ALOGI("Device id:%d type:0x%08X:%s, addr:%s", mId, mDeviceType,
107 ::android::toString(mDeviceType).c_str(),
jiabince9f20e2019-09-12 16:29:15 -0700108 mAddress.c_str());
jiabin6ae65d82019-09-11 10:16:33 -0700109
110 AudioPort::log(" ");
111}
112
jiabin49e69a12019-10-15 16:04:13 -0700113bool DeviceDescriptorBase::equals(const sp<DeviceDescriptorBase> &other) const
114{
115 return other != nullptr &&
116 static_cast<const AudioPort*>(this)->equals(other) &&
117 static_cast<const AudioPortConfig*>(this)->equals(other) &&
118 mAddress.compare(other->address()) == 0 &&
119 mDeviceType == other->type();
120}
121
jiabin17058fa2019-10-08 17:33:38 -0700122status_t DeviceDescriptorBase::writeToParcel(Parcel *parcel) const
123{
124 status_t status = NO_ERROR;
125 if ((status = AudioPort::writeToParcel(parcel)) != NO_ERROR) return status;
126 if ((status = AudioPortConfig::writeToParcel(parcel)) != NO_ERROR) return status;
127 if ((status = parcel->writeUtf8AsUtf16(mAddress)) != NO_ERROR) return status;
128 if ((status = parcel->writeUint32(mDeviceType)) != NO_ERROR) return status;
129 return status;
130}
131
132status_t DeviceDescriptorBase::readFromParcel(const Parcel *parcel)
133{
134 status_t status = NO_ERROR;
135 if ((status = AudioPort::readFromParcel(parcel)) != NO_ERROR) return status;
136 if ((status = AudioPortConfig::readFromParcel(parcel)) != NO_ERROR) return status;
137 if ((status = parcel->readUtf8FromUtf16(&mAddress)) != NO_ERROR) return status;
138 if ((status = parcel->readUint32(&mDeviceType)) != NO_ERROR) return status;
139 return status;
140}
141
jiabin6ae65d82019-09-11 10:16:33 -0700142} // namespace android