/*! \brief uninitialize the descriptor for local video stream */ static int video_out_uninit(struct video_desc *env) { struct video_out_desc *v = &env->out; int i; /* integer variable used as iterator */ /* XXX this should be a codec callback */ if (v->enc_ctx) { AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx; avcodec_close(enc_ctx); av_free(enc_ctx); v->enc_ctx = NULL; } if (v->enc_in_frame) { av_free(v->enc_in_frame); v->enc_in_frame = NULL; } v->codec = NULL; /* nothing to free, this is only a reference */ /* release the buffers */ fbuf_free(&env->enc_in); fbuf_free(&v->enc_out); /* close the grabbers */ for (i = 0; i < v->device_num; i++) { if (v->devices[i].grabber){ v->devices[i].grabber_data = v->devices[i].grabber->close(v->devices[i].grabber_data); v->devices[i].grabber = NULL; /* dev_buf is already freed by grabber->close() */ v->devices[i].dev_buf = NULL; } v->devices[i].status_index = 0; } v->picture_in_picture = 0; env->frame_freeze = 0; return -1; }
/*! \brief uninitialize the descriptor for local video stream */ static int video_out_uninit(struct video_desc *env) { struct video_out_desc *v = &env->out; /* XXX this should be a codec callback */ if (v->enc_ctx) { AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx; avcodec_close(enc_ctx); av_free(enc_ctx); v->enc_ctx = NULL; } if (v->enc_in_frame) { av_free(v->enc_in_frame); v->enc_in_frame = NULL; } v->codec = NULL; /* nothing to free, this is only a reference */ /* release the buffers */ fbuf_free(&env->enc_in); fbuf_free(&v->enc_out); /* close the grabber */ if (v->grabber) { v->grabber_data = v->grabber->close(v->grabber_data); v->grabber = NULL; } return -1; }
static void *grab_v4l1_close(void *desc) { struct grab_v4l1_desc *v = desc; close(v->fd); v->fd = -1; fbuf_free(&v->b); ast_free(v); return NULL; }
static inline void shc_multi_context_destroy(shc_multi_ctx_t *ctx) { async_read_context_destroy(ctx->reader); fbuf_free(ctx->commands); free(ctx->items); if (ctx->fd >= 0) { if (ctx->response_index == ctx->num_requests) connections_pool_add(ctx->client->connections, ctx->peer, ctx->fd); else close(ctx->fd); } free(ctx); }
void *worker(void *priv) { wurfld_connection_context *ctx = (wurfld_connection_context *)priv; DEBUG1("Worker %p started on fd %d", pthread_self(), ctx->fd); // we don't need to receive anything anymore on this fd int err = shutdown(ctx->fd, SHUT_RD); if (err != 0) NOTICE("Can't shutdown the receive part of fd %d : %s", ctx->fd, strerror(errno)); int opts = fcntl(ctx->fd, F_GETFL); if (opts >= 0) { err = fcntl(ctx->fd, F_SETFL, opts & (~O_NONBLOCK)); if (err != 0) NOTICE("Can't set blocking mode on fd %d : %s", ctx->fd, strerror(errno)); } else { ERROR("Can't get flags on fd %d : %s", ctx->fd, strerror(errno)); } char *useragent = NULL; fbuf_trim(ctx->input); // parse the request char *request_data = fbuf_data(ctx->input); struct sockaddr_in peer; socklen_t socklen = sizeof(struct sockaddr); getpeername(ctx->fd, (struct sockaddr *)&peer, &socklen); if (use_http && strncmp(request_data, "GET /lookup/", 12) == 0) { char *reqline_start = fbuf_data(ctx->input) + 12; char *reqline_end = reqline_start; while (*reqline_end != '\r' && *reqline_end != '\n') reqline_end++; reqline_end++; char reqline[reqline_end-reqline_start]; snprintf(reqline, reqline_end-reqline_start, "%s", reqline_start); char *httpv = strstr(reqline, " HTTP/1"); if (httpv) { *httpv = 0; httpv++; ctx->is_http10 = (strncmp(httpv, "HTTP/1.0", 8) == 0); } useragent = unescape_uri_request(reqline); } else if (!use_http) { useragent = strdup(fbuf_data(ctx->input)); } if (useragent) { NOTICE("(%p) Lookup request from %s: %s", pthread_self(), inet_ntoa(peer.sin_addr), useragent); ctx->useragent = useragent; send_response(ctx); } else if (use_http) { NOTICE("(%p) Unsupported Request from %s: %s", pthread_self(), inet_ntoa(peer.sin_addr), request_data); char response[2048]; snprintf(response, sizeof(response), "%s 400 NOT SUPPORTED\r\n" "Content-Type: text/plain\r\n" "Content-Length: 17\r\n\r\n" "400 NOT SUPPORTED", ctx->is_http10 ? "HTTP/1.0" : "HTTP/1.1"); if (write_socket(ctx->fd, response, strlen(response)) != 0) { ERROR("Worker %p failed writing reponse: %s", pthread_self(), strerror(errno)); } } DEBUG1("Worker %p finished on fd %d", pthread_self(), ctx->fd); shutdown(ctx->fd, SHUT_RDWR); close(ctx->fd); fbuf_free(ctx->input); fbuf_free(ctx->output); free(ctx->useragent); free(ctx); return NULL; }
main( int argc, char **argv ) { fbuf_uint32_t buf_sz = 512; int N_frames = 2; const char filename[] = "fbuf.data"; filebuf_t *fbuf_ptr; MixedElem *elem_ptr; int icontinue; fbuf_uint32_t frame_count; fbuf_uint32_t frame_idx_max; fbuf_uint32_t frame_idx; fbuf_uint32_t N_frame_elem; fbuf_uint32_t elem_idx; if ( argc > 1 ) if ( isdigit( argv[1][0] ) ) buf_sz = atoi( argv[1] ); if ( argc > 2 ) if ( isdigit( argv[2][0] ) ) N_frames = atoi( argv[2] ); fbuf_ptr = fbuf_create( buf_sz ); fbuf_filedesc( fbuf_ptr, fopen( filename, "wb" ) ); bswp_fwrite( &(fbuf_ptr->bsize), sizeof( fbuf_uint32_t ), 1, fbuf_ptr->fd ); elem_idx = 0; for ( frame_count = 0; frame_count < N_frames; frame_count++ ) { fbuf_increment( sizeof( fbuf_uint32_t ), fbuf_ptr ); icontinue = true; frame_idx = 0; while( icontinue ) { elem_ptr = elem_create( ( double ) ( elem_idx * 10 ), ( int ) elem_idx ); if ( icontinue = elem2fbuf( fbuf_ptr, elem_ptr ) ) { printf( "elem_idx = %d, used buffer = %d\n", elem_idx, fbuf_ptr->ptr - fbuf_ptr->head ); frame_idx++; elem_idx++; } else { frame_idx_max = frame_idx - 1; N_frame_elem = frame_idx; } elem_free( elem_ptr ); } /* set fbuf to its head */ fbuf_decrement( fbuf_ptr->ptr - fbuf_ptr->head, fbuf_ptr ); fbuf_deposit( &N_frame_elem, sizeof( fbuf_uint32_t ), 1, fbuf_ptr ); /* memcpy( fbuf_ptr->head, &N_frame_elem, sizeof( fbuf_uint32_t )/sizeof( char ) ); */ fbuf_empty( fbuf_ptr ); } printf( "sizeof( MixedElem ) = %d\n", sizeof( MixedElem )/sizeof( char ) ); printf( "The elem_idx_max is %d\n", elem_idx-1 ); fclose( fbuf_ptr->fd ); fbuf_free( fbuf_ptr ); }
/*! \brief * Open the local video source and allocate a buffer * for storing the image. */ static void *grab_v4l1_open(const char *dev, struct fbuf_t *geom, int fps) { struct video_window vw = { 0 }; /* camera attributes */ struct video_picture vp; int fd, i; struct grab_v4l1_desc *v; struct fbuf_t *b; /* name should be something under /dev/ */ if (strncmp(dev, "/dev/", 5)) return NULL; fd = open(dev, O_RDONLY | O_NONBLOCK); if (fd < 0) { ast_log(LOG_WARNING, "error opening camera %s\n", dev); return NULL; } v = ast_calloc(1, sizeof(*v)); if (v == NULL) { ast_log(LOG_WARNING, "no memory for camera %s\n", dev); close(fd); return NULL; /* no memory */ } v->fd = fd; v->b = *geom; b = &v->b; /* shorthand */ i = fcntl(fd, F_GETFL); if (-1 == fcntl(fd, F_SETFL, i | O_NONBLOCK)) { /* non fatal, just emit a warning */ ast_log(LOG_WARNING, "error F_SETFL for %s [%s]\n", dev, strerror(errno)); } /* set format for the camera. * In principle we could retry with a different format if the * one we are asking for is not supported. */ vw.width = b->w; vw.height = b->h; vw.flags = fps << 16; if (ioctl(fd, VIDIOCSWIN, &vw) == -1) { ast_log(LOG_WARNING, "error setting format for %s [%s]\n", dev, strerror(errno)); goto error; } if (ioctl(fd, VIDIOCGPICT, &vp) == -1) { ast_log(LOG_WARNING, "error reading picture info\n"); goto error; } ast_log(LOG_WARNING, "contrast %d bright %d colour %d hue %d white %d palette %d\n", vp.contrast, vp.brightness, vp.colour, vp.hue, vp.whiteness, vp.palette); /* set the video format. Here again, we don't necessary have to * fail if the required format is not supported, but try to use * what the camera gives us. */ b->pix_fmt = vp.palette; vp.palette = VIDEO_PALETTE_YUV420P; if (ioctl(v->fd, VIDIOCSPICT, &vp) == -1) { ast_log(LOG_WARNING, "error setting palette, using %d\n", b->pix_fmt); } else b->pix_fmt = vp.palette; /* allocate the source buffer. * XXX, the code here only handles yuv411, for other formats * we need to look at pix_fmt and set size accordingly */ b->size = (b->w * b->h * 3)/2; /* yuv411 */ ast_log(LOG_WARNING, "videodev %s opened, size %dx%d %d\n", dev, b->w, b->h, b->size); b->data = ast_calloc(1, b->size); if (!b->data) { ast_log(LOG_WARNING, "error allocating buffer %d bytes\n", b->size); goto error; } ast_log(LOG_WARNING, "success opening camera\n"); return v; error: close(v->fd); fbuf_free(b); ast_free(v); return NULL; }