FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Data Structures | Macros | Enumerations | Functions | Variables
ffserver.c File Reference

multiple format streaming server based on the FFmpeg libraries More...

#include "config.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "libavformat/avformat.h"
#include "libavformat/rtpproto.h"
#include "libavformat/rtsp.h"
#include "libavformat/avio_internal.h"
#include "libavformat/internal.h"
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/lfg.h"
#include "libavutil/dict.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mathematics.h"
#include "libavutil/random_seed.h"
#include "libavutil/rational.h"
#include "libavutil/parseutils.h"
#include "libavutil/opt.h"
#include "libavutil/time.h"
#include <stdarg.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <sys/wait.h>
#include <signal.h>
#include "cmdutils.h"
#include "ffserver_config.h"

Go to the source code of this file.

Data Structures

struct  RTSPActionServerSetup
 
struct  DataRateData
 
struct  HTTPContext
 

Macros

#define closesocket   close
 
#define PATH_LENGTH   1024
 
#define IOBUFFER_INIT_SIZE   8192
 
#define HTTP_REQUEST_TIMEOUT   (15 * 1000)
 
#define RTSP_REQUEST_TIMEOUT   (3600 * 24 * 1000)
 
#define SYNC_TIMEOUT   (10 * 1000)
 
#define COPY(a)   st->a = lst->a;
 
#define SPACE_CHARS   " \t\r\n"
 
#define PBUFFER_SIZE   2048
 
#define CHECK_CODEC(x)   (ccf->codecpar->x != ccs->codecpar->x)
 

Enumerations

enum  HTTPState {
  HTTPSTATE_WAIT_REQUEST, HTTPSTATE_SEND_HEADER, HTTPSTATE_SEND_DATA_HEADER, HTTPSTATE_SEND_DATA,
  HTTPSTATE_SEND_DATA_TRAILER, HTTPSTATE_RECEIVE_DATA, HTTPSTATE_WAIT_FEED, HTTPSTATE_READY,
  RTSPSTATE_WAIT_REQUEST, RTSPSTATE_SEND_REPLY, RTSPSTATE_SEND_PACKET
}
 
enum  RedirType {
  REDIR_NONE, REDIR_ASX, REDIR_RAM, REDIR_ASF,
  REDIR_RTSP, REDIR_SDP
}
 

Functions

static void new_connection (int server_fd, int is_rtsp)
 
static void close_connection (HTTPContext *c)
 
static int handle_connection (HTTPContext *c)
 
static void print_stream_params (AVIOContext *pb, FFServerStream *stream)
 
static void compute_status (HTTPContext *c)
 
static int open_input_stream (HTTPContext *c, const char *info)
 
static int http_parse_request (HTTPContext *c)
 
static int http_send_data (HTTPContext *c)
 
static int http_start_receive_data (HTTPContext *c)
 
static int http_receive_data (HTTPContext *c)
 
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_interrupt (HTTPContext *c, const char *url, RTSPMessageHeader *h, int pause_only)
 
static int prepare_sdp_description (FFServerStream *stream, uint8_t **pbuffer, struct in_addr my_ip)
 
static HTTPContextrtp_new_connection (struct sockaddr_in *from_addr, FFServerStream *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)
 add a new RTP stream in an RTP connection (used in RTSP SETUP command). More...
 
static size_t htmlencode (const char *src, char **dest)
 Substitutes known conflicting chars on a text string with their corresponding HTML entities. More...
 
static void cp_html_entity (char *buffer, const char *entity)
 
static int check_codec_match (LayeredAVStream *ccf, AVStream *ccs, int stream)
 
static void unlayer_stream (AVStream *st, LayeredAVStream *lst)
 
static int64_t ffm_read_write_index (int fd)
 
static int ffm_write_write_index (int fd, int64_t pos)
 
static void ffm_set_write_index (AVFormatContext *s, int64_t pos, int64_t file_size)
 
static char * ctime1 (char *buf2, size_t buf_size)
 
static void http_vlog (const char *fmt, va_list vargs)
 
static void http_log (const char *fmt,...)
 
static void http_av_log (void *ptr, int level, const char *fmt, va_list vargs)
 
static void log_connection (HTTPContext *c)
 
static void update_datarate (DataRateData *drd, int64_t count)
 
static int compute_datarate (DataRateData *drd, int64_t count)
 
static void start_children (FFServerStream *feed)
 
static int socket_open_listen (struct sockaddr_in *my_addr)
 
static void start_multicast (void)
 
static int http_server (void)
 
static void start_wait_request (HTTPContext *c, int is_rtsp)
 
static void http_send_too_busy_reply (int fd)
 
static int extract_rates (char *rates, int ratelen, const char *request)
 
static int find_stream_in_feed (FFServerStream *feed, AVCodecParameters *codec, int bit_rate)
 
static int modify_current_stream (HTTPContext *c, char *rates)
 
static void get_word (char *buf, int buf_size, const char **pp)
 
static FFServerIPAddressACLparse_dynamic_acl (FFServerStream *stream, HTTPContext *c)
 
static void free_acl_list (FFServerIPAddressACL *in_acl)
 
static int validate_acl_list (FFServerIPAddressACL *in_acl, HTTPContext *c)
 
static int validate_acl (FFServerStream *stream, HTTPContext *c)
 
static void compute_real_filename (char *filename, int max_size)
 compute the real filename of a file by matching it without its extensions to all the stream's filenames More...
 
static void fmt_bytecount (AVIOContext *pb, int64_t count)
 
static void clean_html (char *clean, int clean_len, char *dirty)
 
static int64_t get_server_clock (HTTPContext *c)
 
static int64_t get_packet_send_clock (HTTPContext *c)
 
static int http_prepare_data (HTTPContext *c)
 
static void rtsp_reply_header (HTTPContext *c, enum RTSPStatusCode error_number)
 
static void rtsp_reply_error (HTTPContext *c, enum RTSPStatusCode error_number)
 
static HTTPContextfind_rtp_session (const char *session_id)
 
static RTSPTransportFieldfind_transport (RTSPMessageHeader *h, enum RTSPLowerTransport lower_transport)
 
static HTTPContextfind_rtp_session_with_url (const char *url, const char *session_id)
 find an RTP connection by using the session ID. More...
 
static LayeredAVStreamadd_av_stream1 (FFServerStream *stream, AVCodecContext *codec, int copy)
 
static int add_av_stream (FFServerStream *feed, LayeredAVStream *st)
 
static void remove_stream (FFServerStream *stream)
 
static void build_file_streams (void)
 
static int build_feed_streams (void)
 
static void compute_bandwidth (void)
 
static void handle_child_exit (int sig)
 
static void opt_debug (void)
 
void show_help_default (const char *opt, const char *arg)
 Per-fftool specific help handler. More...
 
int main (int argc, char **argv)
 

Variables

const char program_name [] = "ffserver"
 program name, defined by the program for show_version(). More...
 
const int program_birth_year = 2000
 program birth year, defined by the program for show_banner() More...
 
static const OptionDef options []
 
static const char *const http_state []
 
static HTTPContextfirst_http_ctx
 
static FFServerConfig config
 
static const char * my_program_name
 
static int no_launch
 
static int need_to_start_children
 
static unsigned int nb_connections
 
static uint64_t current_bandwidth
 
static int64_t cur_time
 
static AVLFG random_state
 
static FILE * logfile = NULL
 

Detailed Description

multiple format streaming server based on the FFmpeg libraries

Definition in file ffserver.c.

Macro Definition Documentation

#define closesocket   close
#define PATH_LENGTH   1024

Definition at line 69 of file ffserver.c.

Referenced by start_children().

#define IOBUFFER_INIT_SIZE   8192

Definition at line 107 of file ffserver.c.

Referenced by new_connection(), and rtp_new_connection().

#define HTTP_REQUEST_TIMEOUT   (15 * 1000)

Definition at line 110 of file ffserver.c.

Referenced by start_wait_request().

#define RTSP_REQUEST_TIMEOUT   (3600 * 24 * 1000)

Definition at line 111 of file ffserver.c.

Referenced by start_wait_request().

#define SYNC_TIMEOUT   (10 * 1000)

Definition at line 113 of file ffserver.c.

#define COPY (   a)    st->a = lst->a;

Referenced by register_all(), and unlayer_stream().

#define SPACE_CHARS   " \t\r\n"
#define PBUFFER_SIZE   2048

Referenced by prepare_sdp_description().

#define CHECK_CODEC (   x)    (ccf->codecpar->x != ccs->codecpar->x)

Referenced by check_codec_match().

Enumeration Type Documentation

enum HTTPState
Enumerator
HTTPSTATE_WAIT_REQUEST 
HTTPSTATE_SEND_HEADER 
HTTPSTATE_SEND_DATA_HEADER 
HTTPSTATE_SEND_DATA 
HTTPSTATE_SEND_DATA_TRAILER 
HTTPSTATE_RECEIVE_DATA 
HTTPSTATE_WAIT_FEED 
HTTPSTATE_READY 
RTSPSTATE_WAIT_REQUEST 
RTSPSTATE_SEND_REPLY 
RTSPSTATE_SEND_PACKET 

Definition at line 76 of file ffserver.c.

enum RedirType
Enumerator
REDIR_NONE 
REDIR_ASX 
REDIR_RAM 
REDIR_ASF 
REDIR_RTSP 
REDIR_SDP 

Definition at line 1415 of file ffserver.c.

Function Documentation

static void new_connection ( int  server_fd,
int  is_rtsp 
)
static

Definition at line 839 of file ffserver.c.

Referenced by http_server().

static void close_connection ( HTTPContext c)
static

Definition at line 890 of file ffserver.c.

Referenced by handle_connection(), http_server(), and rtsp_cmd_interrupt().

static int handle_connection ( HTTPContext c)
static

Definition at line 975 of file ffserver.c.

Referenced by http_server().

static void print_stream_params ( AVIOContext pb,
FFServerStream stream 
)
inlinestatic

Definition at line 1876 of file ffserver.c.

Referenced by compute_status().

static void compute_status ( HTTPContext c)
static

Definition at line 1950 of file ffserver.c.

Referenced by http_parse_request().

static int open_input_stream ( HTTPContext c,
const char *  info 
)
static
static int http_parse_request ( HTTPContext c)
static

Definition at line 1425 of file ffserver.c.

Referenced by handle_connection().

static int http_send_data ( HTTPContext c)
static

Definition at line 2534 of file ffserver.c.

Referenced by handle_connection().

static int http_start_receive_data ( HTTPContext c)
static

Definition at line 2655 of file ffserver.c.

Referenced by http_parse_request().

static int http_receive_data ( HTTPContext c)
static

Definition at line 2717 of file ffserver.c.

Referenced by handle_connection().

static int rtsp_parse_request ( HTTPContext c)
static

Definition at line 2911 of file ffserver.c.

Referenced by handle_connection().

static void rtsp_cmd_describe ( HTTPContext c,
const char *  url 
)
static

Definition at line 3055 of file ffserver.c.

Referenced by rtsp_parse_request().

static void rtsp_cmd_options ( HTTPContext c,
const char *  url 
)
static

Definition at line 3045 of file ffserver.c.

Referenced by rtsp_parse_request().

static void rtsp_cmd_setup ( HTTPContext c,
const char *  url,
RTSPMessageHeader h 
)
static

Definition at line 3130 of file ffserver.c.

Referenced by rtsp_parse_request().

static void rtsp_cmd_play ( HTTPContext c,
const char *  url,
RTSPMessageHeader h 
)
static

Definition at line 3313 of file ffserver.c.

Referenced by rtsp_parse_request().

static void rtsp_cmd_interrupt ( HTTPContext c,
const char *  url,
RTSPMessageHeader h,
int  pause_only 
)
static

Definition at line 3339 of file ffserver.c.

Referenced by rtsp_parse_request().

static int prepare_sdp_description ( FFServerStream stream,
uint8_t **  pbuffer,
struct in_addr  my_ip 
)
static

Definition at line 3000 of file ffserver.c.

Referenced by http_parse_request(), and rtsp_cmd_describe().

static HTTPContext * rtp_new_connection ( struct sockaddr_in *  from_addr,
FFServerStream stream,
const char *  session_id,
enum RTSPLowerTransport  rtp_protocol 
)
static

Definition at line 3373 of file ffserver.c.

Referenced by rtsp_cmd_setup(), and start_multicast().

static int rtp_new_av_stream ( HTTPContext c,
int  stream_index,
struct sockaddr_in *  dest_addr,
HTTPContext rtsp_c 
)
static

add a new RTP stream in an RTP connection (used in RTSP SETUP command).

If RTP/TCP protocol is used, TCP connection 'rtsp_c' is used.

Definition at line 3442 of file ffserver.c.

Referenced by rtsp_cmd_setup(), and start_multicast().

static size_t htmlencode ( const char *  src,
char **  dest 
)
static

Substitutes known conflicting chars on a text string with their corresponding HTML entities.

Returns the number of bytes in the 'encoded' representation not including the terminating NUL.

Definition at line 287 of file ffserver.c.

Referenced by http_parse_request().

static void cp_html_entity ( char *  buffer,
const char *  entity 
)
inlinestatic

Definition at line 273 of file ffserver.c.

Referenced by htmlencode().

static int check_codec_match ( LayeredAVStream ccf,
AVStream ccs,
int  stream 
)
inlinestatic

Definition at line 3690 of file ffserver.c.

Referenced by build_feed_streams().

static void unlayer_stream ( AVStream st,
LayeredAVStream lst 
)
static
static int64_t ffm_read_write_index ( int  fd)
static

Definition at line 348 of file ffserver.c.

Referenced by build_feed_streams(), and http_start_receive_data().

static int ffm_write_write_index ( int  fd,
int64_t  pos 
)
static

Definition at line 359 of file ffserver.c.

Referenced by http_receive_data(), and http_start_receive_data().

static void ffm_set_write_index ( AVFormatContext s,
int64_t  pos,
int64_t  file_size 
)
static

Definition at line 377 of file ffserver.c.

Referenced by http_prepare_data().

static char* ctime1 ( char *  buf2,
size_t  buf_size 
)
static

Definition at line 385 of file ffserver.c.

Referenced by http_vlog().

static void http_vlog ( const char *  fmt,
va_list  vargs 
)
static

Definition at line 403 of file ffserver.c.

Referenced by http_av_log(), and http_log().

static void http_log ( const char *  fmt,
  ... 
)
static
static void http_av_log ( void ptr,
int  level,
const char *  fmt,
va_list  vargs 
)
static

Definition at line 431 of file ffserver.c.

Referenced by main().

static void log_connection ( HTTPContext c)
static

Definition at line 443 of file ffserver.c.

Referenced by http_server().

static void update_datarate ( DataRateData drd,
int64_t  count 
)
static

Definition at line 453 of file ffserver.c.

Referenced by http_receive_data(), and http_send_data().

static int compute_datarate ( DataRateData drd,
int64_t  count 
)
static

Definition at line 467 of file ffserver.c.

Referenced by compute_status().

static void start_children ( FFServerStream feed)
static

FIXME: WIP Safeguard. Remove after clearing all harcoded '1024' path lengths

Definition at line 476 of file ffserver.c.

Referenced by http_server().

static int socket_open_listen ( struct sockaddr_in *  my_addr)
static

Definition at line 558 of file ffserver.c.

Referenced by http_server().

static void start_multicast ( void  )
static

Definition at line 597 of file ffserver.c.

Referenced by http_server().

static int http_server ( void  )
static

Definition at line 657 of file ffserver.c.

Referenced by main().

static void start_wait_request ( HTTPContext c,
int  is_rtsp 
)
static

Definition at line 807 of file ffserver.c.

Referenced by handle_connection(), and new_connection().

static void http_send_too_busy_reply ( int  fd)
static

Definition at line 817 of file ffserver.c.

Referenced by new_connection().

static int extract_rates ( char *  rates,
int  ratelen,
const char *  request 
)
static

Definition at line 1156 of file ffserver.c.

Referenced by http_parse_request().

static int find_stream_in_feed ( FFServerStream feed,
AVCodecParameters codec,
int  bit_rate 
)
static

Definition at line 1203 of file ffserver.c.

Referenced by modify_current_stream().

static int modify_current_stream ( HTTPContext c,
char *  rates 
)
static

Definition at line 1241 of file ffserver.c.

Referenced by http_parse_request().

static void get_word ( char *  buf,
int  buf_size,
const char **  pp 
)
static

Definition at line 1281 of file ffserver.c.

Referenced by http_parse_request(), and rtsp_parse_request().

static FFServerIPAddressACL* parse_dynamic_acl ( FFServerStream stream,
HTTPContext c 
)
static

Definition at line 1301 of file ffserver.c.

Referenced by validate_acl().

static void free_acl_list ( FFServerIPAddressACL in_acl)
static

Definition at line 1342 of file ffserver.c.

Referenced by validate_acl().

static int validate_acl_list ( FFServerIPAddressACL in_acl,
HTTPContext c 
)
static

Definition at line 1354 of file ffserver.c.

Referenced by validate_acl().

static int validate_acl ( FFServerStream stream,
HTTPContext c 
)
static

Definition at line 1371 of file ffserver.c.

Referenced by http_parse_request().

static void compute_real_filename ( char *  filename,
int  max_size 
)
static

compute the real filename of a file by matching it without its extensions to all the stream's filenames

Definition at line 1392 of file ffserver.c.

Referenced by http_parse_request().

static void fmt_bytecount ( AVIOContext pb,
int64_t  count 
)
static

Definition at line 1866 of file ffserver.c.

Referenced by compute_status().

static void clean_html ( char *  clean,
int  clean_len,
char *  dirty 
)
static

Definition at line 1922 of file ffserver.c.

Referenced by compute_status().

static int64_t get_server_clock ( HTTPContext c)
static

Definition at line 2273 of file ffserver.c.

Referenced by http_send_data().

static int64_t get_packet_send_clock ( HTTPContext c)
static

Definition at line 2280 of file ffserver.c.

Referenced by http_send_data().

static int http_prepare_data ( HTTPContext c)
static

Definition at line 2294 of file ffserver.c.

Referenced by http_send_data().

static void rtsp_reply_header ( HTTPContext c,
enum RTSPStatusCode  error_number 
)
static
static void rtsp_reply_error ( HTTPContext c,
enum RTSPStatusCode  error_number 
)
static
static HTTPContext* find_rtp_session ( const char *  session_id)
static

Definition at line 3103 of file ffserver.c.

Referenced by find_rtp_session_with_url(), and rtsp_cmd_setup().

static RTSPTransportField* find_transport ( RTSPMessageHeader h,
enum RTSPLowerTransport  lower_transport 
)
static

Definition at line 3117 of file ffserver.c.

Referenced by rtsp_cmd_setup().

static HTTPContext* find_rtp_session_with_url ( const char *  url,
const char *  session_id 
)
static

find an RTP connection by using the session ID.

Check consistency with filename

Definition at line 3279 of file ffserver.c.

Referenced by rtsp_cmd_interrupt(), and rtsp_cmd_play().

static LayeredAVStream* add_av_stream1 ( FFServerStream stream,
AVCodecContext codec,
int  copy 
)
static

