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