Live program replacement
This example demonstrates how to use an EdgeWorkers function to dynamically replace a live stream with a blackout slate during a specific time period and within specific geolocations. With live sporting events it’s sometimes necessary to blackout a channel due to regional rights or league specific rules.
Before you begin
We recommend that you select the Dynamic Compute resource tier when creating the EdgeWorker ID for this tutorial. Dynamic Compute provides higher consumption limits that may be necessary to perform program replacement.
👍 The complete code sample for this example is available in the GitHub repo.
📘 This example is only applicable the HLS Parser module. Live program replacement for DASH manifests is not yet supported.
About blackout slates
For more detailed information about blackout slates refer to the HLS parser section in this guide. A blackout slate has its own child playlists and segments that adhere to the parent stream’s encoding profile such as bitrates, resolutions, and segment duration as the original content. There should be one policy per live stream that contains a list of blackout events coded as tuples such as geo, interval, and url.
- The origin needs to contain the blackout slate playlists.
- The blackout child playlists must be less than 128 KB.
- A live media playlist should contain at least one
EXT-X-PROGRAM-DATE-TIMEtag for the first sample of a
Media Segment with an absolute date and time.
EdgeKV support
EdgeKV is not currently supported. Development efforts are underway to let you use an EdgeKV database to:
- Store policies that contain lists of blackout events.
- Filter the applicable policies based on User Location Object properties.
- Use the stream URL as the key in the EdgeKV data model.
📘 EdgeWorkers is supported on both the Enhanced TLS and Standard TLS delivery methods.
1. Import the HLS parser
-
To configure live program replacement, import the HLS parser module into your
main.jsfile.Refer to the instructions in Import a JavaScript module for more information.
The HLS parser module includes the
LiveManifestTransformerhelper class. You can use it to insert data such as start-time, end-time, and alternative content or a URL.
2. Specify the blackout slate
- Specify the details of the blackout slate using these parameters.
- startDate in ISO 8601 format, representing the start date of the replacement window.
- endDate in ISO 8601 format, representing the end date of the replacement window.
- content is the blackout playlist that is used to replace the media segment URI. If a URL is passed, the HLS parser makes a sub-request to fetch the blackout slate content.
- Additionally, you can also specify a geolocation using the User Location properties. The media playlist request from the player contains the User Location properties. You can configure the EdgeWorkers function to filter the applicable policy data based on a user’s location.
📘 All media segments that occur during the specified time replacement window are replaced with blackout segments.
- The EdgeWorkers function makes a sub-request for the original media playlist and on the response does the following:
- Gets complete original media and converts bytes to UTF-8.
- Invokes the necessary helper functions for the
LiveManifestTransformerclass to perform blackout slate replacements. - Checks for any applicable blackout intervals at
EXT-X-PROGRAM-DATE-TIME. For example, policy start-time<= EXT-X-PROGRAM-DATE-TIME <= policy end-time. If so, replaces the original content segment with a content segment from the policy. - Repeats the segment replacement process until the
EXT-X-PROGRAM-DATE-TIMEfor the original content segment is not in range of the start-time and end-time.
-
The above logic executes whenever the player reloads a live media playlist.
-
This code sample demonstrates the usage of the
LiveManifestTransformerclass from the HLS parser.
📘 In the below example, the video and audio policies are hardcoded. You can, however, load the policies from Property Manager using a user defined variable.
Refer Request.getVariable() to learn how to read the value of a Property Manager user-defined variable in your EdgeWorkers code.