MJPG-streamer源码分析-输出部分

MJPG-streamer源码分析-输出部分

MJPG-streamer可以创建多个输出,简单的说,根据主函数中输入的参数解析的结果,确定输出通道的个数,至少保证有一个输出通道在程序运行时存在。从参数解析结果确定每个输出通道的参数,并以这些参数为每个通道创建发送线程。在每个发送线程上,不断侦听是否有连接请求。每当有连接请求,在未达到最高连接数目时,为每个连接请求创建连接线程。在连接线程中,根据参数,确实发送方式是stream?snapshot?或者是其他方式。在连接请求不关闭时,连接线程一直存在,连接请求退出时,线程随之退出,并有系统自动释放其所分配的资源。当程序运行终止信号stop不为1时,输出通道线程不断侦听连接和发送数据,当终止信号被置1时,退出输出线程,同时释放其所分配的资源。

输出通道的初始化部分仅仅是将输出通道的参数分别解析和分配到各自通道指定的变量中,为后续每个通道的run函数提供参数。其余的,都是run函数所执行部分,包括其执行的子函数和操作等。

需要说明的是,由于有多个输出通道,故而需要对每个通道都进行初始化和执行输出,这个过程通过一个循环体完成,循环体的控制变量就是输出通道的个数。

本文根据上述思路,把关键部分源码进行解释,具体的还得参考源码,如有错误,欢迎指出以便改正。

===============================================初始化部分====================================================

1、定义和初始化部分参数,这些参数用于在对输出通道参数解析过程中的临时变量;

[cpp]  view plain  copy
  1. char *argv[MAX_ARGUMENTS]={NULL};  
  2. int  argc=1, i;  
  3. int  port;  
  4. char *credentials, *www_folder;  
  5. char nocommands;  
  6.   
  7. port = htons(8080);  
  8. credentials = NULL;  
  9. www_folder = NULL;  
  10. nocommands = 0;  
  11. argv[0] = OUTPUT_PLUGIN_NAME;  

2、【解析参数】
将传入的单串参数param->parameter_string转换为字符串数组,存放在argv数组中,调用c = getopt_long_only(argc, argv, "", long_options, &option_index),当c==-1时,说明解析完毕,退出解析过程,否则根据option_index参数设置port,credentials,www_folder,nocommands等参数

这部分的代码与前面主程序和输入通道程序的解析过程类似,不再详述,都是把对应参数填写到指定的变量上。

3、根据param->id配置每个服务器线程参数(servers全局)

程序为每个输出通道都定义了一个对应的参数,用于配置和存储每个输出通道的信息。其结构体如下所示:

[cpp]  view plain  copy
  1. /* context of each server thread */  
  2. typedef struct {  
  3.   int sd;  
  4.   int id;  
  5.   globals *pglobal;  
  6.   pthread_t threadID;  
  7.   
  8.   config conf;  
  9. } context;  

经过上述的参数解析过程之后,用户输入的参数都被识别,之后再被存放到每个输出通道服务器线程参数变量上。即servers[param->id]。

[cpp]  view plain  copy
  1. servers[param->id].id = param->id;  
  2. servers[param->id].pglobal = param->global;  
  3. servers[param->id].conf.port = port;  
  4. servers[param->id].conf.credentials = credentials;  
  5. servers[param->id].conf.www_folder = www_folder;  
  6. servers[param->id].conf.nocommands = nocommands;  

至此,输出通道的初始化完毕,等待run函数的执行。

===============================================输出通道执行部分====================================================

 在每个输出通道上,先为每个通道创建服务线程,调用线程处理函数进行连接请求的侦听和处理

[cpp]  view plain  copy
  1. int output_run(int id) {  
  2.   DBG("launching server thread #%02d\n", id);  
  3.   
  4.   //server_thread位于httpd.c头文件中  
  5.   pthread_create(&(servers[id].threadID), NULL, server_thread, &(servers[id]));  
  6.   //将状态改为unjoinable状态,确保资源的释放,无须调用join函数释放资源  
  7.   pthread_detach(servers[id].threadID);  
  8.   
  9.   return 0;  
  10. }  

