Git Product home page Git Product logo

webrtc-example's Introduction

WebRTC Example

An 'as simple as it gets' WebRTC example.

See https://shanetully.com/2014/09/a-dead-simple-webrtc-example/ for a detailed walkthrough of the code.

Note: This repo is kept updated. The general ideas are there, but the above blog post may be somewhat out of date with the code in this repo.

Usage

The signaling server uses Node.js and ws and can be started as such:

$ npm install
$ npm start

With the server running, open Firefox/Chrome/Safari and visit https://localhost:8443.

Please note the following:

  • Note the HTTPS! There is no redirect from HTTP to HTTPS.
  • You'll need to accept the invalid TLS certificate as it is self-signed and WebRTC must be run over TLS.
  • Some browsers or OSs may not allow the webcam to be used by multiple pages at once. You may need to use two different browsers or machines.

Problems?

This is a short example that I don't check often. As such, I rely on users for reports if something breaks. Issues and pull requests are greatly appreciated.

License

The MIT License (MIT)

Copyright (c) 2014 Shane Tully

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

webrtc-example's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webrtc-example's Issues

Got problem with ICE candidate on Chrome on Windows

Nice stuff,

I got this error: Uncaught SyntaxError: Failed to execute 'addIceCandidate' on 'RTCPeerConnection': The ICE candidate could not be added.

For this line of code: peerConnection.addIceCandidate(new RTCIceCandidate(signal.ice));

And one more question: Do you pipe everything through the websocket. I mean both audio and video data frame at the same time. I use different technique and experienced data clogging or data bottleneck sending video or audio data alone through websocket.

Response will be greatly appreciated. I've a plan to make use of your code with more complicated stuffs. Hope you don't mind about that.

Showing Error

hi sir,
you done a great job.
i am getting a error is "Upgrade Required".
can you help me to solve this problem?

thank you.

Is it possible to add sample code to support node-webrtc

Hi,

I am new to webrtc, and I would like to implement peer-to-server for audio and video. I use existing code as client & signal server, and use "node-webrtc" as "peer-to-server"'s server, it can build up the connection, but the server side cannot receive video stream. Attached file is my test code by node-webrtc.

index.zip

No local video with Firefox

With the latest Firefox (v56) I get no local video feed and in the console appears the following error:

MediaStreamError { name: "InternalError", message: "Starting video failed", constraint: "", stack: "" }

I can not see the remote interlocutor

Hello, I can not see the remote interlocutor. I did it like in the code, everything works fine and when you press the button, the start of the video connection two times receives the video stream from the remote interlocutor, but there is no video. The signaling server is built on the WebSocket java library, data through this server is perfectly sent to two interlocutors. I also use https on the signaling server, it receives and sends connection data and formats perfectly, and the interlocutors accept this data and the function of receiving the remote video signal is triggered, but the video does not.

only works locally?

I tried to use a remote server, but it is not working. The remote video does not appear, only a black screen . The scripts were changed only the WebSocket server path. Any ideas ?

Always display "waiting for (IP)", works, then displays error as given in the comments

While running on browser, it keeps displaying "waiting for ".

After closing both the the browsers, command window displays an error.
This is the error at the end...

  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
at TLSWrap.onStreamRead (internal/stream_base_commons.js:205:27)
Emitted 'error' event on WebSocket instance at:
at WebSocket.finalize (C:\inetpub\pkVC1\node_modules\ws\lib\WebSocket.js:182:41)
at TLSSocket.emit (events.js:315:20)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] start: node server/server.js
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\HP\AppData\Roaming\npm-cache_logs\2020-08-08T09_45_25_858Z-debug.log

ws broadcast

To make it work, I had to update the broadcast from WS:

// Broadcast to all.
wss.broadcast = function broadcast(data) {
wss.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
};

Audio & Video works well on same network but not in different network

I tried your source which is really a good one. But I am facing one problem. On Same netwrok it works perfectly but on different network remote side video & audio is not working. My server is a dedicated Windows 2012 server with static IP. On Same network means if I use it on a PC and laptop with wifi on the same network it works perfectly but when ever I go to different network it doesn't work. What should I do?

"onaddstream" is deprecated

Hey.
Today my browser said me "RTCPeerConnection.onaddstream is deprecated. Use RTCPeerConnection.ontrack".
:)

Invalid peerConnectionConfig

Hi.

I just tried your code, curious about this tech, and there is a little problem:

In webrtc.js, the following variable is not correct (taken from the source repo):

var peerConnectionConfig = {
  'iceServers': [
    {'urls': 'stun:stun.stunprotocol.org:3478'},
    {'urls': 'stun:stun.l.google.com:19302'},
  ]
};

