dvbtraffic.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* This file is released into the public domain by its authors */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/ioctl.h>
  5. #include <unistd.h>
  6. #include <sys/types.h>
  7. #include <sys/stat.h>
  8. #include <fcntl.h>
  9. #include <time.h>
  10. #include <sys/poll.h>
  11. #include <sys/time.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <libdvbapi/dvbdemux.h>
  15. #define BSIZE 188
  16. static int pidt[0x2001];
  17. static void usage(FILE *output)
  18. {
  19. fprintf(output,
  20. "Usage: dvbtraffic [OPTION]...\n"
  21. "Options:\n"
  22. " -a N use dvb adapter N\n"
  23. " -d N use demux N\n"
  24. " -h display this help\n");
  25. }
  26. int main(int argc, char **argv)
  27. {
  28. struct timeval startt;
  29. int adapter = 0, demux = 0;
  30. char *search = NULL;
  31. int fd, ffd, packets = 0;
  32. int opt;
  33. while ((opt = getopt(argc, argv, "a:d:hs:")) != -1) {
  34. switch (opt) {
  35. case 'a':
  36. adapter = atoi(optarg);
  37. break;
  38. case 'd':
  39. demux = atoi(optarg);
  40. break;
  41. case 'h':
  42. usage(stdout);
  43. exit(0);
  44. case 's':
  45. search = strdup(optarg);
  46. break;
  47. default:
  48. usage(stderr);
  49. exit(1);
  50. }
  51. }
  52. // open the DVR device
  53. fd = dvbdemux_open_dvr(adapter, demux, 1, 0);
  54. if (fd < 0) {
  55. fprintf(stderr, "dvbtraffic: Could not open dvr device: %m\n");
  56. exit(1);
  57. }
  58. dvbdemux_set_buffer(fd, 1024 * 1024);
  59. ffd = dvbdemux_open_demux(adapter, demux, 0);
  60. if (ffd < 0) {
  61. fprintf(stderr, "dvbtraffic: Could not open demux device: %m\n");
  62. exit(1);
  63. }
  64. if (dvbdemux_set_pid_filter(ffd, -1, DVBDEMUX_INPUT_FRONTEND, DVBDEMUX_OUTPUT_DVR, 1)) {
  65. perror("dvbdemux_set_pid_filter");
  66. return -1;
  67. }
  68. gettimeofday(&startt, 0);
  69. while (1) {
  70. unsigned char buffer[BSIZE];
  71. int pid, ok;
  72. ssize_t r;
  73. if ((r = read(fd, buffer, BSIZE)) <= 0) {
  74. perror("read");
  75. break;
  76. }
  77. if (r != BSIZE) {
  78. fprintf(stderr, "dvbtraffic: only read %zd bytes\n", r);
  79. break;
  80. }
  81. if (buffer[0] != 0x47) {
  82. continue;
  83. printf("desync (%x)\n", buffer[0]);
  84. while (buffer[0] != 0x47)
  85. read(fd, buffer, 1);
  86. continue;
  87. }
  88. ok = 1;
  89. pid = ((((unsigned) buffer[1]) << 8) |
  90. ((unsigned) buffer[2])) & 0x1FFF;
  91. if (search) {
  92. int i, sl = strlen(search);
  93. ok = 0;
  94. if (pid != 0x1fff) {
  95. for (i = 0; i < (188 - sl); ++i) {
  96. if (!memcmp(buffer + i, search, sl))
  97. ok = 1;
  98. }
  99. }
  100. }
  101. if (ok) {
  102. pidt[pid]++;
  103. pidt[0x2000]++;
  104. }
  105. packets++;
  106. if (!(packets & 0xFF)) {
  107. struct timeval now;
  108. int diff;
  109. gettimeofday(&now, 0);
  110. diff =
  111. (now.tv_sec - startt.tv_sec) * 1000 +
  112. (now.tv_usec - startt.tv_usec) / 1000;
  113. if (diff > 1000) {
  114. int _pid = 0;
  115. for (_pid = 0; _pid < 0x2001; _pid++) {
  116. if (pidt[_pid]) {
  117. printf("%04x %5d p/s %5d kb/s %5d kbit\n",
  118. _pid,
  119. pidt[_pid] * 1000 / diff,
  120. pidt[_pid] * 1000 / diff * 188 / 1024,
  121. pidt[_pid] * 8 * 1000 / diff * 188 / 1000);
  122. }
  123. pidt[_pid] = 0;
  124. }
  125. printf("-PID--FREQ-----BANDWIDTH-BANDWIDTH-\n");
  126. startt = now;
  127. }
  128. }
  129. }
  130. close(ffd);
  131. close(fd);
  132. return 0;
  133. }