test_dvr_play.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * test_dvr_play.c - Play TS file via dvr device.
  3. *
  4. * Copyright (C) 2000 Ralph Metzler <ralph@convergence.de>
  5. * & Marcus Metzler <marcus@convergence.de>
  6. * for convergence integrated media GmbH
  7. * Copyright (C) 2003 Convergence GmbH
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public License
  11. * as published by the Free Software Foundation; either version 2.1
  12. * of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. *
  23. */
  24. #include <sys/ioctl.h>
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <sys/types.h>
  28. #include <sys/stat.h>
  29. #include <fcntl.h>
  30. #include <time.h>
  31. #include <sys/poll.h>
  32. #include <string.h>
  33. #include <unistd.h>
  34. #include <errno.h>
  35. #include <stdlib.h>
  36. #include <linux/dvb/dmx.h>
  37. #define BUFSIZE (512*188)
  38. void play_file_dvr(int filefd, int dvrfd)
  39. {
  40. char buf[BUFSIZE];
  41. int count, written, bytes, total = 0;
  42. while ((count = read(filefd, buf, BUFSIZE)) > 0) {
  43. total += count;
  44. fprintf(stderr, "read %d (%d total)\n", count, total);
  45. written = 0;
  46. while (written < count) {
  47. bytes = write(dvrfd, buf + written, count - written);
  48. fprintf(stderr, "write %d\n", bytes);
  49. if (bytes < 0) {
  50. perror("write dvr");
  51. return;
  52. }
  53. else if (bytes == 0) {
  54. fprintf(stderr, "write dvr: 0 bytes !");
  55. return;
  56. }
  57. written += bytes;
  58. }
  59. }
  60. }
  61. void set_pid(int fd, int pid, int type)
  62. {
  63. struct dmx_pes_filter_params pesFilterParams;
  64. fprintf(stderr, "set PID 0x%04x (%d)\n", pid, type);
  65. if (ioctl(fd, DMX_STOP) < 0)
  66. perror("DMX STOP:");
  67. if (ioctl(fd, DMX_SET_BUFFER_SIZE, 64*1024) < 0)
  68. perror("DMX SET BUFFER:");
  69. pesFilterParams.pid = pid;
  70. pesFilterParams.input = DMX_IN_DVR;
  71. pesFilterParams.output = DMX_OUT_DECODER;
  72. pesFilterParams.pes_type = type;
  73. pesFilterParams.flags = DMX_IMMEDIATE_START;
  74. if (ioctl(fd, DMX_SET_PES_FILTER, &pesFilterParams) < 0)
  75. perror("DMX SET FILTER");
  76. }
  77. int main(int argc, char **argv)
  78. {
  79. char *dmxdev = "/dev/dvb/adapter0/demux0";
  80. char *dvrdev = "/dev/dvb/adapter0/dvr0";
  81. int vpid, apid;
  82. int filefd, dvrfd, vfd, afd;
  83. if (argc < 4) {
  84. fprintf(stderr, "usage: test_dvr_play TS-file video-PID audio-PID\n");
  85. return 1;
  86. }
  87. vpid = strtoul(argv[2], NULL, 0);
  88. apid = strtoul(argv[3], NULL, 0);
  89. filefd = open(argv[1], O_RDONLY);
  90. if (filefd == -1) {
  91. fprintf(stderr, "Failed to open '%s': %d %m\n", argv[1], errno);
  92. return 1;
  93. }
  94. fprintf(stderr, "Playing '%s', video PID 0x%04x, audio PID 0x%04x\n",
  95. argv[1], vpid, apid);
  96. if (getenv("DEMUX"))
  97. dmxdev = getenv("DEMUX");
  98. if (getenv("DVR"))
  99. dvrdev = getenv("DVR");
  100. if ((dvrfd = open(dvrdev, O_WRONLY)) == -1) {
  101. fprintf(stderr, "Failed to open '%s': %d %m\n", dvrdev, errno);
  102. return 1;
  103. }
  104. if ((vfd = open(dmxdev, O_WRONLY)) == -1) {
  105. fprintf(stderr, "Failed to open video '%s': %d %m\n", dmxdev, errno);
  106. return 1;
  107. }
  108. if ((afd = open(dmxdev, O_WRONLY)) == -1) {
  109. fprintf(stderr, "Failed to open audio '%s': %d %m\n", dmxdev, errno);
  110. return 1;
  111. }
  112. /* playback timing is controlled via A/V PTS, so we cannot start
  113. * writing to the DVR device before the PIDs are set...
  114. */
  115. set_pid(afd, apid, DMX_PES_AUDIO);
  116. set_pid(vfd, vpid, DMX_PES_VIDEO);
  117. play_file_dvr(filefd, dvrfd);
  118. close(dvrfd);
  119. close(afd);
  120. close(vfd);
  121. close(filefd);
  122. return 0;
  123. }