Skip to content

livepeer/livepeer-java

Repository files navigation

Livepeer Java SDK

The Livepeer Java SDK provides convenient access to the Livepeer Studio API from applications written in Java.

Documentation

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

SDK Installation

Getting started

JDK 11 or later is required.

The samples below show how a published SDK artifact is used:

Gradle:

implementation 'studio.livepeer:livepeer:0.5.0'

Maven:

<dependency>
    <groupId>studio.livepeer</groupId>
    <artifactId>livepeer</artifactId>
    <version>0.5.0</version>
</dependency>

How to build

After cloning the git repository to your file system you can build the SDK artifact from source to the build directory by running ./gradlew build on *nix systems or gradlew.bat on Windows systems.

If you wish to build from source and publish the SDK artifact to your local Maven repository (on your filesystem) then use the following command (after cloning the git repo locally):

On *nix:

./gradlew publishToMavenLocal -Pskip.signing

On Windows:

gradlew.bat publishToMavenLocal -Pskip.signing

SDK Example Usage

Example

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.FfmpegProfile;
import studio.livepeer.livepeer.models.components.Location;
import studio.livepeer.livepeer.models.components.Multistream;
import studio.livepeer.livepeer.models.components.NewStreamPayload;
import studio.livepeer.livepeer.models.components.NewStreamPayloadRecordingSpec;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.Profile;
import studio.livepeer.livepeer.models.components.Pull;
import studio.livepeer.livepeer.models.components.Target;
import studio.livepeer.livepeer.models.components.TargetSpec;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.CreateStreamResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewStreamPayload req = NewStreamPayload.builder()
                .name("test_stream")
                .pull(Pull.builder()
                    .source("https://myservice.com/live/stream.flv")
                    .headers(Map.ofEntries(
                        Map.entry("Authorization", "Bearer 123")))
                    .location(Location.builder()
                        .lat(39.739d)
                        .lon(-104.988d)
                        .build())
                    .build())
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    FfmpegProfile.builder()
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .bitrate(3000000L)
                        .fps(30L)
                        .fpsDen(1L)
                        .quality(23L)
                        .gop("2")
                        .profile(Profile.H264_BASELINE)
                        .build()))
                .record(false)
                .recordingSpec(NewStreamPayloadRecordingSpec.builder()
                    .profiles(List.of(
                        TranscodeProfile.builder()
                            .bitrate(3000000L)
                            .width(1280L)
                            .name("720p")
                            .height(720L)
                            .quality(23L)
                            .fps(30L)
                            .fpsDen(1L)
                            .gop("2")
                            .profile(TranscodeProfileProfile.H264_BASELINE)
                            .encoder(TranscodeProfileEncoder.H264)
                            .build()))
                    .build())
                .multistream(Multistream.builder()
                    .targets(List.of(
                        Target.builder()
                            .profile("720p0")
                            .videoOnly(false)
                            .id("PUSH123")
                            .spec(TargetSpec.builder()
                                .url("rtmps://live.my-service.tv/channel/secretKey")
                                .name("My target")
                                .build())
                            .build()))
                    .build())
                .build();

            CreateStreamResponse res = sdk.stream().create()
                .request(req)
                .call();

            if (res.stream().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Available Resources and Operations

  • getAll - Retrieve Multistream Targets
  • create - Create a multistream target
  • get - Retrieve a multistream target
  • update - Update Multistream Target
  • delete - Delete a multistream target
  • 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
  • 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 Tasks
  • get - Retrieve a Task
  • get - Retrieve Playback Info

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or raise an error. If Error objects are specified in your OpenAPI Spec, the SDK will throw the appropriate Exception type.

Error Object Status Code Content Type
models/errors/Error 404 application/json
models/errors/SDKError 4xx-5xx */*

Example

package hello.world;

import java.lang.Exception;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.GetPlaybackInfoResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            GetPlaybackInfoResponse res = sdk.playback().get()
                .id("<value>")
                .call();

            if (res.playbackInfo().isPresent()) {
                // handle response
            }
        } catch (studio.livepeer.livepeer.models.errors.Error e) {
            // handle exception
            throw e;
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Server Selection

Select Server by Index

You can override the default server globally by passing a server index to the serverIndex builder method when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Variables
0 https://livepeer.studio/api None

Example

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.FfmpegProfile;
import studio.livepeer.livepeer.models.components.Location;
import studio.livepeer.livepeer.models.components.Multistream;
import studio.livepeer.livepeer.models.components.NewStreamPayload;
import studio.livepeer.livepeer.models.components.NewStreamPayloadRecordingSpec;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.Profile;
import studio.livepeer.livepeer.models.components.Pull;
import studio.livepeer.livepeer.models.components.Target;
import studio.livepeer.livepeer.models.components.TargetSpec;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.CreateStreamResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .serverIndex(0)
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewStreamPayload req = NewStreamPayload.builder()
                .name("test_stream")
                .pull(Pull.builder()
                    .source("https://myservice.com/live/stream.flv")
                    .headers(Map.ofEntries(
                        Map.entry("Authorization", "Bearer 123")))
                    .location(Location.builder()
                        .lat(39.739d)
                        .lon(-104.988d)
                        .build())
                    .build())
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    FfmpegProfile.builder()
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .bitrate(3000000L)
                        .fps(30L)
                        .fpsDen(1L)
                        .quality(23L)
                        .gop("2")
                        .profile(Profile.H264_BASELINE)
                        .build()))
                .record(false)
                .recordingSpec(NewStreamPayloadRecordingSpec.builder()
                    .profiles(List.of(
                        TranscodeProfile.builder()
                            .bitrate(3000000L)
                            .width(1280L)
                            .name("720p")
                            .height(720L)
                            .quality(23L)
                            .fps(30L)
                            .fpsDen(1L)
                            .gop("2")
                            .profile(TranscodeProfileProfile.H264_BASELINE)
                            .encoder(TranscodeProfileEncoder.H264)
                            .build()))
                    .build())
                .multistream(Multistream.builder()
                    .targets(List.of(
                        Target.builder()
                            .profile("720p0")
                            .videoOnly(false)
                            .id("PUSH123")
                            .spec(TargetSpec.builder()
                                .url("rtmps://live.my-service.tv/channel/secretKey")
                                .name("My target")
                                .build())
                            .build()))
                    .build())
                .build();

            CreateStreamResponse res = sdk.stream().create()
                .request(req)
                .call();

            if (res.stream().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Override Server URL Per-Client

The default server can also be overridden globally by passing a URL to the serverURL builder method when initializing the SDK client instance. For example:

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.FfmpegProfile;
import studio.livepeer.livepeer.models.components.Location;
import studio.livepeer.livepeer.models.components.Multistream;
import studio.livepeer.livepeer.models.components.NewStreamPayload;
import studio.livepeer.livepeer.models.components.NewStreamPayloadRecordingSpec;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.Profile;
import studio.livepeer.livepeer.models.components.Pull;
import studio.livepeer.livepeer.models.components.Target;
import studio.livepeer.livepeer.models.components.TargetSpec;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.CreateStreamResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .serverURL("https://livepeer.studio/api")
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewStreamPayload req = NewStreamPayload.builder()
                .name("test_stream")
                .pull(Pull.builder()
                    .source("https://myservice.com/live/stream.flv")
                    .headers(Map.ofEntries(
                        Map.entry("Authorization", "Bearer 123")))
                    .location(Location.builder()
                        .lat(39.739d)
                        .lon(-104.988d)
                        .build())
                    .build())
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    FfmpegProfile.builder()
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .bitrate(3000000L)
                        .fps(30L)
                        .fpsDen(1L)
                        .quality(23L)
                        .gop("2")
                        .profile(Profile.H264_BASELINE)
                        .build()))
                .record(false)
                .recordingSpec(NewStreamPayloadRecordingSpec.builder()
                    .profiles(List.of(
                        TranscodeProfile.builder()
                            .bitrate(3000000L)
                            .width(1280L)
                            .name("720p")
                            .height(720L)
                            .quality(23L)
                            .fps(30L)
                            .fpsDen(1L)
                            .gop("2")
                            .profile(TranscodeProfileProfile.H264_BASELINE)
                            .encoder(TranscodeProfileEncoder.H264)
                            .build()))
                    .build())
                .multistream(Multistream.builder()
                    .targets(List.of(
                        Target.builder()
                            .profile("720p0")
                            .videoOnly(false)
                            .id("PUSH123")
                            .spec(TargetSpec.builder()
                                .url("rtmps://live.my-service.tv/channel/secretKey")
                                .name("My target")
                                .build())
                            .build()))
                    .build())
                .build();

            CreateStreamResponse res = sdk.stream().create()
                .request(req)
                .call();

            if (res.stream().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
apiKey http HTTP Bearer

To authenticate with the API the apiKey parameter must be set when initializing the SDK client instance. For example:

package hello.world;

import java.lang.Exception;
import java.util.List;
import java.util.Map;
import studio.livepeer.livepeer.Livepeer;
import studio.livepeer.livepeer.models.components.FfmpegProfile;
import studio.livepeer.livepeer.models.components.Location;
import studio.livepeer.livepeer.models.components.Multistream;
import studio.livepeer.livepeer.models.components.NewStreamPayload;
import studio.livepeer.livepeer.models.components.NewStreamPayloadRecordingSpec;
import studio.livepeer.livepeer.models.components.PlaybackPolicy;
import studio.livepeer.livepeer.models.components.Profile;
import studio.livepeer.livepeer.models.components.Pull;
import studio.livepeer.livepeer.models.components.Target;
import studio.livepeer.livepeer.models.components.TargetSpec;
import studio.livepeer.livepeer.models.components.TranscodeProfile;
import studio.livepeer.livepeer.models.components.TranscodeProfileEncoder;
import studio.livepeer.livepeer.models.components.TranscodeProfileProfile;
import studio.livepeer.livepeer.models.components.Type;
import studio.livepeer.livepeer.models.errors.SDKError;
import studio.livepeer.livepeer.models.operations.CreateStreamResponse;

public class Application {

    public static void main(String[] args) throws Exception {
        try {
            Livepeer sdk = Livepeer.builder()
                .apiKey("<YOUR_BEARER_TOKEN_HERE>")
                .build();

            NewStreamPayload req = NewStreamPayload.builder()
                .name("test_stream")
                .pull(Pull.builder()
                    .source("https://myservice.com/live/stream.flv")
                    .headers(Map.ofEntries(
                        Map.entry("Authorization", "Bearer 123")))
                    .location(Location.builder()
                        .lat(39.739d)
                        .lon(-104.988d)
                        .build())
                    .build())
                .playbackPolicy(PlaybackPolicy.builder()
                    .type(Type.WEBHOOK)
                    .webhookId("1bde4o2i6xycudoy")
                    .webhookContext(Map.ofEntries(
                        Map.entry("streamerId", "my-custom-id")))
                    .refreshInterval(600d)
                    .build())
                .profiles(List.of(
                    FfmpegProfile.builder()
                        .width(1280L)
                        .name("720p")
                        .height(720L)
                        .bitrate(3000000L)
                        .fps(30L)
                        .fpsDen(1L)
                        .quality(23L)
                        .gop("2")
                        .profile(Profile.H264_BASELINE)
                        .build()))
                .record(false)
                .recordingSpec(NewStreamPayloadRecordingSpec.builder()
                    .profiles(List.of(
                        TranscodeProfile.builder()
                            .bitrate(3000000L)
                            .width(1280L)
                            .name("720p")
                            .height(720L)
                            .quality(23L)
                            .fps(30L)
                            .fpsDen(1L)
                            .gop("2")
                            .profile(TranscodeProfileProfile.H264_BASELINE)
                            .encoder(TranscodeProfileEncoder.H264)
                            .build()))
                    .build())
                .multistream(Multistream.builder()
                    .targets(List.of(
                        Target.builder()
                            .profile("720p0")
                            .videoOnly(false)
                            .id("PUSH123")
                            .spec(TargetSpec.builder()
                                .url("rtmps://live.my-service.tv/channel/secretKey")
                                .name("My target")
                                .build())
                            .build()))
                    .build())
                .build();

            CreateStreamResponse res = sdk.stream().create()
                .request(req)
                .call();

            if (res.stream().isPresent()) {
                // handle response
            }
        } catch (SDKError e) {
            // handle exception
            throw e;
        } catch (Exception e) {
            // handle exception
            throw e;
        }

    }
}

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Feel free to open a PR or a Github issue as a proof of concept and we'll do our best to include it in a future release!

SDK Created by Speakeasy

About

Java library for the Livepeer API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages