Skip to content

livepeer/livepeer-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Livepeer Go SDK

The Livepeer Go library provides convenient access to the Livepeer Studio API from applications written in Golang.

Documentation

For full documentation and examples, please visit docs.livepeer.org.

Summary

Livepeer API Reference: Welcome to the Livepeer API reference docs. Here you will find all the endpoints exposed on the standard Livepeer API, learn how to use them and what they return.

Table of Contents

SDK Installation

To add the SDK as a dependency to your project:

go get github.com/livepeer/livepeer-go

SDK Example Usage

Example

package main

import (
	"context"
	livepeer "github.com/livepeer/livepeer-go"
	"github.com/livepeer/livepeer-go/models/components"
	"log"
)

func main() {
	s := livepeer.New(
		livepeer.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)

	ctx := context.Background()
	res, err := s.Stream.Create(ctx, components.NewStreamPayload{
		Name: "test_stream",
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Available Resources and Operations

Available methods
  • Create - Create a signing key
  • GetAll - Retrieves signing keys
  • Delete - Delete Signing Key
  • Get - Retrieves a signing key
  • Update - Update a signing key
  • GetAll - Retrieve Multistream Targets
  • Create - Create a multistream target
  • Get - Retrieve a multistream target
  • Update - Update Multistream Target
  • Delete - Delete a multistream target
  • Get - Retrieve Playback Info
  • Create - Create a room ⚠️ Deprecated
  • Get - Retrieve a room ⚠️ Deprecated
  • Delete - Delete a room ⚠️ Deprecated
  • StartEgress - Start room RTMP egress ⚠️ Deprecated
  • StopEgress - Stop room RTMP egress ⚠️ Deprecated
  • CreateUser - Create a room user ⚠️ Deprecated
  • GetUser - Get user details ⚠️ Deprecated
  • UpdateUser - Update a room user ⚠️ Deprecated
  • DeleteUser - Remove a user from the room ⚠️ Deprecated
  • GetAll - Retrieve Tasks
  • Get - Retrieve a Task

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or an error, they will never return both. When specified by the OpenAPI spec document, the SDK will return the appropriate subclass.

Error Object Status Code Content Type
sdkerrors.Error 404 application/json
sdkerrors.SDKError 4xx-5xx /

Example

package main

import (
	"context"
	"errors"
	livepeergo "github.com/livepeer/livepeer-go"
	"github.com/livepeer/livepeer-go/models/sdkerrors"
	"log"
)

func main() {
	s := livepeergo.New(
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)

	ctx := context.Background()
	res, err := s.Playback.Get(ctx, "<id>")
	if err != nil {

		var e *sdkerrors.Error
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}

		var e *sdkerrors.SDKError
		if errors.As(err, &e) {
			// handle error
			log.Fatal(e.Error())
		}
	}
}

Custom HTTP Client

The Go SDK makes API calls that wrap an internal HTTP client. The requirements for the HTTP client are very simple. It must match this interface:

type HTTPClient interface {
	Do(req *http.Request) (*http.Response, error)
}

The built-in net/http client satisfies this interface and a default client based on the built-in is provided by default. To replace this default with a client of your own, you can implement this interface yourself or provide your own client configured as desired. Here's a simple example, which adds a client with a 30 second timeout.

import (
	"net/http"
	"time"
	"github.com/myorg/your-go-sdk"
)

var (
	httpClient = &http.Client{Timeout: 30 * time.Second}
	sdkClient  = sdk.New(sdk.WithClient(httpClient))
)

This can be a convenient way to configure timeouts, cookies, proxies, custom headers, and other low-level configuration.

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
APIKey http HTTP Bearer

You can configure it using the WithSecurity option when initializing the SDK client instance. For example:

package main

import (
	"context"
	livepeergo "github.com/livepeer/livepeer-go"
	"github.com/livepeer/livepeer-go/models/components"
	"log"
)

func main() {
	s := livepeergo.New(
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)

	ctx := context.Background()
	res, err := s.Stream.Create(ctx, components.NewStreamPayload{
		Name: "test_stream",
		Pull: &components.Pull{
			Source: "https://myservice.com/live/stream.flv",
			Headers: map[string]string{
				"Authorization": "Bearer 123",
			},
			Location: &components.Location{
				Lat: 39.739,
				Lon: -104.988,
			},
		},
		PlaybackPolicy: &components.PlaybackPolicy{
			Type:      components.TypeWebhook,
			WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
			WebhookContext: map[string]any{
				"streamerId": "my-custom-id",
			},
			RefreshInterval: livepeergo.Float64(600),
		},
		Profiles: []components.FfmpegProfile{
			components.FfmpegProfile{
				Width:   1280,
				Name:    "720p",
				Height:  720,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		RecordingSpec: &components.NewStreamPayloadRecordingSpec{
			Profiles: []components.TranscodeProfile{
				components.TranscodeProfile{
					Width:   livepeergo.Int64(1280),
					Name:    livepeergo.String("720p"),
					Height:  livepeergo.Int64(720),
					Bitrate: 3000000,
					Quality: livepeergo.Int64(23),
					Fps:     livepeergo.Int64(30),
					FpsDen:  livepeergo.Int64(1),
					Gop:     livepeergo.String("2"),
					Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
					Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
				},
			},
		},
		Multistream: &components.Multistream{
			Targets: []components.Target{
				components.Target{
					Profile:   "720p",
					VideoOnly: livepeergo.Bool(false),
					ID:        livepeergo.String("PUSH123"),
					Spec: &components.TargetSpec{
						Name: livepeergo.String("My target"),
						URL:  "rtmps://live.my-service.tv/channel/secretKey",
					},
				},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

Special Types

Retries

Some of the endpoints in this SDK support retries. If you use the SDK without any configuration, it will fall back to the default retry strategy provided by the API. However, the default retry strategy can be overridden on a per-operation basis, or across the entire SDK.

To change the default retry strategy for a single API call, simply provide a retry.Config object to the call by using the WithRetries option:

package main

import (
	"context"
	livepeergo "github.com/livepeer/livepeer-go"
	"github.com/livepeer/livepeer-go/models/components"
	"github.com/livepeer/livepeer-go/retry"
	"log"
	"models/operations"
)

func main() {
	s := livepeergo.New(
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)

	ctx := context.Background()
	res, err := s.Stream.Create(ctx, components.NewStreamPayload{
		Name: "test_stream",
		Pull: &components.Pull{
			Source: "https://myservice.com/live/stream.flv",
			Headers: map[string]string{
				"Authorization": "Bearer 123",
			},
			Location: &components.Location{
				Lat: 39.739,
				Lon: -104.988,
			},
		},
		PlaybackPolicy: &components.PlaybackPolicy{
			Type:      components.TypeWebhook,
			WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
			WebhookContext: map[string]any{
				"streamerId": "my-custom-id",
			},
			RefreshInterval: livepeergo.Float64(600),
		},
		Profiles: []components.FfmpegProfile{
			components.FfmpegProfile{
				Width:   1280,
				Name:    "720p",
				Height:  720,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		RecordingSpec: &components.NewStreamPayloadRecordingSpec{
			Profiles: []components.TranscodeProfile{
				components.TranscodeProfile{
					Width:   livepeergo.Int64(1280),
					Name:    livepeergo.String("720p"),
					Height:  livepeergo.Int64(720),
					Bitrate: 3000000,
					Quality: livepeergo.Int64(23),
					Fps:     livepeergo.Int64(30),
					FpsDen:  livepeergo.Int64(1),
					Gop:     livepeergo.String("2"),
					Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
					Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
				},
			},
		},
		Multistream: &components.Multistream{
			Targets: []components.Target{
				components.Target{
					Profile:   "720p",
					VideoOnly: livepeergo.Bool(false),
					ID:        livepeergo.String("PUSH123"),
					Spec: &components.TargetSpec{
						Name: livepeergo.String("My target"),
						URL:  "rtmps://live.my-service.tv/channel/secretKey",
					},
				},
			},
		},
	}, operations.WithRetries(
		retry.Config{
			Strategy: "backoff",
			Backoff: &retry.BackoffStrategy{
				InitialInterval: 1,
				MaxInterval:     50,
				Exponent:        1.1,
				MaxElapsedTime:  100,
			},
			RetryConnectionErrors: false,
		}))
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}

If you'd like to override the default retry strategy for all operations that support retries, you can use the WithRetryConfig option at SDK initialization:

package main

import (
	"context"
	livepeergo "github.com/livepeer/livepeer-go"
	"github.com/livepeer/livepeer-go/models/components"
	"github.com/livepeer/livepeer-go/retry"
	"log"
)

func main() {
	s := livepeergo.New(
		livepeergo.WithRetryConfig(
			retry.Config{
				Strategy: "backoff",
				Backoff: &retry.BackoffStrategy{
					InitialInterval: 1,
					MaxInterval:     50,
					Exponent:        1.1,
					MaxElapsedTime:  100,
				},
				RetryConnectionErrors: false,
			}),
		livepeergo.WithSecurity("<YOUR_BEARER_TOKEN_HERE>"),
	)

	ctx := context.Background()
	res, err := s.Stream.Create(ctx, components.NewStreamPayload{
		Name: "test_stream",
		Pull: &components.Pull{
			Source: "https://myservice.com/live/stream.flv",
			Headers: map[string]string{
				"Authorization": "Bearer 123",
			},
			Location: &components.Location{
				Lat: 39.739,
				Lon: -104.988,
			},
		},
		PlaybackPolicy: &components.PlaybackPolicy{
			Type:      components.TypeWebhook,
			WebhookID: livepeergo.String("1bde4o2i6xycudoy"),
			WebhookContext: map[string]any{
				"streamerId": "my-custom-id",
			},
			RefreshInterval: livepeergo.Float64(600),
		},
		Profiles: []components.FfmpegProfile{
			components.FfmpegProfile{
				Width:   1280,
				Name:    "720p",
				Height:  720,
				Bitrate: 3000000,
				Fps:     30,
				FpsDen:  livepeergo.Int64(1),
				Quality: livepeergo.Int64(23),
				Gop:     livepeergo.String("2"),
				Profile: components.ProfileH264Baseline.ToPointer(),
			},
		},
		Record: livepeergo.Bool(false),
		RecordingSpec: &components.NewStreamPayloadRecordingSpec{
			Profiles: []components.TranscodeProfile{
				components.TranscodeProfile{
					Width:   livepeergo.Int64(1280),
					Name:    livepeergo.String("720p"),
					Height:  livepeergo.Int64(720),
					Bitrate: 3000000,
					Quality: livepeergo.Int64(23),
					Fps:     livepeergo.Int64(30),
					FpsDen:  livepeergo.Int64(1),
					Gop:     livepeergo.String("2"),
					Profile: components.TranscodeProfileProfileH264Baseline.ToPointer(),
					Encoder: components.TranscodeProfileEncoderH264.ToPointer(),
				},
			},
		},
		Multistream: &components.Multistream{
			Targets: []components.Target{
				components.Target{
					Profile:   "720p",
					VideoOnly: livepeergo.Bool(false),
					ID:        livepeergo.String("PUSH123"),
					Spec: &components.TargetSpec{
						Name: livepeergo.String("My target"),
						URL:  "rtmps://live.my-service.tv/channel/secretKey",
					},
				},
			},
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	if res.Stream != nil {
		// handle response
	}
}