--- dvbstream/dvbstream.c.exitonfailure~ 2003-07-22 20:05:04.000000000 +0200 +++ dvbstream/dvbstream.c 2003-07-22 20:10:26.000000000 +0200 @@ -440,7 +440,7 @@ srate=atoi(&cmd[i])*1000UL; if (open_fe(&fd_frontend,&fd_sec)) { fprintf(stderr,"Tuning to %ld,%ld,%c\n",freq,srate,pol); - tune_it(fd_frontend,fd_sec,freq,srate,pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); + i=tune_it(fd_frontend,fd_sec,freq,srate,pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); close(fd_frontend); if (fd_sec) close(fd_sec); } @@ -559,6 +559,7 @@ int output_type=RTP_TS; int exit_after_tuning=0; bool do_optimize=false; + bool exit_on_signal_loss=false; /* Output: {uni,multi,broad}cast socket */ char ipOut[20]; @@ -602,6 +603,7 @@ fprintf(stderr," or L-band Frequency (DVB-S in Hz or DVB-T in Hz)\n"); fprintf(stderr,"-p [H,V] Polarity (DVB-S only)\n"); fprintf(stderr,"-s N Symbol rate (DVB-S or DVB-C)\n"); + fprintf(stderr,"-S Exit on signal loss\n"); fprintf(stderr,"-q N Diseqc (DVB-S, 1-4)\n"); fprintf(stderr,"\nAdvanced tuning options:\n\n"); @@ -643,6 +645,8 @@ } } else if (strcmp(argv[i],"-s")==0) { transponder_srate=atoi(argv[++i]); + } else if (strcmp(argv[i],"-S")==0) { + exit_on_signal_loss=true; } else if (strcmp(argv[i],"-q")==0) { diseqc=atoi(argv[++i]); } else if (strcmp(argv[i],"-o")==0) { @@ -676,8 +680,8 @@ case 128: modulation=QAM_128; break; case 256: modulation=QAM_256; break; default: - fprintf(stderr,"Invalid QAM rate: %s\n",argv[i]); - exit(0); + fprintf(stderr,"Invalid QAM rate %s, assuming auto\n",argv[i]); + modulation=QAM_AUTO; } } else if (strcmp(argv[i],"-gi")==0) { i++; @@ -754,8 +758,8 @@ } } - if ((output_type==RTP_PS) && (npids!=2)) { - fprintf(stderr,"ERROR: PS requires exactly two PIDS - video and audio.\n"); + if ((output_type==RTP_PS) && (npids!=2) && (npids!=3)) { + fprintf(stderr,"ERROR: PS requires exactly two or three PIDS - video, audio, optionally VT.\n"); exit; } if (signal(SIGHUP, SignalHandler) == SIG_IGN) signal(SIGHUP, SIG_IGN); @@ -768,6 +772,10 @@ i=tune_it(fd_frontend,fd_sec,transponder_freq,transponder_srate,0,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); close(fd_frontend); if(fd_sec) close(fd_sec); + if(i) { + fprintf(stderr, "No signal.\n"); + return -3; + } if(exit_after_tuning || do_optimize) return -2; } @@ -776,8 +784,12 @@ if(do_optimize) { i=optimize(fd_frontend,fd_sec,transponder_freq,transponder_srate,transponder_pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); fprintf(stderr, "Optimized frequency: %u\n", i); - } else + } else { i=tune_it(fd_frontend,fd_sec,transponder_freq,transponder_srate,transponder_pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); + if(i) { + fprintf(stderr, "No signal.\n"); + } + } close(fd_frontend); if (fd_sec) close(fd_sec); if(exit_after_tuning || do_optimize) @@ -785,7 +797,7 @@ } - } + } else i=0; - if (i<0) { exit(i); } + if (i) { exit(i); } for (i=0;i<npids;i++) { if((fd[i] = open(demuxdev[card],O_RDWR)) < 0){ @@ -826,6 +838,7 @@ /* Read packets */ free_bytes = buf; + if (!to_stdout) { /* Setup socket to accept input from a client */ gethostname(hostname, sizeof(hostname)); if ((hp = gethostbyname(hostname)) == NULL) { @@ -852,6 +865,7 @@ perror("server: listen"); exit(1); } + } connectionOpen=0; ns=-1; @@ -863,14 +877,44 @@ if (secs > 0) alarm(secs); while ( !Interrupted) { /* Poll the open file descriptors */ + int pr; if (ns==-1) { - poll(pfds,1,500); + pr=poll(pfds,1,500); } else { pfds[1].fd=ns; // This can change - poll(pfds,2,500); + pr=poll(pfds,2,500); } - process_telnet(); // See if there is an incoming telnet connection + if(!to_stdout) + process_telnet(); // See if there is an incoming telnet connection + else if(!pr) { + // No data... Try harder + fprintf(stderr, "No data...\n"); + pr=poll(pfds, 1, 3000); + if(!pr) { + // Try re-tuning in case something went wrong + if ( (transponder_freq>100000000)) { + if (open_fe(&fd_frontend,0)) { + i=tune_it(fd_frontend,fd_sec,transponder_freq,transponder_srate,0,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); + close(fd_frontend); + if(fd_sec) close(fd_sec); + } + } else if ((transponder_freq!=0) && (transponder_pol!=0) && (transponder_srate!=0)) { + if (open_fe(&fd_frontend,&fd_sec)) + i=tune_it(fd_frontend,fd_sec,transponder_freq,transponder_srate,transponder_pol,tone,specInv,diseqc,modulation,HP_CodeRate,TransmissionMode,guardInterval,bandWidth); + } + close(fd_frontend); + if (fd_sec) close(fd_sec); + if(i && exit_on_signal_loss) { + fprintf(stderr, "Signal lost.\n"); + exit(9); + } + } + if(!pr && exit_on_signal_loss) { + fprintf(stderr, "Signal disappeared, exiting.\n"); + exit(9); + } + } if (output_type==RTP_TS) { /* Attempt to read 188 bytes from /dev/ost/dvr */