]> git.sesse.net Git - vlc/blobdiff - modules/access_output/rtmp.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / modules / access_output / rtmp.c
index 6e7cfdadd262fb44635d13ef72b72d6c00a4aae6..ab9d159531ee38392a2428c83de0433bed363a89 100644 (file)
@@ -27,7 +27,7 @@
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_sout.h>
 
  * Module descriptor
  *****************************************************************************/
 
-#define URL_TEXT N_( "Destination" )
-#define URL_LONGTEXT N_( \
-    "This is the output URL that will be used." )
+#define RTMP_CONNECT_TEXT N_( "Active TCP connection" )
+#define RTMP_CONNECT_LONGTEXT N_( \
+    "If enabled, VLC will connect to a remote destination instead of " \
+    "waiting for an incoming connection." )
 
 static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 #define SOUT_CFG_PREFIX "sout-rtmp-"
 
-vlc_module_begin();
-    set_description( N_("RTMP stream output") );
-    set_shortname( N_("RTMP" ) );
-    set_capability( "sout access", 50 );
-    set_category( CAT_SOUT );
-    set_subcategory( SUBCAT_SOUT_STREAM );
-    add_shortcut( "rtmp" );
-    set_callbacks( Open, Close );
-vlc_module_end();
+vlc_module_begin ()
+    set_description( N_("RTMP stream output") )
+    set_shortname( N_("RTMP" ) )
+    set_capability( "sout access", 0 )
+    set_category( CAT_SOUT )
+    set_subcategory( SUBCAT_SOUT_STREAM )
+    add_shortcut( "rtmp" )
+    set_callbacks( Open, Close )
+    add_bool( "rtmp-connect", false, NULL, RTMP_CONNECT_TEXT,
+              RTMP_CONNECT_LONGTEXT, false )
+vlc_module_end ()
 
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-static const char *ppsz_sout_options[] = {
-    NULL
-};
-
 static ssize_t Write( sout_access_out_t *, block_t * );
 static int     Seek ( sout_access_out_t *, off_t  );
-static void ThreadControl( vlc_object_t * );
+static void* ThreadControl( void * );
 
 struct sout_access_out_sys_t
 {
@@ -90,20 +89,15 @@ static int Open( vlc_object_t *p_this )
     int length_path, length_media_name;
     int i;
 
-    config_ChainParse( p_access, SOUT_CFG_PREFIX, ppsz_sout_options, p_access->p_cfg );
-
     if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) )
-    {
-        msg_Err( p_access, "not enough memory" );
         return VLC_ENOMEM;
-    }
     p_access->p_sys = p_sys;
 
     p_sys->p_thread =
         vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) );
     if( !p_sys->p_thread )
     {
-        msg_Err( p_access, "out of memory" );
+        free( p_sys );
         return VLC_ENOMEM;
     }
     vlc_object_attach( p_sys->p_thread, p_access );
@@ -132,7 +126,10 @@ static int Open( vlc_object_t *p_this )
     }
 
     length_path = strlen( p_sys->p_thread->url.psz_path );
-    length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 1;
+    char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' );
+    if( !psz_tmp )
+        goto error;
+    length_media_name = strlen( psz_tmp ) - 1;
 
     p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 );
     p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) );
@@ -142,12 +139,10 @@ static int Open( vlc_object_t *p_this )
 
     if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username )
     {
-        msg_Dbg( p_access, "      user='%s', pwd='%s'",
-                 p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password );
+        msg_Dbg( p_access, "      user='%s'", p_sys->p_thread->url.psz_username );
     }
 
     /* Initialize thread variables */
-    p_sys->p_thread->b_die = 0;
     p_sys->p_thread->b_error= 0;
     p_sys->p_thread->p_fifo_input = block_FifoNew();
     p_sys->p_thread->p_empty_blocks = block_FifoNew();
@@ -176,34 +171,44 @@ static int Open( vlc_object_t *p_this )
         p_sys->p_thread->rtmp_headers_send[i].body = NULL;
     }
 
-    vlc_cond_init( p_sys->p_thread, &p_sys->p_thread->wait );
+    vlc_cond_init( &p_sys->p_thread->wait );
     vlc_mutex_init( &p_sys->p_thread->lock );
 
     p_sys->p_thread->result_connect = 1;
     /* p_sys->p_thread->result_publish = only used on access */
     p_sys->p_thread->result_play = 1;
     p_sys->p_thread->result_stop = 0;
+    p_sys->p_thread->fd = -1;
 
     /* Open connection */
-    p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
-    if( p_sys->p_thread->fd == -1 )
+    if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 )
+    {
+#if 0
+        p_sys->p_thread->fd = net_ConnectTCP( p_access,
+                                              p_sys->p_thread->url.psz_host,
+                                              p_sys->p_thread->url.i_port );
+#endif
+        msg_Err( p_access, "to be implemented" );
+        goto error2;
+    }
+    else
     {
         int *p_fd_listen;
 
-        msg_Warn( p_access, "cannot connect to %s:%d", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
-        msg_Dbg( p_access, "switching to passive mode" );
-
         p_sys->active = 0;
-
-        p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
+        p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host,
+                                     p_sys->p_thread->url.i_port );
         if( p_fd_listen == NULL )
         {
-            msg_Warn( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port );
+            msg_Warn( p_access, "cannot listen to %s port %i",
+                      p_sys->p_thread->url.psz_host,
+                      p_sys->p_thread->url.i_port );
             goto error2;
         }
 
-        p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 );
-
+        do
+            p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen );
+        while( p_sys->p_thread->fd == -1 );
         net_ListenClose( p_fd_listen );
 
         if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 )
