Search code examples
javascriptchromecastgoogle-castgoogle-cast-sdk

Handling Chromecast load errors in web-based sender


The documentation for a web-based Chromecast sender says that castSession.loadMedia() returns a promise. Therefore, I should be able to catch errors in an async function like so:

const castSession = cast.framework.CastContext.getInstance().getCurrentSession();

const mediaInfo = new chrome.cast.media.MediaInfo(
  'https://example.com/stream.m3u8',
  'application/vnd.apple.mpegurl'
);

const loadRequest = new chrome.cast.media.LoadRequest(mediaInfo);

try {
  await castSession.getSessionObj().loadMedia(loadRequest);
} catch(e) {
  console.error(e);
}

Unfortunately, this doesn't work. Whatever load error I'm getting seems to occur sometime between the downloading of the HLS manifest and before playback. The loadMedia() promise resolves successfully, so there's no error to catch. The sender's developer console has an error:

cast_sender.js:85 Uncaught TypeError: d is not a function
    at cast_sender.js:85
    at V.onMessage (cast_sender.js:91)
    at S.h (cast_sender.js:71)
(anonymous) @ cast_sender.js:85
V.onMessage @ cast_sender.js:91
S.h @ cast_sender.js:71

Clearly, some sort of error is making it back to the client, and it seems that there should be some event handler that isn't set, but I don't see any documentation for how to catch the error.

How can I catch media load errors, general media errors, and any other errors on the Chromecast sender?


Solution

  • chrome.cast.Session and cast.framework.CastSession are different. While CastSession methods returns promises, Session object works with callback. The error occurs because of missing callback when calling session.loadMedia()

    In your case, you have to check docs of chrome.cast.Session object for further operation, or continue your work by calling directly loadMedia from your CastSession object:

    Option 1: continue your work with CastSession object:

    try {
      await castSession.loadMedia(loadRequest);
    } catch(e) {
      console.error(e);
    }
    

    Option 2: Work with Session object

    try {
      const sessionObj = await castSession.getSessionObj();
      await (new Promise((res) => {
        sessionObj.loadMedia(loadRequest, res);
      }));
    } catch(e) {
      console.error(e);
    }
    

    More info:

    https://developers.google.com/cast/docs/reference/chrome/cast.framework.CastSession https://developers.google.com/cast/docs/reference/chrome/chrome.cast.Session