Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | package com.android.server.hdmi; |
| 17 | |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 18 | import static com.android.server.hdmi.Constants.ALWAYS_SYSTEM_AUDIO_CONTROL_ON_POWER_ON; |
| 19 | import static com.android.server.hdmi.Constants.PROPERTY_SYSTEM_AUDIO_CONTROL_ON_POWER_ON; |
| 20 | import static com.android.server.hdmi.Constants.USE_LAST_STATE_SYSTEM_AUDIO_CONTROL_ON_POWER_ON; |
| 21 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 22 | import android.annotation.Nullable; |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 23 | import android.content.Intent; |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 24 | import android.hardware.hdmi.HdmiControlManager; |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 25 | import android.hardware.hdmi.HdmiDeviceInfo; |
Amy | 59176da | 2018-10-12 16:30:54 -0700 | [diff] [blame] | 26 | import android.hardware.hdmi.HdmiPortInfo; |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 27 | import android.hardware.hdmi.IHdmiControlCallback; |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 28 | import android.media.AudioDeviceInfo; |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 29 | import android.media.AudioFormat; |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 30 | import android.media.AudioManager; |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 31 | import android.media.AudioSystem; |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 32 | import android.media.tv.TvContract; |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 33 | import android.os.SystemProperties; |
Amy | 0c2e29f | 2018-10-23 12:17:52 -0700 | [diff] [blame] | 34 | import android.provider.Settings.Global; |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 35 | import android.util.Slog; |
| 36 | import android.util.SparseArray; |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 37 | |
Amy | 87eda82 | 2018-06-06 17:56:39 -0700 | [diff] [blame] | 38 | import com.android.internal.annotations.GuardedBy; |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
Nick Chalko | b9e48e2 | 2018-10-23 06:59:39 -0700 | [diff] [blame] | 40 | import com.android.internal.util.IndentingPrintWriter; |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 41 | import com.android.server.hdmi.Constants.AudioCodec; |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 42 | import com.android.server.hdmi.DeviceDiscoveryAction.DeviceDiscoveryCallback; |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 43 | import com.android.server.hdmi.HdmiAnnotations.ServiceThreadOnly; |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 44 | import com.android.server.hdmi.HdmiUtils.CodecSad; |
| 45 | import com.android.server.hdmi.HdmiUtils.DeviceConfig; |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 46 | |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 47 | import org.xmlpull.v1.XmlPullParserException; |
| 48 | |
| 49 | import java.io.File; |
| 50 | import java.io.FileInputStream; |
| 51 | import java.io.IOException; |
| 52 | import java.io.InputStream; |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 53 | import java.io.UnsupportedEncodingException; |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 54 | import java.util.ArrayList; |
| 55 | import java.util.Arrays; |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 56 | import java.util.Collections; |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 57 | import java.util.HashMap; |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 58 | import java.util.List; |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 59 | import java.util.stream.Collectors; |
| 60 | |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 61 | |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 62 | /** |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 63 | * Represent a logical device of type {@link HdmiDeviceInfo#DEVICE_AUDIO_SYSTEM} residing in Android |
| 64 | * system. |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 65 | */ |
Amy | 848a9f2 | 2018-08-27 17:21:26 -0700 | [diff] [blame] | 66 | public class HdmiCecLocalDeviceAudioSystem extends HdmiCecLocalDeviceSource { |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 67 | |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 68 | private static final String TAG = "HdmiCecLocalDeviceAudioSystem"; |
| 69 | |
Amy | 87eda82 | 2018-06-06 17:56:39 -0700 | [diff] [blame] | 70 | // Whether System audio mode is activated or not. |
| 71 | // This becomes true only when all system audio sequences are finished. |
| 72 | @GuardedBy("mLock") |
| 73 | private boolean mSystemAudioActivated; |
| 74 | |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 75 | // Whether the System Audio Control feature is enabled or not. True by default. |
| 76 | @GuardedBy("mLock") |
| 77 | private boolean mSystemAudioControlFeatureEnabled; |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 78 | |
Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 79 | private boolean mTvSystemAudioModeSupport; |
| 80 | |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 81 | // Whether ARC is available or not. "true" means that ARC is established between TV and |
| 82 | // AVR as audio receiver. |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 83 | @ServiceThreadOnly private boolean mArcEstablished = false; |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 84 | |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 85 | // If the current device uses TvInput for ARC. We assume all other inputs also use TvInput |
| 86 | // when ARC is using TvInput. |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 87 | private boolean mArcIntentUsed = SystemProperties |
| 88 | .get(Constants.PROPERTY_SYSTEM_AUDIO_DEVICE_ARC_PORT, "0").contains("tvinput"); |
Amy | 6a6d618 | 2018-08-20 17:38:47 -0700 | [diff] [blame] | 89 | |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 90 | // Keeps the mapping (HDMI port ID to TV input URI) to keep track of the TV inputs ready to |
| 91 | // accept input switching request from HDMI devices. Requests for which the corresponding |
| 92 | // input ID is not yet registered by TV input framework need to be buffered for delayed |
| 93 | // processing. |
| 94 | private final HashMap<Integer, String> mTvInputs = new HashMap<>(); |
| 95 | |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 96 | // Copy of mDeviceInfos to guarantee thread-safety. |
| 97 | @GuardedBy("mLock") |
| 98 | private List<HdmiDeviceInfo> mSafeAllDeviceInfos = Collections.emptyList(); |
| 99 | |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 100 | // Map-like container of all cec devices. |
| 101 | // device id is used as key of container. |
| 102 | private final SparseArray<HdmiDeviceInfo> mDeviceInfos = new SparseArray<>(); |
| 103 | |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 104 | protected HdmiCecLocalDeviceAudioSystem(HdmiControlService service) { |
| 105 | super(service, HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM); |
Amy | 0c2e29f | 2018-10-23 12:17:52 -0700 | [diff] [blame] | 106 | mRoutingControlFeatureEnabled = |
Amy | 77e672c | 2018-10-31 15:55:40 -0700 | [diff] [blame] | 107 | mService.readBooleanSetting(Global.HDMI_CEC_SWITCH_ENABLED, false); |
Amy | 79db52f | 2018-10-23 12:45:17 -0700 | [diff] [blame] | 108 | mSystemAudioControlFeatureEnabled = |
| 109 | mService.readBooleanSetting(Global.HDMI_SYSTEM_AUDIO_CONTROL_ENABLED, true); |
Amy | 0063811 | 2018-10-31 17:47:17 -0700 | [diff] [blame] | 110 | // TODO(amyjojo): Maintain a portId to TvinputId map. |
| 111 | mTvInputs.put(2, "com.droidlogic.tvinput/.services.Hdmi1InputService/HW5"); |
| 112 | mTvInputs.put(4, "com.droidlogic.tvinput/.services.Hdmi2InputService/HW6"); |
| 113 | mTvInputs.put(1, "com.droidlogic.tvinput/.services.Hdmi3InputService/HW7"); |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 116 | private static final String SHORT_AUDIO_DESCRIPTOR_CONFIG_PATH = "/vendor/etc/sadConfig.xml"; |
| 117 | |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 118 | /** |
| 119 | * Called when a device is newly added or a new device is detected or |
Amy | 1e4a8cc | 2018-10-15 10:18:14 -0700 | [diff] [blame] | 120 | * an existing device is updated. |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 121 | * |
| 122 | * @param info device info of a new device. |
| 123 | */ |
| 124 | @ServiceThreadOnly |
| 125 | final void addCecDevice(HdmiDeviceInfo info) { |
| 126 | assertRunOnServiceThread(); |
| 127 | HdmiDeviceInfo old = addDeviceInfo(info); |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 128 | if (info.getPhysicalAddress() == mService.getPhysicalAddress()) { |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 129 | // The addition of the device itself should not be notified. |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 130 | // Note that different logical address could still be the same local device. |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 131 | return; |
| 132 | } |
| 133 | if (old == null) { |
| 134 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE); |
| 135 | } else if (!old.equals(info)) { |
| 136 | invokeDeviceEventListener(old, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE); |
| 137 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Called when a device is removed or removal of device is detected. |
| 143 | * |
| 144 | * @param address a logical address of a device to be removed |
| 145 | */ |
| 146 | @ServiceThreadOnly |
| 147 | final void removeCecDevice(int address) { |
| 148 | assertRunOnServiceThread(); |
| 149 | HdmiDeviceInfo info = removeDeviceInfo(HdmiDeviceInfo.idForCecDevice(address)); |
| 150 | |
| 151 | mCecMessageCache.flushMessagesFrom(address); |
| 152 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE); |
| 153 | } |
| 154 | |
| 155 | /** |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 156 | * Called when a device is updated. |
| 157 | * |
| 158 | * @param info device info of the updating device. |
| 159 | */ |
| 160 | @ServiceThreadOnly |
| 161 | final void updateCecDevice(HdmiDeviceInfo info) { |
| 162 | assertRunOnServiceThread(); |
| 163 | HdmiDeviceInfo old = addDeviceInfo(info); |
| 164 | |
| 165 | if (old == null) { |
| 166 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_ADD_DEVICE); |
| 167 | } else if (!old.equals(info)) { |
| 168 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Add a new {@link HdmiDeviceInfo}. It returns old device info which has the same |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 174 | * logical address as new device info's. |
| 175 | * |
| 176 | * @param deviceInfo a new {@link HdmiDeviceInfo} to be added. |
| 177 | * @return {@code null} if it is new device. Otherwise, returns old {@HdmiDeviceInfo} |
| 178 | * that has the same logical address as new one has. |
| 179 | */ |
| 180 | @ServiceThreadOnly |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 181 | @VisibleForTesting |
| 182 | protected HdmiDeviceInfo addDeviceInfo(HdmiDeviceInfo deviceInfo) { |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 183 | assertRunOnServiceThread(); |
| 184 | HdmiDeviceInfo oldDeviceInfo = getCecDeviceInfo(deviceInfo.getLogicalAddress()); |
| 185 | if (oldDeviceInfo != null) { |
| 186 | removeDeviceInfo(deviceInfo.getId()); |
| 187 | } |
| 188 | mDeviceInfos.append(deviceInfo.getId(), deviceInfo); |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 189 | updateSafeDeviceInfoList(); |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 190 | return oldDeviceInfo; |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Remove a device info corresponding to the given {@code logicalAddress}. |
| 195 | * It returns removed {@link HdmiDeviceInfo} if exists. |
| 196 | * |
| 197 | * @param id id of device to be removed |
| 198 | * @return removed {@link HdmiDeviceInfo} it exists. Otherwise, returns {@code null} |
| 199 | */ |
| 200 | @ServiceThreadOnly |
| 201 | private HdmiDeviceInfo removeDeviceInfo(int id) { |
| 202 | assertRunOnServiceThread(); |
| 203 | HdmiDeviceInfo deviceInfo = mDeviceInfos.get(id); |
| 204 | if (deviceInfo != null) { |
| 205 | mDeviceInfos.remove(id); |
| 206 | } |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 207 | updateSafeDeviceInfoList(); |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 208 | return deviceInfo; |
| 209 | } |
| 210 | |
| 211 | /** |
| 212 | * Return a {@link HdmiDeviceInfo} corresponding to the given {@code logicalAddress}. |
| 213 | * |
| 214 | * @param logicalAddress logical address of the device to be retrieved |
| 215 | * @return {@link HdmiDeviceInfo} matched with the given {@code logicalAddress}. |
| 216 | * Returns null if no logical address matched |
| 217 | */ |
| 218 | @ServiceThreadOnly |
| 219 | HdmiDeviceInfo getCecDeviceInfo(int logicalAddress) { |
| 220 | assertRunOnServiceThread(); |
| 221 | return mDeviceInfos.get(HdmiDeviceInfo.idForCecDevice(logicalAddress)); |
| 222 | } |
| 223 | |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 224 | @ServiceThreadOnly |
| 225 | private void updateSafeDeviceInfoList() { |
| 226 | assertRunOnServiceThread(); |
| 227 | List<HdmiDeviceInfo> copiedDevices = HdmiUtils.sparseArrayToList(mDeviceInfos); |
| 228 | synchronized (mLock) { |
| 229 | mSafeAllDeviceInfos = copiedDevices; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | @GuardedBy("mLock") |
| 234 | List<HdmiDeviceInfo> getSafeCecDevicesLocked() { |
| 235 | ArrayList<HdmiDeviceInfo> infoList = new ArrayList<>(); |
| 236 | for (HdmiDeviceInfo info : mSafeAllDeviceInfos) { |
| 237 | infoList.add(info); |
| 238 | } |
| 239 | return infoList; |
| 240 | } |
| 241 | |
Amy | 5c6026b | 2018-10-12 14:42:43 -0700 | [diff] [blame] | 242 | private void invokeDeviceEventListener(HdmiDeviceInfo info, int status) { |
| 243 | mService.invokeDeviceEventListeners(info, status); |
| 244 | } |
| 245 | |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 246 | @Override |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 247 | @ServiceThreadOnly |
Amy | 59176da | 2018-10-12 16:30:54 -0700 | [diff] [blame] | 248 | void onHotplug(int portId, boolean connected) { |
| 249 | assertRunOnServiceThread(); |
| 250 | if (connected) { |
| 251 | mService.wakeUp(); |
| 252 | } |
| 253 | if (mService.getPortInfo(portId).getType() == HdmiPortInfo.PORT_OUTPUT) { |
| 254 | mCecMessageCache.flushAll(); |
| 255 | } else { |
| 256 | if (connected) { |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 257 | launchDeviceDiscovery(); |
Amy | 59176da | 2018-10-12 16:30:54 -0700 | [diff] [blame] | 258 | } else { |
| 259 | // TODO(amyjojo): remove device from mDeviceInfo |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | @Override |
| 265 | @ServiceThreadOnly |
Amy | b887fa0 | 2018-06-21 11:22:13 -0700 | [diff] [blame] | 266 | protected void onStandby(boolean initiatedByCec, int standbyAction) { |
| 267 | assertRunOnServiceThread(); |
Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 268 | mTvSystemAudioModeSupport = false; |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 269 | // Record the last state of System Audio Control before going to standby |
| 270 | synchronized (mLock) { |
Amy | 59c06c1 | 2019-01-18 15:35:15 -0800 | [diff] [blame] | 271 | mService.writeStringSystemProperty( |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 272 | Constants.PROPERTY_LAST_SYSTEM_AUDIO_CONTROL, |
| 273 | mSystemAudioActivated ? "true" : "false"); |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 274 | } |
Shubang | bc2aab3 | 2018-07-19 16:41:17 -0700 | [diff] [blame] | 275 | terminateSystemAudioMode(); |
Amy | b887fa0 | 2018-06-21 11:22:13 -0700 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | @Override |
| 279 | @ServiceThreadOnly |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 280 | protected void onAddressAllocated(int logicalAddress, int reason) { |
| 281 | assertRunOnServiceThread(); |
Amy | b9d7f43 | 2018-11-30 15:08:30 -0800 | [diff] [blame] | 282 | if (reason == mService.INITIATED_BY_ENABLE_CEC) { |
| 283 | mService.setAndBroadcastActiveSource(mService.getPhysicalAddress(), |
| 284 | getDeviceInfo().getDeviceType(), Constants.ADDR_BROADCAST); |
| 285 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 286 | mService.sendCecCommand( |
| 287 | HdmiCecMessageBuilder.buildReportPhysicalAddressCommand( |
| 288 | mAddress, mService.getPhysicalAddress(), mDeviceType)); |
| 289 | mService.sendCecCommand( |
| 290 | HdmiCecMessageBuilder.buildDeviceVendorIdCommand(mAddress, mService.getVendorId())); |
| 291 | int systemAudioControlOnPowerOnProp = |
| 292 | SystemProperties.getInt( |
| 293 | PROPERTY_SYSTEM_AUDIO_CONTROL_ON_POWER_ON, |
| 294 | ALWAYS_SYSTEM_AUDIO_CONTROL_ON_POWER_ON); |
| 295 | boolean lastSystemAudioControlStatus = |
| 296 | SystemProperties.getBoolean(Constants.PROPERTY_LAST_SYSTEM_AUDIO_CONTROL, true); |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 297 | systemAudioControlOnPowerOn(systemAudioControlOnPowerOnProp, lastSystemAudioControlStatus); |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 298 | clearDeviceInfoList(); |
| 299 | launchDeviceDiscovery(); |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 300 | startQueuedActions(); |
| 301 | } |
| 302 | |
Shubang Lu | 00b976a | 2018-08-01 18:11:46 -0700 | [diff] [blame] | 303 | @Override |
| 304 | protected int findKeyReceiverAddress() { |
Amy | 2a5e245 | 2018-11-29 20:32:20 -0800 | [diff] [blame] | 305 | if (getActiveSource().isValid()) { |
| 306 | return getActiveSource().logicalAddress; |
| 307 | } |
| 308 | return Constants.ADDR_INVALID; |
Shubang Lu | 00b976a | 2018-08-01 18:11:46 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 311 | @VisibleForTesting |
| 312 | protected void systemAudioControlOnPowerOn( |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 313 | int systemAudioOnPowerOnProp, boolean lastSystemAudioControlStatus) { |
| 314 | if ((systemAudioOnPowerOnProp == ALWAYS_SYSTEM_AUDIO_CONTROL_ON_POWER_ON) |
| 315 | || ((systemAudioOnPowerOnProp == USE_LAST_STATE_SYSTEM_AUDIO_CONTROL_ON_POWER_ON) |
Amy | 79db52f | 2018-10-23 12:45:17 -0700 | [diff] [blame] | 316 | && lastSystemAudioControlStatus && isSystemAudioControlFeatureEnabled())) { |
Amy | 6506bd6 | 2018-07-02 17:29:36 -0700 | [diff] [blame] | 317 | addAndStartAction(new SystemAudioInitiationActionFromAvr(this)); |
| 318 | } |
| 319 | } |
| 320 | |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 321 | @Override |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 322 | @ServiceThreadOnly |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 323 | protected int getPreferredAddress() { |
| 324 | assertRunOnServiceThread(); |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 325 | return SystemProperties.getInt( |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 326 | Constants.PROPERTY_PREFERRED_ADDRESS_AUDIO_SYSTEM, Constants.ADDR_UNREGISTERED); |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | @Override |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 330 | @ServiceThreadOnly |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 331 | protected void setPreferredAddress(int addr) { |
| 332 | assertRunOnServiceThread(); |
Amy | 59c06c1 | 2019-01-18 15:35:15 -0800 | [diff] [blame] | 333 | mService.writeStringSystemProperty( |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 334 | Constants.PROPERTY_PREFERRED_ADDRESS_AUDIO_SYSTEM, String.valueOf(addr)); |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 335 | } |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 336 | |
| 337 | @Override |
| 338 | @ServiceThreadOnly |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 339 | protected boolean handleReportPhysicalAddress(HdmiCecMessage message) { |
| 340 | assertRunOnServiceThread(); |
| 341 | int path = HdmiUtils.twoBytesToInt(message.getParams()); |
| 342 | int address = message.getSource(); |
| 343 | int type = message.getParams()[2]; |
| 344 | |
| 345 | // Ignore if [Device Discovery Action] is going on. |
| 346 | if (hasAction(DeviceDiscoveryAction.class)) { |
| 347 | Slog.i(TAG, "Ignored while Device Discovery Action is in progress: " + message); |
| 348 | return true; |
| 349 | } |
| 350 | |
| 351 | // Update the device info with TIF, note that the same device info could have added in |
| 352 | // device discovery and we do not want to override it with default OSD name. Therefore we |
| 353 | // need the following check to skip redundant device info updating. |
| 354 | HdmiDeviceInfo oldDevice = getCecDeviceInfo(address); |
| 355 | if (oldDevice == null || oldDevice.getPhysicalAddress() != path) { |
| 356 | addCecDevice(new HdmiDeviceInfo( |
| 357 | address, path, mService.pathToPortId(path), type, |
| 358 | Constants.UNKNOWN_VENDOR_ID, HdmiUtils.getDefaultDeviceName(address))); |
| 359 | // if we are adding a new device info, send out a give osd name command |
| 360 | // to update the name of the device in TIF |
| 361 | mService.sendCecCommand( |
| 362 | HdmiCecMessageBuilder.buildGiveOsdNameCommand(mAddress, address)); |
| 363 | return true; |
| 364 | } |
| 365 | |
| 366 | Slog.w(TAG, "Device info exists. Not updating on Physical Address."); |
| 367 | return true; |
| 368 | } |
| 369 | |
| 370 | @Override |
Amy | 1e4a8cc | 2018-10-15 10:18:14 -0700 | [diff] [blame] | 371 | protected boolean handleReportPowerStatus(HdmiCecMessage command) { |
| 372 | int newStatus = command.getParams()[0] & 0xFF; |
| 373 | updateDevicePowerStatus(command.getSource(), newStatus); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | @Override |
Amy | 89e93af | 2018-10-15 09:56:54 -0700 | [diff] [blame] | 378 | @ServiceThreadOnly |
| 379 | protected boolean handleSetOsdName(HdmiCecMessage message) { |
| 380 | int source = message.getSource(); |
| 381 | String osdName; |
| 382 | HdmiDeviceInfo deviceInfo = getCecDeviceInfo(source); |
| 383 | // If the device is not in device list, ignore it. |
| 384 | if (deviceInfo == null) { |
| 385 | Slog.i(TAG, "No source device info for <Set Osd Name>." + message); |
| 386 | return true; |
| 387 | } |
| 388 | try { |
| 389 | osdName = new String(message.getParams(), "US-ASCII"); |
| 390 | } catch (UnsupportedEncodingException e) { |
| 391 | Slog.e(TAG, "Invalid <Set Osd Name> request:" + message, e); |
| 392 | return true; |
| 393 | } |
| 394 | |
| 395 | if (deviceInfo.getDisplayName().equals(osdName)) { |
| 396 | Slog.d(TAG, "Ignore incoming <Set Osd Name> having same osd name:" + message); |
| 397 | return true; |
| 398 | } |
| 399 | |
| 400 | Slog.d(TAG, "Updating device OSD name from " |
| 401 | + deviceInfo.getDisplayName() |
| 402 | + " to " + osdName); |
| 403 | updateCecDevice(new HdmiDeviceInfo(deviceInfo.getLogicalAddress(), |
| 404 | deviceInfo.getPhysicalAddress(), deviceInfo.getPortId(), |
| 405 | deviceInfo.getDeviceType(), deviceInfo.getVendorId(), osdName)); |
| 406 | return true; |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | @ServiceThreadOnly |
Amy | 4a92266 | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 411 | protected boolean handleReportAudioStatus(HdmiCecMessage message) { |
| 412 | assertRunOnServiceThread(); |
| 413 | // TODO(amyjojo): implement report audio status handler |
| 414 | HdmiLogger.debug(TAG + "Stub handleReportAudioStatus"); |
| 415 | return true; |
| 416 | } |
| 417 | |
| 418 | @Override |
| 419 | @ServiceThreadOnly |
| 420 | protected boolean handleInitiateArc(HdmiCecMessage message) { |
| 421 | assertRunOnServiceThread(); |
| 422 | // TODO(amyjojo): implement initiate arc handler |
| 423 | HdmiLogger.debug(TAG + "Stub handleInitiateArc"); |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | @Override |
| 428 | @ServiceThreadOnly |
| 429 | protected boolean handleReportArcInitiate(HdmiCecMessage message) { |
| 430 | assertRunOnServiceThread(); |
| 431 | // TODO(amyjojo): implement report arc initiate handler |
| 432 | HdmiLogger.debug(TAG + "Stub handleReportArcInitiate"); |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | @ServiceThreadOnly |
| 438 | protected boolean handleReportArcTermination(HdmiCecMessage message) { |
| 439 | assertRunOnServiceThread(); |
| 440 | // TODO(amyjojo): implement report arc terminate handler |
| 441 | HdmiLogger.debug(TAG + "Stub handleReportArcTermination"); |
| 442 | return true; |
| 443 | } |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 444 | |
| 445 | @Override |
| 446 | @ServiceThreadOnly |
| 447 | protected boolean handleGiveAudioStatus(HdmiCecMessage message) { |
| 448 | assertRunOnServiceThread(); |
Amy | 79db52f | 2018-10-23 12:45:17 -0700 | [diff] [blame] | 449 | if (isSystemAudioControlFeatureEnabled()) { |
| 450 | reportAudioStatus(message.getSource()); |
| 451 | } else { |
| 452 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 453 | } |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 454 | return true; |
| 455 | } |
| 456 | |
Amy | 87eda82 | 2018-06-06 17:56:39 -0700 | [diff] [blame] | 457 | @Override |
| 458 | @ServiceThreadOnly |
| 459 | protected boolean handleGiveSystemAudioModeStatus(HdmiCecMessage message) { |
| 460 | assertRunOnServiceThread(); |
Amy | 27ae403 | 2019-01-31 16:42:16 -0800 | [diff] [blame^] | 461 | // If the audio system is initiating the system audio mode on and TV asks the sam status at |
| 462 | // the same time, respond with true. Since we know TV supports sam in this situation. |
| 463 | // If the query comes from STB, we should respond with the current sam status and the STB |
| 464 | // should listen to the <Set System Audio Mode> broadcasting. |
| 465 | boolean isSystemAudioModeOnOrTurningOn = isSystemAudioActivated(); |
| 466 | if (!isSystemAudioModeOnOrTurningOn |
| 467 | && message.getSource() == Constants.ADDR_TV |
| 468 | && hasAction(SystemAudioInitiationActionFromAvr.class)) { |
| 469 | isSystemAudioModeOnOrTurningOn = true; |
| 470 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 471 | mService.sendCecCommand( |
| 472 | HdmiCecMessageBuilder.buildReportSystemAudioMode( |
Amy | 27ae403 | 2019-01-31 16:42:16 -0800 | [diff] [blame^] | 473 | mAddress, message.getSource(), isSystemAudioModeOnOrTurningOn)); |
Amy | 87eda82 | 2018-06-06 17:56:39 -0700 | [diff] [blame] | 474 | return true; |
| 475 | } |
| 476 | |
Amy | 4e7ff1a | 2018-06-07 16:24:31 -0700 | [diff] [blame] | 477 | @Override |
| 478 | @ServiceThreadOnly |
| 479 | protected boolean handleRequestArcInitiate(HdmiCecMessage message) { |
| 480 | assertRunOnServiceThread(); |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 481 | removeAction(ArcInitiationActionFromAvr.class); |
Amy | 59c06c1 | 2019-01-18 15:35:15 -0800 | [diff] [blame] | 482 | if (!mService.readBooleanSystemProperty(Constants.PROPERTY_ARC_SUPPORT, true)) { |
Amy | 77c7c98 | 2018-07-23 18:27:36 -0700 | [diff] [blame] | 483 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
| 484 | } else if (!isDirectConnectToTv()) { |
| 485 | HdmiLogger.debug("AVR device is not directly connected with TV"); |
| 486 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE); |
| 487 | } else { |
| 488 | addAndStartAction(new ArcInitiationActionFromAvr(this)); |
| 489 | } |
Amy | 4e7ff1a | 2018-06-07 16:24:31 -0700 | [diff] [blame] | 490 | return true; |
| 491 | } |
| 492 | |
| 493 | @Override |
| 494 | @ServiceThreadOnly |
| 495 | protected boolean handleRequestArcTermination(HdmiCecMessage message) { |
| 496 | assertRunOnServiceThread(); |
Amy | 77c7c98 | 2018-07-23 18:27:36 -0700 | [diff] [blame] | 497 | if (!SystemProperties.getBoolean(Constants.PROPERTY_ARC_SUPPORT, true)) { |
| 498 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNRECOGNIZED_OPCODE); |
| 499 | } else if (!isArcEnabled()) { |
| 500 | HdmiLogger.debug("ARC is not established between TV and AVR device"); |
| 501 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE); |
| 502 | } else { |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 503 | removeAction(ArcTerminationActionFromAvr.class); |
Amy | 77c7c98 | 2018-07-23 18:27:36 -0700 | [diff] [blame] | 504 | addAndStartAction(new ArcTerminationActionFromAvr(this)); |
| 505 | } |
Amy | 4e7ff1a | 2018-06-07 16:24:31 -0700 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | |
Nick Chalko | 6f5d69e | 2018-07-17 16:07:11 -0700 | [diff] [blame] | 509 | @ServiceThreadOnly |
| 510 | protected boolean handleRequestShortAudioDescriptor(HdmiCecMessage message) { |
| 511 | assertRunOnServiceThread(); |
Nick Chalko | 6f5d69e | 2018-07-17 16:07:11 -0700 | [diff] [blame] | 512 | HdmiLogger.debug(TAG + "Stub handleRequestShortAudioDescriptor"); |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 513 | if (!isSystemAudioControlFeatureEnabled()) { |
| 514 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 515 | return true; |
| 516 | } |
| 517 | if (!isSystemAudioActivated()) { |
| 518 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_NOT_IN_CORRECT_MODE); |
| 519 | return true; |
| 520 | } |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 521 | |
| 522 | List<DeviceConfig> config = null; |
| 523 | File file = new File(SHORT_AUDIO_DESCRIPTOR_CONFIG_PATH); |
| 524 | if (file.exists()) { |
| 525 | try { |
| 526 | InputStream in = new FileInputStream(file); |
| 527 | config = HdmiUtils.ShortAudioDescriptorXmlParser.parse(in); |
| 528 | in.close(); |
| 529 | } catch (IOException e) { |
| 530 | Slog.e(TAG, "Error reading file: " + file, e); |
| 531 | } catch (XmlPullParserException e) { |
| 532 | Slog.e(TAG, "Unable to parse file: " + file, e); |
| 533 | } |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 534 | } |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 535 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 536 | @AudioCodec int[] audioFormatCodes = parseAudioFormatCodes(message.getParams()); |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 537 | byte[] sadBytes; |
| 538 | if (config != null && config.size() > 0) { |
| 539 | sadBytes = getSupportedShortAudioDescriptorsFromConfig(config, audioFormatCodes); |
| 540 | } else { |
| 541 | AudioDeviceInfo deviceInfo = getSystemAudioDeviceInfo(); |
| 542 | if (deviceInfo == null) { |
| 543 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_UNABLE_TO_DETERMINE); |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | sadBytes = getSupportedShortAudioDescriptors(deviceInfo, audioFormatCodes); |
| 548 | } |
| 549 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 550 | if (sadBytes.length == 0) { |
| 551 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_INVALID_OPERAND); |
| 552 | } else { |
| 553 | mService.sendCecCommand( |
| 554 | HdmiCecMessageBuilder.buildReportShortAudioDescriptor( |
| 555 | mAddress, message.getSource(), sadBytes)); |
| 556 | } |
Nick Chalko | 6f5d69e | 2018-07-17 16:07:11 -0700 | [diff] [blame] | 557 | return true; |
| 558 | } |
| 559 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 560 | private byte[] getSupportedShortAudioDescriptors( |
| 561 | AudioDeviceInfo deviceInfo, @AudioCodec int[] audioFormatCodes) { |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 562 | ArrayList<byte[]> sads = new ArrayList<>(audioFormatCodes.length); |
| 563 | for (@AudioCodec int audioFormatCode : audioFormatCodes) { |
| 564 | byte[] sad = getSupportedShortAudioDescriptor(deviceInfo, audioFormatCode); |
| 565 | if (sad != null) { |
| 566 | if (sad.length == 3) { |
| 567 | |
| 568 | sads.add(sad); |
| 569 | } else { |
| 570 | HdmiLogger.warning( |
| 571 | "Dropping Short Audio Descriptor with length %d for requested codec %x", |
| 572 | sad.length, audioFormatCode); |
| 573 | } |
| 574 | } |
| 575 | } |
Amy | d984bc6 | 2018-11-28 18:53:54 -0800 | [diff] [blame] | 576 | return getShortAudioDescriptorBytes(sads); |
| 577 | } |
| 578 | |
| 579 | private byte[] getSupportedShortAudioDescriptorsFromConfig( |
| 580 | List<DeviceConfig> deviceConfig, @AudioCodec int[] audioFormatCodes) { |
| 581 | DeviceConfig deviceConfigToUse = null; |
| 582 | for (DeviceConfig device : deviceConfig) { |
| 583 | // TODO(amyjojo) use PROPERTY_SYSTEM_AUDIO_MODE_AUDIO_PORT to get the audio device name |
| 584 | if (device.name.equals("VX_AUDIO_DEVICE_IN_HDMI_ARC")) { |
| 585 | deviceConfigToUse = device; |
| 586 | break; |
| 587 | } |
| 588 | } |
| 589 | if (deviceConfigToUse == null) { |
| 590 | // TODO(amyjojo) use PROPERTY_SYSTEM_AUDIO_MODE_AUDIO_PORT to get the audio device name |
| 591 | Slog.w(TAG, "sadConfig.xml does not have required device info for " |
| 592 | + "VX_AUDIO_DEVICE_IN_HDMI_ARC"); |
| 593 | return new byte[0]; |
| 594 | } |
| 595 | HashMap<Integer, byte[]> map = new HashMap<>(); |
| 596 | ArrayList<byte[]> sads = new ArrayList<>(audioFormatCodes.length); |
| 597 | for (CodecSad codecSad : deviceConfigToUse.supportedCodecs) { |
| 598 | map.put(codecSad.audioCodec, codecSad.sad); |
| 599 | } |
| 600 | for (int i = 0; i < audioFormatCodes.length; i++) { |
| 601 | if (map.containsKey(audioFormatCodes[i])) { |
| 602 | byte[] sad = map.get(audioFormatCodes[i]); |
| 603 | if (sad != null && sad.length == 3) { |
| 604 | sads.add(sad); |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | return getShortAudioDescriptorBytes(sads); |
| 609 | } |
| 610 | |
| 611 | private byte[] getShortAudioDescriptorBytes(ArrayList<byte[]> sads) { |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 612 | // Short Audio Descriptors are always 3 bytes long. |
| 613 | byte[] bytes = new byte[sads.size() * 3]; |
| 614 | int index = 0; |
| 615 | for (byte[] sad : sads) { |
| 616 | System.arraycopy(sad, 0, bytes, index, 3); |
| 617 | index += 3; |
| 618 | } |
| 619 | return bytes; |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Returns a 3 byte short audio descriptor as described in CEC 1.4 table 29 or null if the |
| 624 | * audioFormatCode is not supported. |
| 625 | */ |
| 626 | @Nullable |
| 627 | private byte[] getSupportedShortAudioDescriptor( |
| 628 | AudioDeviceInfo deviceInfo, @AudioCodec int audioFormatCode) { |
| 629 | switch (audioFormatCode) { |
| 630 | case Constants.AUDIO_CODEC_NONE: { |
| 631 | return null; |
| 632 | } |
| 633 | case Constants.AUDIO_CODEC_LPCM: { |
| 634 | return getLpcmShortAudioDescriptor(deviceInfo); |
| 635 | } |
| 636 | // TODO(b/80297701): implement the rest of the codecs |
| 637 | case Constants.AUDIO_CODEC_DD: |
| 638 | case Constants.AUDIO_CODEC_MPEG1: |
| 639 | case Constants.AUDIO_CODEC_MP3: |
| 640 | case Constants.AUDIO_CODEC_MPEG2: |
| 641 | case Constants.AUDIO_CODEC_AAC: |
| 642 | case Constants.AUDIO_CODEC_DTS: |
| 643 | case Constants.AUDIO_CODEC_ATRAC: |
| 644 | case Constants.AUDIO_CODEC_ONEBITAUDIO: |
| 645 | case Constants.AUDIO_CODEC_DDP: |
| 646 | case Constants.AUDIO_CODEC_DTSHD: |
| 647 | case Constants.AUDIO_CODEC_TRUEHD: |
| 648 | case Constants.AUDIO_CODEC_DST: |
| 649 | case Constants.AUDIO_CODEC_WMAPRO: |
| 650 | default: { |
| 651 | return null; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | @Nullable |
| 657 | private byte[] getLpcmShortAudioDescriptor(AudioDeviceInfo deviceInfo) { |
| 658 | // TODO(b/80297701): implement |
| 659 | return null; |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | @Nullable |
| 663 | private AudioDeviceInfo getSystemAudioDeviceInfo() { |
Nick Chalko | d70c413 | 2018-08-27 16:22:53 -0700 | [diff] [blame] | 664 | AudioManager audioManager = mService.getContext().getSystemService(AudioManager.class); |
| 665 | if (audioManager == null) { |
| 666 | HdmiLogger.error( |
| 667 | "Error getting system audio device because AudioManager not available."); |
| 668 | return null; |
| 669 | } |
| 670 | AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_INPUTS); |
| 671 | HdmiLogger.debug("Found %d audio input devices", devices.length); |
| 672 | for (AudioDeviceInfo device : devices) { |
| 673 | HdmiLogger.debug("%s at port %s", device.getProductName(), device.getPort()); |
| 674 | HdmiLogger.debug("Supported encodings are %s", |
| 675 | Arrays.stream(device.getEncodings()).mapToObj( |
| 676 | AudioFormat::toLogFriendlyEncoding |
| 677 | ).collect(Collectors.joining(", "))); |
| 678 | // TODO(b/80297701) use the actual device type that system audio mode is connected to. |
| 679 | if (device.getType() == AudioDeviceInfo.TYPE_HDMI_ARC) { |
| 680 | return device; |
| 681 | } |
| 682 | } |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 683 | return null; |
| 684 | } |
| 685 | |
| 686 | @AudioCodec |
| 687 | private int[] parseAudioFormatCodes(byte[] params) { |
| 688 | @AudioCodec int[] audioFormatCodes = new int[params.length]; |
| 689 | for (int i = 0; i < params.length; i++) { |
| 690 | byte val = params[i]; |
| 691 | audioFormatCodes[i] = |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 692 | val >= 1 && val <= Constants.AUDIO_CODEC_MAX ? val : Constants.AUDIO_CODEC_NONE; |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 693 | } |
| 694 | return audioFormatCodes; |
| 695 | } |
| 696 | |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 697 | @Override |
| 698 | @ServiceThreadOnly |
| 699 | protected boolean handleSystemAudioModeRequest(HdmiCecMessage message) { |
| 700 | assertRunOnServiceThread(); |
| 701 | boolean systemAudioStatusOn = message.getParams().length != 0; |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 702 | // Check if the request comes from a non-TV device. |
| 703 | // Need to check if TV supports System Audio Control |
| 704 | // if non-TV device tries to turn on the feature |
| 705 | if (message.getSource() != Constants.ADDR_TV) { |
| 706 | if (systemAudioStatusOn) { |
| 707 | handleSystemAudioModeOnFromNonTvDevice(message); |
| 708 | return true; |
| 709 | } |
| 710 | } else { |
| 711 | // If TV request the feature on |
| 712 | // cache TV supporting System Audio Control |
| 713 | // until Audio System loses its physical address. |
| 714 | setTvSystemAudioModeSupport(true); |
| 715 | } |
| 716 | // If TV or Audio System does not support the feature, |
| 717 | // will send abort command. |
| 718 | if (!checkSupportAndSetSystemAudioMode(systemAudioStatusOn)) { |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 719 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 720 | return true; |
| 721 | } |
| 722 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 723 | mService.sendCecCommand( |
| 724 | HdmiCecMessageBuilder.buildSetSystemAudioMode( |
| 725 | mAddress, Constants.ADDR_BROADCAST, systemAudioStatusOn)); |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 726 | return true; |
| 727 | } |
| 728 | |
| 729 | @Override |
| 730 | @ServiceThreadOnly |
| 731 | protected boolean handleSetSystemAudioMode(HdmiCecMessage message) { |
| 732 | assertRunOnServiceThread(); |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 733 | if (!checkSupportAndSetSystemAudioMode( |
| 734 | HdmiUtils.parseCommandParamSystemAudioStatus(message))) { |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 735 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 736 | } |
| 737 | return true; |
| 738 | } |
| 739 | |
| 740 | @Override |
| 741 | @ServiceThreadOnly |
| 742 | protected boolean handleSystemAudioModeStatus(HdmiCecMessage message) { |
| 743 | assertRunOnServiceThread(); |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 744 | if (!checkSupportAndSetSystemAudioMode( |
| 745 | HdmiUtils.parseCommandParamSystemAudioStatus(message))) { |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 746 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 747 | } |
| 748 | return true; |
| 749 | } |
| 750 | |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 751 | @ServiceThreadOnly |
| 752 | void setArcStatus(boolean enabled) { |
| 753 | // TODO(shubang): add tests |
| 754 | assertRunOnServiceThread(); |
| 755 | |
| 756 | HdmiLogger.debug("Set Arc Status[old:%b new:%b]", mArcEstablished, enabled); |
| 757 | // 1. Enable/disable ARC circuit. |
| 758 | enableAudioReturnChannel(enabled); |
| 759 | // 2. Notify arc status to audio service. |
| 760 | notifyArcStatusToAudioService(enabled); |
| 761 | // 3. Update arc status; |
| 762 | mArcEstablished = enabled; |
| 763 | } |
| 764 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 765 | /** Switch hardware ARC circuit in the system. */ |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 766 | @ServiceThreadOnly |
| 767 | private void enableAudioReturnChannel(boolean enabled) { |
| 768 | assertRunOnServiceThread(); |
| 769 | mService.enableAudioReturnChannel( |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 770 | SystemProperties.getInt(Constants.PROPERTY_SYSTEM_AUDIO_DEVICE_ARC_PORT, 0), |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 771 | enabled); |
| 772 | } |
| 773 | |
| 774 | private void notifyArcStatusToAudioService(boolean enabled) { |
| 775 | // Note that we don't set any name to ARC. |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 776 | mService.getAudioManager() |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 777 | .setWiredDeviceConnectionState(AudioSystem.DEVICE_IN_HDMI, enabled ? 1 : 0, "", ""); |
Shubang | 81170da | 2018-07-12 18:02:52 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Nick Chalko | 01b979c | 2018-10-19 14:54:30 -0700 | [diff] [blame] | 780 | void reportAudioStatus(int source) { |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 781 | assertRunOnServiceThread(); |
| 782 | |
| 783 | int volume = mService.getAudioManager().getStreamVolume(AudioManager.STREAM_MUSIC); |
| 784 | boolean mute = mService.getAudioManager().isStreamMute(AudioManager.STREAM_MUSIC); |
| 785 | int maxVolume = mService.getAudioManager().getStreamMaxVolume(AudioManager.STREAM_MUSIC); |
Nick Chalko | 01b979c | 2018-10-19 14:54:30 -0700 | [diff] [blame] | 786 | int minVolume = mService.getAudioManager().getStreamMinVolume(AudioManager.STREAM_MUSIC); |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 787 | int scaledVolume = VolumeControlAction.scaleToCecVolume(volume, maxVolume); |
Nick Chalko | 01b979c | 2018-10-19 14:54:30 -0700 | [diff] [blame] | 788 | HdmiLogger.debug("Reporting volume %i (%i-%i) as CEC volume %i", volume, |
| 789 | minVolume, maxVolume, scaledVolume); |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 790 | |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 791 | mService.sendCecCommand( |
| 792 | HdmiCecMessageBuilder.buildReportAudioStatus( |
Nick Chalko | 01b979c | 2018-10-19 14:54:30 -0700 | [diff] [blame] | 793 | mAddress, source, scaledVolume, mute)); |
Amy | 2a6c3dc | 2018-06-05 17:31:55 -0700 | [diff] [blame] | 794 | } |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 795 | |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 796 | /** |
| 797 | * Method to check if device support System Audio Control. If so, wake up device if necessary. |
| 798 | * |
| 799 | * <p> then call {@link #setSystemAudioMode(boolean)} to turn on or off System Audio Mode |
| 800 | * @param newSystemAudioMode turning feature on or off. True is on. False is off. |
| 801 | * @return true or false. |
| 802 | * |
| 803 | * <p>False when device does not support the feature. Otherwise returns true. |
| 804 | */ |
| 805 | protected boolean checkSupportAndSetSystemAudioMode(boolean newSystemAudioMode) { |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 806 | if (!isSystemAudioControlFeatureEnabled()) { |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 807 | HdmiLogger.debug( |
| 808 | "Cannot turn " |
| 809 | + (newSystemAudioMode ? "on" : "off") |
| 810 | + "system audio mode " |
| 811 | + "because the System Audio Control feature is disabled."); |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 812 | return false; |
| 813 | } |
Nick Chalko | f32fcea | 2018-07-17 16:17:40 -0700 | [diff] [blame] | 814 | HdmiLogger.debug( |
| 815 | "System Audio Mode change[old:%b new:%b]", |
| 816 | mSystemAudioActivated, newSystemAudioMode); |
Amy | ae4ee34 | 2018-10-09 16:55:14 -0700 | [diff] [blame] | 817 | // Wake up device if System Audio Control is turned on |
| 818 | if (newSystemAudioMode) { |
Amy | 0fd41e3 | 2018-06-21 13:40:27 -0700 | [diff] [blame] | 819 | mService.wakeUp(); |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 820 | } |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 821 | setSystemAudioMode(newSystemAudioMode); |
| 822 | return true; |
| 823 | } |
| 824 | |
| 825 | /** |
| 826 | * Real work to turn on or off System Audio Mode. |
| 827 | * |
| 828 | * Use {@link #checkSupportAndSetSystemAudioMode(boolean)} |
| 829 | * if trying to turn on or off the feature. |
| 830 | */ |
| 831 | private void setSystemAudioMode(boolean newSystemAudioMode) { |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 832 | int targetPhysicalAddress = getActiveSource().physicalAddress; |
Amy | 0063811 | 2018-10-31 17:47:17 -0700 | [diff] [blame] | 833 | int port = mService.pathToPortId(targetPhysicalAddress); |
Amy | 6a6d618 | 2018-08-20 17:38:47 -0700 | [diff] [blame] | 834 | if (newSystemAudioMode && port >= 0) { |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 835 | switchToAudioInput(); |
Amy | 0fd41e3 | 2018-06-21 13:40:27 -0700 | [diff] [blame] | 836 | } |
Amy | cce55e0 | 2018-08-20 15:42:25 -0700 | [diff] [blame] | 837 | // Mute device when feature is turned off and unmute device when feature is turned on. |
| 838 | // PROPERTY_SYSTEM_AUDIO_MODE_MUTING_ENABLE is false when device never needs to be muted. |
| 839 | boolean currentMuteStatus = |
| 840 | mService.getAudioManager().isStreamMute(AudioManager.STREAM_MUSIC); |
Amy | 61fc25f | 2018-11-02 17:53:21 -0700 | [diff] [blame] | 841 | if (currentMuteStatus == newSystemAudioMode) { |
Amy | 59c06c1 | 2019-01-18 15:35:15 -0800 | [diff] [blame] | 842 | if (mService.readBooleanSystemProperty( |
Amy | 61fc25f | 2018-11-02 17:53:21 -0700 | [diff] [blame] | 843 | Constants.PROPERTY_SYSTEM_AUDIO_MODE_MUTING_ENABLE, true) |
| 844 | || newSystemAudioMode) { |
| 845 | mService.getAudioManager() |
| 846 | .adjustStreamVolume( |
| 847 | AudioManager.STREAM_MUSIC, |
| 848 | newSystemAudioMode |
| 849 | ? AudioManager.ADJUST_UNMUTE |
| 850 | : AudioManager.ADJUST_MUTE, |
| 851 | 0); |
| 852 | } |
Amy | cce55e0 | 2018-08-20 15:42:25 -0700 | [diff] [blame] | 853 | } |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 854 | updateAudioManagerForSystemAudio(newSystemAudioMode); |
| 855 | synchronized (mLock) { |
| 856 | if (mSystemAudioActivated != newSystemAudioMode) { |
| 857 | mSystemAudioActivated = newSystemAudioMode; |
| 858 | mService.announceSystemAudioModeChange(newSystemAudioMode); |
| 859 | } |
| 860 | } |
Amy | acc1dab | 2018-09-06 17:09:51 -0700 | [diff] [blame] | 861 | // Init arc whenever System Audio Mode is on |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 862 | // Terminate arc when System Audio Mode is off |
| 863 | // Since some TVs don't request ARC on with System Audio Mode on request |
| 864 | if (SystemProperties.getBoolean(Constants.PROPERTY_ARC_SUPPORT, true) |
| 865 | && isDirectConnectToTv()) { |
| 866 | if (newSystemAudioMode && !isArcEnabled()) { |
| 867 | removeAction(ArcInitiationActionFromAvr.class); |
| 868 | addAndStartAction(new ArcInitiationActionFromAvr(this)); |
| 869 | } else if (!newSystemAudioMode && isArcEnabled()) { |
| 870 | removeAction(ArcTerminationActionFromAvr.class); |
| 871 | addAndStartAction(new ArcTerminationActionFromAvr(this)); |
| 872 | } |
Amy | acc1dab | 2018-09-06 17:09:51 -0700 | [diff] [blame] | 873 | } |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Amy | ac1a55c | 2018-07-13 16:38:23 -0700 | [diff] [blame] | 876 | protected void switchToAudioInput() { |
| 877 | // TODO(b/111396634): switch input according to PROPERTY_SYSTEM_AUDIO_MODE_AUDIO_PORT |
| 878 | } |
| 879 | |
Amy | 77c7c98 | 2018-07-23 18:27:36 -0700 | [diff] [blame] | 880 | protected boolean isDirectConnectToTv() { |
| 881 | int myPhysicalAddress = mService.getPhysicalAddress(); |
| 882 | return (myPhysicalAddress & Constants.ROUTING_PATH_TOP_MASK) == myPhysicalAddress; |
| 883 | } |
| 884 | |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 885 | private void updateAudioManagerForSystemAudio(boolean on) { |
| 886 | int device = mService.getAudioManager().setHdmiSystemAudioSupported(on); |
| 887 | HdmiLogger.debug("[A]UpdateSystemAudio mode[on=%b] output=[%X]", on, device); |
| 888 | } |
| 889 | |
Amy | 79db52f | 2018-10-23 12:45:17 -0700 | [diff] [blame] | 890 | void onSystemAduioControlFeatureSupportChanged(boolean enabled) { |
| 891 | setSystemAudioControlFeatureEnabled(enabled); |
| 892 | if (enabled) { |
| 893 | addAndStartAction(new SystemAudioInitiationActionFromAvr(this)); |
| 894 | } |
| 895 | } |
| 896 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 897 | @ServiceThreadOnly |
| 898 | void setSystemAudioControlFeatureEnabled(boolean enabled) { |
| 899 | assertRunOnServiceThread(); |
| 900 | synchronized (mLock) { |
| 901 | mSystemAudioControlFeatureEnabled = enabled; |
| 902 | } |
| 903 | } |
| 904 | |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 905 | @ServiceThreadOnly |
Amy | 0c2e29f | 2018-10-23 12:17:52 -0700 | [diff] [blame] | 906 | void setRoutingControlFeatureEnables(boolean enabled) { |
| 907 | assertRunOnServiceThread(); |
| 908 | synchronized (mLock) { |
| 909 | mRoutingControlFeatureEnabled = enabled; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | @ServiceThreadOnly |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 914 | void doManualPortSwitching(int portId, IHdmiControlCallback callback) { |
| 915 | assertRunOnServiceThread(); |
| 916 | // TODO: validate port ID |
| 917 | if (portId == getLocalActivePort()) { |
| 918 | invokeCallback(callback, HdmiControlManager.RESULT_SUCCESS); |
| 919 | return; |
| 920 | } |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 921 | if (!mService.isControlEnabled()) { |
Amy | f9fa617 | 2018-10-11 16:47:12 -0700 | [diff] [blame] | 922 | setRoutingPort(portId); |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 923 | setLocalActivePort(portId); |
| 924 | invokeCallback(callback, HdmiControlManager.RESULT_INCORRECT_MODE); |
| 925 | return; |
| 926 | } |
Amy | f9fa617 | 2018-10-11 16:47:12 -0700 | [diff] [blame] | 927 | int oldPath = getRoutingPort() != Constants.CEC_SWITCH_HOME |
| 928 | ? mService.portIdToPath(getRoutingPort()) |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 929 | : getDeviceInfo().getPhysicalAddress(); |
Amy | f9fa617 | 2018-10-11 16:47:12 -0700 | [diff] [blame] | 930 | int newPath = mService.portIdToPath(portId); |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 931 | if (oldPath == newPath) { |
| 932 | return; |
| 933 | } |
Amy | f9fa617 | 2018-10-11 16:47:12 -0700 | [diff] [blame] | 934 | setRoutingPort(portId); |
shubang | d932cb5 | 2018-09-14 17:55:16 -0700 | [diff] [blame] | 935 | setLocalActivePort(portId); |
| 936 | HdmiCecMessage routingChange = |
| 937 | HdmiCecMessageBuilder.buildRoutingChange(mAddress, oldPath, newPath); |
| 938 | mService.sendCecCommand(routingChange); |
| 939 | } |
| 940 | |
Nick Chalko | 167529c | 2018-07-18 13:11:31 -0700 | [diff] [blame] | 941 | boolean isSystemAudioControlFeatureEnabled() { |
Amy | 9b91e8c | 2018-06-11 17:26:26 -0700 | [diff] [blame] | 942 | synchronized (mLock) { |
| 943 | return mSystemAudioControlFeatureEnabled; |
| 944 | } |
| 945 | } |
Nick Chalko | f28c7b5 | 2018-06-14 07:46:00 -0700 | [diff] [blame] | 946 | |
Shubang | 595a183 | 2018-06-27 17:52:18 -0700 | [diff] [blame] | 947 | protected boolean isSystemAudioActivated() { |
| 948 | synchronized (mLock) { |
| 949 | return mSystemAudioActivated; |
| 950 | } |
| 951 | } |
| 952 | |
Shubang | 2fd186e | 2018-07-18 16:53:12 -0700 | [diff] [blame] | 953 | protected void terminateSystemAudioMode() { |
| 954 | // remove pending initiation actions |
| 955 | removeAction(SystemAudioInitiationActionFromAvr.class); |
Shubang | bc2aab3 | 2018-07-19 16:41:17 -0700 | [diff] [blame] | 956 | if (!isSystemAudioActivated()) { |
| 957 | return; |
Shubang | 2fd186e | 2018-07-18 16:53:12 -0700 | [diff] [blame] | 958 | } |
| 959 | |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 960 | if (checkSupportAndSetSystemAudioMode(false)) { |
Shubang | bc2aab3 | 2018-07-19 16:41:17 -0700 | [diff] [blame] | 961 | // send <Set System Audio Mode> [“Off”] |
| 962 | mService.sendCecCommand( |
| 963 | HdmiCecMessageBuilder.buildSetSystemAudioMode( |
| 964 | mAddress, Constants.ADDR_BROADCAST, false)); |
Shubang | 2fd186e | 2018-07-18 16:53:12 -0700 | [diff] [blame] | 965 | } |
| 966 | } |
| 967 | |
Nick Chalko | f28c7b5 | 2018-06-14 07:46:00 -0700 | [diff] [blame] | 968 | /** Reports if System Audio Mode is supported by the connected TV */ |
| 969 | interface TvSystemAudioModeSupportedCallback { |
| 970 | |
| 971 | /** {@code supported} is true if the TV is connected and supports System Audio Mode. */ |
| 972 | void onResult(boolean supported); |
Nick Chalko | f28c7b5 | 2018-06-14 07:46:00 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | /** |
| 976 | * Queries the connected TV to detect if System Audio Mode is supported by the TV. |
| 977 | * |
| 978 | * <p>This query may take up to 2 seconds to complete. |
| 979 | * |
| 980 | * <p>The result of the query may be cached until Audio device type is put in standby or loses |
| 981 | * its physical address. |
| 982 | */ |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 983 | // TODO(amyjojo): making mTvSystemAudioModeSupport null originally and fix the logic. |
Nick Chalko | f28c7b5 | 2018-06-14 07:46:00 -0700 | [diff] [blame] | 984 | void queryTvSystemAudioModeSupport(TvSystemAudioModeSupportedCallback callback) { |
Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 985 | if (!mTvSystemAudioModeSupport) { |
| 986 | addAndStartAction(new DetectTvSystemAudioModeSupportAction(this, callback)); |
| 987 | } else { |
| 988 | callback.onResult(true); |
| 989 | } |
| 990 | } |
| 991 | |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 992 | /** |
| 993 | * Handler of System Audio Mode Request on from non TV device |
| 994 | */ |
| 995 | void handleSystemAudioModeOnFromNonTvDevice(HdmiCecMessage message) { |
| 996 | if (!isSystemAudioControlFeatureEnabled()) { |
| 997 | HdmiLogger.debug( |
| 998 | "Cannot turn on" + "system audio mode " |
| 999 | + "because the System Audio Control feature is disabled."); |
| 1000 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 1001 | return; |
| 1002 | } |
Amy | ae4ee34 | 2018-10-09 16:55:14 -0700 | [diff] [blame] | 1003 | // Wake up device |
| 1004 | mService.wakeUp(); |
Amy | 06dc4cd | 2018-08-09 18:22:40 -0700 | [diff] [blame] | 1005 | // Check if TV supports System Audio Control. |
| 1006 | // Handle broadcasting setSystemAudioMode on or aborting message on callback. |
| 1007 | queryTvSystemAudioModeSupport(new TvSystemAudioModeSupportedCallback() { |
| 1008 | public void onResult(boolean supported) { |
| 1009 | if (supported) { |
| 1010 | setSystemAudioMode(true); |
| 1011 | mService.sendCecCommand( |
| 1012 | HdmiCecMessageBuilder.buildSetSystemAudioMode( |
| 1013 | mAddress, Constants.ADDR_BROADCAST, true)); |
| 1014 | } else { |
| 1015 | mService.maySendFeatureAbortCommand(message, Constants.ABORT_REFUSED); |
| 1016 | } |
| 1017 | } |
| 1018 | }); |
| 1019 | } |
| 1020 | |
Shubang | ec66896 | 2018-07-13 19:03:19 -0700 | [diff] [blame] | 1021 | void setTvSystemAudioModeSupport(boolean supported) { |
| 1022 | mTvSystemAudioModeSupport = supported; |
Nick Chalko | f28c7b5 | 2018-06-14 07:46:00 -0700 | [diff] [blame] | 1023 | } |
Amy | 77c7c98 | 2018-07-23 18:27:36 -0700 | [diff] [blame] | 1024 | |
| 1025 | @VisibleForTesting |
| 1026 | protected boolean isArcEnabled() { |
| 1027 | synchronized (mLock) { |
| 1028 | return mArcEstablished; |
| 1029 | } |
| 1030 | } |
Amy | 93a97b9 | 2018-08-17 14:58:41 -0700 | [diff] [blame] | 1031 | |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1032 | @Override |
| 1033 | protected void switchInputOnReceivingNewActivePath(int physicalAddress) { |
Amy | 0063811 | 2018-10-31 17:47:17 -0700 | [diff] [blame] | 1034 | int port = mService.pathToPortId(physicalAddress); |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 1035 | if (isSystemAudioActivated() && port < 0) { |
| 1036 | // If system audio mode is on and the new active source is not under the current device, |
| 1037 | // Will switch to ARC input. |
| 1038 | // TODO(b/115637145): handle system aduio without ARC |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1039 | routeToInputFromPortId(Constants.CEC_SWITCH_ARC); |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 1040 | } else if (mIsSwitchDevice && port >= 0) { |
| 1041 | // If current device is a switch and the new active source is under it, |
| 1042 | // will switch to the corresponding active path. |
| 1043 | routeToInputFromPortId(port); |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1047 | protected void routeToInputFromPortId(int portId) { |
Amy | 0c2e29f | 2018-10-23 12:17:52 -0700 | [diff] [blame] | 1048 | if (!isRoutingControlFeatureEnabled()) { |
| 1049 | HdmiLogger.debug("Routing Control Feature is not enabled."); |
| 1050 | return; |
| 1051 | } |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1052 | if (mArcIntentUsed) { |
| 1053 | routeToTvInputFromPortId(portId); |
| 1054 | } else { |
| 1055 | // TODO(): implement input switching for devices not using TvInput. |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | protected void routeToTvInputFromPortId(int portId) { |
| 1060 | if (portId < 0 || portId >= Constants.CEC_SWITCH_PORT_MAX) { |
| 1061 | HdmiLogger.debug("Invalid port number for Tv Input switching."); |
| 1062 | return; |
| 1063 | } |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 1064 | // Wake up if the current device if ready to route. |
Amy | ae4ee34 | 2018-10-09 16:55:14 -0700 | [diff] [blame] | 1065 | mService.wakeUp(); |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 1066 | if (portId == Constants.CEC_SWITCH_HOME && mService.isPlaybackDevice()) { |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1067 | switchToHomeTvInput(); |
| 1068 | } else if (portId == Constants.CEC_SWITCH_ARC) { |
| 1069 | switchToTvInput(SystemProperties.get(Constants.PROPERTY_SYSTEM_AUDIO_DEVICE_ARC_PORT)); |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 1070 | setLocalActivePort(portId); |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1071 | return; |
| 1072 | } else { |
Amy | 3403742 | 2018-09-06 13:21:08 -0700 | [diff] [blame] | 1073 | String uri = mTvInputs.get(portId); |
| 1074 | if (uri != null) { |
| 1075 | switchToTvInput(mTvInputs.get(portId)); |
| 1076 | } else { |
| 1077 | HdmiLogger.debug("Port number does not match any Tv Input."); |
| 1078 | return; |
| 1079 | } |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | setLocalActivePort(portId); |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 1083 | setRoutingPort(portId); |
Amy | 225d55a | 2018-09-06 11:03:51 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | // For device to switch to specific TvInput with corresponding URI. |
| 1087 | private void switchToTvInput(String uri) { |
| 1088 | mService.getContext().startActivity(new Intent(Intent.ACTION_VIEW, |
| 1089 | TvContract.buildChannelUriForPassthroughInput(uri)) |
| 1090 | .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)); |
| 1091 | } |
| 1092 | |
| 1093 | // For device using TvInput to switch to Home. |
| 1094 | private void switchToHomeTvInput() { |
| 1095 | Intent activityIntent = new Intent(Intent.ACTION_MAIN) |
| 1096 | .addCategory(Intent.CATEGORY_HOME) |
| 1097 | .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
| 1098 | | Intent.FLAG_ACTIVITY_SINGLE_TOP |
| 1099 | | Intent.FLAG_ACTIVITY_NEW_TASK |
| 1100 | | Intent.FLAG_ACTIVITY_NO_ANIMATION); |
| 1101 | mService.getContext().startActivity(activityIntent); |
| 1102 | } |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1103 | |
| 1104 | @Override |
| 1105 | protected void handleRoutingChangeAndInformation(int physicalAddress, HdmiCecMessage message) { |
Amy | 0063811 | 2018-10-31 17:47:17 -0700 | [diff] [blame] | 1106 | int port = mService.pathToPortId(physicalAddress); |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1107 | // Routing change or information sent from switches under the current device can be ignored. |
| 1108 | if (port > 0) { |
| 1109 | return; |
| 1110 | } |
| 1111 | // When other switches route to some other devices not under the current device, |
| 1112 | // check system audio mode status and do ARC switch if needed. |
| 1113 | if (port < 0 && isSystemAudioActivated()) { |
| 1114 | handleRoutingChangeAndInformationForSystemAudio(); |
| 1115 | return; |
| 1116 | } |
| 1117 | // When other switches route to the current device |
| 1118 | // and the current device is also a switch. |
| 1119 | if (port == 0) { |
| 1120 | handleRoutingChangeAndInformationForSwitch(message); |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | // Handle the system audio(ARC) part of the logic on receiving routing change or information. |
| 1125 | private void handleRoutingChangeAndInformationForSystemAudio() { |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1126 | // TODO(b/115637145): handle system aduio without ARC |
| 1127 | routeToInputFromPortId(Constants.CEC_SWITCH_ARC); |
| 1128 | } |
| 1129 | |
| 1130 | // Handle the routing control part of the logic on receiving routing change or information. |
| 1131 | private void handleRoutingChangeAndInformationForSwitch(HdmiCecMessage message) { |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 1132 | if (getRoutingPort() == Constants.CEC_SWITCH_HOME && mService.isPlaybackDevice()) { |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1133 | routeToInputFromPortId(Constants.CEC_SWITCH_HOME); |
Amy | 123ec40 | 2018-09-25 10:56:31 -0700 | [diff] [blame] | 1134 | mService.setAndBroadcastActiveSourceFromOneDeviceType( |
| 1135 | message.getSource(), mService.getPhysicalAddress()); |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1136 | return; |
| 1137 | } |
| 1138 | |
Amy | 238f64f | 2018-11-30 11:27:56 -0800 | [diff] [blame] | 1139 | int routingInformationPath = mService.portIdToPath(getRoutingPort()); |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1140 | // If current device is already the leaf of the whole HDMI system, will do nothing. |
| 1141 | if (routingInformationPath == mService.getPhysicalAddress()) { |
| 1142 | HdmiLogger.debug("Current device can't assign valid physical address" |
| 1143 | + "to devices under it any more. " |
| 1144 | + "It's physical address is " + routingInformationPath); |
| 1145 | return; |
| 1146 | } |
| 1147 | // Otherwise will switch to the current active port and broadcast routing information. |
| 1148 | mService.sendCecCommand(HdmiCecMessageBuilder.buildRoutingInformation( |
| 1149 | mAddress, routingInformationPath)); |
Amy | 03afe48 | 2018-09-18 16:57:45 -0700 | [diff] [blame] | 1150 | routeToInputFromPortId(getRoutingPort()); |
Amy | 79b54e9 | 2018-09-11 17:02:28 -0700 | [diff] [blame] | 1151 | } |
Amy | 1e4a8cc | 2018-10-15 10:18:14 -0700 | [diff] [blame] | 1152 | |
| 1153 | protected void updateDevicePowerStatus(int logicalAddress, int newPowerStatus) { |
| 1154 | HdmiDeviceInfo info = getCecDeviceInfo(logicalAddress); |
| 1155 | if (info == null) { |
| 1156 | Slog.w(TAG, "Can not update power status of non-existing device:" + logicalAddress); |
| 1157 | return; |
| 1158 | } |
| 1159 | |
| 1160 | if (info.getDevicePowerStatus() == newPowerStatus) { |
| 1161 | return; |
| 1162 | } |
| 1163 | |
| 1164 | HdmiDeviceInfo newInfo = HdmiUtils.cloneHdmiDeviceInfo(info, newPowerStatus); |
| 1165 | // addDeviceInfo replaces old device info with new one if exists. |
| 1166 | addDeviceInfo(newInfo); |
| 1167 | |
| 1168 | invokeDeviceEventListener(newInfo, HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE); |
| 1169 | } |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 1170 | |
| 1171 | @ServiceThreadOnly |
| 1172 | private void launchDeviceDiscovery() { |
| 1173 | assertRunOnServiceThread(); |
Amy | 47fe0b1 | 2018-11-06 14:45:18 -0800 | [diff] [blame] | 1174 | if (hasAction(DeviceDiscoveryAction.class)) { |
| 1175 | Slog.i(TAG, "Device Discovery Action is in progress. Restarting."); |
| 1176 | removeAction(DeviceDiscoveryAction.class); |
| 1177 | } |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 1178 | DeviceDiscoveryAction action = new DeviceDiscoveryAction(this, |
| 1179 | new DeviceDiscoveryCallback() { |
| 1180 | @Override |
| 1181 | public void onDeviceDiscoveryDone(List<HdmiDeviceInfo> deviceInfos) { |
| 1182 | for (HdmiDeviceInfo info : deviceInfos) { |
| 1183 | addCecDevice(info); |
| 1184 | } |
| 1185 | } |
| 1186 | }); |
| 1187 | addAndStartAction(action); |
| 1188 | } |
| 1189 | |
| 1190 | // Clear all device info. |
| 1191 | @ServiceThreadOnly |
| 1192 | private void clearDeviceInfoList() { |
| 1193 | assertRunOnServiceThread(); |
| 1194 | for (HdmiDeviceInfo info : HdmiUtils.sparseArrayToList(mDeviceInfos)) { |
| 1195 | if (info.getPhysicalAddress() == mService.getPhysicalAddress()) { |
| 1196 | continue; |
| 1197 | } |
| 1198 | invokeDeviceEventListener(info, HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE); |
| 1199 | } |
| 1200 | mDeviceInfos.clear(); |
Amy | ec126a5 | 2018-10-30 16:51:14 -0700 | [diff] [blame] | 1201 | updateSafeDeviceInfoList(); |
Amy | c00cd4e | 2018-10-16 20:21:33 -0700 | [diff] [blame] | 1202 | } |
Nick Chalko | b9e48e2 | 2018-10-23 06:59:39 -0700 | [diff] [blame] | 1203 | |
| 1204 | @Override |
| 1205 | protected void dump(IndentingPrintWriter pw) { |
| 1206 | pw.println("HdmiCecLocalDeviceAudioSystem:"); |
| 1207 | pw.increaseIndent(); |
| 1208 | pw.println("mSystemAudioActivated: " + mSystemAudioActivated); |
Amy | 77e672c | 2018-10-31 15:55:40 -0700 | [diff] [blame] | 1209 | pw.println("isRoutingFeatureEnabled " + isRoutingControlFeatureEnabled()); |
Nick Chalko | b9e48e2 | 2018-10-23 06:59:39 -0700 | [diff] [blame] | 1210 | pw.println("mSystemAudioControlFeatureEnabled: " + mSystemAudioControlFeatureEnabled); |
| 1211 | pw.println("mTvSystemAudioModeSupport: " + mTvSystemAudioModeSupport); |
| 1212 | pw.println("mArcEstablished: " + mArcEstablished); |
| 1213 | pw.println("mArcIntentUsed: " + mArcIntentUsed); |
Amy | 2e4b25c | 2018-10-30 18:08:49 -0700 | [diff] [blame] | 1214 | pw.println("mRoutingPort: " + getRoutingPort()); |
| 1215 | pw.println("mLocalActivePort: " + getLocalActivePort()); |
Nick Chalko | b9e48e2 | 2018-10-23 06:59:39 -0700 | [diff] [blame] | 1216 | HdmiUtils.dumpMap(pw, "mTvInputs:", mTvInputs); |
| 1217 | HdmiUtils.dumpSparseArray(pw, "mDeviceInfos:", mDeviceInfos); |
| 1218 | pw.decreaseIndent(); |
| 1219 | super.dump(pw); |
| 1220 | } |
| 1221 | |
Nick Chalko | b89d6ff | 2018-05-25 10:29:01 -0700 | [diff] [blame] | 1222 | } |