1 /*****************************************************************************
2 * downloader.c: download thread
3 *****************************************************************************
4 * Copyright (C) 1996-2012 VLC authors and VideoLAN
7 * Author: Frédéric Yhuel <fyhuel _AT_ viotech _DOT_ net>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *****************************************************************************/
27 #include <vlc_common.h>
29 #include <vlc_stream.h>
31 #include <vlc_charset.h>
34 #include "../../demux/mp4/libmp4.h"
36 static bool Replace( char **ppsz_string, off_t off, const char *psz_old,
39 const size_t i_oldlen = strlen( psz_old );
40 const size_t i_newlen = strlen( psz_new );
41 size_t i_stringlen = strlen( *ppsz_string );
43 if ( i_newlen > i_oldlen )
45 i_stringlen += i_newlen - i_oldlen;
46 char *psz_realloc = realloc( *ppsz_string, i_stringlen + 1 );
49 *ppsz_string = psz_realloc;
51 memmove( *ppsz_string + off + i_newlen,
52 *ppsz_string + off + i_oldlen,
53 i_stringlen - off - i_newlen );
54 strncpy( *ppsz_string + off, psz_new, i_newlen );
55 (*ppsz_string)[i_stringlen] = 0;
60 static char *ConstructUrl( const char *psz_template, const char *psz_base_url,
61 const quality_level_t *p_qlevel, const uint64_t i_start_time )
63 char *psz_path = strdup( psz_template );
70 if ( (psz_start = strstr( psz_path, "{bitrate}" )) )
72 char *psz_bitrate = NULL;
73 if ( us_asprintf( &psz_bitrate, "%u", p_qlevel->Bitrate ) < 0 ||
74 ! Replace( &psz_path, psz_start - psz_path, "{bitrate}", psz_bitrate ) )
82 else if ( (psz_start = strstr( psz_path, "{start time}" )) )
84 char *psz_starttime = NULL;
85 if ( us_asprintf( &psz_starttime, "%"PRIu64, i_start_time ) < 0 ||
86 ! Replace( &psz_path, psz_start - psz_path, "{start time}", psz_starttime ) )
88 free( psz_starttime );
92 free( psz_starttime );
94 else if ( (psz_start = strstr( psz_path, "{CustomAttributes}" )) )
96 char *psz_attributes = NULL;
97 FOREACH_ARRAY( const custom_attrs_t *p_attrs, p_qlevel->custom_attrs )
98 if ( asprintf( &psz_attributes,
99 psz_attributes ? "%s,%s=%s" : "%s%s=%s",
100 psz_attributes ? psz_attributes : "",
101 p_attrs->psz_key, p_attrs->psz_value ) < 0 )
104 if ( !psz_attributes ||
105 ! Replace( &psz_path, psz_start - psz_path, "{CustomAttributes}", psz_attributes ) )
107 free( psz_attributes );
111 free( psz_attributes );
118 if( asprintf( &psz_url, "%s/%s", psz_base_url, psz_path ) < 0 )
127 static chunk_t * chunk_Get( sms_stream_t *sms, const uint64_t start_time )
129 int len = vlc_array_count( sms->chunks );
130 for( int i = 0; i < len; i++ )
132 chunk_t * chunk = vlc_array_item_at_index( sms->chunks, i );
133 if( !chunk ) return NULL;
135 if( chunk->start_time <= start_time &&
136 chunk->start_time + chunk->duration > start_time )
144 static unsigned set_track_id( chunk_t *chunk, const unsigned tid )
149 uint8_t *slice = chunk->data;
153 SMS_GET4BYTES( size );
154 SMS_GETFOURCC( type );
155 assert( type == ATOM_moof );
157 SMS_GET4BYTES( size );
158 SMS_GETFOURCC( type );
159 assert( type == ATOM_mfhd );
162 SMS_GET4BYTES( size );
163 SMS_GETFOURCC( type );
164 assert( type == ATOM_traf );
166 SMS_GET4BYTES( size );
167 SMS_GETFOURCC( type );
168 if( type != ATOM_tfhd )
171 unsigned ret = bswap32( ((uint32_t *)slice)[1] );
172 ((uint32_t *)slice)[1] = bswap32( tid );
177 static int sms_Download( stream_t *s, chunk_t *chunk, char *url )
179 stream_sys_t *p_sys = s->p_sys;
181 stream_t *p_ts = stream_UrlNew( s, url );
186 int64_t size = stream_Size( p_ts );
190 chunk->size = (uint64_t) size;
191 chunk->offset = p_sys->download.next_chunk_offset;
192 p_sys->download.next_chunk_offset += chunk->size;
194 chunk->data = malloc( size );
196 if( chunk->data == NULL )
198 stream_Delete( p_ts );
202 int read = stream_Read( p_ts, chunk->data, size );
205 msg_Warn( s, "sms_Download: I requested %"PRIi64" bytes, "\
206 "but I got only %i", size, read );
207 chunk->data = realloc( chunk->data, read );
210 stream_Delete( p_ts );
212 vlc_mutex_lock( &p_sys->download.lock_wait );
213 int index = es_cat_to_index( chunk->type );
214 p_sys->download.lead[index] += chunk->duration;
215 vlc_mutex_unlock( &p_sys->download.lock_wait );
220 #ifdef DISABLE_BANDWIDTH_ADAPTATION
221 static unsigned BandwidthAdaptation( stream_t *s,
222 sms_stream_t *sms, uint64_t bandwidth )
224 VLC_UNUSED(bandwidth);
226 return sms->download_qlvl;
230 static unsigned BandwidthAdaptation( stream_t *s,
231 sms_stream_t *sms, uint64_t bandwidth )
233 if( sms->type != VIDEO_ES )
234 return sms->download_qlvl;
236 uint64_t bw_candidate = 0;
237 quality_level_t *qlevel;
238 unsigned ret = sms->download_qlvl;
240 for( unsigned i = 0; i < sms->qlevel_nb; i++ )
242 qlevel = vlc_array_item_at_index( sms->qlevels, i );
243 if( unlikely( !qlevel ) )
245 msg_Err( s, "Could no get %uth quality level", i );
249 if( qlevel->Bitrate < (bandwidth - bandwidth / 3) &&
250 qlevel->Bitrate > bw_candidate )
252 bw_candidate = qlevel->Bitrate;
261 static int get_new_chunks( stream_t *s, chunk_t *ck )
263 stream_sys_t *p_sys = s->p_sys;
265 uint8_t *slice = ck->data;
268 uint8_t version, fragment_count;
269 uint32_t size, type, flags;
272 TfrfBoxDataFields_t *tfrf_df;
274 sms = SMS_GET_SELECTED_ST( ck->type );
276 SMS_GET4BYTES( size );
277 SMS_GETFOURCC( type );
278 assert( type == ATOM_moof );
280 SMS_GET4BYTES( size );
281 SMS_GETFOURCC( type );
282 assert( type == ATOM_mfhd );
285 SMS_GET4BYTES( size );
286 SMS_GETFOURCC( type );
287 assert( type == ATOM_traf );
291 SMS_GET4BYTES( size );
293 SMS_GETFOURCC( type );
294 if( type == ATOM_mdat )
296 msg_Err( s, "No uuid box found :-(" );
299 else if( type == ATOM_uuid )
301 GetUUID( &uuid, slice);
302 if( !CmpUUID( &uuid, &TfrfBoxUUID ) )
309 SMS_GET1BYTE( version );
310 SMS_GET3BYTES( flags );
311 SMS_GET1BYTE( fragment_count );
313 tfrf_df = calloc( fragment_count, sizeof( TfrfBoxDataFields_t ) );
314 if( unlikely( tfrf_df == NULL ) )
317 for( uint8_t i = 0; i < fragment_count; i++ )
319 SMS_GET4or8BYTES( tfrf_df[i].i_fragment_abs_time );
320 SMS_GET4or8BYTES( tfrf_df[i].i_fragment_duration );
323 msg_Dbg( s, "read box: \"tfrf\" version %d, flags 0x%x, "\
324 "fragment count %"PRIu8, version, flags, fragment_count );
326 for( uint8_t i = 0; i < fragment_count; i++ )
328 uint64_t dur = tfrf_df[i].i_fragment_duration;
329 uint64_t stime = tfrf_df[i].i_fragment_abs_time;
330 msg_Dbg( s, "\"tfrf\" fragment duration %"PRIu64", "\
331 "fragment abs time %"PRIu64, dur, stime);
333 if( !chunk_Get( sms, stime + dur ) )
334 chunk_New( sms, dur, stime );
341 #define STRA_SIZE 334
342 #define SMOO_SIZE (STRA_SIZE * 3 + 24) /* 1026 */
344 /* SmooBox is a very simple MP4 box, used only to pass information
345 * to the demux layer. As this box is not aimed to travel accross networks,
346 * simplicity of the design is better than compactness */
347 static int build_smoo_box( stream_t *s, uint8_t *smoo_box )
349 stream_sys_t *p_sys = s->p_sys;
350 sms_stream_t *sms = NULL;
354 memset( smoo_box, 0, SMOO_SIZE );
355 smoo_box[2] = (SMOO_SIZE & 0xff00)>>8;
356 smoo_box[3] = SMOO_SIZE & 0xff;
362 /* UUID is e1da72ba-24d7-43c3-a6a5-1b5759a1a92c */
363 ((uint32_t *)smoo_box)[2] = bswap32( 0xe1da72ba );
364 ((uint32_t *)smoo_box)[3] = bswap32( 0x24d743c3 );
365 ((uint32_t *)smoo_box)[4] = bswap32( 0xa6a51b57 );
366 ((uint32_t *)smoo_box)[5] = bswap32( 0x59a1a92c );
369 for( int i = 0; i < 3; i++ )
372 int cat = UNKNOWN_ES;
373 stra_box = smoo_box + i * STRA_SIZE;
375 stra_box[26] = (STRA_SIZE & 0xff00)>>8;
376 stra_box[27] = STRA_SIZE & 0xff;
382 /* UUID is b03ef770-33bd-4bac-96c7-bf25f97e2447 */
383 ((uint32_t *)stra_box)[8] = bswap32( 0xb03ef770 );
384 ((uint32_t *)stra_box)[9] = bswap32( 0x33bd4bac );
385 ((uint32_t *)stra_box)[10] = bswap32( 0x96c7bf25 );
386 ((uint32_t *)stra_box)[11] = bswap32( 0xf97e2447 );
388 cat = index_to_es_cat( i );
390 sms = SMS_GET_SELECTED_ST( cat );
392 stra_box[49] = 0; /* reserved */
395 stra_box[50] = (sms->id & 0xff00)>>8;
396 stra_box[51] = sms->id & 0xff;
398 ((uint32_t *)stra_box)[13] = bswap32( sms->timescale );
399 ((uint64_t *)stra_box)[7] = bswap64( p_sys->vod_duration );
401 quality_level_t * qlvl = get_qlevel( sms, sms->download_qlvl );
405 FourCC = qlvl->FourCC ? qlvl->FourCC : sms->default_FourCC;
406 ((uint32_t *)stra_box)[16] = bswap32( FourCC );
407 ((uint32_t *)stra_box)[17] = bswap32( qlvl->Bitrate );
408 ((uint32_t *)stra_box)[18] = bswap32( qlvl->MaxWidth );
409 ((uint32_t *)stra_box)[19] = bswap32( qlvl->MaxHeight );
410 ((uint32_t *)stra_box)[20] = bswap32( qlvl->SamplingRate );
411 ((uint32_t *)stra_box)[21] = bswap32( qlvl->Channels );
412 ((uint32_t *)stra_box)[22] = bswap32( qlvl->BitsPerSample );
413 ((uint32_t *)stra_box)[23] = bswap32( qlvl->AudioTag );
414 ((uint16_t *)stra_box)[48] = bswap16( qlvl->nBlockAlign );
416 if( !qlvl->CodecPrivateData )
418 stra_box[98] = stra_box[99] = stra_box[100] = 0; /* reserved */
419 assert( strlen( qlvl->CodecPrivateData ) < 512 );
420 stra_box[101] = strlen( qlvl->CodecPrivateData ) / 2;
421 uint8_t *binary_cpd = decode_string_hex_to_binary( qlvl->CodecPrivateData );
422 memcpy( stra_box + 102, binary_cpd, stra_box[101] );
430 static chunk_t *build_init_chunk( stream_t *s )
432 chunk_t *ret = calloc( 1, sizeof( chunk_t ) );
433 if( unlikely( ret == NULL ) )
434 goto build_init_chunk_error;
436 ret->size = SMOO_SIZE;
437 ret->data = malloc( SMOO_SIZE );
439 goto build_init_chunk_error;
441 if( build_smoo_box( s, ret->data ) == VLC_SUCCESS)
445 build_init_chunk_error:
447 msg_Err( s, "build_init_chunk failed" );
451 static int Download( stream_t *s, sms_stream_t *sms )
453 stream_sys_t *p_sys = s->p_sys;
455 int index = es_cat_to_index( sms->type );
456 if ( unlikely( index == -1 ) )
458 msg_Err( s, "invalid stream type" );
461 uint64_t start_time = p_sys->download.lead[index];
463 quality_level_t *qlevel = get_qlevel( sms, sms->download_qlvl );
464 if( unlikely( !qlevel ) )
466 msg_Err( s, "Could not get quality level id %u", sms->download_qlvl );
471 chunk_t *chunk = chunk_Get( sms, start_time );
474 msg_Warn( s, "Could not find a chunk for stream %s, "\
475 "start time = %"PRIu64"", sms->name, start_time );
478 if( chunk->data != NULL )
480 /* Segment already downloaded */
481 msg_Warn( s, "Segment already downloaded" );
485 chunk->type = sms->type;
487 char *url = ConstructUrl( sms->url_template, p_sys->base_url,
488 qlevel, chunk->start_time );
491 msg_Err( s, "ConstructUrl returned NULL" );
495 /* sanity check - can we download this chunk on time? */
496 uint64_t avg_bw = sms_queue_avg( p_sys->bws );
497 if( (avg_bw > 0) && (qlevel->Bitrate > 0) )
500 unsigned chunk_duration = chunk->duration * 1000 / sms->timescale;
501 uint64_t size = chunk_duration * qlevel->Bitrate / 1000; /* bits */
502 unsigned estimated = size * 1000 / avg_bw;
503 if( estimated > chunk_duration )
505 msg_Warn( s,"downloading of chunk %d would take %d ms, "\
506 "which is longer than its playback (%d ms)",
507 chunk->sequence, estimated, chunk_duration );
511 mtime_t start = mdate();
512 if( sms_Download( s, chunk, url ) != VLC_SUCCESS )
514 msg_Err( s, "downloaded chunk %u from stream %s at quality"
515 " %u *failed*", chunk->sequence, sms->name, qlevel->Bitrate );
518 mtime_t duration = mdate() - start;
520 unsigned real_id = set_track_id( chunk, sms->id );
523 msg_Err( s, "tfhd box not found or invalid chunk" );
527 //msg_Dbg( s, "chunk ID was %i and is now %i", real_id, sms->id );
530 get_new_chunks( s, chunk );
532 vlc_mutex_lock( &p_sys->download.lock_wait );
533 vlc_array_append( p_sys->download.chunks, chunk );
534 vlc_cond_signal( &p_sys->download.wait );
535 vlc_mutex_unlock( &p_sys->download.lock_wait );
537 msg_Info( s, "downloaded chunk %d from stream %s at quality %u",
538 chunk->sequence, sms->name, qlevel->Bitrate );
540 uint64_t actual_lead = chunk->start_time + chunk->duration;
541 int ind = es_cat_to_index( sms->type );
542 p_sys->download.ck_index[ind] = chunk->sequence;
543 p_sys->download.lead[ind] = __MIN( p_sys->download.lead[ind], actual_lead );
545 if( sms->type == VIDEO_ES ||
546 ( !SMS_GET_SELECTED_ST( VIDEO_ES ) && sms->type == AUDIO_ES ) )
548 p_sys->playback.toffset = __MIN( p_sys->playback.toffset,
549 (uint64_t)chunk->start_time );
552 unsigned dur_ms = __MAX( 1, duration / 1000 );
553 uint64_t bw = chunk->size * 8 * 1000 / dur_ms; /* bits / s */
554 if( sms_queue_put( p_sys->bws, bw ) != VLC_SUCCESS )
556 avg_bw = sms_queue_avg( p_sys->bws );
558 if( sms->type != VIDEO_ES )
561 /* Track could get disabled in mp4 demux if we trigger adaption too soon. */
562 if( chunk->sequence <= 1 )
565 unsigned new_qlevel_id = BandwidthAdaptation( s, sms, avg_bw );
566 quality_level_t *new_qlevel = get_qlevel( sms, new_qlevel_id );
567 if( unlikely( !new_qlevel ) )
569 msg_Err( s, "Could not get quality level id %u", new_qlevel_id );
573 if( new_qlevel->Bitrate != qlevel->Bitrate )
575 msg_Warn( s, "detected %s bandwidth (%u) stream",
576 (new_qlevel->Bitrate >= qlevel->Bitrate) ? "faster" : "lower",
577 new_qlevel->Bitrate );
579 sms->download_qlvl = new_qlevel_id;
582 if( new_qlevel->MaxWidth != qlevel->MaxWidth ||
583 new_qlevel->MaxHeight != qlevel->MaxHeight )
585 chunk_t *new_init_ck = build_init_chunk( s );
591 new_init_ck->offset = p_sys->download.next_chunk_offset;
592 p_sys->download.next_chunk_offset += new_init_ck->size;
594 vlc_mutex_lock( &p_sys->download.lock_wait );
595 vlc_array_append( p_sys->download.chunks, new_init_ck );
596 vlc_array_append( p_sys->init_chunks, new_init_ck );
597 vlc_mutex_unlock( &p_sys->download.lock_wait );
602 static inline uint64_t get_lead( stream_t *s )
604 stream_sys_t *p_sys = s->p_sys;
606 uint64_t alead = p_sys->download.lead[es_cat_to_index( AUDIO_ES )];
607 uint64_t vlead = p_sys->download.lead[es_cat_to_index( VIDEO_ES )];
608 bool video = SMS_GET_SELECTED_ST( VIDEO_ES ) ? true : false;
609 bool audio = SMS_GET_SELECTED_ST( AUDIO_ES ) ? true : false;
612 lead = __MIN( vlead, alead );
618 if( p_sys->playback.toffset < lead )
619 lead -= p_sys->playback.toffset;
625 static int next_track( stream_t *s )
627 stream_sys_t *p_sys = s->p_sys;
628 uint64_t tmp, min = 0;
629 int cat, ret = UNKNOWN_ES;
630 for( int i = 0; i < 3; i++ )
632 tmp = p_sys->download.lead[i];
633 cat = index_to_es_cat( i );
634 if( (!min || tmp < min) && SMS_GET_SELECTED_ST( cat ) )
643 void* sms_Thread( void *p_this )
645 stream_t *s = (stream_t *)p_this;
646 stream_sys_t *p_sys = s->p_sys;
647 sms_stream_t *sms = NULL;
650 int canc = vlc_savecancel();
652 /* We compute the average bandwidth of the 4 last downloaded
653 * chunks, but feel free to replace '4' by whatever you wish */
654 p_sys->bws = sms_queue_init( 4 );
658 chunk_t *init_ck = build_init_chunk( s );
662 vlc_mutex_lock( &p_sys->download.lock_wait );
663 vlc_array_append( p_sys->download.chunks, init_ck );
664 vlc_array_append( p_sys->init_chunks, init_ck );
665 vlc_mutex_unlock( &p_sys->download.lock_wait );
667 p_sys->download.next_chunk_offset = init_ck->size;
669 /* XXX Sometimes, the video stream is cut into pieces of one exact length,
670 * while the audio stream fragments can't be made to match exactly,
671 * and for some reason the n^th advertised video fragment is related to
672 * the n+1^th advertised audio chunk or vice versa */
674 uint64_t start_time = 0, lead = 0;
677 for( int i = 0; i < 3; i++ )
679 sms = SMS_GET_SELECTED_ST( index_to_es_cat( i ) );
680 if( sms && vlc_array_count( sms->chunks ) )
682 chunk = vlc_array_item_at_index( sms->chunks, 0 );
683 p_sys->download.lead[i] = chunk->start_time + p_sys->timescale / 1000;
685 start_time = chunk->start_time;
687 if( Download( s, sms ) != VLC_SUCCESS )
694 /* XXX replace magic number 10 by a value depending on
695 * LookAheadFragmentCount and DVRWindowLength */
696 vlc_mutex_lock( &p_sys->download.lock_wait );
700 vlc_mutex_unlock( &p_sys->download.lock_wait );
704 lead = get_lead( s );
706 if ( stream_Control( s, STREAM_GET_PTS_DELAY, &i_pts_delay ) != VLC_SUCCESS ||
709 i_pts_delay = 10 * p_sys->timescale + start_time;
712 while( !p_sys->b_live && ( lead > (uint64_t) i_pts_delay || NO_MORE_CHUNKS ) )
714 vlc_cond_wait( &p_sys->download.wait, &p_sys->download.lock_wait );
715 lead = get_lead( s );
723 int count = vlc_array_count( p_sys->download.chunks );
725 for( int i = 0; i < count; i++ )
727 ck = vlc_array_item_at_index( p_sys->download.chunks, i );
728 if( unlikely( !ck ) )
731 if( ck->data == NULL )
733 FREENULL( ck->data );
736 vlc_array_destroy( p_sys->download.chunks );
737 p_sys->download.chunks = vlc_array_new();
739 p_sys->playback.toffset = p_sys->time_pos;
740 for( int i = 0; i < 3; i++ )
742 p_sys->download.lead[i] = p_sys->time_pos;
743 p_sys->download.ck_index[i] = 0;
745 p_sys->download.next_chunk_offset = 0;
747 p_sys->playback.boffset = 0;
748 p_sys->playback.index = 0;
750 chunk_t *new_init_ck = build_init_chunk( s );
754 new_init_ck->offset = p_sys->download.next_chunk_offset;
755 p_sys->download.next_chunk_offset += new_init_ck->size;
757 vlc_array_append( p_sys->download.chunks, new_init_ck );
758 vlc_array_append( p_sys->init_chunks, new_init_ck );
759 p_sys->b_tseek = false;
761 vlc_mutex_unlock( &p_sys->download.lock_wait );
763 sms = SMS_GET_SELECTED_ST( next_track( s ) );
765 vlc_mutex_lock( &p_sys->download.lock_wait );
766 unsigned i_ahead = ahead_chunks_count( p_sys, sms );
767 vlc_mutex_unlock( &p_sys->download.lock_wait );
769 if ( vlc_array_count( sms->chunks ) &&
770 ( !p_sys->b_live || i_ahead < p_sys->lookahead_count ) )
772 if( Download( s, sms ) != VLC_SUCCESS )
778 p_sys->b_error = true;
779 msg_Warn(s, "Canceling download thread!");
780 vlc_restorecancel( canc );