comparison codecctrl.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 846535ace7a2
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
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
94 void mpeg_codec_controller(vo_functions_t *video_out){
95 //================== CODEC Controller: ==========================
96 signal(SIGTERM,codec_ctrl_sighandler); // set our SIGTERM handler
97 signal(SIGHUP,codec_ctrl_sighandler); // set our SIGHUP handler
98 printf("starting video codec...\n");
99 while(1){
100 int status;
101 if((child_pid=fork())==0){
102 // child:
103 unsigned int t=0;
104 codec_video_out_ptr=video_out;
105 #if 0
106 signal(SIGTERM,codec_sighandler); // set our SIGTERM handler
107 signal(SIGHUP,codec_sighandler); // set our SIGHUP handler
108 #else
109 // terminate requests:
110 signal(SIGTERM,codec_sighandler); // kill
111 signal(SIGHUP,codec_sighandler); // kill -HUP / xterm closed
112 signal(SIGINT,codec_sighandler); // Interrupt from keyboard
113 signal(SIGQUIT,codec_sighandler); // Quit from keyboard
114 // fatal errors:
115 signal(SIGBUS,codec_sighandler); // bus error
116 signal(SIGSEGV,codec_sighandler); // segfault
117 signal(SIGILL,codec_sighandler); // illegal instruction
118 signal(SIGFPE,codec_sighandler); // floating point exc.
119 signal(SIGABRT,codec_sighandler); // abort()
120 #endif
121
122 send_cmd(control_fifo2,0x22222222); // Send WE_ARE_READY command
123 send_cmd(control_fifo2,getpid()); // Send out PID
124 while(1){
125 unsigned int syncword=0;
126 read(data_fifo2,&syncword,4);
127 if(syncword==0x22222222) break;
128 printf("codec: drop bad frame (%X)\n",syncword);
129 }
130 //printf("codec: connection synced\n");
131
132 while(1){
133 int num_frames;
134 int len=0;
135 int len2;
136 send_cmd(control_fifo2,0x3030303);
137 len2=my_read(data_fifo2,(unsigned char*) &len,4);
138 if(len2!=4){
139 printf("FATAL: cannot read packet len from data fifo (ret=%d, errno=%d)\n",len2,errno);
140 break;
141 }
142 if(len==0){ printf("mpeg2dec: EOF, exiting...\n");break; }
143 // printf("mpeg2dec: frame (%d bytes) read\n",len);
144 t-=GetTimer();
145 mpeg2_decode_data(video_out, videobuffer, videobuffer+len);
146 t+=GetTimer();
147 send_cmd(control_fifo2,0); // FRAME_COMPLETED command
148 send_cmd(control_fifo2,picture->frame_rate); // fps
149 send_cmd(control_fifo2,100+picture->repeat_count);picture->repeat_count=0;
150 send_cmd(control_fifo2,t);t=0;
151 }
152 video_out->uninit();
153 exit(0); // leave process
154 }
155 wait(&status); // Waiting for the child!
156 // printf("restarting video codec...\n");
157 }
158 exit(0);
159 }
160
161 void mplayer_put_key(int code){
162 fd_set rfds;
163 struct timeval tv;
164
165 /* Watch stdin (fd 0) to see when it has input. */
166 FD_ZERO(&rfds);
167 FD_SET(keyb_fifo_put, &rfds);
168 tv.tv_sec = 0;
169 tv.tv_usec = 0;
170
171 //retval = select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv);
172 if(select(keyb_fifo_put+1, NULL, &rfds, NULL, &tv)){
173 write(keyb_fifo_put,&code,4);
174 // printf("*** key event %d sent ***\n",code);
175 } else {
176 // printf("*** key event dropped (FIFO is full) ***\n");
177 }
178 }
179
180 int mplayer_get_key(){
181 fd_set rfds;
182 struct timeval tv;
183 int code=-1;
184
185 /* Watch stdin (fd 0) to see when it has input. */
186 FD_ZERO(&rfds);
187 FD_SET(keyb_fifo_get, &rfds);
188 tv.tv_sec = 0;
189 tv.tv_usec = 0;
190
191 //retval = select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv);
192 if(select(keyb_fifo_put+1, &rfds, NULL, NULL, &tv)){
193 read(keyb_fifo_get,&code,4);
194 // printf("*** key event %d read ***\n",code);
195 }
196 return code;
197 }
198