]> git.sesse.net Git - vlc/blobdiff - modules/access/dvdnav.c
Merge branch '1.0-bugfix'
[vlc] / modules / access / dvdnav.c
index cc3450aac61b1be196a5eeb8a2c0325f60b5db6f..0f805f48a8f602ee0406d7810fccd4930cc7847b 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * dvdnav.c: DVD module using the dvdnav library.
  *****************************************************************************
- * Copyright (C) 2004 the VideoLAN team
+ * Copyright (C) 2004-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -29,6 +29,7 @@
 # include "config.h"
 #endif
 
+#include <assert.h>
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_input.h>
@@ -107,24 +108,6 @@ vlc_module_end ()
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-typedef struct
-{
-    VLC_COMMON_MEMBERS
-
-    demux_t        *p_demux;
-    vlc_mutex_t     lock;
-
-    bool      b_moved;
-    bool      b_clicked;
-    int             i_key_action;
-
-    bool      b_still;
-    int64_t         i_still_end;
-
-} event_thread_t;
-
-static void* EventThread( vlc_object_t * );
-
 struct demux_sys_t
 {
     dvdnav_t    *dvdnav;
@@ -132,6 +115,16 @@ struct demux_sys_t
     /* */
     bool        b_reset_pcr;
 
+    struct
+    {
+        bool         b_created;
+        bool         b_enabled;
+        mtime_t      i_end;
+        vlc_mutex_t  lock;
+        vlc_cond_t   wait;
+        vlc_thread_t thread;
+    } still;
+
     /* track */
     ps_track_t  tk[PS_TK_COUNT];
     int         i_mux_rate;
@@ -140,7 +133,7 @@ struct demux_sys_t
     input_thread_t *p_input;
 
     /* event */
-    event_thread_t *p_ev;
+    vlc_object_t *p_vout;
 
     /* palette for menus */
     uint32_t clut[16];
@@ -172,8 +165,14 @@ static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
 
 static int ControlInternal( demux_t *, int, ... );
 
+static void* StillThread( void * );
+
 static int EventKey( vlc_object_t *, char const *,
                      vlc_value_t, vlc_value_t, void * );
+static int EventMouse( vlc_object_t *, char const *,
+                       vlc_value_t, vlc_value_t, void * );
+static int EventIntf( vlc_object_t *, char const *,
+                      vlc_value_t, vlc_value_t, void * );
 
 /*****************************************************************************
  * DemuxOpen:
@@ -352,21 +351,13 @@ static int Open( vlc_object_t *p_this )
     var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
     var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
 
-    /* Now create our event thread catcher */
-    p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
-    p_sys->p_ev->p_demux = p_demux;
-    vlc_mutex_init( &p_sys->p_ev->lock );
-    p_sys->p_ev->b_moved   = false;
-    p_sys->p_ev->b_clicked = false;
-    p_sys->p_ev->i_key_action = 0;
-    p_sys->p_ev->b_still   = false;
-
     /* catch all key event */
-    var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_sys->p_ev );
-
-    vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
-                       VLC_THREAD_PRIORITY_LOW );
+    var_AddCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
+    /* catch vout creation event */
+    var_AddCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
 
+    p_sys->still.b_enabled = false;
+    vlc_mutex_init( &p_sys->still.lock );
     return VLC_SUCCESS;
 }
 
@@ -379,12 +370,26 @@ static void Close( vlc_object_t *p_this )
     demux_sys_t *p_sys = p_demux->p_sys;
     int i;
 
-    /* stop the event handler */
-    vlc_object_kill( p_sys->p_ev );
-    var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_sys->p_ev );
-    vlc_thread_join( p_sys->p_ev );
-    vlc_mutex_destroy( &p_sys->p_ev->lock );
-    vlc_object_release( p_sys->p_ev );
+    /* Stop vout event handler */
+    var_DelCallback( p_sys->p_input, "intf-event", EventIntf, p_demux );
+    if( p_sys->p_vout != NULL )
+    {   /* Should not happen, but better be safe than sorry. */
+        msg_Warn( p_sys->p_vout, "removing dangling mouse DVD callbacks" );
+        var_DelCallback( p_sys->p_vout, "mouse-moved", EventMouse, p_demux );
+        var_DelCallback( p_sys->p_vout, "mouse-clicked", EventMouse, p_demux );
+    }
+
+    /* Stop key event handler (FIXME: should really be per-vout too) */
+    var_DelCallback( p_demux->p_libvlc, "key-action", EventKey, p_demux );
+
+    /* Stop still image handler */
+    if( p_sys->still.b_created )
+    {
+        vlc_cancel( p_sys->still.thread );
+        vlc_join( p_sys->still.thread, NULL );
+        vlc_cond_destroy( &p_sys->still.wait );
+    }
+    vlc_mutex_destroy( &p_sys->still.lock );
 
     var_Destroy( p_sys->p_input, "highlight-mutex" );
     var_Destroy( p_sys->p_input, "highlight" );
