]> git.sesse.net Git - vlc/blobdiff - modules/access_output/udp.c
* mp4.c: added support for SVQ1.
[vlc] / modules / access_output / udp.c
index e0bd4329ff00554348488ac1496d1f793e534e33..584358ae4e0366cd85c2f0d029a970d9b12211ee 100644 (file)
@@ -2,7 +2,7 @@
  * udp.c
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: udp.c,v 1.11 2003/07/31 23:44:49 fenrir Exp $
+ * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
 #include "network.h"
 
 #define DEFAULT_PORT 1234
-#define LATENCY     100000
-#define MAX_ERROR    500000
 /*****************************************************************************
  * Exported prototypes
  *****************************************************************************/
 static int     Open   ( vlc_object_t * );
 static void    Close  ( vlc_object_t * );
 
-static int     Write( sout_access_out_t *, sout_buffer_t * );
-static int     Seek ( sout_access_out_t *, off_t  );
+static int     Write   ( sout_access_out_t *, sout_buffer_t * );
+static int     WriteRaw( sout_access_out_t *, sout_buffer_t * );
+static int     Seek    ( sout_access_out_t *, off_t  );
 
 static void    ThreadWrite( vlc_object_t * );
 
@@ -71,22 +70,22 @@ static sout_buffer_t *NewUDPPacket( sout_access_out_t *, mtime_t );
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-#define CACHING_TEXT N_("caching value in ms")
+#define CACHING_TEXT N_("Caching value (ms)")
 #define CACHING_LONGTEXT N_( \
     "Allows you to modify the default caching value for udp streams. This " \
-    "value should be set in miliseconds units." )
+    "value should be set in millisecond units." )
 
 vlc_module_begin();
     set_description( _("UDP stream ouput") );
-    add_category_hint( N_("udp stream output"), NULL , VLC_TRUE );
-    add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
+    add_integer( "udp-sout-caching", DEFAULT_PTS_DELAY / 1000, NULL,
+                 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
     set_capability( "sout access", 100 );
     add_shortcut( "udp" );
     add_shortcut( "rtp" ); // Will work only with ts muxer
     set_callbacks( Open, Close );
 vlc_module_end();
 
-typedef struct sout_access_thread_s
+typedef struct sout_access_thread_t
 {
     VLC_COMMON_MEMBERS
 
@@ -96,6 +95,10 @@ typedef struct sout_access_thread_s
 
     int         i_handle;
 
+    int64_t     i_caching;
+    int64_t     i_late;
+    int         i_group;
+
 } sout_access_thread_t;
 
 struct sout_access_out_sys_t
@@ -127,21 +130,22 @@ static int Open( vlc_object_t *p_this )
     module_t            *p_network;
     network_socket_t    socket_desc;
 
-    char                *val;
+    vlc_value_t         val;
+    char                *psz_val;
 
     if( !( p_sys = p_access->p_sys =
                 malloc( sizeof( sout_access_out_sys_t ) ) ) )
     {
-        msg_Err( p_access, "Not enough memory" );
-        return( VLC_EGENERIC );
+        msg_Err( p_access, "not enough memory" );
+        return VLC_EGENERIC;
     }
 
 
     if( p_access->psz_access != NULL &&
         !strcmp( p_access->psz_access, "rtp" ) )
     {
-        msg_Warn( p_access, "becarefull that rtp ouput work only with ts "
-                  "payload(not an error)" );
+        msg_Warn( p_access, "be careful that rtp output only works with ts "
+                  "payload (not an error)" );
         p_sys->b_rtpts = 1;
     }
     else
@@ -181,7 +185,7 @@ static int Open( vlc_object_t *p_this )
     if( !p_sys->p_thread )
     {
         msg_Err( p_access, "out of memory" );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
 
     p_sys->p_thread->p_sout = p_access->p_sout;
@@ -195,28 +199,55 @@ static int Open( vlc_object_t *p_this )
     socket_desc.psz_bind_addr   = "";
     socket_desc.i_bind_port     = 0;
     socket_desc.i_ttl           = 0;
-    if( ( val = sout_cfg_find_value( p_access->p_cfg, "ttl" ) ) )
+    if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "ttl" ) ) )
     {
-        socket_desc.i_ttl = atoi( val );
+        socket_desc.i_ttl = atoi( psz_val );
     }
     p_sys->p_thread->p_private = (void*)&socket_desc;
