00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include "asterisk.h"
00040 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 147811 $")
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
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114 #if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG)
00115
00116 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
00117 {
00118 return 0;
00119 }
00120
00121 int console_video_cli(struct video_desc *env, const char *var, int fd)
00122 {
00123 return 1;
00124 }
00125
00126 int console_video_config(struct video_desc **penv, const char *var, const char *val)
00127 {
00128 return 1;
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;
00143 }
00144
00145 int console_video_formats = 0;
00146
00147 #else
00148
00149
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
00157 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
00158 struct fbuf_t *out, AVPicture *p_out);
00159
00160
00161
00162
00163
00164
00165 struct video_device {
00166 char *name;
00167
00168 struct grab_desc *grabber;
00169 void *grabber_data;
00170 struct fbuf_t *dev_buf;
00171 struct timeval last_frame;
00172 int status_index;
00173
00174
00175 };
00176
00177 struct video_codec_desc;
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191 struct video_out_desc {
00192
00193
00194
00195
00196
00197
00198 int fps;
00199 int bitrate;
00200 int qmin;
00201
00202 int sendvideo;
00203
00204 struct fbuf_t loc_src_geometry;
00205 struct fbuf_t enc_out;
00206
00207 struct video_codec_desc *enc;
00208 void *enc_ctx;
00209 AVCodec *codec;
00210 AVFrame *enc_in_frame;
00211
00212 int mtu;
00213
00214
00215
00216
00217 struct video_device devices[MAX_VIDEO_SOURCES];
00218 int device_num;
00219 int device_primary;
00220 int device_secondary;
00221
00222 int picture_in_picture;
00223
00224
00225
00226 int pip_x;
00227 int pip_y;
00228 };
00229
00230
00231
00232
00233
00234
00235
00236 struct video_desc {
00237 char codec_name[64];
00238
00239 int stayopen;
00240 pthread_t vthread;
00241 ast_mutex_t dec_lock;
00242 int shutdown;
00243 struct ast_channel *owner;
00244
00245
00246 struct fbuf_t enc_in;
00247
00248 char keypad_file[256];
00249 char keypad_font[256];
00250
00251 char sdl_videodriver[256];
00252
00253 struct fbuf_t rem_dpy;
00254 struct fbuf_t loc_dpy;
00255
00256
00257 struct fbuf_t src_dpy[MAX_VIDEO_SOURCES];
00258
00259 int frame_freeze;
00260
00261
00262 struct gui_info *gui;
00263 struct video_dec_desc *in;
00264 struct video_out_desc out;
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
00277 b->w = x.w;
00278 b->h = x.h;
00279 b->pix_fmt = x.pix_fmt;
00280 }
00281
00282
00283
00284
00285 int get_gui_startup(struct video_desc* env)
00286 {
00287 return env ? env->stayopen : 0;
00288 }
00289
00290 #if 0
00291
00292
00293
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
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
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
00330 for (i = 0; i < v->device_num; i++) {
00331
00332 if (v->devices[i].grabber)
00333 continue;
00334
00335 for (j = 0; (g = console_grabbers[j]); j++) {
00336
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
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;
00352 }
00353 return 1;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
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)
00372 return NULL;
00373
00374
00375
00376
00377
00378 if (ast_tvdiff_ms(now, dev->last_frame) < 1000/fps)
00379 return NULL;
00380 dev->last_frame = now;
00381 return dev->grabber->read(dev->grabber_data);
00382 }
00383
00384
00385
00386
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
00397
00398 static struct video_codec_desc *map_config_video_format(char *name)
00399 {
00400 int i;
00401
00402 for (i = 0; supported_codecs[i]; i++)
00403 if (!strcasecmp(name, supported_codecs[i]->name))
00404 break;
00405 if (supported_codecs[i] == NULL) {
00406 ast_log(LOG_WARNING, "Cannot find codec for '%s'\n", name);
00407 i = 0;
00408 strcpy(name, supported_codecs[i]->name);
00409 }
00410 ast_log(LOG_WARNING, "Using codec '%s'\n", name);
00411 return supported_codecs[i];
00412 }
00413
00414
00415
00416 static int video_out_uninit(struct video_desc *env)
00417 {
00418 struct video_out_desc *v = &env->out;
00419 int i;
00420
00421
00422 if (v->enc_ctx) {
00423 AVCodecContext *enc_ctx = (AVCodecContext *)v->enc_ctx;
00424 avcodec_close(enc_ctx);
00425 av_free(enc_ctx);
00426 v->enc_ctx = NULL;
00427 }
00428 if (v->enc_in_frame) {
00429 av_free(v->enc_in_frame);
00430 v->enc_in_frame = NULL;
00431 }
00432 v->codec = NULL;
00433
00434 fbuf_free(&env->enc_in);
00435 fbuf_free(&v->enc_out);
00436
00437 for (i = 0; i < v->device_num; i++) {
00438 if (v->devices[i].grabber){
00439 v->devices[i].grabber_data =
00440 v->devices[i].grabber->close(v->devices[i].grabber_data);
00441 v->devices[i].grabber = NULL;
00442
00443 v->devices[i].dev_buf = NULL;
00444 }
00445 v->devices[i].status_index = 0;
00446 }
00447 v->picture_in_picture = 0;
00448 env->frame_freeze = 0;
00449 return -1;
00450 }
00451
00452
00453
00454
00455
00456
00457
00458
00459 static int video_out_init(struct video_desc *env)
00460 {
00461 int codec;
00462 int size;
00463 struct fbuf_t *enc_in;
00464 struct video_out_desc *v = &env->out;
00465
00466 v->enc_ctx = NULL;
00467 v->codec = NULL;
00468 v->enc_in_frame = NULL;
00469 v->enc_out.data = NULL;
00470
00471 codec = map_video_format(v->enc->format, CM_WR);
00472 v->codec = avcodec_find_encoder(codec);
00473 if (!v->codec) {
00474 ast_log(LOG_WARNING, "Cannot find the encoder for format %d\n",
00475 codec);
00476 return -1;
00477 }
00478
00479 v->mtu = 1400;
00480
00481
00482
00483
00484 enc_in = &env->enc_in;
00485 enc_in->pix_fmt = PIX_FMT_YUV420P;
00486 enc_in->size = (enc_in->w * enc_in->h * 3)/2;
00487 enc_in->data = ast_calloc(1, enc_in->size);
00488 if (!enc_in->data) {
00489 ast_log(LOG_WARNING, "Cannot allocate encoder input buffer\n");
00490 return video_out_uninit(env);
00491 }
00492
00493 v->enc_in_frame = avcodec_alloc_frame();
00494 if (!v->enc_in_frame) {
00495 ast_log(LOG_WARNING, "Unable to allocate the encoding video frame\n");
00496 return video_out_uninit(env);
00497 }
00498
00499
00500 size = enc_in->w * enc_in->h;
00501 v->enc_in_frame->data[0] = enc_in->data;
00502 v->enc_in_frame->data[1] = v->enc_in_frame->data[0] + size;
00503 v->enc_in_frame->data[2] = v->enc_in_frame->data[1] + size/4;
00504 v->enc_in_frame->linesize[0] = enc_in->w;
00505 v->enc_in_frame->linesize[1] = enc_in->w/2;
00506 v->enc_in_frame->linesize[2] = enc_in->w/2;
00507
00508
00509
00510
00511 {
00512 AVCodecContext *enc_ctx = avcodec_alloc_context();
00513 v->enc_ctx = enc_ctx;
00514 enc_ctx->pix_fmt = enc_in->pix_fmt;
00515 enc_ctx->width = enc_in->w;
00516 enc_ctx->height = enc_in->h;
00517
00518
00519
00520 enc_ctx->rtp_mode = 1;
00521 enc_ctx->rtp_payload_size = v->mtu / 2;
00522 enc_ctx->bit_rate = v->bitrate;
00523 enc_ctx->bit_rate_tolerance = enc_ctx->bit_rate/2;
00524 enc_ctx->qmin = v->qmin;
00525 enc_ctx->time_base = (AVRational){1, v->fps};
00526 enc_ctx->gop_size = v->fps*5;
00527
00528 v->enc->enc_init(v->enc_ctx);
00529
00530 if (avcodec_open(enc_ctx, v->codec) < 0) {
00531 ast_log(LOG_WARNING, "Unable to initialize the encoder %d\n",
00532 codec);
00533 av_free(enc_ctx);
00534 v->enc_ctx = NULL;
00535 return video_out_uninit(env);
00536 }
00537 }
00538
00539
00540
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
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559 void console_video_uninit(struct video_desc *env)
00560 {
00561 int i, t = 100;
00562 if (env->stayopen == 0) {
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;
00575 }
00576
00577
00578
00579
00580
00581
00582 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
00583 {
00584
00585 int l4 = b->w * b->h/4;
00586 int len = b->w;
00587 int luv = b->w/2;
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:
00602 sample_size = 2;
00603 luv = 0;
00604 break;
00605 }
00606 len *= sample_size;
00607
00608 p->data[0] = b->data;
00609 p->linesize[0] = len;
00610
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
00617
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
00627
00628
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
00642
00643
00644 if (out->win_w) {
00645 eff_w=out->win_w;
00646 eff_h=out->win_h;
00647 }
00648 #ifdef OLD_FFMPEG
00649
00650 img_convert(p_out, out->pix_fmt,
00651 p_in, in->pix_fmt, in->w, in->h);
00652 #else
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,
00669 p_out->data, p_out->linesize);
00670
00671 sws_freeContext(convert_ctx);
00672 }
00673 #endif
00674 }
00675
00676 struct video_desc *get_video_desc(struct ast_channel *c);
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
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)
00695 return 0;
00696 if (v == NULL)
00697 env->in = v = dec_init(f->subclass & ~1);
00698 if (v == NULL) {
00699
00700 ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
00701 return 0;
00702 }
00703
00704 if (v->dec_in_cur == NULL)
00705 return 0;
00706 #if defined(DROP_PACKETS) && DROP_PACKETS > 0
00707
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
00716
00717
00718
00719
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;
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
00733
00734
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) {
00753
00754 struct fbuf_t *tmp = v->dec_in_cur;
00755 ast_mutex_lock(&env->dec_lock);
00756 if (++v->dec_in_cur == &v->dec_in[N_DEC_IN])
00757 v->dec_in_cur = &v->dec_in[0];
00758 if (v->dec_in_dpy == NULL) {
00759 v->dec_in_dpy = tmp;
00760 } else if (v->dec_in_dpy == v->dec_in_cur) {
00761 v->dec_in_cur = NULL;
00762 }
00763 ast_mutex_unlock(&env->dec_lock);
00764 }
00765 return 0;
00766 }
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
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
00788 if (!env->out.device_num)
00789 return NULL;
00790
00791
00792 for (i = 0; i < env->out.device_num; i++) {
00793 p_read = grabber_read(&env->out.devices[i], env->out.fps);
00794
00795 if (p_read)
00796 env->out.devices[i].dev_buf = p_read;
00797 }
00798
00799 loc_src_primary = env->out.devices[env->out.device_primary].dev_buf;
00800
00801
00802 if (loc_src_primary) {
00803
00804
00805 my_scale(loc_src_primary, NULL, &env->enc_in, NULL);
00806 }
00807 if (env->out.picture_in_picture) {
00808 struct fbuf_t *loc_src_secondary;
00809
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
00817
00818 my_scale(loc_src_secondary, NULL, &env->enc_in, NULL);
00819
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
00827
00828 env->out.picture_in_picture = 0;
00829 }
00830 }
00831 show_frame(env, WIN_LOCAL);
00832 for (i = 0; i < env->out.device_num; i++)
00833 show_frame(env, i+WIN_SRC1);
00834 if (tail == NULL)
00835 tail = &dummy;
00836 *tail = NULL;
00837
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
00852
00853
00854
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;
00862
00863
00864
00865
00866
00867 if (!ast_strlen_zero(env->sdl_videodriver)) {
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 ast_mutex_init(&env->dec_lock);
00880
00881 if (grabber_open(&env->out)) {
00882 ast_log(LOG_WARNING, "cannot open local video source\n");
00883 }
00884
00885 if (env->out.device_num)
00886 env->out.devices[env->out.device_primary].status_index |= IS_PRIMARY | IS_SECONDARY;
00887
00888
00889
00890
00891
00892 video_out_init(env);
00893
00894
00895 if (env->gui) {
00896 for (i = 0; i < env->out.device_num; i++) {
00897 print_message(env->gui->thumb_bd_array[i].board,
00898 src_msgs[env->out.devices[i].status_index]);
00899 }
00900 }
00901
00902 for (;;) {
00903 struct timeval t = { 0, 50000 };
00904 struct ast_frame *p, *f;
00905 struct ast_channel *chan;
00906 int fd;
00907 char *caption = NULL, buf[160];
00908
00909
00910 if (count++ % 10 == 0) {
00911 if (env->out.sendvideo && env->out.devices)
00912 sprintf(buf, "%s %s %dx%d @@ %dfps %dkbps",
00913 env->out.devices[env->out.device_primary].name, env->codec_name,
00914 env->enc_in.w, env->enc_in.h,
00915 env->out.fps, env->out.bitrate/1000);
00916 else
00917 sprintf(buf, "hold");
00918 caption = buf;
00919 }
00920
00921
00922
00923
00924 if (env->gui)
00925 eventhandler(env, caption);
00926
00927
00928 ast_select(0, NULL, NULL, NULL, &t);
00929
00930 if (env->in) {
00931 struct video_dec_desc *v = env->in;
00932
00933
00934
00935
00936
00937 while (v->dec_in_dpy) {
00938 struct fbuf_t *tmp = v->dec_in_dpy;
00939
00940
00941 if (v->d_callbacks->dec_run(v, tmp) && !env->frame_freeze)
00942 show_frame(env, WIN_REMOTE);
00943 tmp->used = 0;
00944 tmp->ebit = 0;
00945 ast_mutex_lock(&env->dec_lock);
00946 if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN])
00947 v->dec_in_dpy = &v->dec_in[0];
00948
00949 if (v->dec_in_cur == NULL)
00950 v->dec_in_cur = tmp;
00951 else if (v->dec_in_dpy == v->dec_in_cur)
00952 v->dec_in_dpy = NULL;
00953 ast_mutex_unlock(&env->dec_lock);
00954 }
00955 }
00956
00957 if (env->shutdown)
00958 break;
00959 f = get_video_frames(env, &p);
00960 if (!f)
00961 continue;
00962 chan = env->owner;
00963 if (chan == NULL) {
00964
00965 while (f) {
00966 struct ast_frame *g = AST_LIST_NEXT(f, frame_list);
00967 ast_frfree(f);
00968 f = g;
00969 }
00970 continue;
00971 }
00972 fd = chan->alertpipe[1];
00973 ast_channel_lock(chan);
00974
00975
00976 if (chan->readq.first == NULL) {
00977 chan->readq.first = f;
00978 } else {
00979 chan->readq.last->frame_list.next = f;
00980 }
00981 chan->readq.last = p;
00982
00983
00984
00985
00986 if (fd > -1) {
00987 int blah = 1, l = sizeof(blah);
00988 for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
00989 if (write(fd, &blah, l) != l)
00990 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
00991 chan->name, f->frametype, f->subclass, strerror(errno));
00992 }
00993 }
00994 ast_channel_unlock(chan);
00995 }
00996
00997
00998 env->in = dec_uninit(env->in);
00999 video_out_uninit(env);
01000
01001 if (env->gui)
01002 env->gui = cleanup_sdl(env->gui, env->out.device_num);
01003 ast_mutex_destroy(&env->dec_lock);
01004 env->shutdown = 0;
01005 return NULL;
01006 }
01007
01008 static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
01009 {
01010 if (dst->w == 0)
01011 dst->w = src->w;
01012 if (dst->h == 0)
01013 dst->h = src->h;
01014 }
01015
01016
01017
01018
01019
01020 static void init_env(struct video_desc *env)
01021 {
01022 struct fbuf_t *c = &(env->out.loc_src_geometry);
01023 struct fbuf_t *ei = &(env->enc_in);
01024 struct fbuf_t *ld = &(env->loc_dpy);
01025 struct fbuf_t *rd = &(env->rem_dpy);
01026 int i;
01027
01028 c->pix_fmt = PIX_FMT_YUV420P;
01029 ei->pix_fmt = PIX_FMT_YUV420P;
01030 if (ei->w == 0 || ei->h == 0) {
01031 ei->w = 352;
01032 ei->h = 288;
01033 }
01034 ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P;
01035
01036 copy_geometry(ei, c);
01037 copy_geometry(ei, rd);
01038 copy_geometry(rd, ld);
01039
01040
01041 for (i = 0; i < env->out.device_num; i++) {
01042 env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
01043 env->src_dpy[i].w = SRC_WIN_W;
01044 env->src_dpy[i].h = SRC_WIN_H;
01045 }
01046
01047
01048
01049 env->out.pip_x = ei->w - ei->w/3;
01050 env->out.pip_y = ei->h - ei->h/3;
01051 }
01052
01053
01054
01055
01056
01057
01058
01059
01060 void console_video_start(struct video_desc *env, struct ast_channel *owner)
01061 {
01062 ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
01063 if (env == NULL)
01064 return;
01065 env->owner = owner;
01066 if (env->vthread)
01067 return;
01068 init_env(env);
01069 env->out.enc = map_config_video_format(env->codec_name);
01070
01071 ast_log(LOG_WARNING, "start video out %s %dx%d\n",
01072 env->codec_name, env->enc_in.w, env->enc_in.h);
01073
01074
01075
01076
01077
01078 avcodec_init();
01079 avcodec_register_all();
01080 av_log_set_level(AV_LOG_ERROR);
01081
01082 if (env->out.fps == 0) {
01083 env->out.fps = 15;
01084 ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
01085 }
01086 if (env->out.bitrate == 0) {
01087 env->out.bitrate = 65000;
01088 ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
01089 }
01090
01091 ast_pthread_create_detached_background(&env->vthread,
01092 NULL, video_thread, env);
01093 }
01094
01095
01096
01097
01098
01099
01100
01101 static int video_geom(struct fbuf_t *b, const char *s)
01102 {
01103 int w = 0, h = 0;
01104
01105 static struct {
01106 const char *s; int w; int h;
01107 } *fp, formats[] = {
01108 {"16cif", 1408, 1152 },
01109 {"xga", 1024, 768 },
01110 {"4cif", 704, 576 },
01111 {"vga", 640, 480 },
01112 {"cif", 352, 288 },
01113 {"qvga", 320, 240 },
01114 {"qcif", 176, 144 },
01115 {"sqcif", 128, 96 },
01116 {NULL, 0, 0 },
01117 };
01118 if (*s == '<' || *s == '>')
01119 sscanf(s+1,"%dx%d", &w, &h);
01120 for (fp = formats; fp->s; fp++) {
01121 if (*s == '>') {
01122 if (fp->w <= w) {
01123 if (fp > formats)
01124 fp--;
01125 break;
01126 }
01127 } else if (*s == '<') {
01128 if (fp->w < w)
01129 break;
01130 } else if (!strcasecmp(s, fp->s)) {
01131 break;
01132 }
01133 }
01134 if (*s == '<' && fp->s == NULL)
01135 fp--;
01136 if (fp->s) {
01137 b->w = fp->w;
01138 b->h = fp->h;
01139 } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
01140 ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
01141 b->w = 352;
01142 b->h = 288;
01143 }
01144 return 0;
01145 }
01146
01147
01148
01149
01150
01151
01152
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163 static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
01164 {
01165 int i;
01166 struct video_device *p;
01167
01168
01169 if (*device_num_p >= 9)
01170 return 0;
01171
01172 for (i = 0; i < *device_num_p; i++) {
01173 if (!strcmp(devices[i].name, s))
01174 return 0;
01175 }
01176
01177 p = &devices[*device_num_p];
01178
01179
01180 p->name = ast_strdup(s);
01181
01182 p->grabber = NULL;
01183 p->grabber_data = NULL;
01184 p->dev_buf = NULL;
01185 p->last_frame = ast_tvnow();
01186 p->status_index = 0;
01187 (*device_num_p)++;
01188 return 0;
01189 }
01190
01191
01192 int console_video_cli(struct video_desc *env, const char *var, int fd)
01193 {
01194 if (env == NULL)
01195 return 1;
01196
01197 if (!strcasecmp(var, "videodevice")) {
01198 ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
01199 } else if (!strcasecmp(var, "videocodec")) {
01200 ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
01201 } else if (!strcasecmp(var, "sendvideo")) {
01202 ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
01203 } else if (!strcasecmp(var, "video_size")) {
01204 int in_w = 0, in_h = 0;
01205 if (env->in) {
01206 in_w = env->in->dec_out.w;
01207 in_h = env->in->dec_out.h;
01208 }
01209 ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
01210 env->enc_in.w, env->enc_in.h,
01211 env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
01212 env->loc_dpy.w, env->loc_dpy.h,
01213 env->rem_dpy.w, env->rem_dpy.h,
01214 in_w, in_h);
01215 } else if (!strcasecmp(var, "bitrate")) {
01216 ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
01217 } else if (!strcasecmp(var, "qmin")) {
01218 ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
01219 } else if (!strcasecmp(var, "fps")) {
01220 ast_cli(fd, "fps is [%d]\n", env->out.fps);
01221 } else if (!strcasecmp(var, "startgui")) {
01222 env->stayopen = 1;
01223 console_video_start(env, NULL);
01224 } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
01225 env->stayopen = 0;
01226 if (env->gui && env->owner)
01227 ast_cli_command(-1, "console hangup");
01228 else
01229 console_video_uninit(env);
01230 } else {
01231 return 1;
01232 }
01233 return 0;
01234 }
01235
01236
01237 int console_video_config(struct video_desc **penv,
01238 const char *var, const char *val)
01239 {
01240 struct video_desc *env;
01241
01242 if (penv == NULL) {
01243 ast_log(LOG_WARNING, "bad argument penv=NULL\n");
01244 return 1;
01245 }
01246
01247 env = *penv;
01248 if (env == NULL) {
01249 env = *penv = ast_calloc(1, sizeof(struct video_desc));
01250 if (env == NULL) {
01251 ast_log(LOG_WARNING, "fail to allocate video_desc\n");
01252 return 1;
01253
01254 }
01255
01256 env->out.device_primary = 0;
01257 env->out.device_secondary = 0;
01258 env->out.fps = 5;
01259 env->out.bitrate = 65000;
01260 env->out.sendvideo = 1;
01261 env->out.qmin = 3;
01262 env->out.device_num = 0;
01263 }
01264 CV_START(var, val);
01265 CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
01266 CV_BOOL("sendvideo", env->out.sendvideo);
01267 CV_F("video_size", video_geom(&env->enc_in, val));
01268 CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
01269 CV_F("local_size", video_geom(&env->loc_dpy, val));
01270 CV_F("remote_size", video_geom(&env->rem_dpy, val));
01271 CV_STR("keypad", env->keypad_file);
01272 CV_F("region", keypad_cfg_read(env->gui, val));
01273 CV_UINT("startgui", env->stayopen);
01274 CV_STR("keypad_font", env->keypad_font);
01275 CV_STR("sdl_videodriver", env->sdl_videodriver);
01276 CV_UINT("fps", env->out.fps);
01277 CV_UINT("bitrate", env->out.bitrate);
01278 CV_UINT("qmin", env->out.qmin);
01279 CV_STR("videocodec", env->codec_name);
01280 return 1;
01281
01282 CV_END;
01283 return 0;
01284 }
01285
01286 #endif