Skip to content
Snippets Groups Projects
ffserver.c 138 KiB
Newer Older
Fabrice Bellard's avatar
Fabrice Bellard committed
/*
 * Multiple format streaming server
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
Fabrice Bellard's avatar
Fabrice Bellard committed
 *
 * This library 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 of the License, or (at your option) any later version.
Fabrice Bellard's avatar
Fabrice Bellard committed
 *
 * This library is distributed in the hope that it will be useful,
Fabrice Bellard's avatar
Fabrice Bellard committed
 * 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.
Fabrice Bellard's avatar
Fabrice Bellard committed
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Fabrice Bellard's avatar
Fabrice Bellard committed
 */
#define HAVE_AV_CONFIG_H
#include "avformat.h"

Fabrice Bellard's avatar
Fabrice Bellard committed
#include <stdarg.h>
#include <netinet/in.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <errno.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <arpa/inet.h>
Fabrice Bellard's avatar
Fabrice Bellard committed
#include <netdb.h>
#include <ctype.h>
#include <signal.h>
Fabrice Bellard's avatar
Fabrice Bellard committed

/* maximum number of simultaneous HTTP connections */
#define HTTP_MAX_CONNECTIONS 2000

enum HTTPState {
    HTTPSTATE_WAIT_REQUEST,
    HTTPSTATE_SEND_HEADER,
    HTTPSTATE_SEND_DATA_HEADER,
    HTTPSTATE_SEND_DATA,          /* sending TCP or UDP data */
Fabrice Bellard's avatar
Fabrice Bellard committed
    HTTPSTATE_SEND_DATA_TRAILER,
    HTTPSTATE_RECEIVE_DATA,       
    HTTPSTATE_WAIT_FEED,          /* wait for data from the feed */
    HTTPSTATE_WAIT,               /* wait before sending next packets */
    HTTPSTATE_WAIT_SHORT,         /* short wait for short term 
                                     bandwidth limitation */
    HTTPSTATE_READY,

    RTSPSTATE_WAIT_REQUEST,
    RTSPSTATE_SEND_REPLY,
Fabrice Bellard's avatar
Fabrice Bellard committed
};

const char *http_state[] = {
Fabrice Bellard's avatar
Fabrice Bellard committed
    "SEND_DATA_HEADER",
    "SEND_DATA",
    "SEND_DATA_TRAILER",
    "RECEIVE_DATA",
    "WAIT_FEED",
    "WAIT",
    "WAIT_SHORT",
    "READY",

    "RTSP_WAIT_REQUEST",
    "RTSP_SEND_REPLY",
Fabrice Bellard's avatar
Fabrice Bellard committed
};

#define IOBUFFER_INIT_SIZE 8192
Fabrice Bellard's avatar
Fabrice Bellard committed

/* coef for exponential mean for bitrate estimation in statistics */
#define AVG_COEF 0.9

/* timeouts are in ms */
#define HTTP_REQUEST_TIMEOUT (15 * 1000)
#define RTSP_REQUEST_TIMEOUT (3600 * 24 * 1000)

Fabrice Bellard's avatar
Fabrice Bellard committed
#define SYNC_TIMEOUT (10 * 1000)

typedef struct {
    INT64 count1, count2;
    long time1, time2;
} DataRateData;

Fabrice Bellard's avatar
Fabrice Bellard committed
/* 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 */
    long timeout;
    UINT8 *buffer_ptr, *buffer_end;
    int http_error;
    struct HTTPContext *next;
    int got_key_frame; /* stream 0 => 1, stream 1 => 2, stream 2=> 4 */
Fabrice Bellard's avatar
Fabrice Bellard committed
    INT64 data_count;
    /* feed input */
    int feed_fd;
    /* input format handling */
    AVFormatContext *fmt_in;
    long start_time;            /* In milliseconds - this wraps fairly often */
    INT64 first_pts;            /* initial pts value */
    int pts_stream_index;       /* stream we choose as clock reference */
Fabrice Bellard's avatar
Fabrice Bellard committed
    /* 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 */
Fabrice Bellard's avatar
Fabrice Bellard committed
    int last_packet_sent; /* true if last data packet was sent */
    DataRateData datarate;
    char protocol[16];
    char method[16];
    char url[128];
    int buffer_size;
    UINT8 *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 *pb_buffer; /* XXX: use that in all the code */
    ByteIOContext *pb;
    int seq; /* RTSP sequence number */

    /* RTP state specific */
    enum RTSPProtocol rtp_protocol;
    char session_id[32]; /* session id */
    AVFormatContext *rtp_ctx[MAX_STREAMS];
    URLContext *rtp_handles[MAX_STREAMS];
    /* RTP short term bandwidth limitation */
    int packet_byte_count;
    int packet_start_time_us; /* used for short durations (a few
                                 seconds max) */
Fabrice Bellard's avatar
Fabrice Bellard committed
} HTTPContext;

/* each generated stream is described here */
enum StreamType {
    STREAM_TYPE_LIVE,
    STREAM_TYPE_STATUS,
    STREAM_TYPE_REDIRECT,
Fabrice Bellard's avatar
Fabrice Bellard committed
};

enum IPAddressAction {
    IP_ALLOW = 1,
    IP_DENY,
};

typedef struct IPAddressACL {
    struct IPAddressACL *next;
    enum IPAddressAction action;
    struct in_addr first;
    struct in_addr last;
} IPAddressACL;

Fabrice Bellard's avatar
Fabrice Bellard committed
/* description of each stream of the ffserver.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) */
    AVOutputFormat *fmt;
Fabrice Bellard's avatar
Fabrice Bellard committed
    int nb_streams;
    int prebuffer;      /* Number of millseconds early to start */
    long max_time;      /* Number of milliseconds to run */
Fabrice Bellard's avatar
Fabrice Bellard committed
    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;
Fabrice Bellard's avatar
Fabrice Bellard committed
    struct FFStream *next;
    int bandwidth; /* bandwidth, in kbits/s */
    /* 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) */
Loading
Loading full blame...