Esempio n. 1
0
            public void Insert_Empty()
            {
                var queue = new MediaQueue();

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                var newMediaFile = new MediaFile();

                queue.Insert(0, newMediaFile);

                Assert.AreEqual(1, queue.Count);
                Assert.AreEqual(newMediaFile, queue.Current);
                Assert.AreEqual(0, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(3, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems?.Count == 1 && e.OldItems == null).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
Esempio n. 2
0
            public void Insert_OutOfRange_Empty()
            {
                var queue = new MediaQueue();

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                var newMediaFile = new MediaFile();

                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    queue.Insert(1, newMediaFile);
                });

                Assert.AreEqual(0, queue.Count);
                Assert.AreEqual(null, queue.Current);
                Assert.AreEqual(-1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(0, propertyChangedEvents.Count);
                Assert.AreEqual(0, collectionChangedEvents.Count);
            }
Esempio n. 3
0
            public void WhenShufflingWorks_OrderIsRandomized()
            {
                var arr = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile()
                };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();

                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.ToggleShuffle();

                Console.WriteLine("Result: {0}", string.Join(",", queue.Cast <MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast <MediaFile>().Select(x => x.Id));
                CollectionAssert.AreEquivalent(queue.Cast <MediaFile>().Select(x => x.Id), arr.Select(x => x.Id));
                Assert.That(queue.Cast <MediaFile>().Select(x => x.Id), Is.Not.Ordered);
                Assert.AreEqual(arr[queue.Index].Id, queue.Cast <MediaFile>().ElementAt(queue.Index).Id, "The current item has been moved");
                Assert.AreEqual(arr.Length, queue.Count, "The array length is different");
            }
Esempio n. 4
0
            public void Remove_CurrentAndLastItem()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.SetIndexAsCurrent(2);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                queue.Remove(tracks[2]);

                Assert.AreEqual(2, queue.Count);
                Assert.AreEqual(tracks[1], queue.Current);
                Assert.AreEqual(1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(3, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems == null && e.OldItems?.Count == 1).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
            public void SetNull()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.SetIndexAsCurrent(1);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                Assert.Throws <ArgumentNullException>(() =>
                {
                    queue[1] = null;
                });

                Assert.AreEqual(3, queue.Count);
                Assert.AreEqual(tracks[1], queue.Current);
                Assert.AreEqual(1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(false, queue.IsShuffled);

                Assert.AreEqual(0, propertyChangedEvents.Count);
                Assert.AreEqual(0, collectionChangedEvents.Count);
            }
            public void Insert_PreCurrent()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.SetIndexAsCurrent(0);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                queue.Insert(0, new MediaFile());

                Assert.AreEqual(2, queue.Count);
                Assert.AreEqual(tracks[0], queue.Current);
                Assert.AreEqual(1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(false, queue.IsShuffled);

                Assert.AreEqual(0, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Current)));
                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Index)));
                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Count)));
                Assert.AreEqual(2, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Count(e => e.NewItems?.Count == 1 && e.OldItems == null));
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
Esempio n. 7
0
            public void SetNotCurrent()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.SetIndexAsCurrent(1);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                queue[0] = new MediaFile();

                Assert.AreEqual(3, queue.Count);
                Assert.AreEqual(tracks[1], queue.Current);
                Assert.AreEqual(1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(0, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems?.Count == 1 && e.OldItems?.Count == 1).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
Esempio n. 8
0
        void OnAppbarPlayButtonClick(object sender, EventArgs args)
        {
            Album thisPagesAlbum = (this.DataContext as AlbumInfo).Album;

            switch (MediaPlayer.State)
            {
            // The MediaPlayer is currently playing so pause it.
            case MediaState.Playing:
                MediaPlayer.Pause();
                break;

            // The MediaPlayer is currently paused...
            case MediaState.Paused:
                MediaQueue queue = MediaPlayer.Queue;

                // so if we're on the same page as the paused song, resume it.
                if (queue.ActiveSong != null &&
                    queue.ActiveSong.Album == thisPagesAlbum)
                {
                    MediaPlayer.Resume();
                }
                // Otherwise, start playing this page's album.
                else
                {
                    goto case MediaState.Stopped;
                }
                break;

            // The MediaPlayer is stopped, so play this page's album.
            case MediaState.Stopped:
                MediaPlayer.Play(thisPagesAlbum.Songs);
                break;
            }
        }
Esempio n. 9
0
            public void Basic()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                queue.Clear();

                Assert.AreEqual(0, queue.Count);
                Assert.AreEqual(null, queue.Current);
                Assert.AreEqual(-1, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Current").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Index").Count());
                Assert.AreEqual(1, propertyChangedEvents.Where(e => e.PropertyName == "Count").Count());
                Assert.AreEqual(3, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Where(e => e.NewItems == null && e.OldItems == null).Count());
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
Esempio n. 10
0
 void SelectedMediaItems_MediaItemAdded(object sender, MediaItemEventArgs e)
 {
     if (!MediaQueue.Contains(e.Item))
     {
         throw new InvalidOperationException("Selected Item doesn't belong to MediaQueue");
     }
 }
