|
| 1 | +// Copyright 2024 Team 254. All Rights Reserved. |
| 2 | +// Author: pat@patfairbank.com (Patrick Fairbank) |
| 3 | +// |
| 4 | +// Client for interfacing with one or more Blackmagic HyperDeck devices to automatically record matches. |
| 5 | + |
| 6 | +package partner |
| 7 | + |
| 8 | +import ( |
| 9 | + "fmt" |
| 10 | + "log" |
| 11 | + "net" |
| 12 | + "strings" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + blackmagicPort = 9993 |
| 18 | + blackmagicConnectTimeoutMs = 100 |
| 19 | + blackmagicStopDelaySec = 10 |
| 20 | +) |
| 21 | + |
| 22 | +type BlackmagicClient struct { |
| 23 | + deviceAddresses []string |
| 24 | +} |
| 25 | + |
| 26 | +// Creates a new Blackmagic client with the given device addresses as a comma-separated string. |
| 27 | +func NewBlackmagicClient(addresses string) *BlackmagicClient { |
| 28 | + deviceAddresses := strings.Split(addresses, ",") |
| 29 | + for i, address := range deviceAddresses { |
| 30 | + deviceAddresses[i] = strings.TrimSpace(address) |
| 31 | + } |
| 32 | + return &BlackmagicClient{deviceAddresses: deviceAddresses} |
| 33 | +} |
| 34 | + |
| 35 | +// Starts recording across all devices. |
| 36 | +func (client *BlackmagicClient) StartRecording() { |
| 37 | + client.sendCommand("record") |
| 38 | +} |
| 39 | + |
| 40 | +// Stops recording across all devices after a delay. |
| 41 | +func (client *BlackmagicClient) StopRecording() { |
| 42 | + time.Sleep(blackmagicStopDelaySec * time.Second) |
| 43 | + client.sendCommand("stop") |
| 44 | +} |
| 45 | + |
| 46 | +// Connects to all devices and executes the given command. |
| 47 | +func (client *BlackmagicClient) sendCommand(command string) { |
| 48 | + for _, address := range client.deviceAddresses { |
| 49 | + conn, err := net.DialTimeout( |
| 50 | + "tcp", fmt.Sprintf("%s:%d", address, blackmagicPort), blackmagicConnectTimeoutMs*time.Millisecond, |
| 51 | + ) |
| 52 | + if err != nil { |
| 53 | + log.Printf("Failed to connect to Blackmagic device at %s: %v", address, err) |
| 54 | + continue |
| 55 | + } |
| 56 | + defer conn.Close() |
| 57 | + _, err = fmt.Fprint(conn, command+"\n") |
| 58 | + if err != nil { |
| 59 | + log.Printf("Failed to send '%s' command to Blackmagic device at %s: %v", command, address, err) |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments