webscreens/requestshowmedia — pull request #1 by tidoust (2014-07-07 16:00:29+00:00)
Title: Chromecast devices supported through Google Cast Extension

Hi Anssi,

I thought I'd see whether I could extend the demo to support Chromecast devices, using the JS API exposed by the Google Cast Extension:
https://chrome.google.com/webstore/detail/google-cast/boadgeojelhgndaghljhdicfkmllpafd?hl=en

Obviously, this only works in Google Chrome with the Google Cast Extension and with some available Chromecast device. The demo should behave as before in other contexts though (and also falls back to previous behavior if the user declines to use available Chromecast devices).

The page loads and initializes the Google Cast API on startup, as required. When the user hits the "share" button, the page:
1. tries to create a session on a Chromecast device if the API reports there are such devices available. Chrome will typically prompt the user to select a Chromecast device in the list of devices available.
2. falls back to previous behavior if there are no Chromecast devices around or if the user declines to use a Chromecast device.

Notes:
- if the user declines to use a Chromecast device, the call to `window.open` occurs in a fallback function and Chrome will block it by default. Pop-ups need to be allowed for the fallback to work in that case.
- the video only pauses on the initial page when it is ready to play on the Chromecast device. From a UX perspective, it might be better to pause the video right away though.
- sending the video to a nearby Chromecast device may take a few seconds, the code does not implement any sort of protection mechanism: it must be relatively easy to break the demo if you click around before an action is over.
- the video content-type is hardcoded for the time being. Chromecast needs it to play the video, it seems. The content-type could be read from the video element instead.
- the page keeps the Chromecast session open even if the user stops sharing the video. The session can be terminated through the extension menu. The page could perhaps close the session permanently, not sure which approach is better.

Code successfully tested with an actual Chromecast device.