Intégration du support arcast dans le module cast #25

Merged
wpetit merged 2 commits from arcast into master 2024-01-12 14:04:31 +01:00
13 changed files with 283 additions and 37 deletions
Showing only changes of commit 7633ae0419 - Show all commits

View File

@ -53,6 +53,7 @@ import (
_ "forge.cadoles.com/arcad/edge/pkg/storage/driver/sqlite"
// Register casting device supported types
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast"
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast"
"forge.cadoles.com/arcad/edge/pkg/storage/share"

View File

@ -2,6 +2,10 @@ package cast
import (
"github.com/urfave/cli/v2"
// Register casting device supported types
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast"
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast"
)
func Root() *cli.Command {

View File

@ -8,9 +8,6 @@ import (
"forge.cadoles.com/arcad/edge/pkg/module/cast"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
// Register casting device supported types
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast"
)
func ScanCommand() *cli.Command {

5
go.mod
View File

@ -5,6 +5,7 @@ go 1.21.4
toolchain go1.21.5
require (
forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d
github.com/getsentry/sentry-go v0.25.0
github.com/hashicorp/golang-lru/v2 v2.0.7
github.com/hashicorp/mdns v1.0.5
@ -21,12 +22,15 @@ require (
require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/cenkalti/backoff v2.2.1+incompatible // indirect
github.com/charmbracelet/lipgloss v0.7.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/gogo/protobuf v0.0.0-20161014173244-50d1bd39ce4e // indirect
github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 // indirect
github.com/jaevor/go-nanoid v1.3.0 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/lestrrat-go/blackmagic v1.0.1 // indirect
github.com/lestrrat-go/httpcc v1.0.1 // indirect
@ -39,6 +43,7 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/wlynxg/anet v0.0.1 // indirect
go.opentelemetry.io/otel v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
golang.org/x/sync v0.5.0 // indirect

12
go.sum
View File

@ -8,10 +8,14 @@ cloud.google.com/go/logging v1.7.0 h1:CJYxlNNNNAMkHp9em/YEXcfJg+rPDg7YfwoRpMU+t5
cloud.google.com/go/logging v1.7.0/go.mod h1:3xjP2CjkM3ZkO73aj4ASA5wRPGGCRrPIAeNqVNkzY8M=
cloud.google.com/go/longrunning v0.5.1 h1:Fr7TXftcqTudoyRJa113hyaqlGdiBQkp0Gq7tErFDWI=
cloud.google.com/go/longrunning v0.5.1/go.mod h1:spvimkwdz6SPWKEt/XBij79E9fiTkHSQl/fRUUQJYJc=
forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d h1:SPDaDDF5StoprDqop8j8zozs8xK32EEWnUHLccWplKM=
forge.cadoles.com/arcad/arcast v0.0.0-20231220090835-5d0311b7315d/go.mod h1:QR8p4kUScWBcTQ0dE/gR+2ndpKOx77mIDSYGqSF1gms=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/barnybug/go-cast v0.0.0-20201201064555-a87ccbc26692 h1:JW4WZlqyaNWUUahfr7MigeDW6jmtam5cTzzo1lwsFhE=
github.com/barnybug/go-cast v0.0.0-20201201064555-a87ccbc26692/go.mod h1:Au0ipPuCBA7zsOC61SnyrYetm8VT3vo1UJtwHeYke44=
github.com/cenkalti/backoff v2.2.1+incompatible h1:tNowT99t7UNflLxfYYSlKYsBpXdEet03Pg2g16Swow4=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E=
github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
@ -69,6 +73,8 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1 h1:cNb52t5fkWv8ZiicKWnc2eZnhsCCoH7WmRBMIbMp04Q=
github.com/grandcat/zeroconf v1.0.1-0.20230119201135-e4f60f8407b1/go.mod h1:I6CSXU4zCGL08JOk9NbcT0ofAgnIkS/fVXbYzfSoDic=
github.com/hashicorp/go.net v0.0.0-20151006203346-104dcad90073/go.mod h1:hjKkEWcCURg++eb33jQU7oqQcI9XDCnUzHA0oac0k90=
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
@ -81,6 +87,8 @@ github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf h1:FtEj8sfIcaaB
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf/go.mod h1:yrqSXGoD/4EKfF26AOGzscPOgTTJcyAwM2rpixWT+t4=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jaevor/go-nanoid v1.3.0 h1:nD+iepesZS6pr3uOVf20vR9GdGgJW1HPaR46gtrxzkg=
github.com/jaevor/go-nanoid v1.3.0/go.mod h1:SI+jFaPuddYkqkVQoNGHs81navCtH388TcrH0RqFKgY=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/keegancsmith/rpc v1.3.0 h1:wGWOpjcNrZaY8GDYZJfvyxmlLljm3YQWF+p918DXtDk=
@ -168,6 +176,8 @@ github.com/ulikunitz/xz v0.5.11/go.mod h1:nbz6k7qbPmH4IRqmfOplQw/tblSgqTqBwxkY0o
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli/v2 v2.26.0 h1:3f3AMg3HpThFNT4I++TKOejZO8yU55t3JnnSr4S4QEI=
github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/wlynxg/anet v0.0.1 h1:VbkEEgHxPSrRQSiyRd0pmrbcEQAEU2TTb8fb4DmSYoQ=
github.com/wlynxg/anet v0.0.1/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
@ -195,6 +205,7 @@ golang.org/x/net v0.0.0-20161013035702-8b4af36cd21a/go.mod h1:mL1N/T3taQHkDXs73r
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210410081132-afb366fc7cd1/go.mod h1:9tjilg8BloeKEkVJvy7fQ90B1CfIiPueXVOjqfkSzI8=
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
@ -211,6 +222,7 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210426080607-c94f62235c83/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View File

@ -0,0 +1,49 @@
package arcast
import (
"context"
"forge.cadoles.com/arcad/arcast/pkg/client"
"forge.cadoles.com/arcad/edge/pkg/module/cast"
"github.com/pkg/errors"
)
type Client struct {
client *client.Client
addr string
}
// Close implements cast.Client.
func (c *Client) Close() error {
return nil
}
// Load implements cast.Client.
func (c *Client) Load(ctx context.Context, url string) error {
if _, err := c.client.Cast(ctx, c.addr, url); err != nil {
return errors.WithStack(err)
}
return nil
}
// Status implements cast.Client.
func (c *Client) Status(ctx context.Context) (cast.DeviceStatus, error) {
status, err := c.client.Status(ctx, c.addr)
if err != nil {
return nil, errors.WithStack(err)
}
return &DeviceStatus{status}, nil
}
// Unload implements cast.Client.
func (c *Client) Unload(ctx context.Context) error {
if _, err := c.client.Reset(ctx, c.addr); err != nil {
return errors.WithStack(err)
}
return nil
}
var _ cast.Client = &Client{}

View File

@ -0,0 +1,57 @@
package arcast
import (
"context"
"net"
"forge.cadoles.com/arcad/arcast/pkg/client"
"forge.cadoles.com/arcad/arcast/pkg/server"
"forge.cadoles.com/arcad/edge/pkg/module/cast"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/logger"
)
const DeviceTypeArcast cast.DeviceType = "arcast"
type Device struct {
player *client.Player
}
// DeviceHost implements cast.Device.
func (d *Device) DeviceHost() net.IP {
rawPreferredIP, err := server.FindPreferredLocalAddress(d.player.IPs...)
if err != nil {
logger.Error(context.Background(), "could not find preferred local address", logger.CapturedE(errors.WithStack(err)))
return d.player.IPs[0]
}
preferredIP := net.ParseIP(rawPreferredIP)
if preferredIP == nil {
logger.Error(context.Background(), "could not parse device preferred ip", logger.F("rawPreferredIP", rawPreferredIP))
return d.player.IPs[0]
}
return preferredIP
}
// DeviceID implements cast.Device.
func (d *Device) DeviceID() string {
return d.player.ID
}
// DeviceName implements cast.Device.
func (d *Device) DeviceName() string {
return d.player.ID
}
// DevicePort implements cast.Device.
func (d *Device) DevicePort() int {
return d.player.Port
}
// DeviceType implements cast.Device.
func (d *Device) DeviceType() cast.DeviceType {
return DeviceTypeArcast
}
var _ cast.Device = &Device{}

View File

@ -0,0 +1,60 @@
package arcast
import (
"context"
"fmt"
"forge.cadoles.com/arcad/arcast/pkg/client"
"forge.cadoles.com/arcad/edge/pkg/module/cast"
"github.com/pkg/errors"
)
func init() {
cast.Register(DeviceTypeArcast, &Service{
client: client.New(),
})
}
type Service struct {
client *client.Client
}
// Find implements cast.Service.
func (s *Service) Find(ctx context.Context, deviceID string) (cast.Device, error) {
players, err := s.client.Scan(ctx, client.WithPlayerIDs(deviceID))
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return nil, errors.WithStack(err)
}
if len(players) == 0 {
return nil, errors.WithStack(cast.ErrDeviceNotFound)
}
return &Device{players[0]}, nil
}
// NewClient implements cast.Service.
func (s *Service) NewClient(ctx context.Context, device cast.Device) (cast.Client, error) {
return &Client{
client: s.client,
addr: fmt.Sprintf("%s:%d", device.DeviceHost(), device.DevicePort()),
}, nil
}
// Scan implements cast.Service.
func (s *Service) Scan(ctx context.Context) ([]cast.Device, error) {
players, err := s.client.Scan(ctx)
if err != nil && !errors.Is(err, context.DeadlineExceeded) {
return nil, errors.WithStack(err)
}
devices := make([]cast.Device, len(players))
for i, p := range players {
devices[i] = &Device{p}
}
return devices, nil
}
var _ cast.Service = &Service{}

View File

@ -0,0 +1,22 @@
package arcast
import (
"forge.cadoles.com/arcad/arcast/pkg/server"
"forge.cadoles.com/arcad/edge/pkg/module/cast"
)
type DeviceStatus struct {
status *server.StatusResponse
}
// State implements cast.DeviceStatus.
func (s *DeviceStatus) State() string {
return s.status.Status
}
// Title implements cast.DeviceStatus.
func (s *DeviceStatus) Title() string {
return s.status.Title
}
var _ cast.DeviceStatus = &DeviceStatus{}

View File

@ -2,6 +2,7 @@ package cast_test
import (
"context"
"fmt"
"os"
"testing"
"time"
@ -13,6 +14,7 @@ import (
"gitlab.com/wpetit/goweb/logger"
// Register casting device supported types
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/arcast"
_ "forge.cadoles.com/arcad/edge/pkg/module/cast/chromecast"
)
@ -39,8 +41,8 @@ func TestCastLoadURL(t *testing.T) {
t.Logf("DEVICES: %s", spew.Sdump(devices))
if e, g := 1, len(devices); e != g {
t.Fatalf("len(devices): expected '%v', got '%v'", e, g)
if e, g := 1, len(devices); e > g {
t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g)
}
devices, err = cast.ListDevices(ctx, false)
@ -50,33 +52,40 @@ func TestCastLoadURL(t *testing.T) {
t.Logf("CACHED DEVICES: %s", spew.Sdump(devices))
if e, g := 1, len(devices); e != g {
t.Fatalf("len(devices): expected '%v', got '%v'", e, g)
if e, g := 1, len(devices); e > g {
t.Fatalf("len(devices): expected 'value >= %v', got '%v'", e, g)
}
dev := devices[0]
for _, device := range devices {
testName := fmt.Sprintf("%s(%s)", device.DeviceType(), device.DeviceID())
func(device cast.Device) {
t.Run(testName, func(t *testing.T) {
t.Parallel()
ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel2()
ctx, cancel2 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel2()
if err := cast.LoadURL(ctx, dev.DeviceID(), "https://go.dev"); err != nil {
t.Error(errors.WithStack(err))
}
if err := cast.LoadURL(ctx, device.DeviceID(), "https://go.dev"); err != nil {
t.Error(errors.WithStack(err))
}
ctx, cancel3 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel3()
ctx, cancel3 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel3()
status, err := cast.GetStatus(ctx, dev.DeviceID())
if err != nil {
t.Error(errors.WithStack(err))
}
status, err := cast.GetStatus(ctx, device.DeviceID())
if err != nil {
t.Error(errors.WithStack(err))
}
t.Logf("DEVICE STATUS: %s", spew.Sdump(status))
t.Logf("DEVICE STATUS: %s", spew.Sdump(status))
ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel4()
ctx, cancel4 := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel4()
if err := cast.StopCast(ctx, dev.DeviceID()); err != nil {
t.Error(errors.WithStack(err))
if err := cast.StopCast(ctx, device.DeviceID()); err != nil {
t.Error(errors.WithStack(err))
}
})
}(device)
}
}

View File

@ -11,3 +11,31 @@ type Device interface {
DeviceName() string
DeviceID() string
}
type legacyDevice struct {
Device `json:"-"`
UUID string `goja:"uuid" json:"uuid"`
Host string `goja:"host" json:"host"`
Port int `goja:"port" json:"port"`
Name string `goja:"name" json:"name"`
Type string `goja:"type" json:"type"`
}
func toLegacyDevice(device Device) *legacyDevice {
return &legacyDevice{
Device: device,
UUID: device.DeviceID(),
Host: device.DeviceHost().String(),
Port: device.DevicePort(),
Name: device.DeviceName(),
Type: string(device.DeviceType()),
}
}
func toLegacyDevices(devices ...Device) []*legacyDevice {
legacyDevices := make([]*legacyDevice, len(devices))
for i, d := range devices {
legacyDevices[i] = toLegacyDevice(d)
}
return legacyDevices
}

View File

@ -67,7 +67,7 @@ func (m *Module) refreshDevices(call goja.FunctionCall, rt *goja.Runtime) goja.V
return
}
promise.Resolve(devices)
promise.Resolve(toLegacyDevices(devices...))
}()
return rt.ToValue(promise)
@ -81,7 +81,7 @@ func (m *Module) getDevices(call goja.FunctionCall, rt *goja.Runtime) goja.Value
panic(rt.ToValue(errors.WithStack(err)))
}
return rt.ToValue(devices)
return rt.ToValue(toLegacyDevices(devices...))
}
func (m *Module) loadUrl(call goja.FunctionCall, rt *goja.Runtime) goja.Value {

View File

@ -62,20 +62,22 @@ func (r *Registry) Scan(ctx context.Context) ([]Device, error) {
wg.Add(len(r.index))
for _, srv := range r.index {
go func() {
defer wg.Done()
func(srv Service) {
go func() {
defer wg.Done()
devices, err := srv.Scan(ctx)
if err != nil {
lock.Lock()
errs = append(errs, errors.WithStack(err))
lock.Unlock()
}
devices, err := srv.Scan(ctx)
if err != nil {
lock.Lock()
errs = append(errs, errors.WithStack(err))
results = append(results, devices...)
lock.Unlock()
}
lock.Lock()
results = append(results, devices...)
lock.Unlock()
}()
}()
}(srv)
}
wg.Wait()