diff --git a/Aurora/RemoteImpl/RemoteSyncImpl.cs b/Aurora/RemoteImpl/RemoteSyncImpl.cs
index faa6a0f..ddfc214 100644
--- a/Aurora/RemoteImpl/RemoteSyncImpl.cs
+++ b/Aurora/RemoteImpl/RemoteSyncImpl.cs
@@ -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");
diff --git a/Aurora/Services/PlayerService/PlayerService.cs b/Aurora/Services/PlayerService/PlayerService.cs
index b82e5a3..c2cfc93 100644
--- a/Aurora/Services/PlayerService/PlayerService.cs
+++ b/Aurora/Services/PlayerService/PlayerService.cs
@@ -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
}
}
- ///
- /// Async method to synchronize music playback with host
- ///
- ///
- ///
- private async Task Sync(RemoteSyncService.RemoteSyncServiceClient remoteSyncClient)
- {
- CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
- using (AsyncServerStreamingCall 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);
- }
- }
- }
-
///
/// Pause currently loaded media.
///
diff --git a/Aurora/Utils/TimeUtils.cs b/Aurora/Utils/TimeUtils.cs
index 720e9a7..9d0ce53 100644
--- a/Aurora/Utils/TimeUtils.cs
+++ b/Aurora/Utils/TimeUtils.cs
@@ -1,13 +1,27 @@
-using System;
+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