Advertisement
Domarius

SDL1 + libvlc demo

Apr 7th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.48 KB | None | 0 0
  1. /* libSDL and libVLC sample code
  2.  * Copyright © 2008 Sam Hocevar <sam@zoy.org>
  3.  * license: [http://en.wikipedia.org/wiki/WTFPL WTFPL] */
  4.  
  5. #include <stdio.h>
  6. #include <stdint.h>
  7. #include <math.h>
  8. #include <stdlib.h>
  9. #include <assert.h>
  10.  
  11. #include <SDL/SDL.h>
  12. #include <SDL/SDL_mutex.h>
  13.  
  14. #include <vlc/vlc.h>
  15.  
  16. int window_width = 640;
  17. int window_height = 480;
  18. int fullscreen_width = 0;
  19. int fullscreen_height = 0;
  20. int current_width = 0;
  21. int current_height = 0;
  22. bool fullscreen = false;
  23. int screen_options = SDL_ANYFORMAT | SDL_HWSURFACE | SDL_DOUBLEBUF;
  24. SDL_Surface *screen;
  25.  
  26. int video_width = 0;
  27. int video_height = 0;
  28.  
  29. struct ctx
  30. {
  31.     SDL_Surface *surf;
  32.     SDL_mutex *mutex;
  33. };
  34.  
  35. static void *lock(void *data, void **p_pixels)
  36. {
  37.     struct ctx *ctx = data;
  38.  
  39.     SDL_LockMutex(ctx->mutex);
  40.     SDL_LockSurface(ctx->surf);
  41.     *p_pixels = ctx->surf->pixels;
  42.     return NULL; /* picture identifier, not needed here */
  43. }
  44.  
  45. static void unlock(void *data, void *id, void *const *p_pixels)
  46. {
  47.     struct ctx *ctx = data;
  48.  
  49.     /* VLC just rendered the video, but we can also render stuff */
  50.     uint16_t *pixels = *p_pixels;
  51.     int x, y;
  52.  
  53.     for(y = 10; y < 40; y++)
  54.         for(x = 10; x < 40; x++)
  55.             if(x < 13 || y < 13 || x > 36 || y > 36)
  56.                 pixels[y * video_width + x] = 0xffff;
  57.             else
  58.                 pixels[y * video_width + x] = 0x0;
  59.  
  60.     SDL_UnlockSurface(ctx->surf);
  61.     SDL_UnlockMutex(ctx->mutex);
  62.  
  63.     assert(id == NULL); /* picture identifier, not needed here */
  64. }
  65.  
  66. static void display(void *data, void *id)
  67. {
  68.     /* VLC wants to display the video */
  69.     (void) data;
  70.     assert(id == NULL);
  71. }
  72.  
  73. static bool set_fullscreen(bool b)
  74. {
  75.     fullscreen = b;
  76.     int options;
  77.     if(fullscreen)
  78.     {
  79.         options = screen_options | SDL_FULLSCREEN;
  80.         current_width = fullscreen_width;
  81.         current_height = fullscreen_height;
  82.     }
  83.     else
  84.     {
  85.         options = screen_options;
  86.         current_width = window_width;
  87.         current_height = window_height;
  88.     }
  89.     printf("Setting resolution %i,%i\n",current_width, current_height);
  90.     screen = SDL_SetVideoMode(current_width, current_height, 0, options);
  91.     if(!screen)
  92.     {
  93.         printf("cannot set video mode\n");
  94.         return false;
  95.     }
  96.     return true;
  97. }
  98.  
  99. int main(int argc, char *argv[])
  100. {
  101.     libvlc_instance_t *libvlc;
  102.     libvlc_media_t *m;
  103.     libvlc_media_player_t *mp;
  104.     char const *vlc_argv[] =
  105.     {
  106.         //"--no-audio", /* skip any audio track */
  107.         "--no-xlib", /* tell VLC to not use Xlib */
  108.     };
  109.     int vlc_argc = sizeof(vlc_argv) / sizeof(*vlc_argv);
  110.  
  111.     SDL_Surface *empty;
  112.     SDL_Event event;
  113.     SDL_Rect rect;
  114.     int done = 0, action = 0, pause = 0, n = 0;
  115.  
  116.     struct ctx ctx;
  117.  
  118.     if(argc < 2)
  119.     {
  120.         printf("Usage: %s <filename>\n", argv[0]);
  121.         return EXIT_FAILURE;
  122.     }
  123.  
  124.     /*
  125.      *  Initialise libSDL
  126.      */
  127.     if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) == -1)
  128.     {
  129.         printf("cannot initialize SDL\n");
  130.         return EXIT_FAILURE;
  131.     }
  132.  
  133.  
  134.     //Create window that fills the desktop resolution
  135.     const SDL_VideoInfo* vid_info = SDL_GetVideoInfo();
  136.     fullscreen_width = vid_info->current_w;
  137.     fullscreen_height = vid_info->current_h;
  138.     if(argc > 2)//TODO: Remove all of this when have a settings file.
  139.     {
  140.         fullscreen_width = atoi(argv[2]);
  141.         fullscreen_height = atoi(argv[3]);
  142.     }
  143.     window_width = fullscreen_width / 2;
  144.     window_height = fullscreen_height / 2;
  145.  
  146.     video_width = (int)((4.0 / 3.0) * (float)fullscreen_height); //Force 4:3
  147.     video_height = fullscreen_height;
  148.     current_width = fullscreen_width;
  149.     current_height = fullscreen_height;
  150.  
  151.     set_fullscreen(true);
  152.  
  153.     printf("Desktop res %i, %i\n", fullscreen_width, fullscreen_height);
  154.  
  155.     //Renderable surface for video,not using videowidth, videoheight right now because I want it to be full screen.
  156.     //VIDEOWID
  157.     empty = SDL_CreateRGBSurface(SDL_SWSURFACE, video_width, video_height,
  158.                                  32, 0, 0, 0, 0);
  159.     ctx.surf = SDL_CreateRGBSurface(SDL_SWSURFACE, video_width, video_height,
  160.                                     16, 0x001f, 0x07e0, 0xf800, 0);
  161.     ctx.mutex = SDL_CreateMutex();
  162.  
  163.     /*
  164.      *  Initialise libVLC
  165.      */
  166.     libvlc = libvlc_new(vlc_argc, vlc_argv);
  167.     m = libvlc_media_new_path(libvlc, argv[1]);
  168.     mp = libvlc_media_player_new_from_media(m);
  169.     libvlc_media_release(m);
  170.  
  171.     libvlc_video_set_callbacks(mp, lock, unlock, display, &ctx);
  172.     libvlc_video_set_format(mp, "RV16", video_width, video_height, video_width*2);
  173.     libvlc_media_player_play(mp);
  174.  
  175.     /*
  176.      *  Main loop
  177.      */
  178.     rect.w = 0;
  179.     rect.h = 0;
  180.     rect.x = (current_width - video_width) / 2;
  181.     rect.y = (current_height - video_height) / 2;
  182.  
  183.     while(!done)
  184.     {
  185.         action = 0;
  186.  
  187.         /* Keys: enter (fullscreen), space (pause), escape (quit) */
  188.         while( SDL_PollEvent( &event ) )
  189.         {
  190.             switch(event.type)
  191.             {
  192.             case SDL_QUIT:
  193.                 done = 1;
  194.                 break;
  195.             case SDL_KEYDOWN:
  196.                 action = event.key.keysym.sym;
  197.                 break;
  198.             }
  199.         }
  200.  
  201.         switch(action)
  202.         {
  203.         case SDLK_ESCAPE:
  204.             done = 1;
  205.             break;
  206.         case SDLK_RETURN:
  207.             set_fullscreen(!fullscreen);
  208.             break;
  209.         case SDLK_SPACE:
  210.             pause = !pause;
  211.             break;
  212.         }
  213.  
  214.         //rect.x = (int)((1. + .5 * sin(0.03 * n)) * (current_width - video_width) / 2);
  215.         //rect.y = (int)((1. + .5 * cos(0.03 * n)) * (current_height - video_height) / 2);
  216.         //if(!pause)
  217.             //n++;
  218.  
  219.         /* Blitting the surface does not prevent it from being locked and
  220.          * written to by another thread, so we use this additional mutex. */
  221.         SDL_LockMutex(ctx.mutex);
  222.         SDL_BlitSurface(ctx.surf, NULL, screen, &rect);
  223.         SDL_UnlockMutex(ctx.mutex);
  224.  
  225.         SDL_Flip(screen);
  226.         SDL_Delay(10);
  227.  
  228.         SDL_BlitSurface(empty, NULL, screen, &rect);
  229.     }
  230.  
  231.     /*
  232.      * Stop stream and clean up libVLC
  233.      */
  234.     libvlc_media_player_stop(mp);
  235.     libvlc_media_player_release(mp);
  236.     libvlc_release(libvlc);
  237.  
  238.     /*
  239.      * Close window and clean up libSDL
  240.      */
  241.     SDL_DestroyMutex(ctx.mutex);
  242.     SDL_FreeSurface(ctx.surf);
  243.     SDL_FreeSurface(empty);
  244.  
  245.     SDL_Quit();
  246.  
  247.     return 0;
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement