1
|
1
|
|
2 //#define DEBUG_SIGNALS
|
|
3 #define DEBUG_SIGNALS_SLEEP ;
|
|
4 //#define DEBUG_SIGNALS_SLEEP sleep(2);
|
|
5
|
|
6 #ifdef DEBUG_SIGNALS
|
|
7 #define DEBUG_SIG if(1)
|
|
8 #else
|
|
9 #define DEBUG_SIG if(0)
|
|
10 #endif
|
|
11
|
|
12 //======= Interprocess Comminication (IPC) between player & codec =========
|
|
13
|
|
14 static int child_pid=0;
|
|
15 static int codec_pid=0;
|
|
16
|
|
17 // player:
|
|
18 static int data_fifo=-1;
|
|
19 static int control_fifo=-1;
|
|
20 // codec:
|
|
21 static int data_fifo2=-1;
|
|
22 static int control_fifo2=-1;
|
|
23 // keyboard:
|
|
24 static int keyb_fifo_put=-1;
|
|
25 static int keyb_fifo_get=-1;
|
|
26
|
|
27
|
|
28 // SIGTERM handler of codec controller (2nd process):
|
|
29 static void codec_ctrl_sighandler(int x){
|
|
30 DEBUG_SIG printf("\nCTRL: received signal %d, terminating child first:\n",x);
|
|
31 // first terminate the codec:
|
|
32 //kill(child_pid,SIGTERM);
|
|
33 kill(child_pid,x);
|
|
34 usleep(50000); // 50ms must be enough
|
|
35 DEBUG_SIG printf("CTRL: Sending KILL signal to child:\n");
|
|
36 kill(child_pid,SIGKILL); // worst case
|
|
37 usleep(10000);
|
|
38 // and exit
|
|
39 if(x!=SIGHUP){
|
|
40 DEBUG_SIG printf("CTRL: Exiting...\n");
|
|
41 exit(0);
|
|
42 }
|
|
43 }
|
|
44
|
|
45 static vo_functions_t *codec_video_out_ptr=NULL;
|
|
46
|
|
47 // SIGTERM handler of the codec (3nd process):
|
|
48 static void codec_sighandler(int x){
|
|
49 DEBUG_SIG printf("\nCHILD: received signal %d, exiting...\n",x);
|
|
50 if(x==SIGTERM){
|
|
51 //mpeg2_close(codec_video_out_ptr);
|
|
52 codec_video_out_ptr->uninit(); // closing video_out
|
|
53 }
|
|
54 exit(0);
|
|
55 }
|
|
56
|
|
57
|
|
58 static void make_pipe(int* pr,int* pw){
|
|
59 int temp[2];
|
|
60 if(pipe(temp)!=0) printf("Cannot make PIPE!\n");
|
|
61 *pr=temp[0];
|
|
62 *pw=temp[1];
|
|
63 }
|
|
64
|
|
65 static inline int my_write(int fd,unsigned char* mem,int len){
|
|
66 int total=0;
|
|
67 int len2;
|
|
68 while(len>0){
|
|
69 len2=write(fd,mem+total,len); if(len2<=0) break;
|
|
70 total+=len2;len-=len2;
|
|
71 // printf("%d bytes received, %d left\n",len2,len);
|
|
72 }
|
|
73 return total;
|
|
74 }
|
|
75
|
|
76 static inline int my_read(int fd,unsigned char* mem,int len){
|
|
77 int total=0;
|
|
78 int len2;
|
|
79 while(len>0){
|
|
80 len2=read(fd,mem+total,len); if(len2<=0) break;
|
|
81 total+=len2;len-=len2;
|
|
82 // printf("%d bytes received, %d left\n",len2,len);
|
|
83 }
|
|
84 return total;
|
|
85 }
|
|
86
|
|
87
|
|
88 void send_cmd(int fd,int cmd){
|
|
89 int fifo_cmd=cmd;
|
|
90 write(fd,&fifo_cmd,4);
|
|
91 // fflush(control_fifo);
|
|
92 }
|
|
93
|
36
|
94 static const int frameratecode2framerate[16] = {
|
|
95 0, 24000*10000/1001, 24*10000,25*10000, 30000*10000/1001, 30*10000,50*10000,60000*10000/1001,
|
|
96 60*10000, 0,0,0,0,0,0,0
|
|
97 };
|
|
98
|
|
99
|
1
|
100 void mpeg_codec_controller(vo_functions_t *video_out){
|
|
101 //================== CODEC Controller: ==========================
|
|
102 signal(SIGTERM,codec_ctrl_sighandler); // set our SIGTERM handler
|
|
103 signal(SIGHUP,codec_ctrl_sighandler); // set our SIGHUP handler
|
|
104 printf("starting video codec...\n");
|
|
105 while(1){
|
|
106 int status;
|
|
107 if((child_pid=fork())==0){
|
|
108 // child:
|
|
109 unsigned int t=0;
|
|
110 codec_video_out_ptr=video_out;
|
|
111 #if 0
|
|
112 signal(SIGTERM,codec_sighandler); // set our SIGTERM handler
|
|
113 signal(SIGHUP,codec_sighandler); // set our SIGHUP handler
|
|
114 #else
|
|
115 // terminate requests:
|
|
116 signal(SIGTERM,codec_sighandler); // kill
|
|
117 signal(SIGHUP,codec_sighandler); // kill -HUP / xterm closed
|
|
118 signal(SIGINT,codec_sighandler); // Interrupt from keyboard
|
|
119 signal(SIGQUIT,codec_sighandler); // Quit from keyboard
|
|
120 // fatal errors:
|
|
121 signal(SIGBUS,codec_sighandler); // bus error
|
|
122 signal(SIGSEGV,codec_sighandler); // segfault
|
|
123 signal(SIGILL,codec_sighandler); // illegal instruction
|
|
124 signal(SIGFPE,codec_sighandler); // floating point exc.
|
|
125 signal(SIGABRT,codec_sighandler); // abort()
|
|
126 #endif
|
|
127
|
|
128 send_cmd(control_fifo2,0x22222222); // Send WE_ARE_READY command
|
|
129 send_cmd(control_fifo2,getpid()); // Send out PID
|
|
130 while(1){
|
|
131 unsigned int syncword=0;
|
|
132 read(data_fifo2,&syncword,4);
|
|
133 if(syncword==0x22222222) break;
|
|
134 printf("codec: drop bad frame (%X)\n",syncword);
|
|
135 }
|
|
136 //printf("codec: connection synced\n");
|
|
137
|
|
138 while(1){
|
|
139 int len=0;
|
|
140 int len2;
|
|
141 send_cmd(control_fifo2,0x3030303);
|
|
142 len2=my_read(data_fifo2,(unsigned char*) &len,4);
|
|
143 if(len2!=4){
|
|
144 printf("FATAL: cannot read packet len from data fifo (ret=%d, errno=%d)\n",len2,errno);
|
|
145 break;
|
|
146 }
|
|
147 if(len==0){ printf("mpeg2dec: EOF, exiting...\n");break; }
|
|
148 // printf("mpeg2dec: frame (%d bytes) read\n",len);
|
|
149 t-=GetTimer();
|
|
150 mpeg2_decode_data(video_out, videobuffer, videobuffer+len);
|
|
151 t+=GetTimer();
|
|
152 send_cmd(control_fifo2,0); // FRAME_COMPLETED command
|
36
|
153 send_cmd(control_fifo2,frameratecode2framerate[picture->frame_rate_code]); // fps
|
1
|
154 send_cmd(control_fifo2,100+picture->repeat_count);picture->repeat_count=0;
|
36
|
155 // send_cmd(control_fifo2,100); // FIXME!
|
1
|
156 send_cmd(control_fifo2,t);t=0;
|
|
157 }
|
|
158 video_out->uninit();
|
|
159 exit(0); // leave process
|
|
160 }
|
|
161 wait(&status); // Waiting for the child!
|
|
162 // printf("restarting video codec...\n");
|
|
163 }
|
|
164 exit(0);
|
|
165 }
|
|
166
|
|
167 void mplayer_put_key(int code){
|
|
168 fd_set rfds;
|
|
169 struct timeval tv;
|
|
170
|
|
171 /* Watch stdin (fd 0) to see when it has input. */
|
|
172 FD_ZERO(&rfds);
|
|
173 FD_SET(keyb_fifo_put, &rfds);
|
|
174 tv.tv_sec = 0;
|
|
175 tv.tv_usec = 0;
|
|
176
|
|
177 //retval = select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv);
|
|
178 if(select(keyb_fifo_put+1, NULL, &rfds, NULL, &tv)){
|
|
179 write(keyb_fifo_put,&code,4);
|
|
180 // printf("*** key event %d sent ***\n",code);
|
|
181 } else {
|
|
182 // printf("*** key event dropped (FIFO is full) ***\n");
|
|
183 }
|
|
184 }
|
|
185
|
|
186 int mplayer_get_key(){
|
|
187 fd_set rfds;
|
|
188 struct timeval tv;
|
|
189 int code=-1;
|
|
190
|
|
191 /* Watch stdin (fd 0) to see when it has input. */
|
|
192 FD_ZERO(&rfds);
|
|
193 FD_SET(keyb_fifo_get, &rfds);
|
|
194 tv.tv_sec = 0;
|
|
195 tv.tv_usec = 0;
|
|
196
|
|
197 //retval = select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv);
|
|
198 if(select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv)){
|
|
199 read(keyb_fifo_get,&code,4);
|
|
200 // printf("*** key event %d read ***\n",code);
|
|
201 }
|
|
202 return code;
|
|
203 }
|
|
204
|