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 | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 22 | import android.content.Context; |
Jinsuk Kim | b2b3151 | 2014-07-21 17:14:44 +0900 | [diff] [blame] | 23 | import android.content.Intent; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 24 | import android.hardware.hdmi.HdmiControlManager; |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 25 | import android.hardware.hdmi.HdmiDeviceInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 26 | import android.hardware.hdmi.HdmiHotplugEvent; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 27 | import android.hardware.hdmi.IHdmiControlService; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 28 | import android.hardware.hdmi.IHdmiDeviceEventListener; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 29 | import android.hardware.hdmi.IHdmiHotplugEventListener; |
| 30 | import android.hardware.hdmi.IHdmiInputChangeListener; |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 31 | import android.hardware.hdmi.IHdmiSystemAudioModeChangeListener; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 32 | import android.media.AudioDevicePort; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 33 | import android.media.AudioFormat; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 34 | import android.media.AudioGain; |
| 35 | import android.media.AudioGainConfig; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 36 | import android.media.AudioManager; |
| 37 | import android.media.AudioPatch; |
| 38 | import android.media.AudioPort; |
| 39 | import android.media.AudioPortConfig; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 40 | import android.media.tv.ITvInputHardware; |
| 41 | import android.media.tv.ITvInputHardwareCallback; |
Jinsuk Kim | b2b3151 | 2014-07-21 17:14:44 +0900 | [diff] [blame] | 42 | import android.media.tv.TvContract; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 43 | import android.media.tv.TvInputHardwareInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 44 | import android.media.tv.TvInputInfo; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 45 | import android.media.tv.TvStreamConfig; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 46 | import android.os.Handler; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 47 | import android.os.IBinder; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 48 | import android.os.Message; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 49 | import android.os.RemoteException; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 50 | import android.os.ServiceManager; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 51 | import android.util.ArrayMap; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 52 | import android.util.Slog; |
| 53 | import android.util.SparseArray; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 54 | import android.util.SparseBooleanArray; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 55 | import android.view.KeyEvent; |
| 56 | import android.view.Surface; |
| 57 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 58 | import com.android.server.SystemService; |
| 59 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 60 | import java.util.ArrayList; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 61 | import java.util.Arrays; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 62 | import java.util.Collections; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 63 | import java.util.Iterator; |
| 64 | import java.util.LinkedList; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 65 | import java.util.List; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 66 | import java.util.Map; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * A helper class for TvInputManagerService to handle TV input hardware. |
| 70 | * |
| 71 | * This class does a basic connection management and forwarding calls to TvInputHal which eventually |
| 72 | * calls to tv_input HAL module. |
| 73 | * |
| 74 | * @hide |
| 75 | */ |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 76 | class TvInputHardwareManager implements TvInputHal.Callback { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 77 | private static final String TAG = TvInputHardwareManager.class.getSimpleName(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 78 | |
| 79 | private final Context mContext; |
| 80 | private final Listener mListener; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 81 | private final TvInputHal mHal = new TvInputHal(this); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 82 | private final SparseArray<Connection> mConnections = new SparseArray<>(); |
| 83 | private final List<TvInputHardwareInfo> mHardwareList = new ArrayList<>(); |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 84 | private final List<HdmiDeviceInfo> mHdmiDeviceList = new LinkedList<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 85 | /* A map from a device ID to the matching TV input ID. */ |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 86 | private final SparseArray<String> mHardwareInputIdMap = new SparseArray<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 87 | /* 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] | 88 | private final SparseArray<String> mHdmiInputIdMap = new SparseArray<>(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 89 | private final Map<String, TvInputInfo> mInputMap = new ArrayMap<>(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 90 | |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 91 | private final AudioManager mAudioManager; |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 92 | private IHdmiControlService mHdmiControlService; |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 93 | private final IHdmiHotplugEventListener mHdmiHotplugEventListener = |
| 94 | new HdmiHotplugEventListener(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 95 | private final IHdmiDeviceEventListener mHdmiDeviceEventListener = new HdmiDeviceEventListener(); |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 96 | private final IHdmiSystemAudioModeChangeListener mHdmiSystemAudioModeChangeListener = |
| 97 | new HdmiSystemAudioModeChangeListener(); |
Jinsuk Kim | d38bf47 | 2014-08-11 11:29:18 +0900 | [diff] [blame] | 98 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 99 | // TODO: Should handle STANDBY case. |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 100 | private final SparseBooleanArray mHdmiStateMap = new SparseBooleanArray(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 101 | private final List<Message> mPendingHdmiDeviceEvents = new LinkedList<>(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 102 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 103 | // Calls to mListener should happen here. |
| 104 | private final Handler mHandler = new ListenerHandler(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 105 | |
| 106 | private final Object mLock = new Object(); |
| 107 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 108 | public TvInputHardwareManager(Context context, Listener listener) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 109 | mContext = context; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 110 | mListener = listener; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 111 | mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 112 | mHal.init(); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | public void onBootPhase(int phase) { |
| 116 | if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 117 | mHdmiControlService = IHdmiControlService.Stub.asInterface(ServiceManager.getService( |
| 118 | Context.HDMI_CONTROL_SERVICE)); |
| 119 | if (mHdmiControlService != null) { |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 120 | try { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 121 | mHdmiControlService.addHotplugEventListener(mHdmiHotplugEventListener); |
| 122 | mHdmiControlService.addDeviceEventListener(mHdmiDeviceEventListener); |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 123 | mHdmiControlService.addSystemAudioModeChangeListener( |
| 124 | mHdmiSystemAudioModeChangeListener); |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 125 | mHdmiDeviceList.addAll(mHdmiControlService.getInputDevices()); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 126 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 127 | Slog.w(TAG, "Error registering listeners to HdmiControlService:", e); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 128 | } |
| 129 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 130 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 134 | public void onDeviceAvailable(TvInputHardwareInfo info, TvStreamConfig[] configs) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 135 | synchronized (mLock) { |
| 136 | Connection connection = new Connection(info); |
| 137 | connection.updateConfigsLocked(configs); |
| 138 | mConnections.put(info.getDeviceId(), connection); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 139 | buildHardwareListLocked(); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 140 | mHandler.obtainMessage( |
| 141 | ListenerHandler.HARDWARE_DEVICE_ADDED, 0, 0, info).sendToTarget(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 142 | if (info.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
| 143 | processPendingHdmiDeviceEventsLocked(); |
| 144 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 148 | private void buildHardwareListLocked() { |
| 149 | mHardwareList.clear(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 150 | for (int i = 0; i < mConnections.size(); ++i) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 151 | mHardwareList.add(mConnections.valueAt(i).getHardwareInfoLocked()); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 152 | } |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void onDeviceUnavailable(int deviceId) { |
| 157 | synchronized (mLock) { |
| 158 | Connection connection = mConnections.get(deviceId); |
| 159 | if (connection == null) { |
| 160 | Slog.e(TAG, "onDeviceUnavailable: Cannot find a connection with " + deviceId); |
| 161 | return; |
| 162 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 163 | connection.resetLocked(null, null, null, null, null); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 164 | mConnections.remove(deviceId); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 165 | buildHardwareListLocked(); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 166 | TvInputHardwareInfo info = connection.getHardwareInfoLocked(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 167 | if (info.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 168 | // Remove HDMI devices linked with this hardware. |
| 169 | for (Iterator<HdmiDeviceInfo> it = mHdmiDeviceList.iterator(); it.hasNext();) { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 170 | HdmiDeviceInfo deviceInfo = it.next(); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 171 | if (deviceInfo.getPortId() == info.getHdmiPortId()) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 172 | mHandler.obtainMessage(ListenerHandler.HDMI_DEVICE_REMOVED, 0, 0, |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 173 | deviceInfo).sendToTarget(); |
| 174 | it.remove(); |
| 175 | } |
| 176 | } |
| 177 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 178 | mHandler.obtainMessage( |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 179 | ListenerHandler.HARDWARE_DEVICE_REMOVED, 0, 0, info).sendToTarget(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | |
| 183 | @Override |
| 184 | public void onStreamConfigurationChanged(int deviceId, TvStreamConfig[] configs) { |
| 185 | synchronized (mLock) { |
| 186 | Connection connection = mConnections.get(deviceId); |
| 187 | if (connection == null) { |
| 188 | Slog.e(TAG, "StreamConfigurationChanged: Cannot find a connection with " |
| 189 | + deviceId); |
| 190 | return; |
| 191 | } |
| 192 | connection.updateConfigsLocked(configs); |
| 193 | try { |
| 194 | connection.getCallbackLocked().onStreamConfigChanged(configs); |
| 195 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 196 | Slog.e(TAG, "error in onStreamConfigurationChanged", e); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 201 | @Override |
| 202 | public void onFirstFrameCaptured(int deviceId, int streamId) { |
| 203 | synchronized (mLock) { |
| 204 | Connection connection = mConnections.get(deviceId); |
| 205 | if (connection == null) { |
| 206 | Slog.e(TAG, "FirstFrameCaptured: Cannot find a connection with " |
| 207 | + deviceId); |
| 208 | return; |
| 209 | } |
| 210 | Runnable runnable = connection.getOnFirstFrameCapturedLocked(); |
| 211 | if (runnable != null) { |
| 212 | runnable.run(); |
| 213 | connection.setOnFirstFrameCapturedLocked(null); |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 218 | public List<TvInputHardwareInfo> getHardwareList() { |
| 219 | synchronized (mLock) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 220 | return Collections.unmodifiableList(mHardwareList); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 224 | public List<HdmiDeviceInfo> getHdmiDeviceList() { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 225 | synchronized (mLock) { |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 226 | return Collections.unmodifiableList(mHdmiDeviceList); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 227 | } |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 228 | } |
| 229 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 230 | private boolean checkUidChangedLocked( |
| 231 | Connection connection, int callingUid, int resolvedUserId) { |
| 232 | Integer connectionCallingUid = connection.getCallingUidLocked(); |
| 233 | Integer connectionResolvedUserId = connection.getResolvedUserIdLocked(); |
| 234 | if (connectionCallingUid == null || connectionResolvedUserId == null) { |
| 235 | return true; |
| 236 | } |
| 237 | if (connectionCallingUid != callingUid || connectionResolvedUserId != resolvedUserId) { |
| 238 | return true; |
| 239 | } |
| 240 | return false; |
| 241 | } |
| 242 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 243 | private int convertConnectedToState(boolean connected) { |
| 244 | if (connected) { |
| 245 | return INPUT_STATE_CONNECTED; |
| 246 | } else { |
| 247 | return INPUT_STATE_DISCONNECTED; |
| 248 | } |
| 249 | } |
| 250 | |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 251 | public void addHardwareTvInput(int deviceId, TvInputInfo info) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 252 | synchronized (mLock) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 253 | String oldInputId = mHardwareInputIdMap.get(deviceId); |
| 254 | if (oldInputId != null) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 255 | Slog.w(TAG, "Trying to override previous registration: old = " |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 256 | + mInputMap.get(oldInputId) + ":" + deviceId + ", new = " |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 257 | + info + ":" + deviceId); |
| 258 | } |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 259 | mHardwareInputIdMap.put(deviceId, info.getId()); |
| 260 | mInputMap.put(info.getId(), info); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 261 | |
| 262 | for (int i = 0; i < mHdmiStateMap.size(); ++i) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 263 | TvInputHardwareInfo hardwareInfo = |
| 264 | findHardwareInfoForHdmiPortLocked(mHdmiStateMap.keyAt(i)); |
| 265 | if (hardwareInfo == null) { |
| 266 | continue; |
| 267 | } |
| 268 | String inputId = mHardwareInputIdMap.get(hardwareInfo.getDeviceId()); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 269 | if (inputId != null && inputId.equals(info.getId())) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 270 | mHandler.obtainMessage(ListenerHandler.STATE_CHANGED, |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 271 | convertConnectedToState(mHdmiStateMap.valueAt(i)), 0, |
| 272 | inputId).sendToTarget(); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 278 | private static <T> int indexOfEqualValue(SparseArray<T> map, T value) { |
| 279 | for (int i = 0; i < map.size(); ++i) { |
| 280 | if (map.valueAt(i).equals(value)) { |
| 281 | return i; |
| 282 | } |
| 283 | } |
| 284 | return -1; |
| 285 | } |
| 286 | |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 287 | public void addHdmiTvInput(int logicalAddress, TvInputInfo info) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 288 | if (info.getType() != TvInputInfo.TYPE_HDMI) { |
| 289 | throw new IllegalArgumentException("info (" + info + ") has non-HDMI type."); |
| 290 | } |
| 291 | synchronized (mLock) { |
| 292 | String parentId = info.getParentId(); |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 293 | int parentIndex = indexOfEqualValue(mHardwareInputIdMap, parentId); |
| 294 | if (parentIndex < 0) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 295 | throw new IllegalArgumentException("info (" + info + ") has invalid parentId."); |
| 296 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 297 | String oldInputId = mHdmiInputIdMap.get(logicalAddress); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 298 | if (oldInputId != null) { |
| 299 | Slog.w(TAG, "Trying to override previous registration: old = " |
| 300 | + mInputMap.get(oldInputId) + ":" + logicalAddress + ", new = " |
| 301 | + info + ":" + logicalAddress); |
| 302 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 303 | mHdmiInputIdMap.put(logicalAddress, info.getId()); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 304 | mInputMap.put(info.getId(), info); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | public void removeTvInput(String inputId) { |
| 309 | synchronized (mLock) { |
| 310 | mInputMap.remove(inputId); |
Wonsik Kim | 38feae9 | 2014-07-21 21:35:50 +0900 | [diff] [blame] | 311 | int hardwareIndex = indexOfEqualValue(mHardwareInputIdMap, inputId); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 312 | if (hardwareIndex >= 0) { |
| 313 | mHardwareInputIdMap.removeAt(hardwareIndex); |
| 314 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 315 | int deviceIndex = indexOfEqualValue(mHdmiInputIdMap, inputId); |
| 316 | if (deviceIndex >= 0) { |
| 317 | mHdmiInputIdMap.removeAt(deviceIndex); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 322 | /** |
| 323 | * Create a TvInputHardware object with a specific deviceId. One service at a time can access |
| 324 | * the object, and if more than one process attempts to create hardware with the same deviceId, |
| 325 | * the latest service will get the object and all the other hardware are released. The |
| 326 | * release is notified via ITvInputHardwareCallback.onReleased(). |
| 327 | */ |
| 328 | public ITvInputHardware acquireHardware(int deviceId, ITvInputHardwareCallback callback, |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 329 | TvInputInfo info, int callingUid, int resolvedUserId) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 330 | if (callback == null) { |
| 331 | throw new NullPointerException(); |
| 332 | } |
| 333 | synchronized (mLock) { |
| 334 | Connection connection = mConnections.get(deviceId); |
| 335 | if (connection == null) { |
| 336 | Slog.e(TAG, "Invalid deviceId : " + deviceId); |
| 337 | return null; |
| 338 | } |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 339 | if (checkUidChangedLocked(connection, callingUid, resolvedUserId)) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 340 | TvInputHardwareImpl hardware = |
| 341 | new TvInputHardwareImpl(connection.getHardwareInfoLocked()); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 342 | try { |
| 343 | callback.asBinder().linkToDeath(connection, 0); |
| 344 | } catch (RemoteException e) { |
| 345 | hardware.release(); |
| 346 | return null; |
| 347 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 348 | connection.resetLocked(hardware, callback, info, callingUid, resolvedUserId); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 349 | } |
| 350 | return connection.getHardwareLocked(); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * Release the specified hardware. |
| 356 | */ |
| 357 | public void releaseHardware(int deviceId, ITvInputHardware hardware, int callingUid, |
| 358 | int resolvedUserId) { |
| 359 | synchronized (mLock) { |
| 360 | Connection connection = mConnections.get(deviceId); |
| 361 | if (connection == null) { |
| 362 | Slog.e(TAG, "Invalid deviceId : " + deviceId); |
| 363 | return; |
| 364 | } |
| 365 | if (connection.getHardwareLocked() != hardware |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 366 | || checkUidChangedLocked(connection, callingUid, resolvedUserId)) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 367 | return; |
| 368 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 369 | connection.resetLocked(null, null, null, null, null); |
| 370 | } |
| 371 | } |
| 372 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 373 | private TvInputHardwareInfo findHardwareInfoForHdmiPortLocked(int port) { |
| 374 | for (TvInputHardwareInfo hardwareInfo : mHardwareList) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 375 | if (hardwareInfo.getType() == TvInputHardwareInfo.TV_INPUT_TYPE_HDMI |
| 376 | && hardwareInfo.getHdmiPortId() == port) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 377 | return hardwareInfo; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 378 | } |
| 379 | } |
| 380 | return null; |
| 381 | } |
| 382 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 383 | private int findDeviceIdForInputIdLocked(String inputId) { |
| 384 | for (int i = 0; i < mConnections.size(); ++i) { |
| 385 | Connection connection = mConnections.get(i); |
| 386 | if (connection.getInfoLocked().getId().equals(inputId)) { |
| 387 | return i; |
| 388 | } |
| 389 | } |
| 390 | return -1; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * Get the list of TvStreamConfig which is buffered mode. |
| 395 | */ |
| 396 | public List<TvStreamConfig> getAvailableTvStreamConfigList(String inputId, int callingUid, |
| 397 | int resolvedUserId) { |
| 398 | List<TvStreamConfig> configsList = new ArrayList<TvStreamConfig>(); |
| 399 | synchronized (mLock) { |
| 400 | int deviceId = findDeviceIdForInputIdLocked(inputId); |
| 401 | if (deviceId < 0) { |
| 402 | Slog.e(TAG, "Invalid inputId : " + inputId); |
| 403 | return configsList; |
| 404 | } |
| 405 | Connection connection = mConnections.get(deviceId); |
| 406 | for (TvStreamConfig config : connection.getConfigsLocked()) { |
| 407 | if (config.getType() == TvStreamConfig.STREAM_TYPE_BUFFER_PRODUCER) { |
| 408 | configsList.add(config); |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | return configsList; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Take a snapshot of the given TV input into the provided Surface. |
| 417 | */ |
| 418 | public boolean captureFrame(String inputId, Surface surface, final TvStreamConfig config, |
| 419 | int callingUid, int resolvedUserId) { |
| 420 | synchronized (mLock) { |
| 421 | int deviceId = findDeviceIdForInputIdLocked(inputId); |
| 422 | if (deviceId < 0) { |
| 423 | Slog.e(TAG, "Invalid inputId : " + inputId); |
| 424 | return false; |
| 425 | } |
| 426 | Connection connection = mConnections.get(deviceId); |
| 427 | final TvInputHardwareImpl hardwareImpl = connection.getHardwareImplLocked(); |
| 428 | if (hardwareImpl != null) { |
| 429 | // Stop previous capture. |
| 430 | Runnable runnable = connection.getOnFirstFrameCapturedLocked(); |
| 431 | if (runnable != null) { |
| 432 | runnable.run(); |
| 433 | connection.setOnFirstFrameCapturedLocked(null); |
| 434 | } |
| 435 | |
| 436 | boolean result = hardwareImpl.startCapture(surface, config); |
| 437 | if (result) { |
| 438 | connection.setOnFirstFrameCapturedLocked(new Runnable() { |
| 439 | @Override |
| 440 | public void run() { |
| 441 | hardwareImpl.stopCapture(config); |
| 442 | } |
| 443 | }); |
| 444 | } |
| 445 | return result; |
| 446 | } |
| 447 | } |
| 448 | return false; |
| 449 | } |
| 450 | |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 451 | private void processPendingHdmiDeviceEventsLocked() { |
| 452 | for (Iterator<Message> it = mPendingHdmiDeviceEvents.iterator(); it.hasNext(); ) { |
| 453 | Message msg = it.next(); |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 454 | HdmiDeviceInfo deviceInfo = (HdmiDeviceInfo) msg.obj; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 455 | TvInputHardwareInfo hardwareInfo = |
| 456 | findHardwareInfoForHdmiPortLocked(deviceInfo.getPortId()); |
| 457 | if (hardwareInfo != null) { |
| 458 | msg.sendToTarget(); |
| 459 | it.remove(); |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 464 | private class Connection implements IBinder.DeathRecipient { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 465 | private final TvInputHardwareInfo mHardwareInfo; |
| 466 | private TvInputInfo mInfo; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 467 | private TvInputHardwareImpl mHardware = null; |
| 468 | private ITvInputHardwareCallback mCallback; |
| 469 | private TvStreamConfig[] mConfigs = null; |
| 470 | private Integer mCallingUid = null; |
| 471 | private Integer mResolvedUserId = null; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 472 | private Runnable mOnFirstFrameCaptured; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 473 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 474 | public Connection(TvInputHardwareInfo hardwareInfo) { |
| 475 | mHardwareInfo = hardwareInfo; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // *Locked methods assume TvInputHardwareManager.mLock is held. |
| 479 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 480 | public void resetLocked(TvInputHardwareImpl hardware, ITvInputHardwareCallback callback, |
| 481 | TvInputInfo info, Integer callingUid, Integer resolvedUserId) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 482 | if (mHardware != null) { |
| 483 | try { |
| 484 | mCallback.onReleased(); |
| 485 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 486 | Slog.e(TAG, "error in Connection::resetLocked", e); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 487 | } |
| 488 | mHardware.release(); |
| 489 | } |
| 490 | mHardware = hardware; |
| 491 | mCallback = callback; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 492 | mInfo = info; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 493 | mCallingUid = callingUid; |
| 494 | mResolvedUserId = resolvedUserId; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 495 | mOnFirstFrameCaptured = null; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 496 | |
| 497 | if (mHardware != null && mCallback != null) { |
| 498 | try { |
| 499 | mCallback.onStreamConfigChanged(getConfigsLocked()); |
| 500 | } catch (RemoteException e) { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 501 | Slog.e(TAG, "error in Connection::resetLocked", e); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | public void updateConfigsLocked(TvStreamConfig[] configs) { |
| 507 | mConfigs = configs; |
| 508 | } |
| 509 | |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 510 | public TvInputHardwareInfo getHardwareInfoLocked() { |
| 511 | return mHardwareInfo; |
| 512 | } |
| 513 | |
| 514 | public TvInputInfo getInfoLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 515 | return mInfo; |
| 516 | } |
| 517 | |
| 518 | public ITvInputHardware getHardwareLocked() { |
| 519 | return mHardware; |
| 520 | } |
| 521 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 522 | public TvInputHardwareImpl getHardwareImplLocked() { |
| 523 | return mHardware; |
| 524 | } |
| 525 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 526 | public ITvInputHardwareCallback getCallbackLocked() { |
| 527 | return mCallback; |
| 528 | } |
| 529 | |
| 530 | public TvStreamConfig[] getConfigsLocked() { |
| 531 | return mConfigs; |
| 532 | } |
| 533 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 534 | public Integer getCallingUidLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 535 | return mCallingUid; |
| 536 | } |
| 537 | |
Wonsik Kim | e7ae0ce | 2014-06-19 00:48:35 +0900 | [diff] [blame] | 538 | public Integer getResolvedUserIdLocked() { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 539 | return mResolvedUserId; |
| 540 | } |
| 541 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 542 | public void setOnFirstFrameCapturedLocked(Runnable runnable) { |
| 543 | mOnFirstFrameCaptured = runnable; |
| 544 | } |
| 545 | |
| 546 | public Runnable getOnFirstFrameCapturedLocked() { |
| 547 | return mOnFirstFrameCaptured; |
| 548 | } |
| 549 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 550 | @Override |
| 551 | public void binderDied() { |
| 552 | synchronized (mLock) { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 553 | resetLocked(null, null, null, null, null); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 554 | } |
| 555 | } |
| 556 | } |
| 557 | |
| 558 | private class TvInputHardwareImpl extends ITvInputHardware.Stub { |
| 559 | private final TvInputHardwareInfo mInfo; |
| 560 | private boolean mReleased = false; |
| 561 | private final Object mImplLock = new Object(); |
| 562 | |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 563 | private final AudioManager.OnAudioPortUpdateListener mAudioListener = |
| 564 | new AudioManager.OnAudioPortUpdateListener() { |
| 565 | @Override |
| 566 | public void onAudioPortListUpdate(AudioPort[] portList) { |
| 567 | synchronized (mImplLock) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 568 | updateAudioConfigLocked(); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void onAudioPatchListUpdate(AudioPatch[] patchList) { |
| 574 | // No-op |
| 575 | } |
| 576 | |
| 577 | @Override |
| 578 | public void onServiceDied() { |
| 579 | synchronized (mImplLock) { |
| 580 | mAudioSource = null; |
| 581 | mAudioSink = null; |
| 582 | mAudioPatch = null; |
| 583 | } |
| 584 | } |
| 585 | }; |
| 586 | private int mOverrideAudioType = AudioManager.DEVICE_NONE; |
| 587 | private String mOverrideAudioAddress = ""; |
| 588 | private AudioDevicePort mAudioSource; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 589 | private AudioDevicePort mAudioSink; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 590 | private AudioPatch mAudioPatch = null; |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 591 | private float mCommittedVolume = 0.0f; |
| 592 | private float mVolume = 0.0f; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 593 | |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 594 | private TvStreamConfig mActiveConfig = null; |
| 595 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 596 | private int mDesiredSamplingRate = 0; |
| 597 | private int mDesiredChannelMask = AudioFormat.CHANNEL_OUT_DEFAULT; |
| 598 | private int mDesiredFormat = AudioFormat.ENCODING_DEFAULT; |
| 599 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 600 | public TvInputHardwareImpl(TvInputHardwareInfo info) { |
| 601 | mInfo = info; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 602 | mAudioManager.registerAudioPortUpdateListener(mAudioListener); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 603 | if (mInfo.getAudioType() != AudioManager.DEVICE_NONE) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 604 | mAudioSource = findAudioDevicePort(mInfo.getAudioType(), mInfo.getAudioAddress()); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 605 | mAudioSink = findAudioSinkFromAudioPolicy(); |
| 606 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | private AudioDevicePort findAudioSinkFromAudioPolicy() { |
| 610 | ArrayList<AudioPort> devicePorts = new ArrayList<AudioPort>(); |
| 611 | if (mAudioManager.listAudioDevicePorts(devicePorts) == AudioManager.SUCCESS) { |
| 612 | int sinkDevice = mAudioManager.getDevicesForStream(AudioManager.STREAM_MUSIC); |
| 613 | for (AudioPort port : devicePorts) { |
| 614 | AudioDevicePort devicePort = (AudioDevicePort) port; |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 615 | if ((devicePort.type() & sinkDevice) != 0) { |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 616 | return devicePort; |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 617 | } |
| 618 | } |
| 619 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 620 | return null; |
| 621 | } |
| 622 | |
| 623 | private AudioDevicePort findAudioDevicePort(int type, String address) { |
| 624 | if (type == AudioManager.DEVICE_NONE) { |
| 625 | return null; |
| 626 | } |
| 627 | ArrayList<AudioPort> devicePorts = new ArrayList<AudioPort>(); |
| 628 | if (mAudioManager.listAudioDevicePorts(devicePorts) != AudioManager.SUCCESS) { |
| 629 | return null; |
| 630 | } |
| 631 | for (AudioPort port : devicePorts) { |
| 632 | AudioDevicePort devicePort = (AudioDevicePort) port; |
| 633 | if (devicePort.type() == type && devicePort.address().equals(address)) { |
| 634 | return devicePort; |
| 635 | } |
| 636 | } |
| 637 | return null; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 638 | } |
| 639 | |
| 640 | public void release() { |
| 641 | synchronized (mImplLock) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 642 | mAudioManager.unregisterAudioPortUpdateListener(mAudioListener); |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 643 | if (mAudioPatch != null) { |
| 644 | mAudioManager.releaseAudioPatch(mAudioPatch); |
| 645 | mAudioPatch = null; |
| 646 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 647 | mReleased = true; |
| 648 | } |
| 649 | } |
| 650 | |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 651 | // A TvInputHardwareImpl object holds only one active session. Therefore, if a client |
| 652 | // attempts to call setSurface with different TvStreamConfig objects, the last call will |
| 653 | // prevail. |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 654 | @Override |
| 655 | public boolean setSurface(Surface surface, TvStreamConfig config) |
| 656 | throws RemoteException { |
| 657 | synchronized (mImplLock) { |
| 658 | if (mReleased) { |
| 659 | throw new IllegalStateException("Device already released."); |
| 660 | } |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 661 | if (surface != null && config == null) { |
| 662 | return false; |
| 663 | } |
| 664 | if (surface == null && mActiveConfig == null) { |
| 665 | return false; |
| 666 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 667 | |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 668 | int result = TvInputHal.ERROR_UNKNOWN; |
| 669 | if (surface == null) { |
| 670 | result = mHal.removeStream(mInfo.getDeviceId(), mActiveConfig); |
| 671 | mActiveConfig = null; |
| 672 | } else { |
| 673 | if (config != mActiveConfig && mActiveConfig != null) { |
| 674 | result = mHal.removeStream(mInfo.getDeviceId(), mActiveConfig); |
| 675 | if (result != TvInputHal.SUCCESS) { |
| 676 | mActiveConfig = null; |
| 677 | return false; |
| 678 | } |
| 679 | } |
| 680 | result = mHal.addStream(mInfo.getDeviceId(), surface, config); |
| 681 | if (result == TvInputHal.SUCCESS) { |
| 682 | mActiveConfig = config; |
| 683 | } |
| 684 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 685 | updateAudioConfigLocked(); |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 686 | return result == TvInputHal.SUCCESS; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 690 | /** |
| 691 | * Update audio configuration (source, sink, patch) all up to current state. |
| 692 | */ |
| 693 | private void updateAudioConfigLocked() { |
| 694 | boolean sinkUpdated = updateAudioSinkLocked(); |
| 695 | boolean sourceUpdated = updateAudioSourceLocked(); |
| 696 | // We can't do updated = updateAudioSinkLocked() || updateAudioSourceLocked() here |
| 697 | // because Java won't evaluate the latter if the former is true. |
| 698 | |
| 699 | if (mAudioSource == null || mAudioSink == null || mActiveConfig == null) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 700 | if (mAudioPatch != null) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 701 | mAudioManager.releaseAudioPatch(mAudioPatch); |
| 702 | mAudioPatch = null; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 703 | } |
| 704 | return; |
| 705 | } |
| 706 | |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 707 | AudioGainConfig sourceGainConfig = null; |
| 708 | if (mAudioSource.gains().length > 0 && mVolume != mCommittedVolume) { |
| 709 | AudioGain sourceGain = null; |
| 710 | for (AudioGain gain : mAudioSource.gains()) { |
| 711 | if ((gain.mode() & AudioGain.MODE_JOINT) != 0) { |
| 712 | sourceGain = gain; |
| 713 | break; |
| 714 | } |
| 715 | } |
| 716 | // NOTE: we only change the source gain in MODE_JOINT here. |
| 717 | if (sourceGain != null) { |
| 718 | int steps = (sourceGain.maxValue() - sourceGain.minValue()) |
| 719 | / sourceGain.stepValue(); |
| 720 | int gainValue = sourceGain.minValue(); |
| 721 | if (mVolume < 1.0f) { |
| 722 | gainValue += sourceGain.stepValue() * (int) (mVolume * steps + 0.5); |
| 723 | } else { |
| 724 | gainValue = sourceGain.maxValue(); |
| 725 | } |
| 726 | int numChannels = 0; |
| 727 | for (int mask = sourceGain.channelMask(); mask > 0; mask >>= 1) { |
| 728 | numChannels += (mask & 1); |
| 729 | } |
| 730 | int[] gainValues = new int[numChannels]; |
| 731 | Arrays.fill(gainValues, gainValue); |
| 732 | sourceGainConfig = sourceGain.buildConfig(AudioGain.MODE_JOINT, |
| 733 | sourceGain.channelMask(), gainValues, 0); |
| 734 | } else { |
| 735 | Slog.w(TAG, "No audio source gain with MODE_JOINT support exists."); |
| 736 | } |
| 737 | } |
| 738 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 739 | AudioPortConfig sourceConfig = mAudioSource.activeConfig(); |
| 740 | AudioPortConfig sinkConfig = mAudioSink.activeConfig(); |
| 741 | AudioPatch[] audioPatchArray = new AudioPatch[] { mAudioPatch }; |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 742 | boolean shouldRecreateAudioPatch = sourceUpdated || sinkUpdated; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 743 | if (sinkConfig == null |
| 744 | || (mDesiredSamplingRate != 0 |
| 745 | && sinkConfig.samplingRate() != mDesiredSamplingRate) |
| 746 | || (mDesiredChannelMask != AudioFormat.CHANNEL_OUT_DEFAULT |
| 747 | && sinkConfig.channelMask() != mDesiredChannelMask) |
| 748 | || (mDesiredFormat != AudioFormat.ENCODING_DEFAULT |
| 749 | && sinkConfig.format() != mDesiredFormat)) { |
| 750 | sinkConfig = mAudioSource.buildConfig(mDesiredSamplingRate, mDesiredChannelMask, |
| 751 | mDesiredFormat, null); |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 752 | shouldRecreateAudioPatch = true; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 753 | } |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 754 | if (sourceConfig == null || sourceGainConfig != null) { |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 755 | sourceConfig = mAudioSource.buildConfig(sinkConfig.samplingRate(), |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 756 | sinkConfig.channelMask(), sinkConfig.format(), sourceGainConfig); |
| 757 | shouldRecreateAudioPatch = true; |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 758 | } |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 759 | if (shouldRecreateAudioPatch) { |
| 760 | mCommittedVolume = mVolume; |
| 761 | mAudioManager.createAudioPatch( |
| 762 | audioPatchArray, |
| 763 | new AudioPortConfig[] { sourceConfig }, |
| 764 | new AudioPortConfig[] { sinkConfig }); |
| 765 | mAudioPatch = audioPatchArray[0]; |
| 766 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 767 | } |
| 768 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 769 | @Override |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 770 | public void setStreamVolume(float volume) throws RemoteException { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 771 | synchronized (mImplLock) { |
| 772 | if (mReleased) { |
| 773 | throw new IllegalStateException("Device already released."); |
| 774 | } |
Wonsik Kim | 8e45a33 | 2014-08-04 10:17:18 +0900 | [diff] [blame] | 775 | mVolume = volume; |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 776 | updateAudioConfigLocked(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 777 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 778 | } |
| 779 | |
| 780 | @Override |
| 781 | public boolean dispatchKeyEventToHdmi(KeyEvent event) throws RemoteException { |
| 782 | synchronized (mImplLock) { |
| 783 | if (mReleased) { |
| 784 | throw new IllegalStateException("Device already released."); |
| 785 | } |
| 786 | } |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 787 | if (mInfo.getType() != TvInputHardwareInfo.TV_INPUT_TYPE_HDMI) { |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 788 | return false; |
| 789 | } |
| 790 | // TODO(hdmi): mHdmiClient.sendKeyEvent(event); |
| 791 | return false; |
| 792 | } |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 793 | |
| 794 | private boolean startCapture(Surface surface, TvStreamConfig config) { |
| 795 | synchronized (mImplLock) { |
| 796 | if (mReleased) { |
| 797 | return false; |
| 798 | } |
| 799 | if (surface == null || config == null) { |
| 800 | return false; |
| 801 | } |
| 802 | if (config.getType() != TvStreamConfig.STREAM_TYPE_BUFFER_PRODUCER) { |
| 803 | return false; |
| 804 | } |
| 805 | |
| 806 | int result = mHal.addStream(mInfo.getDeviceId(), surface, config); |
| 807 | return result == TvInputHal.SUCCESS; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | private boolean stopCapture(TvStreamConfig config) { |
| 812 | synchronized (mImplLock) { |
| 813 | if (mReleased) { |
| 814 | return false; |
| 815 | } |
| 816 | if (config == null) { |
| 817 | return false; |
| 818 | } |
| 819 | |
| 820 | int result = mHal.removeStream(mInfo.getDeviceId(), config); |
| 821 | return result == TvInputHal.SUCCESS; |
| 822 | } |
| 823 | } |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 824 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 825 | private boolean updateAudioSourceLocked() { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 826 | if (mInfo.getAudioType() == AudioManager.DEVICE_NONE) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 827 | return false; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 828 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 829 | AudioDevicePort previousSource = mAudioSource; |
| 830 | mAudioSource = findAudioDevicePort(mInfo.getAudioType(), mInfo.getAudioAddress()); |
| 831 | return mAudioSource == null ? (previousSource != null) |
| 832 | : !mAudioSource.equals(previousSource); |
| 833 | } |
| 834 | |
| 835 | private boolean updateAudioSinkLocked() { |
| 836 | if (mInfo.getAudioType() == AudioManager.DEVICE_NONE) { |
| 837 | return false; |
| 838 | } |
| 839 | AudioDevicePort previousSink = mAudioSink; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 840 | if (mOverrideAudioType == AudioManager.DEVICE_NONE) { |
| 841 | mAudioSink = findAudioSinkFromAudioPolicy(); |
| 842 | } else { |
| 843 | AudioDevicePort audioSink = |
| 844 | findAudioDevicePort(mOverrideAudioType, mOverrideAudioAddress); |
| 845 | if (audioSink != null) { |
| 846 | mAudioSink = audioSink; |
| 847 | } |
| 848 | } |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 849 | return mAudioSink == null ? (previousSink != null) : !mAudioSink.equals(previousSink); |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 850 | } |
| 851 | |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 852 | private void handleAudioSinkUpdated() { |
| 853 | synchronized (mImplLock) { |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 854 | updateAudioConfigLocked(); |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 855 | } |
| 856 | } |
| 857 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 858 | @Override |
| 859 | public void overrideAudioSink(int audioType, String audioAddress, int samplingRate, |
| 860 | int channelMask, int format) { |
| 861 | synchronized (mImplLock) { |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 862 | mOverrideAudioType = audioType; |
| 863 | mOverrideAudioAddress = audioAddress; |
Wonsik Kim | 06c41a3 | 2014-08-04 18:57:47 +0900 | [diff] [blame] | 864 | |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 865 | mDesiredSamplingRate = samplingRate; |
| 866 | mDesiredChannelMask = channelMask; |
| 867 | mDesiredFormat = format; |
| 868 | |
Wonsik Kim | 7587d29 | 2014-08-09 10:01:01 +0900 | [diff] [blame^] | 869 | updateAudioConfigLocked(); |
Wonsik Kim | ca17a90 | 2014-07-31 10:09:46 +0900 | [diff] [blame] | 870 | } |
| 871 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 872 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 873 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 874 | interface Listener { |
| 875 | public void onStateChanged(String inputId, int state); |
| 876 | public void onHardwareDeviceAdded(TvInputHardwareInfo info); |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 877 | public void onHardwareDeviceRemoved(TvInputHardwareInfo info); |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 878 | public void onHdmiDeviceAdded(HdmiDeviceInfo device); |
| 879 | public void onHdmiDeviceRemoved(HdmiDeviceInfo device); |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 880 | public void onHdmiDeviceUpdated(HdmiDeviceInfo device); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 881 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 882 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 883 | private class ListenerHandler extends Handler { |
| 884 | private static final int STATE_CHANGED = 1; |
| 885 | private static final int HARDWARE_DEVICE_ADDED = 2; |
| 886 | private static final int HARDWARE_DEVICE_REMOVED = 3; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 887 | private static final int HDMI_DEVICE_ADDED = 4; |
| 888 | private static final int HDMI_DEVICE_REMOVED = 5; |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 889 | private static final int HDMI_DEVICE_UPDATED = 6; |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 890 | |
| 891 | @Override |
| 892 | public final void handleMessage(Message msg) { |
| 893 | switch (msg.what) { |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 894 | case STATE_CHANGED: { |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 895 | String inputId = (String) msg.obj; |
| 896 | int state = msg.arg1; |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 897 | mListener.onStateChanged(inputId, state); |
| 898 | break; |
| 899 | } |
| 900 | case HARDWARE_DEVICE_ADDED: { |
| 901 | TvInputHardwareInfo info = (TvInputHardwareInfo) msg.obj; |
| 902 | mListener.onHardwareDeviceAdded(info); |
| 903 | break; |
| 904 | } |
| 905 | case HARDWARE_DEVICE_REMOVED: { |
Ji-Hwan Lee | 4f9f57c | 2014-07-19 22:20:31 +0900 | [diff] [blame] | 906 | TvInputHardwareInfo info = (TvInputHardwareInfo) msg.obj; |
| 907 | mListener.onHardwareDeviceRemoved(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 908 | break; |
| 909 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 910 | case HDMI_DEVICE_ADDED: { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 911 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 912 | mListener.onHdmiDeviceAdded(info); |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 913 | break; |
| 914 | } |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 915 | case HDMI_DEVICE_REMOVED: { |
Jungshik Jang | 61f4fbd | 2014-08-06 19:21:12 +0900 | [diff] [blame] | 916 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
Jae Seo | 546c635 | 2014-08-07 11:57:01 -0700 | [diff] [blame] | 917 | mListener.onHdmiDeviceRemoved(info); |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 918 | break; |
| 919 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 920 | case HDMI_DEVICE_UPDATED: { |
| 921 | HdmiDeviceInfo info = (HdmiDeviceInfo) msg.obj; |
| 922 | mListener.onHdmiDeviceUpdated(info); |
| 923 | } |
Wonsik Kim | 969167d | 2014-06-24 16:33:17 +0900 | [diff] [blame] | 924 | default: { |
| 925 | Slog.w(TAG, "Unhandled message: " + msg); |
| 926 | break; |
| 927 | } |
| 928 | } |
| 929 | } |
| 930 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 931 | |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 932 | // Listener implementations for HdmiControlService |
| 933 | |
| 934 | private final class HdmiHotplugEventListener extends IHdmiHotplugEventListener.Stub { |
| 935 | @Override |
| 936 | public void onReceived(HdmiHotplugEvent event) { |
| 937 | synchronized (mLock) { |
| 938 | mHdmiStateMap.put(event.getPort(), event.isConnected()); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 939 | TvInputHardwareInfo hardwareInfo = |
| 940 | findHardwareInfoForHdmiPortLocked(event.getPort()); |
| 941 | if (hardwareInfo == null) { |
| 942 | return; |
| 943 | } |
| 944 | String inputId = mHardwareInputIdMap.get(hardwareInfo.getDeviceId()); |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 945 | if (inputId == null) { |
| 946 | return; |
| 947 | } |
| 948 | mHandler.obtainMessage(ListenerHandler.STATE_CHANGED, |
| 949 | convertConnectedToState(event.isConnected()), 0, inputId).sendToTarget(); |
| 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 954 | private final class HdmiDeviceEventListener extends IHdmiDeviceEventListener.Stub { |
| 955 | @Override |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 956 | public void onStatusChanged(HdmiDeviceInfo deviceInfo, int status) { |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 957 | synchronized (mLock) { |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 958 | int messageType = 0; |
| 959 | switch (status) { |
| 960 | case HdmiControlManager.DEVICE_EVENT_ADD_DEVICE: { |
| 961 | if (!mHdmiDeviceList.contains(deviceInfo)) { |
| 962 | mHdmiDeviceList.add(deviceInfo); |
| 963 | } else { |
| 964 | Slog.w(TAG, "The list already contains " + deviceInfo + "; ignoring."); |
| 965 | return; |
| 966 | } |
| 967 | messageType = ListenerHandler.HDMI_DEVICE_ADDED; |
| 968 | break; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 969 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 970 | case HdmiControlManager.DEVICE_EVENT_REMOVE_DEVICE: { |
| 971 | if (!mHdmiDeviceList.remove(deviceInfo)) { |
| 972 | Slog.w(TAG, "The list doesn't contain " + deviceInfo + "; ignoring."); |
| 973 | return; |
| 974 | } |
| 975 | messageType = ListenerHandler.HDMI_DEVICE_REMOVED; |
| 976 | break; |
| 977 | } |
| 978 | case HdmiControlManager.DEVICE_EVENT_UPDATE_DEVICE: { |
| 979 | if (!mHdmiDeviceList.remove(deviceInfo)) { |
| 980 | Slog.w(TAG, "The list doesn't contain " + deviceInfo + "; ignoring."); |
| 981 | return; |
| 982 | } |
| 983 | mHdmiDeviceList.add(deviceInfo); |
| 984 | messageType = ListenerHandler.HDMI_DEVICE_UPDATED; |
| 985 | break; |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 986 | } |
| 987 | } |
Jungshik Jang | 61daf6b | 2014-08-08 11:38:28 +0900 | [diff] [blame] | 988 | |
| 989 | Message msg = mHandler.obtainMessage(messageType, 0, 0, deviceInfo); |
Wonsik Kim | d922a54 | 2014-07-24 18:25:29 +0900 | [diff] [blame] | 990 | if (findHardwareInfoForHdmiPortLocked(deviceInfo.getPortId()) != null) { |
| 991 | msg.sendToTarget(); |
| 992 | } else { |
| 993 | mPendingHdmiDeviceEvents.add(msg); |
| 994 | } |
| 995 | } |
Wonsik Kim | 187423c | 2014-06-25 14:12:48 +0900 | [diff] [blame] | 996 | } |
| 997 | } |
Jinsuk Kim | 7474fac | 2014-07-18 17:22:26 +0900 | [diff] [blame] | 998 | |
Jungshik Jang | 1f819bb | 2014-08-08 10:46:54 +0900 | [diff] [blame] | 999 | private final class HdmiSystemAudioModeChangeListener extends |
| 1000 | IHdmiSystemAudioModeChangeListener.Stub { |
| 1001 | @Override |
| 1002 | public void onStatusChanged(boolean enabled) throws RemoteException { |
| 1003 | synchronized (mLock) { |
| 1004 | for (int i = 0; i < mConnections.size(); ++i) { |
| 1005 | TvInputHardwareImpl impl = mConnections.valueAt(i).getHardwareImplLocked(); |
| 1006 | impl.handleAudioSinkUpdated(); |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1011 | } |