Introduction
The Sinch SDK is a product that makes adding voice calling and/or instant messaging to mobile apps easy. It handles all the complexity of signalling and audio management while providing you the freedom to create a stunning user interface.
Refer the below link for complete sample code:-
Download Sample Code
Refer the below link for complete sample code:-
Download Sample Code
First time setup
Below is a step-by-step guide on setting up the Sinch SDK for the first time.
Register an Application
1. Register a Sinch Developer account at http://www.sinch.com/signup.
2. Setup a new Application using the Dashboard where you can then obtain an Application Key and an Application Secret.
Download
The Sinch SDK can be downloaded at www.sinch.com/download/. It contains: the library aar, this user guide, reference documentation, and sample apps for calling and instant messaging.
Add the Sinch library
The Sinch SDK library is distributed in AAR format. To use it in your project choose File -> New -> New Module -> Import .JAR/.AAR Package option
Permissions
A minimum set of permissions are needed for the app to use the Sinch SDK. These are specified in the AndroidManifest.xml file. If the calling functionality will be used, all five permissions listed here are needed. However, if the calling functionality isn’t used, the last three (RECORD_AUDIO, MODIFY_AUDIO_SETTINGS and READ_PHONE_STATE) can be omitted.
1 2 3 4 5 | <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> |
Sinch client
The SinchClient is the Sinch SDK entry point. It is used to configure the user’s and device’s capabilities, as well as to provide access to feature classes such as the CallClient, MessageClient and AudioController.
Create a SinchClient
1 2 3 4 5 6 7 8 | // Instantiate a SinchClient using the SinchClientBuilder. android.content.Context context = this.getApplicationContext(); SinchClient sinchClient = Sinch.getSinchClientBuilder().context(context) .applicationKey("<application key>") .applicationSecret("<application secret>") .environmentHost("sandbox.sinch.com") .userId("<user id>") .build(); |
Note: All listener callbacks emitted from the Sinch SDK are invoked on the same thread that the call to SinchClientBuilder.build is made on. If the invoking thread is not the main-thread, it needs to have an associated Looper.
Start the Sinch client
Before starting the client, add a client listener
1 2 3 4 5 6 7 8 | sinchClient.addSinchClientListener(new SinchClientListener() { public void onClientStarted(SinchClient client) { } public void onClientStopped(SinchClient client) { } public void onClientFailed(SinchClient client, SinchError error) { } public void onRegistrationCredentialsRequired(SinchClient client, ClientRegistration registrationCallback) { } public void onLogMessage(int level, String area, String message) { } }); sinchClient.start(); |
When the app is done using the SinchClient, it should be stopped. If the client is currently listening for incoming events, it needs to stop listening as well. After terminate is called, any object retrieved directly from the client object (that is, CallClient, MessageClient, and AudioController) is considered invalid.
Terminating the client:
1. sinchClient.stopListeningOnActiveConnection();
2. sinchClient.terminate();
Setting up a video call
Just like audio calls, video calls are placed through the CallClient and events are received using the CallClientListener. The call client is owned by the SinchClient and accessed using sinchClient.getCallClient(). Calling is not enabled by default.
Showing the video streams
Once you have created a VideoCallListener and added it to a call, the onVideoTrackAdded() method will be called.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | @Override public void onVideoTrackAdded(Call call) { // Get a reference to your SinchClient, in the samples this is done through the service interface: VideoController vc = getSinchServiceInterface().getVideoController(); View myPreview = vc.getLocalView(); View remoteView = vc.getRemoteView(); // Add the views to your view hierarchy } After the call has ended, don’t forget to remove the views from your view hierarchy again. @Override public void onCallEnded(Call call) { // Remove Sinch video views from your view hierarchy } |
To pause the local video stream, use the pauseVideo() method on the call.
1. // User pause the video stream
2. call.pauseVideo();
Resuming video stream
To resume the local video stream, use the resumeVideo() method on the call.
1. // User resumes the video stream
2. call.resumeVideo();
Pausing video stream delegates
Once you have created a VideoCallListener and added it to a call, the onVideoTrackPaused() method will be called when the remote user pause the video stream.
1. @Override
2. public void onVideoTrackPaused(Call call) {
3. // Implement what to be done when remote user pause video stream.
4. }
Resuming video stream delegates
Once you have created a VideoCallListener and added it to a call, the onVideoTrackResumed() method will be called when the remote user resumes the video stream..
1. @Override
2. public void onVideoTrackResumed(Call call) {
3. // Implement what to be done when remote user resumes video stream.
4. }
Video content fitting and aspect ratio
How the remote video stream is fitted into a view can be controller by the setResizeBehaviour() method with possible arguments VideoScalingType.ASPECT_FIT, VideoScalingType.ASPECT_FILL and VideoScalingType.ASPECT_BALANCED. The local preview will always use VideoScalingType.ASPECT_FIT.
Switching capturing device
The capturing device can be switched using videoController.setCaptureDevicePosition(int facing) with possible values Camera.CameraInfo.CAMERA_FACING_FRONT and Camera.CameraInfo.CAMERA_FACING_BACK. Use videoController.toggleCaptureDevicePosition() to alternate the two.
Accessing video frames of the remote streams
The Sinch SDK can provide access to raw video frames via a callback function. This callback can be used to achieve rich functionality such as applying filters, adding stickers to the video frames, or saving the video frame as an image.
Your video frame handler needs to implement VideoFrameListener interface by implementing the onFrame() callback. Note that it is important to explicitly release the video frame by calling release().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Example: import com.sinch.android.rtc.video.VideoFrame; import com.sinch.android.rtc.video.VideoFrameListener; public class YourVideoFrameHandler implements VideoFrameListener { public synchronized void onFrame(String callId, VideoFrame videoFrame) { // Process videoFrame videoFrame.release(); // Release videoFrame}} Use setVideoFrameListener() to register your video frame handler as the callback to receive video frames. Example: YourVideoFrameHandler videoFrameHandler = new YourVideoFrameHandler(); VideoController vc = getSinchServiceInterface().getVideoController(); vc.setVideoFrameListener(videoFrameHandler); |
No comments:
Post a Comment