Skip to content

Commit 56175e4

Browse files
committed
Fixes #184: Add webrtc video support to Hello World sample application
1 parent 0558ec0 commit 56175e4

File tree

7 files changed

+155
-28
lines changed

7 files changed

+155
-28
lines changed

Examples/restcomm-helloworld/app/app.iml

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/rs" />
8585
<excludeFolder url="file://$MODULE_DIR$/build/intermediates/symbols" />
8686
<excludeFolder url="file://$MODULE_DIR$/build/outputs" />
87+
<excludeFolder url="file://$MODULE_DIR$/build/tmp" />
8788
</content>
8889
<orderEntry type="jdk" jdkName="Android API 22 Platform" jdkType="Android SDK" />
8990
<orderEntry type="sourceFolder" forTests="false" />

Examples/restcomm-helloworld/app/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ android {
99
minSdkVersion 16
1010
targetSdkVersion 22
1111
versionCode 1
12-
versionName "1.0"
12+
versionName "1.0.0-BETA2#1"
1313
}
1414
buildTypes {
1515
release {

Examples/restcomm-helloworld/app/src/main/AndroidManifest.xml

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
33
package="com.telestax.restcomm_helloworld" >
44

5+
<uses-feature android:name="android.hardware.camera" />
6+
<uses-feature android:name="android.hardware.camera.autofocus" />
7+
<uses-feature
8+
android:glEsVersion="0x00020000"
9+
android:required="true" />
10+
11+
<uses-permission android:name="android.permission.CAMERA" />
12+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
13+
514
<application
615
android:allowBackup="true"
716
android:icon="@drawable/icon_144x144"
@@ -10,6 +19,8 @@
1019
<activity
1120
android:name=".MainActivity"
1221
android:label="@string/app_name"
22+
android:configChanges="orientation|screenSize|keyboardHidden"
23+
android:screenOrientation="portrait"
1324
android:launchMode= "singleTop" >
1425
<intent-filter>
1526
<action android:name="android.intent.action.MAIN" />

Examples/restcomm-helloworld/app/src/main/java/com/telestax/restcomm_helloworld/MainActivity.java

+131-21
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
//import android.support.v7.app.ActionBarActivity;
44
import android.app.Activity;
55
import android.content.Intent;
6+
import android.opengl.GLSurfaceView;
67
import android.os.Bundle;
78
import android.view.Menu;
89
import android.view.MenuItem;
910
import android.view.View;
1011
import android.util.Log;
12+
import android.view.Window;
13+
import android.view.WindowManager;
1114
import android.widget.Button;
1215
import android.view.View.OnClickListener;
1316
import java.util.HashMap;
@@ -18,21 +21,62 @@
1821
import org.mobicents.restcomm.android.client.sdk.RCDevice;
1922
import org.mobicents.restcomm.android.client.sdk.RCDeviceListener;
2023
import org.mobicents.restcomm.android.client.sdk.RCPresenceEvent;
24+
import org.webrtc.VideoRenderer;
25+
import org.webrtc.VideoRendererGui;
26+
import org.webrtc.VideoTrack;
2127

2228
public class MainActivity extends Activity implements RCDeviceListener, RCConnectionListener, OnClickListener {
2329

2430
private RCDevice device;
2531
private RCConnection connection, pendingConnection;
26-
private HashMap<String, String> params;
32+
private HashMap<String, Object> params;
2733
private static final String TAG = "MainActivity";
2834

35+
private GLSurfaceView videoView;
36+
private VideoRenderer.Callbacks localRender = null;
37+
private VideoRenderer.Callbacks remoteRender = null;
38+
private boolean videoReady = false;
39+
VideoTrack localVideoTrack, remoteVideoTrack;
40+
VideoRenderer localVideoRenderer, remoteVideoRenderer;
41+
42+
// Local preview screen position before call is connected.
43+
private static final int LOCAL_X_CONNECTING = 0;
44+
private static final int LOCAL_Y_CONNECTING = 0;
45+
private static final int LOCAL_WIDTH_CONNECTING = 100;
46+
private static final int LOCAL_HEIGHT_CONNECTING = 100;
47+
// Local preview screen position after call is connected.
48+
private static final int LOCAL_X_CONNECTED = 72;
49+
private static final int LOCAL_Y_CONNECTED = 2;
50+
private static final int LOCAL_WIDTH_CONNECTED = 25;
51+
private static final int LOCAL_HEIGHT_CONNECTED = 25;
52+
// Remote video screen position
53+
private static final int REMOTE_X = 0;
54+
private static final int REMOTE_Y = 0;
55+
private static final int REMOTE_WIDTH = 100;
56+
private static final int REMOTE_HEIGHT = 100;
57+
private VideoRendererGui.ScalingType scalingType;
58+
2959
// UI elements
3060
Button btnDial;
3161
Button btnHangup;
3262

3363
@Override
3464
protected void onCreate(Bundle savedInstanceState) {
3565
super.onCreate(savedInstanceState);
66+
// Set window styles for fullscreen-window size. Needs to be done before
67+
// adding content.
68+
requestWindowFeature(Window.FEATURE_NO_TITLE);
69+
getWindow().addFlags(
70+
WindowManager.LayoutParams.FLAG_FULLSCREEN
71+
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
72+
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
73+
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
74+
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
75+
getWindow().getDecorView().setSystemUiVisibility(
76+
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
77+
| View.SYSTEM_UI_FLAG_FULLSCREEN
78+
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
79+
3680
setContentView(R.layout.activity_main);
3781

3882
// initialize UI
@@ -41,36 +85,57 @@ protected void onCreate(Bundle savedInstanceState) {
4185
btnHangup = (Button)findViewById(R.id.button_hangup);
4286
btnHangup.setOnClickListener(this);
4387

44-
RCClient.initialize(getApplicationContext(), new RCClient.RCInitListener()
45-
{
46-
public void onInitialized()
47-
{
88+
RCClient.initialize(getApplicationContext(), new RCClient.RCInitListener() {
89+
public void onInitialized() {
4890
Log.i(TAG, "RCClient initialized");
49-
5091
}
5192

52-
public void onError(Exception exception)
53-
{
93+
public void onError(Exception exception) {
5494
Log.e(TAG, "RCClient initialization error");
5595
}
5696
});
5797

58-
// TODO: we don't support capability tokens yet so let's use an empty string
59-
device = RCClient.createDevice("", this);
98+
params = new HashMap<String, Object>();
99+
// CHANGEME: update the IP address to your Restcomm instance
100+
params.put("pref_proxy_ip", "23.23.228.238");
101+
params.put("pref_proxy_port", "5080");
102+
params.put("pref_sip_user", "bob");
103+
params.put("pref_sip_password", "1234");
104+
device = RCClient.createDevice(params, this);
60105
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
61106
// we don't have a separate activity for the calls, so use the same intent both for calls and messages
62107
device.setPendingIntents(intent, intent);
63108

64-
connection = null;
109+
// Setup video stuff
110+
scalingType = VideoRendererGui.ScalingType.SCALE_ASPECT_FILL;
111+
videoView = (GLSurfaceView) findViewById(R.id.glview_call);
112+
// Create video renderers.
113+
VideoRendererGui.setView(videoView, new Runnable() {
114+
@Override
115+
public void run() {
116+
videoContextReady();
117+
}
118+
});
119+
remoteRender = VideoRendererGui.create(
120+
REMOTE_X, REMOTE_Y,
121+
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false);
122+
localRender = VideoRendererGui.create(
123+
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
124+
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true);
125+
}
65126

66-
params = new HashMap<String, String>();
67-
// CHANGEME: update the IP address to your Restcomm instance
68-
params.put("pref_proxy_ip", "54.225.212.193");
69-
params.put("pref_proxy_port", "5080");
70-
params.put("pref_sip_user", "bob");
71-
params.put("pref_sip_password", "1234");
72-
// register on startup
73-
device.updateParams(params);
127+
@Override
128+
protected void onDestroy() {
129+
super.onDestroy();
130+
// The activity is about to be destroyed.
131+
Log.i(TAG, "%% onDestroy");
132+
RCClient.shutdown();
133+
device = null;
134+
}
135+
136+
private void videoContextReady()
137+
{
138+
videoReady = true;
74139
}
75140

76141
@Override
@@ -106,7 +171,8 @@ public void onClick(View view) {
106171
HashMap<String, Object> connectParams = new HashMap<String, Object>();
107172
// CHANGEME: update the IP address to your Restcomm instance. Also, you can update the number
108173
// from '1235' to any Restcomm application you wish to reach
109-
connectParams.put("username", "sip:[email protected]:5080");
174+
connectParams.put("username", "sip:[email protected]:5080");
175+
connectParams.put("video-enabled", true);
110176

111177
// if you want to add custom SIP headers, please uncomment this
112178
//HashMap<String, String> sipHeaders = new HashMap<>();
@@ -194,6 +260,22 @@ public void onDisconnected(RCConnection connection)
194260
Log.i(TAG, "RCConnection disconnected");
195261
this.connection = null;
196262
pendingConnection = null;
263+
264+
// reside local renderer to take up all screen now that the call is over
265+
VideoRendererGui.update(localRender,
266+
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
267+
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true);
268+
269+
if (localVideoTrack != null) {
270+
271+
localVideoTrack.removeRenderer(localVideoRenderer);
272+
localVideoTrack = null;
273+
}
274+
275+
if (remoteVideoTrack != null) {
276+
remoteVideoTrack.removeRenderer(remoteVideoRenderer);
277+
remoteVideoTrack = null;
278+
}
197279
}
198280

199281
public void onDisconnected(RCConnection connection, int errorCode, String errorText) {
@@ -214,6 +296,34 @@ public void onDeclined(RCConnection connection) {
214296
this.connection = null;
215297
pendingConnection = null;
216298
}
299+
public void onReceiveLocalVideo(RCConnection connection, VideoTrack videoTrack) {
300+
Log.v(TAG, "onReceiveLocalVideo(), VideoTrack: " + videoTrack);
301+
if (videoTrack != null) {
302+
//show media on screen
303+
videoTrack.setEnabled(true);
304+
localVideoRenderer = new VideoRenderer(localRender);
305+
videoTrack.addRenderer(localVideoRenderer);
306+
localVideoTrack = videoTrack;
307+
}
308+
}
217309

218-
310+
public void onReceiveRemoteVideo(RCConnection connection, VideoTrack videoTrack) {
311+
Log.v(TAG, "onReceiveRemoteVideo(), VideoTrack: " + videoTrack);
312+
if (videoTrack != null) {
313+
//show media on screen
314+
videoTrack.setEnabled(true);
315+
remoteVideoRenderer = new VideoRenderer(remoteRender);
316+
videoTrack.addRenderer(remoteVideoRenderer);
317+
318+
VideoRendererGui.update(remoteRender,
319+
REMOTE_X, REMOTE_Y,
320+
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false);
321+
VideoRendererGui.update(localRender,
322+
LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
323+
LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
324+
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT, true);
325+
326+
remoteVideoTrack = videoTrack;
327+
}
328+
}
219329
}

Examples/restcomm-helloworld/app/src/main/res/layout/activity_main.xml

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
22
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
3-
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
4-
android:paddingRight="@dimen/activity_horizontal_margin"
5-
android:paddingTop="@dimen/activity_vertical_margin"
6-
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
3+
android:layout_height="match_parent"
4+
tools:context=".MainActivity">
5+
6+
<android.opengl.GLSurfaceView
7+
android:id="@+id/glview_call"
8+
android:layout_width="match_parent"
9+
android:layout_height="match_parent" />
710

811
<Button
912
android:layout_width="wrap_content"
1013
android:layout_height="wrap_content"
1114
android:text="Dial"
1215
android:id="@+id/button_dial"
16+
android:minWidth="100dp"
1317
android:layout_alignParentBottom="true"
1418
android:layout_alignParentLeft="true"
1519
android:layout_alignParentStart="true" />
@@ -19,6 +23,7 @@
1923
android:layout_height="wrap_content"
2024
android:text="Hang up"
2125
android:id="@+id/button_hangup"
26+
android:minWidth="100dp"
2227
android:layout_alignParentBottom="true"
2328
android:layout_alignParentRight="true"
2429
android:layout_alignParentEnd="true" />

restcomm.android.client.sdk/restcomm.android.client.sdk.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":restcomm.android.client.sdk" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-messenger" external.system.id="GRADLE" external.system.module.group="restcomm-messenger" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":restcomm.android.client.sdk" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-helloworld" external.system.id="GRADLE" external.system.module.group="restcomm-helloworld" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

sipua/sipua.iml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<module external.linked.project.id=":sipua" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-messenger" external.system.id="GRADLE" external.system.module.group="restcomm-messenger" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
2+
<module external.linked.project.id=":sipua" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/../Examples/restcomm-helloworld" external.system.id="GRADLE" external.system.module.group="restcomm-helloworld" external.system.module.version="unspecified" type="JAVA_MODULE" version="4">
33
<component name="FacetManager">
44
<facet type="android-gradle" name="Android-Gradle">
55
<configuration>

0 commit comments

Comments
 (0)