0

I'm absolutely new to webservers and node JS and I was following this tutorial to create - and understand - a streaming server on my Intel Edison board. I already read the other posts about this problem on this website but I could not find an answer for my case

After starting the .js server I can correctly connect to it typing in my client browser [I'm using Chrome] something like: http://192.168.1.31:8888/ and a video, the same given with the tutorial, is displayed. If I want I can even activate it and it works fine

But looking to the code provided:

http.createServer(function (req, res) {

    var reqResource = url.parse(req.url).pathname;
    //console.log("Resource: " + reqResource);

    if(reqResource == "/"){

        //console.log(req.headers)
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(indexPage);
        res.end();

    } else if (reqResource == "/favicon.ico"){

        res.writeHead(404);
        res.end();

    } else {

            var total;
            if(reqResource == "/movie.mp4"){
                total = movie_mp4.length;
            } else if(reqResource == "/movie.ogg"){
                total = movie_ogg.length;
            } else if(reqResource == "/movie.webm"){
                total = movie_webm.length;
            } 

            var range = req.headers.range;

            var positions = range.replace(/bytes=/, "").split("-");
            var start = parseInt(positions[0], 10);
            // if last byte position is not present then it is the last byte of the video file.
            var end = positions[1] ? parseInt(positions[1], 10) : total - 1;
            var chunksize = (end-start)+1;

            if(reqResource == "/movie.mp4"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/mp4"});
                res.end(movie_mp4.slice(start, end+1), "binary");

            } else if(reqResource == "/movie.ogg"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/ogg"});
                res.end(movie_ogg.slice(start, end+1), "binary");

            } else if(reqResource == "/movie.webm"){
                res.writeHead(206, { "Content-Range": "bytes " + start + "-" + end + "/" + total, 
                                     "Accept-Ranges": "bytes",
                                     "Content-Length": chunksize,
                                     "Content-Type":"video/webm"});
                res.end(movie_webm.slice(start, end+1), "binary");
            }
    }
}).listen(8888);

I think I should also be able to type in my client something like: http://192.168.1.31:8888/movie.mp4 and solve this address correctly receiving slices of video on my chrome client. The problem is that when I try to do this I get the following error:

 var positions = range.replace(/bytes=/, "").split("-");
                                  ^
TypeError: Cannot call method 'replace' of undefined
    at Server.<anonymous> (/home/root/streaming-video-html5/streaming-video-html5/server.js:66:35)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2109:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:122:23)
    at Socket.socket.ondata (http.js:1967:22)

I understood from here that this error comes from having range.replace.split undefined but why?

Any idea on how to fix this annoying problem?

Thanks in advance to those who will try to help

0andriy
  • 4,183
  • 1
  • 24
  • 37
Antonino
  • 3,178
  • 3
  • 24
  • 39

1 Answers1

0

client may not be sending a range in the request header - it's not mandatory, so try this

    var range = req.headers.range;
    var start = 0, end = total - 1;
    if(range) {
        var positions = range.replace(/bytes=/, "").split("-");
        start = parseInt(positions[0], 10);
        // if last byte position is not present then it is the last byte of the video file.
        end = positions[1] ? parseInt(positions[1], 10) : total - 1;
    }

I think that should fix it

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
  • your explanation was the reason of the error and your solution fixes the address problem, after that I can correctly type http://192.168.1.31:8888/movie.mp4 watching the video without the server to crash. But now loading any html page on the server like 192.168.1.31:8888/videolist.html results in an endless process, and even useless because at the end the elements are not loaded. So: 1) any idea on the reason and the fix for that? 2) is it a wrong impression of mine or when the range is not provided the streaming server works as a file server? with a 60MB video I couldn't see it – Antonino Jul 10 '15 at 06:08
  • my idea would be "manually" dividing the chunks with a for cycle until the last byte, according to you does it make sense? thanks again for your time – Antonino Jul 10 '15 at 06:16