0

I am trying to stream a mp4 file using ASP/VBscript. I use ADODB.Stream to lunch the file and some codes to detect that which range is requested by user. The video is being loaded successfully but by clicking on seek bar nothing happens. I have set all Accept-Ranges , Content-Range and Content-Length in response headers:

HTML code:

<video controls>
<source src="video.asp">
</video>

video.asp

filename ="test.mp4"

Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Type = 1
objStream.Open
objStream.LoadFromFile(server.mappath(filename))
sizebyte=objStream.size

Response.ContentType = "video/mp4"
Response.AddHeader "Accept-Ranges", "0-" & sizebyte

'Checking if user requested a range (by clicking on seekbar)

if Request.ServerVariables("HTTP_RANGE")<>"" then
    'Cleaning startbyte from (bytes=) and (-)
    startbyte=replace(Request.ServerVariables("HTTP_RANGE"),"-","")
    startbyte=replace(startbyte,"bytes=","")

    Response.AddHeader "Content-Range", "bytes " & startbyte & "-" & sizebyte-1 & "/" & sizebyte
    response.AddHeader "Content-Length", (int(sizebyte) - int(startbyte))
else
    response.AddHeader "Content-Length", sizebyte
end if

do while not objStream.EOS
    response.binarywrite objStream.Read(1024000)
    Response.Flush
loop

objStream.Close
Set objStream = Nothing
Response.StatusCode = 206
Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
  • Your headers look to be right but after doing all that you then try to stream the full file in chunks when the client is only expecting the range defined in the response headers. For example move the stream 10 seconds in and see what is passed in the `Content-Range` and adjust the `Position` of the `ADODB.Stream` according to the start byte. – user692942 Oct 26 '20 at 19:29
  • Thank you @Lankymart . I added `objStream.position=startbyte` but no change yet. – Ali Sheikhpour Oct 26 '20 at 19:38
  • Okay so the start position has been corrected but what about the amount of bytes you are reading? It needs to match the amount expected by the `Content-Range` header. – user692942 Oct 26 '20 at 21:26
  • @AliSheikhpour As the name suggests `.LoadFromFile` loads all data into memory, even you need to send ~ 1 MB of data, all the video file (say 1 GB in size) will be read and written to memory. Then, this allocated memory will not be released until the responding ends. It's a huge waste of resources in every aspects. – Kul-Tigin Oct 27 '20 at 05:04
  • 2
    @AliSheikhpour If the aim is to not compromise video URL and / or some authorization logic that you're doing in ASP Classic, I'd strongly recommend you to do it with something else. A .NET HTTP Module / Handler or even a PHP script if your web server supports would be better choice. – Kul-Tigin Oct 27 '20 at 05:04
  • I can not find any referece for ado stream to get partial data from file. Can you help? @Lankymart – Ali Sheikhpour Oct 27 '20 at 14:51
  • @AliSheikhpour there isn't anything like that when you have loaded the file you can return part of the stream by moving the position and only returning so many bytes but as Kul-Tigin says that is inefficient. Fact is, Classic ASP was never designed to deal with streaming video and you would be much better off with a newer technology stack. – user692942 Oct 27 '20 at 16:01

0 Answers0