]> git.sesse.net Git - vlc/blobdiff - modules/access/pvr.c
test if access dc1394 is right earlier in the probing process to avoid side effect...
[vlc] / modules / access / pvr.c
index ab80c7f5cedb873a7fab9db917e984bd3553bf4e..13e65cb1b7f12bf323aee4b700eb0ffa0f260fa3 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_access.h>
 
-#include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <sys/ioctl.h>
 #include <sys/poll.h>
 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
-#include VIDEODEV2_H_FILE
+#   ifdef VIDEODEV2_H_FILE
+#   include VIDEODEV2_H_FILE
+#   else
+#   include <linux/videodev2.h>
+#   endif
 #else
 #include "videodev2.h"
 #endif
@@ -106,64 +114,64 @@ static void Close( vlc_object_t * );
 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
     "1 = composite, 2 = svideo)" )
 
-static int i_norm_list[] =
+static const int i_norm_list[] =
     { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
-static const char *psz_norm_list_text[] =
+static const char *const psz_norm_list_text[] =
     { N_("Automatic"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
 
-static int i_bitrates[] = { 0, 1 };
-static const char *psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
+static const int i_bitrates[] = { 0, 1 };
+static const char *const psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
 
-static int pi_radio_range[2] = { 65000, 108000 };
+static const int pi_radio_range[2] = { 65000, 108000 };
 
-vlc_module_begin();
-    set_shortname( _("PVR") );
-    set_description( _("IVTV MPEG Encoding cards input") );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_ACCESS );
-    set_capability( "access2", 0 );
-    add_shortcut( "pvr" );
+vlc_module_begin ()
+    set_shortname( N_("PVR") )
+    set_description( N_("IVTV MPEG Encoding cards input") )
+    set_category( CAT_INPUT )
+    set_subcategory( SUBCAT_INPUT_ACCESS )
+    set_capability( "access", 0 )
+    add_shortcut( "pvr" )
 
     add_integer( "pvr-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
-                 CACHING_LONGTEXT, VLC_TRUE );
+                 CACHING_LONGTEXT, true );
     add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
-                 DEVICE_LONGTEXT, VLC_FALSE );
+                 DEVICE_LONGTEXT, false );
     add_string( "pvr-radio-device", "/dev/radio0", NULL, RADIO_DEVICE_TEXT,
-                 RADIO_DEVICE_LONGTEXT, VLC_FALSE );
+                 RADIO_DEVICE_LONGTEXT, false );
     add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
-                 NORM_LONGTEXT, VLC_FALSE );
-       change_integer_list( i_norm_list, psz_norm_list_text, 0 );
-    add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
+                 NORM_LONGTEXT, false );
+       change_integer_list( i_norm_list, psz_norm_list_text, NULL );
+    add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
     add_integer( "pvr-height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
-                 VLC_TRUE );
+                 true );
     add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
-                 VLC_FALSE );
+                 false );
     add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
-                 VLC_TRUE );
+                 true );
     add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
-                 VLC_TRUE );
+                 true );
     add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
-                 VLC_TRUE );
+                 true );
     add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
-                 VLC_FALSE );
+                 false );
     add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
-                 BITRATE_PEAK_LONGTEXT, VLC_TRUE );
+                 BITRATE_PEAK_LONGTEXT, true );
     add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
-                 BITRATE_MODE_LONGTEXT, VLC_TRUE );
-        change_integer_list( i_bitrates, psz_bitrates_list_text, 0 );
+                 BITRATE_MODE_LONGTEXT, true );
+        change_integer_list( i_bitrates, psz_bitrates_list_text, NULL );
     add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
-                 BITMASK_LONGTEXT, VLC_TRUE );
+                 BITMASK_LONGTEXT, true );
     add_integer( "pvr-audio-volume", -1, NULL, VOLUME_TEXT,
-                 VOLUME_LONGTEXT, VLC_TRUE );
-    add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, VLC_TRUE );
+                 VOLUME_LONGTEXT, true );
+    add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, true )
 
