RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To begin both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To begin both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or fastening local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting embarked, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To begin both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of fine tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or fastening local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or fastening local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of fine tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To begin both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-made user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of fine tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a elementary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or fastening local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of good tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a elementary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting embarked, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a elementary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of fine tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a ordinary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of fine tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a elementary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-built MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting embarked, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or fastening local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and fasten it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To begin both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a plain movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting embarked, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you fasten the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To embark both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then budge into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk commences.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting began, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or linking local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a elementary Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of superb tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a ordinary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk starts.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting commenced, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-built ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and link it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-made constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should most likely be coordinated by a separate Pubnub example, possibly using shove notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Ruin
Eventually, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will comeback a JSONObject of the decent format that you can publish to any user the same way as a hangup.
RealTimeWeekly, Building an Android WebRTC Movie Talk App
Building an Android WebRTC Movie Talk App
The following tutorial is a guest post from Kevin Gleason, a Developer Evangelist Intern at PubNub and Computer Science undergrad at Boston College.
At this point, there are tons of excellent tutorials on building WebRTC movie and voice talk applications in JavaScript. In fact, at PubNub, we have one for building a elementary movie talk app in just twenty lines of JavaScript!
But what about a native Android application? Well, just in time, we just released our fresh Android WebRTC signaling API, meaning you can now create movie talking applications natively on Android in a breeze. Best of all, it is fully compatible with the PubNub Javascript WebRTC SDK! That’s right, you are minutes away from creating your very own cross platform video-chatting application.
In this tutorial, we’ll cover how to build a realtime movie and voice talk application using WebRTC for native Android. We’ll walk through how to create signaling on a standby channel to coordinate users, then stir into the movie talk portion and use the PnWebRTC API for signaling.
Here’s a list of the resources you’ll need:
Android WebRTC Tutorial Overview
In this section, we’ll look at the different components of our example application. We’ll dive deeper into each part further into the tutorial.
Users in this app layout must be subscribed to username-stdby , a standby channel. The Android side checks if a user is online by checking presence on the standby channel.
In this app, a call can be placed by sending a JSON packet to the user’s standby channel:
Upon accepting the call, the call recipient creates the SDP Suggest, and movie talk embarks.
Incoming Calls
AndroidRTC provides a good example of how to treat incoming calls.
User Messages
This app also shows how to send custom-built user messages. These messages could be talk, game scores, and much more.
Diving In: WebRTC and Android
Usage instructions
You have two options, the very first involves compiling your own binaries, and the 2nd uses the hosted library from Pristine. I strongly recommend you take the 2nd path since it is much quicker and cleaner.
Compiling your own WebRTC binaries
The PubNub Android WebRTC Signaling API was compiled using Pristine’s hosted WebRTC library. If you wish to compile your own WebRTC binaries, go after this guide.
Then, clone this repository and import it as a module. You will have to modify the module’s build.gradle and use your own version codes.
Using Pristine’s WebRTC binaries
When getting embarked, I recommend this method. It is quick and Pristine keeps up to date with their WebRTC libraries.
Permissions and Dependencies
In your application’s build.gradle , you will very first need to include a few dependencies. Very first, the WebRTC library from Pristine. 2nd, include the PubNub Signaling Library. Optionally, you may include the PubNub Android SDK.
I recommend including it as it is useful in messaging, presence, and signaling features.
Then, in your application’s AndroidManifest.xml you will need to grant the following permissions for both PubNub and WebRTC to function decently.
Using PnWebRTC in an Activity
PnWebRTC is a signaling service, meaning you will have to gather movie and audio resources separately using the WebRTC Android SDK, but this is effortless. In your movie talking activity initialize the PeerConnectionFactory with your application context and some options. With these configurations set, we can create an example of a PeerConnectionFactory to create audio and movie tracks.
These globals effect the PnPeerConnectionClient as well, so set them before instantiating your PnWebRTCClient .
PnWebRTCClient Constraints and Settings
PnWebRTCClient contains everything you will need to develop movie talk applications. This class has all the functions for signaling with WebRTC protocols, including SDP Suggest Options known as MediaConstraints . You are permitted to define custom-made MediaConstraints for the Client, and default values are used if you do not.
The default values look as goes after:
Optional: To create your own constraints, use PnRTCClient.setSignalParams(PnSignalingParams params)
PnSignalingParams holds all the constraints for a PeerConnection, Movie, and Audio, as well as the list of ICE Servers. To set up custom-made ICE servers, you may instantiate your PnSignalingParams using a List<IceServer> .
Note: All arguments to PnSignalingParams may be null. A null value will simply use the default constraint/ice server.
PnRTCListener Callbacks
PnRTCListener is an abstract class that should be extended to implement all desired WebRTC callbacks. This is what connects and powers your application. The callbacks that are defined in the PubNub Signaling API are:
The best way to extend this is to use a private class nested in your movie activity that extends PnRTCListener and implements the callbacks as you app requires.
Then add the listener to your PnRTCClient using:
It is significant that you link the callbacks to your PnRTCClient before creating connections or affixing local media stream since default callbacks (none) are used prior to calling attachRTCListener .
Using Movie and Audio Tracks
With WebRTC, you share create a MediaStream and fasten it to your PeerConnection s to begin talking. You can access camera/mic resources using the default or custom-built constraints you defined in the PnSignalingParams of your PnRTCClient . Once you have gathered the desired movie and audio tracks, you can create a MediaStream and link it to the PnRTCClient .
This will trigger you PnRTCListener ‘s onLocalStream callback, where you should display it on a GLSurfaceView .
Listening for a connection
All your resources, callbacks, and configurations are now set up. You are ready to listen for a WebRTC connection. This is simply a PubNub Subscribe which will configure a WebRTC PeerConnection and treat all SDP signaling.
This will only permit one incoming PeerConnection at a time, meaning this code could be used for 1-to-1 connections. If you don’t call setMaxConnections , your app will permit all connections.
Then, listenOn will subscribe you to your call channel so that you may begin receiving calls. It is significant to note that the Username you listen on should be unique, much like a phone number. Now we are ready to place a call.
Creating Peer Connections
To create a connection with a Peer, they must very first be listening on that channel, e.g. they have called PnRTCClient#listenOn(String user) . You can then create a connection with that username by calling PnRTCClient#connect(String user) .
In practice, this will only be used to create the RTC PeerConnection. All other signaling to get to that point from other activities or fragments should very likely be coordinated by a separate Pubnub example, possibly using thrust notifications.
My practice has been reserving the -stdby suffix from usernames, and using it as the standby pub/sub channel for each user. A user will be subscribed to their -stdby channel in all other activities until it is time to create a PeerConnection to another user.
Ending Peer Connections
To close a connection you have two options. One being the classing phone hangup where a user is done talking and thus all connections to and from that user are closed. Use PnRCTClient#closeAllConnection() to accomplish the classic hangup. The other is to close a connection with a single Peer.
This feature would use pnRTCClient#closeConnection("Username") .
Both will disconnect the PeerConnection and send a hangup signal via a PubNub Publish.
Sending User Messages
There are methods of PnRTCClient that permit you to publish a message to all/some users you are presently connected with. If you wish every user in your talk to receive the message, use PnRTCClient#transmitAll(JSONObject message) .
If you intend the message to go to a single user, call PnRTCClient#transmit(String user, JSONObject message) instead.
On Pause/Resume
Common practice for WebRTC applications involves stopping your MediaStream when you background the app. To do this, override onPause and pause the GLSurfaceView from rendering movie, then stop your VideoSource .
To commence both these functionalities back up, also override onResume to resume the GLSurfaceView and VideoSource .
On Demolish
Ultimately, you should override onDestroy to decently prepare all objects for garbage collection.
We stop our local movie source so that it will not proceed streaming after we have left the movie activity. We then call PnRTCClient#onDestroy() which cleans up the client and closes all open connections.
Static Methods, Hangup and User Message
If you need to communicate with PnRTCClient instances in situations where no client exists in your activity, perhaps an accept/reject call activity, there are static methods that treat this for you with any Pubnub example.
If you need to generate a hangup message to reject a call, you can do that as goes after with a plain Pubnub example.
Simply pass your username to PnPeerConnectionClient.generateHangupPacket(String user) and publish the returned JSONObject to the user who is calling.
The same goes for User Messages. With any Pubnub object, you can send a message through the UserMessage protocol using PnPeerConnectionClient.generateUserMessage(String user, JSONObject message) .
Again, pass your username and a JSON message. This will come back a JSONObject of the decent format that you can publish to any user the same way as a hangup.