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