Enable automatic detection of decoder node
[jacinto7_multimedia/viddec-test-app.git] / main.c
diff --git a/main.c b/main.c
index 621029cb24fe061d9e913e8f995163d1908c556b..f5d1583f5303d4856aadf200f18b55d07a82cb5b 100644 (file)
--- a/main.c
+++ b/main.c
@@ -1294,9 +1294,8 @@ static int open_device(char *dev_name)
 {
        struct v4l2_capability cap;
        struct v4l2_fmtdesc argp;
-       int ret = 0;
-       int fd = -1;
        struct v4l2_event_subscription sub;
+       int ret = 0, fd = -1;
 
        fd = open(dev_name, O_RDWR | O_NONBLOCK, 0);
        if (-1 == fd) {
@@ -1361,9 +1360,66 @@ static int open_device(char *dev_name)
        return fd;
 }
 
+static int find_device(char *dev_name)
+{
+       const char *default_dev_path = "/dev/";
+       const char *dev_name_mask = "videox";
+       const char *driver_name = "vxd-dec";
+       struct v4l2_capability cap = {0};
+       char name[256] = "";
+       DIR *d;
+       struct dirent *dir;
+       int fd = -1;
+
+       d = opendir(default_dev_path);
+       if (!d) {
+               printf("Failed to open device path %s %d %s\n",
+                      default_dev_path, errno, strerror(errno));
+               return -1;
+       }
+
+       while ((dir = readdir(d)) != NULL) {
+               if (strncmp(dir->d_name, dev_name_mask, 5) == 0) {
+                       strncpy(name, default_dev_path, sizeof(name));
+                       strncat(name, dir->d_name, sizeof(name));
+
+                       fd = open(name, O_RDWR | O_NONBLOCK, 0);
+                       if (fd < 0) {
+                               printf("Failed to open device %s %d %s\n",
+                                      name, errno, strerror(errno));
+                               continue;
+                       }
+
+                       memset(&cap, 0, sizeof(cap));
+
+                       if (ioctl(fd, VIDIOC_QUERYCAP, &cap)) {
+                               printf("VIDIOC_QUERYCAP failed on device %s %d %s\n",
+                                      name, errno, strerror(errno));
+                               close(fd);
+                               continue;
+                       }
+
+                       if (strcmp((const char *)cap.driver, driver_name) == 0) {
+                               close(fd);
+                               fd = -1;
+                               debug_printf("No device specified, using %s\n",
+                                            name);
+                               snprintf(dev_name, sizeof(name), "%s", name);
+                               return 0;
+                       }
+
+                       close(fd);
+                       fd = -1;
+               }
+       }
+
+       printf("Failed to find device in %s\n", default_dev_path);
+       return -1;
+}
+
 int main(int argc, char **argv)
 {
-       char *dev_name = "/dev/video0";
+       char dev_name[256] = "";
        int i = 0;
        int ret = 0;
        unsigned int num_devs = 1;
@@ -1395,7 +1451,7 @@ int main(int argc, char **argv)
        /* Parse the input args */
        while (1)
        {
-               c = getopt (argc, argv, "nf:hbi:o:d:te:");
+               c = getopt (argc, argv, "nf:hbi:o:d:te:v:");
                if (c == -1)
                        break;
 
@@ -1423,6 +1479,8 @@ int main(int argc, char **argv)
                                printf("\t\t-e <number of ms> Number of milliseconds to sleep between\n");
                                printf("\t\t\tqueueing the last bitstream buffer and sending CMD_STOP\n");
                                printf("\t\t\tUsed to test various timing scenarios for EOS\n");
+                               printf("\t\t-v <dev_name>\n");
+                               printf("\t\t\tUsed to specify which device node is the decoder\n");
                                break;
                        case 'b':
                                use_drm_capbuff = 0; /* DO NOT use drm dss device*/
@@ -1449,6 +1507,10 @@ int main(int argc, char **argv)
                        case 'e':
                                sleep_time = 1000 * atoi(optarg);
                                break;
+                       case 'v':
+                               snprintf(dev_name, sizeof(dev_name),
+                                        "%s", optarg);
+                               break;
                        default:
                                printf("Unrecognized argument %c\n", c);
                }
@@ -1467,11 +1529,19 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       if (strlen(dev_name) == 0) {
+               ret = find_device(dev_name);
+               if (ret)
+                       return EXIT_FAILURE;
+       }
+
        for (i = 0; i < num_devs; i++) {
                debug_printf("*** Calling open_device for device %d \n", i);
                fds[i] = open_device(dev_name);
-               if (fds[i] < 0)
+               if (fds[i] < 0) {
+                       printf("Failed to open device %s\n", dev_name);
                        return EXIT_FAILURE;
+               }
                debug_printf("*** device %d is fd %d", i, fds[i]);
        }