webrtp

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: Apache-2.0 Imports: 20 Imported by: 0

README

go-webrtp

Golang library for streaming RTP packet from RTSP source directly to web in real-time.

Screenshot

WebRTP Dashboard

Usage

Download Binary

Download the latest release binary from GitHub Releases:

  • macOS (Apple Silicon)

    curl -L -o webrtp https://github.com/connectedtechco/go-webrtp/releases/latest/download/webrtp-darwin-arm64
    chmod +x webrtp
    ./webrtp -i -c config.yml
    
  • Linux (x64)

    curl -L -o webrtp https://github.com/connectedtechco/go-webrtp/releases/latest/download/webrtp-linux-amd64
    chmod +x webrtp
     ./webrtp -i -c config.yml
    
  • Windows (x64)

    # Windows (x64)
    Invoke-WebRequest -Uri "https://github.com/connectedtechco/go-webrtp/releases/latest/download/webrtp-windows-amd64.exe" -OutFile webrtp.exe
    
Run Server
./webrtp -c config.yml
Command Options
  -c, --config string    Config file path (default: config.yml)
  -i, --interface       Use graphical interface (default: false)
  -p, --port int        HTTP server port (default: 8080)
Configuration

Create a config.yml file:

telemetryServiceName: streamer1     # Optional: OTEL service name (default: "webrtp")
telemetryEndpoint: "localhost:4317" # Optional: OTEL gRPC endpoint for pushing metrics
upstreams:
  - name: camera1
    rtspUrl: rtsp://192.168.1.100:554/stream
Endpoint
Telemetry

Metrics are available at /metrics endpoint in Prometheus format:

  • streamer_clients{name="camera1"} - Current number of clients
  • streamer_bitrate_kbps{name="camera1"} - Current bitrate in Kbps
  • streamer_framerate{name="camera1"} - Current framerate
Embedded Server

You can embed the WebRTP handler in your own Fiber application:

package main

import (
	"fmt"
	"log"

	"github.com/connectedtechco/go-webrtp"
	"github.com/gofiber/fiber/v3"
	"github.com/gofiber/fiber/v3/middleware/cors"
)

func main() {
	// Initialize WebRTP instance
	inst := webrtp.Init(&webrtp.Config{
		Rtsp:   "rtsp://192.168.1.100:554/stream",
		Logger: log.Default(),
	})

	// Connect to RTSP source
	if err := inst.Connect(); err != nil {
		log.Fatalf("connect: %v", err)
	}

	// Create Fiber app
	app := fiber.New()
	app.Use(cors.New())

	// Register WebRTP handler at /stream endpoint
	app.All("/stream", inst.Handler())

	// Start server
	log.Printf("Server started on :8080")
	log.Fatal(app.Listen(":8080"))
}

Libraries

JavaScript / TypeScript

See client/javascript for the JavaScript client library.

npm install @connectedtechco/webrtp
import { createClient } from '@connectedtechco/webrtp';

const client = createClient('ws://localhost:8080/stream/no/0');
client.render(document.getElementById('canvas'));

client.onInfo((info) => {
    console.log('Info:', info);
});

client.onFrame((frameNo, data, isKey) => {
    console.log(`Frame ${frameNo}: ${data.byteLength} bytes, keyframe: ${isKey}`);
});
Python

See client/python for the Python client library.

cd client/python
uv pip install -e .
from webrtp import WebRtpClient
import cv2

client = WebRtpClient("ws://localhost:8080/stream/no/0")

# Get raw frame data with callback
client.on_raw(lambda frame_no, data, is_key: print(f"Frame: {frame_no}, size: {len(data)}"))

# Get decoded frame with callback
client.on_frame(lambda frame_no, frame: cv2.imshow('video', frame))

client.start()

Development

  1. Generate self-signed certificate for TLS connection

    mkdir -p .local
    openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 -keyout .local/x509-key.pem -out .local/x509-cer.pem -days 365 -nodes -subj "/CN=localhost" -addext "subjectAltName=DNS:localhost,IP:127.0.0.1"
    
  2. Build styles (run in background)

    sass --watch command/webrtp/index.scss:command/webrtp/index.css
    
  3. Run the server

    go run ./command/webrtp/
    

Documentation

Overview

Package webrtp provides RTSP to WebSocket streaming with fMP4 output.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnnexbToAvcc

func AnnexbToAvcc(au [][]byte) []byte

AnnexbToAvcc converts Annex-B NAL units to AVCC format.

func BuildFragment

func BuildFragment(seqNr uint32, dts uint64, dur uint32, isIDR bool, avcc []byte) ([]byte, error)

BuildFragment creates an fMP4 media fragment.

func BuildInitH264

func BuildInitH264(sps, pps []byte) ([]byte, error)

BuildInitH264 creates an fMP4 init segment for H264 video.

func BuildInitH265

func BuildInitH265(vps, sps, pps []byte) ([]byte, error)

BuildInitH265 creates an fMP4 init segment for H265 video.

Types

type Config

type Config struct {
	Rtsp            string
	Logger          Logger
	WriteTimeout    time.Duration
	ReadBufferSize  int
	WriteBufferSize int
}

type Hub

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

func NewHub

func NewHub() *Hub

func (*Hub) Broadcast

func (r *Hub) Broadcast(data []byte)

func (*Hub) GetInit

func (r *Hub) GetInit() []byte

func (*Hub) GetStats

func (r *Hub) GetStats(name string) StreamStats

func (*Hub) GetStatus

func (r *Hub) GetStatus() Status

func (*Hub) IsReceivingFrames added in v1.1.0

func (r *Hub) IsReceivingFrames() bool

func (*Hub) Reset added in v1.1.0

func (r *Hub) Reset()

func (*Hub) SetFramerate

func (r *Hub) SetFramerate(framerate float64)

func (*Hub) SetInfo

func (r *Hub) SetInfo(codec string, width, height int, frameRate float64)

func (*Hub) SetInit

func (r *Hub) SetInit(data []byte)

func (*Hub) Subscribe

func (r *Hub) Subscribe() chan []byte

func (*Hub) Unsubscribe

func (r *Hub) Unsubscribe(ch chan []byte)

type Instance

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

func Init

func Init(cfg *Config) *Instance

func (*Instance) Connect

func (r *Instance) Connect() error

func (*Instance) GetHub

func (r *Instance) GetHub() *Hub

func (*Instance) HandleWebsocket

func (r *Instance) HandleWebsocket(conn *websocket.Conn)

func (*Instance) Handler

func (r *Instance) Handler() fiber.Handler

func (*Instance) InstanceReady

func (r *Instance) InstanceReady() bool

func (*Instance) Start

func (r *Instance) Start(addr string) error

func (*Instance) Stop

func (r *Instance) Stop() error

type Logger added in v1.1.0

type Logger interface {
	Print(v ...interface{})
	Printf(format string, v ...interface{})
}

type Status

type Status struct {
	Streams []*StreamStats `json:"streams"`
}

type StreamStats

type StreamStats struct {
	Name        string        `json:"name"`
	Ready       bool          `json:"ready"`
	Codec       string        `json:"codec"`
	Width       int           `json:"width"`
	Height      int           `json:"height"`
	Framerate   float64       `json:"framerate"`
	FrameNo     uint64        `json:"frameNo"`
	ClientCount int32         `json:"clientCount"`
	BytesRecv   uint64        `json:"bytesRecv"`
	Bitrate     float64       `json:"bitrateKbps"`
	Uptime      time.Duration `json:"uptime"`
}

Directories

Path Synopsis
command
webrtp command
experiment
streamable command
webtransport command

Jump to

Keyboard shortcuts

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