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 #define LOG_TAG "TvInputHal"
18
19 //#define LOG_NDEBUG 0
20
21 #include "android_os_MessageQueue.h"
22 #include "android_runtime/AndroidRuntime.h"
23 #include "android_runtime/android_view_Surface.h"
24 #include <nativehelper/JNIHelp.h>
25 #include "jni.h"
26
27 #include <android/hardware/tv/input/1.0/ITvInputCallback.h>
28 #include <android/hardware/tv/input/1.0/ITvInput.h>
29 #include <android/hardware/tv/input/1.0/types.h>
30 #include <gui/Surface.h>
31 #include <utils/Errors.h>
32 #include <utils/KeyedVector.h>
33 #include <utils/Log.h>
34 #include <utils/Looper.h>
35 #include <utils/NativeHandle.h>
36 #include <hardware/tv_input.h>
37
38 using ::android::hardware::audio::common::V2_0::AudioDevice;
39 using ::android::hardware::tv::input::V1_0::ITvInput;
40 using ::android::hardware::tv::input::V1_0::ITvInputCallback;
41 using ::android::hardware::tv::input::V1_0::Result;
42 using ::android::hardware::tv::input::V1_0::TvInputDeviceInfo;
43 using ::android::hardware::tv::input::V1_0::TvInputEvent;
44 using ::android::hardware::tv::input::V1_0::TvInputEventType;
45 using ::android::hardware::tv::input::V1_0::TvInputType;
46 using ::android::hardware::tv::input::V1_0::TvStreamConfig;
47 using ::android::hardware::Return;
48 using ::android::hardware::Void;
49 using ::android::hardware::hidl_vec;
50 using ::android::hardware::hidl_string;
51
52 namespace android {
53
54 static struct {
55 jmethodID deviceAvailable;
56 jmethodID deviceUnavailable;
57 jmethodID streamConfigsChanged;
58 jmethodID firstFrameCaptured;
59 } gTvInputHalClassInfo;
60
61 static struct {
62 jclass clazz;
63 } gTvStreamConfigClassInfo;
64
65 static struct {
66 jclass clazz;
67
68 jmethodID constructor;
69 jmethodID streamId;
70 jmethodID type;
71 jmethodID maxWidth;
72 jmethodID maxHeight;
73 jmethodID generation;
74 jmethodID build;
75 } gTvStreamConfigBuilderClassInfo;
76
77 static struct {
78 jclass clazz;
79
80 jmethodID constructor;
81 jmethodID deviceId;
82 jmethodID type;
83 jmethodID hdmiPortId;
84 jmethodID cableConnectionStatus;
85 jmethodID audioType;
86 jmethodID audioAddress;
87 jmethodID build;
88 } gTvInputHardwareInfoBuilderClassInfo;
89
90 ////////////////////////////////////////////////////////////////////////////////
91
92 class BufferProducerThread : public Thread {
93 public:
94 BufferProducerThread(tv_input_device_t* device, int deviceId, const tv_stream_t* stream);
95
96 virtual status_t readyToRun();
97
98 void setSurface(const sp<Surface>& surface);
99 void onCaptured(uint32_t seq, bool succeeded);
100 void shutdown();
101
102 private:
103 Mutex mLock;
104 Condition mCondition;
105 sp<Surface> mSurface;
106 tv_input_device_t* mDevice;
107 int mDeviceId;
108 tv_stream_t mStream;
109 sp<ANativeWindowBuffer_t> mBuffer;
110 enum {
111 CAPTURING,
112 CAPTURED,
113 RELEASED,
114 } mBufferState;
115 uint32_t mSeq;
116 bool mShutdown;
117
118 virtual bool threadLoop();
119
120 void setSurfaceLocked(const sp<Surface>& surface);
121 };
122
BufferProducerThread(tv_input_device_t * device,int deviceId,const tv_stream_t * stream)123 BufferProducerThread::BufferProducerThread(
124 tv_input_device_t* device, int deviceId, const tv_stream_t* stream)
125 : Thread(false),
126 mDevice(device),
127 mDeviceId(deviceId),
128 mBuffer(NULL),
129 mBufferState(RELEASED),
130 mSeq(0u),
131 mShutdown(false) {
132 memcpy(&mStream, stream, sizeof(mStream));
133 }
134
readyToRun()135 status_t BufferProducerThread::readyToRun() {
136 sp<ANativeWindow> anw(mSurface);
137 status_t err = native_window_set_usage(anw.get(), mStream.buffer_producer.usage);
138 if (err != NO_ERROR) {
139 return err;
140 }
141 err = native_window_set_buffers_dimensions(
142 anw.get(), mStream.buffer_producer.width, mStream.buffer_producer.height);
143 if (err != NO_ERROR) {
144 return err;
145 }
146 err = native_window_set_buffers_format(anw.get(), mStream.buffer_producer.format);
147 if (err != NO_ERROR) {
148 return err;
149 }
150 return NO_ERROR;
151 }
152
setSurface(const sp<Surface> & surface)153 void BufferProducerThread::setSurface(const sp<Surface>& surface) {
154 Mutex::Autolock autoLock(&mLock);
155 setSurfaceLocked(surface);
156 }
157
setSurfaceLocked(const sp<Surface> & surface)158 void BufferProducerThread::setSurfaceLocked(const sp<Surface>& surface) {
159 if (surface == mSurface) {
160 return;
161 }
162
163 if (mBufferState == CAPTURING) {
164 mDevice->cancel_capture(mDevice, mDeviceId, mStream.stream_id, mSeq);
165 }
166 while (mBufferState == CAPTURING) {
167 status_t err = mCondition.waitRelative(mLock, s2ns(1));
168 if (err != NO_ERROR) {
169 ALOGE("error %d while wating for buffer state to change.", err);
170 break;
171 }
172 }
173 mBuffer.clear();
174 mBufferState = RELEASED;
175
176 mSurface = surface;
177 mCondition.broadcast();
178 }
179
onCaptured(uint32_t seq,bool succeeded)180 void BufferProducerThread::onCaptured(uint32_t seq, bool succeeded) {
181 Mutex::Autolock autoLock(&mLock);
182 if (seq != mSeq) {
183 ALOGW("Incorrect sequence value: expected %u actual %u", mSeq, seq);
184 }
185 if (mBufferState != CAPTURING) {
186 ALOGW("mBufferState != CAPTURING : instead %d", mBufferState);
187 }
188 if (succeeded) {
189 mBufferState = CAPTURED;
190 } else {
191 mBuffer.clear();
192 mBufferState = RELEASED;
193 }
194 mCondition.broadcast();
195 }
196
shutdown()197 void BufferProducerThread::shutdown() {
198 Mutex::Autolock autoLock(&mLock);
199 mShutdown = true;
200 setSurfaceLocked(NULL);
201 requestExitAndWait();
202 }
203
threadLoop()204 bool BufferProducerThread::threadLoop() {
205 Mutex::Autolock autoLock(&mLock);
206
207 status_t err = NO_ERROR;
208 if (mSurface == NULL) {
209 err = mCondition.waitRelative(mLock, s2ns(1));
210 // It's OK to time out here.
211 if (err != NO_ERROR && err != TIMED_OUT) {
212 ALOGE("error %d while wating for non-null surface to be set", err);
213 return false;
214 }
215 return true;
216 }
217 sp<ANativeWindow> anw(mSurface);
218 while (mBufferState == CAPTURING) {
219 err = mCondition.waitRelative(mLock, s2ns(1));
220 if (err != NO_ERROR) {
221 ALOGE("error %d while wating for buffer state to change.", err);
222 return false;
223 }
224 }
225 if (mBufferState == CAPTURED && anw != NULL) {
226 err = anw->queueBuffer(anw.get(), mBuffer.get(), -1);
227 if (err != NO_ERROR) {
228 ALOGE("error %d while queueing buffer to surface", err);
229 return false;
230 }
231 mBuffer.clear();
232 mBufferState = RELEASED;
233 }
234 if (mBuffer == NULL && !mShutdown && anw != NULL) {
235 ANativeWindowBuffer_t* buffer = NULL;
236 err = native_window_dequeue_buffer_and_wait(anw.get(), &buffer);
237 if (err != NO_ERROR) {
238 ALOGE("error %d while dequeueing buffer to surface", err);
239 return false;
240 }
241 mBuffer = buffer;
242 mBufferState = CAPTURING;
243 mDevice->request_capture(mDevice, mDeviceId, mStream.stream_id,
244 buffer->handle, ++mSeq);
245 }
246
247 return true;
248 }
249
250 ////////////////////////////////////////////////////////////////////////////////
251
252 class JTvInputHal {
253 public:
254 ~JTvInputHal();
255
256 static JTvInputHal* createInstance(JNIEnv* env, jobject thiz, const sp<Looper>& looper);
257
258 int addOrUpdateStream(int deviceId, int streamId, const sp<Surface>& surface);
259 int removeStream(int deviceId, int streamId);
260 const hidl_vec<TvStreamConfig> getStreamConfigs(int deviceId);
261
262 void onDeviceAvailable(const TvInputDeviceInfo& info);
263 void onDeviceUnavailable(int deviceId);
264 void onStreamConfigurationsChanged(int deviceId);
265 void onCaptured(int deviceId, int streamId, uint32_t seq, bool succeeded);
266
267 private:
268 // Connection between a surface and a stream.
269 class Connection {
270 public:
Connection()271 Connection() {}
272
273 sp<Surface> mSurface;
274 tv_stream_type_t mStreamType;
275
276 // Only valid when mStreamType == TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE
277 sp<NativeHandle> mSourceHandle;
278 // Only valid when mStreamType == TV_STREAM_TYPE_BUFFER_PRODUCER
279 sp<BufferProducerThread> mThread;
280 };
281
282 class NotifyHandler : public MessageHandler {
283 public:
284 NotifyHandler(JTvInputHal* hal, const TvInputEvent& event);
285
286 virtual void handleMessage(const Message& message);
287
288 private:
289 TvInputEvent mEvent;
290 JTvInputHal* mHal;
291 };
292
293 class TvInputCallback : public ITvInputCallback {
294 public:
295 explicit TvInputCallback(JTvInputHal* hal);
296 Return<void> notify(const TvInputEvent& event) override;
297 private:
298 JTvInputHal* mHal;
299 };
300
301 JTvInputHal(JNIEnv* env, jobject thiz, sp<ITvInput> tvInput, const sp<Looper>& looper);
302
303 Mutex mLock;
304 Mutex mStreamLock;
305 jweak mThiz;
306 sp<Looper> mLooper;
307
308 KeyedVector<int, KeyedVector<int, Connection> > mConnections;
309
310 sp<ITvInput> mTvInput;
311 sp<ITvInputCallback> mTvInputCallback;
312 };
313
JTvInputHal(JNIEnv * env,jobject thiz,sp<ITvInput> tvInput,const sp<Looper> & looper)314 JTvInputHal::JTvInputHal(JNIEnv* env, jobject thiz, sp<ITvInput> tvInput,
315 const sp<Looper>& looper) {
316 mThiz = env->NewWeakGlobalRef(thiz);
317 mTvInput = tvInput;
318 mLooper = looper;
319 mTvInputCallback = new TvInputCallback(this);
320 mTvInput->setCallback(mTvInputCallback);
321 }
322
~JTvInputHal()323 JTvInputHal::~JTvInputHal() {
324 mTvInput->setCallback(nullptr);
325 JNIEnv* env = AndroidRuntime::getJNIEnv();
326 env->DeleteWeakGlobalRef(mThiz);
327 mThiz = NULL;
328 }
329
createInstance(JNIEnv * env,jobject thiz,const sp<Looper> & looper)330 JTvInputHal* JTvInputHal::createInstance(JNIEnv* env, jobject thiz, const sp<Looper>& looper) {
331 // TODO(b/31632518)
332 sp<ITvInput> tvInput = ITvInput::getService();
333 if (tvInput == nullptr) {
334 ALOGE("Couldn't get tv.input service.");
335 return nullptr;
336 }
337
338 return new JTvInputHal(env, thiz, tvInput, looper);
339 }
340
addOrUpdateStream(int deviceId,int streamId,const sp<Surface> & surface)341 int JTvInputHal::addOrUpdateStream(int deviceId, int streamId, const sp<Surface>& surface) {
342 Mutex::Autolock autoLock(&mStreamLock);
343 KeyedVector<int, Connection>& connections = mConnections.editValueFor(deviceId);
344 if (connections.indexOfKey(streamId) < 0) {
345 connections.add(streamId, Connection());
346 }
347 Connection& connection = connections.editValueFor(streamId);
348 if (connection.mSurface == surface) {
349 // Nothing to do
350 return NO_ERROR;
351 }
352 // Clear the surface in the connection.
353 if (connection.mSurface != NULL) {
354 if (connection.mStreamType == TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE) {
355 if (Surface::isValid(connection.mSurface)) {
356 connection.mSurface->setSidebandStream(NULL);
357 }
358 }
359 connection.mSurface.clear();
360 }
361 if (connection.mSourceHandle == NULL && connection.mThread == NULL) {
362 // Need to configure stream
363 Result result = Result::UNKNOWN;
364 hidl_vec<TvStreamConfig> list;
365 mTvInput->getStreamConfigurations(deviceId,
366 [&result, &list](Result res, hidl_vec<TvStreamConfig> configs) {
367 result = res;
368 if (res == Result::OK) {
369 list = configs;
370 }
371 });
372 if (result != Result::OK) {
373 ALOGE("Couldn't get stream configs for device id:%d result:%d", deviceId, result);
374 return UNKNOWN_ERROR;
375 }
376 int configIndex = -1;
377 for (size_t i = 0; i < list.size(); ++i) {
378 if (list[i].streamId == streamId) {
379 configIndex = i;
380 break;
381 }
382 }
383 if (configIndex == -1) {
384 ALOGE("Cannot find a config with given stream ID: %d", streamId);
385 return BAD_VALUE;
386 }
387 connection.mStreamType = TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE;
388
389 result = Result::UNKNOWN;
390 const native_handle_t* sidebandStream;
391 mTvInput->openStream(deviceId, streamId,
392 [&result, &sidebandStream](Result res, const native_handle_t* handle) {
393 result = res;
394 if (res == Result::OK) {
395 if (handle) {
396 sidebandStream = native_handle_clone(handle);
397 } else {
398 result = Result::UNKNOWN;
399 }
400 }
401 });
402 if (result != Result::OK) {
403 ALOGE("Couldn't open stream. device id:%d stream id:%d result:%d", deviceId, streamId,
404 result);
405 return UNKNOWN_ERROR;
406 }
407 connection.mSourceHandle = NativeHandle::create((native_handle_t*)sidebandStream, true);
408 }
409 connection.mSurface = surface;
410 if (connection.mSurface != nullptr) {
411 connection.mSurface->setSidebandStream(connection.mSourceHandle);
412 }
413 return NO_ERROR;
414 }
415
removeStream(int deviceId,int streamId)416 int JTvInputHal::removeStream(int deviceId, int streamId) {
417 Mutex::Autolock autoLock(&mStreamLock);
418 KeyedVector<int, Connection>& connections = mConnections.editValueFor(deviceId);
419 if (connections.indexOfKey(streamId) < 0) {
420 return BAD_VALUE;
421 }
422 Connection& connection = connections.editValueFor(streamId);
423 if (connection.mSurface == NULL) {
424 // Nothing to do
425 return NO_ERROR;
426 }
427 if (Surface::isValid(connection.mSurface)) {
428 connection.mSurface->setSidebandStream(NULL);
429 }
430 connection.mSurface.clear();
431 if (connection.mThread != NULL) {
432 connection.mThread->shutdown();
433 connection.mThread.clear();
434 }
435 if (mTvInput->closeStream(deviceId, streamId) != Result::OK) {
436 ALOGE("Couldn't close stream. device id:%d stream id:%d", deviceId, streamId);
437 return BAD_VALUE;
438 }
439 if (connection.mSourceHandle != NULL) {
440 connection.mSourceHandle.clear();
441 }
442 return NO_ERROR;
443 }
444
getStreamConfigs(int deviceId)445 const hidl_vec<TvStreamConfig> JTvInputHal::getStreamConfigs(int deviceId) {
446 Result result = Result::UNKNOWN;
447 hidl_vec<TvStreamConfig> list;
448 mTvInput->getStreamConfigurations(deviceId,
449 [&result, &list](Result res, hidl_vec<TvStreamConfig> configs) {
450 result = res;
451 if (res == Result::OK) {
452 list = configs;
453 }
454 });
455 if (result != Result::OK) {
456 ALOGE("Couldn't get stream configs for device id:%d result:%d", deviceId, result);
457 }
458 return list;
459 }
460
onDeviceAvailable(const TvInputDeviceInfo & info)461 void JTvInputHal::onDeviceAvailable(const TvInputDeviceInfo& info) {
462 {
463 Mutex::Autolock autoLock(&mLock);
464 mConnections.add(info.deviceId, KeyedVector<int, Connection>());
465 }
466 JNIEnv* env = AndroidRuntime::getJNIEnv();
467
468 jobject builder = env->NewObject(
469 gTvInputHardwareInfoBuilderClassInfo.clazz,
470 gTvInputHardwareInfoBuilderClassInfo.constructor);
471 env->CallObjectMethod(
472 builder, gTvInputHardwareInfoBuilderClassInfo.deviceId, info.deviceId);
473 env->CallObjectMethod(
474 builder, gTvInputHardwareInfoBuilderClassInfo.type, info.type);
475 if (info.type == TvInputType::HDMI) {
476 env->CallObjectMethod(
477 builder, gTvInputHardwareInfoBuilderClassInfo.hdmiPortId, info.portId);
478 }
479 env->CallObjectMethod(
480 builder, gTvInputHardwareInfoBuilderClassInfo.cableConnectionStatus,
481 info.cableConnectionStatus);
482 env->CallObjectMethod(
483 builder, gTvInputHardwareInfoBuilderClassInfo.audioType, info.audioType);
484 if (info.audioType != AudioDevice::NONE) {
485 uint8_t buffer[info.audioAddress.size() + 1];
486 memcpy(buffer, info.audioAddress.data(), info.audioAddress.size());
487 buffer[info.audioAddress.size()] = '\0';
488 jstring audioAddress = env->NewStringUTF(reinterpret_cast<const char *>(buffer));
489 env->CallObjectMethod(
490 builder, gTvInputHardwareInfoBuilderClassInfo.audioAddress, audioAddress);
491 env->DeleteLocalRef(audioAddress);
492 }
493
494 jobject infoObject = env->CallObjectMethod(builder, gTvInputHardwareInfoBuilderClassInfo.build);
495
496 env->CallVoidMethod(
497 mThiz,
498 gTvInputHalClassInfo.deviceAvailable,
499 infoObject);
500
501 env->DeleteLocalRef(builder);
502 env->DeleteLocalRef(infoObject);
503 }
504
onDeviceUnavailable(int deviceId)505 void JTvInputHal::onDeviceUnavailable(int deviceId) {
506 {
507 Mutex::Autolock autoLock(&mLock);
508 KeyedVector<int, Connection>& connections = mConnections.editValueFor(deviceId);
509 for (size_t i = 0; i < connections.size(); ++i) {
510 removeStream(deviceId, connections.keyAt(i));
511 }
512 connections.clear();
513 mConnections.removeItem(deviceId);
514 }
515 JNIEnv* env = AndroidRuntime::getJNIEnv();
516 env->CallVoidMethod(
517 mThiz,
518 gTvInputHalClassInfo.deviceUnavailable,
519 deviceId);
520 }
521
onStreamConfigurationsChanged(int deviceId)522 void JTvInputHal::onStreamConfigurationsChanged(int deviceId) {
523 {
524 Mutex::Autolock autoLock(&mLock);
525 KeyedVector<int, Connection>& connections = mConnections.editValueFor(deviceId);
526 for (size_t i = 0; i < connections.size(); ++i) {
527 removeStream(deviceId, connections.keyAt(i));
528 }
529 connections.clear();
530 }
531 JNIEnv* env = AndroidRuntime::getJNIEnv();
532 env->CallVoidMethod(
533 mThiz,
534 gTvInputHalClassInfo.streamConfigsChanged,
535 deviceId);
536 }
537
onCaptured(int deviceId,int streamId,uint32_t seq,bool succeeded)538 void JTvInputHal::onCaptured(int deviceId, int streamId, uint32_t seq, bool succeeded) {
539 sp<BufferProducerThread> thread;
540 {
541 Mutex::Autolock autoLock(&mLock);
542 KeyedVector<int, Connection>& connections = mConnections.editValueFor(deviceId);
543 Connection& connection = connections.editValueFor(streamId);
544 if (connection.mThread == NULL) {
545 ALOGE("capture thread not existing.");
546 return;
547 }
548 thread = connection.mThread;
549 }
550 thread->onCaptured(seq, succeeded);
551 if (seq == 0) {
552 JNIEnv* env = AndroidRuntime::getJNIEnv();
553 env->CallVoidMethod(
554 mThiz,
555 gTvInputHalClassInfo.firstFrameCaptured,
556 deviceId,
557 streamId);
558 }
559 }
560
NotifyHandler(JTvInputHal * hal,const TvInputEvent & event)561 JTvInputHal::NotifyHandler::NotifyHandler(JTvInputHal* hal, const TvInputEvent& event) {
562 mHal = hal;
563 mEvent = event;
564 }
565
handleMessage(const Message & message)566 void JTvInputHal::NotifyHandler::handleMessage(const Message& message) {
567 switch (mEvent.type) {
568 case TvInputEventType::DEVICE_AVAILABLE: {
569 mHal->onDeviceAvailable(mEvent.deviceInfo);
570 } break;
571 case TvInputEventType::DEVICE_UNAVAILABLE: {
572 mHal->onDeviceUnavailable(mEvent.deviceInfo.deviceId);
573 } break;
574 case TvInputEventType::STREAM_CONFIGURATIONS_CHANGED: {
575 mHal->onStreamConfigurationsChanged(mEvent.deviceInfo.deviceId);
576 } break;
577 default:
578 ALOGE("Unrecognizable event");
579 }
580 }
581
TvInputCallback(JTvInputHal * hal)582 JTvInputHal::TvInputCallback::TvInputCallback(JTvInputHal* hal) {
583 mHal = hal;
584 }
585
notify(const TvInputEvent & event)586 Return<void> JTvInputHal::TvInputCallback::notify(const TvInputEvent& event) {
587 mHal->mLooper->sendMessage(new NotifyHandler(mHal, event), static_cast<int>(event.type));
588 return Void();
589 }
590
591 ////////////////////////////////////////////////////////////////////////////////
592
nativeOpen(JNIEnv * env,jobject thiz,jobject messageQueueObj)593 static jlong nativeOpen(JNIEnv* env, jobject thiz, jobject messageQueueObj) {
594 sp<MessageQueue> messageQueue =
595 android_os_MessageQueue_getMessageQueue(env, messageQueueObj);
596 return (jlong)JTvInputHal::createInstance(env, thiz, messageQueue->getLooper());
597 }
598
nativeAddOrUpdateStream(JNIEnv * env,jclass clazz,jlong ptr,jint deviceId,jint streamId,jobject jsurface)599 static int nativeAddOrUpdateStream(JNIEnv* env, jclass clazz,
600 jlong ptr, jint deviceId, jint streamId, jobject jsurface) {
601 JTvInputHal* tvInputHal = (JTvInputHal*)ptr;
602 if (!jsurface) {
603 return BAD_VALUE;
604 }
605 sp<Surface> surface(android_view_Surface_getSurface(env, jsurface));
606 if (!Surface::isValid(surface)) {
607 return BAD_VALUE;
608 }
609 return tvInputHal->addOrUpdateStream(deviceId, streamId, surface);
610 }
611
nativeRemoveStream(JNIEnv * env,jclass clazz,jlong ptr,jint deviceId,jint streamId)612 static int nativeRemoveStream(JNIEnv* env, jclass clazz,
613 jlong ptr, jint deviceId, jint streamId) {
614 JTvInputHal* tvInputHal = (JTvInputHal*)ptr;
615 return tvInputHal->removeStream(deviceId, streamId);
616 }
617
nativeGetStreamConfigs(JNIEnv * env,jclass clazz,jlong ptr,jint deviceId,jint generation)618 static jobjectArray nativeGetStreamConfigs(JNIEnv* env, jclass clazz,
619 jlong ptr, jint deviceId, jint generation) {
620 JTvInputHal* tvInputHal = (JTvInputHal*)ptr;
621 const hidl_vec<TvStreamConfig> configs = tvInputHal->getStreamConfigs(deviceId);
622
623 jobjectArray result = env->NewObjectArray(configs.size(), gTvStreamConfigClassInfo.clazz, NULL);
624 for (size_t i = 0; i < configs.size(); ++i) {
625 jobject builder = env->NewObject(
626 gTvStreamConfigBuilderClassInfo.clazz,
627 gTvStreamConfigBuilderClassInfo.constructor);
628 env->CallObjectMethod(
629 builder, gTvStreamConfigBuilderClassInfo.streamId, configs[i].streamId);
630 env->CallObjectMethod(
631 builder, gTvStreamConfigBuilderClassInfo.type,
632 TV_STREAM_TYPE_INDEPENDENT_VIDEO_SOURCE);
633 env->CallObjectMethod(
634 builder, gTvStreamConfigBuilderClassInfo.maxWidth, configs[i].maxVideoWidth);
635 env->CallObjectMethod(
636 builder, gTvStreamConfigBuilderClassInfo.maxHeight, configs[i].maxVideoHeight);
637 env->CallObjectMethod(
638 builder, gTvStreamConfigBuilderClassInfo.generation, generation);
639
640 jobject config = env->CallObjectMethod(builder, gTvStreamConfigBuilderClassInfo.build);
641
642 env->SetObjectArrayElement(result, i, config);
643
644 env->DeleteLocalRef(config);
645 env->DeleteLocalRef(builder);
646 }
647 return result;
648 }
649
nativeClose(JNIEnv * env,jclass clazz,jlong ptr)650 static void nativeClose(JNIEnv* env, jclass clazz, jlong ptr) {
651 JTvInputHal* tvInputHal = (JTvInputHal*)ptr;
652 delete tvInputHal;
653 }
654
655 static const JNINativeMethod gTvInputHalMethods[] = {
656 /* name, signature, funcPtr */
657 { "nativeOpen", "(Landroid/os/MessageQueue;)J",
658 (void*) nativeOpen },
659 { "nativeAddOrUpdateStream", "(JIILandroid/view/Surface;)I",
660 (void*) nativeAddOrUpdateStream },
661 { "nativeRemoveStream", "(JII)I",
662 (void*) nativeRemoveStream },
663 { "nativeGetStreamConfigs", "(JII)[Landroid/media/tv/TvStreamConfig;",
664 (void*) nativeGetStreamConfigs },
665 { "nativeClose", "(J)V",
666 (void*) nativeClose },
667 };
668
669 #define FIND_CLASS(var, className) \
670 var = env->FindClass(className); \
671 LOG_FATAL_IF(! (var), "Unable to find class " className)
672
673 #define GET_METHOD_ID(var, clazz, methodName, fieldDescriptor) \
674 var = env->GetMethodID(clazz, methodName, fieldDescriptor); \
675 LOG_FATAL_IF(! (var), "Unable to find method" methodName)
676
register_android_server_tv_TvInputHal(JNIEnv * env)677 int register_android_server_tv_TvInputHal(JNIEnv* env) {
678 int res = jniRegisterNativeMethods(env, "com/android/server/tv/TvInputHal",
679 gTvInputHalMethods, NELEM(gTvInputHalMethods));
680 LOG_FATAL_IF(res < 0, "Unable to register native methods.");
681 (void)res; // Don't complain about unused variable in the LOG_NDEBUG case
682
683 jclass clazz;
684 FIND_CLASS(clazz, "com/android/server/tv/TvInputHal");
685
686 GET_METHOD_ID(
687 gTvInputHalClassInfo.deviceAvailable, clazz,
688 "deviceAvailableFromNative", "(Landroid/media/tv/TvInputHardwareInfo;)V");
689 GET_METHOD_ID(
690 gTvInputHalClassInfo.deviceUnavailable, clazz, "deviceUnavailableFromNative", "(I)V");
691 GET_METHOD_ID(
692 gTvInputHalClassInfo.streamConfigsChanged, clazz,
693 "streamConfigsChangedFromNative", "(I)V");
694 GET_METHOD_ID(
695 gTvInputHalClassInfo.firstFrameCaptured, clazz,
696 "firstFrameCapturedFromNative", "(II)V");
697
698 FIND_CLASS(gTvStreamConfigClassInfo.clazz, "android/media/tv/TvStreamConfig");
699 gTvStreamConfigClassInfo.clazz = jclass(env->NewGlobalRef(gTvStreamConfigClassInfo.clazz));
700
701 FIND_CLASS(gTvStreamConfigBuilderClassInfo.clazz, "android/media/tv/TvStreamConfig$Builder");
702 gTvStreamConfigBuilderClassInfo.clazz =
703 jclass(env->NewGlobalRef(gTvStreamConfigBuilderClassInfo.clazz));
704
705 GET_METHOD_ID(
706 gTvStreamConfigBuilderClassInfo.constructor,
707 gTvStreamConfigBuilderClassInfo.clazz,
708 "<init>", "()V");
709 GET_METHOD_ID(
710 gTvStreamConfigBuilderClassInfo.streamId,
711 gTvStreamConfigBuilderClassInfo.clazz,
712 "streamId", "(I)Landroid/media/tv/TvStreamConfig$Builder;");
713 GET_METHOD_ID(
714 gTvStreamConfigBuilderClassInfo.type,
715 gTvStreamConfigBuilderClassInfo.clazz,
716 "type", "(I)Landroid/media/tv/TvStreamConfig$Builder;");
717 GET_METHOD_ID(
718 gTvStreamConfigBuilderClassInfo.maxWidth,
719 gTvStreamConfigBuilderClassInfo.clazz,
720 "maxWidth", "(I)Landroid/media/tv/TvStreamConfig$Builder;");
721 GET_METHOD_ID(
722 gTvStreamConfigBuilderClassInfo.maxHeight,
723 gTvStreamConfigBuilderClassInfo.clazz,
724 "maxHeight", "(I)Landroid/media/tv/TvStreamConfig$Builder;");
725 GET_METHOD_ID(
726 gTvStreamConfigBuilderClassInfo.generation,
727 gTvStreamConfigBuilderClassInfo.clazz,
728 "generation", "(I)Landroid/media/tv/TvStreamConfig$Builder;");
729 GET_METHOD_ID(
730 gTvStreamConfigBuilderClassInfo.build,
731 gTvStreamConfigBuilderClassInfo.clazz,
732 "build", "()Landroid/media/tv/TvStreamConfig;");
733
734 FIND_CLASS(gTvInputHardwareInfoBuilderClassInfo.clazz,
735 "android/media/tv/TvInputHardwareInfo$Builder");
736 gTvInputHardwareInfoBuilderClassInfo.clazz =
737 jclass(env->NewGlobalRef(gTvInputHardwareInfoBuilderClassInfo.clazz));
738
739 GET_METHOD_ID(
740 gTvInputHardwareInfoBuilderClassInfo.constructor,
741 gTvInputHardwareInfoBuilderClassInfo.clazz,
742 "<init>", "()V");
743 GET_METHOD_ID(
744 gTvInputHardwareInfoBuilderClassInfo.deviceId,
745 gTvInputHardwareInfoBuilderClassInfo.clazz,
746 "deviceId", "(I)Landroid/media/tv/TvInputHardwareInfo$Builder;");
747 GET_METHOD_ID(
748 gTvInputHardwareInfoBuilderClassInfo.type,
749 gTvInputHardwareInfoBuilderClassInfo.clazz,
750 "type", "(I)Landroid/media/tv/TvInputHardwareInfo$Builder;");
751 GET_METHOD_ID(
752 gTvInputHardwareInfoBuilderClassInfo.hdmiPortId,
753 gTvInputHardwareInfoBuilderClassInfo.clazz,
754 "hdmiPortId", "(I)Landroid/media/tv/TvInputHardwareInfo$Builder;");
755 GET_METHOD_ID(
756 gTvInputHardwareInfoBuilderClassInfo.cableConnectionStatus,
757 gTvInputHardwareInfoBuilderClassInfo.clazz,
758 "cableConnectionStatus", "(I)Landroid/media/tv/TvInputHardwareInfo$Builder;");
759 GET_METHOD_ID(
760 gTvInputHardwareInfoBuilderClassInfo.audioType,
761 gTvInputHardwareInfoBuilderClassInfo.clazz,
762 "audioType", "(I)Landroid/media/tv/TvInputHardwareInfo$Builder;");
763 GET_METHOD_ID(
764 gTvInputHardwareInfoBuilderClassInfo.audioAddress,
765 gTvInputHardwareInfoBuilderClassInfo.clazz,
766 "audioAddress", "(Ljava/lang/String;)Landroid/media/tv/TvInputHardwareInfo$Builder;");
767 GET_METHOD_ID(
768 gTvInputHardwareInfoBuilderClassInfo.build,
769 gTvInputHardwareInfoBuilderClassInfo.clazz,
770 "build", "()Landroid/media/tv/TvInputHardwareInfo;");
771
772 return 0;
773 }
774
775 } /* namespace android */
776