Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 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 | |
| 17 | package com.android.server.tv; |
| 18 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 19 | import static android.media.tv.TvInputManager.INPUT_STATE_CONNECTED; |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 20 | import static android.media.tv.TvInputManager.INPUT_STATE_CONNECTED_STANDBY; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 21 | import static android.media.tv.TvInputManager.INPUT_STATE_DISCONNECTED; |
| 22 | |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 23 | import android.content.BroadcastReceiver; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 24 | import android.content.Context; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 27 | import android.content.pm.PackageManager; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 28 | import android.hardware.hdmi.HdmiControlManager; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 29 | import android.hardware.hdmi.HdmiDeviceInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 30 | import android.hardware.hdmi.HdmiHotplugEvent; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 31 | import android.hardware.hdmi.IHdmiControlService; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 32 | import android.hardware.hdmi.IHdmiDeviceEventListener; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 33 | import android.hardware.hdmi.IHdmiHotplugEventListener; |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 34 | import android.hardware.hdmi.IHdmiSystemAudioModeChangeListener; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 35 | import android.media.AudioDevicePort; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 36 | import android.media.AudioFormat; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 37 | import android.media.AudioGain; |
| 38 | import android.media.AudioGainConfig; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 39 | import android.media.AudioManager; |
| 40 | import android.media.AudioPatch; |
| 41 | import android.media.AudioPort; |
| 42 | import android.media.AudioPortConfig; |
Wally Yau | ad36f647 | 2015-10-06 11:33:19 -0700 | [diff] [blame] | 43 | import android.media.AudioSystem; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 44 | import android.media.tv.ITvInputHardware; |
| 45 | import android.media.tv.ITvInputHardwareCallback; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 46 | import android.media.tv.TvInputHardwareInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 47 | import android.media.tv.TvInputInfo; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 48 | import android.media.tv.TvStreamConfig; |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 49 | import android.os.Binder; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 50 | import android.os.Handler; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 51 | import android.os.IBinder; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 52 | import android.os.Message; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 53 | import android.os.RemoteException; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 54 | import android.os.ServiceManager; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 55 | import android.util.ArrayMap; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 56 | import android.util.Slog; |
| 57 | import android.util.SparseArray; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 58 | import android.util.SparseBooleanArray; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 59 | import android.view.KeyEvent; |
| 60 | import android.view.Surface; |
| 61 | |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 62 | import com.android.internal.util.IndentingPrintWriter; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 63 | import com.android.server.SystemService; |
| 64 | |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 65 | import java.io.FileDescriptor; |
| 66 | import java.io.PrintWriter; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 67 | import java.util.ArrayList; |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 68 | import java.util.Arrays; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 69 | import java.util.Collections; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 70 | import java.util.Iterator; |
| 71 | import java.util.LinkedList; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 72 | import java.util.List; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 73 | import java.util.Map; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * A helper class for TvInputManagerService to handle TV input hardware. |
| 77 | * |
| 78 | * This class does a basic connection management and forwarding calls to TvInputHal which eventually |
| 79 | * calls to tv_input HAL module. |
| 80 | * |
| 81 | * @hide |
| 82 | */ |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 83 | class TvInputHardwareManager implements TvInputHal.Callback { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 84 | private static final String TAG = TvInputHardwareManager.class.getSimpleName(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 85 | |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 86 | private final Context mContext; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 87 | private final Listener mListener; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 88 | private final TvInputHal mHal = new TvInputHal(this); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 89 | private final SparseArray<Connection> mConnections = new SparseArray<>(); |
| 90 | private final List<TvInputHardwareInfo> mHardwareList = new ArrayList<>(); |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 91 | private final List<HdmiDeviceInfo> mHdmiDeviceList = new LinkedList<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 92 | /* A map from a device ID to the matching TV input ID. */ |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 93 | private final SparseArray<String> mHardwareInputIdMap = new SparseArray<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 94 | /* A map from a HDMI logical address to the matching TV input ID. */ |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 95 | private final SparseArray<String> mHdmiInputIdMap = new SparseArray<>(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 96 | private final Map<String, TvInputInfo> mInputMap = new ArrayMap<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 97 | |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 98 | private final AudioManager mAudioManager; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 99 | private final IHdmiHotplugEventListener mHdmiHotplugEventListener = |
| 100 | new HdmiHotplugEventListener(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 101 | private final IHdmiDeviceEventListener mHdmiDeviceEventListener = new HdmiDeviceEventListener(); |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 102 | private final IHdmiSystemAudioModeChangeListener mHdmiSystemAudioModeChangeListener = |
| 103 | new HdmiSystemAudioModeChangeListener(); |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 104 | private final BroadcastReceiver mVolumeReceiver = new BroadcastReceiver() { |
| 105 | @Override |
| 106 | public void onReceive(Context context, Intent intent) { |
| 107 | handleVolumeChange(context, intent); |
| 108 | } |
| 109 | }; |
| 110 | private int mCurrentIndex = 0; |
| 111 | private int mCurrentMaxIndex = 0; |
Jinsuk Kim | d38bf47 | 2014-08-11 11:29:18 +0900 | [diff] [blame] | 112 | |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 113 | private final SparseBooleanArray mHdmiStateMap = new SparseBooleanArray(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 114 | private final List<Message> mPendingHdmiDeviceEvents = new LinkedList<>(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 115 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 116 | // Calls to mListener should happen here. |
| 117 | private final Handler mHandler = new ListenerHandler(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 118 | |
| 119 | private final Object mLock = new Object(); |
| 120 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 121 | public TvInputHardwareManager(Context context, Listener listener) { |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 122 | mContext = context; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 123 | mListener = listener; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 124 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 125 | mHal.init(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | public void onBootPhase(int phase) { |
| 129 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Jae Seo | 38b3257 | 2015-06-05 14:43:11 -0700 | [diff] [blame] | 130 | IHdmiControlService hdmiControlService = IHdmiControlService.Stub.asInterface( |
| 131 | ServiceManager.getService(Context.HDMI_CONTROL_SERVICE)); |
| 132 | if (hdmiControlService != null) { |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 133 | try { |
Jae Seo | 38b3257 | 2015-06-05 14:43:11 -0700 | [diff] [blame] | 134 | hdmiControlService.addHotplugEventListener(mHdmiHotplugEventListener); |
| 135 | hdmiControlService.addDeviceEventListener(mHdmiDeviceEventListener); |
| 136 | hdmiControlService.addSystemAudioModeChangeListener( |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 137 | mHdmiSystemAudioModeChangeListener); |
Jae Seo | 38b3257 | 2015-06-05 14:43:11 -0700 | [diff] [blame] | 138 | mHdmiDeviceList.addAll(hdmiControlService.getInputDevices()); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 139 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 140 | Slog.w(TAG, "Error registering listeners to HdmiControlService:", e); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 141 | } |
Jinsuk Kim | 08f1ab0 | 2014-10-13 10:38:16 +0900 | [diff] [blame] | 142 | } else { |
| 143 | Slog.w(TAG, "HdmiControlService is not available"); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 144 | } |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 145 | final IntentFilter filter = new IntentFilter(); |
| 146 | filter.addAction(AudioManager.VOLUME_CHANGED_ACTION); |
| 147 | filter.addAction(AudioManager.STREAM_MUTE_CHANGED_ACTION); |
| 148 | mContext.registerReceiver(mVolumeReceiver, filter); |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 149 | updateVolume(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 150 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | @Override |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 154 | public void onDeviceAvailable(TvInputHardwareInfo info, TvStreamConfig[] configs) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 155 | synchronized (mLock) { |
| 156 | Connection connection = new Connection(info); |
| 157 | connection.updateConfigsLocked(configs); |
| 158 | mConnections.put(info.getDeviceId(), connection); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 159 | buildHardwareListLocked(); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 160 | mHandler.obtainMessage( |
| 161 | ListenerHandler.HARDWARE_DEVICE_ADDED, 0, 0, info).sendToTarget(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 162 | if (info.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
| 163 | processPendingHdmiDeviceEventsLocked(); |
| 164 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 168 | private void buildHardwareListLocked() { |
| 169 | mHardwareList.clear(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 170 | for (int i = 0; i < mConnections.size(); ++i) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 171 | mHardwareList.add(mConnections.valueAt(i).getHardwareInfoLocked()); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onDeviceUnavailable(int deviceId) { |
| 177 | synchronized (mLock) { |
| 178 | Connection connection = mConnections.get(deviceId); |
| 179 | if (connection == null) { |
| 180 | Slog.e(TAG, "onDeviceUnavailable: Cannot find a connection with " + deviceId); |
| 181 | return; |
| 182 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 183 | connection.resetLocked(null, null, null, null, null); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 184 | mConnections.remove(deviceId); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 185 | buildHardwareListLocked(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 186 | TvInputHardwareInfo info = connection.getHardwareInfoLocked(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 187 | if (info.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 188 | // Remove HDMI devices linked with this hardware. |
| 189 | for (Iterator<HdmiDeviceInfo> it = mHdmiDeviceList.iterator(); it.hasNext();) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 190 | HdmiDeviceInfo deviceInfo = it.next(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 191 | if (deviceInfo.getPortId() == info.getHdmiPortId()) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 192 | mHandler.obtainMessage(ListenerHandler.HDMI_DEVICE_REMOVED, 0, 0, |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 193 | deviceInfo).sendToTarget(); |
| 194 | it.remove(); |
| 195 | } |
| 196 | } |
| 197 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 198 | mHandler.obtainMessage( |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 199 | ListenerHandler.HARDWARE_DEVICE_REMOVED, 0, 0, info).sendToTarget(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onStreamConfigurationChanged(int deviceId, TvStreamConfig[] configs) { |
| 205 | synchronized (mLock) { |
| 206 | Connection connection = mConnections.get(deviceId); |
| 207 | if (connection == null) { |
| 208 | Slog.e(TAG, "StreamConfigurationChanged: Cannot find a connection with " |
| 209 | + deviceId); |
| 210 | return; |
| 211 | } |
| 212 | connection.updateConfigsLocked(configs); |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 213 | String inputId = mHardwareInputIdMap.get(deviceId); |
| 214 | if (inputId != null) { |
| 215 | mHandler.obtainMessage(ListenerHandler.STATE_CHANGED, |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 216 | obtainStateFromConfigs(configs), 0, inputId).sendToTarget(); |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 217 | } |
Dongwon Kang | 017bb85 | 2014-12-26 14:53:14 +0900 | [diff] [blame] | 218 | ITvInputHardwareCallback callback = connection.getCallbackLocked(); |
| 219 | if (callback != null) { |
| 220 | try { |
| 221 | callback.onStreamConfigChanged(configs); |
| 222 | } catch (RemoteException e) { |
| 223 | Slog.e(TAG, "error in onStreamConfigurationChanged", e); |
| 224 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 229 | @Override |
| 230 | public void onFirstFrameCaptured(int deviceId, int streamId) { |
| 231 | synchronized (mLock) { |
| 232 | Connection connection = mConnections.get(deviceId); |
| 233 | if (connection == null) { |
| 234 | Slog.e(TAG, "FirstFrameCaptured: Cannot find a connection with " |
| 235 | + deviceId); |
| 236 | return; |
| 237 | } |
| 238 | Runnable runnable = connection.getOnFirstFrameCapturedLocked(); |
| 239 | if (runnable != null) { |
| 240 | runnable.run(); |
| 241 | connection.setOnFirstFrameCapturedLocked(null); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 246 | public List<TvInputHardwareInfo> getHardwareList() { |
| 247 | synchronized (mLock) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 248 | return Collections.unmodifiableList(mHardwareList); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 249 | } |
| 250 | } |
| 251 | |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 252 | public List<HdmiDeviceInfo> getHdmiDeviceList() { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 253 | synchronized (mLock) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 254 | return Collections.unmodifiableList(mHdmiDeviceList); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 255 | } |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 256 | } |
| 257 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 258 | private boolean checkUidChangedLocked( |
| 259 | Connection connection, int callingUid, int resolvedUserId) { |
| 260 | Integer connectionCallingUid = connection.getCallingUidLocked(); |
| 261 | Integer connectionResolvedUserId = connection.getResolvedUserIdLocked(); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 262 | return connectionCallingUid == null || connectionResolvedUserId == null |
| 263 | || connectionCallingUid != callingUid || connectionResolvedUserId != resolvedUserId; |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 264 | } |
| 265 | |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 266 | private int obtainStateFromConfigs(TvStreamConfig[] configs) { |
| 267 | for (TvStreamConfig config : configs) { |
| 268 | if ((config.getFlags() & TvStreamConfig.FLAG_MASK_SIGNAL_DETECTION) != 0) { |
| 269 | return INPUT_STATE_CONNECTED; |
| 270 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 271 | } |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 272 | return (configs.length > 0) ? INPUT_STATE_CONNECTED_STANDBY : INPUT_STATE_DISCONNECTED; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 273 | } |
| 274 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 275 | public void addHardwareInput(int deviceId, TvInputInfo info) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 276 | synchronized (mLock) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 277 | String oldInputId = mHardwareInputIdMap.get(deviceId); |
| 278 | if (oldInputId != null) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 279 | Slog.w(TAG, "Trying to override previous registration: old = " |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 280 | + mInputMap.get(oldInputId) + ":" + deviceId + ", new = " |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 281 | + info + ":" + deviceId); |
| 282 | } |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 283 | mHardwareInputIdMap.put(deviceId, info.getId()); |
| 284 | mInputMap.put(info.getId(), info); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 285 | |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 286 | // Process pending state changes |
| 287 | |
| 288 | // For logical HDMI devices, they have information from HDMI CEC signals. |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 289 | for (int i = 0; i < mHdmiStateMap.size(); ++i) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 290 | TvInputHardwareInfo hardwareInfo = |
| 291 | findHardwareInfoForHdmiPortLocked(mHdmiStateMap.keyAt(i)); |
| 292 | if (hardwareInfo == null) { |
| 293 | continue; |
| 294 | } |
| 295 | String inputId = mHardwareInputIdMap.get(hardwareInfo.getDeviceId()); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 296 | if (inputId != null && inputId.equals(info.getId())) { |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 297 | // No HDMI hotplug does not necessarily mean disconnected, as old devices may |
| 298 | // not report hotplug state correctly. Using INPUT_STATE_CONNECTED_STANDBY to |
| 299 | // denote unknown state. |
| 300 | int state = mHdmiStateMap.valueAt(i) |
| 301 | ? INPUT_STATE_CONNECTED |
| 302 | : INPUT_STATE_CONNECTED_STANDBY; |
| 303 | mHandler.obtainMessage( |
| 304 | ListenerHandler.STATE_CHANGED, state, 0, inputId).sendToTarget(); |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 305 | return; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 306 | } |
| 307 | } |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 308 | // For the rest of the devices, we can tell by the number of available streams. |
| 309 | Connection connection = mConnections.get(deviceId); |
| 310 | if (connection != null) { |
| 311 | mHandler.obtainMessage(ListenerHandler.STATE_CHANGED, |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 312 | obtainStateFromConfigs(connection.getConfigsLocked()), 0, |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 313 | info.getId()).sendToTarget(); |
Wonsik Kim | 9a10365 | 2014-10-21 17:46:31 +0900 | [diff] [blame] | 314 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 318 | private static <T> int indexOfEqualValue(SparseArray<T> map, T value) { |
| 319 | for (int i = 0; i < map.size(); ++i) { |
| 320 | if (map.valueAt(i).equals(value)) { |
| 321 | return i; |
| 322 | } |
| 323 | } |
| 324 | return -1; |
| 325 | } |
| 326 | |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 327 | private static boolean intArrayContains(int[] array, int value) { |
| 328 | for (int element : array) { |
| 329 | if (element == value) return true; |
| 330 | } |
| 331 | return false; |
| 332 | } |
| 333 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 334 | public void addHdmiInput(int id, TvInputInfo info) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 335 | if (info.getType() != TvInputInfo.TYPE_HDMI) { |
| 336 | throw new IllegalArgumentException("info (" + info + ") has non-HDMI type."); |
| 337 | } |
| 338 | synchronized (mLock) { |
| 339 | String parentId = info.getParentId(); |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 340 | int parentIndex = indexOfEqualValue(mHardwareInputIdMap, parentId); |
| 341 | if (parentIndex < 0) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 342 | throw new IllegalArgumentException("info (" + info + ") has invalid parentId."); |
| 343 | } |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 344 | String oldInputId = mHdmiInputIdMap.get(id); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 345 | if (oldInputId != null) { |
| 346 | Slog.w(TAG, "Trying to override previous registration: old = " |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 347 | + mInputMap.get(oldInputId) + ":" + id + ", new = " |
| 348 | + info + ":" + id); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 349 | } |
Jinsuk Kim | 8960d1b | 2014-08-13 10:48:30 +0900 | [diff] [blame] | 350 | mHdmiInputIdMap.put(id, info.getId()); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 351 | mInputMap.put(info.getId(), info); |
| 352 | } |
| 353 | } |
| 354 | |
Jae Seo | 1abbbcd | 2016-01-28 22:20:41 -0800 | [diff] [blame] | 355 | public void removeHardwareInput(String inputId) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 356 | synchronized (mLock) { |
| 357 | mInputMap.remove(inputId); |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 358 | int hardwareIndex = indexOfEqualValue(mHardwareInputIdMap, inputId); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 359 | if (hardwareIndex >= 0) { |
| 360 | mHardwareInputIdMap.removeAt(hardwareIndex); |
| 361 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 362 | int deviceIndex = indexOfEqualValue(mHdmiInputIdMap, inputId); |
| 363 | if (deviceIndex >= 0) { |
| 364 | mHdmiInputIdMap.removeAt(deviceIndex); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 369 | /** |
| 370 | * Create a TvInputHardware object with a specific deviceId. One service at a time can access |
| 371 | * the object, and if more than one process attempts to create hardware with the same deviceId, |
| 372 | * the latest service will get the object and all the other hardware are released. The |
| 373 | * release is notified via ITvInputHardwareCallback.onReleased(). |
| 374 | */ |
| 375 | public ITvInputHardware acquireHardware(int deviceId, ITvInputHardwareCallback callback, |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 376 | TvInputInfo info, int callingUid, int resolvedUserId) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 377 | if (callback == null) { |
| 378 | throw new NullPointerException(); |
| 379 | } |
| 380 | synchronized (mLock) { |
| 381 | Connection connection = mConnections.get(deviceId); |
| 382 | if (connection == null) { |
| 383 | Slog.e(TAG, "Invalid deviceId : " + deviceId); |
| 384 | return null; |
| 385 | } |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 386 | if (checkUidChangedLocked(connection, callingUid, resolvedUserId)) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 387 | TvInputHardwareImpl hardware = |
| 388 | new TvInputHardwareImpl(connection.getHardwareInfoLocked()); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 389 | try { |
| 390 | callback.asBinder().linkToDeath(connection, 0); |
| 391 | } catch (RemoteException e) { |
| 392 | hardware.release(); |
| 393 | return null; |
| 394 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 395 | connection.resetLocked(hardware, callback, info, callingUid, resolvedUserId); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 396 | } |
| 397 | return connection.getHardwareLocked(); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
| 402 | * Release the specified hardware. |
| 403 | */ |
| 404 | public void releaseHardware(int deviceId, ITvInputHardware hardware, int callingUid, |
| 405 | int resolvedUserId) { |
| 406 | synchronized (mLock) { |
| 407 | Connection connection = mConnections.get(deviceId); |
| 408 | if (connection == null) { |
| 409 | Slog.e(TAG, "Invalid deviceId : " + deviceId); |
| 410 | return; |
| 411 | } |
| 412 | if (connection.getHardwareLocked() != hardware |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 413 | || checkUidChangedLocked(connection, callingUid, resolvedUserId)) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 414 | return; |
| 415 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 416 | connection.resetLocked(null, null, null, null, null); |
| 417 | } |
| 418 | } |
| 419 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 420 | private TvInputHardwareInfo findHardwareInfoForHdmiPortLocked(int port) { |
| 421 | for (TvInputHardwareInfo hardwareInfo : mHardwareList) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 422 | if (hardwareInfo.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI |
| 423 | && hardwareInfo.getHdmiPortId() == port) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 424 | return hardwareInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | return null; |
| 428 | } |
| 429 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 430 | private int findDeviceIdForInputIdLocked(String inputId) { |
| 431 | for (int i = 0; i < mConnections.size(); ++i) { |
| 432 | Connection connection = mConnections.get(i); |
| 433 | if (connection.getInfoLocked().getId().equals(inputId)) { |
| 434 | return i; |
| 435 | } |
| 436 | } |
| 437 | return -1; |
| 438 | } |
| 439 | |
| 440 | /** |
| 441 | * Get the list of TvStreamConfig which is buffered mode. |
| 442 | */ |
| 443 | public List<TvStreamConfig> getAvailableTvStreamConfigList(String inputId, int callingUid, |
| 444 | int resolvedUserId) { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 445 | List<TvStreamConfig> configsList = new ArrayList<>(); |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 446 | synchronized (mLock) { |
| 447 | int deviceId = findDeviceIdForInputIdLocked(inputId); |
| 448 | if (deviceId < 0) { |
| 449 | Slog.e(TAG, "Invalid inputId : " + inputId); |
| 450 | return configsList; |
| 451 | } |
| 452 | Connection connection = mConnections.get(deviceId); |
| 453 | for (TvStreamConfig config : connection.getConfigsLocked()) { |
| 454 | if (config.getType() == TvStreamConfig.STREAM_TYPE_BUFFER_PRODUCER) { |
| 455 | configsList.add(config); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | return configsList; |
| 460 | } |
| 461 | |
| 462 | /** |
| 463 | * Take a snapshot of the given TV input into the provided Surface. |
| 464 | */ |
| 465 | public boolean captureFrame(String inputId, Surface surface, final TvStreamConfig config, |
| 466 | int callingUid, int resolvedUserId) { |
| 467 | synchronized (mLock) { |
| 468 | int deviceId = findDeviceIdForInputIdLocked(inputId); |
| 469 | if (deviceId < 0) { |
| 470 | Slog.e(TAG, "Invalid inputId : " + inputId); |
| 471 | return false; |
| 472 | } |
| 473 | Connection connection = mConnections.get(deviceId); |
| 474 | final TvInputHardwareImpl hardwareImpl = connection.getHardwareImplLocked(); |
| 475 | if (hardwareImpl != null) { |
| 476 | // Stop previous capture. |
| 477 | Runnable runnable = connection.getOnFirstFrameCapturedLocked(); |
| 478 | if (runnable != null) { |
| 479 | runnable.run(); |
| 480 | connection.setOnFirstFrameCapturedLocked(null); |
| 481 | } |
| 482 | |
| 483 | boolean result = hardwareImpl.startCapture(surface, config); |
| 484 | if (result) { |
| 485 | connection.setOnFirstFrameCapturedLocked(new Runnable() { |
| 486 | @Override |
| 487 | public void run() { |
| 488 | hardwareImpl.stopCapture(config); |
| 489 | } |
| 490 | }); |
| 491 | } |
| 492 | return result; |
| 493 | } |
| 494 | } |
| 495 | return false; |
| 496 | } |
| 497 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 498 | private void processPendingHdmiDeviceEventsLocked() { |
| 499 | for (Iterator<Message> it = mPendingHdmiDeviceEvents.iterator(); it.hasNext(); ) { |
| 500 | Message msg = it.next(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 501 | HdmiDeviceInfo deviceInfo = (HdmiDeviceInfo) msg.obj; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 502 | TvInputHardwareInfo hardwareInfo = |
| 503 | findHardwareInfoForHdmiPortLocked(deviceInfo.getPortId()); |
| 504 | if (hardwareInfo != null) { |
| 505 | msg.sendToTarget(); |
| 506 | it.remove(); |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 511 | private void updateVolume() { |
| 512 | mCurrentMaxIndex = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); |
| 513 | mCurrentIndex = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC); |
| 514 | } |
| 515 | |
| 516 | private void handleVolumeChange(Context context, Intent intent) { |
| 517 | String action = intent.getAction(); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 518 | switch (action) { |
| 519 | case AudioManager.VOLUME_CHANGED_ACTION: { |
| 520 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 521 | if (streamType != AudioManager.STREAM_MUSIC) { |
| 522 | return; |
| 523 | } |
| 524 | int index = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_VALUE, 0); |
| 525 | if (index == mCurrentIndex) { |
| 526 | return; |
| 527 | } |
| 528 | mCurrentIndex = index; |
| 529 | break; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 530 | } |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 531 | case AudioManager.STREAM_MUTE_CHANGED_ACTION: { |
| 532 | int streamType = intent.getIntExtra(AudioManager.EXTRA_VOLUME_STREAM_TYPE, -1); |
| 533 | if (streamType != AudioManager.STREAM_MUSIC) { |
| 534 | return; |
| 535 | } |
| 536 | // volume index will be updated at onMediaStreamVolumeChanged() through |
| 537 | // updateVolume(). |
| 538 | break; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 539 | } |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 540 | default: |
| 541 | Slog.w(TAG, "Unrecognized intent: " + intent); |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 542 | return; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 543 | } |
| 544 | synchronized (mLock) { |
| 545 | for (int i = 0; i < mConnections.size(); ++i) { |
| 546 | TvInputHardwareImpl hardwareImpl = mConnections.valueAt(i).getHardwareImplLocked(); |
| 547 | if (hardwareImpl != null) { |
| 548 | hardwareImpl.onMediaStreamVolumeChanged(); |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
| 554 | private float getMediaStreamVolume() { |
John Spurlock | ee5ad72 | 2015-03-03 16:17:21 -0500 | [diff] [blame] | 555 | return (float) mCurrentIndex / (float) mCurrentMaxIndex; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 556 | } |
| 557 | |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 558 | public void dump(FileDescriptor fd, final PrintWriter writer, String[] args) { |
| 559 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
| 560 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 561 | != PackageManager.PERMISSION_GRANTED) { |
| 562 | pw.println("Permission Denial: can't dump TvInputHardwareManager from pid=" |
| 563 | + Binder.getCallingPid() + ", uid=" + Binder.getCallingUid()); |
| 564 | return; |
| 565 | } |
| 566 | |
| 567 | synchronized (mLock) { |
| 568 | pw.println("TvInputHardwareManager Info:"); |
| 569 | pw.increaseIndent(); |
| 570 | pw.println("mConnections: deviceId -> Connection"); |
| 571 | pw.increaseIndent(); |
| 572 | for (int i = 0; i < mConnections.size(); i++) { |
| 573 | int deviceId = mConnections.keyAt(i); |
| 574 | Connection mConnection = mConnections.valueAt(i); |
| 575 | pw.println(deviceId + ": " + mConnection); |
| 576 | |
| 577 | } |
| 578 | pw.decreaseIndent(); |
| 579 | |
| 580 | pw.println("mHardwareList:"); |
| 581 | pw.increaseIndent(); |
| 582 | for (TvInputHardwareInfo tvInputHardwareInfo : mHardwareList) { |
| 583 | pw.println(tvInputHardwareInfo); |
| 584 | } |
| 585 | pw.decreaseIndent(); |
| 586 | |
| 587 | pw.println("mHdmiDeviceList:"); |
| 588 | pw.increaseIndent(); |
| 589 | for (HdmiDeviceInfo hdmiDeviceInfo : mHdmiDeviceList) { |
| 590 | pw.println(hdmiDeviceInfo); |
| 591 | } |
| 592 | pw.decreaseIndent(); |
| 593 | |
| 594 | pw.println("mHardwareInputIdMap: deviceId -> inputId"); |
| 595 | pw.increaseIndent(); |
| 596 | for (int i = 0 ; i < mHardwareInputIdMap.size(); i++) { |
| 597 | int deviceId = mHardwareInputIdMap.keyAt(i); |
| 598 | String inputId = mHardwareInputIdMap.valueAt(i); |
| 599 | pw.println(deviceId + ": " + inputId); |
| 600 | } |
| 601 | pw.decreaseIndent(); |
| 602 | |
| 603 | pw.println("mHdmiInputIdMap: id -> inputId"); |
| 604 | pw.increaseIndent(); |
| 605 | for (int i = 0; i < mHdmiInputIdMap.size(); i++) { |
| 606 | int id = mHdmiInputIdMap.keyAt(i); |
| 607 | String inputId = mHdmiInputIdMap.valueAt(i); |
| 608 | pw.println(id + ": " + inputId); |
| 609 | } |
| 610 | pw.decreaseIndent(); |
| 611 | |
| 612 | pw.println("mInputMap: inputId -> inputInfo"); |
| 613 | pw.increaseIndent(); |
| 614 | for(Map.Entry<String, TvInputInfo> entry : mInputMap.entrySet()) { |
| 615 | pw.println(entry.getKey() + ": " + entry.getValue()); |
| 616 | } |
| 617 | pw.decreaseIndent(); |
| 618 | pw.decreaseIndent(); |
| 619 | } |
| 620 | } |
| 621 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 622 | private class Connection implements IBinder.DeathRecipient { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 623 | private final TvInputHardwareInfo mHardwareInfo; |
| 624 | private TvInputInfo mInfo; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 625 | private TvInputHardwareImpl mHardware = null; |
| 626 | private ITvInputHardwareCallback mCallback; |
| 627 | private TvStreamConfig[] mConfigs = null; |
| 628 | private Integer mCallingUid = null; |
| 629 | private Integer mResolvedUserId = null; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 630 | private Runnable mOnFirstFrameCaptured; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 631 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 632 | public Connection(TvInputHardwareInfo hardwareInfo) { |
| 633 | mHardwareInfo = hardwareInfo; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | // *Locked methods assume TvInputHardwareManager.mLock is held. |
| 637 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 638 | public void resetLocked(TvInputHardwareImpl hardware, ITvInputHardwareCallback callback, |
| 639 | TvInputInfo info, Integer callingUid, Integer resolvedUserId) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 640 | if (mHardware != null) { |
| 641 | try { |
| 642 | mCallback.onReleased(); |
| 643 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 644 | Slog.e(TAG, "error in Connection::resetLocked", e); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 645 | } |
| 646 | mHardware.release(); |
| 647 | } |
| 648 | mHardware = hardware; |
| 649 | mCallback = callback; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 650 | mInfo = info; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 651 | mCallingUid = callingUid; |
| 652 | mResolvedUserId = resolvedUserId; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 653 | mOnFirstFrameCaptured = null; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 654 | |
| 655 | if (mHardware != null && mCallback != null) { |
| 656 | try { |
| 657 | mCallback.onStreamConfigChanged(getConfigsLocked()); |
| 658 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 659 | Slog.e(TAG, "error in Connection::resetLocked", e); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 660 | } |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | public void updateConfigsLocked(TvStreamConfig[] configs) { |
| 665 | mConfigs = configs; |
| 666 | } |
| 667 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 668 | public TvInputHardwareInfo getHardwareInfoLocked() { |
| 669 | return mHardwareInfo; |
| 670 | } |
| 671 | |
| 672 | public TvInputInfo getInfoLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 673 | return mInfo; |
| 674 | } |
| 675 | |
| 676 | public ITvInputHardware getHardwareLocked() { |
| 677 | return mHardware; |
| 678 | } |
| 679 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 680 | public TvInputHardwareImpl getHardwareImplLocked() { |
| 681 | return mHardware; |
| 682 | } |
| 683 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 684 | public ITvInputHardwareCallback getCallbackLocked() { |
| 685 | return mCallback; |
| 686 | } |
| 687 | |
| 688 | public TvStreamConfig[] getConfigsLocked() { |
| 689 | return mConfigs; |
| 690 | } |
| 691 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 692 | public Integer getCallingUidLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 693 | return mCallingUid; |
| 694 | } |
| 695 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 696 | public Integer getResolvedUserIdLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 697 | return mResolvedUserId; |
| 698 | } |
| 699 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 700 | public void setOnFirstFrameCapturedLocked(Runnable runnable) { |
| 701 | mOnFirstFrameCaptured = runnable; |
| 702 | } |
| 703 | |
| 704 | public Runnable getOnFirstFrameCapturedLocked() { |
| 705 | return mOnFirstFrameCaptured; |
| 706 | } |
| 707 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 708 | @Override |
| 709 | public void binderDied() { |
| 710 | synchronized (mLock) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 711 | resetLocked(null, null, null, null, null); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
yangren | 8d718e1 | 2016-02-15 17:02:49 +0900 | [diff] [blame^] | 714 | |
| 715 | public String toString() { |
| 716 | return "Connection{" |
| 717 | + " mHardwareInfo: " + mHardwareInfo |
| 718 | + ", mInfo: " + mInfo |
| 719 | + ", mCallback: " + mCallback |
| 720 | + ", mConfigs: " + Arrays.toString(mConfigs) |
| 721 | + ", mCallingUid: " + mCallingUid |
| 722 | + ", mResolvedUserId: " + mResolvedUserId |
| 723 | + " }"; |
| 724 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | private class TvInputHardwareImpl extends ITvInputHardware.Stub { |
| 728 | private final TvInputHardwareInfo mInfo; |
| 729 | private boolean mReleased = false; |
| 730 | private final Object mImplLock = new Object(); |
| 731 | |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 732 | private final AudioManager.OnAudioPortUpdateListener mAudioListener = |
| 733 | new AudioManager.OnAudioPortUpdateListener() { |
| 734 | @Override |
| 735 | public void onAudioPortListUpdate(AudioPort[] portList) { |
| 736 | synchronized (mImplLock) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 737 | updateAudioConfigLocked(); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 738 | } |
| 739 | } |
| 740 | |
| 741 | @Override |
| 742 | public void onAudioPatchListUpdate(AudioPatch[] patchList) { |
| 743 | // No-op |
| 744 | } |
| 745 | |
| 746 | @Override |
| 747 | public void onServiceDied() { |
| 748 | synchronized (mImplLock) { |
| 749 | mAudioSource = null; |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 750 | mAudioSink.clear(); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 751 | mAudioPatch = null; |
| 752 | } |
| 753 | } |
| 754 | }; |
| 755 | private int mOverrideAudioType = AudioManager.DEVICE_NONE; |
| 756 | private String mOverrideAudioAddress = ""; |
| 757 | private AudioDevicePort mAudioSource; |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 758 | private List<AudioDevicePort> mAudioSink = new ArrayList<>(); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 759 | private AudioPatch mAudioPatch = null; |
Wonsik Kim | 485e7e1 | 2015-01-26 17:28:55 +0900 | [diff] [blame] | 760 | // Set to an invalid value for a volume, so that current volume can be applied at the |
| 761 | // first call to updateAudioConfigLocked(). |
| 762 | private float mCommittedVolume = -1f; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 763 | private float mSourceVolume = 0.0f; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 764 | |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 765 | private TvStreamConfig mActiveConfig = null; |
| 766 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 767 | private int mDesiredSamplingRate = 0; |
| 768 | private int mDesiredChannelMask = AudioFormat.CHANNEL_OUT_DEFAULT; |
| 769 | private int mDesiredFormat = AudioFormat.ENCODING_DEFAULT; |
| 770 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 771 | public TvInputHardwareImpl(TvInputHardwareInfo info) { |
| 772 | mInfo = info; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 773 | mAudioManager.registerAudioPortUpdateListener(mAudioListener); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 774 | if (mInfo.getAudioType() != AudioManager.DEVICE_NONE) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 775 | mAudioSource = findAudioDevicePort(mInfo.getAudioType(), mInfo.getAudioAddress()); |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 776 | findAudioSinkFromAudioPolicy(mAudioSink); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 777 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 778 | } |
| 779 | |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 780 | private void findAudioSinkFromAudioPolicy(List<AudioDevicePort> sinks) { |
| 781 | sinks.clear(); |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 782 | ArrayList<AudioDevicePort> devicePorts = new ArrayList<>(); |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 783 | if (mAudioManager.listAudioDevicePorts(devicePorts) != AudioManager.SUCCESS) { |
| 784 | return; |
| 785 | } |
| 786 | int sinkDevice = mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC); |
Paul McLean | ceb47aa | 2015-02-19 15:09:53 -0800 | [diff] [blame] | 787 | for (AudioDevicePort port : devicePorts) { |
Wally Yau | ad36f647 | 2015-10-06 11:33:19 -0700 | [diff] [blame] | 788 | if ((port.type() & sinkDevice) != 0 && |
| 789 | (port.type() & AudioSystem.DEVICE_BIT_IN) == 0) { |
Paul McLean | ceb47aa | 2015-02-19 15:09:53 -0800 | [diff] [blame] | 790 | sinks.add(port); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 791 | } |
| 792 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | private AudioDevicePort findAudioDevicePort(int type, String address) { |
| 796 | if (type == AudioManager.DEVICE_NONE) { |
| 797 | return null; |
| 798 | } |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 799 | ArrayList<AudioDevicePort> devicePorts = new ArrayList<>(); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 800 | if (mAudioManager.listAudioDevicePorts(devicePorts) != AudioManager.SUCCESS) { |
| 801 | return null; |
| 802 | } |
Paul McLean | ceb47aa | 2015-02-19 15:09:53 -0800 | [diff] [blame] | 803 | for (AudioDevicePort port : devicePorts) { |
| 804 | if (port.type() == type && port.address().equals(address)) { |
| 805 | return port; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | return null; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | public void release() { |
| 812 | synchronized (mImplLock) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 813 | mAudioManager.unregisterAudioPortUpdateListener(mAudioListener); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 814 | if (mAudioPatch != null) { |
| 815 | mAudioManager.releaseAudioPatch(mAudioPatch); |
| 816 | mAudioPatch = null; |
| 817 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 818 | mReleased = true; |
| 819 | } |
| 820 | } |
| 821 | |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 822 | // A TvInputHardwareImpl object holds only one active session. Therefore, if a client |
| 823 | // attempts to call setSurface with different TvStreamConfig objects, the last call will |
| 824 | // prevail. |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 825 | @Override |
| 826 | public boolean setSurface(Surface surface, TvStreamConfig config) |
| 827 | throws RemoteException { |
| 828 | synchronized (mImplLock) { |
| 829 | if (mReleased) { |
| 830 | throw new IllegalStateException("Device already released."); |
| 831 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 832 | |
Wonsik Kim | 102670f | 2014-11-20 14:38:02 +0900 | [diff] [blame] | 833 | int result = TvInputHal.SUCCESS; |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 834 | if (surface == null) { |
Wonsik Kim | b683e6e | 2014-11-17 16:31:39 +0900 | [diff] [blame] | 835 | // The value of config is ignored when surface == null. |
| 836 | if (mActiveConfig != null) { |
| 837 | result = mHal.removeStream(mInfo.getDeviceId(), mActiveConfig); |
| 838 | mActiveConfig = null; |
| 839 | } else { |
| 840 | // We already have no active stream. |
| 841 | return true; |
| 842 | } |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 843 | } else { |
Wonsik Kim | b683e6e | 2014-11-17 16:31:39 +0900 | [diff] [blame] | 844 | // It's impossible to set a non-null surface with a null config. |
| 845 | if (config == null) { |
| 846 | return false; |
| 847 | } |
| 848 | // Remove stream only if we have an existing active configuration. |
| 849 | if (mActiveConfig != null && !config.equals(mActiveConfig)) { |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 850 | result = mHal.removeStream(mInfo.getDeviceId(), mActiveConfig); |
| 851 | if (result != TvInputHal.SUCCESS) { |
| 852 | mActiveConfig = null; |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 853 | } |
| 854 | } |
Wonsik Kim | b683e6e | 2014-11-17 16:31:39 +0900 | [diff] [blame] | 855 | // Proceed only if all previous operations succeeded. |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 856 | if (result == TvInputHal.SUCCESS) { |
Wonsik Kim | b683e6e | 2014-11-17 16:31:39 +0900 | [diff] [blame] | 857 | result = mHal.addOrUpdateStream(mInfo.getDeviceId(), surface, config); |
| 858 | if (result == TvInputHal.SUCCESS) { |
| 859 | mActiveConfig = config; |
| 860 | } |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 861 | } |
| 862 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 863 | updateAudioConfigLocked(); |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 864 | return result == TvInputHal.SUCCESS; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 865 | } |
| 866 | } |
| 867 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 868 | /** |
| 869 | * Update audio configuration (source, sink, patch) all up to current state. |
| 870 | */ |
| 871 | private void updateAudioConfigLocked() { |
| 872 | boolean sinkUpdated = updateAudioSinkLocked(); |
| 873 | boolean sourceUpdated = updateAudioSourceLocked(); |
| 874 | // We can't do updated = updateAudioSinkLocked() || updateAudioSourceLocked() here |
| 875 | // because Java won't evaluate the latter if the former is true. |
| 876 | |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 877 | if (mAudioSource == null || mAudioSink.isEmpty() || mActiveConfig == null) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 878 | if (mAudioPatch != null) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 879 | mAudioManager.releaseAudioPatch(mAudioPatch); |
| 880 | mAudioPatch = null; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 881 | } |
| 882 | return; |
| 883 | } |
| 884 | |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 885 | updateVolume(); |
| 886 | float volume = mSourceVolume * getMediaStreamVolume(); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 887 | AudioGainConfig sourceGainConfig = null; |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 888 | if (mAudioSource.gains().length > 0 && volume != mCommittedVolume) { |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 889 | AudioGain sourceGain = null; |
| 890 | for (AudioGain gain : mAudioSource.gains()) { |
| 891 | if ((gain.mode() & AudioGain.MODE_JOINT) != 0) { |
| 892 | sourceGain = gain; |
| 893 | break; |
| 894 | } |
| 895 | } |
| 896 | // NOTE: we only change the source gain in MODE_JOINT here. |
| 897 | if (sourceGain != null) { |
| 898 | int steps = (sourceGain.maxValue() - sourceGain.minValue()) |
| 899 | / sourceGain.stepValue(); |
| 900 | int gainValue = sourceGain.minValue(); |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 901 | if (volume < 1.0f) { |
| 902 | gainValue += sourceGain.stepValue() * (int) (volume * steps + 0.5); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 903 | } else { |
| 904 | gainValue = sourceGain.maxValue(); |
| 905 | } |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 906 | // size of gain values is 1 in MODE_JOINT |
| 907 | int[] gainValues = new int[] { gainValue }; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 908 | sourceGainConfig = sourceGain.buildConfig(AudioGain.MODE_JOINT, |
| 909 | sourceGain.channelMask(), gainValues, 0); |
| 910 | } else { |
| 911 | Slog.w(TAG, "No audio source gain with MODE_JOINT support exists."); |
| 912 | } |
| 913 | } |
| 914 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 915 | AudioPortConfig sourceConfig = mAudioSource.activeConfig(); |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 916 | List<AudioPortConfig> sinkConfigs = new ArrayList<>(); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 917 | AudioPatch[] audioPatchArray = new AudioPatch[] { mAudioPatch }; |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 918 | boolean shouldRecreateAudioPatch = sourceUpdated || sinkUpdated; |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 919 | |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 920 | for (AudioDevicePort audioSink : mAudioSink) { |
| 921 | AudioPortConfig sinkConfig = audioSink.activeConfig(); |
| 922 | int sinkSamplingRate = mDesiredSamplingRate; |
| 923 | int sinkChannelMask = mDesiredChannelMask; |
| 924 | int sinkFormat = mDesiredFormat; |
| 925 | // If sinkConfig != null and values are set to default, |
| 926 | // fill in the sinkConfig values. |
| 927 | if (sinkConfig != null) { |
| 928 | if (sinkSamplingRate == 0) { |
| 929 | sinkSamplingRate = sinkConfig.samplingRate(); |
| 930 | } |
| 931 | if (sinkChannelMask == AudioFormat.CHANNEL_OUT_DEFAULT) { |
| 932 | sinkChannelMask = sinkConfig.channelMask(); |
| 933 | } |
| 934 | if (sinkFormat == AudioFormat.ENCODING_DEFAULT) { |
| 935 | sinkChannelMask = sinkConfig.format(); |
| 936 | } |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 937 | } |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 938 | |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 939 | if (sinkConfig == null |
| 940 | || sinkConfig.samplingRate() != sinkSamplingRate |
| 941 | || sinkConfig.channelMask() != sinkChannelMask |
| 942 | || sinkConfig.format() != sinkFormat) { |
| 943 | // Check for compatibility and reset to default if necessary. |
| 944 | if (!intArrayContains(audioSink.samplingRates(), sinkSamplingRate) |
| 945 | && audioSink.samplingRates().length > 0) { |
| 946 | sinkSamplingRate = audioSink.samplingRates()[0]; |
| 947 | } |
| 948 | if (!intArrayContains(audioSink.channelMasks(), sinkChannelMask)) { |
| 949 | sinkChannelMask = AudioFormat.CHANNEL_OUT_DEFAULT; |
| 950 | } |
| 951 | if (!intArrayContains(audioSink.formats(), sinkFormat)) { |
| 952 | sinkFormat = AudioFormat.ENCODING_DEFAULT; |
| 953 | } |
| 954 | sinkConfig = audioSink.buildConfig(sinkSamplingRate, sinkChannelMask, |
| 955 | sinkFormat, null); |
| 956 | shouldRecreateAudioPatch = true; |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 957 | } |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 958 | sinkConfigs.add(sinkConfig); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 959 | } |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 960 | // sinkConfigs.size() == mAudioSink.size(), and mAudioSink is guaranteed to be |
| 961 | // non-empty at the beginning of this method. |
| 962 | AudioPortConfig sinkConfig = sinkConfigs.get(0); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 963 | if (sourceConfig == null || sourceGainConfig != null) { |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 964 | int sourceSamplingRate = 0; |
| 965 | if (intArrayContains(mAudioSource.samplingRates(), sinkConfig.samplingRate())) { |
| 966 | sourceSamplingRate = sinkConfig.samplingRate(); |
| 967 | } else if (mAudioSource.samplingRates().length > 0) { |
| 968 | // Use any sampling rate and hope audio patch can handle resampling... |
| 969 | sourceSamplingRate = mAudioSource.samplingRates()[0]; |
| 970 | } |
| 971 | int sourceChannelMask = AudioFormat.CHANNEL_IN_DEFAULT; |
| 972 | for (int inChannelMask : mAudioSource.channelMasks()) { |
| 973 | if (AudioFormat.channelCountFromOutChannelMask(sinkConfig.channelMask()) |
| 974 | == AudioFormat.channelCountFromInChannelMask(inChannelMask)) { |
| 975 | sourceChannelMask = inChannelMask; |
| 976 | break; |
| 977 | } |
| 978 | } |
| 979 | int sourceFormat = AudioFormat.ENCODING_DEFAULT; |
| 980 | if (intArrayContains(mAudioSource.formats(), sinkConfig.format())) { |
| 981 | sourceFormat = sinkConfig.format(); |
| 982 | } |
| 983 | sourceConfig = mAudioSource.buildConfig(sourceSamplingRate, sourceChannelMask, |
| 984 | sourceFormat, sourceGainConfig); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 985 | shouldRecreateAudioPatch = true; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 986 | } |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 987 | if (shouldRecreateAudioPatch) { |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 988 | mCommittedVolume = volume; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 989 | mAudioManager.createAudioPatch( |
| 990 | audioPatchArray, |
| 991 | new AudioPortConfig[] { sourceConfig }, |
Jae Seo | 93ff14b | 2015-06-21 14:08:54 -0700 | [diff] [blame] | 992 | sinkConfigs.toArray(new AudioPortConfig[sinkConfigs.size()])); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 993 | mAudioPatch = audioPatchArray[0]; |
Wonsik Kim | 71dfa96 | 2014-11-15 14:18:49 +0900 | [diff] [blame] | 994 | if (sourceGainConfig != null) { |
| 995 | mAudioManager.setAudioPortGain(mAudioSource, sourceGainConfig); |
| 996 | } |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 997 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 998 | } |
| 999 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1000 | @Override |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 1001 | public void setStreamVolume(float volume) throws RemoteException { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1002 | synchronized (mImplLock) { |
| 1003 | if (mReleased) { |
| 1004 | throw new IllegalStateException("Device already released."); |
| 1005 | } |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 1006 | mSourceVolume = volume; |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1007 | updateAudioConfigLocked(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1008 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | @Override |
| 1012 | public boolean dispatchKeyEventToHdmi(KeyEvent event) throws RemoteException { |
| 1013 | synchronized (mImplLock) { |
| 1014 | if (mReleased) { |
| 1015 | throw new IllegalStateException("Device already released."); |
| 1016 | } |
| 1017 | } |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 1018 | if (mInfo.getType() != TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1019 | return false; |
| 1020 | } |
| 1021 | // TODO(hdmi): mHdmiClient.sendKeyEvent(event); |
| 1022 | return false; |
| 1023 | } |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1024 | |
| 1025 | private boolean startCapture(Surface surface, TvStreamConfig config) { |
| 1026 | synchronized (mImplLock) { |
| 1027 | if (mReleased) { |
| 1028 | return false; |
| 1029 | } |
| 1030 | if (surface == null || config == null) { |
| 1031 | return false; |
| 1032 | } |
| 1033 | if (config.getType() != TvStreamConfig.STREAM_TYPE_BUFFER_PRODUCER) { |
| 1034 | return false; |
| 1035 | } |
| 1036 | |
Wonsik Kim | 8f24a8b | 2014-10-22 16:27:39 +0900 | [diff] [blame] | 1037 | int result = mHal.addOrUpdateStream(mInfo.getDeviceId(), surface, config); |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 1038 | return result == TvInputHal.SUCCESS; |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | private boolean stopCapture(TvStreamConfig config) { |
| 1043 | synchronized (mImplLock) { |
| 1044 | if (mReleased) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | if (config == null) { |
| 1048 | return false; |
| 1049 | } |
| 1050 | |
| 1051 | int result = mHal.removeStream(mInfo.getDeviceId(), config); |
| 1052 | return result == TvInputHal.SUCCESS; |
| 1053 | } |
| 1054 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 1055 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1056 | private boolean updateAudioSourceLocked() { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1057 | if (mInfo.getAudioType() == AudioManager.DEVICE_NONE) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1058 | return false; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1059 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1060 | AudioDevicePort previousSource = mAudioSource; |
| 1061 | mAudioSource = findAudioDevicePort(mInfo.getAudioType(), mInfo.getAudioAddress()); |
| 1062 | return mAudioSource == null ? (previousSource != null) |
| 1063 | : !mAudioSource.equals(previousSource); |
| 1064 | } |
| 1065 | |
| 1066 | private boolean updateAudioSinkLocked() { |
| 1067 | if (mInfo.getAudioType() == AudioManager.DEVICE_NONE) { |
| 1068 | return false; |
| 1069 | } |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 1070 | List<AudioDevicePort> previousSink = mAudioSink; |
| 1071 | mAudioSink = new ArrayList<>(); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1072 | if (mOverrideAudioType == AudioManager.DEVICE_NONE) { |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 1073 | findAudioSinkFromAudioPolicy(mAudioSink); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1074 | } else { |
| 1075 | AudioDevicePort audioSink = |
| 1076 | findAudioDevicePort(mOverrideAudioType, mOverrideAudioAddress); |
| 1077 | if (audioSink != null) { |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 1078 | mAudioSink.add(audioSink); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1079 | } |
| 1080 | } |
Wonsik Kim | b2b85f9 | 2015-01-19 16:07:48 +0900 | [diff] [blame] | 1081 | |
| 1082 | // Returns true if mAudioSink and previousSink differs. |
| 1083 | if (mAudioSink.size() != previousSink.size()) { |
| 1084 | return true; |
| 1085 | } |
| 1086 | previousSink.removeAll(mAudioSink); |
| 1087 | return !previousSink.isEmpty(); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1088 | } |
| 1089 | |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 1090 | private void handleAudioSinkUpdated() { |
| 1091 | synchronized (mImplLock) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1092 | updateAudioConfigLocked(); |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 1093 | } |
| 1094 | } |
| 1095 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 1096 | @Override |
| 1097 | public void overrideAudioSink(int audioType, String audioAddress, int samplingRate, |
| 1098 | int channelMask, int format) { |
| 1099 | synchronized (mImplLock) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1100 | mOverrideAudioType = audioType; |
| 1101 | mOverrideAudioAddress = audioAddress; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 1102 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 1103 | mDesiredSamplingRate = samplingRate; |
| 1104 | mDesiredChannelMask = channelMask; |
| 1105 | mDesiredFormat = format; |
| 1106 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame] | 1107 | updateAudioConfigLocked(); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 1108 | } |
| 1109 | } |
Wonsik Kim | 49e5593 | 2014-12-04 17:22:09 +0900 | [diff] [blame] | 1110 | |
| 1111 | public void onMediaStreamVolumeChanged() { |
| 1112 | synchronized (mImplLock) { |
| 1113 | updateAudioConfigLocked(); |
| 1114 | } |
| 1115 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1116 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1117 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1118 | interface Listener { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 1119 | void onStateChanged(String inputId, int state); |
| 1120 | void onHardwareDeviceAdded(TvInputHardwareInfo info); |
| 1121 | void onHardwareDeviceRemoved(TvInputHardwareInfo info); |
| 1122 | void onHdmiDeviceAdded(HdmiDeviceInfo device); |
| 1123 | void onHdmiDeviceRemoved(HdmiDeviceInfo device); |
| 1124 | void onHdmiDeviceUpdated(String inputId, HdmiDeviceInfo device); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1125 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1126 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1127 | private class ListenerHandler extends Handler { |
| 1128 | private static final int STATE_CHANGED = 1; |
| 1129 | private static final int HARDWARE_DEVICE_ADDED = 2; |
| 1130 | private static final int HARDWARE_DEVICE_REMOVED = 3; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 1131 | private static final int HDMI_DEVICE_ADDED = 4; |
| 1132 | private static final int HDMI_DEVICE_REMOVED = 5; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1133 | private static final int HDMI_DEVICE_UPDATED = 6; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1134 | |
| 1135 | @Override |
| 1136 | public final void handleMessage(Message msg) { |
| 1137 | switch (msg.what) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1138 | case STATE_CHANGED: { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1139 | String inputId = (String) msg.obj; |
| 1140 | int state = msg.arg1; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1141 | mListener.onStateChanged(inputId, state); |
| 1142 | break; |
| 1143 | } |
| 1144 | case HARDWARE_DEVICE_ADDED: { |
| 1145 | TvInputHardwareInfo info = (TvInputHardwareInfo) msg.obj; |
| 1146 | mListener.onHardwareDeviceAdded(info); |
| 1147 | break; |
| 1148 | } |
| 1149 | case HARDWARE_DEVICE_REMOVED: { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 1150 | TvInputHardwareInfo info = (TvInputHardwareInfo) msg.obj; |
| 1151 | mListener.onHardwareDeviceRemoved(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1152 | break; |
| 1153 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 1154 | case HDMI_DEVICE_ADDED: { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1155 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 1156 | mListener.onHdmiDeviceAdded(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1157 | break; |
| 1158 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 1159 | case HDMI_DEVICE_REMOVED: { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 1160 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 1161 | mListener.onHdmiDeviceRemoved(info); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1162 | break; |
| 1163 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1164 | case HDMI_DEVICE_UPDATED: { |
Wonsik Kim | 184a6d6 | 2014-10-08 13:05:56 +0900 | [diff] [blame] | 1165 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 1166 | String inputId; |
Wonsik Kim | 184a6d6 | 2014-10-08 13:05:56 +0900 | [diff] [blame] | 1167 | synchronized (mLock) { |
| 1168 | inputId = mHdmiInputIdMap.get(info.getId()); |
| 1169 | } |
| 1170 | if (inputId != null) { |
| 1171 | mListener.onHdmiDeviceUpdated(inputId, info); |
| 1172 | } else { |
| 1173 | Slog.w(TAG, "Could not resolve input ID matching the device info; " |
| 1174 | + "ignoring."); |
| 1175 | } |
| 1176 | break; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1177 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 1178 | default: { |
| 1179 | Slog.w(TAG, "Unhandled message: " + msg); |
| 1180 | break; |
| 1181 | } |
| 1182 | } |
| 1183 | } |
| 1184 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1185 | |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 1186 | // Listener implementations for HdmiControlService |
| 1187 | |
| 1188 | private final class HdmiHotplugEventListener extends IHdmiHotplugEventListener.Stub { |
| 1189 | @Override |
| 1190 | public void onReceived(HdmiHotplugEvent event) { |
| 1191 | synchronized (mLock) { |
| 1192 | mHdmiStateMap.put(event.getPort(), event.isConnected()); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 1193 | TvInputHardwareInfo hardwareInfo = |
| 1194 | findHardwareInfoForHdmiPortLocked(event.getPort()); |
| 1195 | if (hardwareInfo == null) { |
| 1196 | return; |
| 1197 | } |
| 1198 | String inputId = mHardwareInputIdMap.get(hardwareInfo.getDeviceId()); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 1199 | if (inputId == null) { |
| 1200 | return; |
| 1201 | } |
Wonsik Kim | 102d0b7 | 2015-11-26 18:51:09 +0900 | [diff] [blame] | 1202 | // No HDMI hotplug does not necessarily mean disconnected, as old devices may |
| 1203 | // not report hotplug state correctly. Using INPUT_STATE_CONNECTED_STANDBY to |
| 1204 | // denote unknown state. |
| 1205 | int state = event.isConnected() |
| 1206 | ? INPUT_STATE_CONNECTED |
| 1207 | : INPUT_STATE_CONNECTED_STANDBY; |
| 1208 | mHandler.obtainMessage( |
| 1209 | ListenerHandler.STATE_CHANGED, state, 0, inputId).sendToTarget(); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | } |
| 1213 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1214 | private final class HdmiDeviceEventListener extends IHdmiDeviceEventListener.Stub { |
| 1215 | @Override |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1216 | public void onStatusChanged(HdmiDeviceInfo deviceInfo, int status) { |
Jinsuk Kim | 98d760e | 2014-12-23 07:01:51 +0900 | [diff] [blame] | 1217 | if (!deviceInfo.isSourceType()) return; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 1218 | synchronized (mLock) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1219 | int messageType = 0; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 1220 | Object obj = null; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1221 | switch (status) { |
| 1222 | case HdmiControlManager.DEVICE_EVENT_ADD_DEVICE: { |
Jungshik Jang | cf44587 | 2014-08-20 15:06:01 +0900 | [diff] [blame] | 1223 | if (findHdmiDeviceInfo(deviceInfo.getId()) == null) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1224 | mHdmiDeviceList.add(deviceInfo); |
| 1225 | } else { |
| 1226 | Slog.w(TAG, "The list already contains " + deviceInfo + "; ignoring."); |
| 1227 | return; |
| 1228 | } |
| 1229 | messageType = ListenerHandler.HDMI_DEVICE_ADDED; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 1230 | obj = deviceInfo; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1231 | break; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 1232 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1233 | case HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE: { |
Jungshik Jang | cf44587 | 2014-08-20 15:06:01 +0900 | [diff] [blame] | 1234 | HdmiDeviceInfo originalDeviceInfo = findHdmiDeviceInfo(deviceInfo.getId()); |
| 1235 | if (!mHdmiDeviceList.remove(originalDeviceInfo)) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1236 | Slog.w(TAG, "The list doesn't contain " + deviceInfo + "; ignoring."); |
| 1237 | return; |
| 1238 | } |
| 1239 | messageType = ListenerHandler.HDMI_DEVICE_REMOVED; |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 1240 | obj = deviceInfo; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1241 | break; |
| 1242 | } |
| 1243 | case HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE: { |
Jungshik Jang | cf44587 | 2014-08-20 15:06:01 +0900 | [diff] [blame] | 1244 | HdmiDeviceInfo originalDeviceInfo = findHdmiDeviceInfo(deviceInfo.getId()); |
| 1245 | if (!mHdmiDeviceList.remove(originalDeviceInfo)) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1246 | Slog.w(TAG, "The list doesn't contain " + deviceInfo + "; ignoring."); |
| 1247 | return; |
| 1248 | } |
| 1249 | mHdmiDeviceList.add(deviceInfo); |
| 1250 | messageType = ListenerHandler.HDMI_DEVICE_UPDATED; |
Wonsik Kim | 184a6d6 | 2014-10-08 13:05:56 +0900 | [diff] [blame] | 1251 | obj = deviceInfo; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1252 | break; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 1253 | } |
| 1254 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 1255 | |
Wonsik Kim | e92f857 | 2014-08-12 18:30:24 +0900 | [diff] [blame] | 1256 | Message msg = mHandler.obtainMessage(messageType, 0, 0, obj); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 1257 | if (findHardwareInfoForHdmiPortLocked(deviceInfo.getPortId()) != null) { |
| 1258 | msg.sendToTarget(); |
| 1259 | } else { |
| 1260 | mPendingHdmiDeviceEvents.add(msg); |
| 1261 | } |
| 1262 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1263 | } |
Jungshik Jang | cf44587 | 2014-08-20 15:06:01 +0900 | [diff] [blame] | 1264 | |
| 1265 | private HdmiDeviceInfo findHdmiDeviceInfo(int id) { |
| 1266 | for (HdmiDeviceInfo info : mHdmiDeviceList) { |
| 1267 | if (info.getId() == id) { |
| 1268 | return info; |
| 1269 | } |
| 1270 | } |
| 1271 | return null; |
| 1272 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 1273 | } |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 1274 | |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 1275 | private final class HdmiSystemAudioModeChangeListener extends |
| 1276 | IHdmiSystemAudioModeChangeListener.Stub { |
| 1277 | @Override |
| 1278 | public void onStatusChanged(boolean enabled) throws RemoteException { |
| 1279 | synchronized (mLock) { |
| 1280 | for (int i = 0; i < mConnections.size(); ++i) { |
| 1281 | TvInputHardwareImpl impl = mConnections.valueAt(i).getHardwareImplLocked(); |
Jungshik Jang | 44bb3a5 | 2014-08-26 17:24:24 +0900 | [diff] [blame] | 1282 | if (impl != null) { |
| 1283 | impl.handleAudioSinkUpdated(); |
| 1284 | } |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1289 | } |