Thu Oct 11 06:33:45 2012

Asterisk developer's documentation


console_video.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright 2007-2008, Marta Carbone, Sergio Fadda, Luigi Rizzo
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*
00018  * Experimental support for video sessions. We use SDL for rendering, ffmpeg
00019  * as the codec library for encoding and decoding, and Video4Linux and X11
00020  * to generate the local video stream.
00021  *
00022  * If one of these pieces is not available, either at compile time or at
00023  * runtime, we do our best to run without it. Of course, no codec library
00024  * means we can only deal with raw data, no SDL means we cannot do rendering,
00025  * no V4L or X11 means we cannot generate data (but in principle we could
00026  * stream from or record to a file).
00027  *
00028  * We need a recent (2007.07.12 or newer) version of ffmpeg to avoid warnings.
00029  * Older versions might give 'deprecated' messages during compilation,
00030  * thus not compiling in AST_DEVMODE, or don't have swscale, in which case
00031  * you can try to compile #defining OLD_FFMPEG here.
00032  *
00033  * $Revision: 369013 $
00034  */
00035 
00036 //#define DROP_PACKETS 5       /* if set, drop this % of video packets */
00037 //#define OLD_FFMPEG 1  /* set for old ffmpeg with no swscale */
00038 
00039 /*** MODULEINFO
00040    <support_level>extended</support_level>
00041  ***/
00042 
00043 #include "asterisk.h"
00044 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369013 $")
00045 #include <sys/ioctl.h>
00046 #include "asterisk/cli.h"
00047 #include "asterisk/file.h"
00048 #include "asterisk/channel.h"
00049 
00050 #include "console_video.h"
00051 
00052 /*
00053 The code is structured as follows.
00054 
00055 When a new console channel is created, we call console_video_start()
00056 to initialize SDL, the source, and the encoder/ decoder for the
00057 formats in use (XXX the latter two should be done later, once the
00058 codec negotiation is complete).  Also, a thread is created to handle
00059 the video source and generate frames.
00060 
00061 While communication is on, the local source is generated by the
00062 video thread, which wakes up periodically, generates frames and
00063 enqueues them in chan->readq.  Incoming rtp frames are passed to
00064 console_write_video(), decoded and passed to SDL for display.
00065 
00066 For as unfortunate and confusing as it can be, we need to deal with a
00067 number of different video representations (size, codec/pixel format,
00068 codec parameters), as follows:
00069 
00070  loc_src is the data coming from the camera/X11/etc.
00071    The format is typically constrained by the video source.
00072 
00073  enc_in     is the input required by the encoder.
00074    Typically constrained in size by the encoder type.
00075 
00076  enc_out is the bitstream transmitted over RTP.
00077    Typically negotiated while the call is established.
00078 
00079  loc_dpy is the format used to display the local video source.
00080    Depending on user preferences this can have the same size as
00081    loc_src_fmt, or enc_in_fmt, or thumbnail size (e.g. PiP output)
00082 
00083  dec_in     is the incoming RTP bitstream. Negotiated
00084    during call establishment, it is not necessarily the same as
00085    enc_in_fmt
00086 
00087  dec_out the output of the decoder.
00088    The format is whatever the other side sends, and the
00089    buffer is allocated by avcodec_decode_... so we only
00090    copy the data here.
00091 
00092  rem_dpy the format used to display the remote stream
00093 
00094  src_dpy is the format used to display the local video source streams
00095    The number of these fbuf_t is determined at run time, with dynamic allocation
00096 
00097 We store the format info together with the buffer storing the data.
00098 As a future optimization, a format/buffer may reference another one
00099 if the formats are equivalent. This will save some unnecessary format
00100 conversion.
00101 
00102 
00103 In order to handle video you need to add to sip.conf (and presumably
00104 iax.conf too) the following:
00105 
00106    [general](+)
00107       videosupport=yes
00108       allow=h263  ; this or other video formats
00109       allow=h263p ; this or other video formats
00110 
00111  */
00112 
00113 /*
00114  * Codecs are absolutely necessary or we cannot do anything.
00115  * SDL is optional (used for rendering only), so that we can still
00116  * stream video withouth displaying it.
00117  */
00118 #if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG)
00119 /* stubs if required pieces are missing */
00120 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
00121 {
00122    return 0;   /* writing video not supported */
00123 }
00124 
00125 int console_video_cli(struct video_desc *env, const char *var, int fd)
00126 {
00127    return 1;   /* nothing matched */
00128 }
00129 
00130 int console_video_config(struct video_desc **penv, const char *var, const char *val)
00131 {
00132    return 1;   /* no configuration */
00133 }
00134 
00135 void console_video_start(struct video_desc *env, struct ast_channel *owner)
00136 {
00137    ast_log(LOG_NOTICE, "voice only, console video support not present\n");
00138 }
00139 
00140 void console_video_uninit(struct video_desc *env)
00141 {
00142 }
00143 
00144 int get_gui_startup(struct video_desc* env)
00145 {
00146    return 0; /* no gui here */
00147 }
00148 
00149 int console_video_formats = 0;
00150 
00151 #else /* defined(HAVE_FFMPEG) && defined(HAVE_SDL) */
00152 
00153 /*! The list of video formats we support. */
00154 int console_video_formats = 
00155    AST_FORMAT_H263_PLUS | AST_FORMAT_H263 |
00156    AST_FORMAT_MP4_VIDEO | AST_FORMAT_H264 | AST_FORMAT_H261 ;
00157 
00158 
00159 
00160 /* function to scale and encode buffers */
00161 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
00162    struct fbuf_t *out, AVPicture *p_out);
00163 
00164 /*
00165  * this structure will be an entry in the table containing
00166  * every device specified in the file oss.conf, it contains various infomation
00167  * about the device
00168  */
00169 struct video_device {
00170    char        *name;      /* name of the device         */
00171    /* allocated dynamically (see fill_table function) */
00172    struct grab_desc  *grabber;   /* the grabber for the device type  */
00173    void        *grabber_data; /* device's private data structure  */
00174    struct fbuf_t     *dev_buf;   /* buffer for incoming data      */
00175    struct timeval    last_frame; /* when we read the last frame ? */
00176    int         status_index;  /* what is the status of the device (source) */
00177    /* status index is set using the IS_ON, IS_PRIMARY and IS_SECONDARY costants */
00178    /* status_index is the index of the status message in the src_msgs array in console_gui.c */
00179 };
00180 
00181 struct video_codec_desc;   /* forward declaration */
00182 /*
00183  * Descriptor of the local source, made of the following pieces:
00184  *  + configuration info (geometry, device name, fps...). These are read
00185  *    from the config file and copied here before calling video_out_init();
00186  *  + the frame buffer (buf) and source pixel format, allocated at init time;
00187  *  + the encoding and RTP info, including timestamps to generate
00188  *    frames at the correct rate;
00189  *  + source-specific info, i.e. fd for /dev/video, dpy-image for x11, etc,
00190  *    filled in by grabber_open, part of source_specific information are in 
00191  *    the device table (devices member), others are shared;
00192  * NOTE: loc_src.data == NULL means the rest of the struct is invalid, and
00193  * the video source is not available.
00194  */
00195 struct video_out_desc {
00196    /* video device support.
00197     * videodevice and geometry are read from the config file.
00198     * At the right time we try to open it and allocate a buffer.
00199     * If we are successful, webcam_bufsize > 0 and we can read.
00200     */
00201    /* all the following is config file info copied from the parent */
00202    int      fps;
00203    int      bitrate;
00204    int      qmin;
00205 
00206    int sendvideo;
00207 
00208    struct fbuf_t  loc_src_geometry; /* local source geometry only (from config file) */
00209    struct fbuf_t  enc_out; /* encoder output buffer, allocated in video_out_init() */
00210 
00211    struct video_codec_desc *enc; /* encoder */
00212    void     *enc_ctx;   /* encoding context */
00213    AVCodec     *codec;
00214    AVFrame     *enc_in_frame; /* enc_in mapped into avcodec format. */
00215                /* The initial part of AVFrame is an AVPicture */
00216    int      mtu;
00217    
00218    /* Table of devices specified with "videodevice=" in oss.conf.
00219     * Static size as we have a limited number of entries.
00220     */
00221    struct video_device  devices[MAX_VIDEO_SOURCES]; 
00222    int         device_num; /*number of devices in table*/
00223    int         device_primary; /*index of the actual primary device in the table*/
00224    int         device_secondary; /*index of the actual secondary device in the table*/
00225 
00226    int         picture_in_picture; /*Is the PiP mode activated? 0 = NO | 1 = YES*/
00227 
00228    /* these are the coordinates of the picture inside the picture (visible if PiP mode is active) 
00229    these coordinates are valid considering the containing buffer with cif geometry*/
00230    int         pip_x;
00231    int         pip_y;
00232 };
00233 
00234 /*
00235  * The overall descriptor, with room for config info, video source and
00236  * received data descriptors, SDL info, etc.
00237  * This should be globally visible to all modules (grabber, vcodecs, gui)
00238  * and contain all configurtion info.
00239  */
00240 struct video_desc {
00241    char codec_name[64];        /* the codec we use */
00242 
00243    int stayopen;               /* set if gui starts manually */
00244    pthread_t vthread;          /* video thread */
00245    ast_mutex_t dec_lock;       /* sync decoder and video thread */
00246    int shutdown;               /* set to shutdown vthread */
00247    struct ast_channel   *owner; /* owner channel */
00248 
00249 
00250    struct fbuf_t enc_in;       /* encoder input buffer, allocated in video_out_init() */
00251 
00252    char keypad_file[256];      /* image for the keypad */
00253    char keypad_font[256];      /* font for the keypad */
00254 
00255    char sdl_videodriver[256];
00256 
00257    struct fbuf_t rem_dpy;      /* display remote video, no buffer (it is in win[WIN_REMOTE].bmp) */
00258    struct fbuf_t loc_dpy;      /* display local source, no buffer (managed by SDL in bmp[1]) */
00259 
00260    /* geometry of the thumbnails for all video sources. */
00261    struct fbuf_t src_dpy[MAX_VIDEO_SOURCES]; /* no buffer allocated here */
00262 
00263    int frame_freeze;           /* flag to freeze the incoming frame */
00264 
00265    /* local information for grabbers, codecs, gui */
00266    struct gui_info *gui;
00267    struct video_dec_desc *in;  /* remote video descriptor */
00268    struct video_out_desc out;  /* local video descriptor */
00269 };
00270 
00271 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p);
00272 
00273 void fbuf_free(struct fbuf_t *b)
00274 {
00275    struct fbuf_t x = *b;
00276 
00277    if (b->data && b->size)
00278       ast_free(b->data);
00279    memset(b, '\0', sizeof(*b));
00280    /* restore some fields */
00281    b->w = x.w;
00282    b->h = x.h;
00283    b->pix_fmt = x.pix_fmt;
00284 }
00285 
00286 /* return the status of env->stayopen to chan_oss, as the latter
00287  * does not have access to fields of struct video_desc
00288  */
00289 int get_gui_startup(struct video_desc* env)
00290 {
00291    return env ? env->stayopen : 0;
00292 }
00293 
00294 #if 0
00295 /* helper function to print the amount of memory used by the process.
00296  * Useful to track memory leaks, unfortunately this code is OS-specific
00297  * so we keep it commented out.
00298  */
00299 static int
00300 used_mem(const char *msg)
00301 {
00302    char in[128];
00303 
00304    pid_t pid = getpid();
00305    sprintf(in, "ps -o vsz= -o rss= %d", pid);
00306    ast_log(LOG_WARNING, "used mem (vsize, rss) %s ", msg);
00307    system(in);
00308    return 0;
00309 }
00310 #endif
00311    
00312 #include "vcodecs.c"
00313 #include "console_gui.c"
00314 
00315 /*! \brief Try to open video sources, return 0 on success, 1 on error
00316  * opens all video sources found in the oss.conf configuration files.
00317  * Saves the grabber and the datas in the device table (in the devices field
00318  * of the descriptor referenced by v).
00319  * Initializes the device_primary and device_secondary
00320  * fields of v with the first devices that was
00321  * successfully opened.
00322  *
00323  * \param v = video out environment descriptor
00324  *
00325  * returns 0 on success, 1 on error 
00326 */
00327 static int grabber_open(struct video_out_desc *v)
00328 {
00329    struct grab_desc *g;
00330    void *g_data;
00331    int i, j;
00332 
00333    /* for each device in the device table... */
00334    for (i = 0; i < v->device_num; i++) {
00335       /* device already open */
00336       if (v->devices[i].grabber)
00337          continue;
00338       /* for each type of grabber supported... */
00339       for (j = 0; (g = console_grabbers[j]); j++) {
00340          /* the grabber is opened and the informations saved in the device table */
00341          g_data = g->open(v->devices[i].name, &v->loc_src_geometry, v->fps);
00342          if (!g_data)
00343             continue;
00344          v->devices[i].grabber = g;
00345          v->devices[i].grabber_data = g_data;
00346          v->devices[i].status_index |= IS_ON;
00347       }
00348    }
00349    /* the first working device is selected as the primary one and the secondary one */
00350    for (i = 0; i < v->device_num; i++) {
00351       if (!v->devices[i].grabber) 
00352          continue;
00353       v->device_primary = i;
00354       v->device_secondary = i;
00355       return 0; /* source found */
00356    }
00357    return 1; /* no source found */
00358 }
00359 
00360 
00361 /*! \brief complete a buffer from the specified local video source.
00362  * Called by get_video_frames(), in turn called by the video thread.
00363  *
00364  * \param dev = video environment descriptor
00365  * \param fps = frame per seconds, for every device
00366  *
00367  * returns:
00368  * - NULL on falure
00369  * - reference to the device buffer on success
00370  */
00371 static struct fbuf_t *grabber_read(struct video_device *dev, int fps)
00372 {
00373    struct timeval now = ast_tvnow();
00374 
00375    if (dev->grabber == NULL) /* not initialized */
00376       return NULL;
00377    
00378    /* the last_frame field in this row of the device table (dev)
00379    is always initialized, it is set during the parsing of the config
00380    file, and never unset, function fill_device_table(). */
00381    /* check if it is time to read */
00382    if (ast_tvdiff_ms(now, dev->last_frame) < 1000/fps)
00383       return NULL; /* too early */
00384    dev->last_frame = now; /* XXX actually, should correct for drift */
00385    return dev->grabber->read(dev->grabber_data);
00386 }
00387 
00388 /*! \brief handler run when dragging with the left button on
00389  * the local source window - the effect is to move the offset
00390  * of the captured area.
00391  */
00392 static void grabber_move(struct video_device *dev, int dx, int dy)
00393 {
00394    if (dev->grabber && dev->grabber->move) {
00395       dev->grabber->move(dev->grabber_data, dx, dy);
00396    }
00397 }
00398 
00399 /*
00400  * Map the codec name to the library. If not recognised, use a default.
00401  * This is useful in the output path where we decide by name, presumably.
00402  */
00403 static struct video_codec_desc *map_config_video_format(char *name)
00404 {
00405    int i;
00406 
00407    for (i = 0; supported_codecs[i]; i++)
00408       if (!strcasecmp(name, supported_codecs[i]->name))
00409          break;
00410    if (supported_codecs[i] == NULL) {
00411       ast_log(LOG_WARNING, "Cannot find codec for '%s'\n", name);
00412       i = 0;
00413       strcpy(name, supported_codecs[i]->name);
00414    }
00415    ast_log(LOG_WARNING, "Using codec '%s'\n", name);
00416    return supported_codecs[i];
00417 }
00418 
00419 
00420 /*! \brief uninitialize the descriptor for local video stream */
00421 static int video_out_uninit(struct video_desc *env)
00422 {
00423    struct video_out_desc *v = &env->out;
00424    int i; /* integer variable used as iterator */
00425    
00426    /* XXX this should be a codec callback */
00427    if (v->enc_ctx) {
00428       AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx;
00429       avcodec_close(enc_ctx);
00430       av_free(enc_ctx);
00431       v->enc_ctx = NULL;
00432    }
00433    if (v->enc_in_frame) {
00434       av_free(v->enc_in_frame);
00435       v->enc_in_frame = NULL;
00436    }
00437    v->codec = NULL;  /* nothing to free, this is only a reference */
00438    /* release the buffers */
00439    fbuf_free(&env->enc_in);
00440    fbuf_free(&v->enc_out);
00441    /* close the grabbers */
00442    for (i = 0; i < v->device_num; i++) {
00443       if (v->devices[i].grabber){
00444          v->devices[i].grabber_data =
00445             v->devices[i].grabber->close(v->devices[i].grabber_data);
00446          v->devices[i].grabber = NULL;
00447          /* dev_buf is already freed by grabber->close() */
00448          v->devices[i].dev_buf = NULL;
00449       }
00450       v->devices[i].status_index = 0;
00451    }
00452    v->picture_in_picture = 0;
00453    env->frame_freeze = 0;
00454    return -1;
00455 }
00456 
00457 /*
00458  * Initialize the encoder for the local source:
00459  * - enc_ctx, codec, enc_in_frame are used by ffmpeg for encoding;
00460  * - enc_out is used to store the encoded frame (to be sent)
00461  * - mtu is used to determine the max size of video fragment
00462  * NOTE: we enter here with the video source already open.
00463  */
00464 static int video_out_init(struct video_desc *env)
00465 {
00466    int codec;
00467    int size;
00468    struct fbuf_t *enc_in;
00469    struct video_out_desc *v = &env->out;
00470 
00471    v->enc_ctx     = NULL;
00472    v->codec    = NULL;
00473    v->enc_in_frame      = NULL;
00474    v->enc_out.data      = NULL;
00475 
00476    codec = map_video_format(v->enc->format, CM_WR);
00477    v->codec = avcodec_find_encoder(codec);
00478    if (!v->codec) {
00479       ast_log(LOG_WARNING, "Cannot find the encoder for format %d\n",
00480          codec);
00481       return -1;  /* error, but nothing to undo yet */
00482    }
00483 
00484    v->mtu = 1400; /* set it early so the encoder can use it */
00485 
00486    /* allocate the input buffer for encoding.
00487     * ffmpeg only supports PIX_FMT_YUV420P for the encoding.
00488     */
00489    enc_in = &env->enc_in;
00490    enc_in->pix_fmt = PIX_FMT_YUV420P;
00491    enc_in->size = (enc_in->w * enc_in->h * 3)/2;
00492    enc_in->data = ast_calloc(1, enc_in->size);
00493    if (!enc_in->data) {
00494       ast_log(LOG_WARNING, "Cannot allocate encoder input buffer\n");
00495       return video_out_uninit(env);
00496    }
00497    /* construct an AVFrame that points into buf_in */
00498    v->enc_in_frame = avcodec_alloc_frame();
00499    if (!v->enc_in_frame) {
00500       ast_log(LOG_WARNING, "Unable to allocate the encoding video frame\n");
00501       return video_out_uninit(env);
00502    }
00503 
00504    /* parameters for PIX_FMT_YUV420P */
00505    size = enc_in->w * enc_in->h;
00506    v->enc_in_frame->data[0] = enc_in->data;
00507    v->enc_in_frame->data[1] = v->enc_in_frame->data[0] + size;
00508    v->enc_in_frame->data[2] = v->enc_in_frame->data[1] + size/4;
00509    v->enc_in_frame->linesize[0] = enc_in->w;
00510    v->enc_in_frame->linesize[1] = enc_in->w/2;
00511    v->enc_in_frame->linesize[2] = enc_in->w/2;
00512 
00513    /* now setup the parameters for the encoder.
00514     * XXX should be codec-specific
00515     */
00516    {
00517       AVCodecContext *enc_ctx = avcodec_alloc_context();
00518       v->enc_ctx = enc_ctx;
00519       enc_ctx->pix_fmt = enc_in->pix_fmt;
00520       enc_ctx->width = enc_in->w;
00521       enc_ctx->height = enc_in->h;
00522       /* XXX rtp_callback ?
00523        * rtp_mode so ffmpeg inserts as many start codes as possible.
00524        */
00525       enc_ctx->rtp_mode = 1;
00526       enc_ctx->rtp_payload_size = v->mtu / 2; // mtu/2
00527       enc_ctx->bit_rate = v->bitrate;
00528       enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate/2;
00529       enc_ctx->qmin = v->qmin;   /* should be configured */
00530       enc_ctx->time_base = (AVRational){1, v->fps};
00531       enc_ctx->gop_size = v->fps*5; // emit I frame every 5 seconds
00532 
00533       v->enc->enc_init(v->enc_ctx);
00534 
00535       if (avcodec_open(enc_ctx, v->codec) < 0) {
00536          ast_log(LOG_WARNING, "Unable to initialize the encoder %d\n", codec);
00537          av_free(enc_ctx);
00538          v->enc_ctx = NULL;
00539          return video_out_uninit(env);
00540       }
00541    }
00542    /*
00543     * Allocate enough for the encoded bitstream. As we are compressing,
00544     * we hope that the output is never larger than the input size.
00545     */
00546    v->enc_out.data = ast_calloc(1, enc_in->size);
00547    v->enc_out.size = enc_in->size;
00548    v->enc_out.used = 0;
00549 
00550    return 0;
00551 }
00552 
00553 /*! \brief possibly uninitialize the video console.
00554  * Called at the end of a call, should reset the 'owner' field,
00555  * then possibly terminate the video thread if the gui has
00556  * not been started manually.
00557  * In practice, signal the thread and give it a bit of time to
00558  * complete, giving up if it gets stuck. Because uninit
00559  * is called from hangup with the channel locked, and the thread
00560  * uses the chan lock, we need to unlock here. This is unsafe,
00561  * and we should really use refcounts for the channels.
00562  */
00563 void console_video_uninit(struct video_desc *env)
00564 {
00565    int i, t = 100;   /* initial wait is shorter, than make it longer */
00566    if (env->stayopen == 0) { /* gui opened by a call, do the shutdown */
00567       env->shutdown = 1;
00568       for (i=0; env->shutdown && i < 10; i++) {
00569          if (env->owner)
00570             ast_channel_unlock(env->owner);
00571          usleep(t);
00572          t = 1000000;
00573          if (env->owner)
00574             ast_channel_lock(env->owner);
00575       }
00576       env->vthread = NULL;
00577    }
00578    env->owner = NULL;   /* this is unconditional */
00579 }
00580 
00581 /*! fill an AVPicture from our fbuf info, as it is required by
00582  * the image conversion routines in ffmpeg. Note that the pointers
00583  * are recalculated if the fbuf has an offset (and so represents a picture in picture)
00584  * XXX This depends on the format.
00585  */
00586 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
00587 {
00588    /* provide defaults for commonly used formats */
00589    int l4 = b->w * b->h/4; /* size of U or V frame */
00590    int len = b->w;      /* Y linesize, bytes */
00591    int luv = b->w/2; /* U/V linesize, bytes */
00592    int sample_size = 1;
00593    
00594    memset(p, '\0', sizeof(*p));
00595    switch (b->pix_fmt) {
00596    case PIX_FMT_RGB555:
00597    case PIX_FMT_RGB565:
00598       sample_size = 2;
00599       luv = 0;
00600       break;
00601    case PIX_FMT_RGBA32:
00602       sample_size = 4;
00603       luv = 0;
00604       break;
00605    case PIX_FMT_YUYV422:   /* Packed YUV 4:2:2, 16bpp, Y0 Cb Y1 Cr */
00606       sample_size = 2;  /* all data in first plane, probably */
00607       luv = 0;
00608       break;
00609    }
00610    len *= sample_size;
00611    
00612    p->data[0] = b->data;
00613    p->linesize[0] = len;
00614    /* these are only valid for component images */
00615    p->data[1] = luv ? b->data + 4*l4 : b->data+len;
00616    p->data[2] = luv ? b->data + 5*l4 : b->data+len;
00617    p->linesize[1] = luv;
00618    p->linesize[2] = luv;
00619    
00620    /* add the offsets to the pointers previously calculated, 
00621    it is necessary for the picture in picture mode */
00622    p->data[0] += len*b->win_y + b->win_x*sample_size;
00623    if (luv) { 
00624       p->data[1] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
00625       p->data[2] += luv*(b->win_y/2) + (b->win_x/2) * sample_size;
00626    }
00627    return p;
00628 }
00629 
00630 /*! convert/scale between an input and an output format.
00631  * Old version of ffmpeg only have img_convert, which does not rescale.
00632  * New versions use sws_scale which does both.
00633  */
00634 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
00635    struct fbuf_t *out, AVPicture *p_out)
00636 {
00637    AVPicture my_p_in, my_p_out;
00638    int eff_w=out->w, eff_h=out->h;
00639 
00640    if (p_in == NULL)
00641       p_in = fill_pict(in, &my_p_in);
00642    if (p_out == NULL)
00643       p_out = fill_pict(out, &my_p_out);
00644 
00645    /*if win_w is different from zero then we must change
00646    the size of the scaled buffer (the position is already
00647    encoded into the out parameter)*/
00648    if (out->win_w) { /* picture in picture enabled */
00649       eff_w=out->win_w;
00650       eff_h=out->win_h;
00651    }
00652 #ifdef OLD_FFMPEG
00653    /* XXX img_convert is deprecated, and does not do rescaling, PiP not supported */
00654    img_convert(p_out, out->pix_fmt,
00655       p_in, in->pix_fmt, in->w, in->h);
00656 #else /* XXX replacement */
00657    {
00658       struct SwsContext *convert_ctx;
00659 
00660       convert_ctx = sws_getContext(in->w, in->h, in->pix_fmt,
00661          eff_w, eff_h, out->pix_fmt,
00662          SWS_BICUBIC, NULL, NULL, NULL);
00663       if (convert_ctx == NULL) {
00664          ast_log(LOG_ERROR, "FFMPEG::convert_cmodel : swscale context initialization failed\n");
00665          return;
00666       }
00667       if (0)
00668          ast_log(LOG_WARNING, "in %d %dx%d out %d %dx%d\n",
00669             in->pix_fmt, in->w, in->h, out->pix_fmt, eff_w, eff_h);
00670       sws_scale(convert_ctx,
00671          p_in->data, p_in->linesize,
00672          in->w, in->h, /* src slice */
00673          p_out->data, p_out->linesize);
00674 
00675       sws_freeContext(convert_ctx);
00676    }
00677 #endif /* XXX replacement */
00678 }
00679 
00680 struct video_desc *get_video_desc(struct ast_channel *c);
00681 
00682 /*
00683  * This function is called (by asterisk) for each video packet
00684  * coming from the network (the 'in' path) that needs to be processed.
00685  * We need to reconstruct the entire video frame before we can decode it.
00686  * After a video packet is received we have to:
00687  * - extract the bitstream with pre_process_data()
00688  * - append the bitstream to a buffer
00689  * - if the fragment is the last (RTP Marker) we decode it with decode_video()
00690  * - after the decoding is completed we display the decoded frame with show_frame()
00691  */
00692 int console_write_video(struct ast_channel *chan, struct ast_frame *f);
00693 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
00694 {
00695    struct video_desc *env = get_video_desc(chan);
00696    struct video_dec_desc *v = env->in;
00697 
00698    if (!env->gui) /* no gui, no rendering */
00699       return 0;
00700    if (v == NULL)
00701       env->in = v = dec_init(f->subclass & ~1);
00702    if (v == NULL) {
00703       /* This is not fatal, but we won't have incoming video */
00704       ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
00705       return 0;
00706    }
00707 
00708    if (v->dec_in_cur == NULL) /* no buffer for incoming frames, drop */
00709       return 0;
00710 #if defined(DROP_PACKETS) && DROP_PACKETS > 0
00711    /* Simulate lost packets */
00712    if ((random() % 10000) <= 100*DROP_PACKETS) {
00713       ast_log(LOG_NOTICE, "Packet lost [%d]\n", f->seqno);
00714       return 0;
00715    }
00716 #endif
00717    if (v->discard) {
00718       /*
00719        * In discard mode, drop packets until we find one with
00720        * the RTP marker set (which is the end of frame).
00721        * Note that the RTP marker flag is sent as the LSB of the
00722        * subclass, which is a  bitmask of formats. The low bit is
00723        * normally used for audio so there is no interference.
00724        */
00725       if (f->subclass & 0x01) {
00726          v->dec_in_cur->used = 0;
00727          v->dec_in_cur->ebit = 0;
00728          v->next_seq = f->seqno + 1;   /* wrap at 16 bit */
00729          v->discard = 0;
00730          ast_log(LOG_WARNING, "out of discard mode, frame %d\n", f->seqno);
00731       }
00732       return 0;
00733    }
00734 
00735    /*
00736     * Only in-order fragments will be accepted. Remember seqno
00737     * has 16 bit so there is wraparound. Also, ideally we could
00738     * accept a bit of reordering, but at the moment we don't.
00739     */
00740    if (v->next_seq != f->seqno) {
00741       ast_log(LOG_WARNING, "discarding frame out of order, %d %d\n",
00742          v->next_seq, f->seqno);
00743       v->discard = 1;
00744       return 0;
00745    }
00746    v->next_seq++;
00747 
00748    if (f->data.ptr == NULL || f->datalen < 2) {
00749       ast_log(LOG_WARNING, "empty video frame, discard\n");
00750       return 0;
00751    }
00752    if (v->d_callbacks->dec_decap(v->dec_in_cur, f->data.ptr, f->datalen)) {
00753       ast_log(LOG_WARNING, "error in dec_decap, enter discard\n");
00754       v->discard = 1;
00755    }
00756    if (f->subclass & 0x01) {  // RTP Marker
00757       /* prepare to decode: advance the buffer so the video thread knows. */
00758       struct fbuf_t *tmp = v->dec_in_cur; /* store current pointer */
00759       ast_mutex_lock(&env->dec_lock);
00760       if (++v->dec_in_cur == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
00761          v->dec_in_cur = &v->dec_in[0];
00762       if (v->dec_in_dpy == NULL) {  /* were not displaying anything, so set it */
00763          v->dec_in_dpy = tmp;
00764       } else if (v->dec_in_dpy == v->dec_in_cur) { /* current slot is busy */
00765          v->dec_in_cur = NULL;
00766       }
00767       ast_mutex_unlock(&env->dec_lock);
00768    }
00769    return 0;
00770 }
00771 
00772 
00773 /*! \brief refreshes the buffers of all the device by calling the
00774  * grabber_read on each device in the device table.
00775  * it encodes the primary source buffer, if the picture in picture mode is
00776  * enabled it encodes (in the buffer to split) the secondary source buffer too.
00777  * The encoded buffer is splitted to build the local and the remote view.
00778  * Return a list of ast_frame representing the video fragments.
00779  * The head pointer is returned by the function, the tail pointer
00780  * is returned as an argument.
00781  *
00782  * \param env = video environment descriptor
00783  * \param tail = tail ponter (pratically a return value)
00784  */
00785 static struct ast_frame *get_video_frames(struct video_desc *env, struct ast_frame **tail)
00786 {
00787    struct video_out_desc *v = &env->out;
00788    struct ast_frame *dummy;
00789    struct fbuf_t *loc_src_primary = NULL, *p_read;
00790    int i;
00791    /* if no device was found in the config file */
00792    if (!env->out.device_num)
00793       return NULL;
00794    /* every time this function is called we refresh the buffers of every device,
00795    updating the private device buffer in the device table */
00796    for (i = 0; i < env->out.device_num; i++) {
00797       p_read = grabber_read(&env->out.devices[i], env->out.fps);
00798       /* it is used only if different from NULL, we mantain last good buffer otherwise */
00799       if (p_read)
00800          env->out.devices[i].dev_buf = p_read;
00801    }
00802    /* select the primary device buffer as the one to encode */
00803    loc_src_primary = env->out.devices[env->out.device_primary].dev_buf;
00804    /* loc_src_primary can be NULL if the device has been turned off during
00805    execution of it is read too early */
00806    if (loc_src_primary) {
00807       /* Scale the video for the encoder, then use it for local rendering
00808       so we will see the same as the remote party */
00809       my_scale(loc_src_primary, NULL, &env->enc_in, NULL);
00810    }
00811    if (env->out.picture_in_picture) { /* the picture in picture mode is enabled */
00812       struct fbuf_t *loc_src_secondary;
00813       /* reads from the secondary source */
00814       loc_src_secondary = env->out.devices[env->out.device_secondary].dev_buf;
00815       if (loc_src_secondary) {
00816          env->enc_in.win_x = env->out.pip_x;
00817          env->enc_in.win_y = env->out.pip_y;
00818          env->enc_in.win_w = env->enc_in.w/3;
00819          env->enc_in.win_h = env->enc_in.h/3;
00820          /* scales to the correct geometry and inserts in
00821          the enc_in buffer the picture in picture */
00822          my_scale(loc_src_secondary, NULL, &env->enc_in, NULL);
00823          /* returns to normal parameters (not picture in picture) */
00824          env->enc_in.win_x = 0;
00825          env->enc_in.win_y = 0;
00826          env->enc_in.win_w = 0;
00827          env->enc_in.win_h = 0;
00828       }
00829       else {
00830          /* loc_src_secondary can be NULL if the device has been turned off during
00831          execution of it is read too early */
00832          env->out.picture_in_picture = 0; /* disable picture in picture */
00833       }
00834    }
00835    show_frame(env, WIN_LOCAL); /* local rendering */
00836    for (i = 0; i < env->out.device_num; i++) 
00837       show_frame(env, i+WIN_SRC1); /* rendering of every source device in thumbnails */
00838    if (tail == NULL)
00839       tail = &dummy;
00840    *tail = NULL;
00841    /* if no reason for encoding, do not encode */
00842    if (!env->owner || !loc_src_primary || !v->sendvideo)
00843       return NULL;
00844    if (v->enc_out.data == NULL) {
00845       static volatile int a = 0;
00846       if (a++ < 2)
00847          ast_log(LOG_WARNING, "fail, no encoder output buffer\n");
00848       return NULL;
00849    }
00850    v->enc->enc_run(v);
00851    return v->enc->enc_encap(&v->enc_out, v->mtu, tail);
00852 }
00853 
00854 /*
00855  * Helper thread to periodically poll the video sources and enqueue the
00856  * generated frames directed to the remote party to the channel's queue.
00857  * Using a separate thread also helps because the encoding can be
00858  * computationally expensive so we don't want to starve the main thread.
00859  */
00860 static void *video_thread(void *arg)
00861 {
00862    struct video_desc *env = arg;
00863    int count = 0;
00864    char save_display[128] = "";
00865    int i; /* integer variable used as iterator */
00866 
00867    /* if sdl_videodriver is set, override the environment. Also,
00868     * if it contains 'console' override DISPLAY around the call to SDL_Init
00869     * so we use the console as opposed to the x11 version of aalib
00870     */
00871    if (!ast_strlen_zero(env->sdl_videodriver)) { /* override */
00872       const char *s = getenv("DISPLAY");
00873       setenv("SDL_VIDEODRIVER", env->sdl_videodriver, 1);
00874       if (s && !strcasecmp(env->sdl_videodriver, "aalib-console")) {
00875          ast_copy_string(save_display, s, sizeof(save_display));
00876          unsetenv("DISPLAY");
00877       }
00878    }
00879    sdl_setup(env);
00880    if (!ast_strlen_zero(save_display)) {
00881       setenv("DISPLAY", save_display, 1);
00882    }
00883 
00884    ast_mutex_init(&env->dec_lock);  /* used to sync decoder and renderer */
00885 
00886    if (grabber_open(&env->out)) {
00887       ast_log(LOG_WARNING, "cannot open local video source\n");
00888    }
00889 
00890    if (env->out.device_num) {
00891       env->out.devices[env->out.device_primary].status_index |= IS_PRIMARY | IS_SECONDARY;
00892    }
00893 
00894    /* even if no device is connected, we must call video_out_init,
00895     * as some of the data structures it initializes are
00896     * used in get_video_frames()
00897     */
00898    video_out_init(env);
00899 
00900    /* Writes intial status of the sources. */
00901    if (env->gui) {
00902       for (i = 0; i < env->out.device_num; i++) {
00903          print_message(env->gui->thumb_bd_array[i].board,
00904             src_msgs[env->out.devices[i].status_index]);
00905       }
00906    }
00907 
00908    for (;;) {
00909       struct timespec t = { 0, 50000000 };   /* XXX 20 times/sec */
00910       struct ast_frame *p, *f;
00911       struct ast_channel *chan;
00912       int fd;
00913       char *caption = NULL, buf[160];
00914 
00915       /* determine if video format changed */
00916       if (count++ % 10 == 0) {
00917          if (env->out.sendvideo && env->out.devices) {
00918             snprintf(buf, sizeof(buf), "%s %s %dx%d @@ %dfps %dkbps",
00919             env->out.devices[env->out.device_primary].name, env->codec_name,
00920             env->enc_in.w, env->enc_in.h,
00921             env->out.fps, env->out.bitrate / 1000);
00922          } else {
00923             sprintf(buf, "hold");
00924          }
00925          caption = buf;
00926       }
00927 
00928       /* manage keypad events */
00929       /* XXX here we should always check for events,
00930       * otherwise the drag will not work */ 
00931       if (env->gui)
00932          eventhandler(env, caption);
00933 
00934       /* sleep for a while */
00935       nanosleep(&t, NULL);
00936 
00937        if (env->in) {
00938          struct video_dec_desc *v = env->in;
00939 
00940          /*
00941           * While there is something to display, call the decoder and free
00942           * the buffer, possibly enabling the receiver to store new data.
00943           */
00944          while (v->dec_in_dpy) {
00945             struct fbuf_t *tmp = v->dec_in_dpy; /* store current pointer */
00946 
00947             /* decode the frame, but show it only if not frozen */
00948             if (v->d_callbacks->dec_run(v, tmp) && !env->frame_freeze)
00949                show_frame(env, WIN_REMOTE);
00950             tmp->used = 0; /* mark buffer as free */
00951             tmp->ebit = 0;
00952             ast_mutex_lock(&env->dec_lock);
00953             if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN]) /* advance to next, circular */
00954                v->dec_in_dpy = &v->dec_in[0];
00955 
00956             if (v->dec_in_cur == NULL) /* receiver was idle, enable it... */
00957                v->dec_in_cur = tmp; /* using the slot just freed */
00958             else if (v->dec_in_dpy == v->dec_in_cur) /* this was the last slot */
00959                v->dec_in_dpy = NULL;   /* nothing more to display */
00960             ast_mutex_unlock(&env->dec_lock);
00961          }
00962       }
00963 
00964       if (env->shutdown)
00965          break;
00966       f = get_video_frames(env, &p);   /* read and display */
00967       if (!f)
00968          continue;
00969       chan = env->owner;
00970       if (chan == NULL) {
00971          /* drop the chain of frames, nobody uses them */
00972          while (f) {
00973             struct ast_frame *g = AST_LIST_NEXT(f, frame_list);
00974             ast_frfree(f);
00975             f = g;
00976          }
00977          continue;
00978       }
00979       ast_channel_lock(chan);
00980 
00981       /* AST_LIST_INSERT_TAIL is only good for one frame, cannot use here */
00982       if (ast_channel_readq(chan).first == NULL) {
00983          ast_channel_readq(chan).first = f;
00984       } else {
00985          ast_channel_readq(chan).last->frame_list.next = f;
00986       }
00987       ast_channel_readq(chan).last = p;
00988       /*
00989        * more or less same as ast_queue_frame, but extra
00990        * write on the alertpipe to signal frames.
00991        */
00992       if (ast_channel_alertable(chan)) {
00993          for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
00994             if (ast_channel_alert(chan)) {
00995                ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
00996                   ast_channel_name(chan), f->frametype, f->subclass, strerror(errno));
00997          }
00998       }
00999       ast_channel_unlock(chan);
01000    }
01001    /* thread terminating, here could call the uninit */
01002    /* uninitialize the local and remote video environments */
01003    env->in = dec_uninit(env->in);
01004    video_out_uninit(env);
01005 
01006    if (env->gui)
01007       env->gui = cleanup_sdl(env->gui, env->out.device_num);
01008    ast_mutex_destroy(&env->dec_lock);
01009    env->shutdown = 0;
01010    return NULL;
01011 }
01012 
01013 static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
01014 {
01015    if (dst->w == 0)
01016       dst->w = src->w;
01017    if (dst->h == 0)
01018       dst->h = src->h;
01019 }
01020 
01021 /*! initialize the video environment.
01022  * Apart from the formats (constant) used by sdl and the codec,
01023  * we use enc_in as the basic geometry.
01024  */
01025 static void init_env(struct video_desc *env)
01026 {
01027    struct fbuf_t *c = &(env->out.loc_src_geometry);      /* local source */
01028    struct fbuf_t *ei = &(env->enc_in);    /* encoder input */
01029    struct fbuf_t *ld = &(env->loc_dpy);   /* local display */
01030    struct fbuf_t *rd = &(env->rem_dpy);      /* remote display */
01031    int i; /* integer working as iterator */
01032 
01033    c->pix_fmt = PIX_FMT_YUV420P; /* default - camera format */
01034    ei->pix_fmt = PIX_FMT_YUV420P;   /* encoder input */
01035    if (ei->w == 0 || ei->h == 0) {
01036       ei->w = 352;
01037       ei->h = 288;
01038    }
01039    ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P; /* sdl format */
01040    /* inherit defaults */
01041    copy_geometry(ei, c);   /* camera inherits from encoder input */
01042    copy_geometry(ei, rd);  /* remote display inherits from encoder input */
01043    copy_geometry(rd, ld);  /* local display inherits from remote display */
01044 
01045    /* fix the size of buffers for small windows */
01046    for (i = 0; i < env->out.device_num; i++) {
01047       env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
01048       env->src_dpy[i].w = SRC_WIN_W;
01049       env->src_dpy[i].h = SRC_WIN_H;
01050    }
01051    /* now we set the default coordinates for the picture in picture
01052    frames inside the env_in buffers, those can be changed by dragging the
01053    picture in picture with left click */
01054    env->out.pip_x = ei->w - ei->w/3;
01055    env->out.pip_y = ei->h - ei->h/3;
01056 }
01057 
01058 /*!
01059  * The first call to the video code, called by oss_new() or similar.
01060  * Here we initialize the various components we use, namely SDL for display,
01061  * ffmpeg for encoding/decoding, and a local video source.
01062  * We do our best to progress even if some of the components are not
01063  * available.
01064  */
01065 void console_video_start(struct video_desc *env, struct ast_channel *owner)
01066 {
01067    ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
01068    if (env == NULL)  /* video not initialized */
01069       return;
01070    env->owner = owner;  /* work even if no owner is specified */
01071    if (env->vthread)
01072       return;     /* already initialized, nothing to do */
01073    init_env(env);
01074    env->out.enc = map_config_video_format(env->codec_name);
01075 
01076    ast_log(LOG_WARNING, "start video out %s %dx%d\n",
01077       env->codec_name, env->enc_in.w,  env->enc_in.h);
01078    /*
01079     * Register all codecs supported by the ffmpeg library.
01080     * We only need to do it once, but probably doesn't
01081     * harm to do it multiple times.
01082     */
01083    avcodec_init();
01084    avcodec_register_all();
01085    av_log_set_level(AV_LOG_ERROR);  /* only report errors */
01086 
01087    if (env->out.fps == 0) {
01088       env->out.fps = 15;
01089       ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
01090    }
01091    if (env->out.bitrate == 0) {
01092       env->out.bitrate = 65000;
01093       ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
01094    }
01095    /* create the thread as detached so memory is freed on termination */
01096    ast_pthread_create_detached_background(&env->vthread,
01097       NULL, video_thread, env);
01098 }
01099 
01100 /*
01101  * Parse a geometry string, accepting also common names for the formats.
01102  * Trick: if we have a leading > or < and a numeric geometry,
01103  * return the larger or smaller one.
01104  * E.g. <352x288 gives the smaller one, 320x240
01105  */
01106 static int video_geom(struct fbuf_t *b, const char *s)
01107 {
01108    int w = 0, h = 0;
01109 
01110    static struct {
01111       const char *s; int w; int h;
01112    } *fp, formats[] = {
01113       {"16cif",   1408, 1152 },
01114       {"xga",     1024, 768 },
01115       {"4cif", 704, 576 },
01116       {"vga",     640, 480 },
01117       {"cif",     352, 288 },
01118       {"qvga", 320, 240 },
01119       {"qcif", 176, 144 },
01120       {"sqcif",   128, 96 },
01121       {NULL,      0, 0 },
01122    };
01123    if (*s == '<' || *s == '>')
01124       sscanf(s+1,"%dx%d", &w, &h);
01125    for (fp = formats; fp->s; fp++) {
01126       if (*s == '>') {  /* look for a larger one */
01127          if (fp->w <= w) {
01128             if (fp > formats)
01129                fp--; /* back one step if possible */
01130             break;
01131          }
01132       } else if (*s == '<') { /* look for a smaller one */
01133          if (fp->w < w)
01134             break;
01135       } else if (!strcasecmp(s, fp->s)) { /* look for a string */
01136          break;
01137       }
01138    }
01139    if (*s == '<' && fp->s == NULL)  /* smallest */
01140       fp--;
01141    if (fp->s) {
01142       b->w = fp->w;
01143       b->h = fp->h;
01144    } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
01145       ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
01146       b->w = 352;
01147       b->h = 288;
01148    }
01149    return 0;
01150 }
01151 
01152 
01153 /*! \brief add an entry to the video_device table,
01154  * ignoring duplicate names.
01155  * The table is a static array of 9 elements.
01156  * The last_frame field of each entry of the table is initialized to
01157  * the current time (we need a value inside this field, on stop of the
01158  * GUI the last_frame value is not changed, to avoid checking if it is 0 we
01159  * set the initial value on current time) XXX
01160  *
01161  * PARAMETERS:
01162  * \param devices_p = pointer to the table of devices
01163  * \param device_num_p = pointer to the number of devices
01164  * \param s = name of the new device to insert
01165  *
01166  * returns 0 on success, 1 on error
01167  */
01168 static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
01169 {
01170    int i;
01171    struct video_device *p;
01172 
01173    /* with the current implementation, we support a maximum of 9 devices.*/
01174    if (*device_num_p >= 9)
01175       return 0; /* more devices will be ignored */
01176    /* ignore duplicate names */
01177    for (i = 0; i < *device_num_p; i++) {
01178       if (!strcmp(devices[i].name, s))
01179          return 0;
01180    }
01181    /* inserts the new video device */
01182    p = &devices[*device_num_p];
01183    /* XXX the string is allocated but NEVER deallocated,
01184    the good time to do that is when the module is unloaded, now we skip the problem */
01185    p->name = ast_strdup(s);      /* copy the name */
01186    /* other fields initially NULL */
01187    p->grabber = NULL;
01188    p->grabber_data = NULL;
01189    p->dev_buf = NULL;
01190    p->last_frame = ast_tvnow();
01191    p->status_index = 0;
01192    (*device_num_p)++;         /* one device added */
01193    return 0;
01194 }
01195 
01196 /* extend ast_cli with video commands. Called by console_video_config */
01197 int console_video_cli(struct video_desc *env, const char *var, int fd)
01198 {
01199    if (env == NULL)
01200       return 1;   /* unrecognised */
01201 
01202    if (!strcasecmp(var, "videodevice")) {
01203       ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
01204    } else if (!strcasecmp(var, "videocodec")) {
01205       ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
01206    } else if (!strcasecmp(var, "sendvideo")) {
01207       ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
01208    } else if (!strcasecmp(var, "video_size")) {
01209       int in_w = 0, in_h = 0;
01210       if (env->in) {
01211          in_w = env->in->dec_out.w;
01212          in_h = env->in->dec_out.h;
01213       }
01214       ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
01215          env->enc_in.w, env->enc_in.h,
01216          env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
01217          env->loc_dpy.w, env->loc_dpy.h,
01218          env->rem_dpy.w, env->rem_dpy.h,
01219          in_w, in_h);
01220    } else if (!strcasecmp(var, "bitrate")) {
01221       ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
01222    } else if (!strcasecmp(var, "qmin")) {
01223       ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
01224    } else if (!strcasecmp(var, "fps")) {
01225       ast_cli(fd, "fps is [%d]\n", env->out.fps);
01226    } else if (!strcasecmp(var, "startgui")) {
01227       env->stayopen = 1;
01228       console_video_start(env, NULL);
01229    } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
01230       env->stayopen = 0;
01231       if (env->gui && env->owner)
01232          ast_cli_command(-1, "console hangup");
01233       else /* not in a call */
01234          console_video_uninit(env);
01235    } else {
01236       return 1;   /* unrecognised */
01237    }
01238    return 0;   /* recognised */
01239 }
01240 
01241 /*! parse config command for video support. */
01242 int console_video_config(struct video_desc **penv,
01243    const char *var, const char *val)
01244 {
01245    struct video_desc *env;
01246 
01247    if (penv == NULL) {
01248       ast_log(LOG_WARNING, "bad argument penv=NULL\n");
01249       return 1;   /* error */
01250    }
01251    /* allocate the video descriptor first time we get here */
01252    env = *penv;
01253    if (env == NULL) {
01254       env = *penv = ast_calloc(1, sizeof(struct video_desc));
01255       if (env == NULL) {
01256          ast_log(LOG_WARNING, "fail to allocate video_desc\n");
01257          return 1;   /* error */
01258       
01259       }
01260       /* set default values - 0's are already there */
01261       env->out.device_primary = 0;
01262       env->out.device_secondary = 0;
01263       env->out.fps = 5;
01264       env->out.bitrate = 65000;
01265       env->out.sendvideo = 1;
01266       env->out.qmin = 3;
01267       env->out.device_num = 0;
01268    }
01269    CV_START(var, val);
01270    CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
01271    CV_BOOL("sendvideo", env->out.sendvideo);
01272    CV_F("video_size", video_geom(&env->enc_in, val));
01273    CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
01274    CV_F("local_size", video_geom(&env->loc_dpy, val));
01275    CV_F("remote_size", video_geom(&env->rem_dpy, val));
01276    CV_STR("keypad", env->keypad_file);
01277    CV_F("region", keypad_cfg_read(env->gui, val));
01278    CV_UINT("startgui", env->stayopen); /* enable gui at startup */
01279    CV_STR("keypad_font", env->keypad_font);
01280    CV_STR("sdl_videodriver", env->sdl_videodriver);
01281    CV_UINT("fps", env->out.fps);
01282    CV_UINT("bitrate", env->out.bitrate);
01283    CV_UINT("qmin", env->out.qmin);
01284    CV_STR("videocodec", env->codec_name);
01285    return 1;   /* nothing found */
01286 
01287    CV_END;     /* the 'nothing found' case */
01288    return 0;      /* found something */
01289 }
01290 
01291 #endif   /* video support */

Generated on Thu Oct 11 06:33:45 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.5.6