Esempio n. 11
0
        public void SourceLoop(MediaQueue mediaQueue)
        {
            while (true)
            {
                try
                {
                    var endPoint  = new IPEndPoint(IPAddress.Any, udpPort);
                    var udpClient = new UdpClient(endPoint)
                    {
                        Client = { ReceiveBufferSize = 1024 * 1024 }
                    };

                    while (true)
                    {
                        var buffer = udpClient.Receive(ref endPoint);

                        var chunk = new Chunk(buffer, buffer.Length);
                        mediaQueue.Write(chunk);
                    }
                }
                catch (Exception e)
                {
                    logger.Error(e.Message);
                }
            }
        }
Esempio n. 12
0
            public void Insert_OutOfRange()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.SetIndexAsCurrent(0);

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                Assert.Throws <ArgumentOutOfRangeException>(() =>
                {
                    queue.Insert(2, new MediaFile());
                });

                Assert.AreEqual(1, queue.Count);
                Assert.AreEqual(tracks[0], queue.Current);
                Assert.AreEqual(0, queue.Index);
                Assert.AreEqual(RepeatType.None, queue.Repeat);
                Assert.AreEqual(RepeatType.None, queue.Shuffle);

                Assert.AreEqual(0, propertyChangedEvents.Count);
                Assert.AreEqual(0, collectionChangedEvents.Count);
            }
Esempio n. 13
0
        /// <summary>
        /// Adds the file to queue.
        /// </summary>
        /// <param name="bytes">The bytes.</param>
        /// <param name="mediaId">The media id.</param>
        /// <param name="file">The file.</param>
        public void AddFileToQueue(byte[] bytes, int mediaId, MediaFile file)
        {
            MediaQueue mediaQueue = new MediaQueue {
                MediaId = mediaId, Filename = file.FilePath, MediaBytes = bytes
            };

            _mediaQueueRepository.Insert(mediaQueue);
        }
Esempio n. 14
0
        public void Initialization()
        {
            var queue = new MediaQueue();

            Assert.AreEqual(0, queue.Count);
            Assert.AreEqual(null, queue.Current);
            Assert.AreEqual(-1, queue.Index);
            Assert.AreEqual(RepeatType.None, queue.Repeat);
            Assert.AreEqual(RepeatType.None, queue.Shuffle);
        }
Esempio n. 15
0
        /// <summary>
        /// Inserts the specified queue.
        /// </summary>
        /// <param name="queue">The queue.</param>
        public void Insert(MediaQueue queue)
        {
            List <DbParameter> parameters = new List <DbParameter>
            {
                database.MakeParameter("@MediaId", queue.MediaId),
                database.MakeParameter("@MediaBytes", queue.MediaBytes),
                database.MakeParameter("@Filename", queue.Filename),
            };

            database.NonQuery("MediaQueue_Insert", parameters);
        }
Esempio n. 16
0
        /// <summary>
        /// Populates the specified reader.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <returns></returns>
        private static MediaQueue Populate(INullableReader reader)
        {
            MediaQueue queue = new MediaQueue
            {
                Id         = reader.GetInt64("MediaQueueId"),
                MediaId    = reader.GetInt32("MediaId"),
                Filename   = reader.GetString("Filename"),
                MediaBytes = (byte[])reader.GetValue("MediaBytes")
            };

            return(queue);
        }
Esempio n. 17
0
            public void WhenAddingItemsWhileShuffled_OrderIsSameAsBeforeButWithExtraItem()
            {
                var arr = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile()
                };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();

                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.ToggleShuffle();
                queue.Add(new MediaFile());

                Console.WriteLine("Shuffled: {0}", string.Join(",", queue.Cast <MediaFile>().Select(x => x.Id)));

                queue.ToggleShuffle();

                Console.WriteLine("Unshuffled: {0}", string.Join(",", queue.Cast <MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast <MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[0].Id, queue.Cast <MediaFile>().ElementAt(0).Id);
                Assert.AreEqual(arr[1].Id, queue.Cast <MediaFile>().ElementAt(1).Id);
                Assert.AreEqual(arr[2].Id, queue.Cast <MediaFile>().ElementAt(2).Id);
                Assert.AreEqual(arr[3].Id, queue.Cast <MediaFile>().ElementAt(3).Id);
                Assert.AreEqual(arr[4].Id, queue.Cast <MediaFile>().ElementAt(4).Id);
                Assert.AreEqual(arr[5].Id, queue.Cast <MediaFile>().ElementAt(5).Id);
                Assert.AreEqual(arr[6].Id, queue.Cast <MediaFile>().ElementAt(6).Id);
                Assert.AreEqual(arr[7].Id, queue.Cast <MediaFile>().ElementAt(7).Id);
                Assert.AreEqual(arr[8].Id, queue.Cast <MediaFile>().ElementAt(8).Id);
                Assert.AreEqual(arr[9].Id, queue.Cast <MediaFile>().ElementAt(9).Id);
                Assert.AreEqual(arr[10].Id, queue.Cast <MediaFile>().ElementAt(10).Id);
                Assert.AreEqual(99, queue.Cast <MediaFile>().Last().Id);
                Assert.AreEqual(arr.Length + 1, queue.Count, "The array length is different");
            }
            public void ShuffledUnshuffled_PropertyUnchanged(
                [Values(nameof(MediaQueue.Count), nameof(MediaQueue.Current))] string propertyName)
            {
                var queue = new MediaQueue {
                    new MediaFile(), new MediaFile()
                };

                var propertyChangedEvents = new List <PropertyChangedEventArgs>();

                queue.PropertyChanged += (sender, e) => propertyChangedEvents.Add(e);

                queue.IsShuffled = true;

                queue.IsShuffled = false;

                Assert.Zero(propertyChangedEvents.Count(e => e.PropertyName == propertyName));
            }
Esempio n. 19
0
            public void ShuffleTwice_WhenRandomNumberGeneratorWorks_OrderIsDifferent()
            {
                var arr = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile()
                };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue1 = new MediaQueue();

                queue1.AddRange(arr);

                var queue2 = new MediaQueue();

                queue2.AddRange(arr);

                // Randomize in quick succession to prove that the result is not the same
                // Using Random() here would create two equal lists, hence the use of PCLCrypto.
                queue1.ToggleShuffle();
                queue2.ToggleShuffle();

                Console.WriteLine("Queue1: {0}", string.Join(",", queue1.Cast <MediaFile>().Select(x => x.Id)));
                Console.WriteLine("Queue2: {0}", string.Join(",", queue2.Cast <MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue1.Cast <MediaFile>().Select(x => x.Id));
                CollectionAssert.AllItemsAreUnique(queue2.Cast <MediaFile>().Select(x => x.Id));
                CollectionAssert.AreNotEqual(queue1.Cast <MediaFile>().Select(x => x.Id), queue2.Cast <MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[queue1.Index].Id, queue1.Cast <MediaFile>().ElementAt(queue1.Index).Id, "The current item has been moved");
                Assert.AreEqual(arr[queue2.Index].Id, queue2.Cast <MediaFile>().ElementAt(queue2.Index).Id, "The current item has been moved");
            }
        /// <summary>
        /// Images the resizer.
        /// </summary>
        public void ResizeTheImages()
        {
            List <MediaFile> mediaFiles = CreateAndInsertMediaFiles();
            MediaQueue       mediaQueue = _mediaQueueRepository.RetrieveQueuedByMediaId(_uploaderMedia.Id)[0];

            foreach (MediaFile file in mediaFiles)
            {
                byte[] bytes = _resizerService.ResizeImage(mediaQueue.MediaBytes, file.PhotoType);

                using (Bitmap bitmap = _queueFileService.GetBitmap(bytes))
                {
                    file.Size   = bytes.LongLength;
                    file.Width  = bitmap.Width;
                    file.Height = bitmap.Height;
                }

                _queueFileService.AddFileToQueue(bytes, _uploaderMedia.Id, file);
            }

            _mediaFileRepository.Save(mediaFiles);
        }
        private void AddActionButtons(bool mediaIsPlaying)
        {
            // Add previous/next button based on media queue
            var canGoPrevious = MediaQueue?.HasPrevious() ?? false;
            var canGoNext     = MediaQueue?.HasNext() ?? false;

            _builder.MActions.Clear();
            if (canGoPrevious)
            {
                _builder.AddAction(GenerateActionCompat(Resource.Drawable.IcMediaPrevious, "Previous",
                                                        MediaServiceBase.ActionPrevious));
            }
            _builder.AddAction(mediaIsPlaying
                ? GenerateActionCompat(Resource.Drawable.IcMediaPause, "Pause", MediaServiceBase.ActionPause)
                : GenerateActionCompat(Resource.Drawable.IcMediaPlay, "Play", MediaServiceBase.ActionPlay));
            if (canGoNext)
            {
                _builder.AddAction(GenerateActionCompat(Resource.Drawable.IcMediaNext, "Next",
                                                        MediaServiceBase.ActionNext));
            }
        }
