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

Generated on Sat Mar 10 01:54:15 2012 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7