TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

HTML Live Video Streaming Via WebSockets

134 pointsby phoboslabover 11 years ago

12 comments

DougWebbover 11 years ago
I <i>just</i> implemented something similar. I have an IP webcam on my network, but I noticed that the built-in viewer uses 30% cpu time on my laptop when I view it in Chrome. (20% on Firefox, about the same on IE using an ActiveX version of the client.) The built-in viewer supports a &#x27;cell phone&#x27; mode that sends a stream of jpegs, but in an awful way that uses even more cpu time. I decided to write my own wrapper around the url it uses to get a jpeg of the current camera view.<p>This uses requestAnimationFrame, and lets me set the max framerate I want. Since I&#x27;m just watching my dog Mischa and she sleeps most of the time, I set it to 0.5fps which keeps my cpu usage around 1%.<p><pre><code> &lt;div&gt; &lt;img id=&quot;mischacam&quot; &#x2F;&gt; &lt;br &#x2F;&gt; &lt;span id=&quot;time&quot;&#x2F;&gt; &lt;&#x2F;div&gt; &lt;script type=&quot;text&#x2F;javascript&quot;&gt; var camURL = &quot;....&quot;; var msPerFrame = 2000; var loading = false; function update(timestamp) { if (loading) return; loading = true; lastFrameTime = timestamp; $(&quot;#mischacam&quot;).attr(&quot;src&quot;, camURL + &quot;&#x2F;snapshot.cgi?_=&quot; + Math.random()); $(&quot;#time&quot;).html(new Date().toString()); } window.requestAnimFrame = (function(){ return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || function( callback ){ window.setTimeout(callback, 1000 &#x2F; 60); }; })(); var lastFrameTime; function animloop(timestamp){ if (lastFrameTime == null) lastFrameTime = timestamp; if (timestamp - lastFrameTime &gt; msPerFrame) update(timestamp); requestAnimFrame(animloop); } $(document).ready(function () { $(&quot;#mischacam&quot;).on(&quot;load&quot;, function () { loading = false; }); animloop(0); }); &lt;&#x2F;script&gt;</code></pre>
评论 #6370884 未加载
zimbatmover 11 years ago
It&#x27;s not very clear from the article why this solution is superior to using a x-mixed-replace&#x2F;multipart stream that contains image&#x2F;jpeg bodies. If you don&#x27;t have any audio just stream your jpegs using the x-mixed-replace HTTP response type. All browsers support it (even IE6).
评论 #6368773 未加载
评论 #6372312 未加载
paraboulover 11 years ago
It&#x27;s awkward that in 2013 we have to complexify design to this point :<p>&quot;Streaming video through WebSocket (thus over http upgrade) through a plain-tcp-to-websocket-proxy on top of...&quot;.<p>It feels over-engineered and like TCP-Sockets are a revolution. I know, we can&#x27;t easily expose plain-socket because of security but still... What&#x27;s next? UDP for 2018? Socket-binding for 2022?<p>(Nice work though)
评论 #6370289 未加载
devongovettover 11 years ago
Super cool. I just submitted a pull request to jsmpeg to move some of the RGB conversion stuff to the GPU using WebGL. Reduces CPU usage by 5-10%. <a href="https://github.com/phoboslab/jsmpeg/pull/1" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;phoboslab&#x2F;jsmpeg&#x2F;pull&#x2F;1</a>
shurcooLover 11 years ago
I just tried your instant webcam app, and omg it is so cool. I&#x27;ve been looking for something like that. Very impressive low latency.<p>It&#x27;s free too, how are you making money? Thanks for making it! (Now I just need something for streaming video between laptops, between laptop and mobile device. I find it very annoying how FaceTime doesn&#x27;t let you call yourself from one device to another.)
bashtoniover 11 years ago
The Raspberry Pi has hardware video encoding support via the GPU. I dug up this blog post about it:<p><a href="http://theiopage.blogspot.co.uk/2013/04/enabling-hardware-h264-encoding-with.html" rel="nofollow">http:&#x2F;&#x2F;theiopage.blogspot.co.uk&#x2F;2013&#x2F;04&#x2F;enabling-hardware-h2...</a><p>Would be great to integrate this with the node app.
评论 #6368396 未加载
hardwaresoftonover 11 years ago
This looks awesome, but I&#x27;d love to hear some comparisons between this hack (it is definitely far simpler) and WebRTC or multipart JPG.<p>Also, great hack, it seems so obvious, but is actually pretty great.
Jhstoover 11 years ago
As a side note, the page completely hangs Chrome for iOS.
评论 #6368788 未加载
评论 #6368774 未加载
IgorPartolaover 11 years ago
Just released something that streams video like this using MJPEG: <a href="https://igorpartola.com/projects/hawkeye/" rel="nofollow">https:&#x2F;&#x2F;igorpartola.com&#x2F;projects&#x2F;hawkeye&#x2F;</a>. I thought about using WebSockets but MJPEG seemed to be reasonably well supported by all the browsers I cared about do I chose to use it instead.
0x006Aover 11 years ago
why is that better than any webcam using multipart jpg?
adambomover 11 years ago
I wonder if you could just encode each frame as a gif and send it to the browser as part of an animated gif a la gifsockets (<a href="https://github.com/videlalvaro/gifsockets" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;videlalvaro&#x2F;gifsockets</a>)?
评论 #6371199 未加载
kzahelover 11 years ago
Very cool! I like the simple approach of foregoing all the fancy new tech and doing the simplest possible thing (though the quality&#x2F;bitrate does suffer)