Stability fixes with streaming

This commit is contained in:
watsonb8 2020-02-05 18:44:48 -05:00
parent aae221801a
commit 89b928bd00
4 changed files with 52 additions and 34 deletions

View File

@ -32,7 +32,6 @@ namespace Aurora.Design.Views.Party
private ObservableCollection<Member> _members; private ObservableCollection<Member> _members;
private ObservableCollection<BaseMedia> _queue; private ObservableCollection<BaseMedia> _queue;
private BaseMedia _selectedMedia; private BaseMedia _selectedMedia;
// private IClientService _client;
private ISettingsService _settingsService; private ISettingsService _settingsService;
private IClientService _clientService; private IClientService _clientService;
private IServerService _serverService; private IServerService _serverService;

View File

@ -58,6 +58,7 @@ namespace Aurora.Models.Media
{ {
try try
{ {
_cancellationTokenSource = new CancellationTokenSource();
while (await call.ResponseStream.MoveNext(_cancellationTokenSource.Token)) while (await call.ResponseStream.MoveNext(_cancellationTokenSource.Token))
{ {
Chunk chunk = call.ResponseStream.Current; Chunk chunk = call.ResponseStream.Current;
@ -80,13 +81,13 @@ namespace Aurora.Models.Media
/// </summary> /// </summary>
public override void Unload() public override void Unload()
{ {
// if (!_cancellationTokenSource.IsCancellationRequested) if (!_cancellationTokenSource.IsCancellationRequested)
// { {
// _cancellationTokenSource.Cancel(); _cancellationTokenSource.Cancel();
// //Wait for cancellation //Wait for cancellation
// WaitHandle.WaitAny(new[] { _cancellationTokenSource.Token.WaitHandle }); WaitHandle.WaitAny(new[] { _cancellationTokenSource.Token.WaitHandle });
// } }
base.Unload(); base.Unload();
} }
} }

View File

@ -16,6 +16,7 @@ namespace Aurora.Services.Player
private MediaPlayer _mediaPlayer; private MediaPlayer _mediaPlayer;
private LibVLC _libvlc; private LibVLC _libvlc;
private PlaybackState _state; private PlaybackState _state;
private CancellationTokenSource _remoteSyncCancellationTokenSource;
public PlayerService() public PlayerService()
{ {
@ -61,7 +62,7 @@ namespace Aurora.Services.Player
{ {
get get
{ {
return _mediaPlayer.Position; return _mediaPlayer != null ? _mediaPlayer.Position : -1;
} }
} }
@ -69,7 +70,7 @@ namespace Aurora.Services.Player
{ {
get get
{ {
return _mediaPlayer.Length; return _mediaPlayer != null ? _mediaPlayer.Length : -1;
} }
} }
@ -105,6 +106,13 @@ namespace Aurora.Services.Player
PlaybackState oldState = _state; PlaybackState oldState = _state;
_state = PlaybackState.Playing; _state = PlaybackState.Playing;
//Cancel sync if not cancelled
if (_remoteSyncCancellationTokenSource != null && !_remoteSyncCancellationTokenSource.IsCancellationRequested)
{
_remoteSyncCancellationTokenSource.Cancel();
_remoteSyncCancellationTokenSource = null;
}
_mediaPlayer.Play(); _mediaPlayer.Play();
//Use sync RPC for remote audio //Use sync RPC for remote audio
if (_currentMedia is RemoteAudio) if (_currentMedia is RemoteAudio)
@ -118,13 +126,13 @@ namespace Aurora.Services.Player
//Task completes when host stops syncing (when a song is complete) //Task completes when host stops syncing (when a song is complete)
Task syncTask = new Task(async () => Task syncTask = new Task(async () =>
{ {
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); _remoteSyncCancellationTokenSource = new CancellationTokenSource();
using (AsyncServerStreamingCall<Sync> syncStream = remotePartyServiceClient using (AsyncServerStreamingCall<Sync> syncStream = remotePartyServiceClient
.SyncMedia(new SyncMediaRequest() { })) .SyncMedia(new SyncMediaRequest() { }))
{ {
try try
{ {
while (await syncStream.ResponseStream.MoveNext(cancellationTokenSource.Token)) while (await syncStream.ResponseStream.MoveNext(_remoteSyncCancellationTokenSource.Token))
{ {
Sync sync = new Sync(syncStream.ResponseStream.Current); Sync sync = new Sync(syncStream.ResponseStream.Current);
if (sync != null) if (sync != null)
@ -159,7 +167,7 @@ namespace Aurora.Services.Player
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message); Console.WriteLine("Exception caught while attempting to sync: " + ex.Message + ": " + ex.StackTrace);
} }
} }
}); });
@ -198,6 +206,13 @@ namespace Aurora.Services.Player
_state = PlaybackState.Stopped; _state = PlaybackState.Stopped;
_mediaPlayer.Stop(); _mediaPlayer.Stop();
//Cancel sync if not cancelled
if (_remoteSyncCancellationTokenSource != null && !_remoteSyncCancellationTokenSource.IsCancellationRequested)
{
_remoteSyncCancellationTokenSource.Cancel();
_remoteSyncCancellationTokenSource = null;
}
if (PlaybackStateChanged != null) if (PlaybackStateChanged != null)
{ {
PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state)); PlaybackStateChanged.Invoke(this, new PlaybackStateChangedEventArgs(oldState, _state));
@ -219,6 +234,10 @@ namespace Aurora.Services.Player
/// </summary> /// </summary>
private void Unload() private void Unload()
{ {
if (_currentMedia == null)
{
return;
}
_currentMedia.Unload(); _currentMedia.Unload();
_currentMedia = null; _currentMedia = null;
_mediaPlayer.Media = null; _mediaPlayer.Media = null;

View File

@ -109,20 +109,12 @@ namespace Aurora.Services.Server.Controllers
{ {
bool continueSync = true; bool continueSync = true;
using (var scope = App.Container.BeginLifetimeScope()) using (var scope = App.Container.BeginLifetimeScope())
{
try
{ {
IPlayer player = scope.Resolve<IPlayer>(); IPlayer player = scope.Resolve<IPlayer>();
string currentId = player.CurrentMedia.Id; string currentId = player.CurrentMedia.Id;
MediaChangedEventHandler mediaChanged = (sender, e) =>
{
if (e.NewId != currentId)
{
continueSync = false;
}
};
player.MediaChanged += mediaChanged;
while (continueSync) while (continueSync)
{ {
float length = player.CurrentMediaLength; float length = player.CurrentMediaLength;
@ -137,6 +129,13 @@ namespace Aurora.Services.Server.Controllers
await Task.Delay(5000); await Task.Delay(5000);
} }
} }
catch (Exception ex)
{
Console.WriteLine("Error sending sync: " + ex.Message);
}
}
} }
} }