This repository has been archived on 2020-12-20. You can view files and clone it, but cannot push or open issues or pull requests.
aurora-sharp-desktop/Aurora/Models/Media/RemoteAudio.cs

87 lines
2.6 KiB
C#
Raw Normal View History

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