[vuplus-wifi-util] fix default ccode
[vuplus_openvuplus_3.0] / showiframe / showiframe.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9
10
11 #include <linux/dvb/video.h>
12
13 void c(int a)
14 {
15         if (a < 0)
16         {
17                 perror("ioctl");
18                 exit(6);
19         }
20 }
21
22 int main(int argc, char **argv)
23 {
24         struct stat s;
25         if (argc != 2)
26         {
27                 printf("usage: %s <iframe>\n", *argv);
28                 return 3;
29         }
30
31         int f = open(argv[1], O_RDONLY);
32         if (f < 0)
33         {
34                 perror(argv[1]);
35                 return 4;
36         }
37         fstat(f, &s);
38
39         int fd = open("/dev/dvb/adapter0/video0", O_WRONLY|O_NONBLOCK);
40
41         if (fd <= 0)
42         {
43                 perror("/dev/dvb/adapter0/video0");
44                 return 2;
45         }
46         else if (fork() != 0)
47                 return 0;
48         else
49         {
50                 size_t pos=0;
51                 int seq_end_avail = 0;
52
53                 int count = 7;
54                 /* 0x0 0x0 0x1 0xffffffe0 0x10 0x8 0xffffff80 0xffffff80 0x5 0x21 0x0 0x1 0x0 0x1 */
55                 
56                 /* unsigned char pes_header[] = { 0x00, 0x00, 0x01, 0xE0, 0x00, 0x00, 0x80, 0x00, 0x00 }; */
57
58                 unsigned char pes_header[] = {0x0, 0x0, 0x1, 0xe0, 0x00, 0x00, 0x80, 0x80, 0x5, 0x21, 0x0, 0x1, 0x0, 0x1};
59                 
60                 unsigned char seq_end[] = { 0x00, 0x00, 0x01, 0xB7 };
61                 unsigned char iframe[s.st_size];
62                 unsigned char stuffing[8192];
63                 memset(stuffing, 0, 8192);
64                 read(f, iframe, s.st_size);
65                 ioctl(fd, VIDEO_SET_STREAMTYPE, 0); // set to mpeg2
66                 c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_MEMORY));
67                 c(ioctl(fd, VIDEO_PLAY));
68                 c(ioctl(fd, VIDEO_CONTINUE));
69                 c(ioctl(fd, VIDEO_CLEAR_BUFFER));
70                 while(pos <= (s.st_size-4) && !(seq_end_avail = (!iframe[pos] && !iframe[pos+1] && iframe[pos+2] == 1 && iframe[pos+3] == 0xB7)))
71                         ++pos;
72                 while(count--){
73                         if ((iframe[3] >> 4) != 0xE) // no pes header
74                         {
75                                 write(fd, pes_header, sizeof(pes_header));
76                                 usleep(8000);
77                         }
78                         else {
79                                 iframe[4] = iframe[5] = 0x00;
80                         }
81                         write(fd, iframe, s.st_size);
82                         usleep(8000);
83                 }
84                 if (!seq_end_avail)
85                         write(fd, seq_end, sizeof(seq_end));
86                 write(fd, stuffing, 8192);
87                 usleep(150000);
88                 c(ioctl(fd, VIDEO_STOP, 0));
89                 c(ioctl(fd, VIDEO_SELECT_SOURCE, VIDEO_SOURCE_DEMUX));
90         }
91         return 0;
92 }
93