1 /*****************************************************************************
2 * hds.c: Http Dynamic Streaming (HDS) stream filter
3 *****************************************************************************
5 * Author: Jonathan Thambidurai <jonathan _AT_ fastly _DOT_ com>
6 * Heavily inspired by SMooth module of Frédéric Yhuel <fyhuel _AT_ viotech _DOT_ net>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 *****************************************************************************/
27 #include <vlc_common.h>
28 #include <vlc_plugin.h>
29 #include <vlc_stream.h>
30 #include <vlc_strings.h> /* b64_decode */
32 #include <vlc_charset.h> /* FromCharset */
33 #include <vlc_es.h> /* UNKNOWN_ES */
35 typedef struct chunk_s
37 int64_t duration; /* chunk duration in afrt timescale units */
41 uint32_t frun_entry; /* Used to speed things up in vod situations */
45 uint32_t mdat_pos; /* position in the mdat */
56 typedef struct segment_run_s
58 uint32_t first_segment;
59 uint32_t fragments_per_segment;
62 typedef struct fragment_run_s
64 uint32_t fragment_number_start;
65 uint32_t fragment_duration;
66 uint64_t fragment_timestamp;
70 typedef struct hds_stream_s
72 /* linked-list of chunks */
74 chunk_t *chunks_livereadpos;
75 chunk_t *chunks_downloadpos;
77 char* quality_segment_modifier;
79 /* we can make this configurable */
80 uint64_t download_leadtime;
82 /* in timescale units */
83 uint32_t total_duration;
85 uint32_t afrt_timescale;
87 /* these two values come from the abst */
89 uint64_t live_current_time;
91 vlc_mutex_t abst_lock;
96 /* can be left as null */
99 /* this comes from the manifest media section */
102 /* this comes from the bootstrap info */
105 #define MAX_HDS_SERVERS 10
106 char* server_entries[MAX_HDS_SERVERS];
107 uint8_t server_entry_count;
109 #define MAX_HDS_SEGMENT_RUNS 256
110 segment_run_t segment_runs[MAX_HDS_SEGMENT_RUNS];
111 uint8_t segment_run_count;
113 #define MAX_HDS_FRAGMENT_RUNS 10000
114 fragment_run_t fragment_runs[MAX_HDS_FRAGMENT_RUNS];
115 uint32_t fragment_run_count;
118 /* this is effectively just a sanity check mechanism */
119 #define MAX_REQUEST_SIZE (50*1024*1024)
123 char *base_url; /* URL common part for chunks */
124 vlc_thread_t live_thread;
125 vlc_thread_t dl_thread;
127 /* we pend on peek until some number of segments arrives; otherwise
128 * the downstream system dies in case of playback */
129 uint64_t chunk_count;
131 vlc_array_t *hds_streams; /* available streams */
133 uint32_t flv_header_bytes_sent;
134 uint64_t duration_seconds;
140 typedef struct _bootstrap_info {
148 typedef struct _media_info {
154 /*****************************************************************************
156 *****************************************************************************/
157 static int Open( vlc_object_t * );
158 static void Close( vlc_object_t * );
161 set_category( CAT_INPUT )
162 set_subcategory( SUBCAT_INPUT_STREAM_FILTER )
163 set_description( N_("HTTP Dynamic Streaming") )
164 set_shortname( "Dynamic Streaming")
165 add_shortcut( "hds" )
166 set_capability( "stream_filter", 30 )
167 set_callbacks( Open, Close )
170 static int Read( stream_t *, void *, unsigned );
171 static int Peek( stream_t *, const uint8_t **, unsigned );
172 static int Control( stream_t *, int , va_list );
174 static inline bool isFQUrl( char* url )
176 return ( NULL != vlc_strcasestr( url, "https://") ||
177 NULL != vlc_strcasestr( url, "http://" ) );
180 static bool isHDS( stream_t *s )
183 int i_size = stream_Peek( s->p_source, (const uint8_t**) &peek, 200 );
189 if( !memcmp( peek, "\xFF\xFE", 2 ) )
191 str = FromCharset( "UTF-16LE", peek, 512 );
193 else if( !memcmp( peek, "\xFE\xFF", 2 ) )
195 str = FromCharset( "UTF-16BE", peek, 512 );
203 bool ret = strstr( str, "<manifest" ) != NULL;
207 static uint8_t* parse_asrt( vlc_object_t* p_this,
212 uint8_t* data_p = data;
214 uint32_t asrt_len = 0;
215 asrt_len = U32_AT( data_p );
216 if( asrt_len > data_end - data ||
217 data_end - data < 14 )
219 msg_Err( p_this, "Not enough asrt data (%"PRIu32", %lu)", asrt_len, data_end - data );
223 data_p += sizeof(asrt_len);
225 if( 0 != memcmp( "asrt", data_p, 4 ) )
227 msg_Err( p_this, "Cant find asrt in bootstrap" );
232 /* ignore flags and versions (we don't handle multiple updates) */
235 uint8_t quality_entry_count = *data_p;
236 bool quality_found = false;
239 if( ! s->quality_segment_modifier )
241 quality_found = true;
244 while( quality_entry_count-- > 0 )
246 char* str_start = (char*) data_p;
247 data_p = memchr( data_p, '\0', data_end - data_p );
250 msg_Err( p_this, "Couldn't find quality entry string in asrt" );
255 if( ! quality_found )
257 if( ! strncmp( str_start, s->quality_segment_modifier,
258 strlen(s->quality_segment_modifier) ) )
260 quality_found = true;
264 if( data_p >= data_end )
266 msg_Err( p_this, "Premature end of asrt in quality entries" );
271 if( data_end - data_p < 4 )
273 msg_Err( p_this, "Premature end of asrt after quality entries" );
277 uint32_t segment_run_entry_count = U32_AT( data_p );
278 data_p += sizeof(segment_run_entry_count);
280 if( data_end - data_p < 8 * segment_run_entry_count )
282 msg_Err( p_this, "Not enough data in asrt for segment run entries" );
286 if( segment_run_entry_count >= MAX_HDS_SEGMENT_RUNS )
288 msg_Err( p_this, "Too many segment runs" );
292 while( segment_run_entry_count-- > 0 )
296 s->segment_runs[s->segment_run_count].first_segment = U32_AT(data_p);
302 s->segment_runs[s->segment_run_count].fragments_per_segment = U32_AT(data_p);
306 s->segment_run_count++;
312 static uint8_t* parse_afrt( vlc_object_t* p_this,
317 uint8_t* data_p = data;
319 uint32_t afrt_len = U32_AT( data_p );
320 if( afrt_len > data_end - data ||
321 data_end - data < 9 )
323 msg_Err( p_this, "Not enough afrt data %u, %ld", afrt_len, data_end - data );
326 data_p += sizeof(afrt_len);
328 if( 0 != memcmp( data_p, "afrt", 4 ) )
330 msg_Err( p_this, "Cant find afrt in bootstrap" );
335 /* ignore flags and versions (we don't handle multiple updates) */
338 if( data_end - data_p < 9 )
340 msg_Err( p_this, "afrt is too short" );
344 s->afrt_timescale = U32_AT( data_p );
347 bool quality_found = false;
348 if( ! s->quality_segment_modifier )
350 quality_found = true;
353 uint32_t quality_entry_count = *data_p;
355 while( quality_entry_count-- > 0 )
357 char* str_start = (char*)data_p;
358 data_p = memchr( data_p, '\0', data_end - data_p );
361 msg_Err( p_this, "Couldn't find quality entry string in afrt" );
366 if( ! quality_found )
368 if( ! strncmp( str_start, s->quality_segment_modifier,
369 strlen(s->quality_segment_modifier) ) )
371 quality_found = true;
376 if( data_end - data_p < 5 )
378 msg_Err( p_this, "No more space in afrt after quality entries" );
382 uint32_t fragment_run_entry_count = U32_AT( data_p );
383 data_p += sizeof(uint32_t);
385 while(fragment_run_entry_count-- > 0)
387 if( data_end - data_p < 16 )
389 msg_Err( p_this, "Not enough data in afrt" );
393 if( s->fragment_run_count >= MAX_HDS_FRAGMENT_RUNS )
395 msg_Err( p_this, "Too many fragment runs, exiting" );
399 s->fragment_runs[s->fragment_run_count].fragment_number_start = U32_AT(data_p);
402 s->fragment_runs[s->fragment_run_count].fragment_timestamp = U64_AT( data_p );
405 s->fragment_runs[s->fragment_run_count].fragment_duration = U32_AT( data_p );
408 s->fragment_runs[s->fragment_run_count].discont = 0;
409 if( s->fragment_runs[s->fragment_run_count].fragment_duration == 0 )
411 /* discontinuity flag */
412 s->fragment_runs[s->fragment_run_count].discont = *(data_p++);
415 s->fragment_run_count++;
421 static inline chunk_t* chunk_new()
423 chunk_t* chunk = calloc(1, sizeof(chunk_t));
427 static void chunk_free( chunk_t * chunk )
429 FREENULL( chunk->data );
433 static void parse_BootstrapData( vlc_object_t* p_this,
438 uint8_t* data_p = data;
440 uint32_t abst_len = U32_AT( data_p );
441 if( abst_len > data_end - data
442 || data_end - data < 29 /* min size of data */ )
444 msg_Warn( p_this, "Not enough bootstrap data" );
447 data_p += sizeof(abst_len);
449 if( 0 != memcmp( data_p, "abst", 4 ) )
451 msg_Warn( p_this, "Cant find abst in bootstrap" );
459 /* we ignore the version */
462 /* some flags we don't care about here because they are
468 s->timescale = U32_AT( data_p );
469 data_p += sizeof(s->timescale);
471 s->live_current_time = U64_AT( data_p );
472 data_p += sizeof(s->live_current_time);
474 /* smtpe time code offset */
477 s->movie_id = strndup( (char*)data_p, data_end - data_p );
478 data_p += ( strlen( s->movie_id ) + 1 );
480 if( data_end - data_p < 4 ) {
481 msg_Warn( p_this, "Not enough bootstrap after Movie Identifier" );
485 uint8_t server_entry_count = 0;
486 server_entry_count = (uint8_t) *data_p;
489 s->server_entry_count = 0;
490 while( server_entry_count-- > 0 )
492 if( s->server_entry_count < MAX_HDS_SERVERS )
494 s->server_entries[s->server_entry_count++] = strndup( (char*)data_p,
496 data_p += strlen( s->server_entries[s->server_entry_count-1] ) + 1;
500 msg_Warn( p_this, "Too many servers" );
501 data_p = memchr( data_p, '\0', data_end - data_p );
504 msg_Err( p_this, "Couldn't find server entry" );
510 if( data_p >= data_end )
512 msg_Warn( p_this, "Premature end of bootstrap info while reading servers" );
517 if( data_end - data_p < 3 ) {
518 msg_Warn( p_this, "Not enough bootstrap after Servers" );
522 s->quality_segment_modifier = NULL;
524 uint8_t quality_entry_count = *data_p;
527 if( quality_entry_count > 1 )
529 msg_Err( p_this, "I don't know what to do with multiple quality levels in the bootstrap - shouldn't this be handled at the manifest level?" );
533 s->quality_segment_modifier = NULL;
534 while( quality_entry_count-- > 0 )
536 if( s->quality_segment_modifier )
538 s->quality_segment_modifier = strndup( (char*)data_p, data_end - data_p );
540 data_p += strnlen( (char*)data_p, data_end - data_p ) + 1;
543 if( data_end - data_p < 2 ) {
544 msg_Warn( p_this, "Not enough bootstrap after quality entries" );
548 /* ignoring "DrmData" */
549 data_p = memchr( data_p, '\0', data_end - data_p );
552 msg_Err( p_this, "Couldn't find DRM Data" );
557 if( data_end - data_p < 2 ) {
558 msg_Warn( p_this, "Not enough bootstrap after drm data" );
562 /* ignoring "metadata" */
563 data_p = memchr( data_p, '\0', data_end - data_p );
566 msg_Err( p_this, "Couldn't find metadata");
571 if( data_end - data_p < 2 ) {
572 msg_Warn( p_this, "Not enough bootstrap after drm data" );
576 uint8_t asrt_count = *data_p;
579 s->segment_run_count = 0;
580 while( asrt_count-- > 0 &&
582 (data_p = parse_asrt( p_this, s, data_p, data_end )) );
584 uint8_t afrt_count = *data_p;
587 s->fragment_run_count = 0;
588 while( afrt_count-- > 0 &&
590 (data_p = parse_afrt( p_this, s, data_p, data_end )) );
593 /* this only works with ANSI characters - this is ok
594 for the bootstrapinfo field which this function is
595 exclusively used for since it is merely a base64 encoding
597 static bool is_whitespace( char c )
607 /* see above note for is_whitespace */
608 static void whitespace_substr( char** start,
611 while( is_whitespace( **start ) && *start != *end ) {
618 while( is_whitespace(*(*end - 1) ) ) {
623 /* returns length (could be zero, indicating all of remaining data) */
624 /* ptr is to start of data, right after 'mdat' string */
625 static uint32_t find_chunk_mdat( vlc_object_t* p_this,
626 uint8_t* chunkdata, uint8_t* chunkdata_end,
629 uint8_t* boxname = NULL;
630 uint8_t* boxdata = NULL;
631 uint64_t boxsize = 0;
634 if( chunkdata_end < chunkdata ||
635 chunkdata_end - chunkdata < 8 )
637 msg_Err( p_this, "Couldn't find mdat in box 1!" );
642 boxsize = (uint64_t)U32_AT( chunkdata );
650 if( chunkdata_end - chunkdata >= 12 )
652 boxsize = U64_AT(chunkdata);
657 msg_Err( p_this, "Couldn't find mdat in box 2!");
662 chunkdata += (boxsize - 16);
667 chunkdata += (boxsize - 8);
669 } while ( 0 != memcmp( boxname, "mdat", 4 ) );
673 return chunkdata_end - ((uint8_t*)boxdata);
676 /* returns data ptr if valid (updating the chunk itself
677 tells the reader that the chunk is safe to read, which is not yet correct)*/
678 static uint8_t* download_chunk( stream_t *s,
680 hds_stream_t* stream, chunk_t* chunk )
682 const char* quality = "";
683 char* server_base = sys->base_url;
684 if( stream->server_entry_count > 0 &&
685 strlen(stream->server_entries[0]) > 0 )
687 server_base = stream->server_entries[0];
690 if( stream->quality_segment_modifier )
692 quality = stream->quality_segment_modifier;
695 const char* movie_id = "";
696 if( stream->url && strlen(stream->url) > 0 )
698 if( isFQUrl( stream->url ) )
700 server_base = stream->url;
704 movie_id = stream->url;
709 if( 0 > asprintf( &fragment_url, "%s/%s%sSeg%u-Frag%u",
714 chunk->frag_num ) ) {
715 msg_Err(s, "Failed to allocate memory for fragment url" );
719 msg_Info(s, "Downloading fragment %s", fragment_url );
721 stream_t* download_stream = stream_UrlNew( s, fragment_url );
722 if( ! download_stream )
724 msg_Err(s, "Failed to download fragment %s", fragment_url );
725 free( fragment_url );
726 chunk->failed = true;
729 free( fragment_url );
731 int64_t size = stream_Size( download_stream );
732 chunk->data_len = (uint32_t) size;
734 if( size > MAX_REQUEST_SIZE )
736 msg_Err(s, "Strangely-large chunk of %"PRIi64" Bytes", size );
740 uint8_t* data = malloc( size );
743 msg_Err(s, "Couldn't allocate chunk" );
747 int read = stream_Read( download_stream, data,
749 chunk->data_len = read;
753 msg_Err( s, "Requested %"PRIi64" bytes, "\
754 "but only got %d", size, read );
755 data = realloc( chunk->data, read );
756 chunk->failed = true;
761 chunk->failed = false;
764 stream_Delete( download_stream );
768 static void* download_thread( void* p )
770 vlc_object_t* p_this = (vlc_object_t*)p;
771 stream_t* s = (stream_t*) p_this;
772 stream_sys_t* sys = s->p_sys;
774 if ( vlc_array_count( sys->hds_streams ) == 0 )
777 // TODO: Change here for selectable stream
778 hds_stream_t* hds_stream = sys->hds_streams->pp_elems[0];
780 int canc = vlc_savecancel();
782 vlc_mutex_lock( & hds_stream->dl_lock );
784 while( ! sys->closed )
786 if( ! hds_stream->chunks_downloadpos )
788 chunk_t* chunk = hds_stream->chunks_head;
789 while(chunk && chunk->data )
794 if( chunk && ! chunk->data )
795 hds_stream->chunks_downloadpos = chunk;
798 while( hds_stream->chunks_downloadpos )
800 chunk_t *chunk = hds_stream->chunks_downloadpos;
802 uint8_t *data = download_chunk( (stream_t*)p_this,
807 if( ! chunk->failed )
810 find_chunk_mdat( p_this,
812 data + chunk->data_len,
813 & chunk->mdat_data );
814 if( chunk->mdat_len == 0 ) {
815 chunk->mdat_len = chunk->data_len - (chunk->mdat_data - data);
817 hds_stream->chunks_downloadpos = chunk->next;
824 vlc_cond_wait( & hds_stream->dl_cond,
825 & hds_stream->dl_lock );
828 vlc_mutex_unlock( & hds_stream->dl_lock );
830 vlc_restorecancel( canc );
834 static chunk_t* generate_new_chunk(
835 vlc_object_t* p_this,
837 hds_stream_t* hds_stream )
839 stream_t* s = (stream_t*) p_this;
840 stream_sys_t *sys = s->p_sys;
841 chunk_t *chunk = chunk_new();
842 unsigned int frun_entry = 0;
845 msg_Err( p_this, "Couldn't allocate new chunk!" );
851 chunk->timestamp = last_chunk->timestamp + last_chunk->duration;
852 chunk->frag_num = last_chunk->frag_num + 1;
856 frun_entry = last_chunk->frun_entry;
861 fragment_run_t* first_frun = hds_stream->fragment_runs;
864 chunk->timestamp = (hds_stream->live_current_time * ((uint64_t)hds_stream->afrt_timescale)) / ((uint64_t)hds_stream->timescale);
868 chunk->timestamp = first_frun->fragment_timestamp;
869 chunk->frag_num = first_frun->fragment_number_start;
873 for( ; frun_entry < hds_stream->fragment_run_count;
876 /* check for discontinuity first */
877 if( hds_stream->fragment_runs[frun_entry].fragment_duration == 0 )
879 if( frun_entry == hds_stream->fragment_run_count - 1 )
881 msg_Err( p_this, "Discontinuity but can't find next timestamp!");
885 chunk->frag_num = hds_stream->fragment_runs[frun_entry+1].fragment_number_start;
886 chunk->duration = hds_stream->fragment_runs[frun_entry+1].fragment_duration;
887 chunk->timestamp = hds_stream->fragment_runs[frun_entry+1].fragment_timestamp;
893 if( chunk->frag_num == 0 )
895 if( frun_entry == hds_stream->fragment_run_count - 1 ||
896 ( chunk->timestamp >= hds_stream->fragment_runs[frun_entry].fragment_timestamp &&
897 chunk->timestamp < hds_stream->fragment_runs[frun_entry+1].fragment_timestamp )
900 fragment_run_t* frun = hds_stream->fragment_runs + frun_entry;
901 chunk->frag_num = frun->fragment_number_start + ( (chunk->timestamp - frun->fragment_timestamp) /
902 frun->fragment_duration );
903 chunk->duration = frun->fragment_duration;
908 if( hds_stream->fragment_runs[frun_entry].fragment_number_start <=
910 (frun_entry == hds_stream->fragment_run_count - 1 ||
911 hds_stream->fragment_runs[frun_entry+1].fragment_number_start > chunk->frag_num ) )
913 chunk->duration = hds_stream->fragment_runs[frun_entry].fragment_duration;
914 chunk->timestamp = hds_stream->fragment_runs[frun_entry].fragment_timestamp +
915 chunk->duration * (chunk->frag_num - hds_stream->fragment_runs[frun_entry].fragment_number_start);
920 if( frun_entry == hds_stream->fragment_run_count )
922 msg_Err( p_this, "Couldn'd find the fragment run!" );
927 unsigned int segment = 0;
928 uint64_t fragments_accum = chunk->frag_num;
929 for( srun_entry = 0; srun_entry < hds_stream->segment_run_count;
932 segment = hds_stream->segment_runs[srun_entry].first_segment +
933 (chunk->frag_num - fragments_accum ) / hds_stream->segment_runs[srun_entry].fragments_per_segment;
935 if( srun_entry + 1 == hds_stream->segment_run_count ||
936 hds_stream->segment_runs[srun_entry+1].first_segment > segment )
942 (hds_stream->segment_runs[srun_entry+1].first_segment -
943 hds_stream->segment_runs[srun_entry].first_segment) *
944 hds_stream->segment_runs[srun_entry].fragments_per_segment );
947 chunk->seg_num = segment;
948 chunk->frun_entry = frun_entry;
952 if( (chunk->timestamp + chunk->duration) / hds_stream->afrt_timescale >= sys->duration_seconds )
961 static void maintain_live_chunks(
962 vlc_object_t* p_this,
963 hds_stream_t* hds_stream
966 if( ! hds_stream->chunks_head )
968 /* just start with the earliest in the abst
969 * maybe it would be better to use the currentMediaTime?
970 * but then we are right on the edge of buffering, esp for
972 hds_stream->chunks_head = generate_new_chunk(
973 p_this, 0, hds_stream );
974 hds_stream->chunks_livereadpos = hds_stream->chunks_head;
977 chunk_t* chunk = hds_stream->chunks_head;
979 while( chunk && ( chunk->timestamp * ((uint64_t)hds_stream->timescale) )
980 / ((uint64_t)hds_stream->afrt_timescale)
981 <= hds_stream->live_current_time )
989 chunk->next = generate_new_chunk( p_this, chunk, hds_stream );
996 vlc_cond_signal( & hds_stream->dl_cond );
998 chunk = hds_stream->chunks_head;
999 while( chunk && chunk->data && chunk->mdat_pos >= chunk->mdat_len && chunk->next )
1001 chunk_t* next_chunk = chunk->next;
1002 chunk_free( chunk );
1006 if( ! hds_stream->chunks_livereadpos )
1007 hds_stream->chunks_livereadpos = hds_stream->chunks_head;
1009 hds_stream->chunks_head = chunk;
1013 static void* live_thread( void* p )
1015 vlc_object_t* p_this = (vlc_object_t*)p;
1016 stream_t* s = (stream_t*) p_this;
1017 stream_sys_t* sys = s->p_sys;
1019 if ( vlc_array_count( sys->hds_streams ) == 0 )
1022 // TODO: Change here for selectable stream
1023 hds_stream_t* hds_stream = sys->hds_streams->pp_elems[0];
1025 int canc = vlc_savecancel();
1029 if( hds_stream->abst_url &&
1030 ( isFQUrl( hds_stream->abst_url ) ) )
1032 if( !( abst_url = strdup( hds_stream->abst_url ) ) )
1037 char* server_base = sys->base_url;
1040 if( 0 > asprintf( &abst_url, "%s/%s",
1042 hds_stream->abst_url ) )
1048 mtime_t last_dl_start_time;
1050 while( ! sys->closed )
1052 last_dl_start_time = mdate();
1053 stream_t* download_stream = stream_UrlNew( p_this, abst_url );
1054 if( ! download_stream )
1056 msg_Err( p_this, "Failed to download abst %s", abst_url );
1060 int64_t size = stream_Size( download_stream );
1061 uint8_t* data = malloc( size );
1062 int read = stream_Read( download_stream, data,
1066 msg_Err( p_this, "Requested %"PRIi64" bytes, " \
1067 "but only got %d", size, read );
1072 vlc_mutex_lock( & hds_stream->abst_lock );
1073 parse_BootstrapData( p_this, hds_stream,
1074 data, data + read );
1075 vlc_mutex_unlock( & hds_stream->abst_lock );
1076 maintain_live_chunks( p_this, hds_stream );
1081 stream_Delete( download_stream );
1084 mwait( last_dl_start_time + ( ((int64_t)hds_stream->fragment_runs[hds_stream->fragment_run_count-1].fragment_duration) * 1000000LL) / ((int64_t)hds_stream->afrt_timescale) );
1091 vlc_restorecancel( canc );
1095 static int parse_Manifest( stream_t *s )
1097 xml_t *vlc_xml = NULL;
1098 xml_reader_t *vlc_reader = NULL;
1099 int type = UNKNOWN_ES;
1100 stream_t *st = s->p_source;
1102 msg_Dbg( s, "Manifest parsing\n" );
1104 vlc_xml = xml_Create( st );
1107 msg_Err( s, "Failed to open XML parser" );
1108 return VLC_EGENERIC;
1111 vlc_reader = xml_ReaderCreate( vlc_xml, st );
1114 msg_Err( s, "Failed to open source for parsing" );
1115 xml_Delete( vlc_xml );
1116 return VLC_EGENERIC;
1121 stream_sys_t *sys = s->p_sys;
1123 sys->duration_seconds = 0;
1125 #define MAX_BOOTSTRAP_INFO 10
1126 bootstrap_info bootstraps[MAX_BOOTSTRAP_INFO];
1127 uint8_t bootstrap_idx = 0;
1128 memset( bootstraps, 0, sizeof(bootstrap_info) * MAX_BOOTSTRAP_INFO );
1130 #define MAX_MEDIA_ELEMENTS 10
1131 media_info medias[MAX_MEDIA_ELEMENTS];
1132 uint8_t media_idx = 0;
1133 memset( medias, 0, sizeof(media_info) * MAX_MEDIA_ELEMENTS );
1135 #define MAX_XML_DEPTH 256
1136 char* element_stack[256];
1137 uint8_t current_element_idx = 0;
1138 char* current_element = NULL;
1139 memset( element_stack, 0, sizeof(char*) * MAX_XML_DEPTH );
1141 const char* attr_name;
1142 const char* attr_value;
1144 char* media_id = NULL;
1146 #define TIMESCALE 10000000
1147 while( (type = xml_ReaderNextNode( vlc_reader, (const char**) &node )) > 0 )
1151 case XML_READER_STARTELEM:
1152 if( current_element_idx == 0 && element_stack[current_element_idx] == 0 ) {
1153 if( !( element_stack[current_element_idx] = strdup( node ) ) )
1156 if ( !( element_stack[++current_element_idx] = strdup( node ) ) )
1161 case XML_READER_ENDELEM:
1162 if( ! strcmp( current_element, "bootstrapInfo") ) {
1163 if( bootstrap_idx + 1 == MAX_BOOTSTRAP_INFO ) {
1164 msg_Warn( (vlc_object_t*) s, "Too many bootstraps, ignoring" );
1170 free( current_element );
1171 element_stack[current_element_idx--] = 0;
1175 if( ! element_stack[current_element_idx] ) {
1179 current_element = element_stack[current_element_idx];
1181 if( type == XML_READER_STARTELEM && ! strcmp( current_element, "media") )
1183 if( media_idx == MAX_MEDIA_ELEMENTS )
1185 msg_Err( (vlc_object_t*) s, "Too many media elements, quitting" );
1186 return VLC_EGENERIC;
1189 while( ( attr_name = xml_ReaderNextAttr( vlc_reader, &attr_value )) )
1191 if( !strcmp(attr_name, "streamId" ) )
1193 if( !( medias[media_idx].stream_id = strdup( attr_value ) ) )
1196 if( !strcmp(attr_name, "url" ) )
1198 if( !( medias[media_idx].media_url = strdup( attr_value ) ) )
1201 if( !strcmp(attr_name, "bootstrapInfoId" ) )
1203 if( !( medias[media_idx].bootstrap_id = strdup( attr_value ) ) )
1211 if( type == XML_READER_STARTELEM && ! strcmp( current_element, "bootstrapInfo") )
1213 while( ( attr_name = xml_ReaderNextAttr( vlc_reader, &attr_value )) )
1215 if( !strcmp(attr_name, "url" ) )
1217 if( !( bootstraps[bootstrap_idx].url = strdup( attr_value ) ) )
1220 if( !strcmp(attr_name, "id" ) )
1222 if( !( bootstraps[bootstrap_idx].id = strdup( attr_value ) ) )
1225 if( !strcmp(attr_name, "profile" ) )
1227 if( !( bootstraps[bootstrap_idx].profile = strdup( attr_value ) ) )
1233 if( type == XML_READER_TEXT )
1235 if( ! strcmp( current_element, "bootstrapInfo" ) )
1238 char* end = start + strlen(start);
1239 whitespace_substr( &start, &end );
1242 bootstraps[bootstrap_idx].data_len =
1243 vlc_b64_decode_binary( (uint8_t**)&bootstraps[bootstrap_idx].data, start );
1244 if( ! bootstraps[bootstrap_idx].data )
1246 msg_Err( (vlc_object_t*) s, "Couldn't decode bootstrap info" );
1249 if( ! strcmp( current_element, "duration" ) )
1251 double shutup_gcc = atof( node );
1252 sys->duration_seconds = (uint64_t) shutup_gcc;
1254 if( ! strcmp( current_element, "id" ) )
1256 if( current_element &&
1257 ! strcmp( element_stack[current_element_idx-1], "manifest" ) )
1259 if( !( media_id = strdup( node ) ) )
1266 xml_ReaderDelete( vlc_reader );
1267 xml_Delete( vlc_xml );
1269 for( int i = 0; i <= media_idx; i++ )
1271 for( int j = 0; j < bootstrap_idx; j++ )
1273 if( ( ! medias[i].bootstrap_id && ! bootstraps[j].id ) ||
1274 (medias[i].bootstrap_id && bootstraps[j].id &&
1275 ! strcmp( medias[i].bootstrap_id, bootstraps[j].id ) ) )
1277 hds_stream_t* new_stream = malloc(sizeof(hds_stream_t));
1278 memset( new_stream, 0, sizeof(hds_stream_t));
1280 vlc_mutex_init( & new_stream->abst_lock );
1281 vlc_mutex_init( & new_stream->dl_lock );
1282 vlc_cond_init( & new_stream->dl_cond );
1284 if( sys->duration_seconds )
1293 if( medias[i].media_url )
1295 if( !(new_stream->url = strdup( medias[i].media_url ) ) )
1304 parse_BootstrapData( (vlc_object_t*)s,
1307 bootstraps[j].data + bootstraps[j].data_len );
1309 new_stream->download_leadtime = 15;
1311 new_stream->chunks_head = generate_new_chunk(
1312 (vlc_object_t*) s, 0, new_stream );
1313 chunk_t* chunk = new_stream->chunks_head;
1314 uint64_t total_duration = chunk->duration;
1315 while( chunk && total_duration/new_stream->afrt_timescale < new_stream->download_leadtime )
1317 chunk->next = generate_new_chunk(
1318 (vlc_object_t*) s, chunk, new_stream );
1319 chunk = chunk->next;
1321 total_duration += chunk->duration;
1326 if( !(new_stream->abst_url = strdup( bootstraps[j].url ) ) )
1333 vlc_array_append( sys->hds_streams, new_stream );
1335 msg_Info( (vlc_object_t*)s, "New track with quality_segment(%s), timescale(%u), movie_id(%s), segment_run_count(%d), fragment_run_count(%u)",
1336 new_stream->quality_segment_modifier?"":new_stream->quality_segment_modifier, new_stream->timescale,
1337 new_stream->movie_id, new_stream->segment_run_count, new_stream->fragment_run_count );
1343 for( int i = 0; i < MAX_MEDIA_ELEMENTS; i++ )
1345 FREENULL( medias[media_idx].stream_id );
1346 FREENULL( medias[media_idx].media_url );
1347 FREENULL( medias[media_idx].bootstrap_id );
1350 for( int i = 0; i < MAX_BOOTSTRAP_INFO; i++ )
1352 FREENULL( bootstraps[i].data );
1353 FREENULL( bootstraps[i].id );
1354 FREENULL( bootstraps[i].url );
1355 FREENULL( bootstraps[i].profile );
1358 FREENULL( media_id );
1364 static void hds_free( hds_stream_t *p_stream )
1366 FREENULL( p_stream->quality_segment_modifier );
1368 FREENULL( p_stream->abst_url );
1370 FREENULL( p_stream->url );
1371 FREENULL( p_stream->movie_id );
1372 for( int i = 0; i < p_stream->server_entry_count; i++ )
1374 FREENULL( p_stream->server_entries[i] );
1377 chunk_t* chunk = p_stream->chunks_head;
1380 chunk_t* next = chunk->next;
1381 chunk_free( chunk );
1388 static void SysCleanup( stream_sys_t *p_sys )
1390 if ( p_sys->hds_streams )
1392 for ( int i=0; i< p_sys->hds_streams->i_count ; i++ )
1393 hds_free( p_sys->hds_streams->pp_elems[i] );
1394 vlc_array_destroy( p_sys->hds_streams );
1396 free( p_sys->base_url );
1399 static int Open( vlc_object_t *p_this )
1401 stream_t *s = (stream_t*)p_this;
1402 stream_sys_t *p_sys;
1405 return VLC_EGENERIC;
1407 msg_Info( p_this, "HTTP Dynamic Streaming (%s)", s->psz_path );
1409 s->p_sys = p_sys = calloc( 1, sizeof(*p_sys ) );
1410 if( unlikely( p_sys == NULL ) )
1414 if( unlikely( asprintf( &uri, "%s://%s", s->psz_access, s->psz_path ) < 0 ) )
1420 /* remove the last part of the url */
1421 char *pos = strrchr( uri, '/');
1423 p_sys->base_url = uri;
1425 p_sys->flv_header_bytes_sent = 0;
1427 p_sys->hds_streams = vlc_array_new();
1429 if( parse_Manifest( s ) != VLC_SUCCESS )
1436 s->pf_control = Control;
1438 if( vlc_clone( &p_sys->dl_thread, download_thread, s, VLC_THREAD_PRIORITY_INPUT ) )
1444 msg_Info( p_this, "Live stream detected" );
1446 if( vlc_clone( &p_sys->live_thread, live_thread, s, VLC_THREAD_PRIORITY_INPUT ) )
1455 SysCleanup( p_sys );
1457 return VLC_EGENERIC;
1460 static void Close( vlc_object_t *p_this )
1462 stream_t *s = (stream_t*)p_this;
1463 stream_sys_t *p_sys = s->p_sys;
1465 // TODO: Change here for selectable stream
1466 hds_stream_t *stream = vlc_array_count(p_sys->hds_streams) ?
1467 s->p_sys->hds_streams->pp_elems[0] : NULL;
1469 p_sys->closed = true;
1471 vlc_cond_signal( & stream->dl_cond );
1473 vlc_join( p_sys->dl_thread, NULL );
1477 vlc_mutex_destroy( &stream->dl_lock );
1478 vlc_cond_destroy( &stream->dl_cond );
1479 vlc_mutex_destroy( &stream->abst_lock );
1484 vlc_join( p_sys->live_thread, NULL );
1487 SysCleanup( p_sys );
1491 static unsigned char flv_header[] = {
1496 0x5, //indicates audio and video
1500 0x9, // length of header
1504 0x0, // initial "trailer"
1507 static int send_flv_header( stream_sys_t* p_sys, void* buffer, unsigned i_read,
1510 uint32_t to_be_read = i_read;
1511 if( to_be_read > 13 - p_sys->flv_header_bytes_sent ) {
1512 to_be_read = 13 - p_sys->flv_header_bytes_sent;
1515 memcpy( buffer, flv_header + p_sys->flv_header_bytes_sent, to_be_read );
1519 p_sys->flv_header_bytes_sent += to_be_read;
1524 static unsigned read_chunk_data(
1525 vlc_object_t* p_this,
1526 uint8_t* buffer, unsigned read_len,
1527 hds_stream_t* stream,
1531 stream_t* s = (stream_t*) p_this;
1532 stream_sys_t* sys = s->p_sys;
1533 chunk_t* chunk = stream->chunks_head;
1534 uint8_t* buffer_start = buffer;
1537 if( chunk && chunk->eof && chunk->mdat_pos >= chunk->mdat_len ) {
1542 while( chunk && chunk->data && read_len > 0 && ! (chunk->eof && chunk->mdat_pos >= chunk->mdat_len ) )
1544 /* in the live case, it is necessary to store the next
1545 * pointer here, since as soon as we increment the mdat_pos, that
1546 * chunk may be deleted */
1547 chunk_t* next = chunk->next;
1549 if( chunk->mdat_pos < chunk->mdat_len )
1551 unsigned cp_len = chunk->mdat_len - chunk->mdat_pos;
1552 if( cp_len > read_len )
1554 memcpy( buffer, chunk->mdat_data + chunk->mdat_pos,
1559 chunk->mdat_pos += cp_len;
1562 if( ! sys->live && (chunk->mdat_pos >= chunk->mdat_len || chunk->failed) )
1569 /* make sure there is at least one chunk in the queue */
1570 if( ! chunk->next && ! chunk->eof )
1572 chunk->next = generate_new_chunk( p_this, chunk, stream );
1578 chunk_free( chunk );
1580 stream->chunks_head = chunk;
1583 else if( sys->live && (chunk->mdat_pos >= chunk->mdat_len || chunk->failed) )
1591 stream->chunks_livereadpos = chunk;
1594 /* new chunk generation is handled by a different thread in live case */
1597 chunk = stream->chunks_head;
1600 uint64_t total_duration = chunk->duration;
1601 while( chunk && total_duration/stream->afrt_timescale < stream->download_leadtime && ! chunk->eof )
1603 if( ! chunk->next && ! chunk->eof )
1605 chunk->next = generate_new_chunk( p_this, chunk, stream );
1611 chunk = chunk->next;
1614 total_duration += chunk->duration;
1621 vlc_cond_signal( & stream->dl_cond );
1624 return ( ((uint8_t*)buffer) - ((uint8_t*)buffer_start));
1627 static int Read( stream_t *s, void *buffer, unsigned i_read )
1629 stream_sys_t *p_sys = s->p_sys;
1631 if ( vlc_array_count( p_sys->hds_streams ) == 0 )
1634 // TODO: change here for selectable stream
1635 hds_stream_t *stream = s->p_sys->hds_streams->pp_elems[0];
1638 uint8_t *buffer_uint8 = (uint8_t*) buffer;
1640 unsigned hdr_bytes = send_flv_header( p_sys, buffer, i_read, false );
1641 length += hdr_bytes;
1642 i_read -= hdr_bytes;
1643 buffer_uint8 += hdr_bytes;
1646 while( i_read > 0 && ! eof )
1648 int tmp_length = read_chunk_data( (vlc_object_t*)s, buffer_uint8, i_read, stream, &eof );
1649 buffer_uint8 += tmp_length;
1650 i_read -= tmp_length;
1651 length += tmp_length;
1657 static int Peek( stream_t *s, const uint8_t **pp_peek, unsigned i_peek )
1659 stream_sys_t *p_sys = s->p_sys;
1661 if ( vlc_array_count( p_sys->hds_streams ) == 0 )
1664 // TODO: change here for selectable stream
1665 hds_stream_t *stream = p_sys->hds_streams->pp_elems[0];
1667 if( p_sys->flv_header_bytes_sent < 13 )
1669 *pp_peek = flv_header + p_sys->flv_header_bytes_sent;
1670 return 13 - p_sys->flv_header_bytes_sent;
1673 if( stream->chunks_head && ! stream->chunks_head->failed && stream->chunks_head->data )
1675 // TODO: change here for selectable stream
1676 chunk_t* chunk = stream->chunks_head;
1677 *pp_peek = chunk->mdat_data + chunk->mdat_pos;
1678 if( chunk->mdat_len - chunk->mdat_pos < i_peek )
1680 return chunk->mdat_len - chunk->mdat_pos;
1692 static int Control( stream_t *s, int i_query, va_list args )
1696 case STREAM_CAN_SEEK:
1697 *(va_arg( args, bool * )) = false;
1699 case STREAM_CAN_FASTSEEK:
1700 case STREAM_CAN_PAUSE: /* TODO */
1701 *(va_arg( args, bool * )) = false;
1703 case STREAM_CAN_CONTROL_PACE:
1704 *(va_arg( args, bool * )) = true;
1706 case STREAM_GET_PTS_DELAY:
1707 *va_arg (args, int64_t *) = INT64_C(1000) *
1708 var_InheritInteger(s, "network-caching");
1711 return VLC_EGENERIC;