Archive for iphone streaming

How to http stream to iphone/ipad with dynamic bitrate

I’ve been working on setting up dynamic streaming for the iphone. Since the iphone not only support normal h.264 encoded video streaming but also dynamic streaming based on available bandwidth I thought I set it up in four profiles.

I started with a very low profile (It’s a requirement if you want to submit a streaming app for the App Store), this profile is audio only but you can add a image that’s showing when you play. My low profile is set at 32kbit/s audio, I used the Advanced Audio Codec (AAC).

My other three profiles are 192kbit/s, 600kbit/s and 1500kbit/s. The 1500kbit version will probably come handy if you have Wifi or really good 3G connections. And the video looks really good on the Ipad. These profiles are all coded with Advanced Video Codec (AVC). I use 480×272 in resolutions on the two lower versions and 720×400 in the high one.

When you choose resolution for encoding you want the resolution should be possible to devide with 4, 8 or 16. The higher the better the video will look. I would have choosen 408 intead of 400 (on the higher bitrate version) but I followed a specification from a another person so I had to adapt. Adobe have given out a great tool for this called the bitrate calculator. Their calculator is for flash video (FLV/F4V) but works the same on other formats. If you follow that your web video will look even better.

To stream material to mobile phones you should wrap your video in the 3GP container. This container works on almost all phones. Newer phones like the iphone or android phones can also play mp4 files (h.264). But I prefere to encode all mobile material in H.264 and wrap it in the 3GP container.

We then need to use a couple of tools that Apple have created. Media Stream Segmenter (This is used if you stream live content and not VOD content), File Stream Segmenter, Media Stream Validator and Varient Playlist Creator. All these tools are included with Mac OS X, version 10.6 (Snow Leopard) and later. They can be found in the /usr/bin/ directory. This directory is hidden from the finder, but is accessible from the Terminal application. You can also download them if you are a member of either the iPhone or Mac Developer Program. One way to navigate to the tools is to log onto connect.apple.com, then click either iPhone or QuickTime under the Downloads heading.

Media Stream Segmenter
The mediastreamsegmenter command-line tool takes an MPEG-2 transport stream as an input and produces a series of equal-length files from it, suitable for use in HTTP Live Streaming. It can also generate index files, encrypt the media, produce encryption keys, optimize the files by reducing overhead, and create the necessary files for automatically generating multiple stream alternates. For details, type man mediastreamsegmenter from the terminal window.

File Stream Segmenter
The filestreamsegmenter command-line tool takes an encoded media file as an input, wraps it in an MPEF-2 transport stream (unless it is already encapsulated in one), and produces a series of equal-length files from it, suitable for use in HTTP Live Streaming. The file stream segmenter behaves very much like the media stream segmenter, but it works on existing files instead of streams coming from an encoder. For details, type man filetreamsegmenter from the terminal window.

Media Stream Validator
The mediastreamvalidator command-line tool examines the index files, stream alternates, and media segment files on a server and tests to determine whether they will work with HTTP Live Streaming clients. For details, type man mediastreamvalidator from the terminal window.

Varient Playlist Creator
The variantplaylistcreator command-line tool creates index files, or playlists, for alternate streams from the output of the media stream segmenter (the media stream segmenter must be invoked with the -generate-variant-info argument to produce the required output for the playlist creator). For details, type man varientplaylistcreator from the terminal window.

I will not go in any further on these programs other then I will show you how to use them. But if you want to read more about the protocols and so on you can find the information here at Apple Developer.

To get started:
Choose your videofiles, in this example I will use three files low-chi-phi.3gp/mid-chi-phi.3gp/high-chi-phi.3gp these files have been created in advanced and will not be further explained in this post. Also choose one image that will be used for the stream with only audio.

Open the terminal window on your Mac (write “terminal” in finder if you don’t know how to start it).
Go to the folder where you will put your files, I’ve choosen to save them in /Users/niklashammarstrom/Sites/testfiler

Create four different folders to save the mediafiles in, this will make it easier to keep track of your material. (I’ve created low/mid/high/audio)
mkdir low
mkdir mid
mkdir high
mkdir audio

After that you need to decide where you going to put these files on the web, since the playlist we’re going to create have hardcoded web url’s. I’ve choosen to put my testfiles at http://www.niklashammarstrom.com/iphone/ (You can change this url to fit your server url in the example below.

We start by creating the audio only stream.
/usr/bin/mediafilesegmenter –audio-only –meta-file /Users/niklashammarstrom/Desktop/bild.PNG –meta-type picture -f audio/ low-chi-phi.3gp -b http://www.niklashammarstrom.com/iphone/testfiler/audio -I -i audio-sport.m3u8 -l iphone.log

Some explanations to the above
-I in tells the system that you which to create a dynamic bitrate file that you will use with variantplaylistcreator later.
-i gives you the possibility to set your own name on the index file. Default is prog_index.m3u8
-l tells the system to log everything in a file called iphone.log
-f is just to indicate the path to where you want to store your media files.

Next we create the low version
/usr/bin/mediafilesegmenter -f low/ low-chi-phi.3gp -b ttp://www.niklashammarstrom.com/iphone/testfiler/low -I -i sport.m3u8 -l iphone.log

And then the mid and high version
/usr/bin/mediafilesegmenter -f mid/ mid-chi-phi.3gp -b http://www.niklashammarstrom.com/iphone/testfiler/low -I -i sport.m3u8 -l iphone.log
/usr/bin/mediafilesegmenter -f high/ high-chi-phi.3gp -b http://www.niklashammarstrom.com/iphone/testfiler/low -I -i sport.m3u8 -l iphone.log

Once this is done you should be able to a lot of small files in the folders named low/mid/high. You will also see files called *.plist in the same folders as original 3gp files are stored.

The next thing we need to do is to created the dynamic playlist so the iphone will know what file to choose. This is done by the variantplaylistcreator.

/usr/bin/variantplaylistcreator -o iphone-sport.m3u8 http://www.niklashammarstrom.com/iphone/testfiler/low/sport.m3u8 testfiler/low-chi-phi.plist http://www.niklashammarstrom.com/iphone/testfiler/mid/sport.m3u8 testfiler/mid-chi-phi.plist http://www.niklashammarstrom.com/iphone/testfiler/high/sport.m3u8 testfiler/high-chi-phi.plist

This will create a playlist called iphone-sport.m3u8 that knows of all the other video bitrates.

If you have done this right your iphone-sport.m3u8 should look something similiar to this when you open it up in a text editor.

#EXTM3U
#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=79358

http://www.niklashammarstrom.com/iphone/testfiler/audio/audio-sport.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=79358

http://www.niklashammarstrom.com/iphone/testfiler/low/sport.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=621958

http://www.niklashammarstrom.com/iphone/testfiler/mid/sport.m3u8

#EXT-X-STREAM-INF:PROGRAM-ID=1,BANDWIDTH=1533985

http://www.niklashammarstrom.com/iphone/testfiler/high/sport.m3u8

You can now try to open the url in your browser on your iphone and test it.

Good luck.

If you have any questions please leave your name on blog and I will get back to you.