线程处理函数创建打开一个socket并等待连接请求,在未达到最大连接请求时,为每个请求创建相对应的客户端服务线程client_thread(),并使用通道线程相同的处理方式,将该子线程detach处理。子线程根据不同的请求方式,进行数据的分发。

[cpp]  view plain  copy
  1. void *server_thread( void *arg ) {  
  2.   //定义线程参数:服务端和客户端地址变量,客户端服务子线程  
  3.   struct sockaddr_in addr, client_addr;  
  4.   int on;  
  5.   pthread_t client;  
  6.   socklen_t addr_len = sizeof(struct sockaddr_in);  
  7.   
  8.   //每个服务器线程的参数  
  9.   context *pcontext = arg;  
  10.   pglobal = pcontext->pglobal;  
  11.   
  12.   /* set cleanup handler to cleanup ressources */  
  13.   pthread_cleanup_push(server_cleanup, pcontext);  
  14.   
  15.   /* 打开通道服务端线程 */  
  16.   pcontext->sd = socket(PF_INET, SOCK_STREAM, 0);  
  17.   if ( pcontext->sd < 0 ) {  
  18.     fprintf(stderr, "socket failed\n");  
  19.     exit(EXIT_FAILURE);  
  20.   }  
  21.   
  22.   //默认情况下,server重启,调用socket,bind,然后listen,会失败.因为该端口正在被使用.  
  23.   //一个端口释放后会等待两分钟之后才能再被使用,SO_REUSEADDR是让端口释放后立即就可以被再次使用。  
  24.   on = 1;  
  25.   if (setsockopt(pcontext->sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0) {  
  26.     perror("setsockopt(SO_REUSEADDR) failed");  
  27.     exit(EXIT_FAILURE);  
  28.   }  
  29.   
  30.   /* 配置服务器参数,用于监听 */  
  31.   memset(&addr, 0, sizeof(addr));  
  32.   addr.sin_family = AF_INET;  
  33.   addr.sin_port = pcontext->conf.port; /* is already in right byteorder */  
  34.   //宏INADDR_ANY代替本机的IP,无需手动选择,自动替换,当存在多个网卡情况下,自动选择  
  35.   addr.sin_addr.s_addr = htonl(INADDR_ANY);  
  36.   if ( bind(pcontext->sd, (struct sockaddr*)&addr, sizeof(addr)) != 0 ) {  
  37.     perror("bind");  
  38.     OPRINT("%s(): bind(%d) failed", __FUNCTION__, htons(pcontext->conf.port));  
  39.     closelog();  
  40.     exit(EXIT_FAILURE);  
  41.   }  
  42.   
  43.   //socket()函数创建的socket默认是一个主动类型的,listen函数将socket变为被动类型的,等待客户的连接请求。  
  44.   if ( listen(pcontext->sd, 10) != 0 ) {  
  45.     fprintf(stderr, "listen failed\n");  
  46.     exit(EXIT_FAILURE);  
  47.   }  
  48.   
  49.   //等待客户端连接,有连接则创建新的线程进行处理  
  50.   while ( !pglobal->stop ) {  
  51.     //cfd结构体定义在httpd.d  
  52.     cfd *pcfd = malloc(sizeof(cfd));  
  53.   
  54.     if (pcfd == NULL) {  
  55.       fprintf(stderr, "failed to allocate (a very small amount of) memory\n");  
  56.       exit(EXIT_FAILURE);  
  57.     }  
  58.   
  59.     DBG("waiting for clients to connect\n");  
  60.     pcfd->fd = accept(pcontext->sd, (struct sockaddr *)&client_addr, &addr_len);  
  61.     pcfd->pc = pcontext;  
  62.   
  63.     /* 创建客户端子线程处理连接请求 */  
  64.     DBG("create thread to handle client that just established a connection\n");  
  65.     syslog(LOG_INFO, "serving client: %s:%d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port));  
  66.   
  67.     if( pthread_create(&client, NULL, &client_thread, pcfd) != 0 ) {  
  68.       DBG("could not launch another client thread\n");  
  69.       close(pcfd->fd);  
  70.       free(pcfd);  
  71.       continue;  
  72.     }  
  73.     pthread_detach(client);  
  74.   }  
  75.   
  76.   DBG("leaving server thread, calling cleanup function now\n");  
  77.   pthread_cleanup_pop(1);  
  78.   
  79.   return NULL;  
  80. }  

其中,用于保存客户端子线程参数的结构体定义如下:

[cpp]  view plain  copy
  1. typedef struct {  
  2.   context *pc;  //保留父线程的变量  
  3.   int fd;       //保存监听accept返回的套接字描述符  
  4. } cfd;  

每个连接请求的处理都是在void *client_thread( void *arg )中进行。函数首先对连接请求的参数进行解析配置,根据req.type确定发送数据的方式:

[cpp]  view plain  copy
  1. /* thread for clients that connected to this server */  
  2. void *client_thread( void *arg ) {  
  3.   int cnt;  
  4.   char buffer[BUFFER_SIZE]={0}, *pb=buffer;  
  5.   iobuffer iobuf;  
  6.   request req;  
  7.   //本地连接请求文件描述变量  
  8.   cfd lcfd;            
  9.   
  10.   //如果传入参数不为空,则将参数的内容拷贝到 lcfd 中(参数为 pcfd ,不为空)  
  11.   if (arg != NULL) {  
  12.     memcpy(&lcfd, arg, sizeof(cfd));  
  13.     free(arg);  
  14.   }  
  15.   else  
  16.     return NULL;  
  17.   
  18.   /* 初始化结构体 */  
  19.   // 把iobuf清为0,iobuf变量在_readline函数中被使用,起一个临时缓存的作用  
  20.   // iobuf的level成员表示buffer中还剩多少字节的空间,而buffer成员用于存放数据  
  21.   init_iobuffer(&iobuf);  
  22.   //http协议,需要客服端给服务器发送一个请求,而request就是这个请求  
  23.   init_request(&req);  
  24.   
  25.   //从客服端接收数据,表示客服端发来的请求,确定给客服端发什么数据  
  26.   memset(buffer, 0, sizeof(buffer));  
  27.   //_readline()函数:从客服端中读取一行的数据,以换行符结束  
  28.   if ( (cnt = _readline(lcfd.fd, &iobuf, buffer, sizeof(buffer)-1, 5)) == -1 ) {  
  29.     close(lcfd.fd);  
  30.     return NULL;  
  31.   }  
  32.   
  33.   /* 解析参数 */  
  34.   if ( strstr(buffer, "GET /?action=snapshot") != NULL ) {  
  35.     //请求字符串中含有"GET /?action=snapshot",则请求类型为 A_SNAPSHOT(拍照类型)  
  36.     req.type = A_SNAPSHOT;  
  37.   }  
  38.   else if ( strstr(buffer, "GET /?action=stream") != NULL ) {  
  39.     //如果请求字符串中含有"GET /?action=stream",则请求类型为 A_STREAM(发送视频流类型)  
  40.     req.type = A_STREAM;  
  41.   }  
  42.   else if ( strstr(buffer, "GET /?action=command") != NULL ) {  
  43.     //命令请求,将请求后面的参数保存到 req.parameter  
  44.     int len;  
  45.     req.type = A_COMMAND;  
  46.   
  47.     /* advance by the length of known string */  
  48.     if ( (pb = strstr(buffer, "GET /?action=command")) == NULL ) {  
  49.       DBG("HTTP request seems to be malformed\n");  
  50.       send_error(lcfd.fd, 400, "Malformed HTTP request");  
  51.       close(lcfd.fd);  
  52.       return NULL;  
  53.     }  
  54.     pb += strlen("GET /?action=command");  
  55.   
  56.     /* only accept certain characters */  
  57.     len = MIN(MAX(strspn(pb, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-=&1234567890"), 0), 100);  
  58.     req.parameter = malloc(len+1);  
  59.     if ( req.parameter == NULL ) {  
  60.       exit(EXIT_FAILURE);  
  61.     }  
  62.     memset(req.parameter, 0, len+1);  
  63.     strncpy(req.parameter, pb, len);  
  64.   
  65.     DBG("command parameter (len: %d): \"%s\"\n", len, req.parameter);  
  66.   }  
  67.   else {  
  68.     int len;  
  69.   
  70.     DBG("try to serve a file\n");  
  71.     req.type = A_FILE;  
  72.   
  73.     if ( (pb = strstr(buffer, "GET /")) == NULL ) {  
  74.       DBG("HTTP request seems to be malformed\n");  
  75.       send_error(lcfd.fd, 400, "Malformed HTTP request");  
  76.       close(lcfd.fd);  
  77.       return NULL;  
  78.     }  
  79.   
  80.     pb += strlen("GET /");  
  81.     len = MIN(MAX(strspn(pb, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ._-1234567890"), 0), 100);  
  82.     req.parameter = malloc(len+1);  
  83.     if ( req.parameter == NULL ) {  
  84.       exit(EXIT_FAILURE);  
  85.     }  
  86.     memset(req.parameter, 0, len+1);  
  87.     strncpy(req.parameter, pb, len);  
  88.   
  89.     DBG("parameter (len: %d): \"%s\"\n", len, req.parameter);  
  90.   }  
  91.   
  92.   /* 
  93.    * parse the rest of the HTTP-request 
  94.    * the end of the request-header is marked by a single, empty line with "\r\n" 
  95.    */  
  96.   do {  
  97.     memset(buffer, 0, sizeof(buffer));  
  98.   
  99.     //从客户端再次读取一次字符串  
  100.     if ( (cnt = _readline(lcfd.fd, &iobuf, buffer, sizeof(buffer)-1, 5)) == -1 ) {  
  101.       free_request(&req);  
  102.       close(lcfd.fd);  
  103.       return NULL;  
  104.     }  
  105.   
  106.     if ( strstr(buffer, "User-Agent: ") != NULL ) {  
  107.       //如果buffer(客服端)中存有(发送了)用户名,则将用户名保存到 req.client 中  
  108.       req.client = strdup(buffer+strlen("User-Agent: "));  
  109.     }  
  110.     //如果buffer(客服端)中存有(发送了)密码,则将密码保存到 req.credentials 中  
  111.     else if ( strstr(buffer, "Authorization: Basic ") != NULL ) {  
  112.       req.credentials = strdup(buffer+strlen("Authorization: Basic "));  
  113.       //对密码进行解码  
  114.       decodeBase64(req.credentials);  
  115.       DBG("username:password: %s\n", req.credentials);  
  116.     }  
  117.   
  118.   } while( cnt > 2 && !(buffer[0] == '\r' && buffer[1] == '\n') );  
  119.   
  120.   //如果支持密码功能,则要检查用户名和密码是否匹配  
  121.   if ( lcfd.pc->conf.credentials != NULL ) {  
  122.     if ( req.credentials == NULL || strcmp(lcfd.pc->conf.credentials, req.credentials) != 0 ) {  
  123.       DBG("access denied\n");  
  124.       send_error(lcfd.fd, 401, "username and password do not match to configuration");  
  125.       close(lcfd.fd);  
  126.       if ( req.parameter != NULL ) free(req.parameter);  
  127.       if ( req.client != NULL ) free(req.client);  
  128.       if ( req.credentials != NULL ) free(req.credentials);  
  129.       return NULL;  
  130.     }  
  131.     DBG("access granted\n");  
  132.   }  
  133.   
  134.   //根据请求的类型,采取相应的行动   
  135.   switch ( req.type ) {  
  136.     case A_SNAPSHOT:  
  137.       DBG("Request for snapshot\n");  
  138.       send_snapshot(lcfd.fd);  
  139.       break;  
  140.     case A_STREAM:  
  141.       DBG("Request for stream\n");  
  142.       send_stream(lcfd.fd);  
  143.       break;  
  144.     case A_COMMAND:  
  145.       if ( lcfd.pc->conf.nocommands ) {  
  146.         send_error(lcfd.fd, 501, "this server is configured to not accept commands");  
  147.         break;  
  148.       }  
  149.       command(lcfd.pc->id, lcfd.fd, req.parameter);  
  150.       break;  
  151.     case A_FILE:  
  152.       if ( lcfd.pc->conf.www_folder == NULL )  
  153.         send_error(lcfd.fd, 501, "no www-folder configured");  
  154.       else  
  155.         send_file(lcfd.pc->id, lcfd.fd, req.parameter);  
  156.       break;  
  157.     default:  
  158.       DBG("unknown request\n");  
  159.   }  
  160.   
  161.   close(lcfd.fd);  
  162.   free_request(&req);  
  163.   
  164.   DBG("leaving HTTP client thread\n");  
  165.   return NULL;  
  166. }  

其中_readline()函数通过调用_read()函数从客服端中读取一行的数据,将其逐个防止在buffer上,并将其buffer返回,用于参数解析;

[cpp]  view plain  copy
  1. /* read just a single line or timeout */  
  2. int _readline(int fd, iobuffer *iobuf, void *buffer, size_t len, int timeout) {  
  3.   char c='\0', *out=buffer;  
  4.   int i;  
  5.   
  6.   memset(buffer, 0, len);  
  7.   
  8.   //从iobuf.buf[]中逐个字节的将数据取出存放到buffer中,直到遇见换行符'\n'或者长度达到了  
  9.   for ( i=0; i<len && c != '\n'; i++ ) {  
  10.     if ( _read(fd, iobuf, &c, 1, timeout) <= 0 ) {  
  11.       /* timeout or error occured */  
  12.       return -1;  
  13.     }  
  14.     *out++ = c;  
  15.   }  
  16.   
  17.   return i;  
  18. }  
  19.   
  20. int _read(int fd, iobuffer *iobuf, void *buffer, size_t len, int timeout) {  
  21.   int copied=0, rc, i;  
  22.   fd_set fds;  
  23.   struct timeval tv;  
  24.   
  25.   memset(buffer, 0, len);  
  26.   
  27.   while ( (copied < len) ) {  
  28.     //第一次,i=0(iobuf->level初始化为0,len-copied,其中len为1,copied为0),以后,i=1    
  29.     //i=0相当于下面的不拷贝  
  30.     //iobuf->level表示iobuf->buffer中的字节数,初始时为0  
  31.     i = MIN(iobuf->level, len-copied);  
  32.     memcpy(buffer+copied, iobuf->buffer+IO_BUFFER-iobuf->level, i);  
  33.   
  34.     iobuf->level -= i;  
  35.     copied += i;  
  36.     if ( copied >= len )  
  37.       return copied;  
  38.   
  39.     //当客服端发有数据或者超时的时候,select函数就返回,目的防止while循环永不退出  
  40.     tv.tv_sec = timeout;  
  41.     tv.tv_usec = 0;  
  42.     FD_ZERO(&fds);  
  43.     FD_SET(fd, &fds);  
  44.     if ( (rc = select(fd+1, &fds, NULL, NULL, &tv)) <= 0 ) {  
  45.       if ( rc < 0)  
  46.         exit(EXIT_FAILURE);  
  47.   
  48.       /* this must be a timeout */  
  49.       return copied;  
  50.     }  
  51.   
  52.     init_iobuffer(iobuf);  
  53.   
  54.     /* 
  55.      * there should be at least one byte, because select signalled it. 
  56.      * But: It may happen (very seldomly), that the socket gets closed remotly between 
  57.      * the select() and the following read. That is the reason for not relying 
  58.      * on reading at least one byte. 
  59.      */  
  60.      //调用read函数,从客服端读取最多 256 字节的数据,存放到iobuf->buffer  
  61.     if ( (iobuf->level = read(fd, &iobuf->buffer, IO_BUFFER)) <= 0 ) {  
  62.       /* an error occured */  
  63.       return -1;  
  64.     }  
  65.       
  66.     //拷贝iobuf->buffer中的数据到地址iobuf->buffer+(IO_BUFFER-iobuf->level)  
  67.     memmove(iobuf->buffer+(IO_BUFFER-iobuf->level), iobuf->buffer, iobuf->level);  
  68.   }  
  69.   
  70.   return 0;  
  71. }  

经过对http请求的参数解析,确定服务器发送数据的类型,常用的是snapshot和stream模式,简单的说,就是发送单帧图像或者实时视频(其实是通过一帧帧图像实现的)。

[cpp]  view plain  copy
  1. /****************************************************************************** 
  2. Description.: Send a complete HTTP response and a single JPG-frame. 
  3. Input Value.: fildescriptor fd to send the answer to 
  4. Return Value: - 
  5. ******************************************************************************/  
  6. void send_snapshot(int fd) {  
  7.   unsigned char *frame=NULL;  
  8.   int frame_size=0;  
  9.   char buffer[BUFFER_SIZE] = {0};  
  10.   
  11.   //等待输入通道input_uvc.c里发送数据更新请求 */  
  12.   //输入通道:摄像头源源不断采集数据,每采集完一帧数据就会往仓库里存放数据,  
  13.   //存放好后,通过条件变量发出一个数据更新信号(通过phread_cond_broadcast函数)  
  14.   //得到数据更新信号后锁定互斥锁  
  15.   pthread_cond_wait(&pglobal->db_update, &pglobal->db);  
  16.   
  17.   //获得一帧图像的大小,经过输入通道采集并复制到全局缓冲区后,该全局变量会随之更新  
  18.   frame_size = pglobal->size;  
  19.   
  20.   //根据一帧数据的大小,分配一个本地 frame 缓冲区,如分配内存出错,释放内存并解锁互斥锁  
  21.   if ( (frame = malloc(frame_size+1)) == NULL ) {  
  22.     free(frame);  
  23.     pthread_mutex_unlock( &pglobal->db );  
  24.     send_error(fd, 500, "not enough memory");  
  25.     return;  
  26.   }  
  27.   
  28.   //从仓库(pglobal->buf)中取出一帧数据,并将其放置在frame中  
  29.   memcpy(frame, pglobal->buf, frame_size);  
  30.   DBG("got frame (size: %d kB)\n", frame_size/1024);  
  31.   
  32.   pthread_mutex_unlock( &pglobal->db );  
  33.   
  34.   /* write the response */  
  35.   //buffer的字符串为HTTP/1.0 200 OK\r\n" STD_HEADER \"Content-type: image/jpeg   
  36.   //HTTP/1.0 表明http协议所用版本1.0  
  37.   sprintf(buffer, "HTTP/1.0 200 OK\r\n" \  
  38.                   STD_HEADER \  
  39.                   "Content-type: image/jpeg\r\n" \  
  40.                   "\r\n");  
  41.   
  42.     
  43.   //将buffer中的字符串发送给客服端 */  
  44.   //对于mjpeg-streamer,输出通道是通过socket编程来模拟http协议,  
  45.   //而对于我们的http协议来说,它需要先让客户端发送一个请求,  
  46.   //当服务器收到这个请求以后,接下来会发送应答,首先会发送一个头部信息,  
  47.   //http应答中的头部信息保存在buffer(报文),会报告http协议所用的版本  
  48.   if( write(fd, buffer, strlen(buffer)) < 0 ) {  
  49.     free(frame);  
  50.     return;  
  51.   }  
  52.   //发送一帧图像  
  53.   write(fd, frame, frame_size);  
  54.   
  55.   free(frame);  
  56. }  

[cpp]  view plain  copy
  1. /****************************************************************************** 
  2. Description.: Send a complete HTTP response and a stream of JPG-frames. 
  3. Input Value.: fildescriptor fd to send the answer to 
  4. Return Value: - 
  5. ******************************************************************************/  
  6. void send_stream(int fd) {  
  7.   unsigned char *frame=NULL, *tmp=NULL;  
  8.   int frame_size=0, max_frame_size=0;  
  9.   char buffer[BUFFER_SIZE] = {0};  
  10.   
  11.   DBG("preparing header\n");  
  12.   
  13.   sprintf(buffer, "HTTP/1.0 200 OK\r\n" \  
  14.                   STD_HEADER \  
  15.                   "Content-Type: multipart/x-mixed-replace;boundary=" BOUNDARY "\r\n" \  
  16.                   "\r\n" \  
  17.                   "--" BOUNDARY "\r\n");  
  18.   
  19.   // 将 buffer 中的字符串发送出去(报文)  
  20.   if ( write(fd, buffer, strlen(buffer)) < 0 ) {  
  21.     free(frame);  
  22.     return;  
  23.   }  
  24.   
  25.   DBG("Headers send, sending stream now\n");  
  26.   
  27.   //循环发送图片形成视频流  
  28.   //进入循环,pglobal->stop为1时终止,按ctrl+c时pglobal->stop为1  
  29.   while ( !pglobal->stop ) {  
  30.   
  31.     /* 等待输入通道发出数据更新的信号 */  
  32.     pthread_cond_wait(&pglobal->db_update, &pglobal->db);  
  33.   
  34.     /* 读取一帧图像的大小 */  
  35.     frame_size = pglobal->size;  
  36.   
  37.     /* 检查我们之前分配的缓存是否够大,如果不够,则重新分配 */  
  38.     if ( frame_size > max_frame_size ) {  
  39.       DBG("increasing buffer size to %d\n", frame_size);  
  40.   
  41.       max_frame_size = frame_size+TEN_K;  
  42.       if ( (tmp = realloc(frame, max_frame_size)) == NULL ) {  
  43.         free(frame);  
  44.         pthread_mutex_unlock( &pglobal->db );  
  45.         send_error(fd, 500, "not enough memory");  
  46.         return;  
  47.       }  
  48.   
  49.       frame = tmp;  
  50.     }  
  51.   
  52.     //从仓库中取出一帧数据放在 frame   
  53.     memcpy(frame, pglobal->buf, frame_size);  
  54.     DBG("got frame (size: %d kB)\n", frame_size/1024);  
  55.   
  56.     pthread_mutex_unlock( &pglobal->db );  
  57.   
  58.      //让 buffer = ""报文,告诉客服端即将发送的图片的大小  
  59.     sprintf(buffer, "Content-Type: image/jpeg\r\n" \  
  60.                     "Content-Length: %d\r\n" \  
  61.                     "\r\n", frame_size);  
  62.     DBG("sending intemdiate header\n");  
  63.     //发送报文  
  64.     if ( write(fd, buffer, strlen(buffer)) < 0 ) break;  
  65.   
  66.     //发送一帧图像  
  67.     DBG("sending frame\n");  
  68.     if( write(fd, frame, frame_size) < 0 ) break;  
  69.   
  70.     //发送这个字符串是因为对于客户端来说,接收一帧数据怎么知道接收一帧数据接收完。  
  71.     //一种是根据frame_size来判断一帧数据是否接收完,  
  72.     //另一种是接收的数据是字符串boundarydonotcross(每两帧图片的边界值)的时候,  
  73.     //就表示一帧图片接收完了,即将接收的是第二帧图片  
  74.     DBG("sending boundary\n");  
  75.     sprintf(buffer, "\r\n--" BOUNDARY "\r\n");  
  76.     if ( write(fd, buffer, strlen(buffer)) < 0 ) break;  
  77.   }  
  78.   
  79.   free(frame);  
  80. }  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值