⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netcam.c

📁 motion motion
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *      netcam.c * *      Module for handling network cameras. * *      This code was inspired by the original netcam.c module *      written by Jeroen Vreeken and enhanced by several Motion *      project contributors, particularly Angel Carpintero and *      Christopher Price. * *      Copyright 2005, William M. Brack *      This software is distributed under the GNU Public license *      Version 2.  See also the file 'COPYING'. * * *      When a netcam has been configured, instead of using the routines *      within video.c (which handle a CCTV-type camera) the routines *      within this module are used.  There are only four entry points - *      one for "starting up" the camera (netcam_start), for "fetching a *      picture" from it (netcam_next), one for cleanup at the end of a *      run (netcam_cleanup), and a utility routine for receiving data *      from the camera (netcam_recv). * *      Two quite different types of netcams are handled.  The simplest *      one is the type which supplies a single JPEG frame each time it *      is accessed.  The other type is one which supplies an mjpeg *      stream of data. * *      For each of these cameras, the routine taking care of the netcam *      will start up a completely separate thread (which I call the "camera *      handler thread" within subsequent comments).  For a streaming camera, *      this handler will receive the mjpeg stream of data from the camera, *      and save the latest complete image when it begins to work on the next *      one.  For the non-streaming version, this handler will be "triggered" *      (signalled) whenever the main motion-loop asks for a new image, and *      will start to fetch the next image at that time.  For either type, *      the most recent image received from the camera will be returned to *      motion. */#include "motion.h"#include <netdb.h>#include <netinet/in.h>#include <regex.h>                  /* For parsing of the URL *///#include <stdio.h>//#include <stdlib.h>//#include <string.h>#include <sys/socket.h>//#include <sys/types.h>#include "netcam_ftp.h"#define CONNECT_TIMEOUT        10   /* timeout on remote connection attempt */#define READ_TIMEOUT            5   /* default timeout on recv requests */#define POLLING_TIMEOUT  READ_TIMEOUT /* file polling timeout [s] */#define POLLING_TIME  500*1000*1000 /* file polling time quantum [ns] (500ms) */#define MAX_HEADER_RETRIES      5   /* Max tries to find a header record */#define MINVAL(x, y) ((x) < (y) ? (x) : (y))/* * The macro NETCAM_DEBUG is for development testing of this module. * The macro SETUP is to assure that "configuration-setup" type messages * are also printed when NETCAM_DEBUG is set.  Set the following #if to * 1 to enable it, or 0 (normal setting) to disable it. */#define SETUP    ((cnt->conf.setup_mode) || (debug_level >= CAMERA_INFO))tfile_context *file_new_context(void);void file_free_context(tfile_context* ctxt);/* These strings are used for the HTTP connection */static const char    *connect_req;static const char    *connect_req_http10 = "GET %s HTTP/1.0\r\n"                      "Host: %s\r\n"                      "User-Agent: Motion-netcam/" VERSION "\r\n";static const char    *connect_req_http11 = "GET %s HTTP/1.1\r\n"                      "Host: %s\r\n"                      "User-Agent: Motion-netcam/" VERSION "\r\n";static const char    *connect_req_close = "Connection: close\r\n";static const char    *connect_req_keepalive = "Connection: Keep-Alive\r\n";static const char    *connect_auth_req = "Authorization: Basic %s\r\n";/* * The following three routines (netcam_url_match, netcam_url_parse and * netcam_url_free are for 'parsing' (i.e. separating into the relevant * components) the URL provided by the user.  They make use of regular * expressions (which is outside the scope of this module, so detailed * comments are not provided).  netcam_url_parse is called from netcam_start, * and puts the "broken-up" components of the URL into the "url" element of * the netcam_context structure. * * Note that the routines are not "very clever", but they work sufficiently * well for the limited requirements of this module.  The expression: *   (http)://(((.*):(.*))@)?([^/:]|[-.a-z0-9]+)(:([0-9]+))?($|(/[^:]*)) * requires *   1) a string which begins with 'http', followed by '://' *   2) optionally a '@' which is preceded by two strings *      (with 0 or more characters each) separated by a ':' *      [this is for an optional username:password] *   3) a string comprising alpha-numerics, '-' and '.' characters *      [this is for the hostname] *   4) optionally a ':' followed by one or more numeric characters *      [this is for an optional port number] *   5) finally, either an end of line or a series of segments, *      each of which begins with a '/', and contains anything *      except a ':' *//** * netcam_url_match * *      Finds the matched part of a regular expression * * Parameters: * *      m          A structure containing the regular expression to be used *      input      The input string * * Returns:        The string which was matched * */static char *netcam_url_match(regmatch_t m, const char *input){	char *match = NULL;	int len;	if (m.rm_so != -1) {		len = m.rm_eo - m.rm_so;		if ((match = (char *) malloc(len + 1)) != NULL) {			strncpy(match, input + m.rm_so, len);			match[len] = '\0';		}	}	return (match);}/** * netcam_url_parse * *      parses a string containing a URL into it's components * * Parameters: *      parse_url          A structure which will receive the results *                         of the parsing *      text_url           The input string containing the URL * * Returns:                Nothing * */static void netcam_url_parse(struct url_t *parse_url, const char *text_url){	char *s;	int i;	const char *re = "(http|ftp)://(((.*):(.*))@)?"	                 "([^/:]|[-.a-z0-9]+)(:([0-9]+))?($|(/[^:]*))";	regex_t pattbuf;	regmatch_t matches[10];	if( !strncmp( text_url, "file", 4 ) ) 		re = "(file)://(((.*):(.*))@)?"		     "([^/:]|[-.a-z0-9]*)(:([0-9]*))?($|(/[^:][/-_.a-z0-9]+))";	if (debug_level > CAMERA_DEBUG)		motion_log(-1, 0, "Entry netcam_url_parse data %s", text_url );	memset(parse_url, 0, sizeof(struct url_t));	/*	 * regcomp compiles regular expressions into a form that is	 * suitable for regexec searches	 * regexec matches the URL string against the regular expression	 * and returns an array of pointers to strings matching each match	 * within (). The results that we need are finally placed in parse_url	 */	if (!regcomp(&pattbuf, re, REG_EXTENDED | REG_ICASE)) {		if (regexec(&pattbuf, text_url, 10, matches, 0) != REG_NOMATCH) {			for (i = 0; i < 10; i++) {				if ((s = netcam_url_match(matches[i], text_url)) != NULL) {					if (debug_level > CAMERA_DEBUG)						motion_log(-1, 0, "Parse case %d data %s", i, s );					switch (i) {						case 1:							parse_url->service = s;							break;						case 3:							parse_url->userpass = s;							break;						case 6:							parse_url->host = s;							break;						case 8:							parse_url->port = atoi(s);							free(s);							break;						case 9:							parse_url->path = s;							break;						/* other components ignored */						default:							free(s);							break;					}				}			}		}	}	if ((!parse_url->port) && (parse_url->service)){		if (!strcmp(parse_url->service, "http"))			parse_url->port = 80;		else if (!strcmp(parse_url->service, "ftp"))			parse_url->port = 21;	}	regfree(&pattbuf);}/** * netcam_url_free * *      General cleanup of the URL structure, called from netcam_cleanup. * * Parameters: * *      parse_url       Structure containing the parsed data * * Returns:             Nothing * */static void netcam_url_free(struct url_t *parse_url){	if (parse_url->service) {		free(parse_url->service);		parse_url->service = NULL;	}	if (parse_url->userpass) {		free(parse_url->userpass);		parse_url->userpass = NULL;	}	if (parse_url->host) {		free(parse_url->host);		parse_url->host = NULL;	}	if (parse_url->path) {		free(parse_url->path);		parse_url->path = NULL;	}}/** * check_quote * *      Checks a string to see if it's quoted, and if so removes the *      quotes. * * Parameters: * *      str             Pointer to a string * * Returns:             Nothing, but updates the target if necessary * */static void check_quote(char *str){	int len;	char ch;	ch = *str;	if ((ch == '"') || (ch == '\'')) {		len = strlen(str) - 1;		if (str[len] == ch) {			memmove(str, str+1, len-1);			str[len-1] = 0;		}	}}/** * netcam_check_content_length * * 	Analyse an HTTP-header line to see if it is a Content-length * * Parameters: * *      header          Pointer to a string containing the header line * * Returns: *      -1              Not a Content-length line *      >=0             Value of Content-length field * */static long netcam_check_content_length(char *header){	long length=-1;	/* note this is a long, not an int */	if (!header_process(header, "Content-Length", header_extract_number, &length)) {		/*		 * Some netcams deliver some bad-format data, but if		 * we were able to recognize the header section and the		 * number we might as well try to use it.		 */		if (length > 0)			return length;		return -1;	}	return length;}/** * netcam_check_keepalive * * 	Analyse an HTTP-header line to see if it is a Keep-Alive. * * Parameters: * *      header          Pointer to a string containing the header line * * Returns: *      -1              Not a Keep-Alive line *      1               Is a Keep-Alive line * */static int netcam_check_keepalive(char *header){	char *content_type = NULL;	if (!header_process(header, "Keep-Alive", http_process_type, &content_type))		return -1;	/* We do not detect the second field or other case mixes at present. */	if (content_type) 		free(content_type);	return 1;}/** * netcam_check_close * * 	Analyse an HTTP-header line to see if it is a Connection: close * * Parameters: * *      header          Pointer to a string containing the header line * * Returns: *      -1              Not a Connection: close *      1               Is a Connection: close * */static int netcam_check_close(char *header){	char *type = NULL;	int ret=-1;	if (!header_process(header, "Connection", http_process_type, &type))		return -1;		if (!strcmp(type, "close")) /* strcmp returns 0 for match */		ret=1;		if (type) 		free(type);	return ret;}/** * netcam_check_content_type * * 	Analyse an HTTP-header line to see if it is a Content-type * * Parameters: * *      header          Pointer to a string containing the header line * * Returns: *      -1              Not a Content-type line *      0               Content-type not recognized *      1               image/jpeg *      2               multipart/x-mixed-replace or multipart/mixed * */static int netcam_check_content_type(char *header){	char *content_type = NULL;	int ret;	if (!header_process(header, "Content-type", http_process_type, &content_type))		return -1;	if (!strcmp(content_type, "image/jpeg")) {		ret = 1;	} else if (!strcmp(content_type, "multipart/x-mixed-replace") ||	           !strcmp(content_type, "multipart/mixed")) {		ret = 2;	} else		ret = 0;	if (content_type)		free(content_type);	return ret;}/** * netcam_read_next_header

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -