test_pes.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* test_pes.c - Test for PES filters.
  2. * usage: DEMUX=/dev/dvb/adapterX/demuxX test_pes PID
  3. *
  4. * Copyright (C) 2002 convergence GmbH
  5. * Johannes Stezenbach <js@convergence.de>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public License
  9. * as published by the Free Software Foundation; either version 2.1
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/stat.h>
  27. #include <fcntl.h>
  28. #include <sys/ioctl.h>
  29. #include <errno.h>
  30. #include <linux/dvb/dmx.h>
  31. #include "hex_dump.h"
  32. #define MAX_PES_SIZE (4*1024)
  33. void usage(void)
  34. {
  35. fprintf(stderr, "usage: test_pes PID [filename]\n");
  36. fprintf(stderr, " Print a hexdump of PES packets from PID to stdout.\n");
  37. fprintf(stderr, " filename : Write binary PES data to file (no hexdump).\n");
  38. fprintf(stderr, " The default demux device used can be changed\n");
  39. fprintf(stderr, " using the DEMUX environment variable\n");
  40. exit(1);
  41. }
  42. void process_pes(int fd, FILE *out)
  43. {
  44. uint8_t buf[MAX_PES_SIZE];
  45. int bytes;
  46. bytes = read(fd, buf, sizeof(buf));
  47. if (bytes < 0) {
  48. if (errno == EOVERFLOW) {
  49. fprintf(stderr, "read error: buffer overflow (%d)\n",
  50. EOVERFLOW);
  51. return;
  52. }
  53. else {
  54. perror("read");
  55. exit(1);
  56. }
  57. }
  58. if (out == stdout) {
  59. hex_dump(buf, bytes);
  60. printf("\n");
  61. }
  62. else {
  63. printf("got %d bytes\n", bytes);
  64. if (fwrite(buf, 1, bytes, out) == 0)
  65. perror("write output");
  66. }
  67. }
  68. int set_filter(int fd, unsigned int pid)
  69. {
  70. struct dmx_pes_filter_params f;
  71. f.pid = (uint16_t) pid;
  72. f.input = DMX_IN_FRONTEND;
  73. f.output = DMX_OUT_TAP;
  74. f.pes_type = DMX_PES_OTHER;
  75. f.flags = DMX_IMMEDIATE_START;
  76. if (ioctl(fd, DMX_SET_PES_FILTER, &f) == -1) {
  77. perror("ioctl DMX_SET_PES_FILTER");
  78. return 1;
  79. }
  80. return 0;
  81. }
  82. int main(int argc, char *argv[])
  83. {
  84. int dmxfd;
  85. unsigned long pid;
  86. char *dmxdev = "/dev/dvb/adapter0/demux0";
  87. FILE *out = stdout;
  88. if (argc != 2 && argc != 3)
  89. usage();
  90. pid = strtoul(argv[1], NULL, 0);
  91. if (pid > 0x1fff)
  92. usage();
  93. if (getenv("DEMUX"))
  94. dmxdev = getenv("DEMUX");
  95. fprintf(stderr, "test_pes: using '%s'\n", dmxdev);
  96. fprintf(stderr, " PID 0x%04lx\n", pid);
  97. if (argc == 3) {
  98. out = fopen(argv[2], "wb");
  99. if (!out) {
  100. perror("open output file");
  101. exit(1);
  102. }
  103. fprintf(stderr, " output to '%s'\n", argv[2]);
  104. }
  105. if ((dmxfd = open(dmxdev, O_RDWR)) < 0){
  106. perror("open");
  107. return 1;
  108. }
  109. if (set_filter(dmxfd, pid) != 0)
  110. return 1;
  111. for (;;) {
  112. process_pes(dmxfd, out);
  113. }
  114. close(dmxfd);
  115. return 0;
  116. }