2 * Asterisk -- An open source telephony toolkit.
4 * Copyright 2007-2008, Marta Carbone, Sergio Fadda, Luigi Rizzo
6 * See http://www.asterisk.org for more information about
7 * the Asterisk project. Please do not directly contact
8 * any of the maintainers of this project for assistance;
9 * the project provides a web site, mailing lists and IRC
10 * channels for your use.
12 * This program is free software, distributed under the terms of
13 * the GNU General Public License Version 2. See the LICENSE file
14 * at the top of the source tree.
18 * Experimental support for video sessions. We use SDL for rendering, ffmpeg
19 * as the codec library for encoding and decoding, and Video4Linux and X11
20 * to generate the local video stream.
22 * If one of these pieces is not available, either at compile time or at
23 * runtime, we do our best to run without it. Of course, no codec library
24 * means we can only deal with raw data, no SDL means we cannot do rendering,
25 * no V4L or X11 means we cannot generate data (but in principle we could
26 * stream from or record to a file).
28 * We need a recent (2007.07.12 or newer) version of ffmpeg to avoid warnings.
29 * Older versions might give 'deprecated' messages during compilation,
30 * thus not compiling in AST_DEVMODE, or don't have swscale, in which case
31 * you can try to compile #defining OLD_FFMPEG here.
36 //#define DROP_PACKETS 5 /* if set, drop this % of video packets */
37 //#define OLD_FFMPEG 1 /* set for old ffmpeg with no swscale */
40 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
41 #include <sys/ioctl.h>
42 #include "asterisk/cli.h"
43 #include "asterisk/file.h"
44 #include "asterisk/channel.h"
46 #include "console_video.h"
49 The code is structured as follows.
51 When a new console channel is created, we call console_video_start()
52 to initialize SDL, the source, and the encoder/ decoder for the
53 formats in use (XXX the latter two should be done later, once the
54 codec negotiation is complete). Also, a thread is created to handle
55 the video source and generate frames.
57 While communication is on, the local source is generated by the
58 video thread, which wakes up periodically, generates frames and
59 enqueues them in chan->readq. Incoming rtp frames are passed to
60 console_write_video(), decoded and passed to SDL for display.
62 For as unfortunate and confusing as it can be, we need to deal with a
63 number of different video representations (size, codec/pixel format,
64 codec parameters), as follows:
66 loc_src is the data coming from the camera/X11/etc.
67 The format is typically constrained by the video source.
69 enc_in is the input required by the encoder.
70 Typically constrained in size by the encoder type.
72 enc_out is the bitstream transmitted over RTP.
73 Typically negotiated while the call is established.
75 loc_dpy is the format used to display the local video source.
76 Depending on user preferences this can have the same size as
77 loc_src_fmt, or enc_in_fmt, or thumbnail size (e.g. PiP output)
79 dec_in is the incoming RTP bitstream. Negotiated
80 during call establishment, it is not necessarily the same as
83 dec_out the output of the decoder.
84 The format is whatever the other side sends, and the
85 buffer is allocated by avcodec_decode_... so we only
88 rem_dpy the format used to display the remote stream
90 src_dpy is the format used to display the local video source streams
91 The number of these fbuf_t is determined at run time, with dynamic allocation
93 We store the format info together with the buffer storing the data.
94 As a future optimization, a format/buffer may reference another one
95 if the formats are equivalent. This will save some unnecessary format
99 In order to handle video you need to add to sip.conf (and presumably
100 iax.conf too) the following:
104 allow=h263 ; this or other video formats
105 allow=h263p ; this or other video formats
110 * Codecs are absolutely necessary or we cannot do anything.
111 * SDL is optional (used for rendering only), so that we can still
112 * stream video withouth displaying it.
114 #if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG)
115 /* stubs if required pieces are missing */
116 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
118 return 0; /* writing video not supported */
121 int console_video_cli(struct video_desc *env, const char *var, int fd)
123 return 1; /* nothing matched */
126 int console_video_config(struct video_desc **penv, const char *var, const char *val)
128 return 1; /* no configuration */
131 void console_video_start(struct video_desc *env, struct ast_channel *owner)
133 ast_log(LOG_NOTICE, "voice only, console video support not present\n");
136 void console_video_uninit(struct video_desc *env)
140 int get_gui_startup(struct video_desc* env)
142 return 0; /* no gui here */
145 int console_video_formats = 0;
147 #else /* defined(HAVE_FFMPEG) && defined(HAVE_SDL) */
149 /*! The list of video formats we support. */
150 int console_video_formats =
151 AST_FORMAT_H263_PLUS | AST_FORMAT_H263 |
152 AST_FORMAT_MP4_VIDEO | AST_FORMAT_H264 | AST_FORMAT_H261 ;
156 /* function to scale and encode buffers */
157 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
158 struct fbuf_t *out, AVPicture *p_out);
161 * this structure will be an entry in the table containing
162 * every device specified in the file oss.conf, it contains various infomation
165 struct video_device {
166 char *name; /* name of the device */
167 /* allocated dynamically (see fill_table function) */
168 struct grab_desc *grabber; /* the grabber for the device type */
169 void *grabber_data; /* device's private data structure */
170 struct fbuf_t *dev_buf; /* buffer for incoming data */
171 struct timeval last_frame; /* when we read the last frame ? */
172 int status_index; /* what is the status of the device (source) */
173 /* status index is set using the IS_ON, IS_PRIMARY and IS_SECONDARY costants */
174 /* status_index is the index of the status message in the src_msgs array in console_gui.c */
177 struct video_codec_desc; /* forward declaration */
179 * Descriptor of the local source, made of the following pieces:
180 * + configuration info (geometry, device name, fps...). These are read
181 * from the config file and copied here before calling video_out_init();
182 * + the frame buffer (buf) and source pixel format, allocated at init time;
183 * + the encoding and RTP info, including timestamps to generate
184 * frames at the correct rate;
185 * + source-specific info, i.e. fd for /dev/video, dpy-image for x11, etc,
186 * filled in by grabber_open, part of source_specific information are in
187 * the device table (devices member), others are shared;
188 * NOTE: loc_src.data == NULL means the rest of the struct is invalid, and
189 * the video source is not available.
191 struct video_out_desc {
192 /* video device support.
193 * videodevice and geometry are read from the config file.
194 * At the right time we try to open it and allocate a buffer.
195 * If we are successful, webcam_bufsize > 0 and we can read.
197 /* all the following is config file info copied from the parent */
204 struct fbuf_t loc_src_geometry; /* local source geometry only (from config file) */
205 struct fbuf_t enc_out; /* encoder output buffer, allocated in video_out_init() */
207 struct video_codec_desc *enc; /* encoder */
208 void *enc_ctx; /* encoding context */
210 AVFrame *enc_in_frame; /* enc_in mapped into avcodec format. */
211 /* The initial part of AVFrame is an AVPicture */
214 /* Table of devices specified with "videodevice=" in oss.conf.
215 * Static size as we have a limited number of entries.
217 struct video_device devices[MAX_VIDEO_SOURCES];
218 int device_num; /*number of devices in table*/
219 int device_primary; /*index of the actual primary device in the table*/
220 int device_secondary; /*index of the actual secondary device in the table*/
222 int picture_in_picture; /*Is the PiP mode activated? 0 = NO | 1 = YES*/
224 /* these are the coordinates of the picture inside the picture (visible if PiP mode is active)
225 these coordinates are valid considering the containing buffer with cif geometry*/
231 * The overall descriptor, with room for config info, video source and
232 * received data descriptors, SDL info, etc.
233 * This should be globally visible to all modules (grabber, vcodecs, gui)
234 * and contain all configurtion info.
237 char codec_name[64]; /* the codec we use */
239 int stayopen; /* set if gui starts manually */
240 pthread_t vthread; /* video thread */
241 ast_mutex_t dec_lock; /* sync decoder and video thread */
242 int shutdown; /* set to shutdown vthread */
243 struct ast_channel *owner; /* owner channel */
246 struct fbuf_t enc_in; /* encoder input buffer, allocated in video_out_init() */
248 char keypad_file[256]; /* image for the keypad */
249 char keypad_font[256]; /* font for the keypad */
251 char sdl_videodriver[256];
253 struct fbuf_t rem_dpy; /* display remote video, no buffer (it is in win[WIN_REMOTE].bmp) */
254 struct fbuf_t loc_dpy; /* display local source, no buffer (managed by SDL in bmp[1]) */
256 /* geometry of the thumbnails for all video sources. */
257 struct fbuf_t src_dpy[MAX_VIDEO_SOURCES]; /* no buffer allocated here */
259 int frame_freeze; /* flag to freeze the incoming frame */
261 /* local information for grabbers, codecs, gui */
262 struct gui_info *gui;
263 struct video_dec_desc *in; /* remote video descriptor */
264 struct video_out_desc out; /* local video descriptor */
267 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p);
269 void fbuf_free(struct fbuf_t *b)
271 struct fbuf_t x = *b;
273 if (b->data && b->size)
275 bzero(b, sizeof(*b));
276 /* restore some fields */
279 b->pix_fmt = x.pix_fmt;
282 /* return the status of env->stayopen to chan_oss, as the latter
283 * does not have access to fields of struct video_desc
285 int get_gui_startup(struct video_desc* env)
287 return env ? env->stayopen : 0;
291 /* helper function to print the amount of memory used by the process.
292 * Useful to track memory leaks, unfortunately this code is OS-specific
293 * so we keep it commented out.
296 used_mem(const char *msg)
300 pid_t pid = getpid();
301 sprintf(in, "ps -o vsz= -o rss= %d", pid);
302 ast_log(LOG_WARNING, "used mem (vsize, rss) %s ", msg);
309 #include "console_gui.c"
311 /*! \brief Try to open video sources, return 0 on success, 1 on error
312 * opens all video sources found in the oss.conf configuration files.
313 * Saves the grabber and the datas in the device table (in the devices field
314 * of the descriptor referenced by v).
315 * Initializes the device_primary and device_secondary
316 * fields of v with the first devices that was
317 * successfully opened.
319 * \param v = video out environment descriptor
321 * returns 0 on success, 1 on error
323 static int grabber_open(struct video_out_desc *v)
329 /* for each device in the device table... */
330 for (i = 0; i < v->device_num; i++) {
331 /* device already open */
332 if (v->devices[i].grabber)
334 /* for each type of grabber supported... */
335 for (j = 0; (g = console_grabbers[j]); j++) {
336 /* the grabber is opened and the informations saved in the device table */
337 g_data = g->open(v->devices[i].name, &v->loc_src_geometry, v->fps);
340 v->devices[i].grabber = g;
341 v->devices[i].grabber_data = g_data;
342 v->devices[i].status_index |= IS_ON;
345 /* the first working device is selected as the primary one and the secondary one */
346 for (i = 0; i < v->device_num; i++) {
347 if (!v->devices[i].grabber)
349 v->device_primary = i;
350 v->device_secondary = i;
351 return 0; /* source found */
353 return 1; /* no source found */
357 /*! \brief complete a buffer from the specified local video source.
358 * Called by get_video_frames(), in turn called by the video thread.
360 * \param dev = video environment descriptor
361 * \param fps = frame per seconds, for every device
365 * - reference to the device buffer on success
367 static struct fbuf_t *grabber_read(struct video_device *dev, int fps)
369 struct timeval now = ast_tvnow();
371 if (dev->grabber == NULL) /* not initialized */
374 /* the last_frame field in this row of the device table (dev)
375 is always initialized, it is set during the parsing of the config
376 file, and never unset, function fill_device_table(). */
377 /* check if it is time to read */
378 if (ast_tvdiff_ms(now, dev->last_frame) < 1000/fps)
379 return NULL; /* too early */
380 dev->last_frame = now; /* XXX actually, should correct for drift */
381 return dev->grabber->read(dev->grabber_data);
384 /*! \brief handler run when dragging with the left button on
385 * the local source window - the effect is to move the offset
386 * of the captured area.
388 static void grabber_move(struct video_device *dev, int dx, int dy)
390 if (dev->grabber && dev->grabber->move)
391 dev->grabber->move(dev->grabber_data, dx, dy);
395 * Map the codec name to the library. If not recognised, use a default.
396 * This is useful in the output path where we decide by name, presumably.
398 static struct video_codec_desc *map_config_video_format(char *name)
402 for (i = 0; supported_codecs[i]; i++)
403 if (!strcasecmp(name, supported_codecs[i]->name))
405 if (supported_codecs[i] == NULL) {
406 ast_log(LOG_WARNING, "Cannot find codec for '%s'\n", name);
408 strcpy(name, supported_codecs[i]->name);
410 ast_log(LOG_WARNING, "Using codec '%s'\n", name);
411 return supported_codecs[i];
415 /*! \brief uninitialize the descriptor for local video stream */
416 static int video_out_uninit(struct video_desc *env)
418 struct video_out_desc *v = &env->out;
419 int i; /* integer variable used as iterator */
421 /* XXX this should be a codec callback */
423 AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx;
424 avcodec_close(enc_ctx);
428 if (v->enc_in_frame) {
429 av_free(v->enc_in_frame);
430 v->enc_in_frame = NULL;
432 v->codec = NULL; /* nothing to free, this is only a reference */
433 /* release the buffers */
434 fbuf_free(&env->enc_in);
435 fbuf_free(&v->enc_out);
436 /* close the grabbers */
437 for (i = 0; i < v->device_num; i++) {
438 if (v->devices[i].grabber){
439 v->devices[i].grabber_data =
440 v->devices[i].grabber->close(v->devices[i].grabber_data);
441 v->devices[i].grabber = NULL;
442 /* dev_buf is already freed by grabber->close() */
443 v->devices[i].dev_buf = NULL;
445 v->devices[i].status_index = 0;
447 v->picture_in_picture = 0;
448 env->frame_freeze = 0;
453 * Initialize the encoder for the local source:
454 * - enc_ctx, codec, enc_in_frame are used by ffmpeg for encoding;
455 * - enc_out is used to store the encoded frame (to be sent)
456 * - mtu is used to determine the max size of video fragment
457 * NOTE: we enter here with the video source already open.
459 static int video_out_init(struct video_desc *env)
463 struct fbuf_t *enc_in;
464 struct video_out_desc *v = &env->out;
468 v->enc_in_frame = NULL;
469 v->enc_out.data = NULL;
471 codec = map_video_format(v->enc->format, CM_WR);
472 v->codec = avcodec_find_encoder(codec);
474 ast_log(LOG_WARNING, "Cannot find the encoder for format %d\n",
476 return -1; /* error, but nothing to undo yet */
479 v->mtu = 1400; /* set it early so the encoder can use it */
481 /* allocate the input buffer for encoding.
482 * ffmpeg only supports PIX_FMT_YUV420P for the encoding.
484 enc_in = &env->enc_in;
485 enc_in->pix_fmt = PIX_FMT_YUV420P;
486 enc_in->size = (enc_in->w * enc_in->h * 3)/2;
487 enc_in->data = ast_calloc(1, enc_in->size);
489 ast_log(LOG_WARNING, "Cannot allocate encoder input buffer\n");
490 return video_out_uninit(env);
492 /* construct an AVFrame that points into buf_in */
493 v->enc_in_frame = avcodec_alloc_frame();
494 if (!v->enc_in_frame) {
495 ast_log(LOG_WARNING, "Unable to allocate the encoding video frame\n");
496 return video_out_uninit(env);
499 /* parameters for PIX_FMT_YUV420P */
500 size = enc_in->w * enc_in->h;
501 v->enc_in_frame->data[0] = enc_in->data;
502 v->enc_in_frame->data[1] = v->enc_in_frame->data[0] + size;
503 v->enc_in_frame->data[2] = v->enc_in_frame->data[1] + size/4;
504 v->enc_in_frame->linesize[0] = enc_in->w;
505 v->enc_in_frame->linesize[1] = enc_in->w/2;
506 v->enc_in_frame->linesize[2] = enc_in->w/2;
508 /* now setup the parameters for the encoder.
509 * XXX should be codec-specific
512 AVCodecContext *enc_ctx = avcodec_alloc_context();
513 v->enc_ctx = enc_ctx;
514 enc_ctx->pix_fmt = enc_in->pix_fmt;
515 enc_ctx->width = enc_in->w;
516 enc_ctx->height = enc_in->h;
517 /* XXX rtp_callback ?
518 * rtp_mode so ffmpeg inserts as many start codes as possible.
520 enc_ctx->rtp_mode = 1;
521 enc_ctx->rtp_payload_size = v->mtu / 2; // mtu/2
522 enc_ctx->bit_rate = v->bitrate;
523 enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate/2;
524 enc_ctx->qmin = v->qmin; /* should be configured */
525 enc_ctx->time_base = (AVRational){1, v->fps};
526 enc_ctx->gop_size = v->fps*5; // emit I frame every 5 seconds
528 v->enc->enc_init(v->enc_ctx);
530 if (avcodec_open(enc_ctx, v->codec) < 0) {
531 ast_log(LOG_WARNING, "Unable to initialize the encoder %d\n",
535 return video_out_uninit(env);
539 * Allocate enough for the encoded bitstream. As we are compressing,
540 * we hope that the output is never larger than the input size.
542 v->enc_out.data = ast_calloc(1, enc_in->size);
543 v->enc_out.size = enc_in->size;
549 /*! \brief possibly uninitialize the video console.
550 * Called at the end of a call, should reset the 'owner' field,
551 * then possibly terminate the video thread if the gui has
552 * not been started manually.
553 * In practice, signal the thread and give it a bit of time to
554 * complete, giving up if it gets stuck. Because uninit
555 * is called from hangup with the channel locked, and the thread
556 * uses the chan lock, we need to unlock here. This is unsafe,
557 * and we should really use refcounts for the channels.
559 void console_video_uninit(struct video_desc *env)
561 int i, t = 100; /* initial wait is shorter, than make it longer */
562 if (env->stayopen == 0) { /* gui opened by a call, do the shutdown */
564 for (i=0; env->shutdown && i < 10; i++) {
566 ast_channel_unlock(env->owner);
570 ast_channel_lock(env->owner);
574 env->owner = NULL; /* this is unconditional */
577 /*! fill an AVPicture from our fbuf info, as it is required by
578 * the image conversion routines in ffmpeg. Note that the pointers
579 * are recalculated if the fbuf has an offset (and so represents a picture in picture)
580 * XXX This depends on the format.
582 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
584 /* provide defaults for commonly used formats */
585 int l4 = b->w * b->h/4; /* size of U or V frame */
586 int len = b->w; /* Y linesize, bytes */
587 int luv = b->w/2; /* U/V linesize, bytes */
590 bzero(p, sizeof(*p));
591 switch (b->pix_fmt) {
601 case PIX_FMT_YUYV422: /* Packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr */
602 sample_size = 2; /* all data in first plane, probably */
608 p->data[0] = b->data;
609 p->linesize[0] = len;
610 /* these are only valid for component images */
611 p->data[1] = luv ? b->data + 4*l4 : b->data+len;
612 p->data[2] = luv ? b->data + 5*l4 : b->data+len;
613 p->linesize[1] = luv;
614 p->linesize[2] = luv;
616 /* add the offsets to the pointers previously calculated,
617 it is necessary for the picture in picture mode */
618 p->data[0] += len*b->win_y + b->win_x*sample_size;
620 p->data[1] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
621 p->data[2] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
626 /*! convert/scale between an input and an output format.
627 * Old version of ffmpeg only have img_convert, which does not rescale.
628 * New versions use sws_scale which does both.
630 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
631 struct fbuf_t *out, AVPicture *p_out)
633 AVPicture my_p_in, my_p_out;
634 int eff_w=out->w, eff_h=out->h;
637 p_in = fill_pict(in, &my_p_in);
639 p_out = fill_pict(out, &my_p_out);
641 /*if win_w is different from zero then we must change
642 the size of the scaled buffer (the position is already
643 encoded into the out parameter)*/
644 if (out->win_w) { /* picture in picture enabled */
649 /* XXX img_convert is deprecated, and does not do rescaling, PiP not supported */
650 img_convert(p_out, out->pix_fmt,
651 p_in, in->pix_fmt, in->w, in->h);
652 #else /* XXX replacement */
654 struct SwsContext *convert_ctx;
656 convert_ctx = sws_getContext(in->w, in->h, in->pix_fmt,
657 eff_w, eff_h, out->pix_fmt,
658 SWS_BICUBIC, NULL, NULL, NULL);
659 if (convert_ctx == NULL) {
660 ast_log(LOG_ERROR, "FFMPEG::convert_cmodel : swscale context initialization failed");
664 ast_log(LOG_WARNING, "in %d %dx%d out %d %dx%d\n",
665 in->pix_fmt, in->w, in->h, out->pix_fmt, eff_w, eff_h);
666 sws_scale(convert_ctx,
667 p_in->data, p_in->linesize,
668 in->w, in->h, /* src slice */
669 p_out->data, p_out->linesize);
671 sws_freeContext(convert_ctx);
673 #endif /* XXX replacement */
676 struct video_desc *get_video_desc(struct ast_channel *c);
679 * This function is called (by asterisk) for each video packet
680 * coming from the network (the 'in' path) that needs to be processed.
681 * We need to reconstruct the entire video frame before we can decode it.
682 * After a video packet is received we have to:
683 * - extract the bitstream with pre_process_data()
684 * - append the bitstream to a buffer
685 * - if the fragment is the last (RTP Marker) we decode it with decode_video()
686 * - after the decoding is completed we display the decoded frame with show_frame()
688 int console_write_video(struct ast_channel *chan, struct ast_frame *f);
689 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
691 struct video_desc *env = get_video_desc(chan);
692 struct video_dec_desc *v = env->in;
694 if (!env->gui) /* no gui, no rendering */
697 env->in = v = dec_init(f->subclass & ~1);
699 /* This is not fatal, but we won't have incoming video */
700 ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
704 if (v->dec_in_cur == NULL) /* no buffer for incoming frames, drop */
706 #if defined(DROP_PACKETS) && DROP_PACKETS > 0
707 /* Simulate lost packets */
708 if ((random() % 10000) <= 100*DROP_PACKETS) {
709 ast_log(LOG_NOTICE, "Packet lost [%d]\n", f->seqno);
715 * In discard mode, drop packets until we find one with
716 * the RTP marker set (which is the end of frame).
717 * Note that the RTP marker flag is sent as the LSB of the
718 * subclass, which is a bitmask of formats. The low bit is
719 * normally used for audio so there is no interference.
721 if (f->subclass & 0x01) {
722 v->dec_in_cur->used = 0;
723 v->dec_in_cur->ebit = 0;
724 v->next_seq = f->seqno + 1; /* wrap at 16 bit */
726 ast_log(LOG_WARNING, "out of discard mode, frame %d\n", f->seqno);
732 * Only in-order fragments will be accepted. Remember seqno
733 * has 16 bit so there is wraparound. Also, ideally we could
734 * accept a bit of reordering, but at the moment we don't.
736 if (v->next_seq != f->seqno) {
737 ast_log(LOG_WARNING, "discarding frame out of order, %d %d\n",
738 v->next_seq, f->seqno);
744 if (f->data.ptr == NULL || f->datalen < 2) {
745 ast_log(LOG_WARNING, "empty video frame, discard\n");
748 if (v->d_callbacks->dec_decap(v->dec_in_cur, f->data.ptr, f->datalen)) {
749 ast_log(LOG_WARNING, "error in dec_decap, enter discard\n");
752 if (f->subclass & 0x01) { // RTP Marker
753 /* prepare to decode: advance the buffer so the video thread knows. */
754 struct fbuf_t *tmp = v->dec_in_cur; /* store current pointer */
755 ast_mutex_lock(&env->dec_lock);
756 if (++v->dec_in_cur == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
757 v->dec_in_cur = &v->dec_in[0];
758 if (v->dec_in_dpy == NULL) { /* were not displaying anything, so set it */
760 } else if (v->dec_in_dpy == v->dec_in_cur) { /* current slot is busy */
761 v->dec_in_cur = NULL;
763 ast_mutex_unlock(&env->dec_lock);
769 /*! \brief refreshes the buffers of all the device by calling the
770 * grabber_read on each device in the device table.
771 * it encodes the primary source buffer, if the picture in picture mode is
772 * enabled it encodes (in the buffer to split) the secondary source buffer too.
773 * The encoded buffer is splitted to build the local and the remote view.
774 * Return a list of ast_frame representing the video fragments.
775 * The head pointer is returned by the function, the tail pointer
776 * is returned as an argument.
778 * \param env = video environment descriptor
779 * \param tail = tail ponter (pratically a return value)
781 static struct ast_frame *get_video_frames(struct video_desc *env, struct ast_frame **tail)
783 struct video_out_desc *v = &env->out;
784 struct ast_frame *dummy;
785 struct fbuf_t *loc_src_primary = NULL, *p_read;
787 /* if no device was found in the config file */
788 if (!env->out.device_num)
790 /* every time this function is called we refresh the buffers of every device,
791 updating the private device buffer in the device table */
792 for (i = 0; i < env->out.device_num; i++) {
793 p_read = grabber_read(&env->out.devices[i], env->out.fps);
794 /* it is used only if different from NULL, we mantain last good buffer otherwise */
796 env->out.devices[i].dev_buf = p_read;
798 /* select the primary device buffer as the one to encode */
799 loc_src_primary = env->out.devices[env->out.device_primary].dev_buf;
800 /* loc_src_primary can be NULL if the device has been turned off during
801 execution of it is read too early */
802 if (loc_src_primary) {
803 /* Scale the video for the encoder, then use it for local rendering
804 so we will see the same as the remote party */
805 my_scale(loc_src_primary, NULL, &env->enc_in, NULL);
807 if (env->out.picture_in_picture) { /* the picture in picture mode is enabled */
808 struct fbuf_t *loc_src_secondary;
809 /* reads from the secondary source */
810 loc_src_secondary = env->out.devices[env->out.device_secondary].dev_buf;
811 if (loc_src_secondary) {
812 env->enc_in.win_x = env->out.pip_x;
813 env->enc_in.win_y = env->out.pip_y;
814 env->enc_in.win_w = env->enc_in.w/3;
815 env->enc_in.win_h = env->enc_in.h/3;
816 /* scales to the correct geometry and inserts in
817 the enc_in buffer the picture in picture */
818 my_scale(loc_src_secondary, NULL, &env->enc_in, NULL);
819 /* returns to normal parameters (not picture in picture) */
820 env->enc_in.win_x = 0;
821 env->enc_in.win_y = 0;
822 env->enc_in.win_w = 0;
823 env->enc_in.win_h = 0;
826 /* loc_src_secondary can be NULL if the device has been turned off during
827 execution of it is read too early */
828 env->out.picture_in_picture = 0; /* disable picture in picture */
831 show_frame(env, WIN_LOCAL); /* local rendering */
832 for (i = 0; i < env->out.device_num; i++)
833 show_frame(env, i+WIN_SRC1); /* rendering of every source device in thumbnails */
837 /* if no reason for encoding, do not encode */
838 if (!env->owner || !loc_src_primary || !v->sendvideo)
840 if (v->enc_out.data == NULL) {
841 static volatile int a = 0;
843 ast_log(LOG_WARNING, "fail, no encoder output buffer\n");
847 return v->enc->enc_encap(&v->enc_out, v->mtu, tail);
851 * Helper thread to periodically poll the video sources and enqueue the
852 * generated frames directed to the remote party to the channel's queue.
853 * Using a separate thread also helps because the encoding can be
854 * computationally expensive so we don't want to starve the main thread.
856 static void *video_thread(void *arg)
858 struct video_desc *env = arg;
860 char save_display[128] = "";
861 int i; /* integer variable used as iterator */
863 /* if sdl_videodriver is set, override the environment. Also,
864 * if it contains 'console' override DISPLAY around the call to SDL_Init
865 * so we use the console as opposed to the x11 version of aalib
867 if (!ast_strlen_zero(env->sdl_videodriver)) { /* override */
868 const char *s = getenv("DISPLAY");
869 setenv("SDL_VIDEODRIVER", env->sdl_videodriver, 1);
870 if (s && !strcasecmp(env->sdl_videodriver, "aalib-console")) {
871 ast_copy_string(save_display, s, sizeof(save_display));
876 if (!ast_strlen_zero(save_display))
877 setenv("DISPLAY", save_display, 1);
879 ast_mutex_init(&env->dec_lock); /* used to sync decoder and renderer */
881 if (grabber_open(&env->out)) {
882 ast_log(LOG_WARNING, "cannot open local video source\n");
885 if (env->out.device_num)
886 env->out.devices[env->out.device_primary].status_index |= IS_PRIMARY | IS_SECONDARY;
888 /* even if no device is connected, we must call video_out_init,
889 * as some of the data structures it initializes are
890 * used in get_video_frames()
894 /* Writes intial status of the sources. */
895 for (i = 0; i < env->out.device_num; i++) {
896 print_message(env->gui->thumb_bd_array[i].board,
897 src_msgs[env->out.devices[i].status_index]);
901 struct timeval t = { 0, 50000 }; /* XXX 20 times/sec */
902 struct ast_frame *p, *f;
903 struct ast_channel *chan;
905 char *caption = NULL, buf[160];
907 /* determine if video format changed */
908 if (count++ % 10 == 0) {
909 if (env->out.sendvideo && env->out.devices)
910 sprintf(buf, "%s %s %dx%d @@ %dfps %dkbps",
911 env->out.devices[env->out.device_primary].name, env->codec_name,
912 env->enc_in.w, env->enc_in.h,
913 env->out.fps, env->out.bitrate/1000);
915 sprintf(buf, "hold");
919 /* manage keypad events */
920 /* XXX here we should always check for events,
921 * otherwise the drag will not work */
923 eventhandler(env, caption);
925 /* sleep for a while */
926 ast_select(0, NULL, NULL, NULL, &t);
929 struct video_dec_desc *v = env->in;
932 * While there is something to display, call the decoder and free
933 * the buffer, possibly enabling the receiver to store new data.
935 while (v->dec_in_dpy) {
936 struct fbuf_t *tmp = v->dec_in_dpy; /* store current pointer */
938 /* decode the frame, but show it only if not frozen */
939 if (v->d_callbacks->dec_run(v, tmp) && !env->frame_freeze)
940 show_frame(env, WIN_REMOTE);
941 tmp->used = 0; /* mark buffer as free */
943 ast_mutex_lock(&env->dec_lock);
944 if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
945 v->dec_in_dpy = &v->dec_in[0];
947 if (v->dec_in_cur == NULL) /* receiver was idle, enable it... */
948 v->dec_in_cur = tmp; /* using the slot just freed */
949 else if (v->dec_in_dpy == v->dec_in_cur) /* this was the last slot */
950 v->dec_in_dpy = NULL; /* nothing more to display */
951 ast_mutex_unlock(&env->dec_lock);
957 f = get_video_frames(env, &p); /* read and display */
962 /* drop the chain of frames, nobody uses them */
964 struct ast_frame *g = AST_LIST_NEXT(f, frame_list);
970 fd = chan->alertpipe[1];
971 ast_channel_lock(chan);
973 /* AST_LIST_INSERT_TAIL is only good for one frame, cannot use here */
974 if (chan->readq.first == NULL) {
975 chan->readq.first = f;
977 chan->readq.last->frame_list.next = f;
979 chan->readq.last = p;
981 * more or less same as ast_queue_frame, but extra
982 * write on the alertpipe to signal frames.
985 int blah = 1, l = sizeof(blah);
986 for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
987 if (write(fd, &blah, l) != l)
988 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
989 chan->name, f->frametype, f->subclass, strerror(errno));
992 ast_channel_unlock(chan);
994 /* thread terminating, here could call the uninit */
995 /* uninitialize the local and remote video environments */
996 env->in = dec_uninit(env->in);
997 video_out_uninit(env);
1000 env->gui = cleanup_sdl(env->gui, env->out.device_num);
1001 ast_mutex_destroy(&env->dec_lock);
1006 static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
1014 /*! initialize the video environment.
1015 * Apart from the formats (constant) used by sdl and the codec,
1016 * we use enc_in as the basic geometry.
1018 static void init_env(struct video_desc *env)
1020 struct fbuf_t *c = &(env->out.loc_src_geometry); /* local source */
1021 struct fbuf_t *ei = &(env->enc_in); /* encoder input */
1022 struct fbuf_t *ld = &(env->loc_dpy); /* local display */
1023 struct fbuf_t *rd = &(env->rem_dpy); /* remote display */
1024 int i; /* integer working as iterator */
1026 c->pix_fmt = PIX_FMT_YUV420P; /* default - camera format */
1027 ei->pix_fmt = PIX_FMT_YUV420P; /* encoder input */
1028 if (ei->w == 0 || ei->h == 0) {
1032 ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P; /* sdl format */
1033 /* inherit defaults */
1034 copy_geometry(ei, c); /* camera inherits from encoder input */
1035 copy_geometry(ei, rd); /* remote display inherits from encoder input */
1036 copy_geometry(rd, ld); /* local display inherits from remote display */
1038 /* fix the size of buffers for small windows */
1039 for (i = 0; i < env->out.device_num; i++) {
1040 env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
1041 env->src_dpy[i].w = SRC_WIN_W;
1042 env->src_dpy[i].h = SRC_WIN_H;
1044 /* now we set the default coordinates for the picture in picture
1045 frames inside the env_in buffers, those can be changed by dragging the
1046 picture in picture with left click */
1047 env->out.pip_x = ei->w - ei->w/3;
1048 env->out.pip_y = ei->h - ei->h/3;
1052 * The first call to the video code, called by oss_new() or similar.
1053 * Here we initialize the various components we use, namely SDL for display,
1054 * ffmpeg for encoding/decoding, and a local video source.
1055 * We do our best to progress even if some of the components are not
1058 void console_video_start(struct video_desc *env, struct ast_channel *owner)
1060 ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
1061 if (env == NULL) /* video not initialized */
1063 env->owner = owner; /* work even if no owner is specified */
1065 return; /* already initialized, nothing to do */
1067 env->out.enc = map_config_video_format(env->codec_name);
1069 ast_log(LOG_WARNING, "start video out %s %dx%d\n",
1070 env->codec_name, env->enc_in.w, env->enc_in.h);
1072 * Register all codecs supported by the ffmpeg library.
1073 * We only need to do it once, but probably doesn't
1074 * harm to do it multiple times.
1077 avcodec_register_all();
1078 av_log_set_level(AV_LOG_ERROR); /* only report errors */
1080 if (env->out.fps == 0) {
1082 ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
1084 if (env->out.bitrate == 0) {
1085 env->out.bitrate = 65000;
1086 ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
1088 /* XXX below probably can use ast_pthread_create_detace\hed() */
1089 ast_pthread_create_background(&env->vthread, NULL, video_thread, env);
1090 /* detach the thread to make sure memory is freed on termination */
1091 pthread_detach(env->vthread);
1095 * Parse a geometry string, accepting also common names for the formats.
1096 * Trick: if we have a leading > or < and a numeric geometry,
1097 * return the larger or smaller one.
1098 * E.g. <352x288 gives the smaller one, 320x240
1100 static int video_geom(struct fbuf_t *b, const char *s)
1105 const char *s; int w; int h;
1106 } *fp, formats[] = {
1107 {"16cif", 1408, 1152 },
1108 {"xga", 1024, 768 },
1109 {"4cif", 704, 576 },
1112 {"qvga", 320, 240 },
1113 {"qcif", 176, 144 },
1114 {"sqcif", 128, 96 },
1117 if (*s == '<' || *s == '>')
1118 sscanf(s+1,"%dx%d", &w, &h);
1119 for (fp = formats; fp->s; fp++) {
1120 if (*s == '>') { /* look for a larger one */
1123 fp--; /* back one step if possible */
1126 } else if (*s == '<') { /* look for a smaller one */
1129 } else if (!strcasecmp(s, fp->s)) { /* look for a string */
1133 if (*s == '<' && fp->s == NULL) /* smallest */
1138 } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
1139 ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
1147 /*! \brief add an entry to the video_device table,
1148 * ignoring duplicate names.
1149 * The table is a static array of 9 elements.
1150 * The last_frame field of each entry of the table is initialized to
1151 * the current time (we need a value inside this field, on stop of the
1152 * GUI the last_frame value is not changed, to avoid checking if it is 0 we
1153 * set the initial value on current time) XXX
1156 * \param devices_p = pointer to the table of devices
1157 * \param device_num_p = pointer to the number of devices
1158 * \param s = name of the new device to insert
1160 * returns 0 on success, 1 on error
1162 static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
1165 struct video_device *p;
1167 /* with the current implementation, we support a maximum of 9 devices.*/
1168 if (*device_num_p >= 9)
1169 return 0; /* more devices will be ignored */
1170 /* ignore duplicate names */
1171 for (i = 0; i < *device_num_p; i++) {
1172 if (!strcmp(devices[i].name, s))
1175 /* inserts the new video device */
1176 p = &devices[*device_num_p];
1177 /* XXX the string is allocated but NEVER deallocated,
1178 the good time to do that is when the module is unloaded, now we skip the problem */
1179 p->name = ast_strdup(s); /* copy the name */
1180 /* other fields initially NULL */
1182 p->grabber_data = NULL;
1184 p->last_frame = ast_tvnow();
1185 p->status_index = 0;
1186 (*device_num_p)++; /* one device added */
1190 /* extend ast_cli with video commands. Called by console_video_config */
1191 int console_video_cli(struct video_desc *env, const char *var, int fd)
1194 return 1; /* unrecognised */
1196 if (!strcasecmp(var, "videodevice")) {
1197 ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
1198 } else if (!strcasecmp(var, "videocodec")) {
1199 ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
1200 } else if (!strcasecmp(var, "sendvideo")) {
1201 ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
1202 } else if (!strcasecmp(var, "video_size")) {
1203 int in_w = 0, in_h = 0;
1205 in_w = env->in->dec_out.w;
1206 in_h = env->in->dec_out.h;
1208 ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
1209 env->enc_in.w, env->enc_in.h,
1210 env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
1211 env->loc_dpy.w, env->loc_dpy.h,
1212 env->rem_dpy.w, env->rem_dpy.h,
1214 } else if (!strcasecmp(var, "bitrate")) {
1215 ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
1216 } else if (!strcasecmp(var, "qmin")) {
1217 ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
1218 } else if (!strcasecmp(var, "fps")) {
1219 ast_cli(fd, "fps is [%d]\n", env->out.fps);
1220 } else if (!strcasecmp(var, "startgui")) {
1222 console_video_start(env, NULL);
1223 } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
1225 if (env->gui && env->owner)
1226 ast_cli_command(-1, "console hangup");
1227 else /* not in a call */
1228 console_video_uninit(env);
1230 return 1; /* unrecognised */
1232 return 0; /* recognised */
1235 /*! parse config command for video support. */
1236 int console_video_config(struct video_desc **penv,
1237 const char *var, const char *val)
1239 struct video_desc *env;
1242 ast_log(LOG_WARNING, "bad argument penv=NULL\n");
1243 return 1; /* error */
1245 /* allocate the video descriptor first time we get here */
1248 env = *penv = ast_calloc(1, sizeof(struct video_desc));
1250 ast_log(LOG_WARNING, "fail to allocate video_desc\n");
1251 return 1; /* error */
1254 /* set default values - 0's are already there */
1255 env->out.device_primary = 0;
1256 env->out.device_secondary = 0;
1258 env->out.bitrate = 65000;
1259 env->out.sendvideo = 1;
1261 env->out.device_num = 0;
1264 CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
1265 CV_BOOL("sendvideo", env->out.sendvideo);
1266 CV_F("video_size", video_geom(&env->enc_in, val));
1267 CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
1268 CV_F("local_size", video_geom(&env->loc_dpy, val));
1269 CV_F("remote_size", video_geom(&env->rem_dpy, val));
1270 CV_STR("keypad", env->keypad_file);
1271 CV_F("region", keypad_cfg_read(env->gui, val));
1272 CV_UINT("startgui", env->stayopen); /* enable gui at startup */
1273 CV_STR("keypad_font", env->keypad_font);
1274 CV_STR("sdl_videodriver", env->sdl_videodriver);
1275 CV_UINT("fps", env->out.fps);
1276 CV_UINT("bitrate", env->out.bitrate);
1277 CV_UINT("qmin", env->out.qmin);
1278 CV_STR("videocodec", env->codec_name);
1279 return 1; /* nothing found */
1281 CV_END; /* the 'nothing found' case */
1282 return 0; /* found something */
1285 #endif /* video support */