Search code examples
javascriptgoogle-chromechromecastgoogle-cast-sdk

Skip Chromecast selection when sending from Google Chrome


I'm trying to send data to a chromecast, but I would like to send the data to a certain Chromecast directly, without selecting it in the Google Chrome.

I would like to skip the Chromecast selection before sending data.

This is want to avoid.

enter image description here

I dont want to select the cast but directly cast the data to it.

I've been checking the session object that we get from chrome.cast.initialize and it return something like this:

      {
        "sessionId": "b59f1754-fd13-48cd-b237-4952a69cade4",
        "appId": "5B797F56",
        "displayName": "url-cast-sender",
        "statusText": "URL Cast ready...",
        "receiver": {
          "label": "rTflOUigItAIYPwoZZ87Uv5oK8yI.",
          "friendlyName": "Sala de Juntas",
          "capabilities": [
            "video_out",
            "audio_out"
          ],
          "volume": {
            "controlType": "attenuation",
            "level": 1,
            "muted": false,
            "stepInterval": 0.05000000074505806
          },
          "receiverType": "cast",
          "isActiveInput": null,
          "displayStatus": null
        },
        "senderApps": [],
        "namespaces": [
          {
            "name": "urn:x-cast:com.google.cast.debugoverlay"
          },
          {
            "name": "urn:x-cast:com.url.cast"
          }
        ],
        "media": [],
        "status": "connected",
        "transportId": "b59f1754-fd13-48cd-b237-4952a69cade4"
      };

As you can see there is label there, I've been trying to work with it but nothing.

The way the page request the connection to a chromecast is the following:

// click handlers
document.getElementById('requestSession').onclick = function () {
  chrome.cast.requestSession(sessionListener, onErr);
};

Which seems to be the part that opens the selection alert in Google Chrome.

My work is a fork from url-cast-receiver and you can check a demo here.


Solution

  • Turns out it is not possible from the frontend part.

    So I ended up using a library called SharpCaster created by Tapanila, in which there is a controller that allows you to do this kind of stuff, here you can find an example of it.

    Had some trouble to make it work and also opened an issue in the repository, but ended up fixing it myself, issue #141.

    WebPageCastingTester.cs

    using System.Linq;
    using System.Threading.Tasks;
    using SharpCaster.Controllers;
    using SharpCaster.Services;
    using Xunit;
    
    namespace SharpCaster.Test
    {
        public class WebPageCastingTester
        {
            private ChromecastService _chromecastService;
            public WebPageCastingTester()
            {
                _chromecastService = ChromecastService.Current;
                var device = _chromecastService.StartLocatingDevices().Result;
                _chromecastService.ConnectToChromecast(device.First()).Wait(2000);
            }
    
            [Fact]
            public async void TestingLaunchingSharpCasterDemo()
            {
                var controller =  await _chromecastService.ChromeCastClient.LaunchWeb();
                await Task.Delay(4000);
                Assert.NotNull(_chromecastService.ChromeCastClient.ChromecastStatus.Applications.First(x => x.AppId == WebController.WebAppId));
                await controller.LoadUrl("https://www.windytv.com/");
                await Task.Delay(4000);
                Assert.Equal(_chromecastService.ChromeCastClient.ChromecastStatus.Applications.First(x => x.AppId == WebController.WebAppId).StatusText,
                    "Now Playing: https://www.windytv.com/");
            }
        }
    }