2019-11-04 04:17:34 +00:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
|
|
|
using System.Threading;
|
2019-11-04 06:43:26 +00:00
|
|
|
using System.Threading.Tasks;
|
2019-11-04 04:17:34 +00:00
|
|
|
using Aurora.Proto.General;
|
2019-11-11 20:10:08 +00:00
|
|
|
using Aurora.Proto.Playback;
|
|
|
|
using Aurora.Proto.Sync;
|
2019-11-04 04:17:34 +00:00
|
|
|
|
|
|
|
namespace Aurora.Models.Media
|
|
|
|
{
|
|
|
|
public class RemoteAudio : BaseMedia
|
|
|
|
{
|
2019-11-11 20:10:08 +00:00
|
|
|
private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient;
|
|
|
|
private RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient;
|
2019-11-04 04:17:34 +00:00
|
|
|
private CancellationTokenSource _cancellationTokenSource;
|
|
|
|
|
|
|
|
#region Constructor
|
2019-11-13 01:09:45 +00:00
|
|
|
public RemoteAudio(string id,
|
|
|
|
bool fromHost,
|
|
|
|
AudioMetadata metadata,
|
2019-11-11 20:10:08 +00:00
|
|
|
RemotePlaybackService.RemotePlaybackServiceClient playbackClient,
|
|
|
|
RemoteSyncService.RemoteSyncServiceClient syncClient)
|
2019-11-04 04:17:34 +00:00
|
|
|
{
|
|
|
|
this.Id = id;
|
2019-11-11 20:10:08 +00:00
|
|
|
this._remotePlaybackClient = playbackClient;
|
|
|
|
this._remoteSyncClient = syncClient;
|
2019-11-04 04:17:34 +00:00
|
|
|
this.Metadata = metadata;
|
2019-11-13 01:09:45 +00:00
|
|
|
this.FromHost = fromHost;
|
2019-11-04 04:17:34 +00:00
|
|
|
|
|
|
|
_cancellationTokenSource = new CancellationTokenSource();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion Constructor
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
|
|
public override BaseMetadata Metadata { get; protected set; }
|
|
|
|
|
|
|
|
public override MediaTypeEnum MediaType
|
|
|
|
{
|
|
|
|
get { return MediaTypeEnum.Audio; }
|
|
|
|
}
|
|
|
|
|
2019-11-11 20:10:08 +00:00
|
|
|
public RemoteSyncService.RemoteSyncServiceClient RemoteSyncClient
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _remoteSyncClient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-13 01:09:45 +00:00
|
|
|
public bool FromHost { get; private set; }
|
2019-11-04 04:17:34 +00:00
|
|
|
#endregion Properties
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Override load method.
|
|
|
|
/// </summary>
|
2019-11-04 06:43:26 +00:00
|
|
|
public override async Task Load()
|
2019-11-04 04:17:34 +00:00
|
|
|
{
|
|
|
|
this.DataStream = new MemoryStream();
|
2019-11-11 20:10:08 +00:00
|
|
|
using (var call = _remotePlaybackClient.GetSongStream(new SongRequest() { Id = this.Id }))
|
2019-11-04 04:17:34 +00:00
|
|
|
{
|
2019-11-13 01:09:45 +00:00
|
|
|
try
|
2019-11-04 04:17:34 +00:00
|
|
|
{
|
2019-11-13 01:09:45 +00:00
|
|
|
while (await call.ResponseStream.MoveNext(_cancellationTokenSource.Token))
|
|
|
|
{
|
|
|
|
Chunk chunk = call.ResponseStream.Current;
|
|
|
|
byte[] buffer = chunk.Content.ToByteArray();
|
2019-11-04 04:17:34 +00:00
|
|
|
|
2019-11-13 01:09:45 +00:00
|
|
|
await this.DataStream.WriteAsync(buffer, 0, buffer.Length);
|
|
|
|
}
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Done receiving stream");
|
2019-11-13 01:09:45 +00:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Exception caught while loading remote audio:" + ex.Message);
|
2019-11-04 04:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 06:43:26 +00:00
|
|
|
await base.Load();
|
2019-11-04 04:17:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Override unload method
|
|
|
|
/// </summary>
|
|
|
|
public override void Unload()
|
|
|
|
{
|
2019-11-04 06:43:26 +00:00
|
|
|
// if (!_cancellationTokenSource.IsCancellationRequested)
|
|
|
|
// {
|
|
|
|
// _cancellationTokenSource.Cancel();
|
2019-11-04 04:17:34 +00:00
|
|
|
|
2019-11-04 06:43:26 +00:00
|
|
|
// //Wait for cancellation
|
|
|
|
// WaitHandle.WaitAny(new[] { _cancellationTokenSource.Token.WaitHandle });
|
|
|
|
// }
|
2019-11-04 04:17:34 +00:00
|
|
|
base.Unload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|