Added a little more stability (not really)

This commit is contained in:
watsonb8 2019-11-29 19:27:31 -05:00
parent 85ab39defd
commit ad1fca2009
3 changed files with 29 additions and 59 deletions

View File

@ -33,16 +33,13 @@ namespace Aurora.RemoteImpl
while (continueSync)
{
DateTime time = Utils.TimeUtils.GetNetworkTime();
float position = PlayerService.Instance.CurrentMediaPosition;
Utils.Time networkTime = Utils.TimeUtils.GetNetworkTime();
float length = PlayerService.Instance.CurrentMediaLength;
float trackTime = length * position;
Sync sync = new Sync()
{
TrackTime = trackTime,
ServerTime = time.Ticks
TrackTime = length * PlayerService.Instance.CurrentMediaPosition,
ServerTime = networkTime.DateTime.Ticks + networkTime.Elapsed.Ticks
};
await responseStream.WriteAsync(sync);
Console.WriteLine("Sent Sync");

View File

@ -129,13 +129,15 @@ namespace Aurora.Services.PlayerService
Sync sync = new Sync(syncStream.ResponseStream.Current);
if (sync != null)
{
Utils.Time time = Utils.TimeUtils.GetNetworkTime();
//Adjust position based on sync
DateTime localTime = Utils.TimeUtils.GetNetworkTime();
//Get offset converted to milliseconds
float offset = ((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000);
DateTime localTime = time.DateTime;
//Get offset - elapsed time converted to milliseconds
float offset = (((localTime.Ticks - sync.ServerTime) * 100) / (1000 * 1000));
float length = CurrentMediaLength;
float newPosition = (sync.TrackTime + offset) / length;
float newPosition = (sync.TrackTime + (offset + time.Elapsed.Milliseconds)) / length;
//Adjust position if greater than 10 percent difference
float oldPosition = _mediaPlayer.Position;
@ -143,7 +145,7 @@ namespace Aurora.Services.PlayerService
newPosition - oldPosition < -0.001)
{
_mediaPlayer.Position = newPosition;
Console.WriteLine("Audio synced");
Console.WriteLine(string.Format("Audio synced: oldPosition: {0} newPosition: {1}", oldPosition, newPosition));
}
}
}
@ -166,50 +168,6 @@ namespace Aurora.Services.PlayerService
}
}
/// <summary>
/// Async method to synchronize music playback with host
/// </summary>
/// <param name="remoteSyncClient"></param>
/// <returns></returns>
private async Task Sync(RemoteSyncService.RemoteSyncServiceClient remoteSyncClient)
{
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 newPosition = (sync.TrackTime + offset) / length;
//Adjust position if greater than 10 percent difference
float oldPosition = _mediaPlayer.Position;
if (newPosition - oldPosition > 0.001 ||
newPosition - oldPosition < -0.001)
{
_mediaPlayer.Position = newPosition;
Console.WriteLine("Audio synced");
}
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
}
}
}
/// <summary>
/// Pause currently loaded media.
/// </summary>

View File

@ -1,13 +1,27 @@
using System;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;
namespace Aurora.Utils
{
public class Time
{
public Time(DateTime dateTime, TimeSpan elapsed)
{
this.DateTime = dateTime;
this.Elapsed = elapsed;
}
public DateTime DateTime { get; private set; }
public TimeSpan Elapsed { get; private set; }
}
public static class TimeUtils
{
public static DateTime GetNetworkTime()
public static Time GetNetworkTime()
{
Stopwatch stopwatch = new Stopwatch();
//default Windows time server
const string ntpServer = "time.windows.com";
@ -29,7 +43,7 @@ namespace Aurora.Utils
//Stops code hang if NTP is blocked
socket.ReceiveTimeout = 3000;
stopwatch.Start();
socket.Send(ntpData);
socket.Receive(ntpData);
socket.Close();
@ -53,8 +67,9 @@ namespace Aurora.Utils
//**UTC** time
var networkDateTime = (new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc)).AddMilliseconds((long)milliseconds);
stopwatch.Stop();
return networkDateTime.ToLocalTime();
return new Time(networkDateTime.ToLocalTime(), stopwatch.Elapsed); ;
}
// stackoverflow.com/a/3294698/162671