2019-11-11 20:10:08 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using Aurora.Services;
|
|
|
|
using Aurora.Proto.Playback;
|
|
|
|
using Aurora.Proto.General;
|
|
|
|
using Aurora.Models.Media;
|
|
|
|
|
|
|
|
namespace Aurora.RemoteImpl
|
|
|
|
{
|
|
|
|
public class RemotePlaybackServiceImpl : RemotePlaybackService.RemotePlaybackServiceBase
|
|
|
|
{
|
|
|
|
public override async Task GetSongStream(SongRequest request,
|
|
|
|
Grpc.Core.IServerStreamWriter<Chunk> responseStream,
|
|
|
|
Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
2019-11-13 01:09:45 +00:00
|
|
|
BaseMedia originalSong = LibraryService.Instance.GetSong(request.Id);
|
|
|
|
if (!(originalSong is LocalAudio))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Copy media object to not interfere with other threads
|
|
|
|
LocalAudio songCopy = new LocalAudio((LocalAudio)originalSong);
|
2019-11-11 20:10:08 +00:00
|
|
|
|
2019-11-13 01:09:45 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
//Load only if not already loaded. (Multiple clients may be requesting media)
|
|
|
|
if (!songCopy.IsLoaded)
|
|
|
|
{
|
|
|
|
await songCopy.Load();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Send stream
|
2019-12-04 22:53:49 +00:00
|
|
|
System.Diagnostics.Debug.WriteLine("Begin sending file");
|
2019-11-13 01:09:45 +00:00
|
|
|
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 });
|
|
|
|
}
|
2019-12-04 22:53:49 +00:00
|
|
|
System.Diagnostics.Debug.WriteLine("Done sending file");
|
2019-11-13 01:09:45 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2019-11-11 20:10:08 +00:00
|
|
|
{
|
2019-12-04 22:53:49 +00:00
|
|
|
System.Diagnostics.Debug.WriteLine("Exception caught while sending audio file: " + ex.Message);
|
2019-11-11 20:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|