public async Task Play(CancellationToken cancellationToken)
        {
            if (!Uri.TryCreate(Invariant($"https://{device.DeviceIP}/"), UriKind.Absolute, out Uri deviceUri))
            {
                throw new ChromecastException(Invariant($"Failed to create Uri for Chromecast {device.Name} on {device.DeviceIP}"));
            }

            using (var client = new ChromeCastClient(deviceUri))
            {
                await Connect(client, cancellationToken).ConfigureAwait(false);

                var status = await LaunchApplication(client, cancellationToken).ConfigureAwait(false);

                bool resetVolumeBack = false;
                var  currentVolume   = status?.Volume;
                if (volume.HasValue)
                {
                    double chromecastVolume = volume.Value / 100;
                    if (currentVolume != null && ((currentVolume.Level != chromecastVolume) || currentVolume.Muted))
                    {
                        Trace.WriteLine(Invariant($"Setting Volume on Chromecast {device.Name} to {volume.Value}"));
                        status = await client.ReceiverChannel.SetVolume(chromecastVolume, false, cancellationToken).ConfigureAwait(false);

                        Trace.WriteLine(Invariant($"Finished Setting Volume on Chromecast {device.Name} to {volume.Value}"));
                        resetVolumeBack = true;
                    }
                }

                var defaultApplication = GetDefaultApplication(status);

                client.MediaChannel.MessageReceived += MediaChannel_MessageReceived;

                loadedMediaStatus = await LoadMedia(client, defaultApplication, cancellationToken).ConfigureAwait(false);

                if (loadedMediaStatus == null)
                {
                    throw new MediaLoadException(device.DeviceIP.ToString(), "Failed to load");
                }

                await playbackFinished.Task.WaitOnRequestCompletion(cancellationToken).ConfigureAwait(false);

                client.MediaChannel.MessageReceived -= MediaChannel_MessageReceived;

                // Restore the existing volume
                if (resetVolumeBack)
                {
                    Trace.WriteLine(Invariant($"Restoring Volume on Chromecast {device.Name}"));
                    await client.ReceiverChannel.SetVolume(currentVolume.Level, currentVolume.Muted, cancellationToken)
                    .ConfigureAwait(false);

                    Trace.WriteLine(Invariant($"Finished Restoring Volume on Chromecast {device.Name}"));
                }

                this.logger.LogInfo(Invariant($"Played Speech on Chromecast {device.Name}"));
                Trace.WriteLine(Invariant($"Disconnecting Chromecast {device.Name}"));
                await client.Disconnect(cancellationToken).ConfigureAwait(false);

                Trace.WriteLine(Invariant($"Disconnected Chromecast {device.Name}"));
            }
        }
        private async Task <ChromecastStatus> LaunchApplication(ChromeCastClient client, CancellationToken cancellationToken)
        {
            Trace.WriteLine(Invariant($"Launching default app on Chromecast {device.Name}"));
            ChromecastStatus status = await client.ReceiverChannel.GetChromecastStatus(cancellationToken).ConfigureAwait(false);

            var defaultApplication = GetDefaultApplication(status);

            //if (defaultApplication == null)
            //{
            status = await client.ReceiverChannel.LaunchApplication(defaultAppId, cancellationToken).ConfigureAwait(false);

            defaultApplication = GetDefaultApplication(status);
            //}
            //else
            //{
            //    Trace.WriteLine(Invariant($"Default app is already running on Chromecast {device.Name}"));
            //}

            if (defaultApplication == null)
            {
                throw new ChromecastDeviceException(device.Name, "No default app found inspite of launching it");
            }

            await client.ConnectionChannel.ConnectWithDestination(defaultApplication.TransportId, cancellationToken)
            .ConfigureAwait(false);

            Trace.WriteLine(Invariant($"Launched default app on Chromecast {device.Name}"));
            return(status);
        }
Exemplo n.º 3
0
        public static async Task <SharpCasterDemoController> LaunchSharpCaster(this ChromeCastClient client)
        {
            var controller = new SharpCasterDemoController(client);
            await controller.LaunchApplication();

            return(controller);
        }
Exemplo n.º 4
0
        public static async void LoadPlexMedia(this PlexChannel channel, ChromeCastClient client, PlexObject content)
        {
            var mediaObject = PlexMediaData.DataFromContent(content);
            var req         = new LoadRequest(client.CurrentApplicationSessionId, mediaObject, true, 0, mediaObject.CustomData);

            var reqJson = req.ToJson();
            await channel.Write(MessageFactory.Load(client.CurrentApplicationTransportId, reqJson));
        }
