jhiesey / videostream

Play html5 video when from a file-like object
MIT License
247 stars 73 forks source link

Converting data to an ArrayBuffer in node #3

Closed jakefb closed 8 years ago

jakefb commented 9 years ago

Hi, I want to use videostream with webtorrent in node. This is because I'm trying to make an electron app which runs the hybrid client version of webtorrent, for streaming videos. When I try to use videostream as a module in node I get:

"Uncaught TypeError: data.arrayBuffer is not a function", source: /usr/lib/node_modules/webtorrent/node_modules/dezalgo/node_modules/asap/asap.js

I think this is because node doesn't support toArrayBuffer. I tried using the node buffer API instead but I couldn't get it to work. Could you please help? Thanks.

jhiesey commented 9 years ago

Hi @jakefb sorry I missed this. I'll take a look soon

jhiesey commented 9 years ago

Hi again, I finally got a chance to take a look at this. I haven't used electron myself, but my understanding is that it has two types of processes, a node-like main process on the backend that doesn't have access to the dom, and a chrome-like renderer process (controlled by a BrowserWindow) for each webpage.

Which process are you trying to run videostream in?

I'm not sure how you would use videostream on the main node-like process, since it won't have access to the video element in the dom.

In the renderer process buffer.toArrayBuffer() should be available already and videostream ought to work as is. If this is what you're doing and it still isn't working let me know and I can look into it.

jakefb commented 9 years ago

Sorry for the late reply, I'm pretty new to how Electron works so I would have to find out more to give you a detailed answer.

Pretty much what I wanted to do, was run WebTorrent inside the node-like process so that it is faster and can communicate with BitTorrent peers and use videostream to stream the torrent to the video element in the dom.

jhiesey commented 9 years ago

It sounds like you'd have to build a bridge between webtorrent and videostream.

You would need to write something that takes a createReadStream() call from videostream in the renderer and send it to the node process, and then pass the stream with the video data back to the renderer. I would guess that wouldn't be too hard, but you'll have to look into it and see.

jakefb commented 9 years ago

Thanks, I will give this a try and let you know how it goes!

jakefb commented 9 years ago

I have tried to fix this but I'm sure the issue with how webtorrent works in node instead of electron. I don't think the code is running in the main process because it access to the dom (the video tag still appears but nothing gets streamed to it). Take a look at these comments from asap.js.

    } catch (e) {
        if (isNodeJS) {
            // In node, uncaught exceptions are considered fatal errors.
            // Re-throw them synchronously to interrupt flushing!

            // Ensure continuation if the uncaught exception is suppressed
            // listening "uncaughtException" events (as domains does).
            // Continue in next event to avoid tick recursion.
            if (domain) {
                domain.exit();
            }
            setTimeout(flush, 0);
            if (domain) {
                domain.enter();
            }

            throw e;

        } else {
            // In browsers, uncaught exceptions are not fatal.
            // Re-throw them asynchronously to avoid slow-downs.
            setTimeout(function() {
               throw e;
            }, 0);
        }
    }
jakefb commented 9 years ago

I got video streaming working in the node process by setting up a http server to stream the video file to the video element. It works well with node modules like airplay-js which is nice https://github.com/fastcast/fastcast-app/commit/80ede76cc502c954b8a2e5a23afff7308bf68e17

bjackson commented 9 years ago

Hmm. Perhaps it has something to do with the fact that it's using the NativeBuffer, the browser implementation of node buffer instead of node's buffer.

jhiesey commented 9 years ago

@jakefb your approach of setting up an http server is likely to work much better, since the browser handles everything necessary to request the right byte ranges.

@bjackson what specific issue are you experiencing?

bjackson commented 9 years ago

I get the exact same exception: "Uncaught TypeError: data.arrayBuffer is not a function", source: /usr/lib/node_modules/webtorrent/node_modules/dezalgo/node_modules/asap/asap.js

RaulTsc commented 8 years ago

Possibly related https://github.com/feross/buffer/issues/90

jhiesey commented 8 years ago

Yep, that's the right related issue. This should be fixed properly since the rewrite.