+/*****************************************************************************
+ * 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);
+ demux_t *p_demux = (demux_t*)p_this;
+ demux_sys_t *p_sys = p_demux->p_sys;
+ int i_tmp = (intptr_t)p_data;
+
+ vlc_mutex_lock( &p_sys->csa_lock );
+ if ( i_tmp )
+ i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
+ else
+ i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
+
+ vlc_mutex_unlock( &p_sys->csa_lock );
+ return i_tmp;
+}
+
+/*****************************************************************************
+ * DemuxFile:
+ *****************************************************************************/
+static int DemuxFile( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ uint8_t *p_buffer = p_sys->buffer; /* Put first on sync byte */
+ int i_diff= 0;
+ int i_data= 0;
+ int i_pos = 0;
+ int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
+
+ i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
+ if( (i_data <= 0) && (i_data < p_sys->i_packet_size) )
+ {
+ msg_Dbg( p_demux, "error reading malformed packets" );
+ return i_data;
+ }
+
+ /* Test continuity counter */
+ while( i_pos < i_data )
+ {
+ ts_pid_t *p_pid; /* point to a PID structure */
+ bool b_payload; /* indicates a packet with payload */
+ bool b_adaptation; /* adaptation field */
+ int i_cc = 0; /* continuity counter */
+
+ if( p_sys->buffer[i_pos] != 0x47 )
+ {
+ msg_Warn( p_demux, "lost sync" );
+ while( vlc_object_alive (p_demux) && (i_pos < i_data) )
+ {
+ i_pos++;
+ if( p_sys->buffer[i_pos] == 0x47 )
+ break;
+ }
+ if( vlc_object_alive (p_demux) )
+ msg_Warn( p_demux, "sync found" );
+ }
+
+ /* continuous when (one of this):
+ * diff == 1
+ * diff == 0 and payload == 0
+ * diff == 0 and duplicate packet (playload != 0) <- should we
+ * test the content ?
+ */
+ i_cc = p_buffer[i_pos+3]&0x0f;
+ b_payload = p_buffer[i_pos+3]&0x10;
+ b_adaptation = p_buffer[i_pos+3]&0x20;
+
+ /* Get the PID */
+ p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
+
+ /* Detect discontinuity indicator in adaptation field */
+ if( b_adaptation && p_buffer[i_pos + 4] > 0 )
+ {
+ if( p_buffer[i_pos+5]&0x80 )
+ msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
+ if( p_buffer[i_pos+5]&0x40 )
+ msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
+ }
+
+ i_diff = ( i_cc - p_pid->i_cc )&0x0f;
+ if( b_payload && i_diff == 1 )
+ {
+ p_pid->i_cc = ( p_pid->i_cc + 1 ) & 0xf;
+ }
+ else
+ {
+ if( p_pid->i_cc == 0xff )
+ {
+ msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
+ p_pid->i_pid, i_cc );
+ p_pid->i_cc = i_cc;
+ }
+ else if( i_diff != 0 )
+ {
+ /* FIXME what to do when discontinuity_indicator is set ? */
+ msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
+ i_cc, ( p_pid->i_cc + 1 )&0x0f );
+
+ p_pid->i_cc = i_cc;
+ /* Mark transport error in the TS packet. */
+ p_buffer[i_pos+1] |= 0x80;
+ }
+ }
+
+ /* Test if user wants to decrypt it first */
+ if( p_sys->csa )
+ {
+ vlc_mutex_lock( &p_sys->csa_lock );
+ csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
+ vlc_mutex_unlock( &p_sys->csa_lock );
+ }
+
+ i_pos += p_sys->i_packet_size;
+ }
+
+ /* Then write */
+ i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
+ if( i_data < 0 )
+ {
+ msg_Err( p_demux, "failed to write data" );
+ return -1;
+ }
+#if 0
+ msg_Dbg( p_demux, "dumped %d bytes", i_data );
+#endif
+
+ p_sys->i_write += i_data;
+ return 1;
+}
+