Esempio n. 1
0
        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());
        }
        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);
            }
        }