Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello dear.
i need to play a stream a MP4 type with Asp.net or ASHX that user can tracking it video.
i found this code :
C#
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(",") > -1)
                {
                    // (?) Shoud this be issued here, or should the first
                    // range be used? Or should the header be ignored and
                    // we output the whole content?
                    context.Response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
                    throw new HttpException(416, "Requested Range Not Satisfiable");

                }

                // If the range starts with an '-' we start from the beginning
                // If not, we forward the file pointer
                // And make sure to get the end byte if spesified
                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 > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
                }
                // End bytes can not be larger than $end.
                anotherEnd = (anotherEnd > end) ? end : anotherEnd;
                // Validate the requested range and return an error if it's not correct.
                if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= 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
            using (FileStream Strm = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
            {
                context.Response.Clear();
                context.Response.ClearContent();
                //context.Response.ClearHeaders();
                Strm.Seek(fp,SeekOrigin.Begin);
                byte[] bt = new byte[Convert.ToInt32(length)];
                Strm.Read(bt, 0, bt.Length);
                context.Response.BinaryWrite(bt);
                bt = new byte[0];
            }
            context.Response.Flush();
            context.Response.Close();
            context.Response.End();
         
    }


C#
public void ProcessRequest(HttpContext context)
  {
      //context.Response.Headers.Clear();
      string filename = context.Request["fn"];
      string mimetype = "video/mp4";
      string fullpath = @"E:\" + filename;
      if (System.IO.File.Exists(fullpath))
      {

          context.Response.ContentType = mimetype;
          if (!String.IsNullOrEmpty(context.Request.ServerVariables["HTTP_RANGE"]))
          {
              //request for chunk
              RangeDownload(fullpath,context);
          }
          else
          {
              //ask for all
                  System.IO.FileInfo fileInfo = new System.IO.FileInfo(fullpath);
                  long fileLength = fileInfo.Length;
                  context.Response.AddHeader("Content-Length", fileLength.ToString());
                  //context.Response.WriteFile(fullpath);

                  using (FileStream Strm = new FileStream(fullpath, FileMode.Open, FileAccess.Read))
                  {
                  context.Response.Clear();
                  context.Response.ClearContent();
                  context.Response.ClearHeaders();
                  context.Response.ContentType = "video/mp4";
                  byte[] bt = new byte[Strm.Length];
                  Strm.Read(bt, 0, bt.Length);
                  context.Response.BinaryWrite(bt);
                  bt = new byte[0];
                 }

                  context.Response.Flush();
                  context.Response.Close();
                  context.Response.End();
          }
      }
      else
      {
          throw new HttpException(404, "Video Not Found Path:"+fullpath);
      }
  }

  public bool IsReusable {
      get {
          return false;
      }
  }


But when user tracking video , after several moves , ram will full.
Where is my problem?
Thank you.
Posted
Updated 23-Jun-14 18:20pm
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900