void init_device() { set_format(); request_buffer(); query_buf_and_mmap(); queue_buffer(); }
/** * Parse filepath URL from incoming HTTP request */ std::string HttpServer::parse_request(tcp::socket &socket, boost::system::error_code &error_code) { std::regex url_path_regex("(\\/([^\\s]+)|\\/)"); std::smatch url_path_match; std::vector<char> request_buffer(1024); socket.receive(boost::asio::buffer(request_buffer), {}, error_code); std::string request_str(request_buffer.data()); std::regex_search(request_str, url_path_match, url_path_regex); return url_path_match[0]; }
XCamReturn V4l2Device::start () { XCamReturn ret = XCAM_RETURN_NO_ERROR; // request buffer first ret = request_buffer (); XCAM_FAIL_RETURN ( ERROR, ret == XCAM_RETURN_NO_ERROR, ret, "device(%s) start failed", XCAM_STR (_name)); //alloc buffers ret = init_buffer_pool (); XCAM_FAIL_RETURN ( ERROR, ret == XCAM_RETURN_NO_ERROR, ret, "device(%s) start failed", XCAM_STR (_name)); //queue all buffers for (uint32_t i = 0; i < _buf_count; ++i) { SmartPtr<V4l2Buffer> &buf = _buf_pool [i]; XCAM_ASSERT (buf.ptr()); XCAM_ASSERT (buf->get_buf().index == i); ret = queue_buffer (buf); if (ret != XCAM_RETURN_NO_ERROR) { XCAM_LOG_ERROR ( "device(%s) start failed on queue index:%d", XCAM_STR (_name), i); stop (); return ret; } } // stream on if (io_control (VIDIOC_STREAMON, &_capture_buf_type) < 0) { XCAM_LOG_ERROR ( "device(%s) start failed on VIDIOC_STREAMON", XCAM_STR (_name)); stop (); return XCAM_RETURN_ERROR_IOCTL; } _active = true; XCAM_LOG_INFO ("device(%s) started successfully", XCAM_STR (_name)); return XCAM_RETURN_NO_ERROR; }
static void * comp_proc(void * para) { int i, j; int fd; int channel; int n_buffers = 0; unsigned int length, offset; int index, num; struct buffer_av buf_av; int result; struct pollfd pfd; unsigned int buf_index; unsigned int buf_num; char * packet_addr; struct packet_header pkt_header; Query_Buf_Res * buffers = NULL; //用于保存获取视频数据 Spct_Data comp_data; int got_video[CHS_PER_CARD]; int got_audio[CHS_PER_CARD]; struct timeval v_time_stamp[CHS_PER_CARD]; struct timeval a_time_stamp[CHS_PER_CARD]; unsigned int last_vtstamp[CHS_PER_CARD]; unsigned int last_atstamp[CHS_PER_CARD]; unsigned int duration; DebugPrintf("thread compress\n"); Ip_Cam_Device * ipcam = (Ip_Cam_Device *) para; memset(&buf_av, 0, sizeof(buf_av)); memset(&got_video, 0, sizeof(got_video)); memset(&got_audio, 0, sizeof(got_audio)); memset(&last_vtstamp, 0, sizeof(last_vtstamp)); memset(&last_atstamp, 0, sizeof(last_atstamp)); fd = ipcam->comp_fd; action_fd = fd; n_buffers = request_buffer(fd); buffers = calloc(n_buffers, sizeof(* buffers)); if(!buffers) EXIT("Out of memory."); for(i = 0; i < n_buffers; ++i) { if(query_buffer(fd, i, &length, &offset) != 0) EXIT("VIDIOC_QUERYBUF"); buffers[i].length = length; buffers[i].start = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); DebugPrintf("buffers[%d].start = 0x%x\n",i, buffers[i].start); if(MAP_FAILED == buffers[i].start) EXIT("mmap"); } for(index = 0; index < n_buffers; index++) if(queue_buffer(fd, index) != 0) EXIT("VIDIOC_QBUF"); if(streaming_on(fd) != 0) EXIT("VIDIOC_STREAMON"); DebugPrintf("card stream on================================\n"); pfd.fd = fd; pfd.events = POLLIN; int comp_proc_count = 0; //set_action(fd, catch_sen); // fd threshold time /*检测ipcam是否处于运行状态*/ while(ipcam->status == CAM_STATUS_RUN) { comp_proc_count++; /*定时打印信息*/ if(comp_proc_count == 3000) { comp_proc_count = 0; PrintScreen("\n----- comp_proc thread running -----\n"); } //IsSetaction(fd); /*一个结构体,函数阻塞时间为15s*/ result = poll(&pfd, 1, 15000); /*函数调用失败*/ if(result < 0) DebugPrintf("pool ing errro ==================\n"); /*在规定时间内没有检测到可读套接字*/ if(result == 0) { DebugPrintf("pool ing time out --------------\n"); exit(1); } if(result < 0) continue; if(result == 0) continue; dequeue_buffer(fd, &buf_av); buf_index = buf_av.buf_index; buf_num = buf_av.length; for(i = 0; i < CHS_PER_CARD; i++) { index = buf_index & 0xff; num = buf_num & 0xff; buf_index >>= 8; buf_num >>= 8; if(index != 0xff) { /*DATA PACKET*/ channel = i; for(j = 0, packet_addr = buffers[index].start; j < num; j++) { /*s数据包处理,动态处理就在此实现*/ parse_packet(&packet_addr, &pkt_header); //pkt_header.motion_addr, 4 * 9 /*捕捉到动作*/ if(pkt_header.motion_flag) { #if RELEASE_MODE #else PrintScreen("\n-------------receive a motion------------\n\n"); #endif catchonemotion = 1; is_action = 1; } if (pkt_header.videolost_flag) { //DebugPrintf("video lost\n"); } if(pkt_header.audio_flag) {//length if(!got_audio[channel]) { got_audio[channel] = 1; //a_time_stamp[channel] = buf_av.timestamp; a_time_stamp[channel].tv_usec = ((pkt_header.audio_timestamp % 32768) * 1000ULL * 1000ULL) >> 15; a_time_stamp[channel].tv_sec = (pkt_header.audio_timestamp >> 15) + (a_time_stamp[channel].tv_usec / 1000000); a_time_stamp[channel].tv_usec %= 1000000; } else { duration = pkt_header.audio_timestamp - last_atstamp[channel]; a_time_stamp[channel].tv_usec += ((duration % 32768) * 1000ULL * 1000ULL) >> 15; a_time_stamp[channel].tv_sec += (duration >> 15) + (a_time_stamp[channel].tv_usec / 1000000); a_time_stamp[channel].tv_usec %= 1000000; } last_atstamp[channel] = pkt_header.audio_timestamp; //DebugPrintf("audio frame\n"); comp_data.channel = channel; comp_data.type = DATA_AUDIO; comp_data.flags = 0; comp_data.timestamp = a_time_stamp[channel]; comp_data.size = pkt_header.audio_length; comp_data.data = pkt_header.audio_addr; ipcam->fun(ipcam->datahandler, &comp_data); } if(pkt_header.video_flag) { if(!got_video[channel]) { got_video[channel] = 1; //v_time_stamp[channel] = buf_av.timestamp; v_time_stamp[channel].tv_usec = ((pkt_header.video_timestamp % 32768) * 1000ULL * 1000ULL) >> 15; v_time_stamp[channel].tv_sec = (pkt_header.video_timestamp >> 15) + (v_time_stamp[channel].tv_usec / 1000000); v_time_stamp[channel].tv_usec %= 1000000; } else { duration = pkt_header.video_timestamp - last_vtstamp[channel]; v_time_stamp[channel].tv_usec += ((duration % 32768) * 1000ULL * 1000ULL) >> 15; v_time_stamp[channel].tv_sec += (duration >> 15) + (v_time_stamp[channel].tv_usec / 1000000); v_time_stamp[channel].tv_usec %= 1000000; } last_vtstamp[channel] = pkt_header.video_timestamp; comp_data.channel = channel; comp_data.type = DATA_VIDEO; comp_data.flags = pkt_header.video_type; comp_data.timestamp = v_time_stamp[channel]; comp_data.size = pkt_header.video_length; comp_data.data = pkt_header.video_addr; ipcam->fun(ipcam->datahandler, &comp_data); }