clientpublisher

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Mar 22, 2025 License: MIT Imports: 7 Imported by: 2

README

HLS WebSocket CDN - Publisher client library

This is a client library to publish HLS to HLS WebSocket CDN, implemented in golang.

Documentation

Installation

To install the library into your project, run:

go get github.com/AgustinSRG/hls-websocket-cdn/client-publisher

Usage

For each HLS stream you want to publish, create and instance of HlsWebSocketPublisher by calling the NewHlsWebSocketPublisher function.

You can then call the PublishFragment(duration, data) to publish each HLS fragment.

When the stream finishes. You must call the Close() method.

package main

import (
    "fmt"
	"time"

    // Import the module
    clientpublisher "github.com/AgustinSRG/hls-websocket-cdn/client-publisher"
)

func main() {
    // Create publisher instance
	publisher := clientpublisher.NewHlsWebSocketPublisher(clientpublisher.HlsWebSocketPublisherConfiguration{
		// URL of the CDN server
		ServerUrl: "ws://127.0.0.1/",
		// ID of the stream to publish
		StreamId: "test",
		// Secret to sign the authentication tokens
		AuthSecret: "secret",
		OnReady: func() {
			fmt.Println("Publisher is ready.")
		},
		OnError: func(url string, msg string) {
			fmt.Printf("Could not connect to %v: %v", url, msg)
		},
	})

	// After creating the publisher,
	// send the HLS fragments as soon as they are available
	for i := 0; i < 10; i++ {
		publisher.SendFragment(2, []byte{0x00})
		time.Sleep(3 * time.Second)
	}

	// When the last fragment is send, call Close() to finish
	publisher.Close()
}

Compilation

To compile the source code, you need to install Go. Install the latest stable version to avoid bugs.

In order to install dependencies, type:

go get .

To compile the code type:

go build .

Tests

To test the code, type:

go test -v

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HlsWebSocketPublisher

type HlsWebSocketPublisher struct {

	// Configuration
	Config HlsWebSocketPublisherConfiguration
	// contains filtered or unexported fields
}

HLS WebSocket publisher client

func NewHlsWebSocketPublisher

func NewHlsWebSocketPublisher(config HlsWebSocketPublisherConfiguration) *HlsWebSocketPublisher

Creates a new instance of HlsWebSocketPublisher Receives the configuration as the only parameter

func (*HlsWebSocketPublisher) Close

func (pub *HlsWebSocketPublisher) Close()

Finish the publisher This sends the CLOSE message and terminates the connection

func (*HlsWebSocketPublisher) IsClosed

func (publisher *HlsWebSocketPublisher) IsClosed() bool

Checks if the publisher is closed

func (*HlsWebSocketPublisher) SendFragment

func (pub *HlsWebSocketPublisher) SendFragment(duration float32, data []byte)

type HlsWebSocketPublisherConfiguration

type HlsWebSocketPublisherConfiguration struct {
	// Server URL
	ServerUrl string

	// Function to get the server URL
	// If set, ServerUrl is ignored
	GetServerUrl func() string

	// ID of the stream to publish
	StreamId string

	// Secret to generate authentication tokens
	AuthSecret string

	// Max length of the queue to keep fragments
	// if they cannot be sent to the server immediately
	// (10 by default)
	QueueMaxLength int

	// Function to be called when the publisher is ready
	OnReady func()

	// Function called on connection or authentication error
	// Receives the server URL and the error message
	OnError func(url string, msg string)

	// Delay to retry the connection after an error
	// Default: 1 second
	ConnectionRetryDelay time.Duration
}

Configuration for the publisher client

type WebsocketProtocolMessage

type WebsocketProtocolMessage struct {
	// Message type
	MessageType string

	// Message parameters
	Parameters map[string]string
}

Websocket protocol message

func ParseWebsocketProtocolMessage

func ParseWebsocketProtocolMessage(str string) *WebsocketProtocolMessage

Parses websocket protocol message from string

func (*WebsocketProtocolMessage) GetParameter

func (msg *WebsocketProtocolMessage) GetParameter(param string) string

Gets the parameter value

func (*WebsocketProtocolMessage) Serialize

func (msg *WebsocketProtocolMessage) Serialize() string

Serializes message to string (to be sent)

Jump to

Keyboard shortcuts

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