AWS for M&E Blog

Learn about IoT integration and server-side ad insertion with AWS Media Services

Overview
This blog helps Raspberry Pi enthusiasts who want to monetize exclusive, but simple-to-set-up, IoT video feeds using server-side ad insertion (SSAI) powered by AWS Media Services from Amazon Web Services (AWS).

For example, an exotic dog breed kennel club that nurtures rare and magnificent dog species may choose to showcase its residents on a private live feed for emotional therapy purposes to an audience that finds comfort in watching cute, furry dogs hanging out together and going about their day. To support a private feed via the cloud, the kennel club needs to monetize live streams delivered to thousands of concurrent viewers by stitching in 5-6 short ads every hour. Interested dog lovers are given the URL of the live feed and can watch it from anywhere with a good Wi-Fi connection on their phones, tablets, and laptops.

Architecture

This image shows an end-to-end integration between RasPi and AWS Media Services for server side ad insertion

Deploy AWS Media Services resources and Amazon CloudFront distribution using a CDK app 

You will use AWS CDK to deploy the following resources in your AWS account:

1. AWS Elemental MediaLive channel for live transcoding

2. AWS Elemental MediaPackage channel for live packaging and origination

3. AWS Elemental MediaTailor configuration for server-side ad insertion

4. Amazon CloudFront distribution as the CDN

5. AWS Lambda function to insert ad markers using splice insert

Follow the instructions on how to deploy the CDK app from here.

Trigger Lambda function using Amazon CloudWatch Events

 With the Lambda function defined, you will schedule it to get triggered at a regular interval, say every 1 minute so that we can easily test this with MediaTailor later.

  1. From the AWS console, navigate to CloudWatch.
  2. Under Events, click on Rules. Click on Create Rule
  3. Under Event Source, choose Schedule.
  4. In Fixed rate of, edit to say 1 minute.
  5. Under Targets, click on Add Target.
  6. In the Functiondropdown menu, select your InsertAdMarker function (created as part of the CDK app deployment).

Image shows rule configuration

7. Click on Configure details

8. Provide a name like InsertAdMarkerEveryMinute. Make sure to leave the Stateto Enabled. Otherwise, this rule will not run. Click on Create Rule

9. With the event enabled, navigate to the MediaLive console, select Channels and choose your MediaLive channel that was created by the CDK app. Select the “Schedule” tab, and you should see schedule ad break opportunities every minute.

Image shows the schedule actions

Create Amazon S3 bucket and upload ad assets

Create your own Amazon S3 bucket, turn on blocking all public access, and upload ad assets in mp4 format with a 10 second duration.

Create and upload a static VAST.xml to the S3 bucket as an ad serving template

In order to insert ads into your live stream, you can use an Ad Decision Server (ADS) to specify what ads to play for each viewer. For the purposes of this demonstration, we use a static .xml file to simulate an ADS.

Create and upload your own copy of the following VAST.xml template to your S3 bucket. You can use the following sample xml and replace the CDATA with a link to your own ad content. In this example, the sample ad video is 10 seconds in duration; you can change this value based on the duration of your ad video.

<VAST version=”3.0″>
<Ad>
<InLine>
<AdSystem>2.0</AdSystem>
<AdTitle>ad-1</AdTitle>
<Impression/>
<Creatives>
<Creative>
<Linear>