Definition at line 3541 of file ffserver.c.

Referenced by add_av_stream(), and build_file_streams().

static int add_av_stream ( FFServerStream feed,
LayeredAVStream st 
)
static

Definition at line 3576 of file ffserver.c.

Referenced by build_feed_streams().

static void remove_stream ( FFServerStream stream)
static

Definition at line 3618 of file ffserver.c.

Referenced by build_file_streams().

static void build_file_streams ( void  )
static

Definition at line 3631 of file ffserver.c.

Referenced by main().

static int build_feed_streams ( void  )
static

Definition at line 3724 of file ffserver.c.

Referenced by main().

static void compute_bandwidth ( void  )
static

Definition at line 3882 of file ffserver.c.

Referenced by main().

static void handle_child_exit ( int  sig)
static

Definition at line 3905 of file ffserver.c.

Referenced by main().

static void opt_debug ( void  )
static

Definition at line 3934 of file ffserver.c.

void show_help_default ( const char *  opt,
const char *  arg 
)

Per-fftool specific help handler.

Implemented in each fftool, called by show_help().

Definition at line 3940 of file ffserver.c.

Referenced by show_help().

int main ( int  argc,
char **  argv 
)

Definition at line 3956 of file ffserver.c.

Variable Documentation

const char program_name[] = "ffserver"

program name, defined by the program for show_version().

Definition at line 71 of file ffserver.c.

Referenced by compute_status(), expand_filename_template(), init_report(), print_program_info(), show_help_default(), show_license(), and show_usage().

const int program_birth_year = 2000

program birth year, defined by the program for show_banner()

Definition at line 72 of file ffserver.c.

Referenced by print_program_info().

const OptionDef options[]
static
Initial value:
= {
{ "n", OPT_BOOL, {(void *)&no_launch }, "enable no-launch mode" },
{ "d", 0, {(void*)opt_debug}, "enable debug mode" },
{ "f", HAS_ARG | OPT_STRING, {(void*)&config.filename }, "use configfile instead of /etc/ffserver.conf", "configfile" },
{ NULL },
}
#define NULL
Definition: coverity.c:32
static FFServerConfig config
Definition: ffserver.c:193
#define CMDUTILS_COMMON_OPTIONS
Definition: cmdutils.h:231
static void opt_debug(void)
Definition: ffserver.c:3934
#define HAS_ARG
Definition: cmdutils.h:166
#define OPT_STRING
Definition: cmdutils.h:169
#define OPT_BOOL
Definition: cmdutils.h:167
static int no_launch
Definition: ffserver.c:243

Definition at line 74 of file ffserver.c.

const char* const http_state[]
static
Initial value:
= {
"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",
}

Definition at line 91 of file ffserver.c.

Referenced by compute_status().

HTTPContext* first_http_ctx
static
FFServerConfig config
static
Initial value:
= {
.nb_max_http_connections = 2000,
.nb_max_connections = 5,
.max_bandwidth = 1000,
.use_defaults = 1,
}

Definition at line 193 of file ffserver.c.

Referenced by celt_search_for_tf(), dshow_cycle_formats(), dshow_set_audio_buffer_size(), parse_dsd_prop(), parse_fmtp_config(), vaapi_frames_get_constraints(), and wavpack_encode_init().

const char* my_program_name
static

Definition at line 241 of file ffserver.c.

Referenced by main(), and start_children().

int no_launch
static

Definition at line 243 of file ffserver.c.

Referenced by start_children().

int need_to_start_children
static

Definition at line 244 of file ffserver.c.

Referenced by handle_child_exit(), and http_server().

unsigned int nb_connections
static
uint64_t current_bandwidth
static
int64_t cur_time
static
AVLFG random_state
static

Definition at line 254 of file ffserver.c.

FILE* logfile = NULL
static

Definition at line 256 of file ffserver.c.

Referenced by http_vlog(), and main().