Index: libs/libmythtv/NuppelVideoPlayer.cpp
===================================================================
--- libs/libmythtv/NuppelVideoPlayer.cpp	(revision 8742)
+++ libs/libmythtv/NuppelVideoPlayer.cpp	(working copy)
@@ -367,6 +367,7 @@
 
 bool NuppelVideoPlayer::InitVideo(void)
 {
+    VERBOSE(VB_RECORD, "InitVideo()");
     InitFilters();
     if (using_null_videoout)
     {
@@ -447,6 +448,7 @@
 
 void NuppelVideoPlayer::ReinitVideo(void)
 {
+    VERBOSE(VB_RECORD, "ReinitVideo()");
     InitFilters();
 
     vidExitLock.lock();
Index: libs/libmythtv/videoout_xv.cpp
===================================================================
--- libs/libmythtv/videoout_xv.cpp	(revision 8742)
+++ libs/libmythtv/videoout_xv.cpp	(working copy)
@@ -27,6 +27,7 @@
 #include "XvMCSurfaceTypes.h"
 #include "util-x11.h"
 #include "config.h"
+#include "exitcodes.h"
 
 #define LOC QString("VideoOutputXv: ")
 #define LOC_ERR QString("VideoOutputXv Error: ")
@@ -48,7 +49,54 @@
 #define round(x) ((int) ((x) + 0.5))
 #endif
 
+class port_info { public: Display *disp; int port; };
+static QMap<int,port_info> open_xv_ports;
 
+static void close_all_xv_ports_signal_handler(int sig)
+{
+    cerr<<"Signal: "<<sys_siglist[sig]<<endl;
+    QMap<int,port_info>::iterator it;
+    for (it = open_xv_ports.begin(); it != open_xv_ports.end(); ++it)
+    {
+        cerr<<"Ungrabbing XVideo port: "<<(*it).port<<endl;
+        XvUngrabPort((*it).disp, (*it).port, CurrentTime);
+    }
+    exit(GENERIC_EXIT_NOT_OK);
+}
+static void add_open_xv_port(Display *disp, int port)
+{
+    if (port >= 0)
+    {
+        open_xv_ports[port].disp = disp;
+        open_xv_ports[port].port = port;
+        //signal(SIGSTOP, close_all_xv_ports_signal_handler);
+        //signal(SIGABRT, close_all_xv_ports_signal_handler);
+        signal(SIGINT,  close_all_xv_ports_signal_handler);
+    }
+}
+static void del_open_xv_port(int port)
+{
+    if (port >= 0)
+    {
+        open_xv_ports.remove(port);
+
+        if (!open_xv_ports.count())
+        {
+            //signal(SIGSTOP, SIG_DFL);
+            //signal(SIGABRT, SIG_DFL);
+            signal(SIGHUP, SIG_DFL);
+        }
+    }
+}
+static bool has_open_xv_port(int port)
+{
+    return open_xv_ports.find(port) != open_xv_ports.end();
+}
+static uint cnt_open_xv_port(void)
+{
+    return open_xv_ports.count();
+}
+
 //#define DEBUG_PAUSE /* enable to debug XvMC pause frame */
 
 #ifdef USING_XVMC
@@ -245,7 +293,11 @@
     // ungrab port...
     if (xv_port >= 0)
     {
-        X11S(XvUngrabPort(XJ_disp, xv_port, CurrentTime));
+        VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << xv_port);
+        X11L;
+        XvUngrabPort(XJ_disp, xv_port, CurrentTime);
+        del_open_xv_port(xv_port);
+        X11U;
         xv_port = -1;
     }
 
@@ -631,22 +683,38 @@
                 if (!surf.size())
                     continue;
 
-                X11S(ret = XvGrabPort(disp, p, CurrentTime));
+                X11L;
+                ret = XvGrabPort(disp, p, CurrentTime);
+                if (Success == ret)
+                {
+                    VERBOSE(VB_PLAYBACK, "Grabbed xv port "<<p);
+                    port = p;
+                    add_open_xv_port(disp, p);
+                }
+                X11U;
                 if (Success != ret)
+                {
+                    VERBOSE(VB_PLAYBACK, "Failed to grab xv port "<<p);
                     continue;
+                }
                 
                 if (xvmc_surf_info)
                     surf.set(surfNum, xvmc_surf_info);
-                port = p;
 #endif // USING_XVMC
             }
             else
             {
                 for (p = firstPort; (p <= lastPort) && (port == -1); ++p)
                 {
-                    X11S(ret = XvGrabPort(disp, p, CurrentTime));
+                    X11L;
+                    ret = XvGrabPort(disp, p, CurrentTime);
                     if (Success == ret)
+                    {
+                        VERBOSE(VB_PLAYBACK, "Grabbed xv port "<<p);
                         port = p;
+                        add_open_xv_port(disp, p);
+                    }
+                    X11U;
                 }
             }
         }