-    set_callbacks( Open, Close );
-vlc_module_end();
+    set_callbacks( Open, Close )
+vlc_module_end ()
 
 /*****************************************************************************
  * Prototypes
  *****************************************************************************/
-static int Read   ( access_t *, uint8_t *, int );
+static ssize_t Read   ( access_t *, uint8_t *, size_t );
 static int Control( access_t *, int, va_list );
 
 /* ivtv specific ioctls */
@@ -215,7 +223,7 @@ struct access_sys_t
     int i_volume;
 
     /* driver version */
-    vlc_bool_t b_v4l2_api;
+    bool b_v4l2_api;
 };
 
 /*****************************************************************************
@@ -539,7 +547,6 @@ static int Open( vlc_object_t * p_this )
     access_sys_t * p_sys;
     char * psz_tofree;
     char * psz_parser;
-    char * psz_device = NULL;
     vlc_value_t val;
     struct v4l2_capability device_capability;
     int result;
@@ -553,7 +560,7 @@ static int Open( vlc_object_t * p_this )
     p_access->info.i_update = 0;
     p_access->info.i_size = 0;
     p_access->info.i_pos = 0;
-    p_access->info.b_eof = VLC_FALSE;
+    p_access->info.b_eof = false;
     p_access->info.i_title = 0;
     p_access->info.i_seekpoint = 0;
 
@@ -636,199 +643,91 @@ static int Open( vlc_object_t * p_this )
     /* parse command line options */
     psz_tofree = strdup( p_access->psz_path );
     if( !psz_tofree )
-        return VLC_ENOMEM;
+        return VLC_ENOMEM; /* <-- FIXME MEMORY LEAK */
 
     psz_parser = psz_tofree;
