[FFmpeg-devel] [PATCH] lavd/sdl: add event handler thread

Stefano Sabatini stefasab at gmail.com
Sun Nov 24 20:12:05 CET 2013


Experimental and partially untested, meant to address ticket #1743 and #1744.
---
 libavdevice/sdl.c | 115 +++++++++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 96 insertions(+), 19 deletions(-)

diff --git a/libavdevice/sdl.c b/libavdevice/sdl.c
index 0210ad2..5043c74 100644
--- a/libavdevice/sdl.c
+++ b/libavdevice/sdl.c
@@ -24,10 +24,13 @@
  */
 
 #include <SDL.h>
+#include <SDL_thread.h>
+
 #include "libavutil/avstring.h"
 #include "libavutil/opt.h"
 #include "libavutil/parseutils.h"
 #include "libavutil/pixdesc.h"
+#include "libavutil/time.h"
 #include "avdevice.h"
 
 typedef struct {
@@ -42,6 +45,11 @@ typedef struct {
     int overlay_x, overlay_y;
     int overlay_fmt;
     int sdl_was_already_inited;
+    SDL_Thread *event_thread;
+    SDL_mutex *mutex;
+    SDL_cond *init_event_cond;
+    int inited;
+    int quit;
 } SDLContext;
 
 static const struct sdl_overlay_pix_fmt_entry {
@@ -57,16 +65,87 @@ static int sdl_write_trailer(AVFormatContext *s)
 {
     SDLContext *sdl = s->priv_data;
 
+    sdl->quit = 1;
+
     if (sdl->overlay) {
         SDL_FreeYUVOverlay(sdl->overlay);
         sdl->overlay = NULL;
     }
+
+    if (sdl->event_thread)
+        SDL_WaitThread(sdl->event_thread, NULL);
+
     if (!sdl->sdl_was_already_inited)
         SDL_Quit();
 
     return 0;
 }
 
+static int event_thread(void *arg)
+{
+    AVFormatContext *s = arg;
+    SDLContext *sdl = s->priv_data;
+    int flags = SDL_SWSURFACE | sdl->window_fullscreen ? SDL_FULLSCREEN : 0;
+    AVStream *st = s->streams[0];
+    AVCodecContext *encctx = st->codec;
+
+    /* initialization */
+    SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
+    sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
+                                    24, flags);
+    if (!sdl->surface) {
+        av_log(sdl, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
+        return AVERROR(EINVAL);
+    }
+
+    sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
+                                        sdl->overlay_fmt, sdl->surface);
+    if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
+        av_log(s, AV_LOG_ERROR,
+               "SDL does not support an overlay with size of %dx%d pixels\n",
+               encctx->width, encctx->height);
+        return AVERROR(EINVAL);
+    }
+
+    av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s -> w:%d h:%d\n",
+           encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt),
+           sdl->overlay_width, sdl->overlay_height);
+
+    SDL_LockMutex(sdl->mutex);
+    sdl->inited = 1;
+    SDL_CondSignal(sdl->init_event_cond);
+    SDL_UnlockMutex(sdl->mutex);
+
+    /* event loop */
+    while (!sdl->quit) {
+        SDL_Event event;
+        SDL_PumpEvents();
+        if (SDL_PeepEvents(&event, 1, SDL_GETEVENT, SDL_ALLEVENTS) <= 0) {
+            /* fixme: make this customizable? */
+            av_usleep(1000.0);
+            continue;
+        }
+
+        switch (event.type) {
+        case SDL_KEYDOWN:
+            switch (event.key.keysym.sym) {
+            case SDLK_ESCAPE:
+            case SDLK_q:
+                sdl->quit = 1;
+                break;
+            }
+            break;
+        case SDL_QUIT:
+            sdl->quit = 1;
+            break;
+        default:
+            break;
+        }
+    }
+
+    return 0;
+}
+
 static int sdl_write_header(AVFormatContext *s)
 {
     SDLContext *sdl = s->priv_data;
@@ -74,7 +153,6 @@ static int sdl_write_header(AVFormatContext *s)
     AVCodecContext *encctx = st->codec;
     AVRational sar, dar; /* sample and display aspect ratios */
     int i, ret;
-    int flags = SDL_SWSURFACE | sdl->window_fullscreen ? SDL_FULLSCREEN : 0;
 
     if (!sdl->window_title)
         sdl->window_title = av_strdup(s->filename);
@@ -148,30 +226,24 @@ static int sdl_write_header(AVFormatContext *s)
     sdl->overlay_x = (sdl->window_width  - sdl->overlay_width ) / 2;
     sdl->overlay_y = (sdl->window_height - sdl->overlay_height) / 2;
 
-    SDL_WM_SetCaption(sdl->window_title, sdl->icon_title);
-    sdl->surface = SDL_SetVideoMode(sdl->window_width, sdl->window_height,
-                                    24, flags);
-    if (!sdl->surface) {
-        av_log(s, AV_LOG_ERROR, "Unable to set video mode: %s\n", SDL_GetError());
-        ret = AVERROR(EINVAL);
+    sdl->init_event_cond = SDL_CreateCond();
+    sdl->mutex = SDL_CreateMutex();
+
+    sdl->event_thread = SDL_CreateThread(event_thread, s);
+    if (!sdl->event_thread) {
+        av_log(s, AV_LOG_ERROR, "Could not create SDL event thread: %s\n", SDL_GetError());
+        ret = AVERROR_EXTERNAL;
         goto fail;
     }
 
-    sdl->overlay = SDL_CreateYUVOverlay(encctx->width, encctx->height,
-                                        sdl->overlay_fmt, sdl->surface);
-    if (!sdl->overlay || sdl->overlay->pitches[0] < encctx->width) {
-        av_log(s, AV_LOG_ERROR,
-               "SDL does not support an overlay with size of %dx%d pixels\n",
-               encctx->width, encctx->height);
-        ret = AVERROR(EINVAL);
-        goto fail;
+    /* wait until the video system has been inited */
+    SDL_LockMutex(sdl->mutex);
+    if (!sdl->inited) {
+        SDL_CondWait(sdl->init_event_cond, sdl->mutex);
     }
+    SDL_UnlockMutex(sdl->mutex);
 
-    av_log(s, AV_LOG_VERBOSE, "w:%d h:%d fmt:%s sar:%d/%d -> w:%d h:%d\n",
-           encctx->width, encctx->height, av_get_pix_fmt_name(encctx->pix_fmt), sar.num, sar.den,
-           sdl->overlay_width, sdl->overlay_height);
     return 0;
-
 fail:
     sdl_write_trailer(s);
     return ret;
@@ -185,10 +257,15 @@ static int sdl_write_packet(AVFormatContext *s, AVPacket *pkt)
     AVPicture pict;
     int i;
 
+    if (sdl->quit)
+        return AVERROR_EOF;
     avpicture_fill(&pict, pkt->data, encctx->pix_fmt, encctx->width, encctx->height);
 
+    SDL_LockMutex(sdl->mutex);
     SDL_FillRect(sdl->surface, &sdl->surface->clip_rect,
                  SDL_MapRGB(sdl->surface->format, 0, 0, 0));
+    SDL_UnlockMutex(sdl->mutex);
+
     SDL_LockYUVOverlay(sdl->overlay);
     for (i = 0; i < 3; i++) {
         sdl->overlay->pixels [i] = pict.data    [i];
-- 
1.8.1.2



More information about the ffmpeg-devel mailing list