]> git.sesse.net Git - vlc/blobdiff - src/input/input.c
* Coding style fixes here and there.
[vlc] / src / input / input.c
index c01615b679dcc57cb98ff5d850c0bf8974b6a946..e923239accfdf8b4d4694d845651e925b7208e91 100644 (file)
@@ -4,7 +4,7 @@
  * decoders.
  *****************************************************************************
  * Copyright (C) 1998, 1999, 2000 VideoLAN
- * $Id: input.c,v 1.92 2001/03/11 19:00:18 henri Exp $
+ * $Id: input.c,v 1.102 2001/04/28 03:36:25 sam Exp $
  *
  * Authors: Christophe Massiot <massiot@via.ecp.fr>
  *
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
+#ifdef STRNCASECMP_IN_STRINGS_H
+#   include <strings.h>
+#endif
 #include <errno.h>
 
 /* Network functions */
 
-#ifndef SYS_BEOS
-#include <netdb.h>                                             /* hostent ... */
+#if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
+#include <netdb.h>                                            /* hostent ... */
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "common.h"
 #include "threads.h"
 #include "mtime.h"
+#include "netutils.h"
 #include "modules.h"
 
 #include "intf_msg.h"
-#include "intf_plst.h"
+#include "intf_playlist.h"
 
 #include "stream_control.h"
 #include "input_ext-intf.h"
@@ -125,12 +129,13 @@ input_thread_t *input_CreateThread ( playlist_item_t *p_item, int *pi_status )
     p_input->stream.i_new_status = p_input->stream.i_new_rate = 0;
     p_input->stream.i_mux_rate = 0;
 
+    /* no stream, no area */
     p_input->stream.i_area_nb = 0;
     p_input->stream.pp_areas = NULL;
+    p_input->stream.p_selected_area = NULL;
     /* By default there is one areas in a stream */
     input_AddArea( p_input );
     p_input->stream.p_selected_area = p_input->stream.pp_areas[0];
-    p_input->stream.p_selected_area->i_seek = NO_SEEK;
 
     /* Initialize stream control properties. */
     p_input->stream.control.i_status = PLAYING_S;
@@ -223,6 +228,11 @@ static void RunThread( input_thread_t *p_input )
         return;
     }
 
+    /* initialization is completed */
+    vlc_mutex_lock( &p_input->stream.stream_lock );
+    p_input->stream.b_changed = 1;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
     while( !p_input->b_die && !p_input->b_error && !p_input->b_eof )
     {
         data_packet_t *         pp_packets[p_input->i_read_once];
@@ -232,6 +242,7 @@ static void RunThread( input_thread_t *p_input )
 #endif
 
         vlc_mutex_lock( &p_input->stream.stream_lock );
+
         if( p_input->stream.p_selected_area->i_seek != NO_SEEK )
         {
             if( p_input->stream.b_seekable && p_input->pf_seek != NULL )
@@ -253,6 +264,7 @@ static void RunThread( input_thread_t *p_input )
             }
             p_input->stream.p_selected_area->i_seek = NO_SEEK;
         }
+
         vlc_mutex_unlock( &p_input->stream.stream_lock );
 
         i_error = p_input->pf_read( p_input, pp_packets );
@@ -269,7 +281,7 @@ static void RunThread( input_thread_t *p_input )
             {
                 /* End of file - we do not set b_die because only the
                  * interface is allowed to do so. */
-                intf_WarnMsg( 1, "End of file reached" );
+                intf_WarnMsg( 1, "input: EOF reached" );
                 p_input->b_eof = 1;
             }
             else
@@ -288,7 +300,7 @@ static void RunThread( input_thread_t *p_input )
 
     DestroyThread( p_input );
 
-    intf_DbgMsg("Thread end");
+    intf_DbgMsg("input: Thread end");
 }
 
 /*****************************************************************************
@@ -312,7 +324,8 @@ static int InitThread( input_thread_t * p_input )
 
     if( p_input->p_input_module == NULL )
     {
-        intf_ErrMsg( "input error: no suitable input module" );
+        intf_ErrMsg( "input error: no suitable input module for `%s'",
+                     p_input->p_source );
         return( -1 );
     }
 
@@ -343,6 +356,14 @@ static int InitThread( input_thread_t * p_input )
 
     p_input->pf_init( p_input );
 
+    if( p_input->b_error )
+    {
+        /* We barfed -- exit nicely */
+        p_input->pf_close( p_input );
+        module_Unneed( p_main->p_bank, p_input->p_input_module );
+        return( -1 );
+    }
+
     *p_input->pi_status = THREAD_READY;
 
     return( 0 );