Exemplo n.º 5
0
        public static async Task <YouTubeController> LaunchYouTube(this ChromeCastClient client)
        {
            client.MakeSureChannelExist(new YouTubeChannel(client));
            var controller = new YouTubeController(client);
            await controller.LaunchApplication();

            return(controller);
        }
Exemplo n.º 6
0
        public static async Task <PlexController> LaunchPlex(this ChromeCastClient client)
        {
            client.Channels.Add(new PlexChannel(client));
            var controller = new PlexController(client);
            await controller.LaunchApplication();

            return(controller);
        }
 public static void MakeSureChannelExist(this ChromeCastClient client, IEnumerable <IChromecastChannel> channels)
 {
     foreach (var channel in channels)
     {
         if (!client.Channels.Exists(x => x.Namespace == channel.Namespace))
         {
             client.Channels.Add(channel);
         }
     }
 }
        private async Task WaitForTaskCompleteOrDisconnect(ChromeCastClient client, Task task, CancellationToken token)
        {
            var disconnectedTask = disConnectedSource.Task;
            var completedTask    = await Task.WhenAny(disconnectedTask, task, Task.Delay(-1, token)).ConfigureAwait(false);

            if (completedTask == disconnectedTask)
            {
                throw new ChromecastDeviceException(device.Name, "Device got disconnected");
            }
            token.ThrowIfCancellationRequested();
        }
        private async Task Connect(ChromeCastClient client, CancellationToken cancellationToken)
        {
            Trace.WriteLine(Invariant($"Connecting to Chromecast {device.Name} on {device.DeviceIP}"));
            connectedSource          = new TaskCompletionSource <bool>(cancellationToken);
            client.ConnectedChanged += Client_ConnectedChanged;
            await client.ConnectChromecast(cancellationToken).ConfigureAwait(false);

            await connectedSource.Task.WaitOnRequestCompletion(cancellationToken).ConfigureAwait(false);

            client.ConnectedChanged -= Client_ConnectedChanged;

            Trace.WriteLine(Invariant($"Connected to Chromecast {device.Name} on {device.DeviceIP}"));
        }
Exemplo n.º 10
0
        private async Task <MediaStatus> LoadMedia(ChromeCastClient client, ChromecastApplication defaultApplication,
                                                   CancellationToken cancellationToken)
        {
            Trace.WriteLine(Invariant($"Loading Media [{playUri}] in on Chromecast {device.Name}"));

            var metadata = new GenericMediaMetadata()
            {
                title        = "Homeseer",
                metadataType = SharpCaster.Models.Enums.MetadataType.GENERIC,
            };

            var mediaStatus = await client.MediaChannel.LoadMedia(defaultApplication, playUri, null, cancellationToken,
                                                                  metadata : metadata,
                                                                  duration : duration.HasValue?duration.Value.TotalSeconds : 0D).ConfigureAwait(false);

            Trace.WriteLine(Invariant($"Loaded Media [{playUri}] in on Chromecast {device.Name}"));
            return(mediaStatus);
        }
Exemplo n.º 11
0
 public YouTubeChannel(ChromeCastClient client) : base(client, Urn)
 {
     MessageReceived += YouTubeChannel_MessageReceived;
 }
Exemplo n.º 12
0
 public PlexChannel(ChromeCastClient client) : base(client, MessageFactory.DialConstants.PlexUrn)
 {
 }
Exemplo n.º 13
0
 public ConnectionChannel(ChromeCastClient client) :
     base(client, MessageFactory.DialConstants.DialConnectionUrn)
 {
 }
Exemplo n.º 14
0
 public ChromecastService()
 {
     DeviceLocator = new DeviceLocator();
     ChromeCastClient = new ChromeCastClient();
 }
Exemplo n.º 15
0
 public MediaChannel(ChromeCastClient client, string nameSpace = "urn:x-cast:com.google.cast.media")
     : base(client, nameSpace)
 {
     MessageReceived += OnMessageReceived;
 }
Exemplo n.º 16
0
 public MediaChannel(ChromeCastClient client) 
     :base(client, MessageFactory.DialConstants.DialMediaUrn)
 {
     MessageReceived += OnMessageReceived;
 }
Exemplo n.º 17
0
 protected ChromecastChannel(ChromeCastClient client, string ns)
 {
     Namespace = ns;
     Client    = client;
 }
Exemplo n.º 18
0
 protected ChromecastChannel(ChromeCastClient client, string ns)
 {
     Namespace = ns;
     Client = client;
 }
