func (d *MediaPlayer) onReceiverStatus(status *controllers.ReceiverStatus) error {

	//spew.Dump("Got status", status)

	mediaSession := status.GetSessionByNamespace("urn:x-cast:com.google.cast.media")

	if mediaSession != nil && d.media.DestinationID != *mediaSession.TransportId {
		connection := controllers.NewConnectionController(d.client, "sender-0", *mediaSession.TransportId)
		connection.Connect()

		log.Infof("Connected to media session %s", *mediaSession.TransportId)

		d.media.SetDestinationID(*mediaSession.TransportId)
		d.media.GetStatus(time.Second * 5)
	}

	if mediaSession == nil {
		//d.media = nil
	}

	return d.player.UpdateVolumeState(&channels.VolumeState{
		Level: status.Volume.Level,
		Muted: status.Volume.Muted,
	})
}
Beispiel #2
0
// A simple example, showing how to find a Chromecast using mdns, and request its status.
func main() {

	castService := "_googlecast._tcp.local"

	// Make a channel for results and start listening
	entriesCh := make(chan *mdns.ServiceEntry, 4)
	go func() {
		for entry := range entriesCh {

			if !strings.Contains(entry.Name, castService) {
				return
			}

			fmt.Printf("Got new chromecast: %v\n", entry)

			client, err := castv2.NewClient(entry.Addr, entry.Port)

			if err != nil {
				log.Fatalf("Failed to connect to chromecast %s", entry.Addr)
			}

			//_ = controllers.NewHeartbeatController(client, "[email protected]$p0rt-0", "[email protected]$p0rt-0")

			heartbeat := controllers.NewHeartbeatController(client, "sender-0", "receiver-0")
			heartbeat.Start()

			connection := controllers.NewConnectionController(client, "sender-0", "receiver-0")
			connection.Connect()

			receiver := controllers.NewReceiverController(client, "sender-0", "receiver-0")

			response, err := receiver.GetStatus(time.Second * 5)

			spew.Dump("Status response", response, err)
		}
	}()

	go func() {
		mdns.Query(&mdns.QueryParam{
			Service: castService,
			Domain:  "local",
			Timeout: time.Second * 30,
			Entries: entriesCh,
		})
	}()

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt, os.Kill)

	// Block until a signal is received.
	s := <-c
	fmt.Println("Got signal:", s)
}
func NewMediaPlayer(driver ninja.Driver, conn *ninja.Connection, info map[string]string, client *castv2.Client) (*MediaPlayer, error) {

	name := info["fn"]
	sigs := map[string]string{
		"ninja:manufacturer": "Google",
		"ninja:productName":  "Chromecast",
		"ninja:thingType":    "mediaplayer",
	}

	for k, v := range info {
		sigs["chromecast:"+k] = v
	}

	player, err := devices.CreateMediaPlayerDevice(driver, &model.Device{
		NaturalID:     info["id"],
		NaturalIDType: "chromecast",
		Name:          &name,
		Signatures:    &sigs,
	}, conn)

	if err != nil {
		return nil, err
	}

	device := &MediaPlayer{
		player: player,
		client: client,
	}

	player.ApplyVolume = device.applyVolume
	if err := player.EnableVolumeChannel(true); err != nil {
		player.Log().Fatalf("Failed to enable volume channel: %s", err)
	}

	player.ApplyPlayPause = device.applyPlayPause
	if err := player.EnableControlChannel([]string{"playing", "paused", "stopped", "buffering", "busy", "idle", "inactive"}); err != nil {
		player.Log().Fatalf("Failed to enable control channel: %s", err)
	}

	//_ = controllers.NewHeartbeatController(client, "[email protected]$p0rt-0", "[email protected]$p0rt-0")

	heartbeat := controllers.NewHeartbeatController(client, "sender-0", "receiver-0")
	heartbeat.Start()

	connection := controllers.NewConnectionController(client, "sender-0", "receiver-0")
	connection.Connect()

	device.receiver = controllers.NewReceiverController(client, "sender-0", "receiver-0")

	go func() {
		for msg := range device.receiver.Incoming {
			if err := device.onReceiverStatus(msg); err != nil {
				device.player.Log().Warningf("Failed to update status: %s", err)
			}
		}
	}()

	_, _ = device.receiver.GetStatus(time.Second * 5)

	//spew.Dump("Status response", status, err)

	//spew.Dump("Media namespace?", status.GetSessionByNamespace("urn:x-cast:com.google.cast.media"))

	device.media = controllers.NewMediaController(device.client, "sender-0", "")

	go func() {
		for {
			mediaStatus := <-device.media.Incoming
			if len(mediaStatus) == 0 {
				break
			}
			device.media.MediaSessionID = mediaStatus[0].MediaSessionID
		}
	}()

	return device, nil
}