1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
|
package browser
import (
"fmt"
"github.com/raitonoberu/sptlrx/player"
"io"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/coder/websocket"
)
const helloMessage = "ADAPTER_VERSION 1.0.0;WNPRLIB_REVISION 2"
type state int
const (
stopped state = iota
paused
playing
)
func New(port int) (*Client, error) {
c := &Client{}
return c, c.start(port)
}
// Client implements player.Player
type Client struct {
state state
position int
title string
artist string
updateTime time.Time
stateMu sync.Mutex
connMu sync.Mutex
}
func (c *Client) handler(w http.ResponseWriter, r *http.Request) {
// make sure we only have one connection
c.connMu.Lock()
defer c.connMu.Unlock()
conn, err := websocket.Accept(w, r, &websocket.AcceptOptions{
InsecureSkipVerify: true,
})
if err != nil {
return
}
defer conn.Close(websocket.StatusInternalError, "internal error")
writer, err := conn.Writer(r.Context(), websocket.MessageText)
if err != nil {
return
}
writer.Write([]byte(helloMessage))
writer.Close()
for {
t, reader, err := conn.Reader(r.Context())
if err != nil {
return
}
msg, err := io.ReadAll(reader)
if err != nil {
return
}
if t != websocket.MessageText || len(msg) == 0 {
continue
}
c.processMessage(string(msg))
}
}
func (c *Client) processMessage(msg string) {
spaceIndex := strings.IndexByte(msg, ' ')
if spaceIndex == -1 {
return
}
msgType := strings.ToUpper(msg[:spaceIndex])
data := msg[spaceIndex+1:]
// we are not doing global locking here because
// we are not interested in most of the messages
switch msgType {
case "STATE":
c.stateMu.Lock()
switch data {
case "PLAYING":
c.state = playing
case "PAUSED":
c.state = paused
case "STOPPED":
c.state = stopped
}
c.stateMu.Unlock()
case "TITLE":
c.stateMu.Lock()
c.title = data
c.stateMu.Unlock()
case "ARTIST":
c.stateMu.Lock()
c.artist = data
c.stateMu.Unlock()
case "POSITION_SECONDS":
pos, _ := strconv.Atoi(data)
c.stateMu.Lock()
c.position = pos * 1000
c.updateTime = time.Now()
c.stateMu.Unlock()
}
}
func (c *Client) start(port int) error {
l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", port))
if err != nil {
return err
}
server := &http.Server{
Handler: http.HandlerFunc(c.handler),
}
go server.Serve(l)
return nil
}
func (c *Client) State() (*player.State, error) {
c.stateMu.Lock()
defer c.stateMu.Unlock()
if c.state == stopped {
return nil, nil
}
var query string
if c.artist != "" {
query = c.artist + " " + c.title
} else {
query = c.title
}
position := c.position
if c.state != paused {
position += int(time.Since(c.updateTime).Milliseconds())
}
return &player.State{
ID: query,
Query: query,
Position: position,
Playing: c.state == playing,
}, nil
}
|