iPhone HTTP Streaming using ASP.NET, C# and VB.NET

02 February 2012

With iPhone OS 3 update came the ability to do live streaming. There are a few types of streaming and each requires a certain encoding and segmentation. In this example i am building stream using asp.net media handler pro, open source ffmpeg and segmenter utility.

Requirements

i: asp.net media handler pro

ii: open source ffmpeg utility

iii: segmenter

Steps

Publish video to iphone device

Supported Settings For Iphone

The supported bitrates for streaming are: 100 Kbps to 1.6 Mbps

The suggested bitrates for streaming are*: Low – 96 Kbps video, 64 Kbps audio

Medium – 256 Kbps video, 64 Kbps audio

High – 800 Kbps video, 64 Kbps audio

The iPhone screen size is: 480×320

Suggested ffmpeg command for encoding iphone video

ffmpeg -i <in file> -f mpegts -acodec libmp3lame -ar 48000 -ab 64k -s 320×240 -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 <output file>

 

Segment Video For Http Streaming

Now you have a complete video but you don't want to toss the entire thing up or you wouldn't be reading about HTTP streaming. What you need is a way to segment the video stream into smaller chunks. You can download Apple's segmenter 

Publishing Iphone .TS Video and Making Playlist Segment via ASP.NET Media Handler Pro

Below sample code will help you to publish .ts (video/MP2T) video and create .m3u8 (application/x-mpegURL) segments via segmenter tool.

MediaHandler media = new MediaHandler();

string RootPath = Server.MapPath(Request.ApplicationPath);

media.FFMPEGPath = RootPath + "\\\\ffmpeg\\\\ffmpeg.exe";

media.InputPath = RootPath + "\\\\contents\\\\hls";

media.OutputPath = RootPath + "\\\\contents\\\\hls";

media.FileName = "a.wmv";

media.OutputFileName = "a";

media.OutputExtension = ".ts";

media.Parameters = "-f mpegts -acodec libmp3lame -ar 48000 -ab 64k -vcodec libx264 -b 96k -flags +loop -cmp +chroma -partitions +parti4x4+partp8x8+partb8x8 -subq 5 -trellis 1 -refs 1 -coder 0 -me_range 16 -keyint_min 25 -sc_threshold 40 -i_qfactor 0.71 -bt 200k -maxrate 96k -bufsize 96k -rc_eq 'blurCplx^(1-qComp)' -qcomp 0.6 -qmin 10 -qmax 51 -qdiff 4 -level 30 -aspect 320:240 -g 30 -async 2 -s 320x240";

VideoInfo info = media.Process();

if (info.ErrorCode > 0)

{

    Response.Write("Error Occured: " + info.ErrorCode);

    return;

}

media.ServicePath = RootPath + "\\\\segmenter\\\\segmenter.exe";

string ts_filename = "\\"" + media.OutputPath + "\\\\a.ts\\"";

string m3u8_filename = "\\"" + media.OutputPath + "\\\\stream.m3u8\\"";

string prefix = "\\"" + media.OutputPath + "\\\\stream\\"";

media.Parameters = ts_filename + " 10 " + prefix + " " + m3u8_filename + " http://www.example.com/";

string output = media.ProcessCMD();

Response.Write(output);

This code will generate output .ts video with .m3u8 and segment playlist.

Add Mimetype

In order to stream .ts and .m3u8 format files properly you must add the following two mimetypes

.m3u8 application/x-mpegURL

.ts video/MP2T

 

Test Streaming

The video is encoded for the iPhone, segmented for streaming, and the server is configured. The only thing left to do is test the stream and the fastest way to do that is to use the new HTML5 video tag.

<video width='150' height='150' src="stream-128k.m3u8" />