Create video streaming server in indy

2019-02-28 08:37发布

问题:

I am trying to create video streaming server using Indy Http server. I am using ranged requests to send large files. One chunk of data is 10 Mb long. If video file which requests client is smaller than 10 Mb then it is all ok and vido is played. But if file size is longer than 10 Mb I return first chunk of data. Then client asks me for another chunk of data from the end of file and then my client says that it is unrecognizable video format. Can someone tell me where is problem in my code.

my server code

procedure TForm1.Button1Click(Sender: TObject);
begin
  Caption := 'Running';
  FServer := TIdHTTPServer.Create(Self);
  FServer.DefaultPort := 7070;
  FServer.OnCommandGet:=@External_Get;
  FServer.Active := True;
end;

procedure TForm1.External_Get(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  FS: TFileStream;
  Ranges: TIdEntityRanges;
  Range: TIdEntityRange;
begin
  Ranges := ARequestInfo.Ranges;
  Range := Ranges.Ranges[0];

  FS := TFileStream.Create('/home/user/Desktop/large_file.mp4', fmOpenRead or fmShareDenyWrite);
  AResponseInfo.ContentType := 'video/mp4';
  AResponseInfo.AcceptRanges := 'bytes';
  AResponseInfo.ContentStream := TIdHTTPRangeStream.Create(
    FS,
    Range.StartPos,
    Range.StartPos + 1024*1024*10,
    True
  );
  AResponseInfo.FreeContentStream := True;

  AResponseInfo.ContentRangeStart := TIdHTTPRangeStream(AResponseInfo.ContentStream).RangeStart;
  AResponseInfo.ContentRangeEnd := TIdHTTPRangeStream(AResponseInfo.ContentStream).RangeEnd;
  AResponseInfo.ContentRangeInstanceLength := AResponseInfo.ContentRangeEnd - Range.StartPos + 1;
  AResponseInfo.ContentLength := FS.Size;  
  AResponseInfo.ResponseNo := 206;
end; 

And here is my client code (I use firefox):

<!DOCTYPE html> 
<html> 
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
</head>
<body> 

<video width="400" controls>
  <source src="http://localhost:7070/test38.mp4" type="video/mp4">
  Your browser does not support HTML5 video.
</video>

</body> 
</html>

回答1:

There are several errors in your server code.

You are not validating that a range is actually being requested, or even respecting an end range if one is present.

You are setting the AResponseInfo.ContentLength property to the full size of the file, even when you are not sending the full file at one time. That value belongs in the AResponseInfo.ContentRangeInstanceLength property instead when sending a ranged response. You must set ContentLength to the size of the data actually being sent in the response, which in this case is your current range chunk. It is best not to set the ContentLength at all, you can let the server calculate it for you based on the assigned ContentStream.

You are setting the AResponseInfo.ResponseNo property to 206 unconditionally, even if a range is not requested at all, or if the requested range cannot be satisfied. TIdHTTPRangeStream performs validations in its constructor and sets its ResponseCode property accordingly. That is the value you should be assigning to ResponseNo.

Try something more like this instead:

procedure TForm1.External_Get(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
  FS: TFileStream;
  Range: TIdEntityRange;
  StartPos, EndPos: Int64;
begin
  if not FileExists('/home/user/Desktop/large_file.mp4') then
  begin
    AResponseInfo.ResponseNo := 404;
    Exit;
  end;

  try
    FS := TFileStream.Create('/home/user/Desktop/large_file.mp4', fmOpenRead or fmShareDenyWrite);
  except
    AResponseInfo.ResponseNo := 500;
    Exit;
  end;

  AResponseInfo.ContentType := 'video/mp4';
  AResponseInfo.AcceptRanges := 'bytes';

  if ARequestInfo.Ranges.Count = 1 then
  begin
    Range := ARequestInfo.Ranges.Ranges[0];

    StartPos := Range.StartPos;
    EndPos := Range.EndPos;

    if StartPos >= 0 then
    begin
      // requesting prefix range from BOF
      if EndPos >= 0 then
        EndPos := IndyMin(EndPos, StartPos + (1024*1024*10) - 1)
      else
        EndPos := StartPos + (1024*1024*10) - 1;
    end else
    begin
      // requesting suffix range from EOF
      if EndPos >= 0 then
        EndPos := IndyMin(EndPos, 1024*1024*10)
      else
        EndPos := (1024*1024*10);
    end;

    AResponseInfo.ContentStream := TIdHTTPRangeStream.Create(FS, StartPos, EndPos);
    AResponseInfo.ResponseNo := TIdHTTPRangeStream(AResponseInfo.ContentStream).ResponseCode;

    if AResponseInfo.ResponseNo = 206 then
    begin
      AResponseInfo.ContentRangeStart := TIdHTTPRangeStream(AResponseInfo.ContentStream).RangeStart;
      AResponseInfo.ContentRangeEnd := TIdHTTPRangeStream(AResponseInfo.ContentStream).RangeEnd;
      AResponseInfo.ContentRangeInstanceLength := FS.Size;
    end;
  end else
  begin
    AResponseInfo.ContentStream := FS;
    AResponseInfo.ResponseNo := 200;
  end;
end;