Esempio n. 22
0
        public void DistributionLoop(MediaQueue source, ConnectionManager connectionManager, Buffer buffer)
        {
            var maxConnectionSize = MaxQueueSize + buffer.Size;

            while (true)
            {
                var chunk = source.ReadBlocking();

                foreach (var connection in connectionManager.GetConnections())
                {
                    try
                    {
                        if (connection.Size > maxConnectionSize)
                        {
                            connection.Dispose();
                            logger.Warning("Buffer overflow in connection");
                        }

                        if (!connection.HasWrites && buffer.Size > 0)
                        {
                            var bufferChunks = buffer.GetChunks();
                            foreach (var bufferChunk in bufferChunks)
                            {
                                connection.Write(bufferChunk);
                            }
                            logger.Info($"Fill connection with {bufferChunks.Count} blocks");
                        }

                        connection.Write(chunk);
                    }
                    catch (Exception e)
                    {
                        logger.Error(e.Message);
                    }
                }

                buffer.Write(chunk);
            }
        }
            public void AddRange_OnlyOneCollectionChangedEvent()
            {
                var queue = new MediaQueue();

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);

                Assert.AreEqual(1, collectionChangedEvents.Count);

                var onlyAddEvents = collectionChangedEvents
                                    .All(changedEvent => changedEvent.Action == NotifyCollectionChangedAction.Add);

                Assert.True(onlyAddEvents);
            }
            public void WhenUnshufflingWorks_OrderIsSameAsBefore()
            {
                var arr = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile(),
                    new MediaFile()
                };

                Console.WriteLine("Original: {0}", string.Join(",", arr.Select(x => x.Id)));

                var queue = new MediaQueue();

                Console.WriteLine("Current Index: {0}", queue.Index);
                queue.AddRange(arr);

                queue.IsShuffled = true;

                Console.WriteLine("Shuffled: {0}", string.Join(",", queue.Cast <MediaFile>().Select(x => x.Id)));

                queue.IsShuffled = false;

                Console.WriteLine("Unshuffled: {0}", string.Join(",", queue.Cast <MediaFile>().Select(x => x.Id)));

                CollectionAssert.AllItemsAreUnique(queue.Cast <MediaFile>().Select(x => x.Id));
                CollectionAssert.AreEqual(queue.Cast <MediaFile>().Select(x => x.Id), arr.Cast <MediaFile>().Select(x => x.Id));
                Assert.AreEqual(arr[queue.Index].Id, queue.Cast <MediaFile>().ElementAt(queue.Index).Id, "The current item has been moved");
                Assert.AreEqual(arr.Length, queue.Count, "The array length is different");
            }
