Skip to content
Snippets Groups Projects
ffserver.c 132 KiB
Newer Older
  • Learn to ignore specific revisions
  •     "License as published by the Free Software Foundation; either\n"
        "version 2 of the License, or (at your option) any later version.\n"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        "\n"
    
        "This library is distributed in the hope that it will be useful,\n"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
    
        "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\n"
        "Lesser General Public License for more details.\n"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        "\n"
    
        "You should have received a copy of the GNU Lesser General Public\n"
        "License along with this library; if not, write to the Free Software\n"
        "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\n"
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        );
    }
    
    
    static void handle_child_exit(int sig)
    {
        pid_t pid;
        int status;
    
        while ((pid = waitpid(-1, &status, WNOHANG)) > 0) {
            FFStream *feed;
    
            for (feed = first_feed; feed; feed = feed->next) {
                if (feed->pid == pid) {
                    int uptime = time(0) - feed->pid_start;
    
                    feed->pid = 0;
                    fprintf(stderr, "%s: Pid %d exited with status %d after %d seconds\n", feed->filename, pid, status, uptime);
    
                    if (uptime < 30) {
                        /* Turn off any more restarts */
                        feed->child_argv = 0;
                    }    
                }
            }
        }
    
        need_to_start_children = 1;
    }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    int main(int argc, char **argv)
    {
        const char *config_filename;
        int c;
    
        struct sigaction sigact;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
    
        config_filename = "/etc/ffserver.conf";
    
    
        my_program_name = argv[0];
    
        my_program_dir = getcwd(0, 0);
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        for(;;) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            if (c == -1)
                break;
            switch(c) {
            case 'L':
                licence();
                exit(1);
            case '?':
            case 'h':
                help();
                exit(1);
    
            case 'n':
                no_launch = 1;
                break;
            case 'd':
                ffserver_debug = 1;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            case 'f':
                config_filename = optarg;
                break;
            default:
                exit(2);
            }
        }
    
    
        putenv("http_proxy");               /* Kill the http_proxy */
    
    
        /* address on which the server will handle HTTP connections */
        my_http_addr.sin_family = AF_INET;
        my_http_addr.sin_port = htons (8080);
        my_http_addr.sin_addr.s_addr = htonl (INADDR_ANY);
    
        /* address on which the server will handle RTSP connections */
        my_rtsp_addr.sin_family = AF_INET;
        my_rtsp_addr.sin_port = htons (5454);
        my_rtsp_addr.sin_addr.s_addr = htonl (INADDR_ANY);
        
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        nb_max_connections = 5;
    
        nb_max_bandwidth = 1000;
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        first_stream = NULL;
        logfilename[0] = '\0';
    
    
        memset(&sigact, 0, sizeof(sigact));
        sigact.sa_handler = handle_child_exit;
        sigact.sa_flags = SA_NOCLDSTOP | SA_RESTART;
        sigaction(SIGCHLD, &sigact, 0);
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        if (parse_ffconfig(config_filename) < 0) {
            fprintf(stderr, "Incorrect config file - exiting.\n");
            exit(1);
        }
    
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        build_feed_streams();
    
    
        /* put the process in background and detach it from its TTY */
        if (ffserver_daemon) {
            int pid;
    
            pid = fork();
            if (pid < 0) {
                perror("fork");
                exit(1);
            } else if (pid > 0) {
                /* parent : exit */
                exit(0);
            } else {
                /* child */
                setsid();
                chdir("/");
                close(0);
                open("/dev/null", O_RDWR);
    
                if (strcmp(logfilename, "-") != 0) {
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
        /* signal init */
        signal(SIGPIPE, SIG_IGN);
    
        /* open log file if needed */
        if (logfilename[0] != '\0') {
            if (!strcmp(logfilename, "-"))
                logfile = stdout;
            else
                logfile = fopen(logfilename, "w");
        }
    
    
        if (http_server() < 0) {
            fprintf(stderr, "Could not start server\n");
    
    Fabrice Bellard's avatar
    Fabrice Bellard committed
            exit(1);
        }
    
        return 0;
    }