- Timestamp:
- Apr 13, 2011 8:06:12 PM (14 years ago)
- File:
-
- 1 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
Note: See TracChangeset
for help on using the changeset viewer.