Skip to content

Commit

Permalink
serve logo as album art uri in didl metadatat
Browse files Browse the repository at this point in the history
  • Loading branch information
ugjka committed Feb 11, 2024
1 parent 981d6e5 commit 9c6ece1
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 13 deletions.
9 changes: 5 additions & 4 deletions av1transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"github.com/huin/goupnp/dcps/av1"
)

func AV1SetAndPlay(loc *url.URL, stream string) error {
func AV1SetAndPlay(loc *url.URL, albumart, stream string) error {
client, err := av1.NewAVTransport1ClientsByURL(loc)
if err != nil {
return err
Expand All @@ -53,7 +53,7 @@ func AV1SetAndPlay(loc *url.URL, stream string) error {
return nil
}

metadata := didlMetadata(stream)
metadata := didlMetadata(albumart, stream)
err = try(metadata)
if err == nil {
return nil
Expand All @@ -71,8 +71,8 @@ func AV1Stop(loc *url.URL) {
client[0].Stop(0)
}

func didlMetadata(uri string) string {
out := fmt.Sprintf(didlTemplate, uri)
func didlMetadata(albumart, stream string) string {
out := fmt.Sprintf(didlTemplate, albumart, stream)
out = strings.ReplaceAll(out, "\n", " ")
out = strings.ReplaceAll(out, "> <", "><")
return out
Expand All @@ -91,6 +91,7 @@ xmlns:pv="http://www.pv.com/pvns/">
<dc:title>Audio Cast</dc:title>
<dc:creator>Blast</dc:creator>
<upnp:artist>Blast</upnp:artist>
<upnp:albumArtURI>%s</upnp:albumArtURI>
<res protocolInfo="http-get:*:audio/mpeg:*">%s</res>
</item>
</DIDL-Lite>`
22 changes: 22 additions & 0 deletions logo_serve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"
"net/http"
)

type logo []byte

func (l logo) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "image/png")
w.Header().Set("Content-Length", fmt.Sprint(len(l)))
if r.Method == http.MethodHead {
w.WriteHeader(http.StatusOK)
return
}
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}
w.Write(l)
}
30 changes: 21 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ package main

import (
"bytes"
_ "embed"
"flag"
"fmt"
"io"
Expand All @@ -42,17 +43,21 @@ import (
"github.com/huin/goupnp/dcps/av1"
)

// open in firewall
const STREAMPORT = 9000

// lame encoder bitrate
const MP3BITRATE = 320

const BLASTMONITOR = "blast.monitor"
const STREAM_NAME = "stream.mp3"
//go:embed logo.png
var logobytes []byte

var headers = new(bool)

const (
BLASTMONITOR = "blast.monitor"
STREAM_NAME = "stream.mp3"
LOGO_NAME = "logo.png"
// open in firewall
STREAMPORT = 9000
// lame encoder bitrate
MP3BITRATE = 320
)

func main() {
// check for dependencies
exes := []string{
Expand Down Expand Up @@ -157,6 +162,8 @@ func main() {

mux := http.NewServeMux()
mux.Handle("/"+STREAM_NAME, audioSource)
var logoHandler logo = logobytes
mux.Handle("/"+LOGO_NAME, logoHandler)
httpServer := &http.Server{
Addr: fmt.Sprintf(":%d", STREAMPORT),
ReadTimeout: -1,
Expand All @@ -180,13 +187,16 @@ func main() {
}

var streamURL string
var albumArtURL string
var protocol = "http"
if detectSonos(DLNADevice) {
protocol = "x-rincon-mp3radio"
}
if streamAddress.To4() != nil {
streamURL = fmt.Sprintf("%s://%s:%d/%s",
protocol, streamAddress, STREAMPORT, STREAM_NAME)
albumArtURL = fmt.Sprintf("http://%s:%d/%s",
streamAddress, STREAMPORT, LOGO_NAME)
} else {
var zone string
if streamAddress.IsLinkLocalUnicast() {
Expand All @@ -197,10 +207,12 @@ func main() {
}
streamURL = fmt.Sprintf("%s://[%s%s]:%d/%s",
protocol, streamAddress, zone, STREAMPORT, STREAM_NAME)
albumArtURL = fmt.Sprintf("http://[%s%s]:%d/%s",
streamAddress, zone, STREAMPORT, LOGO_NAME)
}
log.Printf("stream URI: %s\n", streamURL)
log.Println("setting av1transport URI and playing")
err = AV1SetAndPlay(DLNADevice.Location, streamURL)
err = AV1SetAndPlay(DLNADevice.Location, albumArtURL, streamURL)
if err != nil {
fmt.Fprintln(os.Stderr, "transport:", err)
cleanup()
Expand Down

0 comments on commit 9c6ece1

Please sign in to comment.