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 <binder/Parcel.h>
22 #include <binder/Parcelable.h>
23 #include <media/AudioContainers.h>
24 #include <media/AudioPort.h>
25 #include <media/AudioDeviceTypeAddr.h>
26 #include <utils/Errors.h>
27 #include <cutils/config_utils.h>
28 #include <system/audio.h>
29 #include <system/audio_policy.h>
30 
31 namespace android {
32 
33 class DeviceDescriptorBase : public AudioPort, public AudioPortConfig
34 {
35 public:
36      // Note that empty name refers by convention to a generic device.
37     explicit DeviceDescriptorBase(audio_devices_t type);
38     DeviceDescriptorBase(audio_devices_t type, const std::string& address);
39     explicit DeviceDescriptorBase(const AudioDeviceTypeAddr& deviceTypeAddr);
40 
~DeviceDescriptorBase()41     virtual ~DeviceDescriptorBase() {}
42 
type()43     audio_devices_t type() const { return mDeviceTypeAddr.mType; }
address()44     std::string address() const { return mDeviceTypeAddr.mAddress; }
setAddress(const std::string & address)45     void setAddress(const std::string &address) { mDeviceTypeAddr.mAddress = address; }
getDeviceTypeAddr()46     const AudioDeviceTypeAddr& getDeviceTypeAddr() const { return mDeviceTypeAddr; }
47 
48     // AudioPortConfig
getAudioPort()49     virtual sp<AudioPort> getAudioPort() const {
50         return static_cast<AudioPort*>(const_cast<DeviceDescriptorBase*>(this));
51     }
52     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
53             const struct audio_port_config *srcConfig = NULL) const;
54 
55     // AudioPort
56     virtual void toAudioPort(struct audio_port *port) const;
57 
58     void dump(std::string *dst, int spaces, int index,
59               const char* extraInfo = nullptr, bool verbose = true) const;
60     void log() const;
61     std::string toString() const;
62 
63     bool equals(const sp<DeviceDescriptorBase>& other) const;
64 
65     status_t writeToParcel(Parcel* parcel) const override;
66     status_t readFromParcel(const Parcel* parcel) override;
67 
68 protected:
69     AudioDeviceTypeAddr mDeviceTypeAddr;
70 };
71 
72 using DeviceDescriptorBaseVector = std::vector<sp<DeviceDescriptorBase>>;
73 
74 /**
75  * Return human readable string for collection of DeviceDescriptorBase.
76  * For a DeviceDescriptorBase, it contains port id, audio device type and address.
77  */
78 std::string toString(const DeviceDescriptorBaseVector& devices);
79 
80 /**
81  * Return a set of device types and addresses from collection of DeviceDescriptorBase.
82  */
83 AudioDeviceTypeAddrVector deviceTypeAddrsFromDescriptors(const DeviceDescriptorBaseVector& devices);
84 
85 } // namespace android
86