-    if( *psz_parser )
+    while( *psz_parser )
     {
-        for( ;; )
+        /* Leading slash -> device path */
+        if( *psz_parser == '/' )
         {
-            if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
-            {
-                char *psz_parser_init;
-                psz_parser += strlen( "norm=" );
-                psz_parser_init = psz_parser;
-                while ( (*psz_parser != ':')
-                        && (*psz_parser != ',')
-                        && (*psz_parser != '\0') )
-                {
-                    psz_parser++;
-                }
-
-                if ( !strncmp( psz_parser_init, "secam" ,
-                               psz_parser - psz_parser_init ) )
-                {
-                    p_sys->i_standard = V4L2_STD_SECAM;
-                }
-                else if ( !strncmp( psz_parser_init, "pal" ,
-                                    psz_parser - psz_parser_init ) )
-                {
-                    p_sys->i_standard = V4L2_STD_PAL;
-                }
-                else if ( !strncmp( psz_parser_init, "ntsc" ,
-                                    psz_parser - psz_parser_init ) )
-                {
-                    p_sys->i_standard = V4L2_STD_NTSC;
-                }
-                else
-                {
-                    p_sys->i_standard = strtol( psz_parser_init ,
-                                                &psz_parser, 0 );
-                }
-            }
-            else if( !strncmp( psz_parser, "channel=",
-                               strlen( "channel=" ) ) )
-            {
-                p_sys->i_input =
-                    strtol( psz_parser + strlen( "channel=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
-            {
-                int i_len = strlen( "/dev/videox" );
-                psz_device = calloc( i_len  + 1, 1 );
-                if( !psz_device )
-                    return VLC_ENOMEM;
-
-                snprintf( psz_device, i_len, "/dev/video%ld",
-                            strtol( psz_parser + strlen( "device=" ),
-                            &psz_parser, 0 ) );
-            }
-            else if( !strncmp( psz_parser, "frequency=",
-                               strlen( "frequency=" ) ) )
-            {
-                p_sys->i_frequency =
-                    strtol( psz_parser + strlen( "frequency=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "framerate=",
-                               strlen( "framerate=" ) ) )
-            {
-                p_sys->i_framerate =
-                    strtol( psz_parser + strlen( "framerate=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "keyint=",
-                               strlen( "keyint=" ) ) )
-            {
-                p_sys->i_keyint =
-                    strtol( psz_parser + strlen( "keyint=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "bframes=",
-                               strlen( "bframes=" ) ) )
-            {
-                p_sys->i_bframes =
-                    strtol( psz_parser + strlen( "bframes=" ),
-                            &psz_parser, 0 );
-            }
+            free( p_sys->psz_videodev );
+            p_sys->psz_videodev = strdup( psz_parser );
+            break;
+        }
 
-            else if( !strncmp( psz_parser, "width=",
-                               strlen( "width=" ) ) )
-            {
-                p_sys->i_width =
-                    strtol( psz_parser + strlen( "width=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "height=",
-                               strlen( "height=" ) ) )
-            {
-                p_sys->i_height =
-                    strtol( psz_parser + strlen( "height=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "audio=",
-                               strlen( "audio=" ) ) )
-            {
-                p_sys->i_audio_bitmask =
-                    strtol( psz_parser + strlen( "audio=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "bitrate=",
-                               strlen( "bitrate=" ) ) )
-            {
-                p_sys->i_bitrate =
-                    strtol( psz_parser + strlen( "bitrate=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "maxbitrate=",
-                               strlen( "maxbitrate=" ) ) )
-            {
-                p_sys->i_bitrate_peak =
-                    strtol( psz_parser + strlen( "maxbitrate=" ),
-                            &psz_parser, 0 );
-            }
-            else if( !strncmp( psz_parser, "bitratemode=",
-                               strlen( "bitratemode=" ) ) )
-            {
-                char *psz_parser_init;
-                psz_parser += strlen( "bitratemode=" );
-                psz_parser_init = psz_parser;
-                while ( (*psz_parser != ':')
-                        && (*psz_parser != ',')
-                        && (*psz_parser != '\0') )
-                {
-                    psz_parser++;
-                }
+        /* Extract option name */
+        const char *optname = psz_parser;
+        psz_parser = strchr( psz_parser, '=' );
+        if( psz_parser == NULL )
+            break;
+        *psz_parser++ = '\0';
 
-                if ( !strncmp( psz_parser_init, "vbr" ,
-                               psz_parser - psz_parser_init ) )
-                {
-                     p_sys->i_bitrate_mode = 0;
-                }
-                else if ( !strncmp( psz_parser_init, "cbr" ,
-                                    psz_parser - psz_parser_init ) )
-                {
-                    p_sys->i_bitrate_mode = 1;
-                }
-            }
-            else if( !strncmp( psz_parser, "size=",
-                               strlen( "size=" ) ) )
-            {
-                p_sys->i_width =
-                    strtol( psz_parser + strlen( "size=" ),
-                            &psz_parser, 0 );
-                p_sys->i_height =
-                    strtol( psz_parser + 1 ,
-                            &psz_parser, 0 );
-            }
-            else
-            {
-                char *psz_parser_init;
-                psz_parser_init = psz_parser;
-                while ( (*psz_parser != ':') &&
-                        (*psz_parser != ',') &&
-                        (*psz_parser != '\0') )
-                {
-                    psz_parser++;
-                }
-                psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
-                if( !psz_device )
-                    return VLC_ENOMEM;
+        /* Extract option value */
+        char *optval = psz_parser;
+        while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
+            psz_parser++;
+        if( *psz_parser ) /* more options to come */
+            *psz_parser++ = '\0'; /* skip , or : */
 
-                strncpy( psz_device, psz_parser_init,
-                         psz_parser - psz_parser_init );
-            }
-            if( *psz_parser )
-                psz_parser++;
+        if ( !strcmp( optname, "norm" ) )
+        {
+            if ( !strcmp( optval, "secam" ) )
+                p_sys->i_standard = V4L2_STD_SECAM;
+            else if ( !strcmp( optval, "pal" ) )
+                p_sys->i_standard = V4L2_STD_PAL;
+            else if ( !strcmp( optval, "ntsc" ) )
+                p_sys->i_standard = V4L2_STD_NTSC;
             else
-                break;
+                p_sys->i_standard = atoi( optval );
         }
-    }
-    free( psz_tofree );
-
-    if( psz_device )
-    {
-        if( p_sys->psz_videodev )
+        else if( !strcmp( optname, "channel" ) )
+            p_sys->i_input = atoi( optval );
+        else if( !strcmp( optname, "device" ) )
+        {
             free( p_sys->psz_videodev );
-        p_sys->psz_videodev = psz_device;
+            if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
+                p_sys->psz_videodev = NULL;
+        }
+        else if( !strcmp( optname, "frequency" ) )
+            p_sys->i_frequency = atoi( optval );
+        else if( !strcmp( optname, "framerate" ) )
+            p_sys->i_framerate = atoi( optval );
+        else if( !strcmp( optname, "keyint" ) )
+            p_sys->i_keyint = atoi( optval );
+        else if( !strcmp( optname, "bframes" ) )
+            p_sys->i_bframes = atoi( optval );
+        else if( !strcmp( optname, "width" ) )
+            p_sys->i_width = atoi( optval );
+        else if( !strcmp( optname, "height" ) )
+            p_sys->i_height = atoi( optval );
+        else if( !strcmp( optname, "audio" ) )
+            p_sys->i_audio_bitmask = atoi( optval );
+        else if( !strcmp( optname, "bitrate" ) )
+            p_sys->i_bitrate = atoi( optval );
+        else if( !strcmp( optname, "maxbitrate" ) )
+            p_sys->i_bitrate_peak = atoi( optval );
+        else if( !strcmp( optname, "bitratemode" ) )
+        {
+            if( !strcmp( optval, "vbr" ) )
+                p_sys->i_bitrate_mode = 0;
+            else if( !strcmp( optval, "cbr" ) )
+                p_sys->i_bitrate_mode = 1;
+        }
+        else if( !strcmp( optname, "size" ) )
+        {
+            p_sys->i_width = strtol( optval, &optval, 0 );
+            p_sys->i_height = atoi( optval );
+        }
     }
+    free( psz_tofree );
 
     /* open the device */
     p_sys->i_fd = open( p_sys->psz_videodev, O_RDWR );
     if( p_sys->i_fd < 0 )
     {
-        msg_Err( p_access, "Cannot open device (%s).", strerror( errno ) );
+        msg_Err( p_access, "Cannot open device %s (%m).",
+                 p_sys->psz_videodev );
         Close( VLC_OBJECT(p_access) );
         return VLC_EGENERIC;
     }
@@ -838,25 +737,29 @@ static int Open( vlc_object_t * p_this )
     result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
     if( result < 0 )
     {
-        msg_Err( p_access, "unknown ivtv driver version in use" );
+        msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
         Close( VLC_OBJECT(p_access) );
         return VLC_EGENERIC;
     }
 
-    msg_Dbg( p_access, "ivtv driver version %02x.%02x.%02x",
+    msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
+              device_capability.driver,
+              device_capability.card,
+              device_capability.bus_info,
             ( device_capability.version >> 16 ) & 0xff,
             ( device_capability.version >>  8 ) & 0xff,
             ( device_capability.version       ) & 0xff);
 
-    if ( device_capability.version >= 0x000800 )
+    if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
+           || device_capability.version >= 0x000800 )
     {
         /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
         msg_Dbg( p_access, "this driver uses the v4l2 API" );
-        p_sys->b_v4l2_api = VLC_TRUE;
+        p_sys->b_v4l2_api = true;
     }
     else
     {
-        p_sys->b_v4l2_api = VLC_FALSE;
+        p_sys->b_v4l2_api = false;
     }
 
     /* set the input */
@@ -933,8 +836,7 @@ static int Open( vlc_object_t * p_this )
             p_sys->i_radio_fd = open( p_sys->psz_radiodev, O_RDWR );
             if( p_sys->i_radio_fd < 0 )
             {
-                msg_Err( p_access, "Cannot open radio device (%s).",
-                         strerror( errno ) );
+                msg_Err( p_access, "Cannot open radio device (%m)." );
                 Close( VLC_OBJECT(p_access) );
                 return VLC_EGENERIC;
             }
@@ -951,8 +853,7 @@ static int Open( vlc_object_t * p_this )
         result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
         if ( result < 0 )
         {
-            msg_Warn( p_access, "Failed to read tuner information (%s).",
-                      strerror( errno ) );
+            msg_Warn( p_access, "Failed to read tuner information (%m)." );
         }
         else
         {
@@ -964,8 +865,7 @@ static int Open( vlc_object_t * p_this )
             result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
             if ( result < 0 )
             {
-                msg_Warn( p_access, "Failed to read tuner frequency (%s).",
-                          strerror( errno ) );
+                msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
             }
             else
             {
@@ -977,8 +877,7 @@ static int Open( vlc_object_t * p_this )
                 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
                 if( result < 0 )
                 {
-                    msg_Warn( p_access, "Failed to set tuner frequency (%s).",
-                              strerror( errno ) );
+                    msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
                 }
                 else
                 {
@@ -1054,17 +953,15 @@ static void Close( vlc_object_t * p_this )
         close( p_sys->i_fd );
     if ( p_sys->i_radio_fd != -1 )
         close( p_sys->i_radio_fd );
-    if ( p_sys->psz_videodev )
-        free( p_sys->psz_videodev );
-    if ( p_sys->psz_radiodev )
-        free( p_sys->psz_radiodev );
+    free( p_sys->psz_videodev );
+    free( p_sys->psz_radiodev );
     free( p_sys );
 }
 
 /*****************************************************************************
  * Read
  *****************************************************************************/
-static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
+static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
 {
     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
     struct pollfd ufd;
@@ -1078,7 +975,7 @@ static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
 
     do
     {
-        if( p_access->b_die )
+        if( !vlc_object_alive (p_access) )
             return 0;
 
         ufd.revents = 0;
@@ -1087,14 +984,14 @@ static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
 
     if( i_ret < 0 )
     {
-        msg_Err( p_access, "Select error (%s).", strerror( errno ) );
+        msg_Err( p_access, "Polling error (%m)." );
         return -1;
     }
 
     i_ret = read( p_sys->i_fd, p_buffer, i_len );
     if( i_ret == 0 )
     {
-        p_access->info.b_eof = VLC_TRUE;
+        p_access->info.b_eof = true;
     }
     else if( i_ret > 0 )
     {
@@ -1109,7 +1006,7 @@ static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
  *****************************************************************************/
 static int Control( access_t *p_access, int i_query, va_list args )
 {
-    vlc_bool_t   *pb_bool;
+    bool   *pb_bool;
     int          *pi_int;
     int64_t      *pi_64;
 
@@ -1118,16 +1015,16 @@ static int Control( access_t *p_access, int i_query, va_list args )
         /* */
         case ACCESS_CAN_SEEK:
         case ACCESS_CAN_FASTSEEK:
-            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
-            *pb_bool = VLC_FALSE;
+            pb_bool = (bool*)va_arg( args, bool* );
+            *pb_bool = false;
             break;
         case ACCESS_CAN_PAUSE:
-            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
-            *pb_bool = VLC_FALSE;
+            pb_bool = (bool*)va_arg( args, bool* );
+            *pb_bool = false;
             break;
         case ACCESS_CAN_CONTROL_PACE:
-            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
-            *pb_bool = VLC_FALSE;
+            pb_bool = (bool*)va_arg( args, bool* );
+            *pb_bool = false;
             break;
 
         /* */
@@ -1150,6 +1047,7 @@ static int Control( access_t *p_access, int i_query, va_list args )
         case ACCESS_SET_TITLE:
         case ACCESS_SET_SEEKPOINT:
         case ACCESS_SET_PRIVATE_ID_STATE:
+        case ACCESS_GET_CONTENT_TYPE:
             return VLC_EGENERIC;
 
         default: