Reorganization
This commit is contained in:
22
Aurora/Services/BaseService.cs
Normal file
22
Aurora/Services/BaseService.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using System;
|
||||
|
||||
namespace Aurora.Services
|
||||
{
|
||||
public abstract class BaseService<T> where T : class
|
||||
{
|
||||
private static volatile Lazy<T> _instance = new Lazy<T>(() => CreateInstanceOfT());
|
||||
|
||||
public static T Instance
|
||||
{
|
||||
get { return _instance.Value; }
|
||||
}
|
||||
|
||||
private static T CreateInstanceOfT()
|
||||
{
|
||||
|
||||
return Activator.CreateInstance(typeof(T), true) as T;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
57
Aurora/Services/LibraryService.cs
Normal file
57
Aurora/Services/LibraryService.cs
Normal file
@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using Aurora.Models.Media;
|
||||
using Aurora.Utils;
|
||||
|
||||
namespace Aurora.Services
|
||||
{
|
||||
public class LibraryService : BaseService<LibraryService>
|
||||
{
|
||||
#region Fields
|
||||
private string _pathName = "/Users/brandonwatson/Music/iTunes/iTunes Media/Music";
|
||||
private string _extensions = ".wav,.mp3,.aiff,.flac,.m4a,.m4b,.wma";
|
||||
private Dictionary<string, BaseMedia> _library;
|
||||
|
||||
#endregion Fields
|
||||
|
||||
public LibraryService()
|
||||
{
|
||||
_library = new Dictionary<string, BaseMedia>();
|
||||
LoadLibrary();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the songs.
|
||||
/// </summary>
|
||||
/// <returns>The songs.</returns>
|
||||
public ObservableCollection<BaseMedia> GetLibrary()
|
||||
{
|
||||
ObservableCollection<BaseMedia> collection = new ObservableCollection<BaseMedia>();
|
||||
foreach (KeyValuePair<string, BaseMedia> pair in _library)
|
||||
{
|
||||
collection.Add(pair.Value);
|
||||
}
|
||||
|
||||
return collection;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads library from files.
|
||||
/// </summary>
|
||||
private void LoadLibrary()
|
||||
{
|
||||
//Get songs
|
||||
List<FileInfo> musicFiles = FileSystemUtils.TraverseFoldersAsync(_pathName, _extensions);
|
||||
|
||||
foreach (FileInfo file in musicFiles)
|
||||
{
|
||||
TagLib.File tagFile = TagLib.File.Create(file.FullName);
|
||||
|
||||
BaseMedia song = new LocalAudio(file);
|
||||
_library.Add(song.Id, song);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
17
Aurora/Services/PlayerService/MediaChangedEvent.cs
Normal file
17
Aurora/Services/PlayerService/MediaChangedEvent.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
public delegate void MediaChangedEventHandler(object source, MediaChangedEventArgs e);
|
||||
|
||||
public class MediaChangedEventArgs : EventArgs
|
||||
{
|
||||
public BaseMetadata NewMetadata { get; private set; }
|
||||
|
||||
public MediaChangedEventArgs(BaseMetadata metadata)
|
||||
{
|
||||
NewMetadata = metadata;
|
||||
}
|
||||
}
|
||||
}
|
9
Aurora/Services/PlayerService/PlaybackState.cs
Normal file
9
Aurora/Services/PlayerService/PlaybackState.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System;
|
||||
|
||||
public enum PlaybackState
|
||||
{
|
||||
Playing,
|
||||
Stopped,
|
||||
Buffering,
|
||||
|
||||
}
|
19
Aurora/Services/PlayerService/PlaybackStateChangedEvent.cs
Normal file
19
Aurora/Services/PlayerService/PlaybackStateChangedEvent.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
public delegate void PlaybackStateChangedEventHandler(object source, PlaybackStateChangedEventArgs e);
|
||||
|
||||
public class PlaybackStateChangedEventArgs : EventArgs
|
||||
{
|
||||
public PlaybackState OldState { get; }
|
||||
public PlaybackState NewState { get; }
|
||||
|
||||
public PlaybackStateChangedEventArgs(PlaybackState oldState, PlaybackState newState)
|
||||
{
|
||||
OldState = oldState;
|
||||
NewState = newState;
|
||||
}
|
||||
}
|
||||
}
|
122
Aurora/Services/PlayerService/PlayerService.cs
Normal file
122
Aurora/Services/PlayerService/PlayerService.cs
Normal file
@ -0,0 +1,122 @@
|
||||
using System;
|
||||
using Aurora.Models.Media;
|
||||
using LibVLCSharp.Shared;
|
||||
|
||||
namespace Aurora.Services.PlayerService
|
||||
{
|
||||
public class PlayerService : BaseService<PlayerService>
|
||||
{
|
||||
private BaseMedia _currentMedia;
|
||||
private MediaPlayer _mediaPlayer;
|
||||
private LibVLC _libvlc;
|
||||
private PlaybackState _state;
|
||||
|
||||
public PlayerService()
|
||||
{
|
||||
_libvlc = new LibVLC();
|
||||
_state = PlaybackState.Stopped;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event handler for changing playback states.
|
||||
/// </summary>
|
||||
public event PlaybackStateChangedEventHandler PlaybackStateChanged;
|
||||
|
||||
public event MediaChangedEventHandler MediaChanged;
|
||||
|
||||
/// <summary>
|
||||
/// The state of playback
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public PlaybackState PlaybackState
|
||||
{
|
||||
get { return _state; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Load media into the media player.
|
||||
/// </summary>
|
||||
/// <param name="media">Media to load</param>
|
||||
public void LoadMedia(BaseMedia media)
|
||||
{
|
||||
if (_state == PlaybackState.Playing || _state == PlaybackState.Buffering)
|
||||
{
|
||||
Unload();
|
||||
}
|
||||
_currentMedia = media;
|
||||
_currentMedia.Load();
|
||||
var md = new Media(_libvlc, _currentMedia.DataStream);
|
||||
_mediaPlayer = new MediaPlayer(md);
|
||||
_mediaPlayer.Stopped += OnStopped;
|
||||
md.Dispose();
|
||||
|
||||
MediaChanged.Invoke(this, new MediaChangedEventArgs(_currentMedia.Metadata));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Play currently loaded media.
|
||||
/// </summary>
|
||||
public void Play()
|
||||
{
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Playing;
|
||||
_mediaPlayer.Play();
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Pause currently loaded media.
|
||||
/// </summary>
|
||||
public void Pause()
|
||||
{
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Buffering;
|
||||
_mediaPlayer.Pause();
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Stop currently loaded media.
|
||||
/// </summary>
|
||||
public void Stop()
|
||||
{
|
||||
PlaybackState oldState = _state;
|
||||
_state = PlaybackState.Stopped;
|
||||
_mediaPlayer.Stop();
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
|
||||
}
|
||||
|
||||
public void Enqueue(BaseMedia song)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public void Dequeue(BaseMedia song)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unload currently loaded media.
|
||||
/// </summary>
|
||||
private void Unload()
|
||||
{
|
||||
_currentMedia.Unload();
|
||||
_mediaPlayer.Media = null;
|
||||
_mediaPlayer = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Event fired when currently loaded media player stops.
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="args"></param>
|
||||
private void OnStopped(object sender, EventArgs args)
|
||||
{
|
||||
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(_state, PlaybackState.Stopped));
|
||||
_state = PlaybackState.Stopped;
|
||||
this.Unload();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
80
Aurora/Services/ServerService.cs
Normal file
80
Aurora/Services/ServerService.cs
Normal file
@ -0,0 +1,80 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Grpc.Core;
|
||||
using Aurora.Proto;
|
||||
|
||||
namespace Aurora.Services
|
||||
{
|
||||
public class ServerService : BaseService<ServerService>
|
||||
{
|
||||
private string _hostname = "127.0.0.1";
|
||||
private int _port = 50001;
|
||||
private Grpc.Core.Server _server;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor. Registers GRPC service implementations.
|
||||
/// </summary>
|
||||
public ServerService()
|
||||
{
|
||||
}
|
||||
|
||||
public int Port
|
||||
{
|
||||
get { return _port; }
|
||||
}
|
||||
|
||||
public string Hostname
|
||||
{
|
||||
get { return _hostname; }
|
||||
}
|
||||
|
||||
public void Initialize(string hostname, int port)
|
||||
{
|
||||
this._port = port;
|
||||
this._hostname = hostname;
|
||||
_server = new Grpc.Core.Server
|
||||
{
|
||||
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Start Server
|
||||
/// </summary>
|
||||
public void Start()
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
|
||||
_server.Start();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Shutdown server async.
|
||||
/// </summary>
|
||||
/// <returns>Task</returns>
|
||||
public async Task Stop()
|
||||
{
|
||||
await _server.ShutdownAsync();
|
||||
}
|
||||
|
||||
public async Task Reset()
|
||||
{
|
||||
await Stop();
|
||||
_server = new Grpc.Core.Server
|
||||
{
|
||||
Ports = { new ServerPort("localhost", _port, ServerCredentials.Insecure) }
|
||||
};
|
||||
}
|
||||
|
||||
public void RegisterService(ServerServiceDefinition definition)
|
||||
{
|
||||
_server.Services.Add(definition);
|
||||
}
|
||||
}
|
||||
}
|
44
Aurora/Services/SettingsService.cs
Normal file
44
Aurora/Services/SettingsService.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using Plugin.Settings;
|
||||
using Plugin.Settings.Abstractions;
|
||||
|
||||
namespace Aurora.Services
|
||||
{
|
||||
public class SettingsService : BaseService<SettingsService>
|
||||
{
|
||||
private Lazy<ISettings> _appSettings;
|
||||
|
||||
public SettingsService()
|
||||
{
|
||||
}
|
||||
|
||||
public ISettings AppSettings
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_appSettings == null)
|
||||
{
|
||||
_appSettings = new Lazy<ISettings>(() => CrossSettings.Current, LazyThreadSafetyMode.PublicationOnly);
|
||||
}
|
||||
|
||||
return _appSettings.Value;
|
||||
}
|
||||
set
|
||||
{
|
||||
_appSettings = new Lazy<ISettings>(() => value, LazyThreadSafetyMode.PublicationOnly);
|
||||
}
|
||||
}
|
||||
|
||||
private string _usernameKey = "username";
|
||||
|
||||
public string Username
|
||||
{
|
||||
get { return AppSettings.GetValueOrDefault(_usernameKey, ""); }
|
||||
set
|
||||
{
|
||||
AppSettings.AddOrUpdateValue(_usernameKey, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user