First pass at syncing worked with some bug fixes

This commit is contained in:
watsonb8
2019-11-12 20:09:45 -05:00
parent 1acc383e90
commit 3398d145ac
8 changed files with 128 additions and 66 deletions

View File

@ -15,19 +15,38 @@ namespace Aurora.RemoteImpl
Grpc.Core.IServerStreamWriter<Chunk> responseStream,
Grpc.Core.ServerCallContext context)
{
BaseMedia song = LibraryService.Instance.GetSong(request.Id);
await song.Load();
//Send stream
Console.WriteLine("Begin sending file");
byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while ((bytesRead = song.DataStream.Read(buffer, 0, buffer.Length)) > 0)
BaseMedia originalSong = LibraryService.Instance.GetSong(request.Id);
if (!(originalSong is LocalAudio))
{
Google.Protobuf.ByteString bufferByteString = Google.Protobuf.ByteString.CopyFrom(buffer);
await responseStream.WriteAsync(new Chunk { Content = bufferByteString });
return;
}
//Copy media object to not interfere with other threads
LocalAudio songCopy = new LocalAudio((LocalAudio)originalSong);
try
{
//Load only if not already loaded. (Multiple clients may be requesting media)
if (!songCopy.IsLoaded)
{
await songCopy.Load();
}
//Send stream
Console.WriteLine("Begin sending file");
byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while ((bytesRead = songCopy.DataStream.Read(buffer, 0, buffer.Length)) > 0)
{
Google.Protobuf.ByteString bufferByteString = Google.Protobuf.ByteString.CopyFrom(buffer);
await responseStream.WriteAsync(new Chunk { Content = bufferByteString });
}
Console.WriteLine("Done sending file");
}
catch (Exception ex)
{
Console.WriteLine("Exception caught while sending audio file: " + ex.Message);
}
Console.WriteLine("Done sending file");
}
}
}

View File

@ -19,18 +19,22 @@ namespace Aurora.RemoteImpl
Grpc.Core.IServerStreamWriter<Sync> responseStream,
Grpc.Core.ServerCallContext context)
{
bool songIsPlaying = true;
PlaybackStateChangedEventHandler playbackStateChanged = (sender, e) =>
bool continueSync = true;
string currentId = PlayerService.Instance.CurrentMedia.Id;
MediaChangedEventHandler mediaChanged = (sender, e) =>
{
songIsPlaying = false;
if (e.NewId != currentId)
{
continueSync = false;
}
};
PlayerService.Instance.PlaybackStateChanged += playbackStateChanged;
PlayerService.Instance.MediaChanged += mediaChanged;
while (songIsPlaying)
while (continueSync)
{
DateTime time = Utils.TimeUtils.GetNetworkTime();
float position = PlayerService.Instance.CurrentMediaTime;
float position = PlayerService.Instance.CurrentMediaPosition;
float length = PlayerService.Instance.CurrentMediaLength;
float trackTime = length * position;
@ -41,7 +45,8 @@ namespace Aurora.RemoteImpl
ServerTime = time.Ticks
};
await responseStream.WriteAsync(sync);
await Task.Delay(10000);
Console.WriteLine("Sent Sync");
await Task.Delay(5000);
}
}
}