2024-04-06 16:38:14 -06:00
|
|
|
package pnyx
|
|
|
|
|
|
|
|
import (
|
2024-04-13 14:00:56 -06:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
2024-04-06 16:38:14 -06:00
|
|
|
)
|
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
type ChannelID byte
|
2024-04-06 16:38:14 -06:00
|
|
|
|
2024-04-08 11:28:52 -06:00
|
|
|
const (
|
2024-04-13 14:00:56 -06:00
|
|
|
MODE_RAW ModeID = iota
|
2024-04-12 18:06:57 -06:00
|
|
|
MODE_AUDIO
|
2024-04-08 11:28:52 -06:00
|
|
|
|
2024-04-12 18:06:57 -06:00
|
|
|
AUDIO_SET_SAMPLE_RATE = iota
|
|
|
|
AUDIO_GET_SAMPLE_RATE
|
2024-04-08 11:28:52 -06:00
|
|
|
)
|
2024-04-06 16:38:14 -06:00
|
|
|
|
|
|
|
type ModeID uint8
|
|
|
|
type CommandID uint8
|
2024-04-09 17:08:46 -06:00
|
|
|
type Permission string
|
2024-04-06 16:38:14 -06:00
|
|
|
|
|
|
|
type Channel struct {
|
2024-04-09 17:08:46 -06:00
|
|
|
id ChannelID
|
2024-04-11 21:05:50 -06:00
|
|
|
name string
|
2024-04-06 16:38:14 -06:00
|
|
|
modes map[ModeID]Mode
|
2024-04-13 14:00:56 -06:00
|
|
|
members []*ServerSession
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(channel *Channel) Data(session *ServerSession, mode ModeID, data []byte) {
|
2024-04-09 17:08:46 -06:00
|
|
|
m, has_mode := channel.modes[mode]
|
2024-04-13 14:00:56 -06:00
|
|
|
if has_mode {
|
|
|
|
m.Data(session, channel.id, channel.members, data)
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(channel *Channel) Command(session *ServerSession, command byte, request_id uuid.UUID, mode_id ModeID, data []byte) error {
|
|
|
|
mode, has_mode := channel.modes[mode_id]
|
|
|
|
if has_mode == false {
|
|
|
|
return fmt.Errorf("Channel has no mode 0x%02x", mode)
|
2024-04-09 17:08:46 -06:00
|
|
|
} else {
|
2024-04-13 14:00:56 -06:00
|
|
|
return mode.Command(session, command, request_id, channel.id, channel.members, data)
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(channel *Channel) Join(client PeerID, session SessionID) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func(channel *Channel) Leave(client PeerID, session SessionID) {
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mode interface {
|
2024-04-13 14:00:56 -06:00
|
|
|
Command(session *ServerSession, command byte, request_id uuid.UUID, channel_id ChannelID, members []*ServerSession, data []byte) error
|
|
|
|
Data(session *ServerSession, channel_id ChannelID, members []*ServerSession, data []byte)
|
|
|
|
|
|
|
|
Join(client PeerID, session SessionID)
|
|
|
|
Leave(client PeerID, session SessionID)
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func multiplex_without_sender(origin SessionID, packet *Packet, sessions []*ServerSession) {
|
|
|
|
for _, session := range(sessions) {
|
|
|
|
if session.ID == origin {
|
2024-04-08 11:28:52 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
session.OutgoingPackets <- packet
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func multiplex(packet *Packet, sessions []*ServerSession) {
|
|
|
|
for _, session := range(sessions) {
|
|
|
|
session.OutgoingPackets <- packet
|
2024-04-08 11:28:52 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type RawMode struct {
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(mode *RawMode) Command(session *ServerSession, command byte, request_id uuid.UUID, channel_id ChannelID, members []*ServerSession, data []byte) error {
|
|
|
|
return fmt.Errorf("unknown raw mode command 0x%02x", command)
|
2024-04-09 17:08:46 -06:00
|
|
|
}
|
2024-04-08 17:23:55 -06:00
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(mode *RawMode) Data(session *ServerSession, channel_id ChannelID, members []*ServerSession, data []byte) {
|
|
|
|
new_packet := NewChannelPeerPacket(session.Peer, channel_id, MODE_RAW, data)
|
|
|
|
multiplex_without_sender(session.ID, new_packet, members)
|
|
|
|
}
|
|
|
|
|
|
|
|
func(mode *RawMode) Join(client PeerID, session SessionID) {
|
|
|
|
}
|
|
|
|
func(mode *RawMode) Leave(client PeerID, session SessionID) {
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type SampleRate byte
|
|
|
|
const (
|
|
|
|
SAMPLE_RATE_UNSET SampleRate = 0xFF
|
|
|
|
SAMPLE_RATE_24KHZ = 0x01
|
|
|
|
SAMPLE_RATE_48KHZ = 0x02
|
|
|
|
)
|
|
|
|
|
|
|
|
type AudioMode struct {
|
|
|
|
SampleRate SampleRate
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(mode *AudioMode) Command(session *ServerSession, command byte, request_id uuid.UUID, channel_id ChannelID, members []*ServerSession, data []byte) error {
|
|
|
|
switch command {
|
2024-04-12 18:06:57 -06:00
|
|
|
case AUDIO_SET_SAMPLE_RATE:
|
2024-04-13 14:00:56 -06:00
|
|
|
if len(data) == 1 {
|
|
|
|
switch SampleRate(data[0]) {
|
2024-04-12 18:06:57 -06:00
|
|
|
case SAMPLE_RATE_24KHZ:
|
|
|
|
fallthrough
|
|
|
|
case SAMPLE_RATE_48KHZ:
|
2024-04-13 14:00:56 -06:00
|
|
|
mode.SampleRate = SampleRate(data[0])
|
|
|
|
update_packet := NewChannelCommandPacket(request_id, channel_id, MODE_AUDIO, AUDIO_SET_SAMPLE_RATE, data)
|
|
|
|
multiplex(update_packet, members)
|
|
|
|
return nil
|
2024-04-12 18:06:57 -06:00
|
|
|
default:
|
2024-04-13 14:00:56 -06:00
|
|
|
return fmt.Errorf("Invalid sample rate: %x", data[0])
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
|
|
|
} else {
|
2024-04-13 14:00:56 -06:00
|
|
|
return fmt.Errorf("Invalid AUDIO_SET_SAMPLE_RATE payload: %x", data)
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
|
|
|
case AUDIO_GET_SAMPLE_RATE:
|
2024-04-13 14:00:56 -06:00
|
|
|
session.OutgoingPackets <- NewChannelCommandPacket(request_id, channel_id, MODE_AUDIO, AUDIO_SET_SAMPLE_RATE, []byte{byte(mode.SampleRate)})
|
|
|
|
return nil
|
2024-04-12 18:06:57 -06:00
|
|
|
default:
|
2024-04-13 14:00:56 -06:00
|
|
|
return fmt.Errorf("unknown audio mode command 0x%02x", command)
|
2024-04-12 18:06:57 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-13 14:00:56 -06:00
|
|
|
func(mode *AudioMode) Data(session *ServerSession, channel_id ChannelID, members []*ServerSession, data []byte) {
|
|
|
|
new_packet := NewChannelPeerPacket(session.Peer, channel_id, MODE_AUDIO, data)
|
|
|
|
multiplex_without_sender(session.ID, new_packet, members)
|
|
|
|
}
|
|
|
|
|
|
|
|
func(mode *AudioMode) Join(client PeerID, session SessionID) {
|
|
|
|
}
|
|
|
|
func(mode *AudioMode) Leave(client PeerID, session SessionID) {
|
2024-04-06 16:38:14 -06:00
|
|
|
}
|