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