From 8d95a7fb476c5bf5d3874d0dbb6a1c9d61afb082 Mon Sep 17 00:00:00 2001
From: fryshorts <leonhard@in-verted.de>
Date: Mon, 15 Sep 2014 23:59:41 +0200
Subject: [PATCH] Use helper function to set input in v4l2 input.

---
 plugins/linux-v4l2/v4l2-helpers.c | 11 +++++++++++
 plugins/linux-v4l2/v4l2-helpers.h | 12 ++++++++++++
 plugins/linux-v4l2/v4l2-input.c   | 11 +++++++----
 3 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/plugins/linux-v4l2/v4l2-helpers.c b/plugins/linux-v4l2/v4l2-helpers.c
index 6497ff1cc..a1cb9934d 100644
--- a/plugins/linux-v4l2/v4l2-helpers.c
+++ b/plugins/linux-v4l2/v4l2-helpers.c
@@ -123,3 +123,14 @@ int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf)
 	return 0;
 }
 
+int_fast32_t v4l2_set_input(int_fast32_t dev, int *input)
+{
+	if (!dev || !input)
+		return -1;
+
+	return (*input == -1)
+		? v4l2_ioctl(dev, VIDIOC_G_INPUT, input)
+		: v4l2_ioctl(dev, VIDIOC_S_INPUT, input);
+}
+
+
diff --git a/plugins/linux-v4l2/v4l2-helpers.h b/plugins/linux-v4l2/v4l2-helpers.h
index b53c9b746..7d2fce511 100644
--- a/plugins/linux-v4l2/v4l2-helpers.h
+++ b/plugins/linux-v4l2/v4l2-helpers.h
@@ -175,6 +175,18 @@ int_fast32_t v4l2_create_mmap(int_fast32_t dev, struct v4l2_buffer_data *buf);
  */
 int_fast32_t v4l2_destroy_mmap(struct v4l2_buffer_data *buf);
 
+/**
+ * Set the video input on the device.
+ *
+ * If the action succeeds input is set to the currently selected input.
+ *
+ * @param dev handle for the v4l2 device
+ * @param input index of the input or -1 to leave it as is
+ *
+ * @return negative on failure
+ */
+int_fast32_t v4l2_set_input(int_fast32_t dev, int *input);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/plugins/linux-v4l2/v4l2-input.c b/plugins/linux-v4l2/v4l2-input.c
index 485705542..53210543d 100644
--- a/plugins/linux-v4l2/v4l2-input.c
+++ b/plugins/linux-v4l2/v4l2-input.c
@@ -61,7 +61,7 @@ struct v4l2_data {
 	os_event_t event;
 
 	char *set_device;
-	int_fast32_t set_input;
+	int set_input;
 	int_fast32_t set_pixfmt;
 	int_fast32_t set_res;
 	int_fast32_t set_fps;
@@ -305,7 +305,8 @@ static void v4l2_input_list(int_fast32_t dev, obs_property_t prop)
 		if (in.type & V4L2_INPUT_TYPE_CAMERA) {
 			obs_property_list_add_int(prop, (char *) in.name,
 					in.index);
-			blog(LOG_INFO, "Found input '%s'", in.name);
+			blog(LOG_INFO, "Found input '%s' (Index %d)", in.name,
+					in.index);
 		}
 		in.index++;
 	}
@@ -609,10 +610,12 @@ static void v4l2_init(struct v4l2_data *data)
 	}
 
 	/* set input */
-	if (v4l2_ioctl(data->dev, VIDIOC_S_INPUT, &data->set_input) < 0) {
-		blog(LOG_ERROR, "Unable to set input");
+	if (v4l2_set_input(data->dev, &data->set_input) < 0) {
+		blog(LOG_ERROR, "Unable to set input %d",
+				data->set_input);
 		goto fail;
 	}
+	blog(LOG_INFO, "Input: %d", data->set_input);
 
 	/* set pixel format and resolution */
 	unpack_tuple(&width, &height, data->set_res);
-- 
GitLab