2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.hdmi;
19 import android.annotation.Nullable;
20 import android.hardware.hdmi.HdmiDeviceInfo;
21 import android.hardware.hdmi.HdmiControlManager;
22 import android.hardware.hdmi.IHdmiControlCallback;
23 import android.os.RemoteException;
24 import android.util.Slog;
26 import java.util.List;
29 * Base feature action class for SystemAudioActionFromTv and SystemAudioActionFromAvr.
31 abstract class SystemAudioAction extends HdmiCecFeatureAction {
32 private static final String TAG = "SystemAudioAction";
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;
38 // State in which waits for <SetSystemAudioMode>.
39 private static final int STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE = 2;
41 private static final int MAX_SEND_RETRY_COUNT = 2;
43 private static final int ON_TIMEOUT_MS = 5000;
44 private static final int OFF_TIMEOUT_MS = HdmiConfig.TIMEOUT_MS;
46 // Logical address of AV Receiver.
47 protected final int mAvrLogicalAddress;
49 // The target audio status of the action, whether to enable the system audio mode or not.
50 protected boolean mTargetAudioStatus;
52 @Nullable private final IHdmiControlCallback mCallback;
54 private int mSendRetryCount = 0;
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
65 SystemAudioAction(HdmiCecLocalDevice source, int avrAddress, boolean targetStatus,
66 IHdmiControlCallback callback) {
68 HdmiUtils.verifyAddressType(avrAddress, HdmiDeviceInfo.DEVICE_AUDIO_SYSTEM);
69 mAvrLogicalAddress = avrAddress;
70 mTargetAudioStatus = targetStatus;
75 protected void sendSystemAudioModeRequest() {
76 List<RoutingControlAction> routingActions = getActions(RoutingControlAction.class);
77 if (!routingActions.isEmpty()) {
78 mState = STATE_CHECK_ROUTING_IN_PRGRESS;
79 // Should have only one Routing Control Action
80 RoutingControlAction routingAction = routingActions.get(0);
81 routingAction.addOnFinishedCallback(this, new Runnable() {
84 sendSystemAudioModeRequestInternal();
89 sendSystemAudioModeRequestInternal();
92 private void sendSystemAudioModeRequestInternal() {
93 int avrPhysicalAddress = tv().getAvrDeviceInfo().getPhysicalAddress();
94 HdmiCecMessage command = HdmiCecMessageBuilder.buildSystemAudioModeRequest(
96 mAvrLogicalAddress, avrPhysicalAddress, mTargetAudioStatus);
97 sendCommand(command, new HdmiControlService.SendMessageCallback() {
99 public void onSendCompleted(int error) {
100 if (error != Constants.SEND_RESULT_SUCCESS) {
101 HdmiLogger.debug("Failed to send <System Audio Mode Request>:" + error);
102 setSystemAudioMode(false);
103 finishWithCallback(HdmiControlManager.RESULT_COMMUNICATION_FAILED);
107 mState = STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE;
108 addTimer(mState, mTargetAudioStatus ? ON_TIMEOUT_MS : OFF_TIMEOUT_MS);
111 private void handleSendSystemAudioModeRequestTimeout() {
112 if (!mTargetAudioStatus // Don't retry for Off case.
113 || mSendRetryCount++ >= MAX_SEND_RETRY_COUNT) {
114 HdmiLogger.debug("[T]:wait for <Set System Audio Mode>.");
115 setSystemAudioMode(false);
116 finishWithCallback(HdmiControlManager.RESULT_TIMEOUT);
119 sendSystemAudioModeRequest();
122 protected void setSystemAudioMode(boolean mode) {
123 tv().setSystemAudioMode(mode, true);
127 final boolean processCommand(HdmiCecMessage cmd) {
128 if (cmd.getSource() != mAvrLogicalAddress) {
132 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE:
133 if (cmd.getOpcode() == Constants.MESSAGE_FEATURE_ABORT
134 && (cmd.getParams()[0] & 0xFF)
135 == Constants.MESSAGE_SYSTEM_AUDIO_MODE_REQUEST) {
136 HdmiLogger.debug("Failed to start system audio mode request.");
137 setSystemAudioMode(false);
138 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION);
141 if (cmd.getOpcode() != Constants.MESSAGE_SET_SYSTEM_AUDIO_MODE
142 || !HdmiUtils.checkCommandSource(cmd, mAvrLogicalAddress, TAG)) {
145 boolean receivedStatus = HdmiUtils.parseCommandParamSystemAudioStatus(cmd);
146 if (receivedStatus == mTargetAudioStatus) {
147 setSystemAudioMode(receivedStatus);
148 startAudioStatusAction();
151 HdmiLogger.debug("Unexpected system audio mode request:" + receivedStatus);
152 // Unexpected response, consider the request is newly initiated by AVR.
153 // To return 'false' will initiate new SystemAudioActionFromAvr by the control
155 finishWithCallback(HdmiControlManager.RESULT_EXCEPTION);
163 protected void startAudioStatusAction() {
164 addAndStartAction(new SystemAudioStatusAction(tv(), mAvrLogicalAddress, mCallback));
168 protected void removeSystemAudioActionInProgress() {
169 removeActionExcept(SystemAudioActionFromTv.class, this);
170 removeActionExcept(SystemAudioActionFromAvr.class, this);
174 final void handleTimerEvent(int state) {
175 if (mState != state) {
179 case STATE_WAIT_FOR_SET_SYSTEM_AUDIO_MODE:
180 handleSendSystemAudioModeRequestTimeout();
185 // TODO: if IHdmiControlCallback is general to other FeatureAction,
186 // move it into FeatureAction.
187 protected void finishWithCallback(int returnCode) {
188 if (mCallback != null) {
190 mCallback.onComplete(returnCode);
191 } catch (RemoteException e) {
192 Slog.e(TAG, "Failed to invoke callback.", e);