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
00040
00041
00042
00043 #include "asterisk.h"
00044 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 369001 $")
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
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
00115
00116
00117
00118 #if !defined(HAVE_VIDEO_CONSOLE) || !defined(HAVE_FFMPEG)
00119
00120 int console_write_video(struct ast_channel *chan, struct ast_frame *f)
00121 {
00122 return 0;
00123 }
00124
00125 int console_video_cli(struct video_desc *env, const char *var, int fd)
00126 {
00127 return 1;
00128 }
00129
00130 int console_video_config(struct video_desc **penv, const char *var, const char *val)
00131 {
00132 return 1;
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;
00147 }
00148
00149 int console_video_formats = 0;
00150
00151 #else
00152
00153
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
00161 static void my_scale(struct fbuf_t *in, AVPicture *p_in,
00162 struct fbuf_t *out, AVPicture *p_out);
00163
00164
00165
00166
00167
00168
00169 struct video_device {
00170 char *name;
00171
00172 struct grab_desc *grabber;
00173 void *grabber_data;
00174 struct fbuf_t *dev_buf;
00175 struct timeval last_frame;
00176 int status_index;
00177
00178
00179 };
00180
00181 struct video_codec_desc;
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195 struct video_out_desc {
00196
00197
00198
00199
00200
00201
00202 int fps;
00203 int bitrate;
00204 int qmin;
00205
00206 int sendvideo;
00207
00208 struct fbuf_t loc_src_geometry;
00209 struct fbuf_t enc_out;
00210
00211 struct video_codec_desc *enc;
00212 void *enc_ctx;
00213 AVCodec *codec;
00214 AVFrame *enc_in_frame;
00215
00216 int mtu;
00217
00218
00219
00220
00221 struct video_device devices[MAX_VIDEO_SOURCES];
00222 int device_num;
00223 int device_primary;
00224 int device_secondary;
00225
00226 int picture_in_picture;
00227
00228
00229
00230 int pip_x;
00231 int pip_y;
00232 };
00233
00234
00235
00236
00237
00238
00239
00240 struct video_desc {
00241 char codec_name[64];
00242
00243 int stayopen;
00244 pthread_t vthread;
00245 ast_mutex_t dec_lock;
00246 int shutdown;
00247 struct ast_channel *owner;
00248
00249
00250 struct fbuf_t enc_in;
00251
00252 char keypad_file[256];
00253 char keypad_font[256];
00254
00255 char sdl_videodriver[256];
00256
00257 struct fbuf_t rem_dpy;
00258 struct fbuf_t loc_dpy;
00259
00260
00261 struct fbuf_t src_dpy[MAX_VIDEO_SOURCES];
00262
00263 int frame_freeze;
00264
00265
00266 struct gui_info *gui;
00267 struct video_dec_desc *in;
00268 struct video_out_desc out;
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
00281 b->w = x.w;
00282 b->h = x.h;
00283 b->pix_fmt = x.pix_fmt;
00284 }
00285
00286
00287
00288
00289 int get_gui_startup(struct video_desc* env)
00290 {
00291 return env ? env->stayopen : 0;
00292 }
00293
00294 #if 0
00295
00296
00297
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
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
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
00334 for (i = 0; i < v->device_num; i++) {
00335
00336 if (v->devices[i].grabber)
00337 continue;
00338
00339 for (j = 0; (g = console_grabbers[j]); j++) {
00340
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
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;
00356 }
00357 return 1;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
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)
00376 return NULL;
00377
00378
00379
00380
00381
00382 if (ast_tvdiff_ms(now, dev->last_frame) < 1000/fps)
00383 return NULL;
00384 dev->last_frame = now;
00385 return dev->grabber->read(dev->grabber_data);
00386 }
00387
00388
00389
00390
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
00401
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
00421 static int video_out_uninit(struct video_desc *env)
00422 {
00423 struct video_out_desc *v = &env->out;
00424 int i;
00425
00426
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;
00438
00439 fbuf_free(&env->enc_in);
00440 fbuf_free(&v->enc_out);
00441
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
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
00459
00460
00461
00462
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;
00482 }
00483
00484 v->mtu = 1400;
00485
00486
00487
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
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
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
00514
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
00523
00524
00525 enc_ctx->rtp_mode = 1;
00526 enc_ctx->rtp_payload_size = v->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;
00530 enc_ctx->time_base = (AVRational){1, v->fps};
00531 enc_ctx->gop_size = v->fps*5;
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
00544
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
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563 void console_video_uninit(struct video_desc *env)
00564 {
00565 int i, t = 100;
00566 if (env->stayopen == 0) {
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;
00579 }
00580
00581
00582
00583
00584
00585
00586 static AVPicture *fill_pict(struct fbuf_t *b, AVPicture *p)
00587 {
00588
00589 int l4 = b->w * b->h/4;
00590 int len = b->w;
00591 int luv = b->w/2;
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:
00606 sample_size = 2;
00607 luv = 0;
00608 break;
00609 }
00610 len *= sample_size;
00611
00612 p->data[0] = b->data;
00613 p->linesize[0] = len;
00614
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
00621
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
00631
00632
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
00646
00647
00648 if (out->win_w) {
00649 eff_w=out->win_w;
00650 eff_h=out->win_h;
00651 }
00652 #ifdef OLD_FFMPEG
00653
00654 img_convert(p_out, out->pix_fmt,
00655 p_in, in->pix_fmt, in->w, in->h);
00656 #else
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,
00673 p_out->data, p_out->linesize);
00674
00675 sws_freeContext(convert_ctx);
00676 }
00677 #endif
00678 }
00679
00680 struct video_desc *get_video_desc(struct ast_channel *c);
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
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)
00699 return 0;
00700 if (v == NULL)
00701 env->in = v = dec_init(f->subclass & ~1);
00702 if (v == NULL) {
00703
00704 ast_log(LOG_WARNING, "Cannot initialize input decoder\n");
00705 return 0;
00706 }
00707
00708 if (v->dec_in_cur == NULL)
00709 return 0;
00710 #if defined(DROP_PACKETS) && DROP_PACKETS > 0
00711
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
00720
00721
00722
00723
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;
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
00737
00738
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) {
00757
00758 struct fbuf_t *tmp = v->dec_in_cur;
00759 ast_mutex_lock(&env->dec_lock);
00760 if (++v->dec_in_cur == &v->dec_in[N_DEC_IN])
00761 v->dec_in_cur = &v->dec_in[0];
00762 if (v->dec_in_dpy == NULL) {
00763 v->dec_in_dpy = tmp;
00764 } else if (v->dec_in_dpy == v->dec_in_cur) {
00765 v->dec_in_cur = NULL;
00766 }
00767 ast_mutex_unlock(&env->dec_lock);
00768 }
00769 return 0;
00770 }
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
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
00792 if (!env->out.device_num)
00793 return NULL;
00794
00795
00796 for (i = 0; i < env->out.device_num; i++) {
00797 p_read = grabber_read(&env->out.devices[i], env->out.fps);
00798
00799 if (p_read)
00800 env->out.devices[i].dev_buf = p_read;
00801 }
00802
00803 loc_src_primary = env->out.devices[env->out.device_primary].dev_buf;
00804
00805
00806 if (loc_src_primary) {
00807
00808
00809 my_scale(loc_src_primary, NULL, &env->enc_in, NULL);
00810 }
00811 if (env->out.picture_in_picture) {
00812 struct fbuf_t *loc_src_secondary;
00813
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
00821
00822 my_scale(loc_src_secondary, NULL, &env->enc_in, NULL);
00823
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
00831
00832 env->out.picture_in_picture = 0;
00833 }
00834 }
00835 show_frame(env, WIN_LOCAL);
00836 for (i = 0; i < env->out.device_num; i++)
00837 show_frame(env, i+WIN_SRC1);
00838 if (tail == NULL)
00839 tail = &dummy;
00840 *tail = NULL;
00841
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
00856
00857
00858
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;
00866
00867
00868
00869
00870
00871 if (!ast_strlen_zero(env->sdl_videodriver)) {
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);
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
00895
00896
00897
00898 video_out_init(env);
00899
00900
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 };
00910 struct ast_frame *p, *f;
00911 struct ast_channel *chan;
00912 int fd;
00913 char *caption = NULL, buf[160];
00914
00915
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
00929
00930
00931 if (env->gui)
00932 eventhandler(env, caption);
00933
00934
00935 nanosleep(&t, NULL);
00936
00937 if (env->in) {
00938 struct video_dec_desc *v = env->in;
00939
00940
00941
00942
00943
00944 while (v->dec_in_dpy) {
00945 struct fbuf_t *tmp = v->dec_in_dpy;
00946
00947
00948 if (v->d_callbacks->dec_run(v, tmp) && !env->frame_freeze)
00949 show_frame(env, WIN_REMOTE);
00950 tmp->used = 0;
00951 tmp->ebit = 0;
00952 ast_mutex_lock(&env->dec_lock);
00953 if (++v->dec_in_dpy == &v->dec_in[N_DEC_IN])
00954 v->dec_in_dpy = &v->dec_in[0];
00955
00956 if (v->dec_in_cur == NULL)
00957 v->dec_in_cur = tmp;
00958 else if (v->dec_in_dpy == v->dec_in_cur)
00959 v->dec_in_dpy = NULL;
00960 ast_mutex_unlock(&env->dec_lock);
00961 }
00962 }
00963
00964 if (env->shutdown)
00965 break;
00966 f = get_video_frames(env, &p);
00967 if (!f)
00968 continue;
00969 chan = env->owner;
00970 if (chan == NULL) {
00971
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 fd = chan->alertpipe[1];
00980 ast_channel_lock(chan);
00981
00982
00983 if (chan->readq.first == NULL) {
00984 chan->readq.first = f;
00985 } else {
00986 chan->readq.last->frame_list.next = f;
00987 }
00988 chan->readq.last = p;
00989
00990
00991
00992
00993 if (fd > -1) {
00994 int blah = 1, l = sizeof(blah);
00995 for (p = f; p; p = AST_LIST_NEXT(p, frame_list)) {
00996 if (write(fd, &blah, l) != l)
00997 ast_log(LOG_WARNING, "Unable to write to alert pipe on %s, frametype/subclass %d/%d: %s!\n",
00998 chan->name, f->frametype, f->subclass, strerror(errno));
00999 }
01000 }
01001 ast_channel_unlock(chan);
01002 }
01003
01004
01005 env->in = dec_uninit(env->in);
01006 video_out_uninit(env);
01007
01008 if (env->gui)
01009 env->gui = cleanup_sdl(env->gui, env->out.device_num);
01010 ast_mutex_destroy(&env->dec_lock);
01011 env->shutdown = 0;
01012 return NULL;
01013 }
01014
01015 static void copy_geometry(struct fbuf_t *src, struct fbuf_t *dst)
01016 {
01017 if (dst->w == 0)
01018 dst->w = src->w;
01019 if (dst->h == 0)
01020 dst->h = src->h;
01021 }
01022
01023
01024
01025
01026
01027 static void init_env(struct video_desc *env)
01028 {
01029 struct fbuf_t *c = &(env->out.loc_src_geometry);
01030 struct fbuf_t *ei = &(env->enc_in);
01031 struct fbuf_t *ld = &(env->loc_dpy);
01032 struct fbuf_t *rd = &(env->rem_dpy);
01033 int i;
01034
01035 c->pix_fmt = PIX_FMT_YUV420P;
01036 ei->pix_fmt = PIX_FMT_YUV420P;
01037 if (ei->w == 0 || ei->h == 0) {
01038 ei->w = 352;
01039 ei->h = 288;
01040 }
01041 ld->pix_fmt = rd->pix_fmt = PIX_FMT_YUV420P;
01042
01043 copy_geometry(ei, c);
01044 copy_geometry(ei, rd);
01045 copy_geometry(rd, ld);
01046
01047
01048 for (i = 0; i < env->out.device_num; i++) {
01049 env->src_dpy[i].pix_fmt = PIX_FMT_YUV420P;
01050 env->src_dpy[i].w = SRC_WIN_W;
01051 env->src_dpy[i].h = SRC_WIN_H;
01052 }
01053
01054
01055
01056 env->out.pip_x = ei->w - ei->w/3;
01057 env->out.pip_y = ei->h - ei->h/3;
01058 }
01059
01060
01061
01062
01063
01064
01065
01066
01067 void console_video_start(struct video_desc *env, struct ast_channel *owner)
01068 {
01069 ast_log(LOG_WARNING, "env %p chan %p\n", env, owner);
01070 if (env == NULL)
01071 return;
01072 env->owner = owner;
01073 if (env->vthread)
01074 return;
01075 init_env(env);
01076 env->out.enc = map_config_video_format(env->codec_name);
01077
01078 ast_log(LOG_WARNING, "start video out %s %dx%d\n",
01079 env->codec_name, env->enc_in.w, env->enc_in.h);
01080
01081
01082
01083
01084
01085 avcodec_init();
01086 avcodec_register_all();
01087 av_log_set_level(AV_LOG_ERROR);
01088
01089 if (env->out.fps == 0) {
01090 env->out.fps = 15;
01091 ast_log(LOG_WARNING, "fps unset, forcing to %d\n", env->out.fps);
01092 }
01093 if (env->out.bitrate == 0) {
01094 env->out.bitrate = 65000;
01095 ast_log(LOG_WARNING, "bitrate unset, forcing to %d\n", env->out.bitrate);
01096 }
01097
01098 ast_pthread_create_detached_background(&env->vthread,
01099 NULL, video_thread, env);
01100 }
01101
01102
01103
01104
01105
01106
01107
01108 static int video_geom(struct fbuf_t *b, const char *s)
01109 {
01110 int w = 0, h = 0;
01111
01112 static struct {
01113 const char *s; int w; int h;
01114 } *fp, formats[] = {
01115 {"16cif", 1408, 1152 },
01116 {"xga", 1024, 768 },
01117 {"4cif", 704, 576 },
01118 {"vga", 640, 480 },
01119 {"cif", 352, 288 },
01120 {"qvga", 320, 240 },
01121 {"qcif", 176, 144 },
01122 {"sqcif", 128, 96 },
01123 {NULL, 0, 0 },
01124 };
01125 if (*s == '<' || *s == '>')
01126 sscanf(s+1,"%dx%d", &w, &h);
01127 for (fp = formats; fp->s; fp++) {
01128 if (*s == '>') {
01129 if (fp->w <= w) {
01130 if (fp > formats)
01131 fp--;
01132 break;
01133 }
01134 } else if (*s == '<') {
01135 if (fp->w < w)
01136 break;
01137 } else if (!strcasecmp(s, fp->s)) {
01138 break;
01139 }
01140 }
01141 if (*s == '<' && fp->s == NULL)
01142 fp--;
01143 if (fp->s) {
01144 b->w = fp->w;
01145 b->h = fp->h;
01146 } else if (sscanf(s, "%dx%d", &b->w, &b->h) != 2) {
01147 ast_log(LOG_WARNING, "Invalid video_size %s, using 352x288\n", s);
01148 b->w = 352;
01149 b->h = 288;
01150 }
01151 return 0;
01152 }
01153
01154
01155
01156
01157
01158
01159
01160
01161
01162
01163
01164
01165
01166
01167
01168
01169
01170 static int device_table_fill(struct video_device *devices, int *device_num_p, const char *s)
01171 {
01172 int i;
01173 struct video_device *p;
01174
01175
01176 if (*device_num_p >= 9)
01177 return 0;
01178
01179 for (i = 0; i < *device_num_p; i++) {
01180 if (!strcmp(devices[i].name, s))
01181 return 0;
01182 }
01183
01184 p = &devices[*device_num_p];
01185
01186
01187 p->name = ast_strdup(s);
01188
01189 p->grabber = NULL;
01190 p->grabber_data = NULL;
01191 p->dev_buf = NULL;
01192 p->last_frame = ast_tvnow();
01193 p->status_index = 0;
01194 (*device_num_p)++;
01195 return 0;
01196 }
01197
01198
01199 int console_video_cli(struct video_desc *env, const char *var, int fd)
01200 {
01201 if (env == NULL)
01202 return 1;
01203
01204 if (!strcasecmp(var, "videodevice")) {
01205 ast_cli(fd, "videodevice is [%s]\n", env->out.devices[env->out.device_primary].name);
01206 } else if (!strcasecmp(var, "videocodec")) {
01207 ast_cli(fd, "videocodec is [%s]\n", env->codec_name);
01208 } else if (!strcasecmp(var, "sendvideo")) {
01209 ast_cli(fd, "sendvideo is [%s]\n", env->out.sendvideo ? "on" : "off");
01210 } else if (!strcasecmp(var, "video_size")) {
01211 int in_w = 0, in_h = 0;
01212 if (env->in) {
01213 in_w = env->in->dec_out.w;
01214 in_h = env->in->dec_out.h;
01215 }
01216 ast_cli(fd, "sizes: video %dx%d camera %dx%d local %dx%d remote %dx%d in %dx%d\n",
01217 env->enc_in.w, env->enc_in.h,
01218 env->out.loc_src_geometry.w, env->out.loc_src_geometry.h,
01219 env->loc_dpy.w, env->loc_dpy.h,
01220 env->rem_dpy.w, env->rem_dpy.h,
01221 in_w, in_h);
01222 } else if (!strcasecmp(var, "bitrate")) {
01223 ast_cli(fd, "bitrate is [%d]\n", env->out.bitrate);
01224 } else if (!strcasecmp(var, "qmin")) {
01225 ast_cli(fd, "qmin is [%d]\n", env->out.qmin);
01226 } else if (!strcasecmp(var, "fps")) {
01227 ast_cli(fd, "fps is [%d]\n", env->out.fps);
01228 } else if (!strcasecmp(var, "startgui")) {
01229 env->stayopen = 1;
01230 console_video_start(env, NULL);
01231 } else if (!strcasecmp(var, "stopgui") && env->stayopen != 0) {
01232 env->stayopen = 0;
01233 if (env->gui && env->owner)
01234 ast_cli_command(-1, "console hangup");
01235 else
01236 console_video_uninit(env);
01237 } else {
01238 return 1;
01239 }
01240 return 0;
01241 }
01242
01243
01244 int console_video_config(struct video_desc **penv,
01245 const char *var, const char *val)
01246 {
01247 struct video_desc *env;
01248
01249 if (penv == NULL) {
01250 ast_log(LOG_WARNING, "bad argument penv=NULL\n");
01251 return 1;
01252 }
01253
01254 env = *penv;
01255 if (env == NULL) {
01256 env = *penv = ast_calloc(1, sizeof(struct video_desc));
01257 if (env == NULL) {
01258 ast_log(LOG_WARNING, "fail to allocate video_desc\n");
01259 return 1;
01260
01261 }
01262
01263 env->out.device_primary = 0;
01264 env->out.device_secondary = 0;
01265 env->out.fps = 5;
01266 env->out.bitrate = 65000;
01267 env->out.sendvideo = 1;
01268 env->out.qmin = 3;
01269 env->out.device_num = 0;
01270 }
01271 CV_START(var, val);
01272 CV_F("videodevice", device_table_fill(env->out.devices, &env->out.device_num, val));
01273 CV_BOOL("sendvideo", env->out.sendvideo);
01274 CV_F("video_size", video_geom(&env->enc_in, val));
01275 CV_F("camera_size", video_geom(&env->out.loc_src_geometry, val));
01276 CV_F("local_size", video_geom(&env->loc_dpy, val));
01277 CV_F("remote_size", video_geom(&env->rem_dpy, val));
01278 CV_STR("keypad", env->keypad_file);
01279 CV_F("region", keypad_cfg_read(env->gui, val));
01280 CV_UINT("startgui", env->stayopen);
01281 CV_STR("keypad_font", env->keypad_font);
01282 CV_STR("sdl_videodriver", env->sdl_videodriver);
01283 CV_UINT("fps", env->out.fps);
01284 CV_UINT("bitrate", env->out.bitrate);
01285 CV_UINT("qmin", env->out.qmin);
01286 CV_STR("videocodec", env->codec_name);
01287 return 1;
01288
01289 CV_END;
01290 return 0;
01291 }
01292
01293 #endif