Esempio n. 25
0
        public void SourceLoop(MediaQueue mediaQueue)
        {
            while (true)
            {
                try
                {
                    logger.Info($"Read from {httpUri}");

                    var request        = (HttpWebRequest)WebRequest.Create(httpUri);
                    var responseStream = ((HttpWebResponse)request.GetResponse()).GetResponseStream();
                    while (true)
                    {
                        var buffer = new byte[ReceiveSize * 4];

                        var receivedLength = 0;
                        while (receivedLength < ReceiveSize * 2)
                        {
                            var length = responseStream.Read(buffer, receivedLength, ReceiveSize);
                            if (length == 0)
                            {
                                throw new Exception("Socket was closed, returned 0 bytes");
                            }

                            receivedLength += length;
                        }

                        var chunk = new Chunk(buffer, receivedLength);
                        mediaQueue.Write(chunk);
                    }
                }
                catch (Exception e)
                {
                    logger.Error($"Lost connection to {httpUri}: {e.Message}");
                    Thread.Sleep(2000);
                }
            }
        }
            public void ShuffledAndRepeated()
            {
                var queue = new MediaQueue();

                var tracks = new[]
                {
                    new MediaFile(),
                    new MediaFile(),
                };

                queue.AddRange(tracks);
                queue.IsShuffled = true;
                queue.Repeat     = RepeatType.RepeatOne;

                IList <NotifyCollectionChangedEventArgs> collectionChangedEvents = new List <NotifyCollectionChangedEventArgs>();
                IList <PropertyChangedEventArgs>         propertyChangedEvents   = new List <PropertyChangedEventArgs>();

                queue.CollectionChanged += (sender, e) => collectionChangedEvents.Add(e);
                queue.PropertyChanged   += (sender, e) => propertyChangedEvents.Add(e);

                queue.Clear();

                Assert.AreEqual(0, queue.Count);
                Assert.AreEqual(null, queue.Current);
                Assert.AreEqual(-1, queue.Index);
                Assert.AreEqual(RepeatType.RepeatOne, queue.Repeat);
                Assert.AreEqual(false, queue.IsShuffled);

                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Current)));
                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Index)));
                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.Count)));
                Assert.AreEqual(1, propertyChangedEvents.Count(e => e.PropertyName == nameof(queue.IsShuffled)));
                Assert.AreEqual(4, propertyChangedEvents.Count);
                Assert.AreEqual(1, collectionChangedEvents.Count(e => e.NewItems == null && e.OldItems == null));
                Assert.AreEqual(1, collectionChangedEvents.Count);
            }
 /// <summary>
 /// Adds the file to queue.
 /// </summary>
 /// <param name="bytes">The bytes.</param>
 /// <param name="mediaId">The media id.</param>
 /// <param name="file">The file.</param>
 public void AddFileToQueue(byte[] bytes, int mediaId, MediaFile file)
 {
     MediaQueue mediaQueue = new MediaQueue { MediaId = mediaId, Filename = file.FilePath, MediaBytes = bytes };
     _mediaQueueRepository.Insert(mediaQueue);
 }
 /// <summary>
 /// Populates the specified reader.
 /// </summary>
 /// <param name="reader">The reader.</param>
 /// <returns></returns>
 private static MediaQueue Populate(INullableReader reader)
 {
     MediaQueue queue = new MediaQueue
                            {
                                Id = reader.GetInt64("MediaQueueId"),
                                MediaId = reader.GetInt32("MediaId"),
                                Filename = reader.GetString("Filename"),
                                MediaBytes = (byte[]) reader.GetValue("MediaBytes")
                            };
     return queue;
 }
        /// <summary>
        /// Inserts the specified queue.
        /// </summary>
        /// <param name="queue">The queue.</param>
        public void Insert(MediaQueue queue)
        {
            List<DbParameter> parameters = new List<DbParameter>
                                               {
                                                   database.MakeParameter("@MediaId", queue.MediaId),
                                                    database.MakeParameter("@MediaBytes", queue.MediaBytes),
                                                    database.MakeParameter("@Filename", queue.Filename),
                                               };

            database.NonQuery("MediaQueue_Insert", parameters);
        }
 public static void Play(Media media, MediaQueue queue = default)
 {
     PlayRequest?.Invoke(queue, media);
 }