@@ -635,7 +640,9 @@ static int Demux( demux_t *p_demux )
     switch( i_event )
     {
     case DVDNAV_BLOCK_OK:   /* mpeg block */
-        p_sys->p_ev->b_still = false;
+        vlc_mutex_lock( &p_sys->still.lock );
+        p_sys->still.b_enabled = false;
+        vlc_mutex_unlock( &p_sys->still.lock );
         if( p_sys->b_reset_pcr )
         {
             es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
@@ -653,20 +660,31 @@ static int Demux( demux_t *p_demux )
         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
         bool b_still_init = false;
 
-        vlc_mutex_lock( &p_sys->p_ev->lock );
-        if( !p_sys->p_ev->b_still )
+        vlc_mutex_lock( &p_sys->still.lock );
+        if( !p_sys->still.b_enabled )
         {
             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
             msg_Dbg( p_demux, "     - length=0x%x", event->length );
-            p_sys->p_ev->b_still = true;
+            /* FIXME: use vlc_timer_create() if it is ever invented */
+            p_sys->still.b_enabled = true;
+
+            if( !p_sys->still.b_created )
+            {
+                vlc_cond_init( &p_sys->still.wait );
+                p_sys->still.b_created =
+                    !vlc_clone( &p_sys->still.thread, StillThread, p_sys,
+                                VLC_THREAD_PRIORITY_LOW );
+            }
+
             b_still_init = true;
             if( event->length == 0xff )
-                p_sys->p_ev->i_still_end = 0;
+                p_sys->still.i_end = 0;
             else
-                p_sys->p_ev->i_still_end = mdate() +
-                                           event->length * INT64_C(1000000);
+                p_sys->still.i_end = mdate() +
+                                     event->length * INT64_C(1000000);
+            vlc_cond_signal( &p_sys->still.wait );
         }
-        vlc_mutex_unlock( &p_sys->p_ev->lock );
+        vlc_mutex_unlock( &p_sys->still.lock );
 
         if( b_still_init )
         {
@@ -1294,151 +1312,118 @@ static void ESNew( demux_t *p_demux, int i_id )
 /*****************************************************************************
  * Event handler code
  *****************************************************************************/
-static int  EventMouse( vlc_object_t *, char const *,
-                        vlc_value_t, vlc_value_t, void * );
-
-static void* EventThread( vlc_object_t *p_this )
+static void* StillThread( void *p_data )
 {
-    event_thread_t *p_ev = (event_thread_t*)p_this;
-    demux_sys_t    *p_sys = p_ev->p_demux->p_sys;
-    vlc_object_t   *p_vout = NULL;
-    int canc = vlc_savecancel ();
+    demux_sys_t    *p_sys = p_data;
 
-    /* main loop */
-    while( vlc_object_alive( p_ev ) )
+    vlc_mutex_lock( &p_sys->still.lock );
+    mutex_cleanup_push( &p_sys->still.lock );
+    for( ;; )
     {
-        bool b_activated = false;
-
-        /* KEY part */
-        if( p_ev->i_key_action != 0 )
+        if( p_sys->still.b_enabled && p_sys->still.i_end )
         {
-            pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
-
-            vlc_mutex_lock( &p_ev->lock );
-            switch( p_ev->i_key_action )
-            {
-            case ACTIONID_NAV_LEFT:
-                dvdnav_left_button_select( p_sys->dvdnav, pci );
-                break;
-            case ACTIONID_NAV_RIGHT:
-                dvdnav_right_button_select( p_sys->dvdnav, pci );
-                break;
-            case ACTIONID_NAV_UP:
-                dvdnav_upper_button_select( p_sys->dvdnav, pci );
-                break;
-            case ACTIONID_NAV_DOWN:
-                dvdnav_lower_button_select( p_sys->dvdnav, pci );
-                break;
-            case ACTIONID_NAV_ACTIVATE:
-                b_activated = true;
-                ButtonUpdate( p_ev->p_demux, true );
-                dvdnav_button_activate( p_sys->dvdnav, pci );
-                break;
-            default:
-                break;
+            if( vlc_cond_timedwait( &p_sys->still.wait, &p_sys->still.lock,
+                                    p_sys->still.i_end ) )
+            {   /* Still image time out */
+                int canc = vlc_savecancel();
+                p_sys->still.b_enabled = false;
+                dvdnav_still_skip( p_sys->dvdnav );
+                vlc_restorecancel( canc );
             }
-            p_ev->i_key_action = 0;
-            vlc_mutex_unlock( &p_ev->lock );
         }
+        else
+            vlc_cond_wait( &p_sys->still.wait, &p_sys->still.lock );
+    }
+    vlc_cleanup_pop(  );
 
-        /* VOUT part */
-        if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
-        {
-            pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
-            vlc_value_t valx, valy;
-
-            vlc_mutex_lock( &p_ev->lock );
-            var_Get( p_vout, "mouse-x", &valx );
-            var_Get( p_vout, "mouse-y", &valy );
-
-            if( p_ev->b_moved )
-            {
-                dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
-                                     valy.i_int );
-            }
-            if( p_ev->b_clicked )
-            {
-                b_activated = true;
-                ButtonUpdate( p_ev->p_demux, true );
-                dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
-                                       valy.i_int );
-            }
-
-            p_ev->b_moved = false;
-            p_ev->b_clicked = false;
-            vlc_mutex_unlock( &p_ev->lock );
-        }
-        if( p_vout && !vlc_object_alive (p_vout) )
-        {
-            var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
-            var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
-            vlc_object_release( p_vout );
-            p_vout = NULL;
-        }
-        if( p_vout == NULL )
-        {
-            p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
-                                      FIND_CHILD );
-            if( p_vout)
-            {
-                var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
-                var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
-            }
-        }
+    assert( 0 );
+}
 
-        /* Still part */
-        vlc_mutex_lock( &p_ev->lock );
-        if( p_ev->b_still )
-        {
-            if( /* b_activated || // This breaks menus */
-                ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
-            {
-                p_ev->b_still = false;
-                dvdnav_still_skip( p_sys->dvdnav );
-            }
-        }
-        vlc_mutex_unlock( &p_ev->lock );
+static int EventMouse( vlc_object_t *p_vout, char const *psz_var,
+                       vlc_value_t oldval, vlc_value_t val, void *p_data )
+{
+    demux_t *p_demux = p_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
 
-        /* Wait a bit */
-        msleep( 10000 );
-    }
+    /* FIXME? PCI usage thread safe? */
+    pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
+    int x = var_GetInteger( p_vout, "mouse-x" );
+    int y = var_GetInteger( p_vout, "mouse-y" );
 
-    /* Release callback */
-    if( p_vout )
+    if( psz_var[6] == 'm' ) /* mouse-moved */
+        dvdnav_mouse_select( p_sys->dvdnav, pci, x, y );
+    else
     {
-        var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
-        var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
-        vlc_object_release( p_vout );
+        assert( psz_var[6] == 'c' ); /* mouse-clicked */
+
+        ButtonUpdate( p_demux, true );
+        dvdnav_mouse_activate( p_sys->dvdnav, pci, x, y );
     }
-    vlc_restorecancel (canc);
 
-    return NULL;
+    (void)oldval; (void)val;
+    return VLC_SUCCESS;
 }
 
-static int EventMouse( vlc_object_t *p_this, char const *psz_var,
-                       vlc_value_t oldval, vlc_value_t newval, void *p_data )
+static int EventKey( vlc_object_t *p_this, char const *psz_var,
+                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
 {
-    (void)p_this;    (void)oldval;    (void)newval;    (void)p_data;
-    event_thread_t *p_ev = p_data;
-    vlc_mutex_lock( &p_ev->lock );
-    if( psz_var[6] == 'c' )
-        p_ev->b_clicked = true;
-    else if( psz_var[6] == 'm' )
-        p_ev->b_moved = true;
-    vlc_mutex_unlock( &p_ev->lock );
+    demux_t *p_demux = p_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    /* FIXME: thread-safe ? */
+    pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
+
+    switch( newval.i_int )
+    {
+    case ACTIONID_NAV_LEFT:
+        dvdnav_left_button_select( p_sys->dvdnav, pci );
+        break;
+    case ACTIONID_NAV_RIGHT:
+        dvdnav_right_button_select( p_sys->dvdnav, pci );
+        break;
+    case ACTIONID_NAV_UP:
+        dvdnav_upper_button_select( p_sys->dvdnav, pci );
+        break;
+    case ACTIONID_NAV_DOWN:
+        dvdnav_lower_button_select( p_sys->dvdnav, pci );
+        break;
+    case ACTIONID_NAV_ACTIVATE:
+        ButtonUpdate( p_demux, true );
+        dvdnav_button_activate( p_sys->dvdnav, pci );
+        break;
+    }
 
+    (void)p_this;    (void)psz_var;    (void)oldval;
     return VLC_SUCCESS;
 }
 
-static int EventKey( vlc_object_t *p_this, char const *psz_var,
-                     vlc_value_t oldval, vlc_value_t newval, void *p_data )
+static int EventIntf( vlc_object_t *p_input, char const *psz_var,
+                      vlc_value_t oldval, vlc_value_t val, void *p_data )
 {
-    (void)p_this;    (void)psz_var;    (void)oldval;
-    event_thread_t *p_ev = p_data;
-    vlc_mutex_lock( &p_ev->lock );
-    p_ev->i_key_action = newval.i_int;
-    vlc_mutex_unlock( &p_ev->lock );
+    demux_t *p_demux = p_data;
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    if (val.i_int == INPUT_EVENT_VOUT)
+    {
+        vlc_object_t *p_vout;
+
+        p_vout = p_sys->p_vout;
+        if( p_vout != NULL )
+        {
+            var_DelCallback( p_vout, "mouse-moved", EventMouse, p_demux );
+            var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
+            vlc_object_release( p_vout );
+        }
 
+        p_vout = (vlc_object_t *)input_GetVout( (input_thread_t *)p_input );
+        p_sys->p_vout = p_vout;
+        if( p_vout != NULL )
+        {
+            var_AddCallback( p_vout, "mouse-moved", EventMouse, p_demux );
+            var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_demux );
+        }
+    }
+    (void) psz_var; (void) oldval;
     return VLC_SUCCESS;
 }