Home | History | Annotate | Download | only in taudio
      1 #include <unistd.h>
      2 #include <stdlib.h>
      3 #include <stdio.h>
      4 #include <sys/types.h>
      5 #include <sys/stat.h>
      6 #include <fcntl.h>
      7 #include <errno.h>
      8 
      9 #include <sys/ioctl.h>
     10 #include <linux/tegra_audio.h>
     11 
     12 #define FAILIF(x, ...) do if (x) { \
     13     fprintf(stderr, __VA_ARGS__);  \
     14     exit(EXIT_FAILURE);            \
     15 } while (0)
     16 
     17 int
     18 main(int argc, char *argv[])
     19 {
     20     int ifd, ofd, ofd_c;
     21     int nr, nw;
     22     int opt;
     23 //    struct tegra_audio_buf_config config;
     24     char *name;
     25     char *buffer;
     26     int len = -1;
     27 //    struct tegra_audio_error_counts errors, errors_tot;
     28 
     29     while ((opt = getopt(argc, argv, "n:")) != -1) {
     30         switch (opt) {
     31         case 'n':
     32             len = atoi(optarg);
     33             break;
     34         default: /* '?' */
     35             fprintf(stderr, "Usage: %s [-n<len>] name\n",
     36                     argv[0]);
     37             exit(EXIT_FAILURE);
     38         }
     39     }
     40 
     41     name = argv[optind];
     42     FAILIF(!name, "Expecting a file to play!\n");
     43 
     44     printf("file to play: [%s]\n", name);
     45 
     46     ifd = open(name, O_RDONLY);
     47     FAILIF(ifd < 0, "could not open %s: %s\n", name, strerror(errno));
     48 
     49     ofd = open("/dev/audio0_out", O_RDWR);
     50     FAILIF(ofd < 0, "could not open output: %s\n", strerror(errno));
     51 
     52     ofd_c = open("/dev/audio0_out_ctl", O_RDWR);
     53     FAILIF(ofd_c < 0, "could not open output control: %s\n", strerror(errno));
     54 
     55 #if 0
     56     FAILIF(ioctl(ofd_c, TEGRA_AUDIO_OUT_GET_BUF_CONFIG, &config) < 0,
     57            "Could not get output config: %s\n", strerror(errno));
     58 #endif
     59 
     60     if (len < 0)
     61         len = 4096;
     62 
     63     printf("write length: %d\n", len);
     64 
     65     buffer = malloc(len);
     66     FAILIF(!buffer, "Could not allocate %d bytes!\n", len);
     67 
     68 //    memset(&errors_tot, 0, sizeof(errors_tot));
     69     do {
     70         nr = read(ifd, buffer, len);
     71         if (!nr) {
     72             printf("EOF\n");
     73             break;
     74         }
     75         FAILIF(nr < 0, "Could not read from %s: %s\n", name, strerror(errno));
     76         nw = write(ofd, buffer, nr);
     77         FAILIF(nw < 0, "Could not copy to output: %s\n", strerror(errno));
     78         FAILIF(nw != nr, "Mismatch nw = %d nr = %d\n", nw, nr);
     79 
     80 #if 0
     81         FAILIF(ioctl(ofd_c, TEGRA_AUDIO_OUT_GET_ERROR_COUNT, &errors) < 0,
     82                "Could not get error count: %s\n", strerror(errno));
     83 
     84         if (errors.late_dma || errors.full_empty) {
     85             printf("out %d (%d late, %d underrun errors)\n", nw,
     86                    errors.late_dma, errors.full_empty);
     87             errors_tot.late_dma += errors.late_dma;
     88             errors_tot.full_empty += errors.full_empty;
     89         }
     90         else
     91 #endif
     92             printf("out %d\n", nw);
     93 
     94     } while (1);
     95 
     96 #if 0
     97     printf("played with %d late, %d underflow errors\n",
     98            errors_tot.late_dma, errors_tot.full_empty);
     99 #endif
    100     return 0;
    101 }
    102 
    103