keunyoung | d32f4e6 | 2015-09-21 11:33:06 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 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.car.hal; |
| 17 | |
| 18 | import com.android.car.vehiclenetwork.VehicleNetworkConsts; |
| 19 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioFocusRequestType; |
| 20 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioFocusStateType; |
| 21 | import com.android.car.vehiclenetwork.VehicleNetworkConsts.VehicleAudioStreamState; |
| 22 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig; |
| 23 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 24 | |
| 25 | import java.io.PrintWriter; |
| 26 | import java.util.LinkedList; |
| 27 | import java.util.List; |
| 28 | |
| 29 | public class AudioHalService extends HalServiceBase { |
| 30 | |
| 31 | public static final int VEHICLE_AUDIO_FOCUS_REQUEST_GAIN = |
| 32 | VehicleAudioFocusRequestType.VEHICLE_AUDIO_FOCUS_REQUEST_GAIN; |
| 33 | public static final int VEHICLE_AUDIO_FOCUS_REQUEST_GAIN_TRANSIENT = |
| 34 | VehicleAudioFocusRequestType.VEHICLE_AUDIO_FOCUS_REQUEST_GAIN_TRANSIENT; |
| 35 | public static final int VEHICLE_AUDIO_FOCUS_REQUEST_GAIN_TRANSIENT_MAY_DUCK = |
| 36 | VehicleAudioFocusRequestType.VEHICLE_AUDIO_FOCUS_REQUEST_GAIN_TRANSIENT_MAY_DUCK; |
| 37 | public static final int VEHICLE_AUDIO_FOCUS_REQUEST_RELEASE = |
| 38 | VehicleAudioFocusRequestType.VEHICLE_AUDIO_FOCUS_REQUEST_RELEASE; |
| 39 | |
| 40 | public static String audioFocusRequestToString(int request) { |
| 41 | return VehicleAudioFocusRequestType.enumToString(request); |
| 42 | } |
| 43 | |
| 44 | public static final int VEHICLE_AUDIO_FOCUS_STATE_GAIN = |
| 45 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_GAIN; |
| 46 | public static final int VEHICLE_AUDIO_FOCUS_STATE_GAIN_TRANSIENT = |
| 47 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_GAIN_TRANSIENT; |
| 48 | public static final int VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT_CAN_DUCK = |
| 49 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT_CAN_DUCK; |
| 50 | public static final int VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT = |
| 51 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT; |
| 52 | public static final int VEHICLE_AUDIO_FOCUS_STATE_LOSS = |
| 53 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_LOSS; |
| 54 | public static final int VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT_EXLCUSIVE = |
| 55 | VehicleAudioFocusStateType.VEHICLE_AUDIO_FOCUS_STATE_LOSS_TRANSIENT_EXLCUSIVE; |
| 56 | |
| 57 | public static String audioFocusStateToString(int state) { |
| 58 | return VehicleAudioFocusStateType.enumToString(state); |
| 59 | } |
| 60 | |
| 61 | public static final int VEHICLE_AUDIO_STREAM_STATE_STOPPED = |
| 62 | VehicleAudioStreamState.VEHICLE_AUDIO_STREAM_STATE_STOPPED; |
| 63 | public static final int VEHICLE_AUDIO_STREAM_STATE_STARTED = |
| 64 | VehicleAudioStreamState.VEHICLE_AUDIO_STREAM_STATE_STARTED; |
| 65 | |
| 66 | public static String audioStreamStateToString(int state) { |
| 67 | return VehicleAudioStreamState.enumToString(state); |
| 68 | } |
| 69 | |
| 70 | public static final int STREAM_NUM_DEFAULT = 0; |
| 71 | |
| 72 | public interface AudioHalListener { |
| 73 | void onFocusChange(int focusState, int streams); |
| 74 | void onVolumeChange(int volume, int streamNumber); |
| 75 | void onStreamStatusChange(int state, int streamNumber); |
| 76 | } |
| 77 | |
| 78 | private final VehicleHal mVehicleHal; |
| 79 | private AudioHalListener mListener; |
| 80 | private boolean mFocusSupported = false; |
| 81 | private boolean mVolumeSupported = false; |
| 82 | |
| 83 | public AudioHalService(VehicleHal vehicleHal) { |
| 84 | mVehicleHal = vehicleHal; |
| 85 | } |
| 86 | |
| 87 | public synchronized void setListener(AudioHalListener listener) { |
| 88 | mListener = listener; |
| 89 | } |
| 90 | |
| 91 | public synchronized void requestAudioFocusChange(int request, int streams) { |
| 92 | int[] payload = { request, streams }; |
| 93 | mVehicleHal.getVehicleNetwork().setIntVectorProperty( |
| 94 | VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_FOCUS, payload); |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public synchronized void init() { |
| 99 | if (mFocusSupported) { |
| 100 | mVehicleHal.subscribeProperty(this, VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_FOCUS, |
| 101 | 0); |
| 102 | mVehicleHal.subscribeProperty(this, |
| 103 | VehicleNetworkConsts.VEHICLE_PROPERTY_INTERNAL_AUDIO_STREAM_STATE, 0); |
| 104 | } |
| 105 | if (mVolumeSupported) { |
| 106 | mVehicleHal.subscribeProperty(this, VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_VOLUME, |
| 107 | 0); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public synchronized void release() { |
| 113 | if (mFocusSupported) { |
| 114 | mVehicleHal.unsubscribeProperty(this, |
| 115 | VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_FOCUS); |
| 116 | mVehicleHal.unsubscribeProperty(this, |
| 117 | VehicleNetworkConsts.VEHICLE_PROPERTY_INTERNAL_AUDIO_STREAM_STATE); |
| 118 | } |
| 119 | if (mVolumeSupported) { |
| 120 | mVehicleHal.unsubscribeProperty(this, |
| 121 | VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_VOLUME); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public synchronized List<VehiclePropConfig> takeSupportedProperties( |
| 127 | List<VehiclePropConfig> allProperties) { |
| 128 | List<VehiclePropConfig> taken = new LinkedList<VehiclePropConfig>(); |
| 129 | for (VehiclePropConfig p : allProperties) { |
| 130 | switch (p.getProp()) { |
| 131 | case VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_FOCUS: |
| 132 | mFocusSupported = true; |
| 133 | taken.add(p); |
| 134 | break; |
| 135 | case VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_VOLUME: |
| 136 | mVolumeSupported = true; |
| 137 | taken.add(p); |
| 138 | break; |
| 139 | case VehicleNetworkConsts.VEHICLE_PROPERTY_INTERNAL_AUDIO_STREAM_STATE: |
| 140 | taken.add(p); |
| 141 | break; |
| 142 | } |
| 143 | } |
| 144 | return taken; |
| 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public void handleHalEvents(List<VehiclePropValue> values) { |
| 149 | AudioHalListener listener = null; |
| 150 | synchronized (this) { |
| 151 | listener = mListener; |
| 152 | } |
| 153 | if (listener == null) { |
| 154 | //TODO queue this? |
| 155 | return; |
| 156 | } |
| 157 | for (VehiclePropValue v : values) { |
| 158 | switch (v.getProp()) { |
| 159 | case VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_FOCUS: { |
| 160 | int focusState = v.getInt32Values(0); |
| 161 | int streams = v.getInt32Values(1); |
| 162 | listener.onFocusChange(focusState, streams); |
| 163 | } break; |
| 164 | case VehicleNetworkConsts.VEHICLE_PROPERTY_AUDIO_VOLUME: { |
| 165 | int volume = v.getInt32Values(0); |
| 166 | int streamNum = v.getInt32Values(1); |
| 167 | listener.onVolumeChange(volume, streamNum); |
| 168 | } break; |
| 169 | case VehicleNetworkConsts.VEHICLE_PROPERTY_INTERNAL_AUDIO_STREAM_STATE: { |
| 170 | int state = v.getInt32Values(0); |
| 171 | int streamNum = v.getInt32Values(1); |
| 172 | listener.onStreamStatusChange(state, streamNum); |
| 173 | } break; |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public void dump(PrintWriter writer) { |
| 180 | writer.println("*Sensor HAL*"); |
| 181 | writer.println(" focus supported:" + mFocusSupported + |
| 182 | " volume supported:" + mVolumeSupported); |
| 183 | } |
| 184 | |
| 185 | } |