Download the Repository and open it with unity (Built in 2021.3.17f1) Install https://github.com/NexPlayer/NexPlayer_Unity_Plugin in the project There is a Main Scene, where there is a sample setup. The project consists of 2 main Prefabs
- The API, which handles the web requests
- The Video Player, which can be used to play Streams through playfab
- Video Player is based on https://github.com/NexPlayer/NexPlayer_Unity_Plugin for HLS video streaming
- First in the API Script you will need to set your Livepeer API Key
- All web requests are built using Unity's WebRequest class
- Web Requests are based on the Original Livepeer Documentation (https://docs.livepeer.org/reference/api/create-asset)
- All API calls are stored in
Scripts/MainAPI.cs
and Data Classes are stored inScripts/DataClasses.cs
Create Stream is used to create a stream with the custom data.
MainAPI.Instance.CreateStream(createStreamData, callback);
- CreateStreamData consists of these values:
public string name;
public StreamProfile[] profiles;
- Callback is called on completion, returning the Stream Response:
public int lastSeen;
public bool isActive;
public bool record;
public bool suspended;
public float sourceSegments;
public float transcodedSegments;
public float sourceSegmentsDuration;
public float sourceBytes;
public float transcodedBytes;
public StreamProfile[] profiles;
public string name;
public string kind;
public string userId;
public string id;
public long createdAt;
public string streamKey;
public string playbackId;
public string createdByTokenName;
Get Stream is used to get the data from the stream, using its ID
MainAPI.Instance.GetStream(streamId, callback);
- Callback is called on completion, returning the Stream Data if found:
public int lastSeen;
public bool isActive;
public bool record;
public bool suspended;
public float sourceSegments;
public float transcodedSegments;
public float sourceSegmentsDuration;
public float sourceBytes;
public float transcodedBytes;
public StreamProfile[] profiles;
public string name;
public string kind;
public string userId;
public string id;
public long createdAt;
public string streamKey;
public string playbackId;
public string createdByTokenName;
Get Stream List is used to get all the available stream, based on the user's API Key
MainAPI.Instance.GetStreamLeast(callback)
- Callback is called on completion, returning the list of the Streams
public List<StreamResponse> list
StreamResponse class, is consisted of the Data of each Stream, same with GetStream response
Update Stream Data is used to change the current data of a stream using its ID
MainAPI.Instance.UpdateStreamData(streamId, newData);
- newData is type of UpdateStreamDataObj
public bool record;
public bool suspended;
Delete Stream is used to delete a stream using its ID
MainAPI.Instance.DeleteStream(streamId);
To upload an asset, your first need to request for a direct upload URL and only then actually upload the contents of the asset.
This also allows your users to upload files since the upload URL is pre-signed with your credentials and is accessible from the browser. The initial API call to get the upload URL must be done from a backend though, from which an API token is available.
MainAPI.Instance.CreateAsset(name, callback);
- callback is of type Action Where response has:
public string url;
public string tusEndpoint;
public AssetObject asset;
public string name;
AssetObject:
public string id;
public string playbackId;
public string userId;
public Status status;
Same like Create asset, but uses external url for the video.
MainAPI.Instance.CreateAssetViaUrl(url, name, callback)
URL is the url of the asset to be created with The callback is the same with the CreateAsset
A signing key is used to sign JWTs and are necessary to make a stream private.
When you create a signing key, the request returns a base64 encoded pem keypair, where
The publicKey is a representation of the public key, encoded as base 64 and is passed as a string The privateKey is displayed only on creation. This is the only moment where the client can save the private key, otherwise it will be lost. Remember to decode your string when signing JWTs
Up to 10 signing keys can be generated, after that you must delete at least one signing key to create a new one.
MainAPI.Instance.CreateSigningKey(callback);`
Callback returns object of type SigningKeyResponse
public string id;
public string name;
public string userId;
public long createdAt;
public string privateKey;
public string publicKey;
Creates a multistream target object.
MainAPI.Instance.CreateMultiStream(name url, callback);`
Callback returns object of type MultistreamTargetResponse
public string id;
public string userId;
public string name;
public string url;
To create a new webhook, you need to make an API call with the events you want to listen for and the URL that will be called when those events occur.
MainAPI.Instance.CreateWebhook(data, callback);
Data is of type WebhookRequestData
public string[] events;
public string url;
public string name;
While callback returns object of type WebhookResponse
public long createdAt;
public string[] events;
public string id;
public string kind;
public string name;
public string url;
public string userId;
This will archive an existing asset. This means that deleted assets can still be accessed through their download and playback URLs.
MainAPI.Instance.DeleteAsset(assetId);
Deletes an existing signing key object.
MainAPI.Instance.DeleteSignkingKey(keyId);
Deletes an existing multistream target object. Make sure to remove any references to the target on existing streams before actually deleting it from the API.
MainAPI.Instance.DeleteMultistream(streamId);
Retrieves the details of an Asset that has previously been imported or uploaded. Supply the unique Asset ID that was returned from your previous request, and Livepeer will return the corresponding Asset information.
MainAPI.Instance.GetAsset(assetId, callback);
Callback returns object of type CreateAssetResponse
You can retrieve a multistream target with its ID
MainAPI.Instance.GetMultiStream(streamId, callback);
Callback returns object of type MultistreamTargetResponse
You can retrieve the playback information by calling the GET playback endpoint with the playbackId.
MainAPI.Instance.GetPlaybackInfo(playbackId, callback);
Callback returns object of type PlaybackInfoResponse
public string type;
public PlaybackInfoMeta[] meta;
PlaybackInfoMeta
public PlaybackInfoSource[] source;
PlaybackInfoSource
public string hrn;
public string type;
public string url;
public long size;
public int width;
public int height;
public long bitrate;
To a return list of recorded sessions with the same parentId.
MainAPI.Instance.GetRecordedSessions(parentId, record, callback);
Callback returns object of type RecordedSessionsResponse
public float sourceSegmentsDuration;
public string id;
public bool record;
public string parentId;
public string recordingStatus;
public string recordingUrl;
public string mp4URL;
You can list all the signing keys in your account.
MainAPI.Instance.GetSigningKeys(callback);
Callback returns object of type SigningKeyResponse
public string id;
public string name;
public string userId;
public long createdAt;
public string privateKey;
public string publicKey;
Get a tsk from the account, using its ID.
MainAPI.Instance.GetTask(taskId, callback);
Callback returns object of type TaskResponse
public string id;
public string type;
public Output output;
public Params @params;
public Status status;
public string userId;
public long createdAt;
public string outputAssetId;
Does the same with Get Task, but returns an array of Task Responses, with the latest task first.
MainAPI.Instance.GetTasks(callback);
Callback returns an array of objects of type TaskResponse
You can retrieve the webhooks by calling the GET webhook endpoint with the webhookId.
MainAPI.Instance.GetWebHook(webhookId, callback);
Callback returns object of type WebhookResponse
public long createdAt;
public string[] events;
public string id;
public string kind;
public string name;
public string url;
public string userId;
You can update a webhook by calling the PATCH webhook endpoint with the webhookId. It is a single endpoint that can update any of the mutable properties of a webhook. Specifically:
- name
- url
- events
MainAPI.Instance.UpdateWebhook(id, data, callback);
Data is type of WebhookRequestData Callback returns object of type Boolean
PATCH /stream/{id}/record can only modify a stream object. You cannot modify the record value on a session object or the historic stream objects with a parentId (stream objects representing a single live stream session).
The record value is inherited by all future child session objects. Child session objects are read-only.
MainAPI.Instance.PatchRecording(id, recording, callback);
Callback returns object of type Boolean
PATCH /multistream/target/{id} updates an existing multistream target object. You can change any user-defined field in the current object, specifically name, url and disabled.
All fields are optional, and if any field is not included in the request payload it will be kept unchanged from the saved object.
You cannot access the current url field for updating it since we redact it from GET responses. If you want to update the URL you have to build it from scratch. You can also omit it from the PATCH payload to keep it as is.
A 204 No Content status response indicates the multistream target was successfully updated.
MainAPI.Instance.UpdateMultistreamTarget(id, name, mUrl, disabled, callback);
Callback returns object of type Boolean
PATCH /access-control/signing-key/{KEY_ID} updates an existing signing key. It is a single endpoint that can update any of the mutable properties of a stream. Specifically:
- name
- disabled
All fields are optional, and if any field is not included in the request payload it will be kept unchanged from the saved object.
MainAPI.Instance.UpdateSigningKey(id, name, disabled, callback);
Callback returns object of type Boolean
PATCH /asset/:id -d { } can be used to modify any asset that was uploaded with On Demand. It is a single endpoint that can update only the following mutable properties of an asset:
- name
- storage
MainAPI.Instance.UpdateAsset(id, name, storageJson, callback);
Callback returns object of type Boolean
Transcode Video transcodes a video file and uploads the results to the specified storage service.
MainAPI.Instance.TranscodeVideo(data, callback);
Data Input:
{
"input":{
"url":"https://www.example.com/video.mp4"
},
"storage":{
"type":"s3",
"endpoint":"https://gateway.storjshare.io",
"credentials":{
"accessKeyId":"$ACCESS_KEY_ID",
"secretAccessKey":"$SECRET_ACCESS_KEY"
},
"bucket":"mybucket"
},
"outputs":{
"hls":{
"path":"/samplevideo/hls"
},
"mp4":{
"path":"/samplevideo/mp4"
}
},
"profiles":[
{
"name":"480p",
"bitrate":1000000,
"fps":30,
"width":854,
"height":480
},
{
"name":"360p",
"bitrate":500000,
"fps":30,
"width":640,
"height":360
}
]
}
- Add the Video Player Prefab
- Find the NexPlayer_Manager object (child of Video Player)
- Set URL value, tick the Is Live Stream and AutoPlay
- Add the VideoManager.cs script in a game object in the scene
- Set the streamId and playback_base_url values
- Assign the NexPlayer script
- It is setup to play the stream On Start, otherwise PlayStream function can be called to play a stream on run-time