From 56175e4fd685a75e08432dcfb9ed7d31984e6536 Mon Sep 17 00:00:00 2001 From: Antonis Tsakiridis Date: Tue, 8 Sep 2015 22:00:41 +0300 Subject: [PATCH] Fixes #184: Add webrtc video support to Hello World sample application --- Examples/restcomm-helloworld/app/app.iml | 1 + Examples/restcomm-helloworld/app/build.gradle | 2 +- .../app/src/main/AndroidManifest.xml | 11 ++ .../restcomm_helloworld/MainActivity.java | 152 +++++++++++++++--- .../app/src/main/res/layout/activity_main.xml | 13 +- .../restcomm.android.client.sdk.iml | 2 +- sipua/sipua.iml | 2 +- 7 files changed, 155 insertions(+), 28 deletions(-) diff --git a/Examples/restcomm-helloworld/app/app.iml b/Examples/restcomm-helloworld/app/app.iml index 55bd22a7..02df64ff 100644 --- a/Examples/restcomm-helloworld/app/app.iml +++ b/Examples/restcomm-helloworld/app/app.iml @@ -84,6 +84,7 @@ + diff --git a/Examples/restcomm-helloworld/app/build.gradle b/Examples/restcomm-helloworld/app/build.gradle index 09ddcd42..67141a95 100644 --- a/Examples/restcomm-helloworld/app/build.gradle +++ b/Examples/restcomm-helloworld/app/build.gradle @@ -9,7 +9,7 @@ android { minSdkVersion 16 targetSdkVersion 22 versionCode 1 - versionName "1.0" + versionName "1.0.0-BETA2#1" } buildTypes { release { diff --git a/Examples/restcomm-helloworld/app/src/main/AndroidManifest.xml b/Examples/restcomm-helloworld/app/src/main/AndroidManifest.xml index 68f70f22..496b719f 100644 --- a/Examples/restcomm-helloworld/app/src/main/AndroidManifest.xml +++ b/Examples/restcomm-helloworld/app/src/main/AndroidManifest.xml @@ -2,6 +2,15 @@ + + + + + + + diff --git a/Examples/restcomm-helloworld/app/src/main/java/com/telestax/restcomm_helloworld/MainActivity.java b/Examples/restcomm-helloworld/app/src/main/java/com/telestax/restcomm_helloworld/MainActivity.java index ae23e79c..54dd49a4 100644 --- a/Examples/restcomm-helloworld/app/src/main/java/com/telestax/restcomm_helloworld/MainActivity.java +++ b/Examples/restcomm-helloworld/app/src/main/java/com/telestax/restcomm_helloworld/MainActivity.java @@ -3,11 +3,14 @@ //import android.support.v7.app.ActionBarActivity; import android.app.Activity; import android.content.Intent; +import android.opengl.GLSurfaceView; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.util.Log; +import android.view.Window; +import android.view.WindowManager; import android.widget.Button; import android.view.View.OnClickListener; import java.util.HashMap; @@ -18,14 +21,41 @@ import org.mobicents.restcomm.android.client.sdk.RCDevice; import org.mobicents.restcomm.android.client.sdk.RCDeviceListener; import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent; +import org.webrtc.VideoRenderer; +import org.webrtc.VideoRendererGui; +import org.webrtc.VideoTrack; public class MainActivity extends Activity implements RCDeviceListener, RCConnectionListener, OnClickListener { private RCDevice device; private RCConnection connection, pendingConnection; - private HashMap params; + private HashMap params; private static final String TAG = "MainActivity"; + private GLSurfaceView videoView; + private VideoRenderer.Callbacks localRender = null; + private VideoRenderer.Callbacks remoteRender = null; + private boolean videoReady = false; + VideoTrack localVideoTrack, remoteVideoTrack; + VideoRenderer localVideoRenderer, remoteVideoRenderer; + + // Local preview screen position before call is connected. + private static final int LOCAL_X_CONNECTING = 0; + private static final int LOCAL_Y_CONNECTING = 0; + private static final int LOCAL_WIDTH_CONNECTING = 100; + private static final int LOCAL_HEIGHT_CONNECTING = 100; + // Local preview screen position after call is connected. + private static final int LOCAL_X_CONNECTED = 72; + private static final int LOCAL_Y_CONNECTED = 2; + private static final int LOCAL_WIDTH_CONNECTED = 25; + private static final int LOCAL_HEIGHT_CONNECTED = 25; + // Remote video screen position + private static final int REMOTE_X = 0; + private static final int REMOTE_Y = 0; + private static final int REMOTE_WIDTH = 100; + private static final int REMOTE_HEIGHT = 100; + private VideoRendererGui.ScalingType scalingType; + // UI elements Button btnDial; Button btnHangup; @@ -33,6 +63,20 @@ public class MainActivity extends Activity implements RCDeviceListener, RCConnec @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + // Set window styles for fullscreen-window size. Needs to be done before + // adding content. + requestWindowFeature(Window.FEATURE_NO_TITLE); + getWindow().addFlags( + WindowManager.LayoutParams.FLAG_FULLSCREEN + | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON + | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD + | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED + | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); + getWindow().getDecorView().setSystemUiVisibility( + View.SYSTEM_UI_FLAG_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY); + setContentView(R.layout.activity_main); // initialize UI @@ -41,36 +85,57 @@ protected void onCreate(Bundle savedInstanceState) { btnHangup = (Button)findViewById(R.id.button_hangup); btnHangup.setOnClickListener(this); - RCClient.initialize(getApplicationContext(), new RCClient.RCInitListener() - { - public void onInitialized() - { + RCClient.initialize(getApplicationContext(), new RCClient.RCInitListener() { + public void onInitialized() { Log.i(TAG, "RCClient initialized"); - } - public void onError(Exception exception) - { + public void onError(Exception exception) { Log.e(TAG, "RCClient initialization error"); } }); - // TODO: we don't support capability tokens yet so let's use an empty string - device = RCClient.createDevice("", this); + params = new HashMap(); + // CHANGEME: update the IP address to your Restcomm instance + params.put("pref_proxy_ip", "23.23.228.238"); + params.put("pref_proxy_port", "5080"); + params.put("pref_sip_user", "bob"); + params.put("pref_sip_password", "1234"); + device = RCClient.createDevice(params, this); Intent intent = new Intent(getApplicationContext(), MainActivity.class); // we don't have a separate activity for the calls, so use the same intent both for calls and messages device.setPendingIntents(intent, intent); - connection = null; + // Setup video stuff + scalingType = VideoRendererGui.ScalingType.SCALE_ASPECT_FILL; + videoView = (GLSurfaceView) findViewById(R.id.glview_call); + // Create video renderers. + VideoRendererGui.setView(videoView, new Runnable() { + @Override + public void run() { + videoContextReady(); + } + }); + remoteRender = VideoRendererGui.create( + REMOTE_X, REMOTE_Y, + REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false); + localRender = VideoRendererGui.create( + LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING, + LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true); + } - params = new HashMap(); - // CHANGEME: update the IP address to your Restcomm instance - params.put("pref_proxy_ip", "54.225.212.193"); - params.put("pref_proxy_port", "5080"); - params.put("pref_sip_user", "bob"); - params.put("pref_sip_password", "1234"); - // register on startup - device.updateParams(params); + @Override + protected void onDestroy() { + super.onDestroy(); + // The activity is about to be destroyed. + Log.i(TAG, "%% onDestroy"); + RCClient.shutdown(); + device = null; + } + + private void videoContextReady() + { + videoReady = true; } @Override @@ -106,7 +171,8 @@ public void onClick(View view) { HashMap connectParams = new HashMap(); // CHANGEME: update the IP address to your Restcomm instance. Also, you can update the number // from '1235' to any Restcomm application you wish to reach - connectParams.put("username", "sip:1235@54.225.212.193:5080"); + connectParams.put("username", "sip:1235@23.23.228.238:5080"); + connectParams.put("video-enabled", true); // if you want to add custom SIP headers, please uncomment this //HashMap sipHeaders = new HashMap<>(); @@ -194,6 +260,22 @@ public void onDisconnected(RCConnection connection) Log.i(TAG, "RCConnection disconnected"); this.connection = null; pendingConnection = null; + + // reside local renderer to take up all screen now that the call is over + VideoRendererGui.update(localRender, + LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING, + LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true); + + if (localVideoTrack != null) { + + localVideoTrack.removeRenderer(localVideoRenderer); + localVideoTrack = null; + } + + if (remoteVideoTrack != null) { + remoteVideoTrack.removeRenderer(remoteVideoRenderer); + remoteVideoTrack = null; + } } public void onDisconnected(RCConnection connection, int errorCode, String errorText) { @@ -214,6 +296,34 @@ public void onDeclined(RCConnection connection) { this.connection = null; pendingConnection = null; } + public void onReceiveLocalVideo(RCConnection connection, VideoTrack videoTrack) { + Log.v(TAG, "onReceiveLocalVideo(), VideoTrack: " + videoTrack); + if (videoTrack != null) { + //show media on screen + videoTrack.setEnabled(true); + localVideoRenderer = new VideoRenderer(localRender); + videoTrack.addRenderer(localVideoRenderer); + localVideoTrack = videoTrack; + } + } - + public void onReceiveRemoteVideo(RCConnection connection, VideoTrack videoTrack) { + Log.v(TAG, "onReceiveRemoteVideo(), VideoTrack: " + videoTrack); + if (videoTrack != null) { + //show media on screen + videoTrack.setEnabled(true); + remoteVideoRenderer = new VideoRenderer(remoteRender); + videoTrack.addRenderer(remoteVideoRenderer); + + VideoRendererGui.update(remoteRender, + REMOTE_X, REMOTE_Y, + REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false); + VideoRendererGui.update(localRender, + LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED, + LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED, + VideoRendererGui.ScalingType.SCALE_ASPECT_FIT, true); + + remoteVideoTrack = videoTrack; + } + } } diff --git a/Examples/restcomm-helloworld/app/src/main/res/layout/activity_main.xml b/Examples/restcomm-helloworld/app/src/main/res/layout/activity_main.xml index 4dc7f39c..c426d5e8 100644 --- a/Examples/restcomm-helloworld/app/src/main/res/layout/activity_main.xml +++ b/Examples/restcomm-helloworld/app/src/main/res/layout/activity_main.xml @@ -1,15 +1,19 @@ + android:layout_height="match_parent" + tools:context=".MainActivity"> + +