@@ -212,14 +217,9 @@ static int Open( vlc_object_t *p_this )
             goto error2;
         }
     }
-    else
-    {
-        msg_Err( p_access, "to be implemented" );
-        goto error2;
-    }
 
-    if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl,
-                           VLC_THREAD_PRIORITY_INPUT, false ) )
+    if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread,
+                   VLC_THREAD_PRIORITY_INPUT ) )
     {
         msg_Err( p_access, "cannot spawn rtmp control thread" );
         goto error2;
@@ -230,6 +230,8 @@ static int Open( vlc_object_t *p_this )
         if( rtmp_connect_passive( p_sys->p_thread ) < 0 )
         {
             msg_Err( p_access, "connect passive failed");
+            vlc_cancel( p_sys->p_thread->thread );
+            vlc_join( p_sys->p_thread->thread, NULL );
             goto error2;
         }
     }
@@ -246,12 +248,11 @@ error2:
     free( p_sys->p_thread->psz_application );
     free( p_sys->p_thread->psz_media );
 
-    net_Close( p_sys->p_thread->fd );
+    if( p_sys->p_thread->fd != -1 )
+        net_Close( p_sys->p_thread->fd );
 error:
-    vlc_object_detach( p_sys->p_thread );
-    vlc_object_release( p_sys->p_thread );
-
     vlc_UrlClean( &p_sys->p_thread->url );
+    vlc_object_release( p_sys->p_thread );
     free( p_sys );
 
     return VLC_EGENERIC;
@@ -266,12 +267,8 @@ static void Close( vlc_object_t * p_this )
     sout_access_out_sys_t *p_sys = p_access->p_sys;
     int i;
 
-//    p_sys->p_thread->b_die = true;
-    vlc_object_kill( p_sys->p_thread );
-    block_FifoWake( p_sys->p_thread->p_fifo_input );
-    block_FifoWake( p_sys->p_thread->p_empty_blocks );
-
-    vlc_thread_join( p_sys->p_thread );
+    vlc_cancel( p_sys->p_thread->thread );
+    vlc_join( p_sys->p_thread->thread, NULL );
 
     vlc_cond_destroy( &p_sys->p_thread->wait );
     vlc_mutex_destroy( &p_sys->p_thread->lock );
@@ -290,7 +287,6 @@ static void Close( vlc_object_t * p_this )
 
     net_Close( p_sys->p_thread->fd );
 
-    vlc_object_detach( p_sys->p_thread );
     vlc_object_release( p_sys->p_thread );
 
     vlc_UrlClean( &p_sys->p_thread->url );
@@ -331,7 +327,8 @@ p_buffer->p_buffer[i], p_buffer->p_buffer[i+1], p_buffer->p_buffer[i+2], p_buffe
 p_buffer->p_buffer[i+8], p_buffer->p_buffer[i+9], p_buffer->p_buffer[i+10], p_buffer->p_buffer[i+11], p_buffer->p_buffer[i+12], p_buffer->p_buffer[i+13], p_buffer->p_buffer[i+14], p_buffer->p_buffer[i+15]);
 }*/
 ////////////////////////
-msg_Warn(p_access, "rtmp.c:360 i_dts %d i_pts %d", p_buffer->i_dts, p_buffer->i_pts);
+        msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64,
+                 p_buffer->i_dts, p_buffer->i_pts);
         rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer );
 
         if( rtmp_packet )
@@ -367,6 +364,7 @@ msg_Warn(p_access, "rtmp.c:360 i_dts %d i_pts %d", p_buffer->i_dts, p_buffer->i_
  *****************************************************************************/
 static int Seek( sout_access_out_t *p_access, off_t i_pos )
 {
+    (void)i_pos;
     msg_Err( p_access, "RTMP sout access cannot seek" );
     return -1;
 }
@@ -374,16 +372,19 @@ static int Seek( sout_access_out_t *p_access, off_t i_pos )
 /*****************************************************************************
  * ThreadControl: manage control messages and pipe media to Read
  *****************************************************************************/
-static void ThreadControl( vlc_object_t *p_this )
+static void* ThreadControl( void *p_this )
 {
-    rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this;
+    rtmp_control_thread_t *p_thread = p_this;
     rtmp_packet_t *rtmp_packet;
+    int canc = vlc_savecancel ();
 
     rtmp_init_handler( p_thread->rtmp_handler );
 
-    while( !p_thread->b_die )
+    for( ;; )
     {
+        vlc_restorecancel( canc );
         rtmp_packet = rtmp_read_net_packet( p_thread );
+        canc = vlc_savecancel( );
         if( rtmp_packet != NULL )
         {
             if( rtmp_packet->content_type < 0x01 /* RTMP_CONTENT_TYPE_CHUNK_SIZE */
@@ -401,14 +402,16 @@ static void ThreadControl( vlc_object_t *p_this )
         else
         {
             /* Sometimes server close connection too soon */
+#warning Locking bug here.
             if( p_thread->result_connect )
             {
                 vlc_mutex_lock( &p_thread->lock );
                 vlc_cond_signal( &p_thread->wait );
                 vlc_mutex_unlock( &p_thread->lock );
             }
-
-            p_thread->b_die = 1;
+            break;
         }
     }
+    vlc_restorecancel (canc);
+    return NULL;
 }