?? netcam.c
字號:
/* * 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 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))/* These strings are used for the HTTP connection */static const char *connect_req = "GET %s HTTP/1.0\r\n" "Host: %s\r\n" "User-Agent: Motion-netcam/" VERSION "\r\n" "Connection: close\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]; 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) { 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_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 * * Read the next header record from the camera. * * Parameters * * netcam pointer to a netcam_context * * Returns: 0 for success, -1 if any error * */static int netcam_read_next_header(netcam_context_ptr netcam){ int retval; char *header; /* * return if not connected */ if (netcam->sock == -1) return -1; /* * We are expecting a header which *must* contain a mime-type of * image/jpeg, and *might* contain a Content-Length. * * If this is a "streaming" camera, the header *must* be preceded * by a "boundary" string. * */ netcam->caps.content_length = 0; /* * If this is a "streaming" camera, the stream header must be * preceded by a "boundary" string */ if (netcam->caps.streaming) { while (1) { retval = header_get(netcam, &header, HG_NONE); if (retval != HG_OK) { motion_log(LOG_ERR, 0, "Error reading image header"); free(header); return -1; } retval = (strstr(header, netcam->boundary) == NULL); free(header); if (!retval) break; } } while (1) { retval = header_get(netcam, &header, HG_NONE); if (retval != HG_OK) { motion_log(LOG_ERR, 0, "Error reading image header"); free(header); return -1; } if (*header == 0) break; if ((retval = netcam_check_content_type(header)) >= 0) { if (retval != 1) { motion_log(LOG_ERR, 0, "Header not JPEG"); free(header); return -1; } } if ((retval = (int) netcam_check_content_length(header)) > 0) { netcam->caps.content_length = 1; /* set flag */ netcam->receiving->content_length = (int) retval; } free(header); } if (debug_level > CAMERA_INFO) motion_log(-1, 0, "Found image header record"); free(header); return 0;}/** * netcam_read_first_header * * This routine attempts to read a header record from the netcam. If * successful, it analyses the header to determine whether the camera is * a "streaming" type. If it is, the routine looks for the Boundary-string; * if found, it positions just past the string so that the image header can * be read. It then reads the image header and continues processing that * header as well. * * If the camera does not appear to be a streaming type, it is assumed that the * header just read was the image header. It is processed to determine whether * a Content-length is present. * * After this processing, the routine returns to the caller. * * Parameters: * netcam Pointer to the netcam_context structure * * Returns: Content-type code if successful, -1 if not * */static int netcam_read_first_header(netcam_context_ptr netcam){ int retval = -2; /* "Unknown err" */ int ret; int firstflag = 1; char *header; char *boundary; struct context *cnt = netcam->cnt; /* for conf debug_level */ /* Send the initial command to the camera */ if (send(netcam->sock, netcam->connect_request, strlen(netcam->connect_request), 0) < 0) { motion_log(LOG_ERR, 1, "Error sending 'connect' request"); return -1; } /* * We expect to get back an HTTP header from the camera. * Successive calls to header_get will return each line * of the header received. We will continue reading until * a blank line is received. * * As we process the header, we are looking for either of * header lines Content-type or Content-length. Content-type * is used to determine whether the camera is "streaming" or * "non-streaming", and Content-length will be used to determine * whether future reads of images will be controlled by the * length specified before the image, or by a boundary string. * * The Content-length will only be present "just before" an * image is sent (if it is present at all). That means that, if * this is a "streaming" camera, it will not be present in the * "first header", but will occur later (after a boundary-string). * For a non-streaming camera, however, there is no boundary-string, * and the first header is, in fact, the only header. In this case, * there may be a Content-length. * */ while (1) { /* 'Do forever' */ ret = header_get(netcam, &header, HG_NONE); if (ret != HG_OK) { if (debug_level > 5) motion_log(LOG_ERR, 0, "Error reading first header (%s)", header); free(header);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -