Search code examples
iosvideompmovieplayercontrolleramazon-cloudfronthttp-streaming

Private HTTP Live Streaming via CloudFront


I am working on an iOS app which allows downloading and HTTP live streaming of private videos. The videos are stored in an Amazon S3 bucket (as mp4 and segmented as m3u8/ts files). Also CloudFront is turned on and connected to the bucket.

Since the content is private, I need to sign the URLs when connecting via CloudFront. In order to sign the URLs it's necessary to use the private key and therefore it's not possible to generate signed URLs in the iOS app without storing the private key in the bundle. And that would be a bad idea!

So I decided to write a simple Ruby server, which performs the URL signing and redirects to the generated signed CloudFront URL as follows:


http://signing.server.local/videos/1.mp4https://acbdefg123456.cloudfront.net/videos/1.mp4??Expires=XXX&Signature=XXX&Key-Pair-Id=XXX

http://signing.server.local/videos/1.m3u8https://acbdefg123456.cloudfront.net/videos/1.m3u8??Expires=XXX&Signature=XXX&Key-Pair-Id=XXX


For video downloads it works well, since there is only one request. But when I want the content streamed and give the MPMoviePlayerController the URL of the signing server, only the first request is signed by the server and redirected to CloudFront. For the next requests the MPMoviePlayerController takes the first signed CloudFront URL as the base and tries to connect directly without going throw the signing server.

The paths in the m3u8 files are relative.

Any suggestions how to implement this feature without the need to send all the content through the signing server?


Solution

  • The correct way to do private HLS with S3/CloudFront or any other storage/CDN is to use HLS encryption. See the Apple documentation about this topic.

    In addition to the storage where your playlists and segmented video files are stored you have to integrate a secure HTTPS server for storing the top level playlists and keys. These keys are generated during the segmenting using the Apple HLS tools.

    Here is how it works:

    1. The MPMoviePlayerController gets an URL pointing to the top level playlist (.m3u8) on the secure HTTPS sever.
    2. In this file there are links to the variant playlists (prog_index.m3u8) which are stored in S3/CloudFront and which point to the video files (.ts).
    3. Additionally the variant playlists contain a link to the keys which are necessary in order to read the video files. These keys are stored on the secure HTTPS server as well.

    See the following image:

    Protecting HLS Keys

    Taken from the presentation Mobile Movies with HTTP LIve Streaming (CocoaConf DC, Jun '12)

    Of course there are possibilities to make the infrastructure more secure, see the linked Apple documentation.

    I also created a Ruby script for segmenting to produce the output with given base URLs, which makes things a lot simpler.