title: Restricting bandwidth of mp4 video streaming to certain limit
body: <p>I am using the following mp4 streaming script for streaming mp4 videos on html5 player.</p>
  
  <pre><code>private void RangeDownload(string fullpath,HttpContext context)
  {
   long size,start,end,length,fp=0;
   using (StreamReader reader = new StreamReader(fullpath))
   {             
      size = reader.BaseStream.Length;       
      start = 0;
      end = size - 1;
      length = size;
     context.Response.AddHeader("Accept-Ranges", "0-" + size);
  
    if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
    {
        long anotherStart = start;
             long anotherEnd = end;
             string[] arr_split = context.Request.ServerVariables["HTTP_RANGE"].Split(new char[] { Convert.ToChar("=") });
            string range = arr_split[1];
  
           // Make sure the client hasn't sent us a multibyte range
           if (range.IndexOf(",") &gt; -1)
          {                    
                 context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                 throw new HttpException(416, "Requested Range Not Satisfiable");
  
            }
  
  
            if (range.StartsWith("-"))
             {
                 // The n-number of the last bytes is requested
                 anotherStart = size - Convert.ToInt64(range.Substring(1));
            }
            else
            {
                arr_split = range.Split(new char[] { Convert.ToChar("-") });
                anotherStart = Convert.ToInt64(arr_split[0]);
              long temp = 0;
                 anotherEnd = (arr_split.Length &gt; 1 &amp;&amp; Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
             }
  
            anotherEnd = (anotherEnd &gt; end) ? end : anotherEnd;
  
            if (anotherStart &gt; anotherEnd || anotherStart &gt; size - 1 || anotherEnd &gt;= size)
            {
  
                context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
              throw new HttpException(416, "Requested Range Not Satisfiable");
           }
           start = anotherStart;
            end = anotherEnd;
  
            length = end - start + 1; // Calculate new content length
          fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
           context.Response.StatusCode = 206;
        }
   }
        // Notify the client the byte range we'll be outputting
    context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
    context.Response.AddHeader("Content-Length", length.ToString());
       // Start buffered download
        context.Response.WriteFile(fullpath, fp, length);
        context.Response.End();
  </code></pre>
  
  <p>}</p>
  
  <p>However it works fine. Now i have sceneario where i need to restrict bandwidth of stream for paid and free users. Paid users have twice more bandwidth speed (kbps) then free user.</p>
  
  <p>For this i integrated bandwith throttling script mentioned here <a href="http://www.codeproject.com/Articles/18243/Bandwidth-throttling" rel="nofollow">http://www.codeproject.com/Articles/18243/Bandwidth-throttling</a></p>
  
  <p>so i replace the following code in above function</p>
  
  <pre><code>length = end - start + 1; // Calculate new content length
  fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
  </code></pre>
  
  <p>with</p>
  
  <pre><code>long speed = 1024000; // 1024kbps
  length = end - start + 1; // Calculate new content length
  Stream throttle = new ThrottledStream(reader.BaseStream, speed);
  fp = throttle.Seek(start, SeekOrigin.Begin);
  </code></pre>
  
  <p>but it doesn't work.
  Can any one help me how to restrict stream on above example.</p>