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

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

  1. Open the AWS Elemental MediaLive Console
  2. In the navigation pane, choose Inputs
  3. Choose Create input
  4. Enter Input name as slate
  5. Select MP4 as the Input type
  6. Select Single Input for Input class
  7. Enter Slate URL in Input Source A, URL field

 

MediaLive MP4 Input

 

Input class and source for MP4 MediaLive Input

 

Part 2 – Create a live input

  1. Choose Create input
  2. Enter Input name as rtmp_push
  3. Select RTMP(Push) as the Input type
  4. Select Single Input for Input class
  5. Enter application name as ingest against Destination A
  6. Enter channel1 as Application Instance against Destination A

 

RTMP Push MediaLive Input

 

Input class & destination configuration

 

Both the inputs are listed under Inputs section upon successful configuration.

 

Picture6 2

 

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

  1. Open the AWS Elemental MediaLive Console
  2. In the navigation pane, choose Channels
  3. Choose Create Channel
  4. In the Navigation Pane, under Input attachments, choose Add
  5. Select input slate created and Confirm
  6. In the Navigation Pane, under Input attachments, choose Add
  7. Select input rtmp-push created and Confirm

 

Attaching Slate Input

 

Attaching live Input

Upon attaching the slate and rtmp-push input successfully, both inputs are listed against a created channel.

 

Picture9

 

Step 3: Enable feature flag

To enable, add input prepare actions to the channel schedule:

  1. In the Navigation Pane, under Channel, select General settings
  2. Collapse Feature Activations Section
  3. Toggle ON Enable Feature Activations
  4. Select Enabled option for Input Prepare Schedule Actions

 

Input Prepare feature activation

 

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.

  1. Open the Lambda Console
  2. In the Navigation Pane select Functions.
  3. Select Create function button
  4. In Basic Information, enter Function name as “Live-Input-Prepare”
  5. Select Ruby 2.7 as a Runtime
  6. Select Create function button
  7. In the Code Tab, double-click “lambda_function.rb”
  8. Copy the following code snippet and replace the existing code displayed in the editor section.
  9. 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

Create Lambda function with basic information

 

Updating the lambda code for Live-Input-Prepare

 

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.

  1. Open the AWS Lambda Console
  2. In the Navigation Pane select Functions.
  3. Select Create function button
  4. In Basic Information, enter Function name as “Hotbackup-Monitor”
  5. Select Ruby 2.7 as a Runtime
  6. Select Create function button
  7. In the Code Tab, double-click “lambda_function.rb”
  8. Copy the following code snippet and replace the existing code displayed in the editor section.
  9. Select Deploy button to deploy the code.
  10. Configure two Environment variables
    1. Name PRIMARY_IP_NAME, value rtmp-push
    2. 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

 

Hotbackup-Monitor lambda basic configuration

 

Setting up Environmental Variables

 

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.

  1. Open the Amazon CloudWatch Console
  2. In the Navigation Pane Rules under Events section
  3. Select Create rule button
  4. Choose Event Pattern in Event Source Section
  5. In Service Name, select MediaLive
  6. Event Type select MediaLive channel state change
  7. In Targets section, select Add target*
  8. Select Live-Input-Prepare Lambda as a target.
  9. Select Configure details and provide a rule name of your choice.

 

Picture15 2

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.

  1. Open the Amazon CloudWatch Console
  2. Go to the Navigation Pane Rules under Events section
  3. Select Create rule button
  4. Choose Event Pattern in Event Source Section
  5. In Service Name, select MediaLive
  6. Event Type select MediaLive channel alert
  7. In Targets section, select Add target*
  8. Select Hotbackup-Monitor Lambda as a target.
  9. Select Configure details and provide a rule name of your choice.

 

Configuring Hotbackup-Monitor Lambda for MediaLive Channel alert

 

Step 8: Start the MediaLive Channel

  1. Open the AWS Elemental MediaLive Console
  2. In the navigation pane, under AWS Elemental MediaLive, choose Channels
  3. Select the channel you created hotbkp-channel and select Start button

 

Picture17 1

 

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.

PictureA

 

PictureB

 

PictureC

 

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.

 

PictureD

 

PictureE

 

PictureF

 

PictureG

 

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.

Categories: Media