• 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 #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             const FormatVector &encodedFormats = FormatVector{});
41     DeviceDescriptorBase(const AudioDeviceTypeAddr& deviceTypeAddr,
42             const FormatVector &encodedFormats = FormatVector{});
43 
44     virtual ~DeviceDescriptorBase() = default;
45 
type()46     audio_devices_t type() const { return mDeviceTypeAddr.mType; }
address()47     const std::string& address() const { return mDeviceTypeAddr.address(); }
48     void setAddress(const std::string &address);
getDeviceTypeAddr()49     const AudioDeviceTypeAddr& getDeviceTypeAddr() const { return mDeviceTypeAddr; }
50 
encodedFormats()51     const FormatVector& encodedFormats() const { return mEncodedFormats; }
52     bool supportsFormat(audio_format_t format);
53 
54     // AudioPortConfig
getAudioPort()55     virtual sp<AudioPort> getAudioPort() const {
56         return static_cast<AudioPort*>(const_cast<DeviceDescriptorBase*>(this));
57     }
58     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
59             const struct audio_port_config *srcConfig = NULL) const;
60 
61     // AudioPort
62     virtual void toAudioPort(struct audio_port *port) const;
63     virtual void toAudioPort(struct audio_port_v7 *port) const;
64 
65     status_t setEncapsulationModes(uint32_t encapsulationModes);
66     status_t setEncapsulationMetadataTypes(uint32_t encapsulationMetadataTypes);
67 
68     void dump(std::string *dst, int spaces,
69               const char* extraInfo = nullptr, bool verbose = true) const;
70     void log() const;
71 
72     /**
73      * Return a string to describe the DeviceDescriptor.
74      *
75      * @param includeSensitiveInfo sensitive information will be added when it is true.
76      * @return a string that can be used to describe the DeviceDescriptor.
77      */
78     std::string toString(bool includeSensitiveInfo = false) const;
79 
80     bool equals(const sp<DeviceDescriptorBase>& other) const;
81 
82     status_t writeToParcelable(media::AudioPort* parcelable) const;
83     status_t readFromParcelable(const media::AudioPort& parcelable);
84 
85 protected:
86     AudioDeviceTypeAddr mDeviceTypeAddr;
87     FormatVector        mEncodedFormats;
88     uint32_t mEncapsulationModes = 0;
89     uint32_t mEncapsulationMetadataTypes = 0;
90 private:
91     template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
92                                         || std::is_same<T, struct audio_port_v7>::value, int> = 0>
toAudioPortInternal(T * port)93     void toAudioPortInternal(T* port) const {
94         AudioPort::toAudioPort(port);
95         toAudioPortConfig(&port->active_config);
96         port->id = mId;
97         port->ext.device.type = mDeviceTypeAddr.mType;
98         port->ext.device.encapsulation_modes = mEncapsulationModes;
99         port->ext.device.encapsulation_metadata_types = mEncapsulationMetadataTypes;
100         (void)audio_utils_strlcpy_zerofill(port->ext.device.address, mDeviceTypeAddr.getAddress());
101     }
102 };
103 
104 using DeviceDescriptorBaseVector = std::vector<sp<DeviceDescriptorBase>>;
105 
106 /**
107  * Return human readable string for collection of DeviceDescriptorBase.
108  * For a DeviceDescriptorBase, it contains port id, audio device type and address.
109  */
110 std::string toString(const DeviceDescriptorBaseVector& devices);
111 
112 /**
113  * Return a set of device types and addresses from collection of DeviceDescriptorBase.
114  */
115 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices);
116 
117 // Conversion routines, according to AidlConversion.h conventions.
118 ConversionResult<sp<DeviceDescriptorBase>>
119 aidl2legacy_DeviceDescriptorBase(const media::AudioPort& aidl);
120 ConversionResult<media::AudioPort>
121 legacy2aidl_DeviceDescriptorBase(const sp<DeviceDescriptorBase>& legacy);
122 
123 } // namespace android
124