python-samples-for-amazon-kinesis-video-streams-with-webrtc

The python-samples-for-amazon-kinesis-video-streams-with-webrtc repository provides a Python-based solution for transmitting media using Amazon Kinesis Video Streams (KVS) with WebRTC. It addresses the challenge of the lack of a dedicated Python library for KVS WebRTC by utilizing the WebRTC standard. This implementation demonstrates how to leverage WebRTC for real-time communication, enabling secure, low-latency media streaming and two-way interaction between IoT devices and WebRTC-compliant web or mobile applications.

Table of Contents

Getting started

Clone the repo and install python dependencies

Clone the repo:

git clone https://github.com/aws-samples/python-samples-for-amazon-kinesis-video-streams-with-webrtc.git
cd python-samples-for-amazon-kinesis-video-streams-with-webrtc

To manually create a virtualenv on macOS and Linux:

python3 -m venv .venv

After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.

source .venv/bin/activate

Once the virtualenv is activated, you can install the required dependencies.

Note

One of the dependencies is YOLOv8 and it is distributed under the GPLv3 license.

pip install -r requirements.txt

Install GStreamer and Dependencies (optional)

Install this if you are going to test GStreamer samples.

Ubuntu

sudo apt-get install pkg-config libssl-dev libcurl4-openssl-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev 
sudo apt install gstreamer1.0-tools gstreamer1.0-plugins-base-apps gstreamer1.0-plugins-good gstreamer1.0-plugins-bad gstreamer1.0-plugins-ugly gstreamer1.0-libav
sudo apt-get install libcairo2-dev libgirepository1.0-dev gir1.2-glib-2.0

find /usr -name gst-plugin-scanner
export GST_PLUGIN_SCANNER=/usr/lib/x86_64-linux-gnu/gstreamer1.0/gstreamer-1.0/gst-plugin-scanner

macOS

brew install gstreamer

Set credentials

Option 1: Temporary credentials using AWS CLI

You will use the AWS CLI to get temporary credentials for AWS authentication. If you have not already set up the AWS CLI on the PC for operation, follow the steps: Installing the AWS CLI and Configuring the AWS CLI.

  1. In the terminal of your device or local terminal, execute the following:
python script/getTemporaryCredentialsAWSCli.py

output

This is the temporary credential valid for 43199 seconds.
Paste them in your shell!

export AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXX7
export AWS_SECRET_ACCESS_KEY=fXXXXXXXXXXXXXXXX4
export AWS_SESSION_TOKEN=FXXXXXXXXXXXXXXXX0
export AWS_DEFAULT_REGION=usXXXX2
  1. Execute the copied output to the terminal of the machine where you want to
export AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXX7
export AWS_SECRET_ACCESS_KEY=fXXXXXXXXXXXXXXXX4
export AWS_SESSION_TOKEN=FXXXXXXXXXXXXXXXX0
export AWS_DEFAULT_REGION=usXXXX2

Option 2: Temporary credentials using CloudShell

  1. login to the AWS Console

  2. Make sure that you are in the AWS region that you want to deploy your resources to

  3. Start CloudShell

  4. Upload getTemporaryCredentialsCloudShell.py to CloudShell

  5. Run getTemporaryCredentialsCloudShell.py retrieve temporary credentials from AWS_CONTAINER_CREDENTIALS_FULL_URI

    In your CloudShell

    python getTemporaryCredentialsCloudShell.py

    output

    This is the temporary credential valid for 43199 seconds.
    Paste them in your shell!
    
    export AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXX7
    export AWS_SECRET_ACCESS_KEY=fXXXXXXXXXXXXXXXX4
    export AWS_SESSION_TOKEN=FXXXXXXXXXXXXXXXX0
    export AWS_DEFAULT_REGION=usXXXX2
  6. Execute the copied output to the terminal of the machine where you want to

    In your device or local terminal

    export AWS_ACCESS_KEY_ID=AXXXXXXXXXXXXX7
    export AWS_SECRET_ACCESS_KEY=fXXXXXXXXXXXXXXXX4
    export AWS_SESSION_TOKEN=FXXXXXXXXXXXXXXXX0
    export AWS_DEFAULT_REGION=usXXXX2

Option 3: IoT Device Certificate

Note

