1# Copyright 2014 The Chromium OS Authors. All rights reserved. 2# Use of this source code is governed by a BSD-style license that can be 3# found in the LICENSE file. 4"""Audio port ids shared in Chameleon audio test framework""" 5 6 7class ChameleonIds(object): 8 """Ids for audio ports on Chameleon 9 10 An Id is composed by host name 'Chameleon' and interface name e.g. 'HDMI'. 11 Note that the interface name must match what get_connector_type method 12 returns on a ChameleonPort so ChameleonPortFinder can find the port. 13 14 """ 15 HDMI = 'Chameleon HDMI' 16 LINEIN = 'Chameleon LineIn' 17 LINEOUT = 'Chameleon LineOut' 18 MIC = 'Chameleon Mic' 19 USBIN = 'Chameleon USBIn' 20 USBOUT = 'Chameleon USBOut' 21 22 SINK_PORTS = [HDMI, LINEIN, MIC, USBIN] 23 SOURCE_PORTS = [LINEOUT, USBOUT] 24 25 26class CrosIds(object): 27 """Ids for audio ports on Cros device. 28 29 Note that an bidirectional interface like 3.5mm jack is separated to 30 two interfaces, that is, 'Headphone' and 'External Mic'. 31 32 """ 33 HDMI = 'Cros HDMI' 34 HEADPHONE = 'Cros Headphone' 35 EXTERNAL_MIC = 'Cros External Mic' 36 SPEAKER = 'Cros Speaker' 37 INTERNAL_MIC = 'Cros Internal Mic' 38 BLUETOOTH_HEADPHONE = 'Cros Bluetooth Headphone' 39 BLUETOOTH_MIC = 'Cros Bluetooth Mic' 40 USBIN = 'Cros USBIn' 41 USBOUT = 'Cros USBOut' 42 HOTWORDING = 'Cros Hotwording' 43 44 SINK_PORTS = [EXTERNAL_MIC, INTERNAL_MIC, BLUETOOTH_MIC, USBIN, HOTWORDING] 45 SOURCE_PORTS = [HDMI, HEADPHONE, SPEAKER, BLUETOOTH_HEADPHONE, USBOUT] 46 47 48class PeripheralIds(object): 49 """Ids for peripherals. 50 51 These peripherals will be accessible by Cros device/Chameleon through 52 audio board. 53 54 """ 55 SPEAKER = 'Peripheral Speaker' 56 MIC = 'Peripheral Mic' 57 58 # Peripheral devices should have two roles but we only care one. 59 # For example, to test internal microphone on Cros device: 60 # 61 # (air) 62 # Peripheral Speaker -------> Internal Microphone 63 # ------ ------ 64 # Chameleon LineOut ----> | | | | 65 # ------ ------ 66 # Audio board Cros device 67 # 68 # In the above example, peripheral speaker is a sink as it takes signal 69 # from audio board. It should be a source as peripheral speaker transfer 70 # signal to internal microphone of Cros device, 71 # However, we do not abstract air as a link because it does not contain 72 # properties like level, channel_map, occupied to manipulate. 73 # So, we set peripheral speaker to be a sink to reflect the part related 74 # to audio bus. 75 # 76 # For example, to test internal speaker on Cros device: 77 # 78 # (air) 79 # Peripheral Micropone <----- Internal Speaker 80 # ------ ------ 81 # Chameleon LineIn <---- | | | | 82 # ------ ------ 83 # Audio board Cros device 84 # 85 # In the above example, peripheral microphone is a source as it feeds signal 86 # to audio board. It should be a sink as peripheral microphone receives 87 # signal from internal speaker of Cros device. 88 # However, we do not abstract air as a link because it does not contain 89 # properties like level, channel_map, occupied to manipulate. 90 # So, we set peripheral microphone to be a source to reflect the part related 91 # to audio bus. 92 93 BLUETOOTH_DATA_RX = ('Peripheral Bluetooth Signal Output and Data Receiver') 94 BLUETOOTH_DATA_TX = ('Peripheral Bluetooth Signal Input and Data ' 95 'Transmitter') 96 97 # Bluetooth module has both source and sink roles. 98 # When Cros device plays audio to bluetooth module data receiver through 99 # bluetooth connection, bluetooth module is a sink of audio signal. 100 # When we route audio signal from bluetooth signal output to Chameleon 101 # Line-In, bluetooth module is a signal source in terms of audio bus. 102 # 103 # Bluetooth module 104 # 105 # signal data (bluetooth) 106 # output receiver <------------ Bluetooth Headphone 107 # ------ ------ 108 # Chameleon LineIn <---- | | | | 109 # ------ ------ 110 # Audio board Cros device 111 # 112 # 113 # When Cros device records audio from bluetooth module data transmitter 114 # through bluetooth connection, bluetooth module is a source of audio 115 # signal. When we route audio signal from Chameleon Line-Out to bluetooth 116 # signal input, bluetooth module is a signal sink in terms of audio bus. 117 # 118 # Bluetooth module 119 # 120 # signal data (bluetooth) 121 # input transmitter -----------> Bluetooth Microphone 122 # ------ ------ 123 # Chameleon LineOut ----> | | | | 124 # ------ ------ 125 # Audio board Cros device 126 # 127 # From above diagram it is clear that "signal output" is connected to 128 # "data receiver", while "signal input" is connected to "data transmitter". 129 # To simplify the number of nodes, we group "signal output" and 130 # "data receiver" into one Id, and group "signal input" and 131 # "data transmitter" into one Id. Also, we let these two Ids be both source 132 # and sink. 133 SOURCE_PORTS = [MIC, BLUETOOTH_DATA_RX, BLUETOOTH_DATA_TX] 134 SINK_PORTS = [SPEAKER, BLUETOOTH_DATA_RX, BLUETOOTH_DATA_TX] 135 136 137SINK_PORTS = [] 138for cls in [ChameleonIds, CrosIds, PeripheralIds]: 139 SINK_PORTS += cls.SINK_PORTS 140 141SOURCE_PORTS = [] 142for cls in [ChameleonIds, CrosIds, PeripheralIds]: 143 SOURCE_PORTS += cls.SOURCE_PORTS 144 145 146def get_host(port_id): 147 """Parses given port_id to get host name. 148 149 @param port_id: A string, that is, id in ChameleonIds, CrosIds, or 150 PeripheralIds. 151 152 @returns: Host name. A string in ['Chameleon', 'Cros', 'Peripheral']. 153 154 @raises: ValueError if port_id is invalid. 155 156 """ 157 host = port_id.split()[0] 158 if host not in ['Chameleon', 'Cros', 'Peripheral']: 159 raise ValueError('Not a valid port id: %r' % port_id) 160 return host 161 162 163def get_interface(port_id): 164 """Parses given port_id to get interface name. 165 166 @param port_id: A string, that is, id in ChameleonIds, CrosIds, or 167 PeripheralIds. 168 169 @returns: Interface name. A string, e.g. 'HDMI', 'LineIn'. 170 171 @raises: ValueError if port_id is invalid. 172 173 """ 174 try: 175 return port_id.split(' ', 1)[1] 176 except IndexError: 177 raise ValueError('Not a valid port id: %r' % port_id) 178 179 180def get_role(port_id): 181 """Gets the role of given port_id. 182 183 @param port_id: A string, that is, id in ChameleonIds, CrosIds, or 184 PeripheralIds. 185 186 @returns: 'source' or 'sink'. 187 188 @raises: ValueError if port_id is invalid. 189 190 """ 191 if port_id in SOURCE_PORTS: 192 return 'source' 193 if port_id in SINK_PORTS: 194 return 'sink' 195 if port_id in SOURCE_PORTS and port_id in SINK_PORTS: 196 return 'sink | source' 197 raise ValueError('Not a valid port id: %r' % port_id) 198