4747 lines · 154.9 KB · C
RawDownload
/* * Multiple format streaming server * Copyright (c) 2000, 2001, 2002 Fabrice Bellard * * This file is part of Libav. * * Libav is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * Libav is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Libav; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include "config.h"#if !HAVE_CLOSESOCKET#define closesocket close#endif#include <string.h>#include <strings.h>#include <stdlib.h>#include "libavformat/avformat.h"#include "libavformat/ffm.h"#include "libavformat/network.h"#include "libavformat/os_support.h"#include "libavformat/rtpdec.h"#include "libavformat/rtsp.h"// XXX for ffio_open_dyn_packet_buffer, to be removed#include "libavformat/avio_internal.h"#include "libavutil/avstring.h"#include "libavutil/lfg.h"#include "libavutil/dict.h"#include "libavutil/mathematics.h"#include "libavutil/random_seed.h"#include "libavutil/parseutils.h"#include "libavutil/opt.h"#include <stdarg.h>#include <unistd.h>#include <fcntl.h>#include <sys/ioctl.h>#if HAVE_POLL_H#include <poll.h>#endif#include <errno.h>#include <sys/time.h>#include <time.h>#include <sys/wait.h>#include <signal.h>#if HAVE_DLFCN_H#include <dlfcn.h>#endif #include "cmdutils.h" const char program_name[] = "avserver";const int program_birth_year = 2000; static const OptionDef options[]; enum HTTPState {    HTTPSTATE_WAIT_REQUEST,    HTTPSTATE_SEND_HEADER,    HTTPSTATE_SEND_DATA_HEADER,    HTTPSTATE_SEND_DATA,          /* sending TCP or UDP data */    HTTPSTATE_SEND_DATA_TRAILER,    HTTPSTATE_RECEIVE_DATA,    HTTPSTATE_WAIT_FEED,          /* wait for data from the feed */    HTTPSTATE_READY,     RTSPSTATE_WAIT_REQUEST,    RTSPSTATE_SEND_REPLY,    RTSPSTATE_SEND_PACKET,}; static const char *http_state[] = {    "HTTP_WAIT_REQUEST",    "HTTP_SEND_HEADER",     "SEND_DATA_HEADER",    "SEND_DATA",    "SEND_DATA_TRAILER",    "RECEIVE_DATA",    "WAIT_FEED",    "READY",     "RTSP_WAIT_REQUEST",    "RTSP_SEND_REPLY",    "RTSP_SEND_PACKET",}; #define MAX_STREAMS 20 #define IOBUFFER_INIT_SIZE 8192 /* timeouts are in ms */#define HTTP_REQUEST_TIMEOUT (15 * 1000)#define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000) #define SYNC_TIMEOUT (10 * 1000) typedef struct RTSPActionServerSetup {    uint32_t ipaddr;    char transport_option[512];} RTSPActionServerSetup; typedef struct {    int64_t count1, count2;    int64_t time1, time2;} DataRateData; /* context associated with one connection */typedef struct HTTPContext {    enum HTTPState state;    int fd; /* socket file descriptor */    struct sockaddr_in from_addr; /* origin */    struct pollfd *poll_entry; /* used when polling */    int64_t timeout;    uint8_t *buffer_ptr, *buffer_end;    int http_error;    int post;    int chunked_encoding;    int chunk_size;               /* 0 if it needs to be read */    struct HTTPContext *next;    int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */    int64_t data_count;    /* feed input */    int feed_fd;    /* input format handling */    AVFormatContext *fmt_in;    int64_t start_time;            /* In milliseconds - this wraps fairly often */    int64_t first_pts;            /* initial pts value */    int64_t cur_pts;             /* current pts value from the stream in us */    int64_t cur_frame_duration;  /* duration of the current frame in us */    int cur_frame_bytes;       /* output frame size, needed to compute                                  the time at which we send each                                  packet */    int pts_stream_index;        /* stream we choose as clock reference */    int64_t cur_clock;           /* current clock reference value in us */    /* output format handling */    struct FFStream *stream;    /* -1 is invalid stream */    int feed_streams[MAX_STREAMS]; /* index of streams in the feed */    int switch_feed_streams[MAX_STREAMS]; /* index of streams in the feed */    int switch_pending;    AVFormatContext fmt_ctx; /* instance of FFStream for one user */    int last_packet_sent; /* true if last data packet was sent */    int suppress_log;    DataRateData datarate;    int wmp_client_id;    char protocol[16];    char method[16];    char url[128];    int buffer_size;    uint8_t *buffer;    int is_packetized; /* if true, the stream is packetized */    int packet_stream_index; /* current stream for output in state machine */     /* RTSP state specific */    uint8_t *pb_buffer; /* XXX: use that in all the code */    AVIOContext *pb;    int seq; /* RTSP sequence number */     /* RTP state specific */    enum RTSPLowerTransport rtp_protocol;    char session_id[32]; /* session id */    AVFormatContext *rtp_ctx[MAX_STREAMS];     /* RTP/UDP specific */    URLContext *rtp_handles[MAX_STREAMS];     /* RTP/TCP specific */    struct HTTPContext *rtsp_c;    uint8_t *packet_buffer, *packet_buffer_ptr, *packet_buffer_end;} HTTPContext; /* each generated stream is described here */enum StreamType {    STREAM_TYPE_LIVE,    STREAM_TYPE_STATUS,    STREAM_TYPE_REDIRECT,}; enum IPAddressAction {    IP_ALLOW = 1,    IP_DENY,}; typedef struct IPAddressACL {    struct IPAddressACL *next;    enum IPAddressAction action;    /* These are in host order */    struct in_addr first;    struct in_addr last;} IPAddressACL; /* description of each stream of the avserver.conf file */typedef struct FFStream {    enum StreamType stream_type;    char filename[1024];     /* stream filename */    struct FFStream *feed;   /* feed we are using (can be null if                                coming from file) */    AVDictionary *in_opts;   /* input parameters */    AVInputFormat *ifmt;       /* if non NULL, force input format */    AVOutputFormat *fmt;    IPAddressACL *acl;    char dynamic_acl[1024];    int nb_streams;    int prebuffer;      /* Number of millseconds early to start */    int64_t max_time;      /* Number of milliseconds to run */    int send_on_key;    AVStream *streams[MAX_STREAMS];    int feed_streams[MAX_STREAMS]; /* index of streams in the feed */    char feed_filename[1024]; /* file name of the feed storage, or                                 input file name for a stream */    char author[512];    char title[512];    char copyright[512];    char comment[512];    pid_t pid;  /* Of ffmpeg process */    time_t pid_start;  /* Of ffmpeg process */    char **child_argv;    struct FFStream *next;    unsigned bandwidth; /* bandwidth, in kbits/s */    /* RTSP options */    char *rtsp_option;    /* multicast specific */    int is_multicast;    struct in_addr multicast_ip;    int multicast_port; /* first port used for multicast */    int multicast_ttl;    int loop; /* if true, send the stream in loops (only meaningful if file) */     /* feed specific */    int feed_opened;     /* true if someone is writing to the feed */    int is_feed;         /* true if it is a feed */    int readonly;        /* True if writing is prohibited to the file */    int truncate;        /* True if feeder connection truncate the feed file */    int conns_served;    int64_t bytes_served;    int64_t feed_max_size;      /* maximum storage size, zero means unlimited */    int64_t feed_write_index;   /* current write position in feed (it wraps around) */    int64_t feed_size;          /* current size of feed */    struct FFStream *next_feed;} FFStream; typedef struct FeedData {    long long data_count;    float avg_frame_size;   /* frame size averaged over last frames with exponential mean */} FeedData; static struct sockaddr_in my_http_addr;static struct sockaddr_in my_rtsp_addr; static char logfilename[1024];static HTTPContext *first_http_ctx;static FFStream *first_feed;   /* contains only feeds */static FFStream *first_stream; /* contains all streams, including feeds */ static void new_connection(int server_fd, int is_rtsp);static void close_connection(HTTPContext *c); /* HTTP handling */static int handle_connection(HTTPContext *c);static int http_parse_request(HTTPContext *c);static int http_send_data(HTTPContext *c);static void compute_status(HTTPContext *c);static int open_input_stream(HTTPContext *c, const char *info);static int http_start_receive_data(HTTPContext *c);static int http_receive_data(HTTPContext *c); /* RTSP handling */static int rtsp_parse_request(HTTPContext *c);static void rtsp_cmd_describe(HTTPContext *c, const char *url);static void rtsp_cmd_options(HTTPContext *c, const char *url);static void rtsp_cmd_setup(HTTPContext *c, const char *url, RTSPMessageHeader *h);static void rtsp_cmd_play(HTTPContext *c, const char *url, RTSPMessageHeader *h);static void rtsp_cmd_pause(HTTPContext *c, const char *url, RTSPMessageHeader *h);static void rtsp_cmd_teardown(HTTPContext *c, const char *url, RTSPMessageHeader *h); /* SDP handling */static int prepare_sdp_description(FFStream *stream, uint8_t **pbuffer,                                   struct in_addr my_ip); /* RTP handling */static HTTPContext *rtp_new_connection(struct sockaddr_in *from_addr,                                       FFStream *stream, const char *session_id,                                       enum RTSPLowerTransport rtp_protocol);static int rtp_new_av_stream(HTTPContext *c,                             int stream_index, struct sockaddr_in *dest_addr,                             HTTPContext *rtsp_c); static const char *my_program_name;static const char *my_program_dir; static const char *config_filename = "/etc/avserver.conf"; static int avserver_debug;static int avserver_daemon;static int no_launch;static int need_to_start_children; /* maximum number of simultaneous HTTP connections */static unsigned int nb_max_http_connections = 2000;static unsigned int nb_max_connections = 5;static unsigned int nb_connections; static uint64_t max_bandwidth = 1000;static uint64_t current_bandwidth; static int64_t cur_time;           // Making this global saves on passing it around everywhere static AVLFG random_state; static FILE *logfile = NULL; void exit_program(int ret){    exit(ret);} /* FIXME: make avserver work with IPv6 *//* resolve host with also IP address parsing */static int resolve_host(struct in_addr *sin_addr, const char *hostname){     if (!ff_inet_aton(hostname, sin_addr)) {#if HAVE_GETADDRINFO        struct addrinfo *ai, *cur;        struct addrinfo hints;        memset(&hints, 0, sizeof(hints));        hints.ai_family = AF_INET;        if (getaddrinfo(hostname, NULL, &hints, &ai))            return -1;        /* getaddrinfo returns a linked list of addrinfo structs.         * Even if we set ai_family = AF_INET above, make sure         * that the returned one actually is of the correct type. */        for (cur = ai; cur; cur = cur->ai_next) {            if (cur->ai_family == AF_INET) {                *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;                freeaddrinfo(ai);                return 0;            }        }        freeaddrinfo(ai);        return -1;#else        struct hostent *hp;        hp = gethostbyname(hostname);        if (!hp)            return -1;        memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));#endif    }    return 0;} static char *ctime1(char *buf2){    time_t ti;    char *p;     ti = time(NULL);    p = ctime(&ti);    strcpy(buf2, p);    p = buf2 + strlen(p) - 1;    if (*p == '\n')        *p = '\0';    return buf2;} static void http_vlog(const char *fmt, va_list vargs){    static int print_prefix = 1;    if (logfile) {        if (print_prefix) {            char buf[32];            ctime1(buf);            fprintf(logfile, "%s ", buf);        }        print_prefix = strstr(fmt, "\n") != NULL;        vfprintf(logfile, fmt, vargs);        fflush(logfile);    }} #ifdef __GNUC____attribute__ ((format (printf, 1, 2)))#endifstatic void http_log(const char *fmt, ...){    va_list vargs;    va_start(vargs, fmt);    http_vlog(fmt, vargs);    va_end(vargs);} static void http_av_log(void *ptr, int level, const char *fmt, va_list vargs){    static int print_prefix = 1;    AVClass *avc = ptr ? *(AVClass**)ptr : NULL;    if (level > av_log_get_level())        return;    if (print_prefix && avc)        http_log("[%s @ %p]", avc->item_name(ptr), ptr);    print_prefix = strstr(fmt, "\n") != NULL;    http_vlog(fmt, vargs);} static void log_connection(HTTPContext *c){    if (c->suppress_log)        return;     http_log("%s - - [%s] \"%s %s\" %d %"PRId64"\n",             inet_ntoa(c->from_addr.sin_addr), c->method, c->url,             c->protocol, (c->http_error ? c->http_error : 200), c->data_count);} static void update_datarate(DataRateData *drd, int64_t count){    if (!drd->time1 && !drd->count1) {        drd->time1 = drd->time2 = cur_time;        drd->count1 = drd->count2 = count;    } else if (cur_time - drd->time2 > 5000) {        drd->time1 = drd->time2;        drd->count1 = drd->count2;        drd->time2 = cur_time;        drd->count2 = count;    }} /* In bytes per second */static int compute_datarate(DataRateData *drd, int64_t count){    if (cur_time == drd->time1)        return 0;     return ((count - drd->count1) * 1000) / (cur_time - drd->time1);}  static void start_children(FFStream *feed){    if (no_launch)        return;     for (; feed; feed = feed->next) {        if (feed->child_argv && !feed->pid) {            feed->pid_start = time(0);             feed->pid = fork();             if (feed->pid < 0) {                http_log("Unable to create children\n");                exit(1);            }            if (!feed->pid) {                /* In child */                char pathname[1024];                char *slash;                int i;                 av_strlcpy(pathname, my_program_name, sizeof(pathname));                 slash = strrchr(pathname, '/');                if (!slash)                    slash = pathname;                else                    slash++;                strcpy(slash, "ffmpeg");                 http_log("Launch commandline: ");                http_log("%s ", pathname);                for (i = 1; feed->child_argv[i] && feed->child_argv[i][0]; i++)                    http_log("%s ", feed->child_argv[i]);                http_log("\n");                 for (i = 3; i < 256; i++)                    close(i);                 if (!avserver_debug) {                    i = open("/dev/null", O_RDWR);                    if (i != -1) {                        dup2(i, 0);                        dup2(i, 1);                        dup2(i, 2);                        close(i);                    }                }                 /* This is needed to make relative pathnames work */                chdir(my_program_dir); 

Lines 1–500 of 4747.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t