1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include <inttypes.h>
6 #include <getopt.h>
7 #include <sys/types.h>
8 #include <sys/stat.h>
9 #include <fcntl.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include <sys/ioctl.h>
13 #include <sys/time.h>
14 #include <dirent.h>
15 #include <math.h>
16 
17 #include "v4l2-ctl.h"
18 
19 static struct v4l2_audio vaudio;	/* list audio inputs */
20 static struct v4l2_audioout vaudout;   	/* audio outputs */
21 static int input;			/* set_input/get_input */
22 static int output;			/* set_output/get_output */
23 
io_usage()24 void io_usage()
25 {
26 	printf("\nInput/Output options:\n"
27 	       "  -I, --get-input    query the video input [VIDIOC_G_INPUT]\n"
28 	       "  -i, --set-input <num>\n"
29 	       "                     set the video input to <num> [VIDIOC_S_INPUT]\n"
30 	       "  -N, --list-outputs display video outputs [VIDIOC_ENUMOUTPUT]\n"
31 	       "  -n, --list-inputs  display video inputs [VIDIOC_ENUMINPUT]\n"
32 	       "  -O, --get-output   query the video output [VIDIOC_G_OUTPUT]\n"
33 	       "  -o, --set-output <num>\n"
34 	       "                     set the video output to <num> [VIDIOC_S_OUTPUT]\n"
35 	       "  --set-audio-output <num>\n"
36 	       "                     set the audio output to <num> [VIDIOC_S_AUDOUT]\n"
37 	       "  --get-audio-input  query the audio input [VIDIOC_G_AUDIO]\n"
38 	       "  --set-audio-input <num>\n"
39 	       "                     set the audio input to <num> [VIDIOC_S_AUDIO]\n"
40 	       "  --get-audio-output query the audio output [VIDIOC_G_AUDOUT]\n"
41 	       "  --set-audio-output <num>\n"
42 	       "                     set the audio output to <num> [VIDIOC_S_AUDOUT]\n"
43 	       "  --list-audio-outputs\n"
44 	       "                     display audio outputs [VIDIOC_ENUMAUDOUT]\n"
45 	       "  --list-audio-inputs\n"
46 	       "                     display audio inputs [VIDIOC_ENUMAUDIO]\n"
47 	       );
48 }
49 
inputtype2s(uint32_t type)50 static const char *inputtype2s(uint32_t type)
51 {
52 	switch (type) {
53 	case V4L2_INPUT_TYPE_TUNER:
54 		return "Tuner";
55 	case V4L2_INPUT_TYPE_CAMERA:
56 		return "Camera";
57 	case V4L2_INPUT_TYPE_TOUCH:
58 		return "Touch";
59 	default:
60 		return "Unknown";
61 	}
62 }
63 
outputtype2s(uint32_t type)64 static const char *outputtype2s(uint32_t type)
65 {
66 	switch (type) {
67 	case V4L2_OUTPUT_TYPE_MODULATOR:
68 		return "Modulator";
69 	case V4L2_OUTPUT_TYPE_ANALOG:
70 		return "Analog";
71 	case V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY:
72 		return "Analog VGA Overlay";
73 	default:
74 		return "Unknown";
75 	}
76 }
77 
io_cmd(int ch,char * optarg)78 void io_cmd(int ch, char *optarg)
79 {
80 	switch (ch) {
81 		case OptSetInput:
82 			input = strtol(optarg, 0L, 0);
83 			break;
84 		case OptSetOutput:
85 			output = strtol(optarg, 0L, 0);
86 			break;
87 		case OptSetAudioInput:
88 			vaudio.index = strtol(optarg, 0L, 0);
89 			break;
90 		case OptSetAudioOutput:
91 			vaudout.index = strtol(optarg, 0L, 0);
92 			break;
93 	}
94 }
95 
io_set(cv4l_fd & _fd)96 void io_set(cv4l_fd &_fd)
97 {
98 	int fd = _fd.g_fd();
99 
100 	if (options[OptSetInput]) {
101 		if (doioctl(fd, VIDIOC_S_INPUT, &input) == 0) {
102 			struct v4l2_input vin;
103 
104 			printf("Video input set to %d", input);
105 			vin.index = input;
106 			if (test_ioctl(fd, VIDIOC_ENUMINPUT, &vin) >= 0)
107 				printf(" (%s: %s, %s)", vin.name,
108 				       inputtype2s(vin.type),
109 				       in_status2s(vin.status).c_str());
110 			printf("\n");
111 		}
112 	}
113 
114 	if (options[OptSetOutput]) {
115 		if (doioctl(fd, VIDIOC_S_OUTPUT, &output) == 0)
116 			printf("Output set to %d\n", output);
117 	}
118 
119 	if (options[OptSetAudioInput]) {
120 		if (doioctl(fd, VIDIOC_S_AUDIO, &vaudio) == 0)
121 			printf("Audio input set to %d\n", vaudio.index);
122 	}
123 
124 	if (options[OptSetAudioOutput]) {
125 		if (doioctl(fd, VIDIOC_S_AUDOUT, &vaudout) == 0)
126 			printf("Audio output set to %d\n", vaudout.index);
127 	}
128 }
129 
io_get(cv4l_fd & _fd)130 void io_get(cv4l_fd &_fd)
131 {
132 	int fd = _fd.g_fd();
133 
134 	if (options[OptGetInput]) {
135 		if (doioctl(fd, VIDIOC_G_INPUT, &input) == 0) {
136 			struct v4l2_input vin;
137 
138 			printf("Video input : %d", input);
139 			vin.index = input;
140 			if (test_ioctl(fd, VIDIOC_ENUMINPUT, &vin) >= 0)
141 				printf(" (%s: %s)", vin.name, in_status2s(vin.status).c_str());
142 			printf("\n");
143 		}
144 	}
145 
146 	if (options[OptGetOutput]) {
147 		if (doioctl(fd, VIDIOC_G_OUTPUT, &output) == 0) {
148 			struct v4l2_output vout;
149 
150 			printf("Video output: %d", output);
151 			vout.index = output;
152 			if (test_ioctl(fd, VIDIOC_ENUMOUTPUT, &vout) >= 0) {
153 				printf(" (%s)", vout.name);
154 			}
155 			printf("\n");
156 		}
157 	}
158 
159 	if (options[OptGetAudioInput]) {
160 		if (doioctl(fd, VIDIOC_G_AUDIO, &vaudio) == 0)
161 			printf("Audio input : %d (%s)\n", vaudio.index, vaudio.name);
162 	}
163 
164 	if (options[OptGetAudioOutput]) {
165 		if (doioctl(fd, VIDIOC_G_AUDOUT, &vaudout) == 0)
166 			printf("Audio output: %d (%s)\n", vaudout.index, vaudout.name);
167 	}
168 }
169 
io_list(cv4l_fd & _fd)170 void io_list(cv4l_fd &_fd)
171 {
172 	int fd = _fd.g_fd();
173 
174 	if (options[OptListInputs]) {
175 		struct v4l2_input vin;
176 
177 		vin.index = 0;
178 		printf("ioctl: VIDIOC_ENUMINPUT\n");
179 		while (test_ioctl(fd, VIDIOC_ENUMINPUT, &vin) >= 0) {
180 			if (vin.index)
181 				printf("\n");
182 			printf("\tInput       : %d\n", vin.index);
183 			printf("\tName        : %s\n", vin.name);
184 			printf("\tType        : 0x%08X (%s)\n", vin.type, inputtype2s(vin.type));
185 			printf("\tAudioset    : 0x%08X\n", vin.audioset);
186 			printf("\tTuner       : 0x%08X\n", vin.tuner);
187 			printf("\tStandard    : 0x%016llX (%s)\n", static_cast<unsigned long long>(vin.std),
188 				std2s(vin.std).c_str());
189 			printf("\tStatus      : 0x%08X (%s)\n", vin.status, in_status2s(vin.status).c_str());
190 			printf("\tCapabilities: 0x%08X (%s)\n", vin.capabilities, input_cap2s(vin.capabilities).c_str());
191                         vin.index++;
192                 }
193 	}
194 
195 	if (options[OptListOutputs]) {
196 		struct v4l2_output vout;
197 
198 		vout.index = 0;
199 		printf("ioctl: VIDIOC_ENUMOUTPUT\n");
200 		while (test_ioctl(fd, VIDIOC_ENUMOUTPUT, &vout) >= 0) {
201 			if (vout.index)
202 				printf("\n");
203 			printf("\tOutput      : %d\n", vout.index);
204 			printf("\tName        : %s\n", vout.name);
205 			printf("\tType        : 0x%08X (%s)\n", vout.type, outputtype2s(vout.type));
206 			printf("\tAudioset    : 0x%08X\n", vout.audioset);
207 			printf("\tStandard    : 0x%016llX (%s)\n", static_cast<unsigned long long>(vout.std),
208 					std2s(vout.std).c_str());
209 			printf("\tCapabilities: 0x%08X (%s)\n", vout.capabilities, output_cap2s(vout.capabilities).c_str());
210 			vout.index++;
211 		}
212 	}
213 
214 	if (options[OptListAudioInputs]) {
215 		struct v4l2_audio vaudio;	/* list audio inputs */
216 		vaudio.index = 0;
217 		printf("ioctl: VIDIOC_ENUMAUDIO\n");
218 		while (test_ioctl(fd, VIDIOC_ENUMAUDIO, &vaudio) >= 0) {
219 			if (vaudio.index)
220 				printf("\n");
221 			printf("\tInput   : %d\n", vaudio.index);
222 			printf("\tName    : %s\n", vaudio.name);
223 			vaudio.index++;
224 		}
225 	}
226 
227 	if (options[OptListAudioOutputs]) {
228 		struct v4l2_audioout vaudio;	/* list audio outputs */
229 		vaudio.index = 0;
230 		printf("ioctl: VIDIOC_ENUMAUDOUT\n");
231 		while (test_ioctl(fd, VIDIOC_ENUMAUDOUT, &vaudio) >= 0) {
232 			if (vaudio.index)
233 				printf("\n");
234 			printf("\tOutput  : %d\n", vaudio.index);
235 			printf("\tName    : %s\n", vaudio.name);
236 			vaudio.index++;
237 		}
238 	}
239 }
240