Guest User

Untitled

a guest
May 29th, 2016
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.37 KB | None | 0 0
  1. #include <linux/dvb/frontend.h>
  2. #include <linux/dvb/dmx.h>
  3. #include <linux/dvb/audio.h>
  4. #include <linux/dvb/version.h>
  5. #include <sys/ioctl.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10.  
  11. #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  12.  
  13. struct channel {
  14. int id;
  15. const char *name;
  16. unsigned int frequency;
  17. unsigned int ts_id;
  18. };
  19.  
  20. static struct channel isdbt_channels[] = {
  21. { 13, "13ch", 473142857 },
  22. { 14, "14ch", 479142857 },
  23. { 15, "15ch", 485142857 },
  24. { 16, "16ch", 491142857 },
  25. { 17, "17ch", 497142857 },
  26. { 18, "18ch", 503142857 },
  27. { 19, "19ch", 509142857 },
  28. { 20, "20ch", 509142857 },
  29. { 21, "21ch", 515142857 },
  30. { 22, "22ch", 521142857 },
  31. { 23, "23ch", 527142857 },
  32. { 24, "24ch", 533142857 },
  33. { 25, "25ch", 539142857 },
  34. { 26, "26ch", 545142857 },
  35. { 27, "27ch", 551142857 },
  36. { 28, "28ch", 557142857 },
  37. { 29, "29ch", 563142857 },
  38. { 30, "30ch", 569142857 },
  39. { 31, "31ch", 575142857 },
  40. { 32, "32ch", 581142857 },
  41. { 33, "33ch", 587142857 },
  42. { 34, "34ch", 593142857 },
  43. { 35, "35ch", 599142857 },
  44. { 36, "36ch", 605142857 },
  45. { 37, "37ch", 611142857 },
  46. { 38, "38ch", 617142857 },
  47. { 39, "39ch", 623142857 },
  48. { 40, "40ch", 629142857 },
  49. { 41, "41ch", 635142857 },
  50. { 42, "42ch", 641142857 },
  51. { 43, "43ch", 647142857 },
  52. { 44, "44ch", 653142857 },
  53. { 45, "45ch", 659142857 },
  54. { 46, "46ch", 665142857 },
  55. { 47, "47ch", 671142857 },
  56. { 48, "48ch", 677142857 },
  57. { 49, "49ch", 683142857 },
  58. { 50, "50ch", 689142857 },
  59. { 51, "51ch", 695142857 },
  60. { 52, "52ch", 701142857 },
  61. { 53, "53ch", 707142857 },
  62. { 54, "54ch", 713142857 },
  63. { 55, "55ch", 719142857 },
  64. { 56, "56ch", 725142857 },
  65. { 57, "57ch", 731142857 },
  66. { 58, "58ch", 737142857 },
  67. { 59, "59ch", 743142857 },
  68. { 60, "60ch", 749142857 },
  69. { 61, "61ch", 755142857 },
  70. { 62, "62ch", 761142857 },
  71. };
  72.  
  73. static struct channel isdbs_channels[] = {
  74. { 101, "NHK BS-1", 1318000, 0x40f1 },
  75. { 102, "NHK BS-2", 1318000, 0x40f1 },
  76. { 103, "NHK BS-Hi", 1318000, 0x40f2 },
  77. { 141, "BS日テレ", 1279640, 0x40d0 },
  78. { 151, "BS朝日", 1049480, 0x4010 },
  79. { 161, "BS-i", 1049480, 0x4011 },
  80. { 171, "BSジャパン", 1087840, 0x4031 },
  81. { 181, "BSフジ", 1279640, 0x40d1 },
  82. { 191, "WOWOW", 1087840, 0x4030 },
  83. { 200, "STAR CHANNEL HV", 1202920, 0x4091 },
  84. { 211, "BS11", 1202920, 0x4090 },
  85. { 222, "TwellV", 1202920, 0x4092 },
  86. };
  87.  
  88. static struct channel *
  89. lookup_channel(int id, struct channel *channels, int nr)
  90. {
  91. int i;
  92. struct channel *channel;
  93. for (i = 0; i < nr; i++) {
  94. channel = &channels[i];
  95. if (channel->id == id)
  96. return channel;
  97. }
  98. return NULL;
  99. }
  100.  
  101. static int search(int adapter_nr, int channel_id)
  102. {
  103. char file[256];
  104. int fd;
  105. struct dvb_frontend_info info;
  106. struct channel *channel;
  107. struct dtv_property prop[3];
  108. struct dtv_properties props;
  109. int i;
  110. fe_status_t status;
  111.  
  112. sprintf(file, "/dev/dvb/adapter%d/frontend0", adapter_nr);
  113. if ((fd = open(file, O_RDWR)) < 0) {
  114. perror("open");
  115. return -1;
  116. }
  117.  
  118. if (ioctl(fd, FE_GET_INFO, &info) < 0) {
  119. perror("ioctl FE_GET_INFO");
  120. goto out;
  121. }
  122.  
  123. if (info.type == FE_QPSK) {
  124. channel = lookup_channel(channel_id, isdbs_channels,
  125. ARRAY_SIZE(isdbs_channels));
  126. } else if (info.type == FE_OFDM) {
  127. channel = lookup_channel(channel_id, isdbt_channels,
  128. ARRAY_SIZE(isdbt_channels));
  129. } else {
  130. fprintf(stderr, "Unknown type of adapter\n");
  131. goto out;
  132. }
  133. if (channel == NULL) {
  134. fprintf(stderr, "Unknown id of channel\n");
  135. goto out;
  136. }
  137.  
  138. prop[0].cmd = DTV_FREQUENCY;
  139. prop[0].u.data = channel->frequency;
  140. prop[1].cmd = DTV_ISDBS_TS_ID_LEGACY;
  141. prop[1].u.data = channel->ts_id;
  142. prop[2].cmd = DTV_TUNE;
  143.  
  144. props.props = prop;
  145. props.num = 3;
  146.  
  147. if ((ioctl(fd, FE_SET_PROPERTY, &props)) < 0) {
  148. perror("ioctl FE_SET_PROPERTY");
  149. goto out;
  150. }
  151.  
  152. for (i = 0; i < 4; i++) {
  153. if (ioctl(fd, FE_READ_STATUS, &status) < 0) {
  154. perror("ioctl FE_READ_STATUS");
  155. goto out;
  156. }
  157. if (status & FE_HAS_LOCK) {
  158. fprintf(stderr, "Successfully tuned to %s .\n",
  159. channel->name);
  160. return 0;
  161. }
  162. sleep(1);
  163. }
  164.  
  165. fprintf(stderr, "Failed to tune to %s (status %02x).\n",
  166. channel->name, status);
  167.  
  168. out:
  169. close(fd);
  170. return -1;
  171. }
  172.  
  173. static int track(int adapter_nr)
  174. {
  175. char file[256];
  176. int fd;
  177. struct dmx_pes_filter_params filter;
  178.  
  179. filter.pid = 0x2000;
  180. filter.input = DMX_IN_FRONTEND;
  181. filter.output = DMX_OUT_TS_TAP;
  182. filter.pes_type = DMX_PES_VIDEO;
  183. filter.flags = DMX_IMMEDIATE_START;
  184.  
  185. sprintf(file, "/dev/dvb/adapter%d/demux0", adapter_nr);
  186. if ((fd = open(file, O_RDWR)) < 0) {
  187. perror("open");
  188. return -1;
  189. }
  190.  
  191.  
  192. if (ioctl(fd, DMX_SET_PES_FILTER, &filter) < 0) {
  193. perror("ioctl DMX_SET_PES_FILTER");
  194. close(fd);
  195. return -1;
  196. }
  197.  
  198. while (1)
  199. sleep(3);
  200.  
  201. /* never returns */
  202. }
  203.  
  204. int main(int argc, char *argv[]) {
  205. int adapter_nr;
  206. int channel_id;
  207. int fd;
  208. int ret;
  209.  
  210. if (argc <= 2) {
  211. fprintf(stderr, "Usage: %s adapter_nr channel_id\n", argv[0]);
  212. return 1;
  213. }
  214.  
  215. adapter_nr = strtol(argv[1], NULL, 0);
  216. channel_id = strtol(argv[2], NULL, 10);
  217.  
  218. fd = search(adapter_nr, channel_id);
  219. if (fd < 0)
  220. return 1;
  221.  
  222. ret = track(adapter_nr);
  223. close(fd);
  224.  
  225. return ret < 0;
  226. }
Add Comment
Please, Sign In to add comment