thibauts / node-castv2

An implementation of the Chromecast CASTV2 protocol
MIT License
768 stars 100 forks source link

Add example for YouTube communication #41

Closed WeeJeWel closed 7 years ago

WeeJeWel commented 7 years ago

Great library, and heavily documented! However I can't seem to get YouTube to work. Launching the app works, but actually sending a video doesn't seem to do anything. My (pretty ugly) code is as follows:

var Client = require('castv2').Client;
var mdns = require('mdns');

var browser = mdns.createBrowser(mdns.tcp('googlecast'));

browser.on('serviceUp', function(service) {
  console.log('found device %s at %s:%d', service.name, service.addresses[0], service.port);

  if( service.name === 'Chromecast-691672b72f7f6dcc5584dc30d70xxxxx' ) {
    ondeviceup(service.addresses[0]);
  }

  browser.stop();
});

browser.start();

function ondeviceup(host) {

  var client = new Client();
  client.connect(host, function() {
    // create various namespace handlers
    var connection = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.tp.connection', 'JSON');
    var heartbeat  = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.tp.heartbeat', 'JSON');
    var receiver   = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.receiver', 'JSON');
    var media           = client.createChannel('sender-0', 'receiver-0', 'urn:x-cast:com.google.cast.media', 'JSON');

    // establish virtual connection to the receiver
    connection.send({ type: 'CONNECT' });

    // start heartbeating
    setInterval(function() {
      heartbeat.send({ type: 'PING' });
    }, 5000);

    // launch YouTube app
    receiver.send({ type: 'LAUNCH', appId: 'YouTube', requestId: 1 });

    // display receiver status updates
    receiver.on('message', function(data, broadcast) {

        console.log(data)
        if( data.status.applications ) {
            data.status.applications.forEach(function(app){
                if( app.appId !== 'YouTube' ) return;

                console.log(app.sessionId);

                var connection = client.createChannel('sender-0', app.sessionId, 'urn:x-cast:com.google.cast.tp.connection', 'JSON');
                    connection.send({ type: 'CONNECT' });

                var yt = client.createChannel('sender-0', app.sessionId, 'urn:x-cast:com.google.youtube.mdx', 'JSON');
                    yt.send({
                        type: 'flingVideo',
                        data: {
                          currentTime: 0,
                          videoId: 'JMl8cQjBfqk'
                        },
                        requestId: 1
                    });
            })
        }

    });

  });

}

How would one play a YouTube video using this library?

thibauts commented 7 years ago

It will probably be easier to use castv2-client with castv2-youtube.

WeeJeWel commented 7 years ago

I've found castv2-youtube unstable in many scenarios. I assume it shouldn't be that hard to write up an example, right? Or is some fundamental concept missing in this llibrary that makes it harder than I can foresee?

thibauts commented 7 years ago

castv2-youtube is the example you're looking for. I could not do it better if I wanted to. If you have stability issues with it, you may want to file issues on its repo.

Honestly it's the best you, me, or anybody else can do.

WeeJeWel commented 7 years ago

Alright, thanks!