AWS for M&E Blog
How to: Automatic failover of file and live inputs in AWS Elemental MediaLive
It’s imperative in a live video streaming workflow to have a video slate (MP4) as a backup for a live input (for example RTMP PUSH). This ensures video continuity by switching automatically to a video slate (MP4) when there is no active live input, and switching back to the live input when live ingest is active again, without human intervention.
This how to post takes you through the steps required to build an automatic failover mechanism between live and mp4 file inputs in an AWS Elemental MediaLive Channel.
Schematic view on automatic failover architecture
Step-by-step workflow build
The following are the steps covered in this post.
Step 1 – Create two MediaLive inputs
Step 2 – Create channel and attach inputs
Step 3 – Enabling feature flag
Step 4 – Create “Live-Input-Prepare” Lambda
Step 5 – Create “Hotbackup-Monitor” Lambda
Step 6 – Configure CloudWatch rule for “MediaLive channel state change”
Step 7 – Configure CloudWatch rule for “MediaLive Channel Alert”
Step 8 – Start the MediaLive Channel
Step 9 – ON/OFF RTMP push ingest
Step 10 – Clean up
Step 1: Create two MediaLive inputs
Part 1 – Create MP4 input
- Open the AWS Elemental MediaLive Console
- In the navigation pane, choose Inputs
- Choose Create input
- Enter Input name as slate
- Select MP4 as the Input type
- Select Single Input for Input class
- Enter Slate URL in Input Source A, URL field
Part 2 – Create a live input
- Choose Create input
- Enter Input name as rtmp_push
- Select RTMP(Push) as the Input type
- Select Single Input for Input class
- Enter application name as ingest against Destination A
- Enter channel1 as Application Instance against Destination A
Both the inputs are listed under Inputs section upon successful configuration.
The workflow can be extended to cover dual pipeline inputs by including a simple health correlation flag tracking both the pipelines to make a decision on the switch over. However for this post we are focusing on the single pipeline input.
Step 2: Create a channel and attach inputs
- Open the AWS Elemental MediaLive Console
- In the navigation pane, choose Channels
- Choose Create Channel
- In the Navigation Pane, under Input attachments, choose Add
- Select input slate created and Confirm
- In the Navigation Pane, under Input attachments, choose Add
- Select input rtmp-push created and Confirm
Upon attaching the slate and rtmp-push input successfully, both inputs are listed against a created channel.
Step 3: Enable feature flag
To enable, add input prepare actions to the channel schedule:
- In the Navigation Pane, under Channel, select General settings
- Collapse Feature Activations Section
- Toggle ON Enable Feature Activations
- Select Enabled option for Input Prepare Schedule Actions
Note: Create a channel with name “hotbkup-channel” with all your other preferred configurations.
Step 4: Create a “Live-Input-Prepare” Lambda
The AWS Lambda function named “Live-Input-Prepare” is created to listen for the Channel state change and applies the preparation schedule action on the Live input (“rtmp-push”) once the channel is started. The input preparation action initiates the monitoring process on the live input despite there not being an active input currently on the channel.
- Open the Lambda Console
- In the Navigation Pane select Functions.
- Select Create function button
- In Basic Information, enter Function name as “Live-Input-Prepare”
- Select Ruby 2.7 as a Runtime
- Select Create function button
- In the Code Tab, double-click “lambda_function.rb”
- Copy the following code snippet and replace the existing code displayed in the editor section.
- Select Deploy
Code snippet for “Live-Input-Prepare” Lambda (Ruby source)
require 'json'
require 'aws-sdk-medialive'
def lambda_handler( event:, context: )
puts "Event => #{event}"
ML_STATE_CHANGE_EVENT = "MediaLive Channel State Change"
if event["detail-type"] == ML_STATE_CHANGE_EVENT
state = event["detail"]["state"]
channel_arn = event["detail"]["channel_arn"]
channel_id = channel_arn.split(":").last
puts "channel ID : #{channel_id} State : #{state}"
if state == "RUNNING"
#Hard coding Live input name , however you can also describe the channel and initiate the prepare for the live inputs associated.
create_input_prepare_schedule_action("rtmp_push")
end
end
end
def create_input_prepare_schedule_action( channel_id, input_name )
ip_prepare_payload = {
channel_id: "#{channel_id}",
creates: {
schedule_actions: [
{
action_name: "#{Time.now.to_i}",
schedule_action_start_settings: {
immediate_mode_schedule_action_start_settings: {}
},
schedule_action_settings: {
input_prepare_settings: {
input_attachment_name_reference: "#{input_name}"
}
}
}
]
}
}
#Initialising MediaLive Client
medialiveclient = Aws::MediaLive::Client.new
medialiveclient.batch_update_schedule(ip_prepare_payload)
end
Note: Make sure that the Lambda has permission for the MediaLive Service. If not, please attach the managed policy (AWSElementalMediaLiveFullAccess) against the role associated with Lambda.
Step 5: Create the “Hotbackup-Monitor” Lambda
The AWS Lambda function named “Hotbackup-Monitor” is created to listen for channel alerts, and toggles between Live RTMP Push Input to the MP4 File input (or vice versa) depending on the nature of the alert it received.
- Open the AWS Lambda Console
- In the Navigation Pane select Functions.
- Select Create function button
- In Basic Information, enter Function name as “Hotbackup-Monitor”
- Select Ruby 2.7 as a Runtime
- Select Create function button
- In the Code Tab, double-click “lambda_function.rb”
- Copy the following code snippet and replace the existing code displayed in the editor section.
- Select Deploy button to deploy the code.
- Configure two Environment variables
- Name PRIMARY_IP_NAME, value rtmp-push
- Name SECONDARY_IP_NAME, Value slate
Code Snippet for “Hotbackup-Monitor” Lambda (Ruby Source)
require 'json'
require 'aws-sdk-medialive'
LIVE_ALERT_TYPE = "RTMP Has No Audio/Video"
ALERT_STATE_ON = "SET"
ALERT_STATE_OFF = "CLEARED"
ML_CHANNEL_ALERT_EVENT = "MediaLive Channel Alert"
def lambda_handler(event:, context:)
if event["detail-type"] == ML_CHANNEL_ALERT_EVENT && event["detail"]["alert_type"] == LIVE_ALERT_TYPE
alert_state = event["detail"]["alarm_state"]
channel_arn = event["detail"]["channel_arn"]
channel_id = channel_arn.split(":").last
primary_ip = ENV["PRIMARY_IP_NAME"] rescue nil
secondary_ip = ENV["SECONDARY_IP_NAME"] rescue nil
if primary_ip.nil? || secondary_ip.nil?
puts "Either Primary or secondary input is not configured; Hence no hot backup monitoring"
return
end
if alert_state == ALERT_STATE_ON
create_input_switch_schedule_action(channel_id, secondary_ip)
elsif alert_state == ALERT_STATE_OFF
create_input_switch_schedule_action(channel_id, primary_ip)
end
end
end
def create_input_switch_schedule_action( channel_id, input_name )
ip_switch_payload = {
channel_id: "#{channel_id}",
creates: {
schedule_actions: [
{
action_name: "#{Time.now.to_i}",
schedule_action_start_settings: {
immediate_mode_schedule_action_start_settings: {}
},
schedule_action_settings: {
input_switch_settings: {
input_attachment_name_reference: "#{input_name}"
}
}
}
]
}
}
medialiveclient = Aws::MediaLive::Client.new
medialiveclient.batch_update_schedule(ip_switch_payload)
end
Note: Make sure that the Lambda has permission for the MediaLive Service. If not, please attach “AWSElementalMediaLiveFullAccess” managed policy against the role associated with Lambda.
Step 6: Configure a CloudWatch rule for “MediaLive channel state change”
Configure a CloudWatch rule for “MediaLive channel state change” event against MediaLive, associating the “Live-Input-Prepare” Lambda.
- Open the Amazon CloudWatch Console
- In the Navigation Pane Rules under Events section
- Select Create rule button
- Choose Event Pattern in Event Source Section
- In Service Name, select MediaLive
- Event Type select MediaLive channel state change
- In Targets section, select Add target*
- Select Live-Input-Prepare Lambda as a target.
- Select Configure details and provide a rule name of your choice.
Step 7: Configure a CloudWatch rule for “MediaLive Channel Alert”
This step associates the Hotbackup-Monitor Lambda with the “MediaLive Channel alert” to monitor the alerts for the MediaLive channel.
- Open the Amazon CloudWatch Console
- Go to the Navigation Pane Rules under Events section
- Select Create rule button
- Choose Event Pattern in Event Source Section
- In Service Name, select MediaLive
- Event Type select MediaLive channel alert
- In Targets section, select Add target*
- Select Hotbackup-Monitor Lambda as a target.
- Select Configure details and provide a rule name of your choice.
Step 8: Start the MediaLive Channel
- Open the AWS Elemental MediaLive Console
- In the navigation pane, under AWS Elemental MediaLive, choose Channels
- Select the channel you created hotbkp-channel and select Start button
Step 9: ON/OFF RTMP push ingest
When the channel is started, there is an Input prepare schedule action against the Live input, which generates an alert indicating NO RTMP ingest, triggering the input switch to the “slate.” The following are the snapshots of schedule, alert, and active pipeline details while there is no RTMP ingest onto the MediaLive channel.
When RTMP ingest is initiated on the rtp-push input the alert is cleared, triggering the input switch to “rtmp-push” input. The following are the snapshots of alerts, schedule actions, and active pipeline details.
Step 10: Clean up
Remember to stop the MediaLive streaming channel when you are not using it. This stops the channel from incurring costs when inactive. To delete the channel permanently, stop and delete the MediaLive channel and its associated inputs.
Learn more
AWS provides a growing number of services designed to assist in the building of media-related workflows. If you would like to explore additional applications for video streaming, processing, and delivery using AWS Services, visit Media Services on AWS.