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 #pragma once 18 19 #include <vector> 20 21 #include <android/media/AudioPort.h> 22 #include <binder/Parcel.h> 23 #include <binder/Parcelable.h> 24 #include <media/AudioContainers.h> 25 #include <media/AudioPort.h> 26 #include <media/AudioDeviceTypeAddr.h> 27 #include <utils/Errors.h> 28 #include <cutils/config_utils.h> 29 #include <system/audio.h> 30 #include <system/audio_policy.h> 31 32 namespace android { 33 34 class DeviceDescriptorBase : public AudioPort, public AudioPortConfig 35 { 36 public: 37 // Note that empty name refers by convention to a generic device. 38 explicit DeviceDescriptorBase(audio_devices_t type); 39 DeviceDescriptorBase(audio_devices_t type, const std::string& address); 40 explicit DeviceDescriptorBase(const AudioDeviceTypeAddr& deviceTypeAddr); 41 ~DeviceDescriptorBase()42 virtual ~DeviceDescriptorBase() {} 43 type()44 audio_devices_t type() const { return mDeviceTypeAddr.mType; } address()45 const std::string& address() const { return mDeviceTypeAddr.address(); } 46 void setAddress(const std::string &address); getDeviceTypeAddr()47 const AudioDeviceTypeAddr& getDeviceTypeAddr() const { return mDeviceTypeAddr; } 48 49 // AudioPortConfig getAudioPort()50 virtual sp<AudioPort> getAudioPort() const { 51 return static_cast<AudioPort*>(const_cast<DeviceDescriptorBase*>(this)); 52 } 53 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 54 const struct audio_port_config *srcConfig = NULL) const; 55 56 // AudioPort 57 virtual void toAudioPort(struct audio_port *port) const; 58 virtual void toAudioPort(struct audio_port_v7 *port) const; 59 60 status_t setEncapsulationModes(uint32_t encapsulationModes); 61 status_t setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes); 62 63 void dump(std::string *dst, int spaces, int index, 64 const char* extraInfo = nullptr, bool verbose = true) const; 65 void log() const; 66 67 /** 68 * Return a string to describe the DeviceDescriptor. 69 * 70 * @param includeSensitiveInfo sensitive information will be added when it is true. 71 * @return a string that can be used to describe the DeviceDescriptor. 72 */ 73 std::string toString(bool includeSensitiveInfo = false) const; 74 75 bool equals(const sp<DeviceDescriptorBase>& other) const; 76 77 status_t writeToParcel(Parcel* parcel) const override; 78 status_t readFromParcel(const Parcel* parcel) override; 79 80 status_t writeToParcelable(media::AudioPort* parcelable) const; 81 status_t readFromParcelable(const media::AudioPort& parcelable); 82 83 protected: 84 AudioDeviceTypeAddr mDeviceTypeAddr; 85 uint32_t mEncapsulationModes = 0; 86 uint32_t mEncapsulationMetadataTypes = 0; 87 private: 88 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value 89 || std::is_same<T, struct audio_port_v7>::value, int> = 0> toAudioPortInternal(T * port)90 void toAudioPortInternal(T* port) const { 91 AudioPort::toAudioPort(port); 92 toAudioPortConfig(&port->active_config); 93 port->id = mId; 94 port->ext.device.type = mDeviceTypeAddr.mType; 95 port->ext.device.encapsulation_modes = mEncapsulationModes; 96 port->ext.device.encapsulation_metadata_types = mEncapsulationMetadataTypes; 97 (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mDeviceTypeAddr.getAddress()); 98 } 99 }; 100 101 using DeviceDescriptorBaseVector = std::vector<sp<DeviceDescriptorBase>>; 102 103 /** 104 * Return human readable string for collection of DeviceDescriptorBase. 105 * For a DeviceDescriptorBase, it contains port id, audio device type and address. 106 */ 107 std::string toString(const DeviceDescriptorBaseVector& devices); 108 109 /** 110 * Return a set of device types and addresses from collection of DeviceDescriptorBase. 111 */ 112 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices); 113 114 // Conversion routines, according to AidlConversion.h conventions. 115 ConversionResult<sp<DeviceDescriptorBase>> 116 aidl2legacy_DeviceDescriptorBase(const media::AudioPort& aidl); 117 ConversionResult<media::AudioPort> 118 legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy); 119 120 } // namespace android 121