webrtc

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 20, 2025 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AudioSampleRate is the sample rate for audio processing
	AudioSampleRate = 48000 // WebRTC default is 48kHz
	// AudioChannels is the number of audio channels
	AudioChannels = 1 // Mono audio for speech recognition
	// OpusFrameSize is the size of an Opus frame in samples
	OpusFrameSize = 960 // 20ms at 48kHz
	// BufferDuration is the duration in seconds to buffer audio before transcription
	BufferDuration = 3 // Buffer 3 seconds of audio for transcription
)
View Source
const (
	MessageTypeCandidate = "candidate"
)

Message types for WebSocket communication

Variables

View Source
var GlobalSessionManager = NewSessionManager()

Initialize a global session manager

View Source
var UseIceServers bool

Global variable to store the useIceServers flag

Functions

func CreatePeerConnection

func CreatePeerConnection(useIceServers bool) (*webrtc.PeerConnection, error)

CreatePeerConnection creates and configures a new WebRTC peer connection

func GenerateSessionID

func GenerateSessionID() string

GenerateSessionID creates a new unique session ID

func HandleOffer

func HandleOffer(w http.ResponseWriter, r *http.Request)

HandleOffer handles incoming WebRTC offers from clients

func HandleWebSocket

func HandleWebSocket(w http.ResponseWriter, r *http.Request)

HandleWebSocket handles WebSocket connections for ICE candidate exchange

func SetupAudioTrackHandler

func SetupAudioTrackHandler(pc *webrtc.PeerConnection)

SetupAudioTrackHandler configures the handlers for incoming audio tracks

Types

type CandidatePayload

type CandidatePayload struct {
	Candidate        string `json:"candidate"`        // Correct tag format
	SDPMid           string `json:"sdpMid"`           // Correct tag format
	SDPMLineIndex    uint16 `json:"sdpMLineIndex"`    // Correct tag format
	UsernameFragment string `json:"usernameFragment"` // Correct tag format
}

CandidatePayload represents the structure expected for an ICE candidate payload Matches the structure sent by the browser's `event.candidate.toJSON()`

type MockOpusDecoder

type MockOpusDecoder struct {
	// contains filtered or unexported fields
}

MockOpusDecoder provides a fallback implementation for development/testing

func (*MockOpusDecoder) Close

func (d *MockOpusDecoder) Close() error

Close is a no-op for the mock decoder

func (*MockOpusDecoder) Decode

func (d *MockOpusDecoder) Decode(data []byte, pcm []int16) (int, error)

Decode simulates decoding Opus packets to PCM

type OpusDecoder

type OpusDecoder interface {
	Decode(data []byte, pcm []int16) (int, error)
	Close() error
}

OpusDecoder interface defines the requirements for an Opus decoder

func NewMockOpusDecoder

func NewMockOpusDecoder() OpusDecoder

NewMockOpusDecoder creates a new mock Opus decoder

func NewOpusDecoder

func NewOpusDecoder(sampleRate int, channels int) (OpusDecoder, error)

NewOpusDecoder creates a new Opus decoder instance The real implementation is in real_opus.go, but the function is declared here so it can be called from the rest of the code

type RealOpusDecoder

type RealOpusDecoder struct {
	// contains filtered or unexported fields
}

RealOpusDecoder implements the OpusDecoder interface using libopus

func (*RealOpusDecoder) Close

func (d *RealOpusDecoder) Close() error

Close releases resources used by the decoder

func (*RealOpusDecoder) Decode

func (d *RealOpusDecoder) Decode(data []byte, pcm []int16) (int, error)

Decode decodes an Opus packet to PCM data

type SDPExchange

type SDPExchange struct {
	SDP  string `json:"sdp"`
	Type string `json:"type"` // "offer" or "answer"

}

SDPExchange represents the SDP offer or answer exchanged between peers

type SessionManager

type SessionManager struct {
	// contains filtered or unexported fields
}

SessionManager handles tracking and cleanup of WebRTC sessions

func NewSessionManager

func NewSessionManager() *SessionManager

NewSessionManager creates a new session manager

func (*SessionManager) CreateSession

func (sm *SessionManager) CreateSession(id string, useIceServers bool) (*WebRTCSession, error)

CreateSession creates a new WebRTC session

func (*SessionManager) GetSession

func (sm *SessionManager) GetSession(id string) (*WebRTCSession, bool)

GetSession retrieves a session by ID

func (*SessionManager) RemoveSession

func (sm *SessionManager) RemoveSession(id string)

RemoveSession removes a session and cleans up resources

func (*SessionManager) SetWebSocket

func (sm *SessionManager) SetWebSocket(id string, ws *websocket.Conn) bool

SetWebSocket associates a WebSocket connection with a session

func (*SessionManager) StopCleanup

func (sm *SessionManager) StopCleanup()

StopCleanup gracefully stops the cleanup goroutine

func (*SessionManager) UpdateActivity

func (sm *SessionManager) UpdateActivity(id string)

UpdateActivity updates the last activity timestamp for a session

type WebRTCSession

type WebRTCSession struct {
	ID             string
	PeerConnection *webrtc.PeerConnection
	WebSocket      *websocket.Conn

	CreatedAt    time.Time
	LastActivity time.Time
	// contains filtered or unexported fields
}

WebRTCSession represents a WebRTC session with its peer connection and related state

type WebSocketMessage

type WebSocketMessage struct {
	Type    string          `json:"type"`    // Correct tag format
	Payload json.RawMessage `json:"payload"` // Correct tag format
}

WebSocketMessage represents a generic message sent over WebSocket

Jump to

Keyboard shortcuts

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