-    if( !( p_network = module_Need( p_sys->p_thread,
-                                    "network", "" ) ) )
+    if( !( p_network = module_Need( p_sys->p_thread, "network", NULL, 0 ) ) )
     {
         msg_Err( p_access, "failed to open a connection (udp)" );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
     module_Unneed( p_sys->p_thread, p_network );
 
     p_sys->p_thread->i_handle = socket_desc.i_handle;
-    p_sys->i_mtu     = socket_desc.i_mtu;
 
+    var_Create( p_this, "udp-sout-caching",
+                VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
+    var_Get( p_this, "udp-sout-caching", &val );
+    p_sys->p_thread->i_caching = val.i_int * 1000;
+    if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "caching" ) ) )
+    {
+        p_sys->p_thread->i_caching = atoll( psz_val ) * 1000;
+    }
+
+    p_sys->p_thread->i_group = 1;
+    if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "group" ) ) )
+    {
+        p_sys->p_thread->i_group = atoi( psz_val );
+    }
+
+    p_sys->p_thread->i_late = 0;
+    if( ( psz_val = sout_cfg_find_value( p_access->p_cfg, "late" ) ) )
+    {
+        p_sys->p_thread->i_late = atoll( psz_val ) * 1000;
+    }
+
+
+    p_sys->i_mtu = socket_desc.i_mtu;
+
+#ifdef WIN32
+    if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
+                           VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
+#else
     if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
+#endif
     {
         msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
         vlc_object_destroy( p_sys->p_thread );
-        return( VLC_EGENERIC );
+        return VLC_EGENERIC;
     }
 
     srand( (uint32_t)mdate());
@@ -224,13 +255,24 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_sequence_number = rand()&0xffff;
     p_sys->i_ssrc            = rand()&0xffffffff;
 
-    p_access->pf_write       = Write;
-    p_access->pf_seek        = Seek;
+    if( sout_cfg_find( p_access->p_cfg, "raw" ) )
+    {
+        p_access->pf_write = WriteRaw;
+    }
+    else
+    {
+        p_access->pf_write = Write;
+    }
+
+    p_access->pf_seek = Seek;
 
-    msg_Info( p_access, "Open: addr:`%s' port:`%d'",
-              psz_dst_addr, i_dst_port );
+    msg_Info( p_access, "Open: addr:`%s' port:`%d'", psz_dst_addr, i_dst_port);
 
     free( psz_dst_addr );
+
+    /* update p_sout->i_out_pace_nocontrol */
+    p_access->p_sout->i_out_pace_nocontrol++;
+
     return VLC_SUCCESS;
 }
 
@@ -239,14 +281,14 @@ static int Open( vlc_object_t *p_this )
  *****************************************************************************/
 static void Close( vlc_object_t * p_this )
 {
-    sout_access_out_t       *p_access = (sout_access_out_t*)p_this;
-    sout_access_out_sys_t   *p_sys = p_access->p_sys;
-    int                 i;
+    sout_access_out_t     *p_access = (sout_access_out_t*)p_this;
+    sout_access_out_sys_t *p_sys = p_access->p_sys;
+    int i;
 
     p_sys->p_thread->b_die = 1;
     for( i = 0; i < 10; i++ )
     {
-        sout_buffer_t       *p_dummy;
+        sout_buffer_t *p_dummy;
 
         p_dummy = sout_BufferNew( p_access->p_sout, p_sys->i_mtu );
         p_dummy->i_dts = 0;
@@ -263,24 +305,21 @@ static void Close( vlc_object_t * p_this )
         sout_BufferDelete( p_access->p_sout, p_sys->p_buffer );
     }
 
-#if defined( UNDER_CE )
-    CloseHandle( (HANDLE)p_sys->p_thread->i_handle );
-#elif defined( WIN32 )
-    closesocket( p_sys->p_thread->i_handle );
-#else
-    close( p_sys->p_thread->i_handle );
-#endif
+    net_Close( p_sys->p_thread->i_handle );
+
+    /* update p_sout->i_out_pace_nocontrol */
+    p_access->p_sout->i_out_pace_nocontrol--;
 
     free( p_sys );
     msg_Info( p_access, "Close" );
 }
 
 /*****************************************************************************
- * Read: standard read on a file descriptor.
+ * Write: standard write on a file descriptor.
  *****************************************************************************/
 static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
 {
-    sout_access_out_sys_t   *p_sys = p_access->p_sys;
+    sout_access_out_sys_t *p_sys = p_access->p_sys;
     unsigned int i_write;
 
     while( p_buffer )
@@ -323,13 +362,24 @@ static int Write( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
     return( p_sys->p_thread->b_error ? -1 : 0 );
 }
 
+/*****************************************************************************
+ * WriteRaw: write p_buffer without trying to fill mtu
+ *****************************************************************************/
+static int WriteRaw( sout_access_out_t *p_access, sout_buffer_t *p_buffer )
+{
+    sout_access_out_sys_t   *p_sys = p_access->p_sys;
+
+    sout_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
+
+    return( p_sys->p_thread->b_error ? -1 : 0 );
+}
+
 /*****************************************************************************
  * Seek: seek to a specific location in a file
  *****************************************************************************/
 static int Seek( sout_access_out_t *p_access, off_t i_pos )
 {
-
-    msg_Err( p_access, "udp sout access cannot seek" );
+    msg_Err( p_access, "UDP sout access cannot seek" );
     return( -1 );
 }
 
@@ -380,38 +430,80 @@ static void ThreadWrite( vlc_object_t *p_this )
 {
     sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
     sout_instance_t      *p_sout = p_thread->p_sout;
-    sout_buffer_t        *p_buffer;
-    mtime_t              i_date, i_pts_delay;
+    mtime_t              i_date_last = -1;
+    mtime_t              i_to_send = p_thread->i_group;
+    int                  i_dropped_packets = 0;
 
-    /* Get the i_pts_delay value */
-    i_pts_delay = config_GetInt( p_this, "udp-sout-caching" ) * 1000;
-
-    p_buffer = sout_FifoGet( p_thread->p_fifo );
-    if( p_thread->b_die ) return;
-
-    i_date = mdate() + i_pts_delay - p_buffer->i_dts;
-
-    while( 1 )
+    while( !p_thread->b_die )
     {
-        mtime_t i_wait;
+        sout_buffer_t *p_pk;
+        mtime_t       i_date, i_sent;
 
-        i_wait = i_date + p_buffer->i_dts;
+        p_pk = sout_FifoGet( p_thread->p_fifo );
 
-        if( i_wait - mdate() > MAX_ERROR || i_wait - mdate() < -MAX_ERROR )
+        i_date = p_thread->i_caching + p_pk->i_dts;
+        if( i_date_last > 0 )
         {
-            msg_Warn( p_sout, "resetting clock" );
-            i_date = mdate() + i_pts_delay - p_buffer->i_dts;
+            if( i_date - i_date_last > 2000000 )
+            {
+                if( !i_dropped_packets )
+                    msg_Dbg( p_thread, "mmh, hole > 2s -> drop" );
+
+                sout_BufferDelete( p_sout, p_pk );
+                i_date_last = i_date;
+                i_dropped_packets++;
+                continue;
+            }
+            else if( i_date - i_date_last < 0 )
+            {
+                if( !i_dropped_packets )
+                    msg_Dbg( p_thread, "mmh, paquets in the past -> drop" );
+
+                sout_BufferDelete( p_sout, p_pk );
+                i_date_last = i_date;
+                i_dropped_packets++;
+                continue;
+            }
         }
-        else
+
+        i_sent = mdate();
+        if( p_thread->i_late > 0 && i_sent > i_date + p_thread->i_late )
         {
-            mwait( i_wait );
+            if( !i_dropped_packets )
+            {
+                msg_Dbg( p_thread, "late packet to send (" I64Fd ") -> drop",
+                         i_sent - i_date );
+            }
+            sout_BufferDelete( p_sout, p_pk );
+            i_date_last = i_date;
+            i_dropped_packets++;
+            continue;
         }
 
-        send( p_thread->i_handle, p_buffer->p_buffer, p_buffer->i_size, 0 );
+        i_to_send--;
+        if ( !i_to_send )
+        {
+            mwait( i_date );
+            i_to_send = p_thread->i_group;
+        }
+        send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_size, 0 );
 
-        sout_BufferDelete( p_sout, p_buffer );
+        if( i_dropped_packets )
+        {
+            msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
+            i_dropped_packets = 0;
+        }
+
+#if 0
+        i_sent = mdate();
+        if ( i_sent > i_date + 20000 )
+        {
+            msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
+                     i_sent - i_date );
+        }
+#endif
 
-        p_buffer = sout_FifoGet( p_thread->p_fifo );
-        if( p_thread->b_die ) return;
+        sout_BufferDelete( p_sout, p_pk );
+        i_date_last = i_date;
     }
 }