public void PropertiesTest() { var musicFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile.wma"); var item = new TranscodeItem(musicFile, "TestFile.mp3"); Assert.AreEqual(musicFile, item.Source); Assert.AreEqual("TestFile.mp3", item.DestinationFileName); Assert.AreEqual(0, item.Progress); AssertHelper.PropertyChangedEvent(item, x => x.Progress, () => item.Progress = 0.5); Assert.AreEqual(0.5, item.Progress); Assert.IsNull(item.Error); var exception = new InvalidOperationException("Test"); AssertHelper.PropertyChangedEvent(item, x => x.Error, () => item.Error = exception); Assert.AreEqual(exception, item.Error); }
public void AddRemoveTranscodeItemsTest() { var musicFile1 = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile1.wma"); var musicFile2 = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile2.wma"); var item1 = new TranscodeItem(musicFile1, "TestFile1.mp3"); var item2 = new TranscodeItem(musicFile1, "TestFile2.mp3"); var manager = new TranscodingManager(); Assert.IsFalse(manager.TranscodeItems.Any()); manager.AddTranscodeItem(item1); manager.AddTranscodeItem(item2); Assert.IsTrue(new[] { item1, item2 }.SequenceEqual(manager.TranscodeItems)); manager.RemoveTranscodeItem(item1); Assert.AreEqual(item2, manager.TranscodeItems.Single()); }
public void TranscodeStatusTest() { var musicFile = new MockMusicFile(new MusicMetadata(TimeSpan.FromSeconds(33), 320), "TestFile.wma"); var item = new TranscodeItem(musicFile, "TestFile.mp3"); Assert.AreEqual(TranscodeStatus.Pending, item.TranscodeStatus); AssertHelper.PropertyChangedEvent(item, x => x.TranscodeStatus, () => item.Progress = 0.01); Assert.AreEqual(TranscodeStatus.InProgress, item.TranscodeStatus); AssertHelper.PropertyChangedEvent(item, x => x.TranscodeStatus, () => item.Progress = 1); Assert.AreEqual(TranscodeStatus.Completed, item.TranscodeStatus); AssertHelper.PropertyChangedEvent(item, x => x.TranscodeStatus, () => item.Error = new InvalidOperationException()); Assert.AreEqual(TranscodeStatus.Error, item.TranscodeStatus); }
public void RemoveTranscodeItem(TranscodeItem item) { transcodeItems.Remove(item); }
public void AddTranscodeItem(TranscodeItem item) { transcodeItems.Add(item); }
private async Task TranscodeAsyncCore(TranscodeItem transcodeItem, uint bitrate, CancellationToken token) { await throttler.WaitAsync(token); // Throttle the transcoding try { var task = transcoder.Value.TranscodeAsync(transcodeItem.Source.FileName, transcodeItem.DestinationFileName, bitrate, token, new Progress<double>(x => transcodeItem.Progress = x / 100d)); transcodingService.RaiseTranscodingTaskCreated(transcodeItem.DestinationFileName, task); await task; var destinationMusicFile = musicFileContext.Create(transcodeItem.DestinationFileName); var sourceMetadata = await transcodeItem.Source.GetMetadataAsync(); var destinationMetadata = await destinationMusicFile.GetMetadataAsync(); destinationMetadata.ApplyValuesFrom(sourceMetadata); await musicFileContext.SaveChangesAsync(destinationMusicFile); } finally { throttler.Release(); } }
private async void TranscodeAsync(MusicFile musicFile) { var destinationFileName = GetDestinationFileName(musicFile.FileName); var transcodeItem = new TranscodeItem(musicFile, destinationFileName); transcodingManager.AddTranscodeItem(transcodeItem); Logger.Verbose("Start Transcode: {0} > {1}", musicFile.FileName, destinationFileName); var cts = new CancellationTokenSource(); cancellationTokenSources.Add(transcodeItem, cts); UpdateCancelCommands(); var metadata = await musicFile.GetMetadataAsync(); uint bitrate = GetConvertBitrate(metadata.Bitrate); try { await TranscodeAsyncCore(transcodeItem, bitrate, cts.Token); } catch (OperationCanceledException) { transcodingManager.RemoveTranscodeItem(transcodeItem); } catch (Exception ex) { Logger.Error("TranscodeAsync exception: {0}", ex); transcodeItem.Error = ex; } finally { cancellationTokenSources.Remove(transcodeItem); if (allTranscodingsCanceledCompletion != null && !cancellationTokenSources.Any()) { allTranscodingsCanceledCompletion.SetResult(null); } UpdateCancelCommands(); Logger.Verbose("End Transcode: {0} > {1}", musicFile.FileName, destinationFileName); } }