Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 | |
Shubang | 39f285d | 2017-03-21 12:56:08 -0700 | [diff] [blame] | 19 | import android.hardware.tv.input.V1_0.Constants; |
Jae Seo | d5cc4a2 | 2014-05-30 16:57:43 -0700 | [diff] [blame] | 20 | import android.media.tv.TvInputHardwareInfo; |
| 21 | import android.media.tv.TvStreamConfig; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 22 | import android.os.Handler; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 23 | import android.os.Message; |
Wonsik Kim | 57b37f6 | 2014-12-17 11:18:36 +0900 | [diff] [blame] | 24 | import android.os.MessageQueue; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 25 | import android.util.Slog; |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 26 | import android.util.SparseArray; |
| 27 | import android.util.SparseIntArray; |
Jae Seo | 5b1caaf | 2014-08-18 15:03:33 -0700 | [diff] [blame] | 28 | import android.view.Surface; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 29 | |
| 30 | import java.util.LinkedList; |
| 31 | import java.util.Queue; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Provides access to the low-level TV input hardware abstraction layer. |
| 35 | */ |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 36 | final class TvInputHal implements Handler.Callback { |
Jae Seo | ee2ec05 | 2014-09-14 10:30:05 -0700 | [diff] [blame] | 37 | private final static boolean DEBUG = false; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 38 | private final static String TAG = TvInputHal.class.getSimpleName(); |
| 39 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 40 | public final static int SUCCESS = 0; |
| 41 | public final static int ERROR_NO_INIT = -1; |
| 42 | public final static int ERROR_STALE_CONFIG = -2; |
| 43 | public final static int ERROR_UNKNOWN = -3; |
| 44 | |
Shubang | 39f285d | 2017-03-21 12:56:08 -0700 | [diff] [blame] | 45 | public static final int EVENT_DEVICE_AVAILABLE = Constants.EVENT_DEVICE_AVAILABLE; |
| 46 | public static final int EVENT_DEVICE_UNAVAILABLE = Constants.EVENT_DEVICE_UNAVAILABLE; |
| 47 | public static final int EVENT_STREAM_CONFIGURATION_CHANGED = |
| 48 | Constants.EVENT_STREAM_CONFIGURATIONS_CHANGED; |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 49 | public static final int EVENT_FIRST_FRAME_CAPTURED = 4; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 50 | |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 51 | public interface Callback { |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 52 | void onDeviceAvailable(TvInputHardwareInfo info, TvStreamConfig[] configs); |
| 53 | void onDeviceUnavailable(int deviceId); |
| 54 | void onStreamConfigurationChanged(int deviceId, TvStreamConfig[] configs); |
| 55 | void onFirstFrameCaptured(int deviceId, int streamId); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Wonsik Kim | 57b37f6 | 2014-12-17 11:18:36 +0900 | [diff] [blame] | 58 | private native long nativeOpen(MessageQueue queue); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 59 | |
Wonsik Kim | 8f24a8b | 2014-10-22 16:27:39 +0900 | [diff] [blame] | 60 | private static native int nativeAddOrUpdateStream(long ptr, int deviceId, int streamId, |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 61 | Surface surface); |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 62 | private static native int nativeRemoveStream(long ptr, int deviceId, int streamId); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 63 | private static native TvStreamConfig[] nativeGetStreamConfigs(long ptr, int deviceId, |
| 64 | int generation); |
| 65 | private static native void nativeClose(long ptr); |
| 66 | |
Jae Seo | 5b1caaf | 2014-08-18 15:03:33 -0700 | [diff] [blame] | 67 | private final Object mLock = new Object(); |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 68 | private long mPtr = 0; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 69 | private final Callback mCallback; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 70 | private final Handler mHandler; |
Jae Seo | 5b1caaf | 2014-08-18 15:03:33 -0700 | [diff] [blame] | 71 | private final SparseIntArray mStreamConfigGenerations = new SparseIntArray(); |
| 72 | private final SparseArray<TvStreamConfig[]> mStreamConfigs = new SparseArray<>(); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 73 | |
| 74 | public TvInputHal(Callback callback) { |
| 75 | mCallback = callback; |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 76 | mHandler = new Handler(this); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 79 | public void init() { |
| 80 | synchronized (mLock) { |
Wonsik Kim | 57b37f6 | 2014-12-17 11:18:36 +0900 | [diff] [blame] | 81 | mPtr = nativeOpen(mHandler.getLooper().getQueue()); |
Wonsik Kim | 839ae5f | 2014-07-03 19:06:56 +0900 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Wonsik Kim | 8f24a8b | 2014-10-22 16:27:39 +0900 | [diff] [blame] | 85 | public int addOrUpdateStream(int deviceId, Surface surface, TvStreamConfig streamConfig) { |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 86 | synchronized (mLock) { |
| 87 | if (mPtr == 0) { |
| 88 | return ERROR_NO_INIT; |
| 89 | } |
| 90 | int generation = mStreamConfigGenerations.get(deviceId, 0); |
| 91 | if (generation != streamConfig.getGeneration()) { |
| 92 | return ERROR_STALE_CONFIG; |
| 93 | } |
Wonsik Kim | 8f24a8b | 2014-10-22 16:27:39 +0900 | [diff] [blame] | 94 | if (nativeAddOrUpdateStream(mPtr, deviceId, streamConfig.getStreamId(), surface) == 0) { |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 95 | return SUCCESS; |
| 96 | } else { |
| 97 | return ERROR_UNKNOWN; |
| 98 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 102 | public int removeStream(int deviceId, TvStreamConfig streamConfig) { |
| 103 | synchronized (mLock) { |
| 104 | if (mPtr == 0) { |
| 105 | return ERROR_NO_INIT; |
| 106 | } |
| 107 | int generation = mStreamConfigGenerations.get(deviceId, 0); |
| 108 | if (generation != streamConfig.getGeneration()) { |
| 109 | return ERROR_STALE_CONFIG; |
| 110 | } |
| 111 | if (nativeRemoveStream(mPtr, deviceId, streamConfig.getStreamId()) == 0) { |
| 112 | return SUCCESS; |
| 113 | } else { |
| 114 | return ERROR_UNKNOWN; |
| 115 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 119 | public void close() { |
| 120 | synchronized (mLock) { |
Jae Seo | 2a2b299 | 2016-01-12 23:13:14 -0800 | [diff] [blame] | 121 | if (mPtr != 0L) { |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 122 | nativeClose(mPtr); |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | private void retrieveStreamConfigsLocked(int deviceId) { |
| 128 | int generation = mStreamConfigGenerations.get(deviceId, 0) + 1; |
| 129 | mStreamConfigs.put(deviceId, nativeGetStreamConfigs(mPtr, deviceId, generation)); |
| 130 | mStreamConfigGenerations.put(deviceId, generation); |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | // Called from native |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 134 | private void deviceAvailableFromNative(TvInputHardwareInfo info) { |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 135 | if (DEBUG) { |
| 136 | Slog.d(TAG, "deviceAvailableFromNative: info = " + info); |
| 137 | } |
| 138 | mHandler.obtainMessage(EVENT_DEVICE_AVAILABLE, info).sendToTarget(); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | private void deviceUnavailableFromNative(int deviceId) { |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 142 | mHandler.obtainMessage(EVENT_DEVICE_UNAVAILABLE, deviceId, 0).sendToTarget(); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | private void streamConfigsChangedFromNative(int deviceId) { |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 146 | mHandler.obtainMessage(EVENT_STREAM_CONFIGURATION_CHANGED, deviceId, 0).sendToTarget(); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 147 | } |
| 148 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 149 | private void firstFrameCapturedFromNative(int deviceId, int streamId) { |
| 150 | mHandler.sendMessage( |
| 151 | mHandler.obtainMessage(EVENT_STREAM_CONFIGURATION_CHANGED, deviceId, streamId)); |
| 152 | } |
| 153 | |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 154 | // Handler.Callback implementation |
| 155 | |
Jae Seo | 6e4cbfd | 2015-06-21 16:40:34 -0700 | [diff] [blame] | 156 | private final Queue<Message> mPendingMessageQueue = new LinkedList<>(); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 157 | |
| 158 | @Override |
| 159 | public boolean handleMessage(Message msg) { |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 160 | switch (msg.what) { |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 161 | case EVENT_DEVICE_AVAILABLE: { |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 162 | TvStreamConfig[] configs; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 163 | TvInputHardwareInfo info = (TvInputHardwareInfo)msg.obj; |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 164 | synchronized (mLock) { |
| 165 | retrieveStreamConfigsLocked(info.getDeviceId()); |
| 166 | if (DEBUG) { |
| 167 | Slog.d(TAG, "EVENT_DEVICE_AVAILABLE: info = " + info); |
| 168 | } |
| 169 | configs = mStreamConfigs.get(info.getDeviceId()); |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 170 | } |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 171 | mCallback.onDeviceAvailable(info, configs); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 172 | break; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 173 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 174 | |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 175 | case EVENT_DEVICE_UNAVAILABLE: { |
| 176 | int deviceId = msg.arg1; |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 177 | if (DEBUG) { |
| 178 | Slog.d(TAG, "EVENT_DEVICE_UNAVAILABLE: deviceId = " + deviceId); |
| 179 | } |
Wonsik Kim | d7c2918 | 2014-05-27 10:38:21 +0900 | [diff] [blame] | 180 | mCallback.onDeviceUnavailable(deviceId); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 181 | break; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 182 | } |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 183 | |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 184 | case EVENT_STREAM_CONFIGURATION_CHANGED: { |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 185 | TvStreamConfig[] configs; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 186 | int deviceId = msg.arg1; |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 187 | synchronized (mLock) { |
| 188 | if (DEBUG) { |
| 189 | Slog.d(TAG, "EVENT_STREAM_CONFIGURATION_CHANGED: deviceId = " + deviceId); |
| 190 | } |
| 191 | retrieveStreamConfigsLocked(deviceId); |
| 192 | configs = mStreamConfigs.get(deviceId); |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 193 | } |
Wonsik Kim | 21aa346 | 2014-07-29 16:39:00 +0900 | [diff] [blame] | 194 | mCallback.onStreamConfigurationChanged(deviceId, configs); |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 195 | break; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 196 | } |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 197 | |
Terry Heo | c086a3d | 2014-06-18 14:26:44 +0900 | [diff] [blame] | 198 | case EVENT_FIRST_FRAME_CAPTURED: { |
| 199 | int deviceId = msg.arg1; |
| 200 | int streamId = msg.arg2; |
| 201 | mCallback.onFirstFrameCaptured(deviceId, streamId); |
| 202 | break; |
| 203 | } |
| 204 | |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 205 | default: |
| 206 | Slog.e(TAG, "Unknown event: " + msg); |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 207 | return false; |
Wonsik Kim | 9e922ca | 2014-06-20 17:27:32 +0900 | [diff] [blame] | 208 | } |
Wonsik Kim | 610ccd9 | 2014-07-19 18:49:49 +0900 | [diff] [blame] | 209 | |
| 210 | return true; |
Wonsik Kim | c22dbb6 | 2014-05-26 02:26:04 +0000 | [diff] [blame] | 211 | } |
| 212 | } |