Documentation
¶
Index ¶
- Constants
- func CaptureAudio(ctx context.Context, streamURL string, cfg *CaptureConfig) (io.ReadCloser, error)
- func GetStreamURL(ctx context.Context, roomID int64) (string, error)
- func ResolveRoomID(ctx context.Context, shortID int64) (int64, error)
- type AudioStream
- type CaptureConfig
- type ClientOption
- type Monitor
- type MonitorOption
- type RoomEvent
- type RoomInfo
- type StreamClient
- type StreamEvent
Constants ¶
const ( EventLive = "live" EventOffline = "offline" EventAudioReady = "audio_ready" EventError = "error" )
Event type constants for StreamEvent.Type.
Variables ¶
This section is empty.
Functions ¶
func CaptureAudio ¶
func CaptureAudio(ctx context.Context, streamURL string, cfg *CaptureConfig) (io.ReadCloser, error)
CaptureAudio starts an ffmpeg process that reads from streamURL and outputs raw PCM audio to the returned ReadCloser. The caller must close the reader or cancel the context to stop ffmpeg and release resources.
ffmpeg must be installed and available in the system PATH.
func GetStreamURL ¶
GetStreamURL fetches the FLV stream URL for a live room. Returns an error if the room is not currently live.
Types ¶
type AudioStream ¶
type AudioStream struct {
RoomID int64
Reader io.ReadCloser
Cancel context.CancelFunc
}
AudioStream represents an active audio capture from a live stream. Reader delivers raw PCM data according to the CaptureConfig used. Call Cancel to stop the ffmpeg process and release resources.
type CaptureConfig ¶
type CaptureConfig struct {
SampleRate int // default 16000
Channels int // default 1 (mono)
Format string // default "s16le"
}
CaptureConfig controls ffmpeg audio capture parameters.
func DefaultCaptureConfig ¶
func DefaultCaptureConfig() CaptureConfig
DefaultCaptureConfig returns a CaptureConfig with sensible defaults for speech processing: 16kHz mono signed 16-bit little-endian PCM.
type ClientOption ¶
type ClientOption func(*clientConfig)
ClientOption configures a StreamClient.
func WithAudioConfig ¶
func WithAudioConfig(cfg CaptureConfig) ClientOption
WithAudioConfig sets the audio capture parameters (sample rate, channels, format).
func WithAutoCapture ¶
func WithAutoCapture(enabled bool) ClientOption
WithAutoCapture controls whether audio capture starts automatically when a room goes live. Default is true.
func WithClientCookie ¶
func WithClientCookie(sessdata string) ClientOption
WithClientCookie sets the SESSDATA cookie for authenticated API requests.
func WithInterval ¶
func WithInterval(d time.Duration) ClientOption
WithInterval sets the polling interval for live status monitoring. Default is 30 seconds.
type Monitor ¶
type Monitor struct {
// contains filtered or unexported fields
}
Monitor watches Bilibili live rooms for live/offline transitions and emits RoomEvent on a channel when a room's status changes.
func NewMonitor ¶
func NewMonitor(opts ...MonitorOption) *Monitor
NewMonitor creates a Monitor with the given options.
func (*Monitor) RemoveRoom ¶
RemoveRoom stops monitoring a room.
type MonitorOption ¶
type MonitorOption func(*monitorConfig)
MonitorOption configures a Monitor.
func WithCookie ¶
func WithCookie(sessdata string) MonitorOption
WithCookie sets the SESSDATA cookie for authenticated API requests. This is optional; most API endpoints work without authentication.
func WithMonitorInterval ¶
func WithMonitorInterval(d time.Duration) MonitorOption
WithMonitorInterval sets the polling interval for live status checks. Default is 30 seconds.
type RoomEvent ¶
type RoomEvent struct {
RoomID int64
Live bool // true = went live, false = went offline
Title string // room title (populated when going live)
}
RoomEvent represents a live/offline transition detected by Monitor.
type RoomInfo ¶
type RoomInfo struct {
RoomID int64
ShortID int64
UID int64
LiveStatus int // 0=offline, 1=live, 2=rotation
Title string
LiveTime string
}
RoomInfo holds metadata about a Bilibili live room.
type StreamClient ¶
type StreamClient struct {
// contains filtered or unexported fields
}
StreamClient is a high-level client that combines Monitor, stream URL fetching, and ffmpeg audio capture into a single pub/sub interface.
When a room goes live, StreamClient automatically fetches the stream URL and starts audio capture (if autoCapture is enabled), emitting StreamEvent on the subscribed channel.
func NewStreamClient ¶
func NewStreamClient(opts ...ClientOption) *StreamClient
NewStreamClient creates a StreamClient with the given options.
func (*StreamClient) AddRoom ¶
func (c *StreamClient) AddRoom(roomID int64)
AddRoom adds a room to the client. Safe to call after Subscribe().
func (*StreamClient) RemoveRoom ¶
func (c *StreamClient) RemoveRoom(roomID int64)
RemoveRoom stops monitoring a room and cancels any active capture.
func (*StreamClient) Subscribe ¶
func (c *StreamClient) Subscribe(ctx context.Context, roomIDs []int64) (<-chan StreamEvent, error)
Subscribe begins monitoring the given rooms and returns a channel that receives StreamEvent for live/offline transitions, audio readiness, and errors. The channel is closed when ctx is cancelled.
type StreamEvent ¶
type StreamEvent struct {
RoomID int64
Type string // "live", "offline", "audio_ready", "error"
Audio *AudioStream // non-nil when Type == "audio_ready"
Error error // non-nil when Type == "error"
Title string
}
StreamEvent is emitted by StreamClient to report room state changes and audio capture lifecycle events.