/// <summary> /// Downloads a new chunk /// </summary> /// <param name="baseUrl">the base url of the file to download</param> /// <param name="urlGenerator">the class that will generator the url for this chunk</param> /// <param name="chunk">the chunk we are trying to download</param> /// <param name="completeCallback">the event handler to call when we are complete</param> /// <param name="instance">A unique id to group downloads. Can be used to group audio and video downloads separately</param> public static void Start( string baseUrl, IUrlGenerator urlGenerator, MediaChunk chunk, EventHandler<DownloadCompleteEventArgs> completeCallback, Guid instance) { Tracer.Assert(chunk.Bitrate > 0, String.Format(CultureInfo.InvariantCulture, "Cannot load chunk {0} with zero bitrate.", chunk.Sid)); Downloader downloader = new Downloader(baseUrl, urlGenerator, chunk, completeCallback, instance); downloader.StartDownload(); }
/// <summary> /// Initializes a new instance of the Downloader class /// </summary> /// <param name="baseUrl">the base url of the file to download</param> /// <param name="urlGenerator">the module that generates our url</param> /// <param name="mediaChunk">the chunk we are trying to download</param> /// <param name="completeCallback">the event handler to call when we are complete</param> /// <param name="instance">A unique id to group downloads. Can be used to group audio and video downloads separately</param> private Downloader( string baseUrl, IUrlGenerator urlGenerator, MediaChunk mediaChunk, EventHandler<DownloadCompleteEventArgs> completeCallback, Guid instance) { m_instanceId = instance; m_chunk = mediaChunk; m_url = urlGenerator.GenerateUrlStringForChunk(baseUrl, mediaChunk.StreamId, mediaChunk.ChunkId, mediaChunk.Bitrate, mediaChunk.StartTime, (long)mediaChunk.Duration); m_url = m_url.Replace('\\', '/'); // Assumption: ChunkId always start from 0 // Add random URL modifier for the first 2 video chunks to make sure they are not in cache. // We use the first two chunks to get reliable bandwidth estimates. if (mediaChunk.ChunkId < PacketPairPacketCount && mediaChunk.MediaType == MediaStreamType.Video) { m_url = m_url + "?packetpair=" + DateTime.Now.Ticks; } if (mediaChunk.Url != m_url) { mediaChunk.Url = m_url; } m_downloadCompleteEventHandler = completeCallback; // Add this download object to our table of objects lock (sm_allDownloads) { if (!sm_allDownloads.ContainsKey(m_instanceId)) { sm_allDownloads.Add(m_instanceId, new List<Downloader>(4)); } List<Downloader> allMyDownloads = sm_allDownloads[m_instanceId]; allMyDownloads.Add(this); } }