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
|
|
24
|
|
25 // SIGTERM handler of codec controller (2nd process):
|
|
26 static void codec_ctrl_sighandler(int x){
|
|
27 DEBUG_SIG printf("\nCTRL: received signal %d, terminating child first:\n",x);
|
|
28 // first terminate the codec:
|
|
29 //kill(child_pid,SIGTERM);
|
|
30 kill(child_pid,x);
|
|
31 usleep(50000); // 50ms must be enough
|
|
32 DEBUG_SIG printf("CTRL: Sending KILL signal to child:\n");
|
|
33 kill(child_pid,SIGKILL); // worst case
|
|
34 usleep(10000);
|
|
35 // and exit
|
|
36 if(x!=SIGHUP){
|
|
37 DEBUG_SIG printf("CTRL: Exiting...\n");
|
|
38 exit(0);
|
|
39 }
|
|
40 }
|
|
41
|
|
42 static vo_functions_t *codec_video_out_ptr=NULL;
|
|
43
|
|
44 // SIGTERM handler of the codec (3nd process):
|
|
45 static void codec_sighandler(int x){
|
|
46 DEBUG_SIG printf("\nCHILD: received signal %d, exiting...\n",x);
|
|
47 if(x==SIGTERM){
|
|
48 //mpeg2_close(codec_video_out_ptr);
|
|
49 codec_video_out_ptr->uninit(); // closing video_out
|
|
50 }
|
|
51 exit(0);
|
|
52 }
|
|
53
|
|
54 void mpeg_codec_controller(vo_functions_t *video_out){
|
|
55 //================== CODEC Controller: ==========================
|
|
56 signal(SIGTERM,codec_ctrl_sighandler); // set our SIGTERM handler
|
|
57 signal(SIGHUP,codec_ctrl_sighandler); // set our SIGHUP handler
|
|
58 printf("starting video codec...\n");
|
|
59 while(1){
|
|
60 int status;
|
|
61 if((child_pid=fork())==0){
|
|
62 // child:
|
|
63 unsigned int t=0;
|
|
64 codec_video_out_ptr=video_out;
|
|
65 #if 0
|
|
66 signal(SIGTERM,codec_sighandler); // set our SIGTERM handler
|
|
67 signal(SIGHUP,codec_sighandler); // set our SIGHUP handler
|
|
68 #else
|
|
69 // terminate requests:
|
|
70 signal(SIGTERM,codec_sighandler); // kill
|
|
71 signal(SIGHUP,codec_sighandler); // kill -HUP / xterm closed
|
|
72 signal(SIGINT,codec_sighandler); // Interrupt from keyboard
|
|
73 signal(SIGQUIT,codec_sighandler); // Quit from keyboard
|
|
74 // fatal errors:
|
|
75 signal(SIGBUS,codec_sighandler); // bus error
|
|
76 signal(SIGSEGV,codec_sighandler); // segfault
|
|
77 signal(SIGILL,codec_sighandler); // illegal instruction
|
|
78 signal(SIGFPE,codec_sighandler); // floating point exc.
|
|
79 signal(SIGABRT,codec_sighandler); // abort()
|
|
80 #endif
|
|
81
|
|
82 send_cmd(control_fifo2,0x22222222); // Send WE_ARE_READY command
|
|
83 send_cmd(control_fifo2,getpid()); // Send out PID
|
|
84 while(1){
|
|
85 unsigned int syncword=0;
|
|
86 read(data_fifo2,&syncword,4);
|
|
87 if(syncword==0x22222222) break;
|
|
88 printf("codec: drop bad frame (%X)\n",syncword);
|
|
89 }
|
|
90 //printf("codec: connection synced\n");
|
|
91
|
|
92 while(1){
|
|
93 int len=0;
|
|
94 int len2;
|
|
95 send_cmd(control_fifo2,0x3030303);
|
|
96 len2=my_read(data_fifo2,(unsigned char*) &len,4);
|
|
97 if(len2!=4){
|
|
98 printf("FATAL: cannot read packet len from data fifo (ret=%d, errno=%d)\n",len2,errno);
|
|
99 break;
|
|
100 }
|
|
101 if(len==0){ printf("mpeg2dec: EOF, exiting...\n");break; }
|
|
102 // printf("mpeg2dec: frame (%d bytes) read\n",len);
|
|
103 t-=GetTimer();
|
|
104 mpeg2_decode_data(video_out, videobuffer, videobuffer+len);
|
|
105 t+=GetTimer();
|
|
106 send_cmd(control_fifo2,0); // FRAME_COMPLETED command
|
36
|
107 send_cmd(control_fifo2,frameratecode2framerate[picture->frame_rate_code]); // fps
|
1
|
108 send_cmd(control_fifo2,100+picture->repeat_count);picture->repeat_count=0;
|
36
|
109 // send_cmd(control_fifo2,100); // FIXME!
|
1
|
110 send_cmd(control_fifo2,t);t=0;
|
|
111 }
|
|
112 video_out->uninit();
|
|
113 exit(0); // leave process
|
|
114 }
|
|
115 wait(&status); // Waiting for the child!
|
|
116 // printf("restarting video codec...\n");
|
|
117 }
|
|
118 exit(0);
|
|
119 }
|
|
120
|