@@ -470,7 +491,7 @@ void input_FileOpen( input_thread_t * p_input )
         p_input->stream.p_selected_area->i_size = stat_info.st_size;
     }
     else if( S_ISFIFO(stat_info.st_mode)
-#ifndef SYS_BEOS
+#if !defined( SYS_BEOS ) && !defined( WIN32 )
              || S_ISSOCK(stat_info.st_mode)
 #endif
              )
@@ -490,9 +511,14 @@ void input_FileOpen( input_thread_t * p_input )
     p_input->stream.p_selected_area->i_tell = 0;
     vlc_mutex_unlock( &p_input->stream.stream_lock );
 
-    intf_Msg( "input: opening %s", p_input->p_source );
+    intf_Msg( "input: opening file `%s'", p_input->p_source );
+#ifndef WIN32
     if( (p_input->i_handle = open( psz_name,
                                    /*O_NONBLOCK | O_LARGEFILE*/0 )) == (-1) )
+#else
+    if( (p_input->i_handle = open( psz_name, O_BINARY
+                                   /*O_NONBLOCK | O_LARGEFILE*/ )) == (-1) )
+#endif
     {
         intf_ErrMsg( "input error: cannot open file (%s)", strerror(errno) );
         p_input->b_error = 1;
@@ -506,30 +532,83 @@ void input_FileOpen( input_thread_t * p_input )
  *****************************************************************************/
 void input_FileClose( input_thread_t * p_input )
 {
+    intf_Msg( "input: closing file `%s'", p_input->p_source );
     close( p_input->i_handle );
 
     return;
 }
 
 
-#ifndef SYS_BEOS
+#if !defined( SYS_BEOS ) && !defined( SYS_NTO ) && !defined( WIN32 )
 /*****************************************************************************
  * input_NetworkOpen : open a network socket 
  *****************************************************************************/
 void input_NetworkOpen( input_thread_t * p_input )
 {
-    int                 i_option_value, i_port;
-    struct sockaddr_in  s_socket;
-    boolean_t           b_broadcast;
+    char                *psz_server = NULL;
+    int                 i_port = 0;
+    int                 i_opt;
+    struct sockaddr_in  sock;
+    char *              psz_broadcast;
     
-    /* FIXME : we don't handle channels for the moment */
+    /* Are we broadcasting ? */
+    psz_broadcast = main_GetPszVariable( INPUT_BROADCAST_VAR, NULL );
     
     /* Get the remote server */
-    if( p_input->p_source == NULL )
+    if( p_input->p_source != NULL )
     {
-        p_input->p_source = main_GetPszVariable( INPUT_SERVER_VAR, 
-                                                 INPUT_SERVER_DEFAULT );
+        psz_server = p_input->p_source;
+
+        /* Skip the protocol name */
+        while( *psz_server && *psz_server != ':' )
+        {
+            psz_server++;
+        }
+
+        /* Skip the "://" part */
+        while( *psz_server && (*psz_server == ':' || *psz_server == '/') )
+        {
+            psz_server++;
+        }
+
+        /* Found a server name */
+        if( *psz_server )
+        {
+            char *psz_port = psz_server;
+
+            /* Skip the hostname part */
+            while( *psz_port && *psz_port != ':' )
+            {
+                psz_port++;
+            }
+
+            /* Found a port name */
+            if( *psz_port )
+            {
+                /* Replace ':' with '\0' */
+                *psz_port = '\0';
+                psz_port++;
+
+                i_port = atoi( psz_port );
+            }
+        }
+        else
+        {
+            psz_server = NULL;
+        }
+    }
+
+    /* Check that we got a valid server */
+    if( psz_server == NULL )
+    {
+        psz_server = main_GetPszVariable( INPUT_SERVER_VAR, 
+                                          INPUT_SERVER_DEFAULT );
+    }
+
+    /* Check that we got a valid port */
+    if( i_port == 0 )
+    {
+        i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
     }
     
     /* Open a SOCK_DGRAM (UDP) socket, in the AF_INET domain, automatic (0)
@@ -537,17 +616,17 @@ void input_NetworkOpen( input_thread_t * p_input )
     p_input->i_handle = socket( AF_INET, SOCK_DGRAM, 0 );
     if( p_input->i_handle == -1 )
     {
-        intf_ErrMsg("NetworkOpen : can't create socket : %s", strerror(errno));
+        intf_ErrMsg("input error: can't create socket : %s", strerror(errno));
         p_input->b_error = 1;
         return;
     }
 
     /* We may want to reuse an already used socket */
-    i_option_value = 1;
+    i_opt = 1;
     if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_REUSEADDR,
-                    &i_option_value,sizeof( i_option_value ) ) == -1 )
+                    &i_opt, sizeof( i_opt ) ) == -1 )
     {
-        intf_ErrMsg("NetworkOpen : can't configure socket (SO_REUSEADDR: %s)",
+        intf_ErrMsg("input error: can't configure socket (SO_REUSEADDR: %s)",
                     strerror(errno));
         close( p_input->i_handle );
         p_input->b_error = 1;
@@ -556,25 +635,19 @@ void input_NetworkOpen( input_thread_t * p_input )
 
     /* Increase the receive buffer size to 1/2MB (8Mb/s during 1/2s) to avoid
      * packet loss caused by scheduling problems */
-    i_option_value = 524288;
-    if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF, &i_option_value,
-                    sizeof( i_option_value ) ) == -1 )
+    i_opt = 0x80000;
+    if( setsockopt( p_input->i_handle, SOL_SOCKET, SO_RCVBUF,
+                    &i_opt, sizeof( i_opt ) ) == -1 )
     {
-        intf_ErrMsg("NetworkOpen : can't configure socket (SO_RCVBUF: %s)", 
+        intf_ErrMsg("input error: can't configure socket (SO_RCVBUF: %s)", 
                     strerror(errno));
         close( p_input->i_handle );
         p_input->b_error = 1;
         return;
     }
 
-    /* Get details about what we are supposed to do */
-    b_broadcast = (boolean_t)main_GetIntVariable( INPUT_BROADCAST_VAR, 0 );
-    i_port = main_GetIntVariable( INPUT_PORT_VAR, INPUT_PORT_DEFAULT );
-
-    /* TODO : here deal with channel stufs */
-    
     /* Build the local socket */
-    if ( input_BuildLocalAddr( &s_socket, i_port, b_broadcast ) 
+    if ( network_BuildLocalAddr( &sock, i_port, psz_broadcast ) 
          == -1 )
     {
         close( p_input->i_handle );
@@ -583,18 +656,17 @@ void input_NetworkOpen( input_thread_t * p_input )
     }
     
     /* Bind it */
-    if( bind( p_input->i_handle, (struct sockaddr *)&s_socket
-              sizeof( s_socket ) ) < 0 )
+    if( bind( p_input->i_handle, (struct sockaddr *)&sock
+              sizeof( sock ) ) < 0 )
     {
-        intf_ErrMsg("NetworkOpen: can't bind socket (%s)", strerror(errno));
+        intf_ErrMsg("input error: can't bind socket (%s)", strerror(errno));
         close( p_input->i_handle );
         p_input->b_error = 1;
         return;
     }
 
     /* Build socket for remote connection */
-    if ( input_BuildRemoteAddr( &s_socket, p_input->p_source ) 
-         == -1 )
+    if ( network_BuildRemoteAddr( &sock, psz_server ) == -1 )
     {
         close( p_input->i_handle );
         p_input->b_error = 1;
@@ -602,10 +674,11 @@ void input_NetworkOpen( input_thread_t * p_input )
     }
 
     /* And connect it ... should we really connect ? */
-    if( connect( p_input->i_handle, (struct sockaddr *) &s_socket,
-                 sizeof( s_socket ) ) == (-1) )
+    if( connect( p_input->i_handle, (struct sockaddr *) &sock,
+                 sizeof( sock ) ) == (-1) )
     {
-        intf_ErrMsg("NetworkOpen: can't connect socket" );
+        intf_ErrMsg( "NetworkOpen: can't connect socket : %s", 
+                     strerror(errno) );
         close( p_input->i_handle );
         p_input->b_error = 1;
         return;
@@ -614,6 +687,7 @@ void input_NetworkOpen( input_thread_t * p_input )
     /* We can't pace control, but FIXME : bug in meuuh's code to sync PCR
      * with the server. */
     p_input->stream.b_pace_control = 1;
+    p_input->stream.b_seekable = 0;
     
     return;
 }
@@ -624,6 +698,5 @@ void input_NetworkOpen( input_thread_t * p_input )
 void input_NetworkClose( input_thread_t * p_input )
 {
     close( p_input->i_handle );
-    /* FIXME: deal with channels */
 }
 #endif