Home | History | Annotate | Download | only in hdmi
      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 
     18 import android.hardware.tv.cec.V1_0.SendMessageResult;
     19 
     20 import com.android.internal.annotations.VisibleForTesting;
     21 
     22 /**
     23  * Feature action that handles System Audio Mode initiated by AVR devices.
     24  */
     25 public class SystemAudioInitiationActionFromAvr extends HdmiCecFeatureAction {
     26 
     27     // State that waits for <Active Source> once send <Request Active Source>.
     28     private static final int STATE_WAITING_FOR_ACTIVE_SOURCE = 1;
     29     // State that waits for TV supporting Audio System Mode or not
     30     // once received <Active Source>
     31     private static final int STATE_WAITING_FOR_TV_SUPPORT = 2;
     32     @VisibleForTesting
     33     static final int MAX_RETRY_COUNT = 5;
     34 
     35     private int mSendRequestActiveSourceRetryCount = 0;
     36     private int mSendSetSystemAudioModeRetryCount = 0;
     37 
     38     SystemAudioInitiationActionFromAvr(HdmiCecLocalDevice source) {
     39         super(source);
     40     }
     41 
     42     @Override
     43     boolean start() {
     44         if (audioSystem().getActiveSource().physicalAddress == Constants.INVALID_PHYSICAL_ADDRESS) {
     45             mState = STATE_WAITING_FOR_ACTIVE_SOURCE;
     46             addTimer(mState, HdmiConfig.TIMEOUT_MS);
     47             sendRequestActiveSource();
     48         } else {
     49             queryTvSystemAudioModeSupport();
     50         }
     51         return true;
     52     }
     53 
     54     @Override
     55     boolean processCommand(HdmiCecMessage cmd) {
     56         switch (cmd.getOpcode()) {
     57             case Constants.MESSAGE_ACTIVE_SOURCE:
     58                 // received <Active Source>
     59                 if (mState != STATE_WAITING_FOR_ACTIVE_SOURCE) {
     60                     return false;
     61                 }
     62                 mActionTimer.clearTimerMessage();
     63                 // Broadcast message is also handled by other device types
     64                 audioSystem().handleActiveSource(cmd);
     65                 mState = STATE_WAITING_FOR_TV_SUPPORT;
     66                 queryTvSystemAudioModeSupport();
     67                 return true;
     68         }
     69         return false;
     70     }
     71 
     72     @Override
     73     void handleTimerEvent(int state) {
     74         if (mState != state) {
     75             return;
     76         }
     77 
     78         switch (mState) {
     79             case STATE_WAITING_FOR_ACTIVE_SOURCE:
     80                 handleActiveSourceTimeout();
     81                 break;
     82         }
     83     }
     84 
     85     protected void sendRequestActiveSource() {
     86         sendCommand(HdmiCecMessageBuilder.buildRequestActiveSource(getSourceAddress()),
     87                 result -> {
     88                     if (result != SendMessageResult.SUCCESS) {
     89                         if (mSendRequestActiveSourceRetryCount < MAX_RETRY_COUNT) {
     90                             mSendRequestActiveSourceRetryCount++;
     91                             sendRequestActiveSource();
     92                         } else {
     93                             audioSystem().checkSupportAndSetSystemAudioMode(false);
     94                             finish();
     95                         }
     96                     }
     97                 });
     98     }
     99 
    100     protected void sendSetSystemAudioMode(boolean on, int dest) {
    101         sendCommand(HdmiCecMessageBuilder.buildSetSystemAudioMode(getSourceAddress(),
    102                 dest, on), result -> {
    103                     if (result != SendMessageResult.SUCCESS) {
    104                         if (mSendSetSystemAudioModeRetryCount < MAX_RETRY_COUNT) {
    105                             mSendSetSystemAudioModeRetryCount++;
    106                             sendSetSystemAudioMode(on, dest);
    107                         } else {
    108                             audioSystem().checkSupportAndSetSystemAudioMode(false);
    109                             finish();
    110                         }
    111                     }
    112                 });
    113     }
    114 
    115     private void handleActiveSourceTimeout() {
    116         HdmiLogger.debug("Cannot get active source.");
    117         // If not able to find Active Source and the current device has playbcak functionality,
    118         // claim Active Source and start to query TV system audio mode support.
    119         if (audioSystem().mService.isPlaybackDevice()) {
    120             audioSystem().mService.setAndBroadcastActiveSourceFromOneDeviceType(
    121                     Constants.ADDR_BROADCAST, getSourcePath());
    122             mState = STATE_WAITING_FOR_TV_SUPPORT;
    123             queryTvSystemAudioModeSupport();
    124         } else {
    125             audioSystem().checkSupportAndSetSystemAudioMode(false);
    126         }
    127         finish();
    128     }
    129 
    130     private void queryTvSystemAudioModeSupport() {
    131         audioSystem().queryTvSystemAudioModeSupport(
    132                 supported -> {
    133                     if (supported) {
    134                         if (audioSystem().checkSupportAndSetSystemAudioMode(true)) {
    135                             sendSetSystemAudioMode(true, Constants.ADDR_BROADCAST);
    136                         }
    137                         finish();
    138                     } else {
    139                         audioSystem().checkSupportAndSetSystemAudioMode(false);
    140                         finish();
    141                     }
    142                 });
    143     }
    144 
    145     private void switchToRelevantInputForDeviceAt(int physicalAddress) {
    146         // TODO(shubang): implement this method
    147     }
    148 }
    149