private void ReadStreamProgressEventsTest(IStorage storage) { // arrange var eventCount = 0; var receivedBytes = new List <byte>(); var streamSize = 9 * _configuration.BufferBlockSize; var source = DummyData.GenerateRandomBytes(streamSize); using var sourceMemoryStream = new MemoryStream(source); var chunk = new Chunk(0, streamSize - 1) { Timeout = 100, Storage = storage }; var chunkDownloader = new ChunkDownloader(chunk, _configuration); chunkDownloader.DownloadProgressChanged += (s, e) => { eventCount++; receivedBytes.AddRange(e.ReceivedBytes); }; // act chunkDownloader.ReadStream(sourceMemoryStream, new CancellationToken()).Wait(); // assert Assert.AreEqual(streamSize / _configuration.BufferBlockSize, eventCount); Assert.AreEqual(chunkDownloader.Chunk.Length, receivedBytes.Count); Assert.IsTrue(source.SequenceEqual(receivedBytes)); chunkDownloader.Chunk.Clear(); }
private async Task DownloadToFile(MetadataTask task, string pathToSave) { YouTubeVideo vid = task.SelectedVideo.YoutubeVideo; long totalDownloaded = 0; string tempFile = $"{pathToSave}.tmp"; string mp3File = $"{pathToSave}.mp3"; try { task.Status = MetadataState.RUNNING; task.Message = String.Format(Properties.Resources.COMMAND_MSG_URI_SEARCH); using (ChunkDownloader client = new ChunkDownloader()) { client.OnProgress += (new ProgressHandler(task)).HandleProgress; totalDownloaded = await client.DownloadToFile(vid.Uri, tempFile); } task.Message = String.Format(Properties.Resources.COMMAND_MSG_START_MP3, tempFile); FFMpegConverter converter = new FFMpegConverter(); converter.ConvertProgress += (new ConvertProgressHandler(task)).HandleProgress; converter.ConvertMedia(tempFile, mp3File, "mp3"); System.IO.File.Delete(tempFile); task.Status = MetadataState.METADATA; task.Message = String.Format(Properties.Resources.COMMAND_MSG_END_MP3, mp3File); } catch (Exception ex) { task.Status = MetadataState.ERROR; task.Message = ex.Message + Environment.NewLine + ex.StackTrace; } }
private void ReadStreamTest(IStorage storage) { // arrange var streamSize = 20480; var randomlyBytes = DummyData.GenerateRandomBytes(streamSize); var chunk = new Chunk(0, streamSize - 1) { Timeout = 100, Storage = storage }; var chunkDownloader = new ChunkDownloader(chunk, _configuration); using var memoryStream = new MemoryStream(randomlyBytes); // act chunkDownloader.ReadStream(memoryStream, new CancellationToken()).Wait(); // assert Assert.AreEqual(memoryStream.Length, chunkDownloader.Chunk.Storage.GetLength()); var chunkStream = chunkDownloader.Chunk.Storage.OpenRead(); for (int i = 0; i < streamSize; i++) { Assert.AreEqual(randomlyBytes[i], chunkStream.ReadByte()); } chunkDownloader.Chunk.Clear(); }
public async Task <IChunk> DownloadChunk() { var chunkInfo = await ChunkSaver.GetNextEmptyChunk(Destination); var checkedChunkInfo = new ChunkInfo( chunkInfo.Start, Math.Min(ChunkLength, chunkInfo.Length) ); var chunk = await ChunkDownloader.DownloadChunk(checkedChunkInfo); return(chunk); }
public void ReadStreamTimeoutExceptionTest() { // arrange var streamSize = 20480; var randomlyBytes = DummyData.GenerateRandomBytes(streamSize); var chunk = new Chunk(0, streamSize - 1) { Timeout = 100 }; var chunkDownloader = new ChunkDownloader(chunk, _configuration); using var memoryStream = new MemoryStream(randomlyBytes); var canceledToken = new CancellationToken(true); // act async Task CallReadStream() => await chunkDownloader.ReadStream(new MemoryStream(), canceledToken).ConfigureAwait(false); // assert Assert.ThrowsExceptionAsync <OperationCanceledException>(CallReadStream); }
/// <summary> /// starts the entire process /// </summary> public void Start() { if (State == DownloadState.Idle) { State = DownloadState.Start; //creates the downloader thread's containers ChunkDownloaders = new ChunkDownloader[ChunkCount]; for (long i = 0, chunkStart = 0; i < ChunkCount; i++, chunkStart += CHUNK_SIZE_LIMIT) { ChunkDownloaders[i] = new ChunkDownloader( ChunkSource, String.Format(ChunkTarget, i), chunkStart, Math.Min(chunkStart + CHUNK_SIZE_LIMIT - 1, DwnlSize)); } GC.Collect(); //to prevent blocking threads used new Thread(() => { Download(); }).Start(); } }
private async Task DownloadToFile(MetadataTask task, string pathToSave) { YouTubeVideo vid = task.SelectedVideo.YoutubeVideo; long totalDownloaded = 0; try { task.Status = MetadataState.RUNNING; task.Message = Properties.Resources.COMMAND_MSG_URI_SEARCH; using (ChunkDownloader client = new ChunkDownloader()) { client.OnProgress += (new ProgressHandler(task)).HandleProgress; totalDownloaded = await client.DownloadToFile(vid.Uri, pathToSave); } task.Status = MetadataState.METADATA; task.Message = String.Format(Properties.Resources.COMMAND_MSG_SUCCESS_DOWNLOAD, totalDownloaded.GetBeautifiedByteSize(), pathToSave); } catch (Exception ex) { task.Status = MetadataState.ERROR; task.Message = ex.Message + Environment.NewLine + ex.StackTrace; } }
private void EnsureBufferizationStarted() { long notDownloadedSpot = DownloadedFilesInfo.Instance.GetNextNotDownloadedSpot(this._fileId, this.Position); if (notDownloadedSpot < this.Length && (this._fileDownloader == null || this._fileDownloader.DownloadFailed || (notDownloadedSpot < this._fileDownloader.FromByte || notDownloadedSpot - this._fileDownloader.FromByte > (long)(3 * MyMusicStream.DESIRED_CHUNK_SIZE)))) { if (this._fileDownloader != null) { this._fileDownloader.SetStopFlag(); } this._fileDownloader = new ChunkDownloader(this._fileId, this._uri, notDownloadedSpot); this._initialResponse = null; this._fileDownloader.StartDownloading(); } else { if (this._initialResponse == null) { return; } this._initialResponse.Close(); this._initialResponse = null; } }