Giter Site home page Giter Site logo

Comments (9)

nkzawa avatar nkzawa commented on July 24, 2024

Thank you for reporting it.
Could you tell me the details of how you benchmarked?

I want to know which part is the problem, since many things will happen asynchronously when you execute ss.createBlobReadStream(blob).pipe(stream);.

from socket.io-stream.

binarykitchen avatar binarykitchen commented on July 24, 2024

Benchmark with Date.now() and console.log.

var stream      = ss.createStream(),
    self        = this,
    framesCount = self.framesCount;

var start1 = Date.now();

this.canvas.toBlob(function(blob) {

    ss(self.socket).emit(
        'newFrame',
        stream,
        {
            framesCount: framesCount
        }
    );

    var blobStream = ss.createBlobReadStream(blob);

    blobStream.on('end', function() {
        var end = Date.now();

        console.log('Time1 (ms)', end - start1);
        console.log('Time2 (ms)', end - start2);
    });

    var start2 = Date.now();
    blobStream.pipe(stream);

}, 'image/jpeg', this.settings.video.quality);

Will output something like:

Time1 (ms) 160
Time2 (ms) 124

You can see that Time2 measures the time for piping alone which consumes over 70% of the whole code snippet which is not good.

from socket.io-stream.

nkzawa avatar nkzawa commented on July 24, 2024

Ah, got it.

Sending data is almost finished when end event happens, which means Time2 includes time of network communications.

And data is sent gradually and asynchronously, so the main thread is not blocked.

If you need more performance, highWaterMark option may help.

ss.createStream({hightWaterMark: 16 * 1024});
ss.createBlobReadStream(blob, {hightWaterMark: 16 * 1024});

from socket.io-stream.

binarykitchen avatar binarykitchen commented on July 24, 2024

@nkzawa Thanks! Can you explain what highWaterMark means? Where it is documented?

from socket.io-stream.

binarykitchen avatar binarykitchen commented on July 24, 2024

@nkzawa And I do not want a writable stream when sending the image to the server. Can I get a readable stream only? (I suspect this because hightWaterMark is for writable streams only)

from socket.io-stream.

nkzawa avatar nkzawa commented on July 24, 2024

highWaterMark is both for readable and writable stream.

see the document: http://nodejs.org/api/stream.html

from socket.io-stream.

binarykitchen avatar binarykitchen commented on July 24, 2024

I see. Have implemented something like this to automatically adjust the buffer size:

            this.canvas.toBlob(function(blob) {

                ss(self.socket).emit(
                    'newFrame',
                    stream,
                    {
                        framesCount: framesCount
                    }
                );

                var blobStream = ss.createBlobReadStream(blob, {hightWaterMark: self.hightWaterMark}),
                    size = 0;

                blobStream.on('data', function(chunk) {
                    size += chunk.length;
                });

                blobStream.on('end', function() {
                    // automatically correct max buffer size for the next image frame
                    self.hightWaterMark = size * 1.1;
                });

                blobStream.pipe(stream);

            }, 'image/jpeg', this.settings.video.quality);

from socket.io-stream.

nkzawa avatar nkzawa commented on July 24, 2024

I'm not sure whether setting hightWaterMark higher than a blob size is good or not. You should try.

I will close the issue anyway.

from socket.io-stream.

binarykitchen avatar binarykitchen commented on July 24, 2024

@nkzawa Thanks but sorry, I have quit this module and am using the websocket-stream one. It's pure binary and faster.

from socket.io-stream.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.