@@ -785,6 +853,7 @@
  */
 bool VideoOutputXv::InitXvMC(MythCodecID mcodecid)
 {
+    VERBOSE(VB_IMPORTANT, "InitXvMC() -- begin");
     (void)mcodecid;
 #ifdef USING_XVMC
     xv_port = GrabSuitableXvPort(XJ_disp, XJ_root, mcodecid,
@@ -826,10 +895,15 @@
             delete xvmc_osd_available[i];
         xvmc_osd_available.clear();
         xvmc_osd_lock.unlock();
-        X11S(XvUngrabPort(XJ_disp, xv_port, CurrentTime));
+        VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << xv_port);
+        X11L;
+        XvUngrabPort(XJ_disp, xv_port, CurrentTime);
+        del_open_xv_port(xv_port);
+        X11U;
         xv_port = -1;
     }
 
+    VERBOSE(VB_IMPORTANT, "InitXvMC() -- end ok("<<ok<<")");
     return ok;
 #else // USING_XVMC
     return false;
@@ -846,6 +920,7 @@
  */
 bool VideoOutputXv::InitXVideo()
 {
+    VERBOSE(VB_IMPORTANT, "InitXVideo() -- begin");
     xv_port = GrabSuitableXvPort(XJ_disp, XJ_root, kCodec_MPEG2,
                                  XJ_width, XJ_height);
     if (xv_port == -1)
@@ -903,7 +978,11 @@
     {
         VERBOSE(VB_IMPORTANT, LOC_ERR +
                 "Couldn't find the proper XVideo image format.");
-        X11S(XvUngrabPort(XJ_disp, xv_port, CurrentTime));
+        VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << xv_port);
+        X11L;
+        XvUngrabPort(XJ_disp, xv_port, CurrentTime);
+        del_open_xv_port(xv_port);
+        X11U;
         xv_port = -1;
     }
 
@@ -916,13 +995,18 @@
     {
         VERBOSE(VB_IMPORTANT, LOC_ERR + "Failed to create XVideo Buffers.");
         DeleteBuffers(XVideo, false);
-        X11S(XvUngrabPort(XJ_disp, xv_port, CurrentTime));
+        VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << xv_port);
+        X11L;
+        XvUngrabPort(XJ_disp, xv_port, CurrentTime);
+        del_open_xv_port(xv_port);
+        X11U;
         xv_port = -1;
         ok = false;
     }
     else
         video_output_subtype = XVideo;
     
+    VERBOSE(VB_IMPORTANT, "InitXVideo() -- end ok("<<ok<<")");
     return ok;
 }
 
@@ -1003,6 +1087,7 @@
     (void)stream_type, (void)xvmc_chroma, (void)test_surface;
 
 #ifdef USING_XVMC
+    VERBOSE(VB_IMPORTANT, "GetBestSupportedCodec() -- begin");
     Display *disp;
     X11S(disp = XOpenDisplay(NULL));
 
@@ -1056,10 +1141,15 @@
                                   width, height);
             ok = NULL != ctx;
             DeleteXvMCContext(disp, ctx);
-            X11S(XvUngrabPort(disp, port, CurrentTime));
+            VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << port);
+            X11L;
+            XvUngrabPort(disp, port, CurrentTime);
+            del_open_xv_port(port);
+            X11U;
         }
     }
     X11S(XCloseDisplay(disp));
+    X11S(ok |= cnt_open_xv_port() > 0); // also ok if we already opened port..
 
     if (!ok)
     {
@@ -1078,6 +1168,7 @@
         ret = (MythCodecID)(kCodec_MPEG1 + (stream_type-1));
     }
 
+    VERBOSE(VB_IMPORTANT, "GetBestSupportedCodec() -- end");
     return ret;
 #else // if !USING_XVMC
     return (MythCodecID)(kCodec_MPEG1 + (stream_type-1));
@@ -1182,7 +1273,11 @@
         DeleteBuffers(VideoOutputSubType(), true);
         if (xv_port >= 0)
         {
-            X11S(XvUngrabPort(XJ_disp, xv_port, CurrentTime));
+            VERBOSE(VB_PLAYBACK, LOC + "Closing XVideo port " << xv_port);
+            X11L;
+            XvUngrabPort(XJ_disp, xv_port, CurrentTime);
+            del_open_xv_port(xv_port);
+            X11U;
             xv_port = -1;
         }
 #endif // USING_XVMC