Exemplo n.º 19
0
 public YouTubeChannel(ChromeCastClient client) : base(client, MessageFactory.DialConstants.YouTubeUrn)
 {
     MessageReceived += YouTubeChannel_MessageReceived;
 }
 public static void MakeSureChannelExist(this ChromeCastClient client, IChromecastChannel channel)
 {
     client.MakeSureChannelExist(new List <IChromecastChannel> {
         channel
     });
 }
 public SharpCasterDemoController(ChromeCastClient client)
     : base(client, "B3419EF5")
 {
 }
Exemplo n.º 22
0
 public YouTubeController(ChromeCastClient client) : base(client, "233637DE")
 {
     client.Channels.GetYouTubeChannel().ScreenIdChanged += OnScreenIdChanged;
 }
Exemplo n.º 23
0
 public WebController(ChromeCastClient client) : base(client, WebAppId)
 {
 }
Exemplo n.º 24
0
 public PlexController(ChromeCastClient client) : base(client, "9AC194DC")
 {
 }
Exemplo n.º 25
0
 public BaseMediaController(ChromeCastClient client, string applicationId)
     : base(client, applicationId)
 {
 }
 protected ChromecastChannelWithRequestTracking(ChromeCastClient client, string ns) :
     base(client, ns)
 {
 }
Exemplo n.º 27
0
 public ChromecastService()
 {
     DeviceLocator    = new DeviceLocator();
     ChromeCastClient = new ChromeCastClient();
 }
Exemplo n.º 28
0
 public PlexController(ChromeCastClient client) : base(client, "9AC194DC")
 {
 }
Exemplo n.º 29
0
 public ReceiverChannel(ChromeCastClient client) :
     base(client, "urn:x-cast:com.google.cast.receiver")
 {
     MessageReceived += ReceiverChannel_MessageReceived;
 }
Exemplo n.º 30
0
 public async Task ConnectToChromecast(ChromeCast chromecast)
 {
     ConnectedChromecast = chromecast;
     await ChromeCastClient.ConnectChromecast(chromecast.DeviceUri);
 }
Exemplo n.º 31
0
 public YouTubeChannel(ChromeCastClient client) : base(client, MessageFactory.DialConstants.YouTubeUrn)
 {
     MessageReceived += YouTubeChannel_MessageReceived;
 }
Exemplo n.º 32
0
 public YouTubeController(ChromeCastClient client) : base(client, "233637DE")
 {
     client.Channels.GetYouTubeChannel().ScreenIdChanged += OnScreenIdChanged;
 }
Exemplo n.º 33
0
 public HeartbeatChannel(ChromeCastClient client) : 
     base(client, MessageFactory.DialConstants.DialHeartbeatUrn)
 {
     
     MessageReceived += HeartbeatChannel_MessageReceived;
 }
Exemplo n.º 34
0
 public BaseController(ChromeCastClient client, string applicationId)
 {
     Client = client;
     ApplicationId = applicationId;
 }
Exemplo n.º 35
0
 protected BaseController(ChromeCastClient client, string applicationId)
 {
     Client        = client;
     ApplicationId = applicationId;
 }
Exemplo n.º 36
0
 public MediaChannel(ChromeCastClient client)
     : base(client, MessageFactory.DialConstants.DialMediaUrn)
 {
     MessageReceived += OnMessageReceived;
 }
Exemplo n.º 37
0
 public void ConnectToChromecast(Chromecast chromecast)
 {
     ConnectedChromecast = chromecast;
     ChromeCastClient.ConnectChromecast(chromecast.DeviceUri);
 }
Exemplo n.º 38
0
 public WebChannel(ChromeCastClient client) : base(client, Urn)
 {
 }
Exemplo n.º 39
0
 public ReceiverChannel(ChromeCastClient client) :
     base(client, MessageFactory.DialConstants.DialReceiverUrn)
 {
     MessageReceived += ReceiverChannel_MessageReceived;
 }
Exemplo n.º 40
0
 public SharpCasterDemoController(ChromeCastClient client)
     : base(client, SharpCasterApplicationId)
 {
 }
Exemplo n.º 41
0
 public ConnectionChannel(ChromeCastClient client) :
     base(client, "urn:x-cast:com.google.cast.tp.connection")
 {
 }
Exemplo n.º 42
0
 public SharpCasterDemoController(ChromeCastClient client)
     : base(client, "B3419EF5")
 {
 }
Exemplo n.º 43
0
 public BaseMediaController(ChromeCastClient client, string applicationId)
     : base(client, applicationId)
 {
     
 }