Skip to content

Commit

Permalink
Working state.
Browse files Browse the repository at this point in the history
Will probably squash or split this into proper commits later.
  • Loading branch information
icedream committed Mar 17, 2024
1 parent cca6ffa commit ba2e9b9
Show file tree
Hide file tree
Showing 31 changed files with 723 additions and 448 deletions.
38 changes: 38 additions & 0 deletions cmd/storage-discover/key_uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"io"
"os"

"github.com/google/uuid"
)

var zeroUUID = uuid.UUID{}

func loadUUIDKey() (string, error) {
var id uuid.UUID
if f, err := os.Open("eaas-id.txt"); err == nil {
defer f.Close()
keyBytes, err := io.ReadAll(f)
if err != nil {
return "", err
}
id, err = uuid.ParseBytes(keyBytes)
if err != nil {
return "", err
}
}
if id == zeroUUID {
var err error
id, err = uuid.NewUUID()
if err != nil {
return "", err
}
keyBytes, err := id.MarshalBinary()
if err != nil {
return "", err
}
os.WriteFile("eaas-id.txt", keyBytes, 0o600)
}
return id.String(), nil
}
117 changes: 46 additions & 71 deletions cmd/storage-discover/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@ package main

import (
"context"
"crypto/ed25519"
"crypto/rand"
"crypto/x509"
"encoding/json"
"errors"
"flag"
"io"
"log"
"os"
"time"

"github.com/google/uuid"
"github.com/icedream/go-stagelinq/eaas"
"github.com/icedream/go-stagelinq/eaas/proto/enginelibrary"
"github.com/icedream/go-stagelinq/eaas/proto/networktrust"
Expand All @@ -27,8 +25,7 @@ const (
var (
grpcURL string
hostname string
key ed25519.PrivateKey
id uuid.UUID
identity string
)

func init() {
Expand All @@ -40,58 +37,6 @@ func init() {
if err != nil {
hostname = "eaas-demo"
}

if f, err := os.Open("eaas-key.bin"); err == nil {
defer f.Close()
keyBytes, err := io.ReadAll(f)
if err != nil {
panic(err)
}
readKey, err := x509.ParsePKCS8PrivateKey(keyBytes)
if err != nil {
panic(err)
}
if edkey, ok := readKey.(ed25519.PrivateKey); !ok {
panic("eaas-key.bin is not an ed25519 private key")
} else {
key = edkey
}
}
if key == nil {
_, priv, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
panic(err)
}
keyBytes, err := x509.MarshalPKCS8PrivateKey(priv)
if err != nil {
panic(err)
}
os.WriteFile("eaas-key.bin", keyBytes, 0o600)
key = priv
}

if f, err := os.Open("eaas-id.txt"); err == nil {
defer f.Close()
keyBytes, err := io.ReadAll(f)
if err != nil {
panic(err)
}
id, err = uuid.ParseBytes(keyBytes)
if err != nil {
panic(err)
}
}
if key == nil {
id, err = uuid.NewUUID()
if err != nil {
panic(err)
}
keyBytes, err := id.MarshalBinary()
if err != nil {
panic(err)
}
os.WriteFile("eaas-id.txt", keyBytes, 0o600)
}
}

type App struct {
Expand All @@ -107,22 +52,33 @@ func main() {
runEngineLibraryUI(grpcURL)
}

func marshalJSON(v any) []byte {
s, _ := json.Marshal(v)
return s
}

func runEngineLibraryUI(grpcURL string) {
// load our identity so we don't have to repeatedly re-verify
identity, err := loadUUIDKey()
if err != nil {
log.Fatal(err)
}

ctx := context.Background()
connection, err := eaas.DialContext(ctx, grpcURL)
if err != nil {
panic(err)
log.Fatal(err)
}

// pk := string(key.Public().(ed25519.PublicKey))
pk := id.String()
log.Println("Waiting for approval on the other end...")
resp, err := connection.CreateTrust(ctx, &networktrust.CreateTrustRequest{
DeviceName: &hostname,
Ed25519Pk: &pk,
// I honestly don't know why in the proto it was defined as "Ed25519Pk"...
Ed25519Pk: &identity,
// ...or why there even is a WireguardPort field, too?!
})
if err != nil {
panic(err)
log.Fatal(err)
}
switch {
case resp.GetGranted() != nil:
Expand All @@ -139,23 +95,42 @@ func runEngineLibraryUI(grpcURL string) {
if err != nil {
panic(err)
}
var pageSize uint32 = 100
var pageSize uint32 = 25
getTracksResp, err := connection.GetTracks(ctx, &enginelibrary.GetTracksRequest{
PageSize: &pageSize,
})
if err != nil {
panic(err)
}
for _, track := range getTracksResp.GetTracks() {
log.Printf("Track: %s", string(marshalJSON(track)))
getTrackResp, err := connection.GetTrack(ctx, &enginelibrary.GetTrackRequest{
TrackId: track.GetMetadata().Id,
})
if err != nil {
log.Println("\tfailed to GetTrack on this track")
continue
}
log.Printf("\t%+v", getTrackResp)
}
for _, playlist := range getLibraryResp.GetPlaylists() {
log.Printf("Playlist %q (%q)", playlist.GetTitle(), playlist.GetListType())

log.Printf("Playlist: %s", string(marshalJSON(playlist)))
getTracksResp, err := connection.GetTracks(ctx, &enginelibrary.GetTracksRequest{
PlaylistId: playlist.Id,
PageSize: &pageSize,
})
if errors.Is(err, io.EOF) {
// BUG - empty playlist causes EOF, reconnect
connection, err = eaas.DialContext(ctx, grpcURL)
if err != nil {
panic(err)
}
}
if err != nil {
panic(err)
}
for _, track := range getTracksResp.GetTracks() {
metadata := track.GetMetadata()
if metadata == nil {
continue
}
log.Printf("\tTrack %s", metadata.String())
log.Printf("\tTrack: ID %s", track.GetMetadata().GetId())
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/storage/.gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
*.m4a filter=lfs diff=lfs merge=lfs -text
*.beatgrid filter=lfs diff=lfs merge=lfs -text
*.waveform filter=lfs diff=lfs merge=lfs -text
2 changes: 1 addition & 1 deletion cmd/storage/Icedream - Whiplash (Radio Edit).m4a
Git LFS file not shown
3 changes: 3 additions & 0 deletions cmd/storage/Icedream - Whiplash (Radio Edit).m4a.beatgrid
Git LFS file not shown
3 changes: 3 additions & 0 deletions cmd/storage/Icedream - Whiplash (Radio Edit).m4a.waveform
Git LFS file not shown
61 changes: 52 additions & 9 deletions cmd/storage/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,81 @@ import (
"bytes"
_ "embed"
"fmt"
"path/filepath"
"strconv"
"strings"

"github.com/dhowden/tag"
"github.com/google/uuid"
"github.com/icedream/go-stagelinq/eaas"
"github.com/icedream/go-stagelinq/eaas/proto/enginelibrary"
"google.golang.org/protobuf/types/known/timestamppb"
)

// Imports needed for image resizing (see commented out code for it)
// import (
// "image"
// _ "image/jpeg"
// _ "image/png"
// )

var (
demoTrackFileName = "Icedream - Whiplash (Radio Edit).flac"
demoTrackFileName = "Icedream - Whiplash (Radio Edit).m4a"
demoLibrary = "12eceaa2-f81a-4b63-b196-94648a3bdd95"
demoLibraryName = "Demo Library"
demoPlaylist = "55ab0c7c-6c35-429a-81d0-25b039a34a9f"
demoPlaylistName = "Demo Playlist"
demoPlaylistTrackCount uint32 = 1
demoTrackIDs []string
demoTrackURL = "/demo/" + demoTrackFileName
demoTrackLength = uint32(len(demoTrackBytes))
demoTrackMetadata enginelibrary.TrackMetadata
demoTrackArtwork []byte
demoTrackIDs = []string{
"1 " + demoLibrary,
}
// HACK - imitating original Engine DJ software behavior by using Windows paths
demoTrackURL = filepath.Join("C:", "demo", demoTrackFileName)
// HACK - imitating original Engine DJ software behavior by adding brackets.
demoTrackURLGRPC = fmt.Sprintf("<%s>", filepath.ToSlash(demoTrackURL))
demoTrackLength = uint32(len(demoTrackBytes))
demoTrackMetadata enginelibrary.TrackMetadata
demoTrackArtwork []byte
demoToken eaas.Token = eaas.Token{
0x5e, 0xff, 0xae, 0x59, 0x12, 0x88, 0x29, 0x30,
0xde, 0xad, 0xc0, 0xde, 0xc0, 0xff, 0xee, 0x00,
}
)

//go:embed "Icedream - Whiplash (Radio Edit).m4a"
var demoTrackBytes []byte

//go:embed "Icedream - Whiplash (Radio Edit).m4a.beatgrid"
var demoBeatGrid []byte

//go:embed "Icedream - Whiplash (Radio Edit).m4a.waveform"
var demoOverviewWaveform []byte

var demoTrackPreviewArtwork []byte

func init() {
for i := 0; i < int(demoPlaylistTrackCount); i++ {
demoTrackIDs = append(demoTrackIDs, uuid.New().String())
if len(demoTrackIDs) == 0 {
for i := 0; i < int(demoPlaylistTrackCount); i++ {
demoTrackIDs = append(demoTrackIDs, uuid.New().String())
}
}

demoTrackMetadata.DateAdded = timestamppb.Now()
if metadata, err := tag.ReadFrom(bytes.NewReader(demoTrackBytes)); err == nil {
if metadata.Picture() != nil {
demoTrackArtwork = metadata.Picture().Data
demoTrackPreviewArtwork = demoTrackArtwork
// // If you wanna be nice to the hardware, you can have the server
// // shrink down the artwork. I don't think even the original Engine
// // DJ software does that though.
// img, _, err := image.Decode(bytes.NewReader(demoTrackArtwork))
// if err == nil {
// img = resize.Resize(240, 240, img, resize.Lanczos2)
// }
// var b bytes.Buffer
// if err := jpeg.Encode(&b, img, &jpeg.Options{Quality: 70}); err == nil {
// demoTrackPreviewArtwork = b.Bytes()
// }
}
if v := metadata.Artist(); len(v) > 0 {
demoTrackMetadata.Artist = &v
Expand All @@ -64,19 +105,21 @@ func init() {
}
if v, ok := metadata.Raw()["KEY"]; ok {
s := fmt.Sprint(v)
s = strings.Trim(s, "\x00 ")
demoTrackMetadata.Key = &s
}
if v, ok := metadata.Raw()["LABEL"]; ok {
s := fmt.Sprint(v)
s = strings.Trim(s, "\x00 ")
demoTrackMetadata.Label = &s
}
if v, ok := metadata.Raw()["REMIXER"]; ok {
s := fmt.Sprint(v)
s = strings.Trim(s, "\x00 ")
demoTrackMetadata.Remixer = &s
}
if v := uint32(metadata.Year()); v > 0 {
demoTrackMetadata.Year = &v
}

}
}
Loading

0 comments on commit ba2e9b9

Please sign in to comment.