#define RATECONTROL_TEXT N_("Use muxers rate control mechanism")
#define KEYURI_TEXT N_("AES key URI to place in playlist")
-#define KEYURI_LONGTEXT N_("Location from where client will retrieve the stream decryption key")
#define KEYFILE_TEXT N_("AES key file")
#define KEYFILE_LONGTEXT N_("File containing the 16 bytes encryption key")
+#define KEYLOADFILE_TEXT N_("File where vlc reads key-uri and keyfile-location")
+#define KEYLOADFILE_LONGTEXT N_("File is read when segment starts and is assumet to be in format: "\
+ "key-uri\\nkey-file. File is read on the segment opening and "\
+ "values are used on that segment.")
+
#define RANDOMIV_TEXT N_("Use randomized IV for encryption")
#define RANDOMIV_LONGTEXT N_("Generate IV instead using segment-number as IV")
KEYURI_TEXT, KEYURI_TEXT, true )
add_loadfile( SOUT_CFG_PREFIX "key-file", NULL,
KEYFILE_TEXT, KEYFILE_LONGTEXT, true )
+ add_loadfile( SOUT_CFG_PREFIX "key-loadfile", NULL,
+ KEYLOADFILE_TEXT, KEYLOADFILE_LONGTEXT, true )
set_callbacks( Open, Close )
vlc_module_end ()
"caching",
"key-uri",
"key-file",
+ "key-loadfile",
"generate-iv",
NULL
};
char *psz_uri;
char *psz_key_uri;
char *psz_duration;
+ float f_seglength;
uint32_t i_segment_number;
uint8_t aes_ivs[16];
} output_segment_t;
char *psz_cursegPath;
char *psz_indexPath;
char *psz_indexUrl;
+ char *psz_keyfile;
+ mtime_t i_keyfile_modification;
mtime_t i_opendts;
mtime_t i_seglenm;
uint32_t i_segment;
vlc_array_t *segments_t;
};
-static int CryptSetup( sout_access_out_t *p_access );
+static int LoadCryptFile( sout_access_out_t *p_access);
+static int CryptSetup( sout_access_out_t *p_access, char *keyfile );
/*****************************************************************************
* Open: open the file
*****************************************************************************/
p_sys->segments_t = vlc_array_new();
p_sys->stuffing_size = 0;
+ p_sys->i_opendts = VLC_TS_INVALID;
p_sys->psz_indexPath = NULL;
psz_idx = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index" );
}
p_sys->psz_indexUrl = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "index-url" );
+ p_sys->psz_keyfile = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-loadfile" );
+ p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
p_access->p_sys = p_sys;
- if( CryptSetup( p_access ) < 0 )
+ if( p_sys->psz_keyfile && ( LoadCryptFile( p_access ) < 0 ) )
+ {
+ free( p_sys->psz_indexUrl );
+ free( p_sys->psz_indexPath );
+ free( p_sys );
+ msg_Err( p_access, "Encryption init failed" );
+ return VLC_EGENERIC;
+ }
+ else if( !p_sys->psz_keyfile && ( CryptSetup( p_access, NULL ) < 0 ) )
{
free( p_sys->psz_indexUrl );
free( p_sys->psz_indexPath );
/************************************************************************
* CryptSetup: Initialize encryption
************************************************************************/
-static int CryptSetup( sout_access_out_t *p_access )
+static int CryptSetup( sout_access_out_t *p_access, char *key_file )
{
sout_access_out_sys_t *p_sys = p_access->p_sys;
uint8_t key[16];
+ char *keyfile = NULL;
+
+ if( key_file )
+ keyfile = strdup( key_file );
+ else
+ keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
- p_sys->key_uri = var_GetNonEmptyString( p_access, SOUT_CFG_PREFIX "key-uri" );
if( !p_sys->key_uri ) /*No key uri, assume no encryption wanted*/
{
msg_Dbg( p_access, "No key uri, no encryption");
return VLC_EGENERIC;
}
- char *keyfile = var_InheritString( p_access, SOUT_CFG_PREFIX "key-file" );
if( unlikely(keyfile == NULL) )
{
msg_Err( p_access, "No key-file, no encryption" );
return VLC_SUCCESS;
}
+
+/************************************************************************
+ * LoadCryptFile: Try to parse key_uri and keyfile-location from file
+ ************************************************************************/
+static int LoadCryptFile( sout_access_out_t *p_access )
+{
+ sout_access_out_sys_t *p_sys = p_access->p_sys;
+
+ FILE *stream = vlc_fopen( p_sys->psz_keyfile, "rt" );
+ char *key_file=NULL,*key_uri=NULL;
+
+ if( unlikely( stream == NULL ) )
+ {
+ msg_Err( p_access, "Unable to open keyloadfile %s: %m", p_sys->psz_keyfile );
+ return VLC_EGENERIC;
+ }
+
+
+ //First read key_uri
+ ssize_t len = getline( &key_uri, &(size_t){0}, stream );
+ if( unlikely( len == -1 ) )
+ {
+ msg_Err( p_access, "Cannot read %s: %m", p_sys->psz_keyfile );
+ clearerr( stream );
+ fclose( stream );
+ free( key_uri );
+ return VLC_EGENERIC;
+ }
+ //Strip the newline from uri, maybe scanf would be better?
+ key_uri[len-1]='\0';
+
+ len = getline( &key_file, &(size_t){0}, stream );
+ if( unlikely( len == -1 ) )
+ {
+ msg_Err( p_access, "Cannot read %s: %m", p_sys->psz_keyfile );
+ clearerr( stream );
+ fclose( stream );
+
+ free( key_uri );
+ free( key_file );
+ return VLC_EGENERIC;
+ }
+ // Strip the last newline from filename
+ key_file[len-1]='\0';
+ fclose( stream );
+
+ int returncode = VLC_SUCCESS;
+ if( !p_sys->key_uri || strcmp( p_sys->key_uri, key_uri ) )
+ {
+ if( p_sys->key_uri )
+ {
+ free( p_sys->key_uri );
+ p_sys->key_uri = NULL;
+ }
+ p_sys->key_uri = strdup( key_uri );
+ returncode = CryptSetup( p_access, key_file );
+ }
+ free( key_file );
+ free( key_uri );
+ return returncode;
+}
+
/************************************************************************
* CryptKey: Set encryption IV to current segment number
************************************************************************/
free( segment );
}
+/************************************************************************
+ * segmentAmountNeeded: check that playlist has atleast 3*p_sys->i_seglength of segments
+ * return how many segments are needed for that (max of p_sys->i_segment )
+ ************************************************************************/
+static uint32_t segmentAmountNeeded( sout_access_out_sys_t *p_sys )
+{
+ float duration = .0f;
+ for( unsigned index = 1; index <= vlc_array_count( p_sys->segments_t ) ; index++ )
+ {
+ output_segment_t* segment = vlc_array_item_at_index( p_sys->segments_t, vlc_array_count( p_sys->segments_t ) - index );
+ duration += segment->f_seglength;
+
+ if( duration >= (float)( 3 * p_sys->i_seglen ) )
+ return __MAX(index, p_sys->i_numsegs);
+ }
+ return vlc_array_count( p_sys->segments_t )-1;
+
+}
+
+
+/************************************************************************
+ * isFirstItemRemovable: Check for draft 11 section 6.2.2
+ * check that the first item has been around outside playlist
+ * segment->f_seglength + p_sys->i_seglen before it is removed.
+ ************************************************************************/
+static bool isFirstItemRemovable( sout_access_out_sys_t *p_sys, uint32_t i_firstseg, uint32_t i_index_offset )
+{
+ float duration = .0f;
+
+ /* Check that segment has been out of playlist for seglenght + p_sys->i_seglen amount
+ * We check this by calculating duration of the items that replaced first item in playlist
+ */
+ for(int index=0; index < i_index_offset; index++ )
+ {
+ output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, p_sys->i_segment - i_firstseg + index );
+ duration += segment->f_seglength;
+ }
+ output_segment_t *first = vlc_array_item_at_index( p_sys->segments_t, 0 );
+
+ return duration >= (first->f_seglength + (float)p_sys->i_seglen);
+}
+
/************************************************************************
* updateIndexAndDel: If necessary, update index file & delete old segments
************************************************************************/
{
uint32_t i_firstseg;
+ unsigned i_index_offset = 0;
if ( p_sys->i_numsegs == 0 || p_sys->i_segment < p_sys->i_numsegs )
i_firstseg = 1;
else
- i_firstseg = ( p_sys->i_segment - p_sys->i_numsegs ) + 1;
+ {
+ unsigned numsegs = segmentAmountNeeded( p_sys );
+ i_firstseg = ( p_sys->i_segment - numsegs ) + 1;
+ i_index_offset = vlc_array_count( p_sys->segments_t ) - numsegs;
+ }
// First update index
if ( p_sys->psz_indexPath )
for ( uint32_t i = i_firstseg; i <= p_sys->i_segment; i++ )
{
- //scale to 0..numsegs-1
- uint32_t index = i-i_firstseg;
+ //scale to i_index_offset..numsegs + i_index_offset
+ uint32_t index = i - i_firstseg + i_index_offset;
output_segment_t *segment = (output_segment_t *)vlc_array_item_at_index( p_sys->segments_t, index );
if( p_sys->key_uri &&
}
// Then take care of deletion
- while( p_sys->b_delsegs && p_sys->i_numsegs && ( (vlc_array_count( p_sys->segments_t ) ) >= p_sys->i_numsegs ) )
+ // Try to follow pantos draft 11 section 6.2.2
+ while( p_sys->b_delsegs && p_sys->i_numsegs &&
+ isFirstItemRemovable( p_sys, i_firstseg, i_index_offset )
+ )
{
output_segment_t *segment = vlc_array_item_at_index( p_sys->segments_t, 0 );
+ msg_Dbg( p_access, "Removing segment number %d", segment->i_segment_number );
vlc_array_remove( p_sys->segments_t, 0 );
+
if ( segment->psz_filename )
{
vlc_unlink( segment->psz_filename );
}
+
destroySegment( segment );
+ i_index_offset -=1;
}
msg_Err( p_access, "Couldn't set duration on closed segment");
return;
}
+ segment->f_seglength = p_sys->f_seglen;
segment->i_segment_number = p_sys->i_segment;
segment->i_segment_number = i_newseg;
segment->psz_filename = formatSegmentPath( p_access->psz_path, i_newseg, true );
char *psz_idxFormat = p_sys->psz_indexUrl ? p_sys->psz_indexUrl : p_access->psz_path;
- segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg, true );
+ segment->psz_uri = formatSegmentPath( psz_idxFormat , i_newseg, false );
if ( unlikely( !segment->psz_filename ) )
{
return -1;
}
-
-
fd = vlc_open( segment->psz_filename, O_WRONLY | O_CREAT | O_LARGEFILE |
O_TRUNC, 0666 );
if ( fd == -1 )
vlc_array_append( p_sys->segments_t, segment);
+ if( p_sys->psz_keyfile )
+ {
+ LoadCryptFile( p_access );
+ }
+
if( p_sys->key_uri )
{
segment->psz_key_uri = strdup( p_sys->key_uri );
if ( p_sys->i_handle < 0 )
{
p_sys->i_opendts = output ? output->i_dts : p_buffer->i_dts;
+ //For first segment we can get negative duration otherwise...?
+ if( ( p_sys->i_opendts != VLC_TS_INVALID ) &&
+ ( p_buffer->i_dts < p_sys->i_opendts ) )
+ p_sys->i_opendts = p_buffer->i_dts;
if ( openNextFile( p_access, p_sys ) < 0 )
return -1;
}