streamd: preview quality picker, dashboard polish, C4 wrap keys
- Preview player can select FHD/HD/SD (or ABR master) from publish links - Embed dashboard screenshots in README; Linux streamd+agent setup guide - C4 Widevine wrap license mode + require --wvd; prefer Widevine PSSH - Channel 4 and My5 modules; streamd auth, variants, schedules, HLS gate
This commit is contained in:
parent
b3955bd81e
commit
066e018749
113 changed files with 19002 additions and 1541 deletions
|
|
@ -10,16 +10,39 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"drmdecryption/apps/agent/internal/client"
|
||||
)
|
||||
|
||||
// Message is one push from streamd.
|
||||
type Message struct {
|
||||
Type string `json:"type"`
|
||||
OK bool `json:"ok"`
|
||||
Error string `json:"error"`
|
||||
Message string `json:"message"`
|
||||
StreamID int64 `json:"stream_id"`
|
||||
StreamName string `json:"stream_name"`
|
||||
App string `json:"app"`
|
||||
Channel string `json:"channel"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
|
||||
// Client keeps a streamd /ws/agent connection alive and sends heartbeats
|
||||
// so claims auto-renew; on disconnect streamd releases this agent's claims.
|
||||
// It also carries pushes from streamd: a capture a viewer or a schedule is
|
||||
// waiting on, which would otherwise not be noticed until the next poll.
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
AgentID string
|
||||
Log *log.Logger
|
||||
|
||||
// OnCapture is called when streamd asks for credentials now.
|
||||
OnCapture func(streamID int64, name, app, channel, reason string)
|
||||
// OnStreamChanged is called when a stream was edited, so the agent can
|
||||
// re-poll instead of waiting out its interval.
|
||||
OnStreamChanged func(name, reason string)
|
||||
|
||||
stop chan struct{}
|
||||
}
|
||||
|
||||
|
|
@ -47,7 +70,8 @@ func (c *Client) Loop() {
|
|||
return
|
||||
default:
|
||||
}
|
||||
if err := c.session(); err != nil {
|
||||
err := c.session()
|
||||
if err != nil {
|
||||
c.Log.Printf("ws: session ended: %v — retry in %s", err, backoff)
|
||||
}
|
||||
select {
|
||||
|
|
@ -75,18 +99,29 @@ func (c *Client) session() error {
|
|||
u.Path = "/ws/agent"
|
||||
q := u.Query()
|
||||
q.Set("agent_id", c.AgentID)
|
||||
if c.Token != "" {
|
||||
q.Set("token", c.Token)
|
||||
}
|
||||
// The token is deliberately NOT put in the query string. It used to be,
|
||||
// which wrote the shared secret into every reverse proxy access log.
|
||||
// streamd authenticates the handshake from the Authorization header below.
|
||||
u.RawQuery = q.Encode()
|
||||
|
||||
hdr := http.Header{}
|
||||
if c.Token != "" {
|
||||
hdr.Set("Authorization", "Bearer "+c.Token)
|
||||
}
|
||||
if c.AgentID != "" {
|
||||
hdr.Set("X-Agent-ID", c.AgentID)
|
||||
}
|
||||
hdr.Set("User-Agent", "drm-agent/"+client.Version)
|
||||
dialer := websocket.Dialer{HandshakeTimeout: 10 * time.Second}
|
||||
conn, _, err := dialer.Dial(u.String(), hdr)
|
||||
conn, resp, err := dialer.Dial(u.String(), hdr)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
switch resp.StatusCode {
|
||||
case http.StatusUnauthorized, http.StatusForbidden:
|
||||
return fmt.Errorf("dial: %w (streamd rejected this agent's API key for /ws/agent; "+
|
||||
"it needs at least the operator role)", err)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("dial: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
|
@ -107,14 +142,11 @@ func (c *Client) session() error {
|
|||
return
|
||||
}
|
||||
_ = conn.SetReadDeadline(time.Now().Add(60 * time.Second))
|
||||
var msg map[string]any
|
||||
if json.Unmarshal(data, &msg) == nil {
|
||||
if t, _ := msg["type"].(string); t == "heartbeat_ack" {
|
||||
if ok, _ := msg["ok"].(bool); !ok {
|
||||
c.Log.Printf("ws: heartbeat nack: %v", msg["error"])
|
||||
}
|
||||
}
|
||||
var msg Message
|
||||
if json.Unmarshal(data, &msg) != nil {
|
||||
continue
|
||||
}
|
||||
c.handle(msg)
|
||||
}
|
||||
}()
|
||||
|
||||
|
|
@ -139,6 +171,28 @@ func (c *Client) session() error {
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Client) handle(msg Message) {
|
||||
switch strings.ToLower(msg.Type) {
|
||||
case "heartbeat_ack":
|
||||
if !msg.OK {
|
||||
c.Log.Printf("ws: heartbeat nack: %s", msg.Error)
|
||||
}
|
||||
case "capture_requested":
|
||||
if msg.StreamID == 0 {
|
||||
return
|
||||
}
|
||||
c.Log.Printf("ws: streamd requested a capture for stream=%s reason=%s",
|
||||
msg.StreamName, msg.Reason)
|
||||
if c.OnCapture != nil {
|
||||
c.OnCapture(msg.StreamID, msg.StreamName, msg.App, msg.Channel, msg.Reason)
|
||||
}
|
||||
case "stream_changed":
|
||||
if c.OnStreamChanged != nil {
|
||||
c.OnStreamChanged(msg.StreamName, msg.Reason)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) writeHeartbeat(conn *websocket.Conn) error {
|
||||
_ = conn.SetWriteDeadline(time.Now().Add(10 * time.Second))
|
||||
return conn.WriteJSON(map[string]string{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue