]> git.sesse.net Git - vlc/blobdiff - modules/mux/mpeg/ts.c
CSA: simplify and remove scary amd64 warning
[vlc] / modules / mux / mpeg / ts.c
index cac4d72580eed5a86776219c515f7a229e0f3e13..6afe0c83d54b927b00c60632c03890ad0267d533 100644 (file)
 # include "config.h"
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_sout.h>
 #include <vlc_codecs.h>
 #include <vlc_block.h>
 
-#include "iso_lang.h"
+#include <vlc_iso_lang.h>
 
 #include "bits.h"
 #include "pes.h"
  *    if they arrive a bit late
  *    (We cannot rely on the fact that the fifo should be full)
  */
+
+/*****************************************************************************
+ * Callback prototypes
+ *****************************************************************************/
+static int ChangeKeyCallback    ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
+static int ActiveKeyCallback    ( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -159,6 +167,14 @@ static void    Close  ( vlc_object_t * );
 #define CK_LONGTEXT N_("CSA encryption key. This must be a " \
   "16 char string (8 hexadecimal bytes).")
 
+#define CK2_TEXT N_("Second CSA Key")
+#define CK2_LONGTEXT N_("The even CSA encryption key. This must be a " \
+  "16 char string (8 hexadecimal bytes).")
+
+#define CU_TEXT N_("CSA Key in use")
+#define CU_LONGTEXT N_("CSA encryption key used. It can be the odd/first/1 " \
+  "(default) or the even/second/2 one.")
+
 #define CPKT_TEXT N_("Packet size in bytes to encrypt")
 #define CPKT_LONGTEXT N_("Size of the TS packet to encrypt. " \
     "The encryption routines subtract the TS-header from the value before " \
@@ -173,7 +189,7 @@ static void    Close  ( vlc_object_t * );
 #define MAX_PMT_PID 64       /* Maximum pids in each pmt.  FIXME: I just chose an arbitary number. Where is the maximum in the spec? */
 
 vlc_module_begin();
-    set_description( _("TS muxer (libdvbpsi)") );
+    set_description( N_("TS muxer (libdvbpsi)") );
     set_shortname( "MPEG-TS");
     set_category( CAT_SOUT );
     set_subcategory( SUBCAT_SOUT_MUX );
@@ -226,6 +242,10 @@ vlc_module_begin();
 
     add_string( SOUT_CFG_PREFIX "csa-ck", NULL, NULL, CK_TEXT, CK_LONGTEXT,
                 true );
+    add_string( SOUT_CFG_PREFIX "csa2-ck", NULL, NULL, CK2_TEXT, CK2_LONGTEXT,
+                true );
+    add_string( SOUT_CFG_PREFIX "csa-use", "1", NULL, CU_TEXT, CU_LONGTEXT,
+                true );
     add_integer( SOUT_CFG_PREFIX "csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, true );
 
     set_callbacks( Open, Close );
@@ -234,10 +254,10 @@ vlc_module_end();
 /*****************************************************************************
  * Local data structures
  *****************************************************************************/
-static const char *ppsz_sout_options[] = {
+static const char *const ppsz_sout_options[] = {
     "pid-video", "pid-audio", "pid-spu", "pid-pmt", "tsid", "netid",
     "es-id-pid", "shaping", "pcr", "bmin", "bmax", "use-key-frames",
-    "dts-delay", "csa-ck", "csa-pkt", "crypt-audio", "crypt-video",
+    "dts-delay", "csa-ck", "csa2-ck", "csa-use", "csa-pkt", "crypt-audio", "crypt-video",
     "muxpmt", "sdtdesc", "program-pmt", "alignment",
     NULL
 };
@@ -355,6 +375,8 @@ struct sout_mux_sys_t
     int             i_pcr_pid;
     sout_input_t    *p_pcr_input;
 
+    vlc_mutex_t     csa_lock;
+
     int             i_audio_bound;
     int             i_video_bound;
 
@@ -492,6 +514,8 @@ static int Open( vlc_object_t *p_this )
     p_sys->dvbpmt = NULL;
     memset( &p_sys->pmtmap, 0, sizeof(p_sys->pmtmap) );
 
+    vlc_mutex_init( &p_sys->csa_lock );
+
     p_mux->pf_control   = Control;
     p_mux->pf_addstream = AddStream;
     p_mux->pf_delstream = DelStream;
@@ -767,52 +791,62 @@ static int Open( vlc_object_t *p_this )
     p_sys->i_pcr    = 0;
 
     p_sys->csa      = NULL;
+    var_Create( p_mux, SOUT_CFG_PREFIX "csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
     var_Get( p_mux, SOUT_CFG_PREFIX "csa-ck", &val );
     if( val.psz_string && *val.psz_string )
     {
-        char *psz = val.psz_string;
+        int i_res;
+        vlc_value_t csa2;
 
-        /* skip 0x */
-        if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
+        p_sys->csa = csa_New();
+
+        var_Create( p_mux, SOUT_CFG_PREFIX "csa2-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
+        var_Get( p_mux, SOUT_CFG_PREFIX "csa2-ck", &csa2 );
+        i_res = csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, true );
+        if( i_res == VLC_SUCCESS && csa2.psz_string && *csa2.psz_string )
         {
-            psz += 2;
+            if( csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, csa2.psz_string, false ) != VLC_SUCCESS )
+            {
+                csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, false );
+            }
         }
-        if( strlen( psz ) != 16 )
+        else if( i_res == VLC_SUCCESS )
         {
-            msg_Dbg( p_mux, "invalid csa ck (it must be 16 chars long)" );
+            csa_SetCW( (vlc_object_t*)p_mux, p_sys->csa, val.psz_string, false );
         }
         else
         {
-            uint64_t i_ck = strtoull( psz, NULL, 16 );
-            uint8_t  ck[8];
-            int      i;
+            csa_Delete( p_sys->csa );
+            p_sys->csa = NULL;
+        }
+
+        if( p_sys->csa )
+        {
+            vlc_value_t use_val, pkt_val;
 
-            for( i = 0; i < 8; i++ )
+            var_Create( p_mux, SOUT_CFG_PREFIX "csa-use", VLC_VAR_STRING | VLC_VAR_DOINHERIT | VLC_VAR_ISCOMMAND );
+            var_Get( p_mux, SOUT_CFG_PREFIX "csa-use", &use_val );
+            var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
+            var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, (void *)1 );
+            var_AddCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
+
+            if ( var_Set( p_mux, SOUT_CFG_PREFIX "csa-use", use_val ) != VLC_SUCCESS )
             {
-                ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
+                var_SetString( p_mux, SOUT_CFG_PREFIX "csa-use", "odd" );
             }
-#ifndef TS_NO_CSA_CK_MSG
-            msg_Dbg( p_mux, "using CSA scrambling with ck=%x:%x:%x:%x:%x:%x:%x:%x",
-                     ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
-#endif
-            p_sys->csa = csa_New();
-            if( p_sys->csa )
-            {
-                vlc_value_t pkt_val;
+            free( use_val.psz_string );
 
-                csa_SetCW( p_sys->csa, ck, ck );
-
-                var_Get( p_mux, SOUT_CFG_PREFIX "csa-pkt", &pkt_val );
-                if( pkt_val.i_int < 12 || pkt_val.i_int > 188 )
-                {
-                    msg_Err( p_mux, "wrong packet size %d specified.", pkt_val.i_int );
-                    msg_Warn( p_mux, "using default packet size of 188 bytes" );
-                    p_sys->i_csa_pkt_size = 188;
-                }
-                else p_sys->i_csa_pkt_size = pkt_val.i_int;
-                msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
+            var_Get( p_mux, SOUT_CFG_PREFIX "csa-pkt", &pkt_val );
+            if( pkt_val.i_int < 12 || pkt_val.i_int > 188 )
+            {
+                msg_Err( p_mux, "wrong packet size %d specified.", pkt_val.i_int );
+                msg_Warn( p_mux, "using default packet size of 188 bytes" );
+                p_sys->i_csa_pkt_size = 188;
             }
+            else p_sys->i_csa_pkt_size = pkt_val.i_int;
+            msg_Dbg( p_mux, "encrypting %d bytes of packet", p_sys->i_csa_pkt_size );
         }
+        free( csa2.psz_string );
     }
     free( val.psz_string );
 
@@ -834,20 +868,74 @@ static void Close( vlc_object_t * p_this )
     sout_mux_sys_t      *p_sys = p_mux->p_sys;
     int i;
 
+    vlc_mutex_lock( &p_sys->csa_lock );
     if( p_sys->csa )
     {
+        var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-ck", ChangeKeyCallback, NULL );
+        var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa2-ck", ChangeKeyCallback, NULL );
+        var_DelCallback( p_mux, SOUT_CFG_PREFIX "csa-use", ActiveKeyCallback, NULL );
         csa_Delete( p_sys->csa );
+        p_sys->csa = NULL;
     }
+    vlc_mutex_unlock( &p_sys->csa_lock );
+
     for( i = 0; i < MAX_PMT; i++ )
     {
         free( p_sys->sdt_descriptors[i].psz_service_name );
         free( p_sys->sdt_descriptors[i].psz_provider );
     }
 
+    vlc_mutex_destroy( &p_sys->csa_lock );
     free( p_sys->dvbpmt );
     free( p_sys );
 }
 
+/*****************************************************************************
+ * ChangeKeyCallback: called when changing the odd encryption key on the fly.
+ *****************************************************************************/
+static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data )
+{
+    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
+    sout_mux_t      *p_mux = (sout_mux_t*)p_this;
+    sout_mux_sys_t  *p_sys = p_mux->p_sys;
+    int ret;
+
+    vlc_mutex_lock( &p_sys->csa_lock );
+    ret = csa_SetCW( p_this, p_sys->csa, newval.psz_string,
+                     !!(intptr_t)p_data );
+    vlc_mutex_unlock( &p_sys->csa_lock );
+
+    return ret;
+}
+
+/*****************************************************************************
+ * ActiveKeyCallback: called when changing the active (in use) encryption key on the fly.
+ *****************************************************************************/
+static int ActiveKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
+                           vlc_value_t oldval, vlc_value_t newval,
+                           void *p_data )
+{
+    VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
+    sout_mux_t      *p_mux = (sout_mux_t*)p_this;
+    sout_mux_sys_t  *p_sys = p_mux->p_sys;
+    int             i_res = VLC_EBADVAR;
+
+    vlc_mutex_lock( &p_sys->csa_lock );
+    if( !strcmp(newval.psz_string, "odd" ) || !strcmp(newval.psz_string, "first" ) || !strcmp(newval.psz_string, "1" ) )
+    {
+        i_res = csa_UseKey( (vlc_object_t*)p_mux, p_sys->csa, 1 );
+    }
+    else if( !strcmp(newval.psz_string, "even" ) || !strcmp(newval.psz_string, "second" ) || !strcmp(newval.psz_string, "2" ) )
+    {
+        i_res = csa_UseKey( (vlc_object_t*)p_mux, p_sys->csa, 0 );
+    }
+    vlc_mutex_unlock( &p_sys->csa_lock );
+
+    return i_res;
+}
+
 /*****************************************************************************
  * Control:
  *****************************************************************************/
@@ -1380,11 +1468,11 @@ static int Mux( sout_mux_t *p_mux )
                                 p_spu->i_dts - p_pcr_stream->i_pes_dts;
 
                             if( ( i_spu_delay > i_shaping_delay ) &&
-                                ( i_spu_delay < I64C(100000000) ) )
+                                ( i_spu_delay < INT64_C(100000000) ) )
                                 continue;
 
-                            if ( ( i_spu_delay >= I64C(100000000) ) ||
-                                 ( i_spu_delay < I64C(10000) ) )
+                            if ( ( i_spu_delay >= INT64_C(100000000) ) ||
+                                 ( i_spu_delay < INT64_C(10000) ) )
                             {
                                 BufferChainClean( &p_stream->chain_pes );
                                 p_stream->i_pes_dts = 0;
@@ -1485,7 +1573,7 @@ static int Mux( sout_mux_t *p_mux )
                                     p_spu->p_buffer[1] = 1;
                                     p_spu->p_buffer[2] = ' ';
 
-                                    E_(EStoPES)( p_mux->p_sout, &p_spu, p_spu,
+                                    EStoPES( p_mux->p_sout, &p_spu, p_spu,
                                                  p_input->p_fmt,
                                                  p_stream->i_stream_id, 1,
                                                  0, 0, 0 );
@@ -1529,7 +1617,7 @@ static int Mux( sout_mux_t *p_mux )
                             p_data->i_pts = p_data->i_dts;
                         }
 
-                        E_( EStoPES )( p_mux->p_sout, &p_data, p_data,
+                         EStoPES ( p_mux->p_sout, &p_data, p_data,
                                        p_input->p_fmt, p_stream->i_stream_id,
                                        1, b_data_alignment, i_header_size, 0 );
 
@@ -1677,7 +1765,7 @@ static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
     else if( i_size > STD_PES_PAYLOAD )
     {
         block_t *p_new = block_New( p_mux, STD_PES_PAYLOAD );
-        p_mux->p_libvlc->pf_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
+        vlc_memcpy( p_new->p_buffer, p_data->p_buffer, STD_PES_PAYLOAD );
         p_new->i_pts = p_data->i_pts;
         p_new->i_dts = p_data->i_dts;
         p_new->i_length = p_data->i_length * STD_PES_PAYLOAD
@@ -1706,8 +1794,7 @@ static block_t *FixPES( sout_mux_t *p_mux, block_fifo_t *p_fifo )
         }
         i_copy = __MIN( STD_PES_PAYLOAD - i_size, p_next->i_buffer );
 
-        p_mux->p_libvlc->pf_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer,
-                                 i_copy );
+        vlc_memcpy( &p_data->p_buffer[i_size], p_next->p_buffer, i_copy );
         p_next->i_pts += p_next->i_length * i_copy / p_next->i_buffer;
         p_next->i_dts += p_next->i_length * i_copy / p_next->i_buffer;
         p_next->i_length -= p_next->i_length * i_copy / p_next->i_buffer;
@@ -1888,7 +1975,9 @@ static void TSDate( sout_mux_t *p_mux, sout_buffer_chain_t *p_chain_ts,
         }
         if( p_ts->i_flags & BLOCK_FLAG_SCRAMBLED )
         {
-            csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size, 0 );
+            vlc_mutex_lock( &p_sys->csa_lock );
+            csa_Encrypt( p_sys->csa, p_ts->p_buffer, p_sys->i_csa_pkt_size );
+            vlc_mutex_unlock( &p_sys->csa_lock );
         }
 
         /* latency */
@@ -2435,7 +2524,8 @@ static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c )
                     bits_write( &bits, 8, 0x21 );   /* Visual 14496-2 */
                     bits_write( &bits, 6, 0x04 );   /* VisualStream */
                 }
-                else if( p_stream->i_stream_type == 0x11  || p_stream->i_stream_type == 0x0f )
+                else if( p_stream->i_stream_type == 0x11 ||
+                         p_stream->i_stream_type == 0x0f )
                 {
                     bits_write( &bits, 8, 0x40 );   /* Audio 14496-3 */
                     bits_write( &bits, 6, 0x05 );   /* AudioStream */