CDK to deploy all the necessary resources including signaling_channel is in infra folder. We will be using CloudShell to deploy it.

  1. Navigate to python-samples-for-amazon-kinesis-video-streams-with-webrtc folder

  2. zip infra folder

    zip -r infra.zip infra
  3. Login to the AWS Console

  4. Make sure that you are in the AWS region that you want to deploy your resources to

  5. Start CloudShell

  6. Upload infra.zip to CloudShell

  7. unzip infra.zip

    In your CloudShell

    unzip infra.zip
  8. Go to infra folder

    cd infra
  9. Update CDK

    sudo npm install -g aws-cdk
  10. Create a virtualenv:

    python -m venv .venv
  11. activate your virtualenv.

    source .venv/bin/activate
  12. Install the required dependencies.

    pip install -r requirements.txt
  13. Bootstrap environment with the AWS CDK (One Time)

    cdk bootstrap
  14. You can now synthesize the CloudFormation template for this code.

    cdk synth
  15. Deploy CDK

    cdk deploy
  16. Run write_certs.py to download IoT device Certificates to the script-output folder.

    python write_certs.py
  17. Verify the files in script-output folder

    ls -a script-output

    output

    .  ..  device.cert.pem  device.private.key  .env  rootca.pem
  18. zip script-output folder

    zip -r script-output.zip script-output
  19. Download script-output.zip to local infra folder

    • Individual file path: infra/script-output.zip
  20. unzip script-output.zip in infra folder In your device or local terminal

    cd infra
    unzip script-output.zip
    ls -a script-output

    output

    .   ..   .env   device.cert.pem    device.private.key    rootca.pem

Configure Kinesis Video Streams WebRTC Signaling Channel

This step creates a signaling channel for the WebRTC Master and Viewer.

  1. login to the AWS Console
  2. Make sure that you are in the AWS region that you want to deploy your resources to
  3. Start CloudShell
  4. If a channel has already been created, check the ARN value for that channel by running the following command.
    aws kinesisvideo describe-signaling-channel --channel-name kvs-demo-channel --region AWS_DEFAULT_REGION
  5. Create a signaling channel for the WebRTC Master and Viewer.
    aws kinesisvideo create-signaling-channel --channel-name kvs-demo-channel --region AWS_DEFAULT_REGION

We will use the channel ARN information returned by executing the above command in the next step.

Samples

Directory File Description
simple kvsWebRTCClientMaster.py Master client for KVS WebRTC: Manages media tracks and signaling, supporting various OS inputs via aiortc.
simple kvsWebRTCClientViewer.py Viewer client for KVS WebRTC: Handles media tracks and signaling, compatible with multiple OS through aiortc.
simple kvsWebrtcClientMasterGst.py Master client for KVS WebRTC: Utilizes GStreamer for advanced media processing and conversion.
ml/yolov8 app-master-infer-self.py Master client for KVS WebRTC: Offers optional YOLO object detection for real-time processing.

kvsWebRTCClientMaster

Use video file as a media source

python source/simple/kvsWebRTCClientMaster.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number] --file-path [your-video-file]

Use camera as a media source

python source/simple/kvsWebRTCClientMaster.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number]

kvsWebRTCClientViewer

Use video file as a media source

python source/simple/kvsWebRTCClientViewer.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number] --file-path [your-video-file]

Use camera as a media source

python source/simple/kvsWebRTCClientViewer.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number]

kvsWebrtcClientMasterGst

python source/simple/kvsWebrtcClientMasterGst.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number] 

app-master-infer-self

Use video file as a media source

python source/ml/yolov8/app-master-infer-self.py ---channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number] --file-path [your-video-file] --mode=yolo

Use camera as a media source

python source/ml/yolov8/app-master-infer-self.py --channel-arn arn:aws:kinesisvideo:[region]:[account-number]:channel/[channel-name]/[number] --mode=yolo

Testing WebRTC with Kinesis Video Streams

To experiment with the WebRTC aspects of this project, you can use the Kinesis Video Streams WebRTC Test Page. This page is built using the WebRTC JS SDK and allows you to:

  • Act as both a master and a viewer
  • Experiment with different ICE Candidate configurations
  • Enable/disable Trickle ICE
  • Enforce TURN usage
  • Enforce specific ICE candidates

Setup Instructions

  1. Navigate to the KVS WebRTC Test Page

  2. Enter the following details:

    • Region: [REGION_NAME] (where your signaling channel is created)
    • Access Key ID: [AWS_ACCESS_KEY_ID]
    • Secret Access Key: [AWS_SECRET_ACCESS_KEY]
    • Session Token: [AWS_SESSION_TOKEN]
    • Channel Name: kvs-demo-channel (must match the signaling channel used by the master or viewer)

Viewing Results

Testing the Master Script
  1. Run the kvsWebRTCClientMaster.py script.
  2. Start a viewer on the Kinesis Video Streams WebRTC Test Page.
Testing the Viewer Script
  1. Start a master on the Kinesis Video Streams WebRTC Test Page.

    • Note: Always start the master before the viewer.
  2. Run the kvsWebRTCClientViewer.py script.

    These steps allow you to test both the master and viewer functionalities of your WebRTC application using the Kinesis Video Streams test page and your Python scripts.

Architecture Diagram

Simple

yolo8

Disclaimers

This demo shows that Amazon Kinesis Video Streams with WebRTC can be implemented in Python. This demo does not officially represent the Python SDK, nor is it officially supported by AWS. AWS is not responsible for issues that may occur when using this demo in a production environment.