protocol

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 27, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package protocol defines the wire protocol for GopherCast audio streaming. Control messages are sent as JSON WebSocket TextMessages, while audio data is sent as binary WebSocket BinaryMessages with a 12-byte header.

Index

Constants

View Source
const AudioFrameHeaderSize = 12

AudioFrameHeaderSize is 12 bytes: 4B seq + 8B sample offset.

Variables

This section is empty.

Functions

func MarshalEnvelope

func MarshalEnvelope(msgType MessageType, data any) ([]byte, error)

Types

type AudioFrame

type AudioFrame struct {
	SeqNum       uint32
	SampleOffset uint64
	Payload      []byte
}

func UnmarshalAudioFrame

func UnmarshalAudioFrame(data []byte) (*AudioFrame, error)

func (*AudioFrame) MarshalBinary

func (f *AudioFrame) MarshalBinary() []byte

type ClientInfo

type ClientInfo struct {
	ID   string `json:"id"`
	Name string `json:"name"`
	Addr string `json:"addr"`
}

type ClientListMsg

type ClientListMsg struct {
	Clients []ClientInfo `json:"clients"`
}

ClientListMsg is broadcast from server to all clients when the client list changes.

type Envelope

type Envelope struct {
	Type MessageType     `json:"type"`
	Data json.RawMessage `json:"data"`
}

Envelope is the outer wrapper for all control messages.

func ParseEnvelope

func ParseEnvelope(data []byte) (*Envelope, error)

type HelloMsg

type HelloMsg struct {
	Name           string `json:"name"`
	AudioLatencyNs int64  `json:"audio_latency_ns,omitempty"`
}

HelloMsg is sent by the client immediately after connecting.

type MessageType

type MessageType string

MessageType identifies the type of a control message.

const (
	MsgHello         MessageType = "hello"
	MsgServerState   MessageType = "server_state"
	MsgClientList    MessageType = "client_list"
	MsgStartPlayback MessageType = "start_playback"
	MsgStopPlayback  MessageType = "stop_playback"
	MsgTrackChange   MessageType = "track_change"
	MsgReject        MessageType = "reject"
)

type RejectMsg

type RejectMsg struct {
	Reason string `json:"reason"`
}

RejectMsg is sent from server to a client to reject a late connection.

type ServerState

type ServerState string
const (
	StateLobby   ServerState = "lobby"
	StatePlaying ServerState = "playing"
	StateStopped ServerState = "stopped"
)

type ServerStateMsg

type ServerStateMsg struct {
	State ServerState `json:"state"`
}

type StartPlaybackMsg

type StartPlaybackMsg struct {
	TrackTitle string `json:"track_title"`
	SampleRate int    `json:"sample_rate"`
	Channels   int    `json:"channels"`
	StartAtNs  int64  `json:"start_at_ns"`
}

StartPlaybackMsg is sent from server to all clients to begin playback.

type StopPlaybackMsg

type StopPlaybackMsg struct {
	Reason string `json:"reason"`
}

StopPlaybackMsg is sent from server to all clients to stop playback.

type TrackChangeMsg

type TrackChangeMsg struct {
	TrackTitle string `json:"track_title"`
	SampleRate int    `json:"sample_rate"`
	Channels   int    `json:"channels"`
	StartAtNs  int64  `json:"start_at_ns"`
}

TrackChangeMsg is sent from server to all clients when the track changes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL