Building a Video Streaming Backend For iOS using HTTP Live Streaming

Recently for a project I was working on, I needed to set up a video streaming backend for an iOS app. After digging around and seeing what the potential solutions for this were, I decided to use Amazon AWS as the backend. Next, in terms of the format that I should use, it seemed that http live streaming was the best option. Since it took a bit of time to organize and layout how this system worked, I figured it would be nice to write what I found.


Steps to Building your own Streaming Backend 

  1. Save an original video file to disk that you want to stream. This will be typically be some quicktime format, mp4, etc.
  2. Log onto your Amazon AWS account (or make one) (http://aws.amazon.com/) and create 3 s3 buckets. One will be for the original video files, one is for a thumbnail preview of each video, and one will be for the actual streaming video files.
  3. Either through code or the amazon web interface, upload that file to the s3 bucket made to store all video files.
  4. Now, to convert these videos to a streaming format, we will need to do several things. First, I think it is probably a good idea to go over how http streaming actually works. so:
    1. HTTP Live Streaming
      1. Basically an extension of audio (mp3) streaming
      2. File format is M3U8.
      3. This looks something like:

        #EXT-X-VERSION:3 #EXTM3U #EXT-X-TARGETDURATION:10 #EXT-X-MEDIA-SEQUENCE:1 # Old-style integer duration; avoid for newer clients. #EXTINF:10, http://media.example.com/segment0.ts # New-style floating-point duration; use for modern clients. #EXTINF:10.0, http://media.example.com/segment1.ts #EXTINF:9.5, http://media.example.com/segment2.ts #EXT-X-ENDLIST


      4. Essentially, it is a list of a ton of smaller .ts files. Apple recommends these to be about 10 seconds of video each. So, we need to convert our large video file into a ton of small .ts file clips, and then generate a M3U8 file that describes all the clips we need to make up our original video and therefore let the video actually stream.
  5. So, one bucket will hole these .m38u files (and all the corresponding .ts files) and then that file url is directly playable by various UI elements of iOS.
  6. To convert from .mov files or any original format to playable .m38u file, you will need to use the Amazon Elastic Transcoder.
    1. Open up the Elastic Transcoder on Amazon AWS console.
    2. Create a new Pipeline and hook up input and output buckets
    3. Create a new Transcoding Job.
      1. Input key:  filename of your desire
      2. output key prefix: folder you want your output in i.e. ‘myStreamingVids/’
      3. I used a system preset of HLS 2m
      4. Use a segment duration of 10
      5. output key: whatever you want
      6. Set master playlist name to final video url stream name
      7. set playlist format to be hlsv3
      8. set ouputs in master playlist to be your output key.
      9. Create job. 
    4. This will kickoff and run this job.
  7. Use Amazon AWS Lambda
    1. Set up notifications to be called whenever a new video is uploaded to s3 input videos bucket. (I have it called on all new uploads)
    2. This calls your Lambda function. (its a .js file)
    3. My JS file extracts file info and then creates an Elastic Transcoder Job and adds it to my pipeline.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s