WebRTC V2 Simple Calling API + Mobile
WebRTC SDK Upgraded! ES6, new camera control and 100x less code than v1.
The following demo uses PubNub for signaling to transfer the metadata and establish the peer-to-peer connection. Once the connection is established, the video and voice runs on public Google STUN/TURN servers. Keep in mind, PubNub can provide the signaling for WebRTC, and requires you to combine it with a hosted WebRTC solution. For more detail on what PubNub does, and what PubNub doesn’t do with WebRTC, check out this article: https://support.pubnub.com/support/solutions/articles/14000043715-does-pubnub-provide-webrtc-and-video-chat-
At PubNub we believe simplicity is essential for our SDK usability. We've taken a simplified approach to WebRTC Peer Connections by creating and easy-to-use SDK for developers. The ideas of simplicity should span all platforms and devices too and that's why we also support Android WebRTC mobile calling with compatibility for iOS native Objective-C based WebRTC SDK. This simple developer WebRTC SDK is powered by PubNub Data Stream Network.
Supported WebRTC Features
WebRTC SDK offers many rich features and capabilities to enhance the WebRTC experience. Here is a list of the options available.
- Photo Snap Camera Transmit (STUN-less Firewall Ready)
- WebRTC Dialing (STUN-less Firewall Ready)
- WebRTC Call Receiving (STUN-less Firewall Ready)
- JSON App Messaging (chat/signals/etc.) (STUN-less Firewall Ready)
- Multi-party Calling
- Audio only Calls Optional
- Broadcasting Mode
- Instant Connect Dialing Optional
- Security SSL, AES256, ACL Access Control Management
- Password Protection via Cipher
- Event History and Call Records
- Photo History and Recording Static Snapshots of Calls (only stills)
- Connectivity Detection and Auto-Recovery
- Pre-configured Video Element for Streaming Video/Audio
- Pre-configured Local Camera Video Element for Streaming Video/Audio
- Network Connectivity Hooks (online/offline)
- SDK Level Debug Output
Testing Locally
You need an HTTPS (TLS) File Server. To start a local secure file server:
python <(curl -L https://goo.gl/LiW3lp)
Then open your browser and point it to your file in the directory you ran the python HTTPS server.
open https://localhost:4443/tutorials/
This is a Simple Python HTTPS Secure Server https://gist.github.com/stephenlb/2e19d98039469b9d0134
We posted an answer on StackOverflow WebRTC HTTPS. This will get you started testing on your laptop.
Supported WebRTC Calling API Mobile Devices and Browser
List of supported WebRTC Calling Clients including Android and Chrome.
- Chrome
- Firefox
- Opera
- Mobile Chrome - Android
- Mobile Firefox - Android
- iOS Native Objective-C Compatible
- Android Native Java Compatible
The Basic Concepts of WebRTC Calling
Making a WebRTC phone Call
// Dial Numbervar session = phone;
Receiving a WebRTC phone Call
phone;
Adding Video Live Stream
phone;
Simple WebRTC Walkthrough
Next we will start with a copy/paste example of our SDK. This Simple Example Comes in Two WebRTC Calling Sections.
- Part One will talk about how you can Make a WebRTC Call.
- Part Two will teach you about Receiving a WebRTC Call.
Making a WebRTC Calling & Receiving - Part One and Two
Make your first html file named dial.html
and copy/paste the following:
<!-- dial.html --> <!-- Video Output Zone --> Making a Call <!-- Libs and Scripts -->
Live WebRTC Call Dialer
If you combine both the WebRTC Dialer and the WebRTC Receiver you will get a complete working phone. We have a live running WebRTC Demo which uses our WebRTC SDK. This demo includes a soft-touch UI for an easy calling experience.
try the live WebRTC Dialing: WebRTC Simple Calling API + Mobile
You can click the link above to try our live WebRTC Demo which is powered by our easy to use SDK.
What Can you build with a WebRTC Simple Calling API?
There are a plethera of important and useful applications which may be built using the PubNub WebRTC Calling SDK. I have cataloged a list of ideas for WebRTC Use Cases:
- Amazon Mayday Help Button
- Salesforce SOS Help Button
- WebRTC Skype Replica
- Traditional Phone Replacement
- Chatroulette
- VoIP Replacement
- Customer Support Calls
- In-bound Sales Calls
- Easy Remote Meetings
- Call Assistant or Specialists
- Big Screen Public Announcemnt
- Live Presentations
So many different options and even more use cases that have yet to be discovered.
What is a WebRTC Session?
A WebRTC Session
is a reference to a call instance
between two connected parties.
The idea is the session is active as long as the two parties are connected.
Once one party member disconnects or leaves, the session will be terminated.
You have access to several helper methods for
session.connected()
and session.ended()
events.
API Documentation for WebRTC Calling SDK
The WebRTC Simple SDK API Reference will provide to you all the options available to you as the developer.
WebRTC Phone Initialization
PHONE({ ... })
Initialize the phone object which can send/receive multiple WebRTC call sessions without limit. Be careful as your bandwidth is the true limiter.
var phone =
WebRTC Phone Number
Your phone number is your true ring-in point of truth. You can set your phone number at init time from the
var phone = ;
WebRTC Local Camera Video Element
We provide you easy access to your local camera with a pre-initialized video element that is easy to access. Simply append the element to your DOM and the feed will stream automatically.
;
WebRTC Phone SSL Mode
You can enable SSL signalling mode for the local client device by setting the
ssl : true
parameter at init.
var phone =
WebRTC Cipher AES 256 Crypto Mode
You can enable AES256 Encryption (essentially password mode) on your phone for additional security. You're friends have to know your password to call you. AES256 option allows you to password protect your phone and only give access to people you know.
You have to give your friend your password before they can call you.
var phone =
WebRTC Phone Audio Only Mode
You can disable video codec and stream only Audio by setting the following param in your init code. Set
video : false
in the media section.
var phone =
WebRTC Phone Mobile Calling on Android
WebRTC calling on Android is web-ready compatible and works out-of-the-box today without modifications or additional code. Also WebRTC Calling is supported for Android and iOS Native too.
WebRTC Photo Sharing Bonus STUN-less Ready
You can easily snap a photo from the video stream and send it to your friends in an instant. You can think of this as an Instagram WebRTC style. Also Photo Sharing works through Corprate Enterprise Firewalls.
WebRTC Camera Photo Sharing Broadcast
phone.snap()
Broadcast your camera photo to all connected sessions. Also get the IMG data as base64 supported format for local display if desired.
phone;
WebRTC Session Camera Photo Share
session.snap()
Send your camera's latest frame as raw IMG to a specific call session.
phone;
Prevent Camera from Starting Automatically
By default the WebRTC SDK starts user's camera. You can optionally prevent this by setting the autocam
flag to false
. Here is an example of disabling the camera on initialization.
<!-- dial.html --> Start CameraStart Call <!-- Video Output Zone --> <!-- Libs and Scripts -->
WebRTC JSON Messaging Bonus STUN-less Ready
Adding extra realtime capabilities between connected parties is essential for creating collaborative and chat features. You can do that with PubNub's WebRTC SDK easily without firewall restrictions from corporate policies.
Message Broadcasting to All Sessions
phone.send(...)
Send a JSON message to all connected sessions.
phone;
Receive a JSON message from Any Session
phone.message(function(message){ ... })
Get all messages sent from any session.
phone;
Send a JSON Message to One Session
session.send(...)
You can send a direct JSON message to one session only.
session;
Receive a JSON message from One Session
session.message(function(){ ... })
You can set callbacks to capture JSON messages from a specific call session.
session;
WebRTC Phone Ready
phone.ready(function(){ ... })
Making calls is easy but you can only do it when the phone is ready to issue the signals properly and the local interfaces have been configured such as audio/video media.
phone;
WebRTC Phone Receiving Calls
phone.receive(function(session){ ... })
It's really ease to setup your phone to receive calls using the
phone.receive()
method. This method expects a callback function and will pass the WebRTC Session object as the only parameter.
phone;
Get Your Phone Number
var num = phone.number()
Sometimes you need to access the phone number that was set during initialization time. You can do that by calling
phone.number()
method which returns the setup number.
var num = phone;
Get Caller Phone Number
var num = session.number
To access current caller number, check the session object number property
session.number
.
var num = sessionnumber;
Get Call Start Time
var start = session.started
The Session object stores the call start time which you can use to display call timer on the screen.
var start = sessionstarted;
WebRTC Phone Call History via PubNub
phone.history(...)
You can get the call history of a phone number by issuing a PubNub History call on the phne number.
phonehistory number : '1234' { console; };
WebRTC Phone Dialing Numbers
phone.dial(number)
You can easily make WebRTC calls by executing the
dial()
method. The number can be any string value such as"415-555-5555"
.
var session = phone; session;session;
Set Camera Resolution
You can change the resolution of your camera's media capture. This allows you to set lower resolutions for slower p2p connections. You can also set HD 4K resolutions if you have the camera to do so.
// Phoneconst phone = ;
WebRTC Phone Multi-party Dialing
phone.dial(number)
The PubNub WebRTC Phone Dialer and Receiver supports unlimited party in/out calling.
var sessions = ; sessions;sessions;sessions;sessions;sessions; sessions;
WebRTC Video and Audio Broadcasting Mode
phone.receive(function(session){ ... })
You can receive unlimited inbound calls and become a broadcast beacon stream. You are limited by your bandwidth upload capacity.
Broadcaster with Audience Members
You'll start by opening the stream for the broadcaster so audience members can join in. Start broadcasting as the broadcaster:
Broadcaster
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// Initialize the Broadcaster's Device// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=var broadcaster = ; // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// Wait for New Viewers to Join// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=broadcaster;
Viewer
// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// Initialize the Viewer's Device// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=var viewer = ; // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// Join a Broadcast as a Viewer// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=viewer; // =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=// Show Broadcast's Video Stream// =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=viewer;
WebRTC Phone Hangup
phone.hangup()
There are two ways to hangup WebRTC calls. You can use the phone-level method
phone.hangup()
which will hangup all calls at once. Or you can use the session-level methodsession.hangup()
which will only hangup that call session.
// hangup all callsphone; // hangup single sessionsession;
WebRTC Phone Network Events
PHONE.disconnect(function(){ ... })
You need to keep track of the connectivity state of your local network connection. You can do that using these helper methods.
PHONEPHONEPHONE
WebRTC Phone Unable to Initialize
phone.unable(function(details){ ... })
Some devices or in certain situations the phone may not initialize. We give you a simple callback for when the phone startup fails.
phone;
WebRTC Stop Camera and Mic
phone.camera.stop()
You may want to Stop the Camera/Mic recording. By default the camera and mic are turned on as soon as possible. This allows for faster calling connection speeds.
var streamref = phonecamera;
WebRTC Phone Debugging
phone.debug(function(details){ ... })
You might want to see under the covers of WebRTC Calling by enabling debugging mode on the WebRTC SDK.
phone;
WebRTC Phone Auto Hangup and Goodbye on Unload
The WebRTC Calling SDK will attempt an automatic goodbye
upon graceful
disconnection attempts.
This allows the 2nd party on the other end of the phone line to
receive a call ended
signal.
This happens automatically.
The WebRTC Session Object
A WebRTC Session represents the connection between two parties with access
to the session.video
element as well as the place to register
event callbacks as needed such as session.connected
and also the ended callback for when the call disconnects or hangs up.
session
A session object is generated automatically for you upon dialing
var session = phone
and also inside registered event callbacks.
phone
WebRTC Session Number
session.number
Returns the 2nd party's (caller/callee) Phone Number associated with the Call Session.
var session = phone;console;
WebRTC Session Connected Callback
session.connected(function(session){})
Sets the callback for when the session is connected and the video stream is ready to display.
session;
WebRTC Session Ended Callback
session.ended(function(session){})
The session has ended by one of the parties. Any secondary session will continue to stream.
session;
WebRTC Session Hangup
session.hangup()
End the session right now. The
ended
callback will fire for both connected parties.
;
WebRTC Session Video Element
session.video
The Session Video Element is Accessable and Ready inside the
connected
only. The Session Video ref is an HTML Video Element<video>
.
session;
WebRTC Session Image Element
session.image
The Session Image Element is Accessable and Ready inside the
thumbnail
,connected
andended
callbacks. The Session Image ref is an HTML Image Element<img>
.
session;
WebRTC Session RTCPeerConnection Reference
session.pc
Reference to WebRTC RTCPeerConnection.
var sesionPeerConnection = sessionpc;
WebRTC Session Call Rejection and Accept Permissions
phone.send
phone.send
allows you to send programmatic messages between two phones without a video/audio stream. You may wish to setup a Call Accept/Reject phase to allow to users to accept or reject calls.
Before the Sending the Video/Audio Stream, send a signal to ask for call permission:
let user_number = "1235445"; // my friends number { phone;} { // start voice/video session phone;} { // show call rejected screen}
This allows you to create a simple contract between two parties before the video and audio stream begins.
WebRTC Adding Custom STUN and TURN Servers
You may desire to add your own custom stun or turn servers by using the
servers
parameter on initialization. For example http://xirsys.com/ offers paid-stun solution.
var phone =
SDK Possible Upgrade Future Patches
- Race - During Ring/Receive Handshake, a Hangup will create Race
- Wire-pulled Disconnect Detect via DataChannels Pings
- 5 Second Timeout to Retry with 30 Second of Retries
- Auto-reconnect re-SDP/ICE Recovery
- Custom Message Events
- Presence
- Call History
- User Lists
Implementation Reference Upgrades
- Pre-Allow Transmit - Before "allow" fire a PubNub message
- Chat on Screen
- Multi-Party Video in GUI
- Full Screen Mode
- Controlling an iFrame
What is Happens Inside the Simple WebRTC SDK
Signaling and Exchanging ICE Candidates via PubNub
The goal is to exchange ICE candidate packets between two peers.
ICE candidate packets
are structured payloads which contain possible
path recommendations between two peers.
You can use a lib which will take care of the nitty gritty such as
WebRTC Simple Calling API + Mobile
however below is the general direction that is taken inside the SDK itself.
Note that the demonstration code below is intintionally incomplete. Note however the PubNub WebRTC Signaling SDK properly covers most Calling Situations.
Signaling Example Code Follows
Generate ICE Candidates Example Code Follows:
WebRTC Troubleshooting
You may need to force clear your cache on your device, close the app completley, then restart it.
This is uncommon.
You can also enable debugging at the code-level by hooking onto the phone.unable(fn)
and phone.debug(fn)
.