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