<MediaFiles>
<MediaFile delivery=”progressive” type=”video/mp4″
width=”1920″ height=”1080″>
<![CDATA[https://yoururl.mp4]]>
</MediaFile>
</MediaFiles>
</Linear>
</Creative>
</Creatives>
</InLine>
</Ad>
</VAST>

Associate a CloudFront distribution with the MediaTailor HLS endpoint as the origin to get a CDN playback URL

Configuring the MediaTailor advanced settings with CloudFront distribution’s content segment and ad segment prefix

You will use the CloudFront distribution created by the CDK app to act as the CDN for your MediaTailor HLS endpoint. Edit your MediaTailor configuration to configure Advanced settings and supply your . For that, navigate to the CloudFront console, and from the Details tab, copy the CloudFront domain name and append the relevant origin path pattern for e.g. add “/out/v1/*” to your CloudFront domain name to present the complete content segment prefix and for the ad-segment prefix simply use the CloudFront domain name as is.

The content segment prefix should look like this: https://dxyz123abc.cloudfront.net/out/v1/. Whereas, the ad segment prefix should simply be https://dxyz123abc.cloudfront.net.

Configuring Raspberry Pi

Download and install the latest version of the Raspberry Pi imager utility on your desktophttps://www.raspberrypi.com/software/

Format a micro SD card with the Raspberry Pi OS using the imager utility

Formatting a micro SD card with the Raspberry Pi OS using the imager utility

Raspberry Pi computers use a micro SD card, except for very early models, which use a full-sized SD card. We recommend using an SD card of 8GB or greater capacity with Raspberry Pi OS. If you are using the lite version of Raspberry Pi OS, you can use a 4GB card. After the Raspberry Pi Imager Utility is installed, follow these steps:

  • Connect an SD card reader with the SD card inside.
  • Open Raspberry Pi Imager and choose the Raspberry Pi OS from the list presented.

Operating system selection

  • Choose the SD card you wish to write your image to.

Choose the SD card to write the image

  • Review your selections and click on the Write button to begin writing data to the SD Card.

Image shows the review page of OS and SD card

Connecting Pi Cam, keyboard, mouse, and monitor to the Raspberry Pi

All current models of Raspberry Pi have a port for connecting the Camera Module. Make sure your Raspberry Pi is turned off. Locate the Camera Module port, gently pull up on the edges of the port’s plastic clip. Insert the camera module ribbon cable and push the plastic clip into place.

Raspberry Pi 3B+ with Camera Module port labelled

Your Raspberry Pi has two micro HDMI connectors that you will use to connect directly to a monitor using a micro HDMI to standard HDMI (A/M) cable. Your Raspberry Pi also has four USB Ports. You will use 2 of them to connect directly to a wired mouse and keyboard.

Image shows how Raspberry Pi hardware looks like

Booting the Raspberry Pi

You will now insert the SD card into your Raspberry Pi and power it up using a type C power supply cable. When your Raspberry Pi boots for the first time, a configuration wizard will run that allows you to set up your Raspberry Pi.

Configure wizard

The wizard starts off by allowing you to configure international settings and your time zone information.

International settings

After choosing “Next”, you’ll be prompted to create a user account. Here, you can choose your username and a password.

Username and password configuration

After creating a user account, you can configure your screen and your wireless network. Once that is configured and your Raspberry Pi has access to the internet, you will be prompted to update the operating system to the latest version. This will automatically download any patches and updates needed to bring your new operating system up to date. Once the operating system is updated, you will be prompted to reboot your Raspberry Pi.

10

Enabling camera interface

Before using any of the Raspicam applications, the camera must be enabled. Go to the main menu and open the Raspberry Pi Configuration tool.

Raspberry Pi Configuration Tool

Select the Interfaces tab and ensure that the camera is enabled.

Camera enabled

Installing FFMPEG from the Raspberry Pi terminal

$ git clone —depth=1 git://source.ffmpeg.org/ffmpeg.git
cd ffmpeg
$./configure --enable-gpl --enable-libx264 --enable-nonfree --
enable-mmal --enable-libfdk-aac --prefix=/usr --extra-ldflags="-
latomic" --enable-openssl
$ make -j4
wait ~90min

$ sudo make install
$ ldconfig

Now, you can start the MediaLive channel you created in the earlier step by navigating to MediaLive on the AWS console, selecting your channel, and clicking on the Start button on the top.

Streaming to a Medialive input RTMP endpoint

You will find the IP address, port number, and stream key from the MediaLive console by navigating to the “Inputs” tab to supply the following CLI command.

$ ffmpeg -f video4linux2 -input_format h264 -s 1280x720 -.
framerate 30 -i /dev/video0 -vcodec copy -f flv rtmp://<IP 
adress>:<Port Number>/<Stream Key>

This command will start streaming from your Pi Cam to your MediaLive channel.

Verifying successful playback of the live stream and SSAI using an HLS web player

Now, your final playback URL can be derived by replacing the domain name in the MediaTailor HLS playback prefix with the CloudFront domain name and appending index.m3u8 as the suffix for the parent manifest file.

https://dxyzabc123.cloudfront.net/v1/master/<MediaTailorConfigUUID>/<MediaTailorConfigurationName>/index.m3u8

To test playback of the virtual linear OTT channel, go to https://www.hlsplayer.net/, introduce the playback URL, and press “Apply”.

Using hls.js as the test HLS player by supplying the playback URL as the input to the web player
Output of the playback test

If the channel does not have programming available at playback, it is expected that the configured slate video will display, which confirms you have  successfully achieved server-side ad insertion with your video stream.

Testing the successful playback of ad break with the HLS web player

Conclusion:

In this blog post, we configured an open-source edge computing device like Raspberry Pi to stream a live feed over RTMP to AWS, perform live transcoding/packaging of this stream, insert server-side ads, and play back the stream from a mobile/web player.

Now that you have seen how to stream video from an IoT device and insert ads, some additional challenges you can try include performing dynamic ad insertion using an ADS such as Freewheel, performing server-side reporting on the ad impressions, and visualizing the tracked beacon data using Grafana.

 
       
Devendra Sharma

Devendra Sharma

Devendra Sharma is a Solutions Architect at AWS with a passion for delivering cloud based live video transcoding and server side ad insertion solutions. He has more than 7 years of experience working with the Media & Entertainment Direct-To-Consumer vertical. In his past time, he likes to integrate open source IoT devices with low latency video streaming web applications and evangelize the benefits of creating and distributing creative content over AWS Cloud

Grace Kunjumon

Grace Kunjumon

Grace Kunjumon is an Associate Solutions Architect and part of the Canadian Public Sector team at AWS. She holds six AWS certifications, and her technical field of competency is media and entertainment. Prior to joining AWS, Grace was working as a senior broadcast technologist at a TV broadcaster.