SystemAudioAction.java revision 339227da7cf025ce4ae0c85ddc52643d63972321
1/* 2 * Copyright (C) 2014 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 17package com.android.server.hdmi; 18 19import android.annotation.Nullable; 20import android.hardware.hdmi.HdmiDeviceInfo; 21import android.hardware.hdmi.HdmiControlManager; 22import android.hardware.hdmi.IHdmiControlCallback; 23import android.os.RemoteException; 24import android.util.Slog; 25 26import java.util.List; 27 28/** 29 * Base feature action class for SystemAudioActionFromTv and SystemAudioActionFromAvr. 30 */ 31abstract class SystemAudioAction extends HdmiCecFeatureAction { 32 private static final String TAG = "SystemAudioAction"; 33 34 // Transient state to differentiate with STATE_NONE where the on-finished callback 35 // will not be called. 36 private static final int STATE_CHECK_ROUTING_IN_PRGRESS = 1; 37 38 // State in which waits for <SetSystemAudioMode>. 39 private static final int STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE = 2; 40 41 private static final int MAX_SEND_RETRY_COUNT = 2; 42 43 private static final int ON_TIMEOUT_MS = 5000; 44 private static final int OFF_TIMEOUT_MS = HdmiConfig.TIMEOUT_MS; 45 46 // Logical address of AV Receiver. 47 protected final int mAvrLogicalAddress; 48 49 // The target audio status of the action, whether to enable the system audio mode or not. 50 protected boolean mTargetAudioStatus; 51 52 @Nullable private final IHdmiControlCallback mCallback; 53 54 private int mSendRetryCount = 0; 55 56 /** 57 * Constructor 58 * 59 * @param source {@link HdmiCecLocalDevice} instance 60 * @param avrAddress logical address of AVR device 61 * @param targetStatus Whether to enable the system audio mode or not 62 * @param callback callback interface to be notified when it's done 63 * @throw IllegalArugmentException if device type of sourceAddress and avrAddress is invalid 64 */ 65 SystemAudioAction(HdmiCecLocalDevice source, int avrAddress, boolean targetStatus, 66 IHdmiControlCallback callback) { 67 super(source); 68 HdmiUtils.verifyAddressType(avrAddress, HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM); 69 mAvrLogicalAddress = avrAddress; 70 mTargetAudioStatus = targetStatus; 71 mCallback = callback; 72 } 73 74 // Seq #27 75 protected void sendSystemAudioModeRequest() { 76 mState = STATE_CHECK_ROUTING_IN_PRGRESS; 77 List<RoutingControlAction> routingActions = getActions(RoutingControlAction.class); 78 if (!routingActions.isEmpty()) { 79 // Should have only one Routing Control Action 80 RoutingControlAction routingAction = routingActions.get(0); 81 routingAction.addOnFinishedCallback(this, new Runnable() { 82 @Override 83 public void run() { 84 sendSystemAudioModeRequestInternal(); 85 } 86 }); 87 return; 88 } 89 sendSystemAudioModeRequestInternal(); 90 } 91 92 private void sendSystemAudioModeRequestInternal() { 93 int avrPhysicalAddress = tv().getAvrDeviceInfo().getPhysicalAddress(); 94 HdmiCecMessage command = HdmiCecMessageBuilder.buildSystemAudioModeRequest( 95 getSourceAddress(), 96 mAvrLogicalAddress, avrPhysicalAddress, mTargetAudioStatus); 97 sendCommand(command, new HdmiControlService.SendMessageCallback() { 98 @Override 99 public void onSendCompleted(int error) { 100 if (error == Constants.SEND_RESULT_SUCCESS) { 101 mState = STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE; 102 addTimer(mState, mTargetAudioStatus ? ON_TIMEOUT_MS : OFF_TIMEOUT_MS); 103 } else { 104 setSystemAudioMode(false); 105 finishWithCallback(HdmiControlManager.RESULT_COMMUNICATION_FAILED); 106 } 107 } 108 }); 109 } 110 111 private void handleSendSystemAudioModeRequestTimeout() { 112 if (!mTargetAudioStatus // Don't retry for Off case. 113 || mSendRetryCount++ >= MAX_SEND_RETRY_COUNT) { 114 setSystemAudioMode(false); 115 finishWithCallback(HdmiControlManager.RESULT_TIMEOUT); 116 return; 117 } 118 sendSystemAudioModeRequest(); 119 } 120 121 protected void setSystemAudioMode(boolean mode) { 122 tv().setSystemAudioMode(mode, true); 123 } 124 125 @Override 126 final boolean processCommand(HdmiCecMessage cmd) { 127 switch (mState) { 128 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE: 129 if (cmd.getOpcode() == Constants.MESSAGE_FEATURE_ABORT 130 && (cmd.getParams()[0] & 0xFF) 131 == Constants.MESSAGE_SYSTEM_AUDIO_MODE_REQUEST) { 132 setSystemAudioMode(false); 133 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION); 134 return true; 135 } 136 if (cmd.getOpcode() != Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE 137 || !HdmiUtils.checkCommandSource(cmd, mAvrLogicalAddress, TAG)) { 138 return false; 139 } 140 boolean receivedStatus = HdmiUtils.parseCommandParamSystemAudioStatus(cmd); 141 if (receivedStatus == mTargetAudioStatus) { 142 setSystemAudioMode(receivedStatus); 143 startAudioStatusAction(); 144 return true; 145 } else { 146 // Unexpected response, consider the request is newly initiated by AVR. 147 // To return 'false' will initiate new SystemAudioActionFromAvr by the control 148 // service. 149 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION); 150 return false; 151 } 152 default: 153 return false; 154 } 155 } 156 157 protected void startAudioStatusAction() { 158 addAndStartAction(new SystemAudioStatusAction(tv(), mAvrLogicalAddress, mCallback)); 159 finish(); 160 } 161 162 protected void removeSystemAudioActionInProgress() { 163 removeActionExcept(SystemAudioActionFromTv.class, this); 164 removeActionExcept(SystemAudioActionFromAvr.class, this); 165 } 166 167 @Override 168 final void handleTimerEvent(int state) { 169 if (mState != state) { 170 return; 171 } 172 switch (mState) { 173 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE: 174 handleSendSystemAudioModeRequestTimeout(); 175 return; 176 } 177 } 178 179 // TODO: if IHdmiControlCallback is general to other FeatureAction, 180 // move it into FeatureAction. 181 protected void finishWithCallback(int returnCode) { 182 if (mCallback != null) { 183 try { 184 mCallback.onComplete(returnCode); 185 } catch (RemoteException e) { 186 Slog.e(TAG, "Failed to invoke callback.", e); 187 } 188 } 189 finish(); 190 } 191} 192