Fix NativeHandle creation in TV Input HAL 2.0 default implementation

Bug: 227673740
Test: atest VtsHalTvInputTargetTest
Change-Id: Ic2958ab08c14848d46054c8422ce4298903bf206
diff --git a/tv/input/aidl/default/TvInput.cpp b/tv/input/aidl/default/TvInput.cpp
index ed12cbc..255bef7 100644
--- a/tv/input/aidl/default/TvInput.cpp
+++ b/tv/input/aidl/default/TvInput.cpp
@@ -95,8 +95,8 @@
         return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
     }
     mStreamConfigs[in_deviceId][in_streamId]->handle = createNativeHandle(in_streamId);
-    mStreamConfigs[in_deviceId][in_streamId]->isOpen = true;
     *_aidl_return = makeToAidl(mStreamConfigs[in_deviceId][in_streamId]->handle);
+    mStreamConfigs[in_deviceId][in_streamId]->isOpen = true;
     return ::ndk::ScopedAStatus::ok();
 }
 
@@ -112,27 +112,21 @@
         ALOGW("Stream with device id %d, stream id %d is already closed", in_deviceId, in_streamId);
         return ::ndk::ScopedAStatus::fromServiceSpecificError(STATUS_INVALID_STATE);
     }
-    releaseNativeHandle(mStreamConfigs[in_deviceId][in_streamId]->handle);
+    native_handle_delete(mStreamConfigs[in_deviceId][in_streamId]->handle);
     mStreamConfigs[in_deviceId][in_streamId]->handle = nullptr;
     mStreamConfigs[in_deviceId][in_streamId]->isOpen = false;
     return ::ndk::ScopedAStatus::ok();
 }
 
 native_handle_t* TvInput::createNativeHandle(int fd) {
-    native_handle_t* nativeHandle = native_handle_create(1, 0);
-    if (nativeHandle == nullptr) {
+    native_handle_t* handle = native_handle_create(1, 1);
+    if (handle == nullptr) {
         ALOGE("[TVInput] Failed to create native_handle %d", errno);
         return nullptr;
     }
-    if (nativeHandle->numFds > 0) {
-        nativeHandle->data[0] = dup(fd);
-    }
-    return nativeHandle;
-}
-
-void TvInput::releaseNativeHandle(native_handle_t* handle) {
-    native_handle_close(handle);
-    native_handle_delete(handle);
+    handle->data[0] = dup(0);
+    handle->data[1] = fd;
+    return handle;
 }
 
 }  // namespace input
diff --git a/tv/input/aidl/default/TvInput.h b/tv/input/aidl/default/TvInput.h
index a72bca3..e5f2b1a 100644
--- a/tv/input/aidl/default/TvInput.h
+++ b/tv/input/aidl/default/TvInput.h
@@ -48,7 +48,6 @@
 
   private:
     native_handle_t* createNativeHandle(int fd);
-    void releaseNativeHandle(native_handle_t* handle);
 
     shared_ptr<ITvInputCallback> mCallback;
     map<int32_t, shared_ptr<TvInputDeviceInfoWrapper>> mDeviceInfos;
diff --git a/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.cpp b/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.cpp
index ec83e29..b072d8f 100644
--- a/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.cpp
+++ b/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.cpp
@@ -158,10 +158,11 @@
         int32_t device_id = stream_config_.keyAt(j);
         vector<TvStreamConfig> config = stream_config_.valueAt(j);
         for (size_t i = 0; i < config.size(); i++) {
+            NativeHandle handle;
             int32_t stream_id = config[i].streamId;
             ALOGD("OpenAndCloseStreamTest: open stream, device_id=%d, stream_id=%d", device_id,
                   stream_id);
-            ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
+            ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk());
             ALOGD("OpenAndCloseStreamTest: close stream, device_id=%d, stream_id=%d", device_id,
                   stream_id);
             ASSERT_TRUE(tv_input_->closeStream(device_id, stream_id).isOk());
@@ -190,9 +191,10 @@
                 ITvInput::STATUS_INVALID_ARGUMENTS);
 
     int32_t stream_id = 0;
+    NativeHandle handle;
 
     ALOGD("InvalidDeviceIdTest: open stream, device_id=%d, stream_id=%d", id, stream_id);
-    ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle_).getServiceSpecificError() ==
+    ASSERT_TRUE(tv_input_->openStream(id, stream_id, &handle).getServiceSpecificError() ==
                 ITvInput::STATUS_INVALID_ARGUMENTS);
 
     ALOGD("InvalidDeviceIdTest: close stream, device_id=%d, stream_id=%d", id, stream_id);
@@ -226,8 +228,10 @@
         id = getNumNotIn(stream_ids);
     }
 
+    NativeHandle handle;
+
     ALOGD("InvalidStreamIdTest: open stream, device_id=%d, stream_id=%d", device_id, id);
-    ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle_).getServiceSpecificError() ==
+    ASSERT_TRUE(tv_input_->openStream(device_id, id, &handle).getServiceSpecificError() ==
                 ITvInput::STATUS_INVALID_ARGUMENTS);
 
     ALOGD("InvalidStreamIdTest: close stream, device_id=%d, stream_id=%d", device_id, id);
@@ -252,11 +256,13 @@
     int32_t device_id = stream_config_.keyAt(indices[0]);
     int32_t stream_id = stream_config_.valueAt(indices[0])[0].streamId;
 
-    ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
-    ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).isOk());
+    NativeHandle handle;
 
     ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
-    ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle_).getServiceSpecificError() ==
+    ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).isOk());
+
+    ALOGD("OpenAnOpenedStreamsTest: open stream, device_id=%d, stream_id=%d", device_id, stream_id);
+    ASSERT_TRUE(tv_input_->openStream(device_id, stream_id, &handle).getServiceSpecificError() ==
                 ITvInput::STATUS_INVALID_STATE);
 
     // close stream as subsequent tests assume no open streams
diff --git a/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.h b/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.h
index c76e568..3441d7f 100644
--- a/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.h
+++ b/tv/input/aidl/vts/functional/VtsHalTvInputTargetTest.h
@@ -84,7 +84,6 @@
     android::KeyedVector<int32_t, TvInputDeviceInfo> device_info_;
     android::KeyedVector<int32_t, vector<TvStreamConfig>> stream_config_;
     mutex mutex_;
-    NativeHandle handle_;
 };
 
 }  // namespace VtsHalTvInputTargetTest