test_av.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * test_av.c - Test for audio and video MPEG decoder API.
  3. *
  4. * Copyright (C) 2000 - 2002 convergence GmbH
  5. * Ralph Metzler <rjkm@convergence.de>
  6. * Marcus Metzler <mocm@convergence.de>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1
  11. * of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <sys/ioctl.h>
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <sys/types.h>
  27. #include <sys/stat.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31. #include <linux/types.h>
  32. #include <linux/dvb/audio.h>
  33. #include <linux/dvb/video.h>
  34. int audioStop(int fd, char *arg)
  35. {
  36. if (arg)
  37. return -1;
  38. if (ioctl(fd, AUDIO_STOP) == -1)
  39. perror("AUDIO_STOP");
  40. return 0;
  41. }
  42. int audioPlay(int fd, char *arg)
  43. {
  44. if (arg)
  45. return -1;
  46. if (ioctl(fd, AUDIO_PLAY) == -1)
  47. perror("AUDIO_PLAY");
  48. return 0;
  49. }
  50. int audioPause(int fd, char *arg)
  51. {
  52. if (arg)
  53. return -1;
  54. if (ioctl(fd, AUDIO_PAUSE) == -1)
  55. perror("AUDIO_PAUSE");
  56. return 0;
  57. }
  58. int audioContinue(int fd, char *arg)
  59. {
  60. if (arg)
  61. return -1;
  62. if (ioctl(fd, AUDIO_CONTINUE) == -1)
  63. perror("AUDIO_CONTINUE");
  64. return 0;
  65. }
  66. int audioSelectSource(int fd, char *arg)
  67. {
  68. int source;
  69. if (!arg)
  70. return -1;
  71. source = atoi(arg);
  72. if (ioctl(fd, AUDIO_SELECT_SOURCE, source) == -1)
  73. perror("AUDIO_SELECT_SOURCE");
  74. return 0;
  75. }
  76. int audioSetMute(int fd, char *arg)
  77. {
  78. int mute;
  79. if (!arg)
  80. return -1;
  81. mute = atoi(arg);
  82. if (ioctl(fd, AUDIO_SET_MUTE, mute) == -1)
  83. perror("AUDIO_SET_MUTE");
  84. return 0;
  85. }
  86. int audioSetAVSync(int fd, char *arg)
  87. {
  88. int _sync;
  89. if (!arg)
  90. return -1;
  91. _sync = atoi(arg);
  92. if (ioctl(fd, AUDIO_SET_AV_SYNC, _sync) == -1)
  93. perror("AUDIO_SET_AV_SYNC");
  94. return 0;
  95. }
  96. int audioSetBypassMode(int fd, char *arg)
  97. {
  98. int byp;
  99. if (!arg)
  100. return -1;
  101. byp = atoi(arg);
  102. if (ioctl(fd, AUDIO_SET_BYPASS_MODE, byp) == -1)
  103. perror("AUDIO_SET_BYPASS_MODE");
  104. return 0;
  105. }
  106. int audioChannelSelect(int fd, char *arg)
  107. {
  108. int chan;
  109. if (!arg)
  110. return -1;
  111. chan = atoi(arg);
  112. if (ioctl(fd, AUDIO_CHANNEL_SELECT, chan) == -1)
  113. perror("AUDIO_CHANNEL_SELECT");
  114. return 0;
  115. }
  116. int audioGetStatus(int fd, char *arg)
  117. {
  118. struct audio_status _stat;
  119. if (arg)
  120. return -1;
  121. if (ioctl(fd, AUDIO_GET_STATUS, &_stat) == -1) {
  122. perror("AUDIO_GET_STATUS");
  123. return 0;
  124. }
  125. printf("Audio Status:\n");
  126. printf(" Sync State : %s\n",
  127. (_stat.AV_sync_state ? "SYNC" : "NO SYNC"));
  128. printf(" Mute State : %s\n",
  129. (_stat.mute_state ? "muted" : "not muted"));
  130. printf(" Play State : ");
  131. switch ((int)_stat.play_state){
  132. case AUDIO_STOPPED:
  133. printf("STOPPED (%d)\n",_stat.play_state);
  134. break;
  135. case AUDIO_PLAYING:
  136. printf("PLAYING (%d)\n",_stat.play_state);
  137. break;
  138. case AUDIO_PAUSED:
  139. printf("PAUSED (%d)\n",_stat.play_state);
  140. break;
  141. default:
  142. printf("unknown (%d)\n",_stat.play_state);
  143. break;
  144. }
  145. printf(" Stream Source : ");
  146. switch((int)_stat.stream_source){
  147. case AUDIO_SOURCE_DEMUX:
  148. printf("DEMUX (%d)\n",_stat.stream_source);
  149. break;
  150. case AUDIO_SOURCE_MEMORY:
  151. printf("MEMORY (%d)\n",_stat.stream_source);
  152. break;
  153. default:
  154. printf("unknown (%d)\n",_stat.stream_source);
  155. break;
  156. }
  157. printf(" Channel Select : ");
  158. switch((int)_stat.channel_select){
  159. case AUDIO_STEREO:
  160. printf("Stereo (%d)\n",_stat.channel_select);
  161. break;
  162. case AUDIO_MONO_LEFT:
  163. printf("Mono left(%d)\n",_stat.channel_select);
  164. break;
  165. case AUDIO_MONO_RIGHT:
  166. printf("Mono right (%d)\n",_stat.channel_select);
  167. break;
  168. default:
  169. printf("unknown (%d)\n",_stat.channel_select);
  170. break;
  171. }
  172. printf(" Bypass Mode : %s\n",
  173. (_stat.bypass_mode ? "ON" : "OFF"));
  174. return 0;
  175. }
  176. int videoStop(int fd, char *arg)
  177. {
  178. if (arg)
  179. return -1;
  180. if (ioctl(fd, VIDEO_STOP) == -1)
  181. perror("VIDEO_STOP");
  182. return 0;
  183. }
  184. int videoPlay(int fd, char *arg)
  185. {
  186. if (arg)
  187. return -1;
  188. if (ioctl(fd, VIDEO_PLAY) == -1)
  189. perror("VIDEO_PLAY");
  190. return 0;
  191. }
  192. int videoFreeze(int fd, char *arg)
  193. {
  194. if (arg)
  195. return -1;
  196. if (ioctl(fd, VIDEO_FREEZE) == -1)
  197. perror("VIDEO_FREEZE");
  198. return 0;
  199. }
  200. int videoContinue(int fd, char *arg)
  201. {
  202. if (arg)
  203. return -1;
  204. if (ioctl(fd, VIDEO_CONTINUE) == -1)
  205. perror("VIDEO_CONTINUE");
  206. return 0;
  207. }
  208. int videoFormat(int fd, char *arg)
  209. {
  210. int format;
  211. if (!arg)
  212. return -1;
  213. format = atoi(arg);
  214. if (ioctl(fd, VIDEO_SET_FORMAT, format) == -1)
  215. perror("VIDEO_SET_FORMAT");
  216. return 0;
  217. }
  218. int videoDisplayFormat(int fd, char *arg)
  219. {
  220. int format;
  221. if (!arg)
  222. return -1;
  223. format = atoi(arg);
  224. if (ioctl(fd, VIDEO_SET_DISPLAY_FORMAT, format) == -1)
  225. perror("VIDEO_SET_DISPLAY_FORMAT");
  226. return 0;
  227. }
  228. int videoSelectSource(int fd, char *arg)
  229. {
  230. int source;
  231. if (!arg)
  232. return -1;
  233. source = atoi(arg);
  234. if (ioctl(fd, VIDEO_SELECT_SOURCE, source) == -1)
  235. perror("VIDEO_SELECT_SOURCE");
  236. return 0;
  237. }
  238. int videoSetBlank(int fd, char *arg)
  239. {
  240. int blank;
  241. if (!arg)
  242. return -1;
  243. blank = atoi(arg);
  244. if (ioctl(fd, VIDEO_SET_BLANK, blank) == -1)
  245. perror("VIDEO_SET_BLANK");
  246. return 0;
  247. }
  248. int videoFastForward(int fd, char *arg)
  249. {
  250. int frames;
  251. if (!arg)
  252. return -1;
  253. frames = atoi(arg);
  254. if (ioctl(fd, VIDEO_FAST_FORWARD, frames) == -1)
  255. perror("VIDEO_FAST_FORWARD");
  256. return 0;
  257. }
  258. int videoSlowMotion(int fd, char *arg)
  259. {
  260. int frames;
  261. if (!arg)
  262. return -1;
  263. frames = atoi(arg);
  264. if (ioctl(fd, VIDEO_SLOWMOTION, frames) == -1)
  265. perror("VIDEO_SLOWMOTION");
  266. return 0;
  267. }
  268. int videoGetStatus(int fd, char *arg)
  269. {
  270. struct video_status _stat;
  271. if (arg)
  272. return -1;
  273. if (ioctl(fd, VIDEO_GET_STATUS, &_stat) == -1){
  274. perror("VIDEO_GET_STATUS");
  275. return 0;
  276. }
  277. printf("Video Status:\n");
  278. printf(" Blank State : %s\n",
  279. (_stat.video_blank ? "BLANK" : "STILL"));
  280. printf(" Play State : ");
  281. switch ((int)_stat.play_state){
  282. case VIDEO_STOPPED:
  283. printf("STOPPED (%d)\n",_stat.play_state);
  284. break;
  285. case VIDEO_PLAYING:
  286. printf("PLAYING (%d)\n",_stat.play_state);
  287. break;
  288. case VIDEO_FREEZED:
  289. printf("FREEZED (%d)\n",_stat.play_state);
  290. break;
  291. default:
  292. printf("unknown (%d)\n",_stat.play_state);
  293. break;
  294. }
  295. printf(" Stream Source : ");
  296. switch((int)_stat.stream_source){
  297. case VIDEO_SOURCE_DEMUX:
  298. printf("DEMUX (%d)\n",_stat.stream_source);
  299. break;
  300. case VIDEO_SOURCE_MEMORY:
  301. printf("MEMORY (%d)\n",_stat.stream_source);
  302. break;
  303. default:
  304. printf("unknown (%d)\n",_stat.stream_source);
  305. break;
  306. }
  307. printf(" Format (Aspect Ratio): ");
  308. switch((int)_stat.video_format){
  309. case VIDEO_FORMAT_4_3:
  310. printf("4:3 (%d)\n",_stat.video_format);
  311. break;
  312. case VIDEO_FORMAT_16_9:
  313. printf("16:9 (%d)\n",_stat.video_format);
  314. break;
  315. case VIDEO_FORMAT_221_1:
  316. printf("2.21:1 (%d)\n",_stat.video_format);
  317. break;
  318. default:
  319. printf("unknown (%d)\n",_stat.video_format);
  320. break;
  321. }
  322. printf(" Display Format : ");
  323. switch((int)_stat.display_format){
  324. case VIDEO_PAN_SCAN:
  325. printf("Pan&Scan (%d)\n",_stat.display_format);
  326. break;
  327. case VIDEO_LETTER_BOX:
  328. printf("Letterbox (%d)\n",_stat.display_format);
  329. break;
  330. case VIDEO_CENTER_CUT_OUT:
  331. printf("Center cutout (%d)\n",_stat.display_format);
  332. break;
  333. default:
  334. printf("unknown (%d)\n",_stat.display_format);
  335. break;
  336. }
  337. return 0;
  338. }
  339. int videoGetSize(int fd, char *arg)
  340. {
  341. video_size_t size;
  342. if (arg)
  343. return -1;
  344. if (ioctl(fd, VIDEO_GET_SIZE, &size) == -1){
  345. perror("VIDEO_GET_SIZE");
  346. return 0;
  347. }
  348. printf("Video Size: %ux%u ", size.w, size.h);
  349. switch (size.aspect_ratio) {
  350. case VIDEO_FORMAT_4_3:
  351. printf("4:3 (%d)\n", size.aspect_ratio);
  352. break;
  353. case VIDEO_FORMAT_16_9:
  354. printf("16:9 (%d)\n", size.aspect_ratio);
  355. break;
  356. case VIDEO_FORMAT_221_1:
  357. printf("2.21:1 (%d)\n", size.aspect_ratio);
  358. break;
  359. default:
  360. printf("unknown aspect ratio (%d)\n", size.aspect_ratio);
  361. break;
  362. }
  363. return 0;
  364. }
  365. int videoStillPicture(int fd, char *arg)
  366. {
  367. int sifd;
  368. struct stat st;
  369. struct video_still_picture sp;
  370. if (!arg)
  371. return -1;
  372. sifd = open(arg, O_RDONLY);
  373. if (sifd == -1) {
  374. perror("open stillimage file");
  375. printf("(%s)\n", arg);
  376. return 0;
  377. }
  378. fstat(sifd, &st);
  379. sp.iFrame = (char *) malloc(st.st_size);
  380. sp.size = st.st_size;
  381. printf("I-frame size: %d\n", sp.size);
  382. if (!sp.iFrame) {
  383. printf("No memory for I-Frame\n");
  384. return 0;
  385. }
  386. printf("read: %d bytes\n", (int) read(sifd,sp.iFrame,sp.size));
  387. if (ioctl(fd, VIDEO_STILLPICTURE, &sp) == -1)
  388. perror("VIDEO_STILLPICTURE");
  389. return 0;
  390. }
  391. typedef int (* cmd_func_t)(int fd, char *args);
  392. typedef struct {
  393. char *cmd;
  394. char *param_help;
  395. cmd_func_t cmd_func;
  396. } cmd_t;
  397. cmd_t audio_cmds[] =
  398. {
  399. { "stop", "", audioStop },
  400. { "play", "", audioPlay },
  401. { "pause", "", audioPause },
  402. { "continue", "", audioContinue },
  403. { "source", "n: 0 demux, 1 memory", audioSelectSource },
  404. { "mute", "n: 0 unmute, 1 mute", audioSetMute },
  405. { "avsync", "n: 0 unsync, 1 sync", audioSetAVSync },
  406. { "bypass", "n: 0 normal, 1 bypass", audioSetBypassMode },
  407. { "channel", "n: 0 stereo, 1 left, 2 right", audioChannelSelect },
  408. { "status", "", audioGetStatus },
  409. { NULL, NULL, NULL }
  410. };
  411. cmd_t video_cmds[] =
  412. {
  413. { "stop", "", videoStop },
  414. { "play", "", videoPlay },
  415. { "freeze", "", videoFreeze },
  416. { "continue", "", videoContinue },
  417. { "source", "n: 0 demux, 1 memory", videoSelectSource },
  418. { "blank", "n: 0 normal, 1 blank", videoSetBlank },
  419. { "ff", "n: number of frames", videoFastForward },
  420. { "slow", "n: number of frames", videoSlowMotion },
  421. { "status", "", videoGetStatus },
  422. { "size", "", videoGetSize },
  423. { "stillpic", "filename", videoStillPicture},
  424. { "format", "n: 0 4:3, 1 16:9", videoFormat},
  425. { "dispformat", "n: 0 pan&scan, 1 letter box, 2 center cut out", videoDisplayFormat},
  426. { NULL, NULL, NULL }
  427. };
  428. int usage(void)
  429. {
  430. int i;
  431. printf ("commands begin with a for audio and v for video:\n\n"
  432. "q : quit\n");
  433. for (i=0; audio_cmds[i].cmd; i++)
  434. printf("a %s %s\n", audio_cmds[i].cmd, audio_cmds[i].param_help);
  435. for (i=0; video_cmds[i].cmd; i++)
  436. printf("v %s %s\n", video_cmds[i].cmd, video_cmds[i].param_help);
  437. printf("\n");
  438. return 0;
  439. }
  440. int syntax_error(void)
  441. {
  442. fprintf(stderr, "syntax error\n");
  443. return 0;
  444. }
  445. int process_kbd_input(int vfd, int afd)
  446. {
  447. char buf[256], *cmd;
  448. int i;
  449. if (!fgets(buf, sizeof(buf), stdin))
  450. return -1;
  451. cmd = strtok(buf, " \n\t");
  452. if (!cmd) {
  453. printf("enter command or h + enter for help\n");
  454. return 0;
  455. }
  456. if (cmd[0] == 'q' || !strcmp(cmd, "quit"))
  457. return -1;
  458. if (cmd[0] == 'h' || !strcmp(cmd, "help"))
  459. return usage();
  460. if (cmd[0] == 'a' || !strcmp(cmd, "audio")) {
  461. cmd = strtok(NULL, " \n\t");
  462. if (!cmd)
  463. return syntax_error();
  464. for (i=0; audio_cmds[i].cmd; i++) {
  465. if (!strcmp(cmd, audio_cmds[i].cmd)) {
  466. cmd = strtok(NULL, " \n\t");
  467. printf("calling '%s', arg '%s'\n", audio_cmds[i].cmd, cmd);
  468. if (audio_cmds[i].cmd_func(afd, cmd))
  469. syntax_error();
  470. return 0;
  471. }
  472. }
  473. return syntax_error();
  474. } else if (buf[0] == 'v') {
  475. cmd = strtok(NULL, " \n\t");
  476. if (!cmd)
  477. return usage();
  478. for (i=0; video_cmds[i].cmd; i++) {
  479. if (!strcmp(cmd, video_cmds[i].cmd)) {
  480. cmd = strtok(NULL, " \n\t");
  481. printf("calling '%s', arg '%s'\n", video_cmds[i].cmd, cmd);
  482. if (video_cmds[i].cmd_func(vfd, cmd))
  483. syntax_error();
  484. return 0;
  485. }
  486. }
  487. return syntax_error();
  488. } else
  489. return syntax_error();
  490. return 0;
  491. }
  492. int main(void)
  493. {
  494. int vfd, afd;
  495. char *videodev = "/dev/dvb/adapter0/video0";
  496. char *audiodev = "/dev/dvb/adapter0/audio0";
  497. if (getenv("VIDEO"))
  498. videodev = getenv("VIDEO");
  499. if (getenv("AUDIO"))
  500. videodev = getenv("AUDIO");
  501. printf("using video device '%s'\n", videodev);
  502. printf("using audio device '%s'\n", audiodev);
  503. printf("enter command or h + enter for help\n");
  504. if ((vfd = open(videodev, O_RDWR | O_NONBLOCK)) < 0) {
  505. perror("open video device");
  506. return 1;
  507. }
  508. if ((afd = open(audiodev, O_RDWR | O_NONBLOCK)) < 0) {
  509. perror("open audio device");
  510. return 1;
  511. }
  512. while (process_kbd_input(vfd, afd) == 0)
  513. ;
  514. close(vfd);
  515. close(afd);
  516. return 0;
  517. }