First pass at sync working. Need to ignore for special cases

This commit is contained in:
watsonb8
2019-11-11 15:10:08 -05:00
parent 5f035e9bcb
commit 1acc383e90
14 changed files with 296 additions and 44 deletions

View File

@ -1,6 +1,9 @@
using System;
using System.Threading.Tasks;
using System.Threading;
using Grpc.Core;
using Aurora.Models.Media;
using Aurora.Proto.Sync;
using LibVLCSharp.Shared;
namespace Aurora.Services.PlayerService
@ -47,6 +50,22 @@ namespace Aurora.Services.PlayerService
return _currentMedia == media;
}
public float CurrentMediaTime
{
get
{
return _mediaPlayer.Position;
}
}
public long CurrentMediaLength
{
get
{
return _mediaPlayer.Length;
}
}
/// <summary>
/// Load media into the media player.
/// </summary>
@ -78,7 +97,50 @@ namespace Aurora.Services.PlayerService
{
PlaybackState oldState = _state;
_state = PlaybackState.Playing;
_mediaPlayer.Play();
//Use sync RPC for remote audio
if (_currentMedia is RemoteAudio)
{
RemoteAudio media = _currentMedia as RemoteAudio;
RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient = media.RemoteSyncClient;
//Sync playback in a separate task
//Task completes when host stops syncing (when a song is complete)
Task syncTask = new Task(async () =>
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
using (AsyncServerStreamingCall<Sync> syncStream = _remoteSyncClient
.GetMediaSync(new Proto.General.Empty()))
{
try
{
while (await syncStream.ResponseStream.MoveNext(cancellationTokenSource.Token))
{
Sync sync = new Sync(syncStream.ResponseStream.Current);
if (sync != null)
{
//Adjust position based on sync
DateTime localTime = Utils.TimeUtils.GetNetworkTime();
//Get offset converted to milliseconds
float offset = ((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000);
float length = CurrentMediaLength;
float position = (sync.TrackTime + offset) / length;
_mediaPlayer.Position = position;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
}
}
});
syncTask.Start();
}
if (PlaybackStateChanged != null)
{