Changeset 3535
- Timestamp:
- Apr 13, 2011 8:06:12 PM (14 years ago)
- Location:
- pjproject/branches/projects/2.0-dev/pjmedia/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/projects/2.0-dev/pjmedia/src/pjmedia-videodev/sdl_dev.c
r3527 r3535 378 378 } 379 379 380 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 381 @implementation SDLDelegate 382 - (int)sdl_thread 383 { 384 #else 385 static int sdlthread(void * data) 386 { 387 struct sdl_stream *strm = (struct sdl_stream*)data; 388 #endif 389 sdl_fmt_info *sdl_info = get_sdl_format_info(strm->param.fmt.id); 380 static void destroy_sdl(struct sdl_stream *strm) 381 { 382 if (strm->surf) { 383 SDL_FreeSurface(strm->surf); 384 strm->surf = NULL; 385 } 386 if (strm->overlay) { 387 SDL_FreeYUVOverlay(strm->overlay); 388 strm->overlay = NULL; 389 } 390 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 391 if (strm->texture) { 392 glDeleteTextures(1, &strm->texture); 393 strm->texture = 0; 394 } 395 #endif 396 } 397 398 static pj_status_t init_sdl(struct sdl_stream *strm, pjmedia_format *fmt) 399 { 400 sdl_fmt_info *sdl_info = get_sdl_format_info(fmt->id); 390 401 const pjmedia_video_format_info *vfi; 391 402 pjmedia_video_format_detail *vfd; 392 403 393 404 vfi = pjmedia_get_video_format_info(pjmedia_video_format_mgr_instance(), 394 strm->param.fmt.id); 395 if (!vfi || !sdl_info) { 396 strm->status = PJMEDIA_EVID_BADFORMAT; 397 goto on_return; 398 } 399 400 strm->vafp.size = strm->param.fmt.det.vid.size; 405 fmt->id); 406 if (!vfi || !sdl_info) 407 return PJMEDIA_EVID_BADFORMAT; 408 409 strm->vafp.size = fmt->det.vid.size; 401 410 strm->vafp.buffer = NULL; 402 if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS) { 403 strm->status = PJMEDIA_EVID_BADFORMAT; 404 goto on_return; 405 } 406 407 /* Initialize the SDL library */ 408 if (SDL_Init(SDL_INIT_VIDEO)) { 409 PJ_LOG(4, (THIS_FILE, "Cannot initialize SDL")); 410 strm->status = PJMEDIA_EVID_INIT; 411 goto on_return; 412 } 413 414 vfd = pjmedia_format_get_video_format_detail(&strm->param.fmt, PJ_TRUE); 411 if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS) 412 return PJMEDIA_EVID_BADFORMAT; 413 414 vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE); 415 415 strm->rect.x = strm->rect.y = 0; 416 416 strm->rect.w = (Uint16)vfd->size.w; 417 417 strm->rect.h = (Uint16)vfd->size.h; 418 418 419 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 420 if (strm->param.rend_id == OPENGL_DEV_IDX) { 421 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); 422 } 423 #endif 424 425 /* Initialize the display, requesting a software surface */ 419 /* Initialize the display */ 426 420 strm->screen = SDL_SetVideoMode(strm->rect.w, strm->rect.h, 0, ( 427 421 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL … … 430 424 #endif 431 425 SDL_RESIZABLE | SDL_SWSURFACE)); 432 if (strm->screen == NULL) { 433 strm->status = PJMEDIA_EVID_SYSERR; 434 goto on_return; 435 } 426 if (strm->screen == NULL) 427 return PJMEDIA_EVID_SYSERR; 428 436 429 SDL_WM_SetCaption("pjmedia-SDL video", NULL); 437 430 431 destroy_sdl(strm); 432 438 433 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 439 434 if (strm->param.rend_id == OPENGL_DEV_IDX) { 435 /* Init some OpenGL settings */ 440 436 glDisable(GL_DEPTH_TEST); 441 437 glDisable(GL_CULL_FACE); 442 438 glEnable(GL_TEXTURE_2D); 443 439 440 /* Init the viewport */ 444 441 glViewport(0, 0, strm->rect.w, strm->rect.h); 445 442 glMatrixMode(GL_PROJECTION); … … 452 449 glLoadIdentity(); 453 450 451 /* Create a texture */ 454 452 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 455 453 glGenTextures(1, &strm->texture); 456 454 457 455 #if defined(PJ_WIN32) && PJ_WIN32 != 0 456 /** 457 * On Win32 platform, the OpenGL drawing must be in the same 458 * thread that calls SDL_SetVideoMode(), hence we need a buffer 459 * for the frame from sdl_stream_put_frame() 460 */ 458 461 if (strm->vafp.framebytes > strm->tex_buf_size) { 459 462 strm->tex_buf_size = strm->vafp.framebytes; … … 471 474 sdl_info->Bmask, 472 475 sdl_info->Amask); 473 if (strm->surf == NULL) { 474 strm->status = PJMEDIA_EVID_SYSERR; 475 goto on_return; 476 } 476 if (strm->surf == NULL) 477 return PJMEDIA_EVID_SYSERR; 477 478 } else if (vfi->color_model == PJMEDIA_COLOR_MODEL_YUV) { 478 479 strm->overlay = SDL_CreateYUVOverlay(strm->rect.w, strm->rect.h, 479 480 sdl_info->sdl_format, 480 481 strm->screen); 481 if (strm->overlay == NULL) { 482 strm->status = PJMEDIA_EVID_SYSERR; 483 goto on_return; 484 } 485 } 482 if (strm->overlay == NULL) 483 return PJMEDIA_EVID_SYSERR; 484 } 485 486 return PJ_SUCCESS; 487 } 488 489 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 490 @implementation SDLDelegate 491 - (int)sdl_thread 492 { 493 #else 494 static int sdlthread(void * data) 495 { 496 struct sdl_stream *strm = (struct sdl_stream*)data; 497 #endif 498 499 /* Initialize the SDL library */ 500 if (SDL_Init(SDL_INIT_VIDEO)) { 501 PJ_LOG(4, (THIS_FILE, "Cannot initialize SDL")); 502 strm->status = PJMEDIA_EVID_INIT; 503 goto on_return; 504 } 505 506 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 507 if (strm->param.rend_id == OPENGL_DEV_IDX) { 508 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); 509 } 510 #endif 511 512 strm->status = init_sdl(strm, &strm->param.fmt); 513 if (strm->status != PJ_SUCCESS) 514 goto on_return; 486 515 487 516 #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 488 517 on_return: 489 518 if (strm->status != PJ_SUCCESS) { 490 if (strm->surf) { 491 SDL_FreeSurface(strm->surf); 492 strm->surf = NULL; 493 } 494 495 if (strm->overlay) { 496 SDL_FreeYUVOverlay(strm->overlay); 497 strm->overlay = NULL; 498 } 499 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 500 if (strm->texture) { 501 glDeleteTextures(1, &strm->texture); 502 strm->texture = 0; 503 } 504 #endif 505 519 destory_sdl(strm); 506 520 SDL_Quit(); 507 521 strm->screen = NULL; … … 554 568 pj_assert(sevent.user.code == PJMEDIA_VID_DEV_CAP_FORMAT); 555 569 556 PJ_TODO(FIX_THIS_FOR_OPENGL);557 570 fmt = (pjmedia_format *)sevent.user.data1; 558 vfi = pjmedia_get_video_format_info( 559 pjmedia_video_format_mgr_instance(), 560 fmt->id); 561 if (!vfi || !sdl_info) { 562 strm->status = PJMEDIA_EVID_BADFORMAT; 563 break; 564 } 565 566 vfd = pjmedia_format_get_video_format_detail(fmt, PJ_TRUE); 567 strm->vafp.size = vfd->size; 568 strm->vafp.buffer = NULL; 569 if (vfi->apply_fmt(vfi, &strm->vafp) != PJ_SUCCESS) { 570 strm->status = PJMEDIA_EVID_BADFORMAT; 571 break; 572 } 573 574 strm->rect.w = (Uint16)vfd->size.w; 575 strm->rect.h = (Uint16)vfd->size.h; 576 577 /* Stop the stream */ 571 572 /* Stop the stream */ 578 573 sdl_stream_stop((pjmedia_vid_dev_stream *)strm); 579 574 580 /* Initialize the display, requesting a software surface */ 581 strm->screen = SDL_SetVideoMode(strm->rect.w, 582 strm->rect.h, 0, 583 SDL_RESIZABLE | 584 SDL_SWSURFACE); 585 if (strm->screen == NULL) { 586 strm->status = PJMEDIA_EVID_SYSERR; 587 break; 588 } 589 590 if (strm->surf) 591 SDL_FreeSurface(strm->surf); 592 if (strm->overlay) 593 SDL_FreeYUVOverlay(strm->overlay); 594 595 /* Update SDL info for the new format */ 596 sdl_info = get_sdl_format_info(fmt->id); 597 598 if (vfi->color_model == PJMEDIA_COLOR_MODEL_RGB) { 599 strm->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, 600 strm->rect.w, 601 strm->rect.h, 602 vfi->bpp, 603 sdl_info->Rmask, 604 sdl_info->Gmask, 605 sdl_info->Bmask, 606 sdl_info->Amask); 607 if (strm->surf == NULL) { 608 strm->status = PJMEDIA_EVID_SYSERR; 609 break; 610 } 611 } else if (vfi->color_model == PJMEDIA_COLOR_MODEL_YUV) { 612 strm->overlay = SDL_CreateYUVOverlay( 613 strm->rect.w, 614 strm->rect.h, 615 sdl_info->sdl_format, 616 strm->screen); 617 if (strm->overlay == NULL) { 618 strm->status = PJMEDIA_EVID_SYSERR; 619 break; 620 } 621 } 622 623 /* Restart the stream */ 624 sdl_stream_start((pjmedia_vid_dev_stream *)strm); 625 626 strm->status = PJ_SUCCESS; 575 /* Re-initialize SDL */ 576 strm->status = init_sdl(strm, fmt); 577 578 if (strm->status == PJ_SUCCESS) { 579 pjmedia_format_copy(&strm->param.fmt, fmt); 580 /* Restart the stream */ 581 sdl_stream_start((pjmedia_vid_dev_stream *)strm); 582 } 583 627 584 break; 628 585 } … … 714 671 #endif 715 672 on_return: 716 if (strm->surf) { 717 SDL_FreeSurface(strm->surf); 718 strm->surf = NULL; 719 } 720 721 if (strm->overlay) { 722 SDL_FreeYUVOverlay(strm->overlay); 723 strm->overlay = NULL; 724 } 725 #if PJMEDIA_VIDEO_DEV_SDL_HAS_OPENGL 726 if (strm->texture) { 727 glDeleteTextures(1, &strm->texture); 728 strm->texture = 0; 729 } 730 #endif 731 673 destroy_sdl(strm); 732 674 SDL_Quit(); 733 675 strm->screen = NULL; … … 818 760 else if (stream->param.rend_id == OPENGL_DEV_IDX) { 819 761 #if defined(PJ_WIN32) && PJ_WIN32 != 0 820 pj_assert(frame->size == stream->vafp.framebytes); 821 pj_memcpy(stream->tex_buf, frame->buf, frame->size); 762 pj_memcpy(stream->tex_buf, frame->buf, stream->vafp.framebytes); 822 763 #else 823 764 draw_gl(stream, frame->buf); … … 892 833 strm->delegate = [[SDLDelegate alloc]init]; 893 834 strm->delegate->strm = strm; 835 /* On Mac OS X, we need to call SDL functions in the main thread */ 894 836 [strm->delegate performSelectorOnMainThread:@selector(sdl_thread) 895 837 withObject:nil waitUntilDone:YES]; … … 1032 974 1033 975 strm->status = status; 1034 } else if (strm->status == PJ_SUCCESS) {1035 pj_memcpy(&strm->param.fmt, pval, sizeof(strm->param.fmt));1036 976 } 1037 977 -
pjproject/branches/projects/2.0-dev/pjmedia/src/pjmedia/videoport.c
r3509 r3535 38 38 struct pjmedia_vid_port 39 39 { 40 pj_pool_t *pool; 40 41 pj_str_t dev_name; 41 42 pjmedia_dir dir; … … 50 51 pj_bool_t destroy_client_port; 51 52 52 pjmedia_converter *cap_conv; 53 void *cap_conv_buf; 54 pj_size_t cap_conv_buf_size; 53 pjmedia_converter *cap_conv; 54 void *cap_conv_buf; 55 pj_size_t cap_conv_buf_size; 56 pjmedia_conversion_param cap_conv_param; 55 57 56 58 pjmedia_clock *enc_clock, … … 136 138 unsigned ptime_usec; 137 139 pjmedia_vid_param vparam; 138 pjmedia_conversion_param conv_param;139 140 140 141 PJ_ASSERT_RETURN(pool && prm && p_vid_port, PJ_EINVAL); … … 152 153 /* Allocate videoport */ 153 154 vp = PJ_POOL_ZALLOC_T(pool, pjmedia_vid_port); 155 vp->pool = pool; 154 156 vp->role = prm->active ? ROLE_ACTIVE : ROLE_PASSIVE; 155 157 vp->dir = prm->vidparam.dir; … … 257 259 vparam.fmt.det.vid.fps.num, vparam.fmt.det.vid.fps.denum)); 258 260 261 if (vp->dir & PJMEDIA_DIR_CAPTURE) { 262 pjmedia_format_copy(&vp->cap_conv_param.src, &vparam.fmt); 263 pjmedia_format_copy(&vp->cap_conv_param.dst, &prm->vidparam.fmt); 264 } else { 265 pjmedia_format_copy(&vp->cap_conv_param.src, &prm->vidparam.fmt); 266 pjmedia_format_copy(&vp->cap_conv_param.dst, &vparam.fmt); 267 } 268 259 269 /* Instantiate converter if necessary */ 260 270 if (vparam.fmt.id != prm->vidparam.fmt.id || … … 268 278 pjmedia_video_apply_fmt_param vafp; 269 279 270 if (vp->dir & PJMEDIA_DIR_CAPTURE) { 271 pjmedia_format_copy(&conv_param.src, &vparam.fmt); 272 pjmedia_format_copy(&conv_param.dst, &prm->vidparam.fmt); 273 274 status = pjmedia_converter_create(NULL, pool, &conv_param, 275 &vp->cap_conv); 276 } else { 277 pjmedia_format_copy(&conv_param.src, &prm->vidparam.fmt); 278 pjmedia_format_copy(&conv_param.dst, &vparam.fmt); 279 280 status = pjmedia_converter_create(NULL, pool, &conv_param, 281 &vp->cap_conv); 282 } 283 280 status = pjmedia_converter_create(NULL, pool, &vp->cap_conv_param, 281 &vp->cap_conv); 284 282 if (status != PJ_SUCCESS) { 285 283 PJ_PERROR(4,(THIS_FILE, status, "Error creating converter")); … … 288 286 289 287 /* Allocate buffer for conversion */ 290 vfi = pjmedia_get_video_format_info(NULL, conv_param.dst.id);288 vfi = pjmedia_get_video_format_info(NULL, vp->cap_conv_param.dst.id); 291 289 if (!vfi) 292 290 return PJMEDIA_EBADFMT; 293 291 294 292 pj_bzero(&vafp, sizeof(vafp)); 295 vafp.size = conv_param.dst.det.vid.size;293 vafp.size = vp->cap_conv_param.dst.det.vid.size; 296 294 status = vfi->apply_fmt(vfi, &vafp); 297 295 if (status != PJ_SUCCESS) … … 354 352 355 353 vfi = pjmedia_get_video_format_info(NULL, vp->cap_conv? 356 conv_param.src.id:354 vp->cap_conv_param.src.id: 357 355 vparam.fmt.id); 358 356 if (!vfi) { … … 362 360 363 361 pj_bzero(&vafp, sizeof(vafp)); 364 vafp.size = (vp->cap_conv? conv_param.src.det.vid.size:362 vafp.size = (vp->cap_conv? vp->cap_conv_param.src.det.vid.size: 365 363 vparam.fmt.det.vid.size); 366 364 status = vfi->apply_fmt(vfi, &vafp); … … 611 609 pj_status_t status; 612 610 611 pjmedia_vid_port_stop(vp); 612 613 613 /* Retrieve the video format detail */ 614 614 vfd = pjmedia_format_get_video_format_detail( … … 617 617 return PJMEDIA_EVID_BADFORMAT; 618 618 pj_assert(vfd->fps.num); 619 620 if (vp->cap_conv) { 621 /* Change the destination format to the new format */ 622 pjmedia_format_copy(&vp->cap_conv_param.src, 623 &vp->client_port->info.fmt); 624 /* Only copy the size here */ 625 vp->cap_conv_param.dst.det.vid.size = 626 vp->client_port->info.fmt.det.vid.size, 627 628 /* Recreate converter */ 629 pjmedia_converter_destroy(vp->cap_conv); 630 status = pjmedia_converter_create(NULL, vp->pool, 631 &vp->cap_conv_param, 632 &vp->cap_conv); 633 if (status != PJ_SUCCESS) { 634 PJ_PERROR(4,(THIS_FILE, status, "Error recreating converter")); 635 return status; 636 } 637 } 619 638 620 639 status = pjmedia_vid_dev_stream_set_cap( … … 628 647 status != PJMEDIA_EVID_ERR ? "success" : 629 648 "failure")); 630 pjmedia_vid_port_stop(vp);631 649 return status; 632 650 } … … 650 668 /* Notify application of the format change. */ 651 669 pevent.event_type = PJMEDIA_EVENT_FMT_CHANGED; 652 pj_memcpy(&pevent.event_desc.fmt_change.new_format,653 &vp->client_port->info.fmt, sizeof(pjmedia_format));670 pjmedia_format_copy(&pevent.event_desc.fmt_change.new_format, 671 &vp->client_port->info.fmt); 654 672 if (vp->strm_cb.on_event_cb) 655 673 (*vp->strm_cb.on_event_cb)(vp->strm, vp->strm_cb_data, &pevent); 674 675 pjmedia_vid_port_start(vp); 656 676 } 657 677
Note: See TracChangeset
for help on using the changeset viewer.