четверг, 8 апреля 2010 г.

Streaming h264 video from Cloudfront to Flash Player: how-to

Flash makes it easy to stream a video, at least in theory. With Amazon Cloudfront it's even easier - you just create a bucket, upload a video to it, and it's ready to be streamed via Flash Player.
Or it seems so.
There're some details which are documented poorly. I will try to fill this hole.

Brief algorithm

To stream a video (including h264) from Cloudfront, you need to do the following:
  1. Create an AWS account
  2. Create an S3 bucket for you videos
  3. Create a Cloudfront distribution for it
Then, for each file you wish to stream, you will have to do the following:
  1. Upload it to your S3 bucket as a public resource
  2. Stream it through Flash Player using correct URL
Let's go!

Configuring software

To work with S3 and Cloudfront you need some special software. For instance, there's an extension for Firefox called S3Fox. I personally prefer using s3cmd command line program to manage S3, I will provide examples for s3cmd.
Anyway, when registering in AWS, you will get a couple of pretty long and ugly strings. Those are Access Key and Secret Key. You have to configure your AWS-related software to use these keys for authentication. For s3cmd, the command is
s3cmd --configure

Creating an AWS account

To create an AWS (Amazon Web Services) account.

Creating an S3 bucket for your videos

First, you have to create an S3 (Simple Storage Service) bucket for your videos, it will feed them to your Cloudfront distributions.
s3cmd mb s3://your-bucket-name.you-site.com
Bucket name must be unique among all buckets, so it may seem reasonable to call your bucket based on your domain name.

Creating a Cloudfront distribution for your videos

A distribution must be bound to a bucket. Distribution will distribute any file with public access from a bucket bound to it.
Distribution may be created using mentioned software or using AWS Console web interface.
When creating, select 'Streaming' as type as we're going to stream video. Note that you can create several distributions for your bucket, so you may also have access to your bucket via HTTP if you wish. Of course, specify your bucket to which distribution will be bound.
Distribution takes some time to create. After it completes, you will get a public domain name for it, this will be domain name you will need to construct video URLs.

Uploading a video to bucket

When uploading a video to your bucket, make sure that you make it publicly readable.
s3cmd --acl-public put video.mp4 s3://your-bucket-name.your-site.com/video.mp4
Note that you don't need to make your whole bucket publicly readable, only files.

Constructing a URL

It's not a trivial task to construct a correct URL for streaming, because RTMP (and we are forced to use RTMP for streaming) has its own logic of constructing URLs. And there's still another catch with correct format prefix which you must use if you stream mp4 (or mp3). So I will provide some examples.
The common schema for Cloudfront is the following:
rtmp://distribution-domain-name/cfx/st/[type_prefix]stream_name
  • for flv, you don't need a type prefix, and you may supply extension name while it's recommented to omit it.
  • for mp4, you have to use type prefix 'mp4:' (with no quotes) and you must supply extension (mov, mp4, aac, f4v, m4v, m4a - any of these).
  • for mp3, you have to use type prefix 'mp3:'.
Let's assume you have some video and audio files stored as s3://your-bucket-name.your-site.com/video1.flv, s3://your-bucket-name.your-site.com/video2.mp4 and s3://your-bucket-name.your-site.com/audio.mp3. Assuming that your Cloudfront distribution domain name is abcdefghijklmn.cloudfront.net, you'll have to use the following URLs:
rtmp://abcdefghijklmn.cloudfront.net/cfx/st/video1
rtmp://abcdefghijklmn.cloudfront.net/cfx/st/mp4:video2.mp4
rtmp://abcdefghijklmn.cloudfront.net/cfx/st/mp3:audio.mp3

Links

http://livedocs.adobe.com/flex/3/langref/flash/net/NetStream.html#play() - here is more info on type prefixes

1 комментарий: