Play a livestream
In this guide, we demonstrate how to play livestreams in your application using either Livepeer React or Video.js.
In a production application, with CORS-enabled API
keys, the viewer should not have
access to the stream ID. The playbackId
should be passed to the viewer
only, since this allows the viewer to watch the asset.
Using the Livepeer React Player
The example below show to use the Livepeer React
Player
to view a video asset, with some custom
styles to demonstrate what’s possible.
Configuring Providers
We
create a new React client and wrap the app with LivepeerConfig
.
Play Video
Now that our providers are set up, we use the
Player
with an IPFS CID as our playbackId
,
which we created previously when uploading a video asset and
storing to IPFS. We use
Next.js Image as
our optimized poster image, but this could also be a simple image URL.
We also override some of the custom styling to match the flow of our app!
import { Player } from "@livepeer/react";
import Image from "next/image";
const playbackId = "f5eese9wwl88k4g8";
import blenderPoster from "../../public/images/blender-poster.png";
const PosterImage = () => {
return (
<Image
src={blenderPoster}
layout="fill"
objectFit="cover"
priority
placeholder="blur"
/>
);
};
export const DemoPlayer = () => {
return (
<Player
title="Waterfalls"
playbackId={playbackId}
showPipButton
showTitle={false}
aspectRatio="16to9"
poster={<PosterImage />}
controls={{
autohide: 3000,
}}
theme={{
borderStyles: { containerBorderStyle: "hidden" },
radii: { containerBorderRadius: "10px" },
}}
/>
);
};
Using your own player
Using Livepeer React is the recommended way to play back a video or a livestream - it handles MP4 renditions, errors from the API, and is a true web3-native media player. However, if you want to use an alternative, you can do so by following the instructions below.
To ensure consistent viewer experience, we strongly recommend using a player (like the Livepeer Player) that handles choosing the correct MP4 rendition and gracefully recovers from routine RTMP reconnects through custom polling logic. In the absence of this capability, reconnects may severely degrade viewer experience and require a hard refresh to resolve, and an incorrect MP4 rendition for the screen size may be chosen.
Fetch the playback URL
To play back a livestream in other players, you’ll need to fetch the playback URL(s). By default, all content has an HLS endpoint. HLS is a protocol that allows you to stream video and audio content over HTTP. Much of the video you watch on the web is delivered using HLS. Livepeer uses HLS to deliver video and audio content.
The playback URL format is subject to change. Do not try to create the playback URLs manually, as they may change periodically.
You can get the playback url by using the below format and replacing the
PLAYBACK_ID
with your stream or asset’s playbackId
.
https://livepeer.studio/api/playback/{PLAYBACK_ID}
The API will return a JSON payload which looks like:
{
"type": "vod",
"meta": {
"source": [
{
"hrn": "HLS (TS)",
"type": "html5/application/vnd.apple.mpegurl",
"url": "https://livepeercdn.studio/recordings/{RECORDING_ID}/index.m3u8"
}
]
}
}
Please note that to play back livestreams inside your application you’ll need to use a video player component that supports HLS.
The format of this payload is defined here. The
url
field can be used as the playback URL.
Handling various playback sources
The playback info endpoint can return multiple sources in the response, as outlined above. These may include short form MP4 playback URLs, which allow you to obtain alternative URLs for your video asset to enable applications (and CDNs) to cache short videos for instant playback of subsequent videos. This means that viewers can experience instant time-to-first-frame (TTFF) when watching short videos.
It is important to note that short form playback URLs are only available for video assets that are less than 2 minutes in duration.
These may also include WebRTC URLs for low latency livestream playback. These must be played back with our ICE servers, which are used to route traffic in restricted networking environments. The WebRTC WHEP negotiation should send back these STUN/TURN servers in the SDP response, so there should not be any customization necessary to use the ICE servers with a custom player.
You can get the playback URLs by retrieving the playback info, replacing
PLAYBACK_ID
with your asset’s playbackId:
https://livepeer.studio/api/playback/{PLAYBACK_ID}
If there are MP4 renditions or WebRTC playback available, the API will return a JSON payload similar to:
{
"type": "vod",
"meta": {
"source": [
{
"hrn": "MP4",
"type": "html5/video/mp4",
"url": "https://asset-cdn.lp-playback.com/hls/{PLAYBACK_ID}/static360p0.mp4",
"size": 494778,
"width": 204,
"height": 360,
"bitrate": 449890
},
{
"hrn": "MP4",
"type": "html5/video/mp4",
"url": "https://asset-cdn.lp-playback.com/hls/{PLAYBACK_ID}/static720p0.mp4",
"size": 1869154,
"width": 406,
"height": 720,
"bitrate": 1996936
},
{
"hrn": "HLS (TS)",
"type": "html5/application/vnd.apple.mpegurl",
"url": "https://livepeercdn.studio/recordings/{RECORDING_ID}/index.m3u8"
}
]
}
}
There are multiple renditions you can choose from, and it is up to you to decide how you want to prioritize each source for your custom player. See the Player’s technical details for more information on how Livepeer Player handles this.
When you make a request for playback URLs, in the response MP4 URLs are always listed before HLS URLs. Additionally, each MP4 URL includes additional metadata about the video, such as its width, height, bitrate, and size. This metadata can be useful for mobile applications that want to optimize playback quality and size based on the viewer’s device and network conditions. The Livepeer React Player automatically handles this. See the Player docs for more information.
Use the playback URL in a player
You can use the playback URL with any video player that supports HLS. Here is a list of popular players that support HLS:
- Video.js
- Plyr.io
- JW Player
- Bitmovin Player
- HLS.js (requires custom logic to wire to a video element)
Here is an example of how to use the playback URL in video.js player.
<head>
<link href="https://vjs.zencdn.net/7.20.3/video-js.css" rel="stylesheet" />
<!-- If you'd like to support IE8 (for Video.js versions prior to v7) -->
<!-- <script src="https://vjs.zencdn.net/ie8/1.1.2/videojs-ie8.min.js"></script> -->
</head>
<body>
<video
id="my-video"
class="video-js"
controls
preload="auto"
width="640"
height="264"
poster="MY_VIDEO_POSTER.jpg"
>
<source
src="https://lp-playback.com/hls/{PLAYBACK_ID}/index.m3u8"
type="application/x-mpegURL"
/>
</video>
<script src="https://vjs.zencdn.net/7.20.3/video.min.js"></script>
</body>
Embeddable Player
Livepeer Studio maintains an embeddable version of the Livepeer Player that is suitable for iframing or similar embeddings. See the guide for more details.
Was this page helpful?