The error is the following:

SyntaxError: RTCPeerConnection constructor passed invalid RTCConfiguration - improper scheme: stun1.l.google.com webrtc.js:42:20

However, using the variable your provide at https://shanetully.com/2014/09/a-dead-simple-webrtc-example/ fixes the error.

var peerConnectionConfig = {'iceServers': [{'url': 'stun:stun.services.mozilla.com'}, {'url': 'stun:stun.l.google.com:19302'}]};

RTCPeerconnection connectionstate "failed"

So I have the same exact code as you with the following exception:

peerConnection.createOffer(offerOptions).then(createdDescription).catch(errorHandler);
where

const offerOptions = {
  offerToReceiveAudio: 1,
  offerToReceiveVideo: 1
};

Furthermore, I am also using a TURN server: {'urls': 'turn:numb.viagenie.ca','credential': '**mypass','username': '**myusername'}

But when I check the peerConnection.connectionState in the console, it says "failed".

What I do is I open 2 incognito windows in Chrome and go to

https://localhost:8443

in each window.

Then I click the "Start Video" on one of the pages and then the remoteVideo shows up with an image (i.e. a snapshot) of the frame (on both the windows). Then the localVideo changes as I move the camera, but the remoteVideo stays as a snapshot of when the connection happened (on both the windows). Then I check to see the connectionState of the peerConnection and it says it failed.

I have tried looking for solutions, but I cannot find any. Is there something I am doing wrong please let me know.

error

got description
VM36 webrtc.js:92 DOMException: Failed to set remote offer sdp: Called in wrong state: kHaveLocalOffer
6VM36 webrtc.js:92 DOMException: Error processing ICE candidate

Cannot connect 2 different users from 2 different networks.

First of all - thank you for your solution on multiple user videochat. I was studying over other examples, but ended up here, because it is open source and simple (but not much for me, as I am a begginer) to use. I would be very grateful if you could guide me quick through some points.

The thing is: I deployed your code on a Plesk server with the following changes:

server.js

const HTTPS_PORT = 8443; //default port for https is 443
const HTTP_PORT = 8443; //default port for http is 80

and changed paths to files

 if (request.url === '/webrtc.js') {
    response.writeHead(200, { 'Content-Type': 'application/javascript' });
    response.end(fs.readFileSync('public/webrtc.js'));
  } else if (request.url === '/style.css') {
    response.writeHead(200, { 'Content-Type': 'text/css' });
    response.end(fs.readFileSync('public/style.css'));
  } else {
    response.writeHead(200, { 'Content-Type': 'text/html' });
    response.end(fs.readFileSync('public/index.html'));
  }
};

webrtc.js

      .then(() => {
        serverConnection = new WebSocket('wss://' + 'localhost' + ':' + WS_PORT + '/');

Node.js:
Document root: /httpdocs/public
Application URL: http://someurl.dev
Application root: /httpdocs

So the structure on the server is:

**httpdocs/**
    |__ server.js
    |__ package.json
    |__ package-lock.json
    |__ key.perm
    |__ cert.perm
    |__ generate_cert.sh
    |__ install_service.js
    |__ **public/**
             |__ webrtc.js
             |__ index.html
             |__ style.css

Locally, I can open multiple tabs in Chrome and I see myself many times duplicated. Which is ok.
I can also duplicate tabs on remote server and I can see all my duplications in one tab. Which is also ok.

But if I test it on the remote server with my colleague: he sees only himself and I see only myself. Which is absolutelly horrible. And no errors or whatsoever in console.
I am struggling around this problem for some days already, obviously, a lack of experience doesn't let me solve it by myself.
Could you let me know, what I am doing wrong?

Error on remote server:
webrtc.js:44 WebSocket connection to 'wss://localhost:8443/' failed: Error in connection establishment: net::ERR_CONNECTION_REFUSED

P.S. I couldn't connect from my mobile also. I saw a topic here already, but I don't find any mobile stun servers.

adapter-latest.js failed

use firefox 41.0.2
http://webrtc.github.io/adapter/adapter-latest.js
fail line
var makeMapStats = (stats, legacyStats) => {
//////////////
npm install webrtc-adapter
after change to adapter.js then works fine...
/////////////
firefox 41.0.2
webrtc.js error in this line
serverConnection = new WebSocket('wss://' + window.location.hostname + ':8443');
change to chrome then works fine
//////////////

can improve for firefox?

How to change pair?

First of all I'm sorry for my English, I'm Brazilian. Your code works very well, but in a test with 6 computers, when you click the "Start" button a second time, it will no longer work. What I wanted was to switch to another pair. Can you help?

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.