]> git.sesse.net Git - vlc/blobdiff - src/stream_output/sap.c
Fix max UDP port number
[vlc] / src / stream_output / sap.c
index 64d7129e1d6f5084f6991d589f0f5ca3df473349..2d2f88ad00af9ac34e097f5a4e1ef87664cec971 100644 (file)
@@ -32,6 +32,7 @@
 #include <vlc/sout.h>
 
 #include <network.h>
+#include "charset.h"
 
 #define SAP_IPV4_ADDR "224.2.127.254" /* Standard port and address for SAP */
 #define SAP_PORT 9875
@@ -62,6 +63,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
 
 static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
                              session_descriptor_t *p_session );
+static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local );
 
 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
 
@@ -75,6 +77,7 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
 sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
 {
     sap_handler_t *p_sap;
+    char *psz_charset;
 
     p_sap = vlc_object_create( p_announce, sizeof( sap_handler_t ) );
 
@@ -86,6 +89,14 @@ sap_handler_t *announce_SAPHandlerCreate( announce_handler_t *p_announce )
 
     vlc_mutex_init( p_sap, &p_sap->object_lock );
 
+    vlc_current_charset( &psz_charset );
+    p_sap->iconvHandle = vlc_iconv_open( "UTF-8", psz_charset );
+    free( psz_charset );
+    if( p_sap->iconvHandle == (vlc_iconv_t)(-1) )
+    {
+        msg_Warn( p_sap, "Unable to do requested conversion" );
+    }
+
     p_sap->pf_add = announce_SAPAnnounceAdd;
     p_sap->pf_del = announce_SAPAnnounceDel;
 
@@ -144,6 +155,9 @@ void announce_SAPHandlerDestroy( sap_handler_t *p_sap )
         FREE( p_address );
     }
 
+    if( p_sap->iconvHandle != (vlc_iconv_t)(-1) )
+        vlc_iconv_close( p_sap->iconvHandle );
+
     /* Free the structure */
     vlc_object_destroy( p_sap );
 }
@@ -234,9 +248,9 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
         if( p_method->i_ip_version == 6 )
         {
             char sz_scope;
-            if( p_method->psz_ipv6_scope != NULL )
+            if( p_method->sz_ipv6_scope )
             {
-                sz_scope = *p_method->psz_ipv6_scope;
+                sz_scope = p_method->sz_ipv6_scope;
             }
             else
             {
@@ -304,6 +318,7 @@ static int announce_SAPAnnounceAdd( sap_handler_t *p_sap,
             p_address->b_enabled = VLC_TRUE;
             p_address->b_ready = VLC_TRUE;
             p_address->i_interval = config_GetInt( p_sap,"sap-interval");
+            p_address->i_rfd = -1;
         }
 
         if( p_address->i_wfd == -1 || (p_address->i_rfd == -1
@@ -421,6 +436,10 @@ static int announce_SAPAnnounceDel( sap_handler_t *p_sap,
             REMOVE_ELEM( p_sap->pp_sessions,
                          p_sap->i_sessions,
                          i );
+
+            FREE( p_session->p_sap->psz_sdp );
+            FREE( p_session->p_sap->psz_data );
+            free( p_session->p_sap );
             break;
         }
     }
@@ -478,6 +497,10 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
 {
     int64_t i_sdp_id = mdate();
     int     i_sdp_version = 1 + p_sap->i_sessions + (rand()&0xfff);
+    char *psz_group, *psz_name;
+
+    psz_group = convert_to_utf8( p_sap, p_session->psz_group );
+    psz_name = convert_to_utf8( p_sap, p_session->psz_name );
 
     /* see the lists in modules/stream_out/rtp.c for compliance stuff */
     p_session->psz_sdp = (char *)malloc(
@@ -489,14 +512,15 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
                                    "m=video  udp\r\n"
                                    "a=tool:"PACKAGE_STRING"\r\n"
                                    "a=type:broadcast\r\n")
-                           + strlen( p_session->psz_name )
+                           + strlen( psz_name )
                            + strlen( p_session->psz_uri ) + 300
-                           + ( p_session->psz_group ?
-                                 strlen( p_session->psz_group ) : 0 ) );
+                           + ( psz_group ? strlen( psz_group ) : 0 ) );
 
-    if( !p_session->psz_sdp )
+    if( p_session->psz_sdp == NULL || psz_name == NULL )
     {
         msg_Err( p_sap, "out of memory" );
+        FREE( psz_name );
+        FREE( psz_group );
         return VLC_ENOMEM;
     }
     sprintf( p_session->psz_sdp,
@@ -509,15 +533,16 @@ static int SDPGenerate( sap_handler_t *p_sap, session_descriptor_t *p_session )
                             "a=tool:"PACKAGE_STRING"\r\n"
                             "a=type:broadcast\r\n",
                             i_sdp_id, i_sdp_version,
-                            p_session->psz_name,
+                            psz_name,
                             p_session->psz_uri, p_session->i_ttl,
                             p_session->i_port, p_session->i_payload );
+    free( psz_name );
 
-    if( p_session->psz_group )
+    if( psz_group )
     {
         sprintf( p_session->psz_sdp, "%sa=x-plgroup:%s\r\n",
-                                     p_session->psz_sdp,
-                                     p_session->psz_group );
+                                     p_session->psz_sdp, psz_group );
+        free( psz_group );
     }
 
     msg_Dbg( p_sap, "Generated SDP (%i bytes):\n%s", strlen(p_session->psz_sdp),
@@ -582,3 +607,34 @@ static int CalculateRate( sap_handler_t *p_sap, sap_address_t *p_address )
 
     return VLC_SUCCESS;
 }
+
+
+static char *convert_to_utf8( struct sap_handler_t *p_this, char *psz_local )
+{
+    char *psz_unicode, *psz_in, *psz_out;
+    size_t ret, i_in, i_out;
+
+    if( psz_local == NULL )
+        return NULL;
+    if ( p_this->iconvHandle == (vlc_iconv_t)(-1) )
+        return strdup( psz_local );
+
+    psz_in = psz_local;
+    i_in = strlen( psz_local );
+
+    i_out = 6 * i_in;
+    psz_unicode = malloc( i_out + 1 );
+    if( psz_unicode == NULL )
+        return strdup( psz_local );
+    psz_out = psz_unicode;
+
+    ret = vlc_iconv( p_this->iconvHandle,
+                     &psz_in, &i_in, &psz_out, &i_out);
+    if( ret == (size_t)(-1) || i_in )
+    {
+        msg_Warn( p_this, "Failed to convert \"%s\" to UTF-8", psz_local );
+        return strdup( psz_local );
+    }
+    *psz_out = '\0';
+    return psz_unicode;
+}