gnutv_data.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  1. /*
  2. gnutv utility
  3. Copyright (C) 2004, 2005 Manu Abraham <abraham.manu@gmail.com>
  4. Copyright (C) 2006 Andrew de Quincey (adq_dvb@lidskialf.net)
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #define _FILE_OFFSET_BITS 64
  18. #define _LARGEFILE_SOURCE 1
  19. #define _LARGEFILE64_SOURCE 1
  20. #include <stdio.h>
  21. #include <unistd.h>
  22. #include <limits.h>
  23. #include <string.h>
  24. #include <fcntl.h>
  25. #include <signal.h>
  26. #include <pthread.h>
  27. #include <errno.h>
  28. #include <sys/poll.h>
  29. #include <sys/socket.h>
  30. #include <netinet/in.h>
  31. #include <arpa/inet.h>
  32. #include <libdvbapi/dvbdemux.h>
  33. #include <libdvbapi/dvbaudio.h>
  34. #include <libucsi/mpeg/section.h>
  35. #include "gnutv.h"
  36. #include "gnutv_dvb.h"
  37. #include "gnutv_ca.h"
  38. #include "gnutv_data.h"
  39. static void *fileoutputthread_func(void* arg);
  40. static void *udpoutputthread_func(void* arg);
  41. static int gnutv_data_create_decoder_filter(int adapter, int demux, uint16_t pid, int pestype);
  42. static int gnutv_data_create_dvr_filter(int adapter, int demux, uint16_t pid);
  43. static void gnutv_data_decoder_pmt(struct mpeg_pmt_section *pmt);
  44. static void gnutv_data_dvr_pmt(struct mpeg_pmt_section *pmt);
  45. static void gnutv_data_append_pid_fd(int pid, int fd);
  46. static void gnutv_data_free_pid_fds(void);
  47. static pthread_t outputthread;
  48. static int outfd = -1;
  49. static int dvrfd = -1;
  50. static int pat_fd_dvrout = -1;
  51. static int pmt_fd_dvrout = -1;
  52. static int outputthread_shutdown = 0;
  53. static int usertp = 0;
  54. static int adapter_id = -1;
  55. static int demux_id = -1;
  56. static int output_type = 0;
  57. static struct addrinfo *outaddrs = NULL;
  58. struct pid_fd {
  59. int pid;
  60. int fd;
  61. };
  62. static struct pid_fd *pid_fds = NULL;
  63. static int pid_fds_count = 0;
  64. void gnutv_data_start(int _output_type,
  65. int ffaudiofd, int _adapter_id, int _demux_id, int buffer_size,
  66. char *outfile,
  67. char* outif, struct addrinfo *_outaddrs, int _usertp)
  68. {
  69. usertp = _usertp;
  70. demux_id = _demux_id;
  71. adapter_id = _adapter_id;
  72. output_type = _output_type;
  73. // setup output
  74. switch(output_type) {
  75. case OUTPUT_TYPE_DECODER:
  76. case OUTPUT_TYPE_DECODER_ABYPASS:
  77. dvbaudio_set_bypass(ffaudiofd, (output_type == OUTPUT_TYPE_DECODER_ABYPASS) ? 1 : 0);
  78. close(ffaudiofd);
  79. break;
  80. case OUTPUT_TYPE_STDOUT:
  81. case OUTPUT_TYPE_FILE:
  82. if (output_type == OUTPUT_TYPE_FILE) {
  83. // open output file
  84. outfd = open(outfile, O_WRONLY|O_CREAT|O_LARGEFILE|O_TRUNC, 0644);
  85. if (outfd < 0) {
  86. fprintf(stderr, "Failed to open output file\n");
  87. exit(1);
  88. }
  89. } else {
  90. outfd = STDOUT_FILENO;
  91. }
  92. // open dvr device
  93. dvrfd = dvbdemux_open_dvr(adapter_id, 0, 1, 0);
  94. if (dvrfd < 0) {
  95. fprintf(stderr, "Failed to open DVR device\n");
  96. exit(1);
  97. }
  98. // optionally set dvr buffer size
  99. if (buffer_size > 0) {
  100. if (dvbdemux_set_buffer(dvrfd, buffer_size) != 0) {
  101. fprintf(stderr, "Failed to set DVR buffer size\n");
  102. exit(1);
  103. }
  104. }
  105. pthread_create(&outputthread, NULL, fileoutputthread_func, NULL);
  106. break;
  107. case OUTPUT_TYPE_UDP:
  108. outaddrs = _outaddrs;
  109. // open output socket
  110. outfd = socket(outaddrs->ai_family, outaddrs->ai_socktype, outaddrs->ai_protocol);
  111. if (outfd < 0) {
  112. fprintf(stderr, "Failed to open output socket\n");
  113. exit(1);
  114. }
  115. // bind to local interface if requested
  116. if (outif != NULL) {
  117. if (setsockopt(outfd, SOL_SOCKET, SO_BINDTODEVICE, outif, strlen(outif)) < 0) {
  118. fprintf(stderr, "Failed to bind to interface %s\n", outif);
  119. exit(1);
  120. }
  121. }
  122. // open dvr device
  123. dvrfd = dvbdemux_open_dvr(adapter_id, 0, 1, 0);
  124. if (dvrfd < 0) {
  125. fprintf(stderr, "Failed to open DVR device\n");
  126. exit(1);
  127. }
  128. // optionally set dvr buffer size
  129. if (buffer_size > 0) {
  130. if (dvbdemux_set_buffer(dvrfd, buffer_size) != 0) {
  131. fprintf(stderr, "Failed to set DVR buffer size\n");
  132. exit(1);
  133. }
  134. }
  135. pthread_create(&outputthread, NULL, udpoutputthread_func, NULL);
  136. break;
  137. }
  138. // output PAT to DVR if requested
  139. switch(output_type) {
  140. case OUTPUT_TYPE_DVR:
  141. case OUTPUT_TYPE_FILE:
  142. case OUTPUT_TYPE_STDOUT:
  143. case OUTPUT_TYPE_UDP:
  144. pat_fd_dvrout = gnutv_data_create_dvr_filter(adapter_id, demux_id, TRANSPORT_PAT_PID);
  145. }
  146. }
  147. void gnutv_data_stop()
  148. {
  149. // shutdown output thread if necessary
  150. if (dvrfd != -1) {
  151. outputthread_shutdown = 1;
  152. pthread_join(outputthread, NULL);
  153. }
  154. gnutv_data_free_pid_fds();
  155. if (pat_fd_dvrout != -1)
  156. close(pat_fd_dvrout);
  157. if (pmt_fd_dvrout != -1)
  158. close(pmt_fd_dvrout);
  159. if (outaddrs)
  160. freeaddrinfo(outaddrs);
  161. }
  162. void gnutv_data_new_pat(int pmt_pid)
  163. {
  164. // output PMT to DVR if requested
  165. switch(output_type) {
  166. case OUTPUT_TYPE_DVR:
  167. case OUTPUT_TYPE_FILE:
  168. case OUTPUT_TYPE_STDOUT:
  169. case OUTPUT_TYPE_UDP:
  170. if (pmt_fd_dvrout != -1)
  171. close(pmt_fd_dvrout);
  172. pmt_fd_dvrout = gnutv_data_create_dvr_filter(adapter_id, demux_id, pmt_pid);
  173. }
  174. }
  175. int gnutv_data_new_pmt(struct mpeg_pmt_section *pmt)
  176. {
  177. // close all old PID FDs
  178. gnutv_data_free_pid_fds();
  179. // deal with the PMT appropriately
  180. switch(output_type) {
  181. case OUTPUT_TYPE_DECODER:
  182. case OUTPUT_TYPE_DECODER_ABYPASS:
  183. gnutv_data_decoder_pmt(pmt);
  184. break;
  185. case OUTPUT_TYPE_DVR:
  186. case OUTPUT_TYPE_FILE:
  187. case OUTPUT_TYPE_STDOUT:
  188. case OUTPUT_TYPE_UDP:
  189. gnutv_data_dvr_pmt(pmt);
  190. break;
  191. }
  192. return 1;
  193. }
  194. static void *fileoutputthread_func(void* arg)
  195. {
  196. (void)arg;
  197. uint8_t buf[4096];
  198. struct pollfd pollfd;
  199. int written;
  200. pollfd.fd = dvrfd;
  201. pollfd.events = POLLIN|POLLPRI|POLLERR;
  202. while(!outputthread_shutdown) {
  203. if (poll(&pollfd, 1, 1000) == -1) {
  204. if (errno == EINTR)
  205. continue;
  206. fprintf(stderr, "DVR device poll failure\n");
  207. return 0;
  208. }
  209. if (pollfd.revents == 0)
  210. continue;
  211. int size = read(dvrfd, buf, sizeof(buf));
  212. if (size < 0) {
  213. if (errno == EINTR)
  214. continue;
  215. if (errno == EOVERFLOW) {
  216. // The error flag has been cleared, next read should succeed.
  217. fprintf(stderr, "DVR overflow\n");
  218. continue;
  219. }
  220. fprintf(stderr, "DVR device read failure\n");
  221. return 0;
  222. }
  223. written = 0;
  224. while(written < size) {
  225. int tmp = write(outfd, buf + written, size - written);
  226. if (tmp == -1) {
  227. if (errno != EINTR) {
  228. fprintf(stderr, "Write error: %m\n");
  229. break;
  230. }
  231. } else {
  232. written += tmp;
  233. }
  234. }
  235. }
  236. return 0;
  237. }
  238. #define TS_PAYLOAD_SIZE (188*7)
  239. static void *udpoutputthread_func(void* arg)
  240. {
  241. (void)arg;
  242. uint8_t buf[12 + TS_PAYLOAD_SIZE];
  243. struct pollfd pollfd;
  244. int bufsize = 0;
  245. int bufbase = 0;
  246. int readsize;
  247. uint16_t rtpseq = 0;
  248. pollfd.fd = dvrfd;
  249. pollfd.events = POLLIN|POLLPRI|POLLERR;
  250. if (usertp) {
  251. srandom(time(NULL));
  252. int ssrc = random();
  253. rtpseq = random();
  254. buf[0x0] = 0x80;
  255. buf[0x1] = 0x21;
  256. buf[0x4] = 0x00; // }
  257. buf[0x5] = 0x00; // } FIXME: should really be a valid stamp
  258. buf[0x6] = 0x00; // }
  259. buf[0x7] = 0x00; // }
  260. buf[0x8] = ssrc >> 24;
  261. buf[0x9] = ssrc >> 16;
  262. buf[0xa] = ssrc >> 8;
  263. buf[0xb] = ssrc;
  264. bufbase = 12;
  265. }
  266. while(!outputthread_shutdown) {
  267. if (poll(&pollfd, 1, 1000) != 1)
  268. continue;
  269. if (pollfd.revents & POLLERR) {
  270. if (errno == EINTR)
  271. continue;
  272. fprintf(stderr, "DVR device read failure\n");
  273. return 0;
  274. }
  275. readsize = TS_PAYLOAD_SIZE - bufsize;
  276. readsize = read(dvrfd, buf + bufbase + bufsize, readsize);
  277. if (readsize < 0) {
  278. if (errno == EINTR)
  279. continue;
  280. fprintf(stderr, "DVR device read failure\n");
  281. return 0;
  282. }
  283. bufsize += readsize;
  284. if (bufsize == TS_PAYLOAD_SIZE) {
  285. if (usertp) {
  286. buf[2] = rtpseq >> 8;
  287. buf[3] = rtpseq;
  288. }
  289. if (sendto(outfd, buf, bufbase + bufsize, 0, outaddrs->ai_addr, outaddrs->ai_addrlen) < 0) {
  290. if (errno != EINTR) {
  291. fprintf(stderr, "Socket send failure: %m\n");
  292. return 0;
  293. }
  294. }
  295. rtpseq++;
  296. bufsize = 0;
  297. }
  298. }
  299. if (bufsize) {
  300. if (usertp) {
  301. buf[2] = rtpseq >> 8;
  302. buf[3] = rtpseq;
  303. }
  304. if (sendto(outfd, buf, bufbase + bufsize, 0, outaddrs->ai_addr, outaddrs->ai_addrlen) < 0) {
  305. if (errno != EINTR)
  306. fprintf(stderr, "Socket send failure: %m\n");
  307. }
  308. }
  309. return 0;
  310. }
  311. static int gnutv_data_create_decoder_filter(int adapter, int demux, uint16_t pid, int pestype)
  312. {
  313. int demux_fd = -1;
  314. // open the demuxer
  315. if ((demux_fd = dvbdemux_open_demux(adapter, demux, 0)) < 0) {
  316. return -1;
  317. }
  318. // create a section filter
  319. if (dvbdemux_set_pes_filter(demux_fd, pid, DVBDEMUX_INPUT_FRONTEND, DVBDEMUX_OUTPUT_DECODER, pestype, 1)) {
  320. close(demux_fd);
  321. return -1;
  322. }
  323. // done
  324. return demux_fd;
  325. }
  326. static int gnutv_data_create_dvr_filter(int adapter, int demux, uint16_t pid)
  327. {
  328. int demux_fd = -1;
  329. // open the demuxer
  330. if ((demux_fd = dvbdemux_open_demux(adapter, demux, 0)) < 0) {
  331. return -1;
  332. }
  333. // create a section filter
  334. if (dvbdemux_set_pid_filter(demux_fd, pid, DVBDEMUX_INPUT_FRONTEND, DVBDEMUX_OUTPUT_DVR, 1)) {
  335. close(demux_fd);
  336. return -1;
  337. }
  338. // done
  339. return demux_fd;
  340. }
  341. static void gnutv_data_decoder_pmt(struct mpeg_pmt_section *pmt)
  342. {
  343. int audio_pid = -1;
  344. int video_pid = -1;
  345. struct mpeg_pmt_stream *cur_stream;
  346. mpeg_pmt_section_streams_for_each(pmt, cur_stream) {
  347. switch(cur_stream->stream_type) {
  348. case 1:
  349. case 2: // video
  350. video_pid = cur_stream->pid;
  351. break;
  352. case 3:
  353. case 4: // audio
  354. audio_pid = cur_stream->pid;
  355. break;
  356. }
  357. }
  358. if (audio_pid != -1) {
  359. int fd = gnutv_data_create_decoder_filter(adapter_id, demux_id, audio_pid, DVBDEMUX_PESTYPE_AUDIO);
  360. if (fd < 0) {
  361. fprintf(stderr, "Unable to create dvr filter for PID %i\n", audio_pid);
  362. } else {
  363. gnutv_data_append_pid_fd(audio_pid, fd);
  364. }
  365. }
  366. if (video_pid != -1) {
  367. int fd = gnutv_data_create_decoder_filter(adapter_id, demux_id, video_pid, DVBDEMUX_PESTYPE_VIDEO);
  368. if (fd < 0) {
  369. fprintf(stderr, "Unable to create dvr filter for PID %i\n", video_pid);
  370. } else {
  371. gnutv_data_append_pid_fd(video_pid, fd);
  372. }
  373. }
  374. int fd = gnutv_data_create_decoder_filter(adapter_id, demux_id, pmt->pcr_pid, DVBDEMUX_PESTYPE_PCR);
  375. if (fd < 0) {
  376. fprintf(stderr, "Unable to create dvr filter for PID %i\n", pmt->pcr_pid);
  377. } else {
  378. gnutv_data_append_pid_fd(pmt->pcr_pid, fd);
  379. }
  380. }
  381. static void gnutv_data_dvr_pmt(struct mpeg_pmt_section *pmt)
  382. {
  383. struct mpeg_pmt_stream *cur_stream;
  384. mpeg_pmt_section_streams_for_each(pmt, cur_stream) {
  385. int fd = gnutv_data_create_dvr_filter(adapter_id, demux_id, cur_stream->pid);
  386. if (fd < 0) {
  387. fprintf(stderr, "Unable to create dvr filter for PID %i\n", cur_stream->pid);
  388. } else {
  389. gnutv_data_append_pid_fd(cur_stream->pid, fd);
  390. }
  391. }
  392. }
  393. static void gnutv_data_append_pid_fd(int pid, int fd)
  394. {
  395. struct pid_fd *tmp;
  396. if ((tmp = realloc(pid_fds, (pid_fds_count +1) * sizeof(struct pid_fd))) == NULL) {
  397. fprintf(stderr, "Out of memory when adding a new pid_fd\n");
  398. exit(1);
  399. }
  400. tmp[pid_fds_count].pid = pid;
  401. tmp[pid_fds_count].fd = fd;
  402. pid_fds_count++;
  403. pid_fds = tmp;
  404. }
  405. static void gnutv_data_free_pid_fds()
  406. {
  407. if (pid_fds_count) {
  408. int i;
  409. for(i=0; i< pid_fds_count; i++) {
  410. close(pid_fds[i].fd);
  411. }
  412. }
  413. if (pid_fds)
  414. free(pid_fds);
  415. pid_fds_count = 0;
  416. pid_fds = NULL;
  417. }