Skip to content

Commit 40d6c97

Browse files
committed
fixed activity cycle issues
1 parent f91e3ef commit 40d6c97

3 files changed

Lines changed: 62 additions & 33 deletions

File tree

AndroidManifest.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<uses-feature android:name="android.hardware.camera.autofocus" />
99
<uses-sdk
1010
android:minSdkVersion="13"
11-
android:targetSdkVersion="18" />
11+
android:targetSdkVersion="21" />
1212

1313
<uses-permission android:name="android.permission.CAMERA" />
1414
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
@@ -17,10 +17,11 @@
1717
<uses-permission android:name="android.permission.WAKE_LOCK" />
1818
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1919

20-
<application android:label="@string/app_name" android:icon="@drawable/icon">
20+
<application android:label="@string/app_name" android:icon="@drawable/icon" android:allowBackup="true">
2121
<activity android:name="RTCActivity"
2222
android:label="@string/app_name"
23-
android:screenOrientation="landscape">
23+
android:screenOrientation="fullUser"
24+
android:configChanges="orientation|screenSize">
2425
<intent-filter>
2526
<action android:name="android.intent.action.MAIN"/>
2627
<category android:name="android.intent.category.LAUNCHER"/>

src/fr/pchab/AndroidRTC/RTCActivity.java

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
import android.app.Activity;
44
import android.content.Intent;
5-
import android.content.pm.ActivityInfo;
6-
import android.content.res.Configuration;
75
import android.graphics.Point;
86
import android.opengl.GLSurfaceView;
97
import android.os.Bundle;
10-
import android.util.Log;
118
import android.view.Window;
9+
import android.view.WindowManager.LayoutParams;
1210
import android.widget.Toast;
1311
import org.json.JSONException;
1412
import org.webrtc.MediaStream;
@@ -49,6 +47,12 @@ public class RTCActivity extends Activity implements WebRtcClient.RTCListener {
4947
public void onCreate(Bundle savedInstanceState) {
5048
super.onCreate(savedInstanceState);
5149
requestWindowFeature(Window.FEATURE_NO_TITLE);
50+
getWindow().addFlags(
51+
LayoutParams.FLAG_FULLSCREEN
52+
| LayoutParams.FLAG_KEEP_SCREEN_ON
53+
| LayoutParams.FLAG_DISMISS_KEYGUARD
54+
| LayoutParams.FLAG_SHOW_WHEN_LOCKED
55+
| LayoutParams.FLAG_TURN_SCREEN_ON);
5256
setContentView(R.layout.main);
5357
mSocketAddress = "http://" + getResources().getString(R.string.host);
5458
mSocketAddress += (":" + getResources().getString(R.string.port) + "/");
@@ -63,7 +67,7 @@ public void run() {
6367
}
6468
});
6569

66-
// Camera display view
70+
// local and remote render
6771
remoteRender = VideoRendererGui.create(
6872
REMOTE_X, REMOTE_Y,
6973
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false);
@@ -87,12 +91,7 @@ private void init() {
8791
true, false, displaySize.x, displaySize.y, 30, 1, VIDEO_CODEC_VP9, true, 1, AUDIO_CODEC_OPUS, true);
8892
PeerConnectionFactory.initializeAndroidGlobals(this, true, true,
8993
params.videoCodecHwAcceleration, VideoRendererGui.getEGLContext());
90-
client = new WebRtcClient(this, mSocketAddress);
91-
}
92-
93-
public void onConfigurationChanged(Configuration newConfig) {
94-
super.onConfigurationChanged(newConfig);
95-
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
94+
client = new WebRtcClient(this, mSocketAddress, params);
9695
}
9796

9897
@Override
@@ -113,6 +112,14 @@ public void onResume() {
113112
}
114113
}
115114

115+
@Override
116+
public void onDestroy() {
117+
if(client != null) {
118+
client.disconnect();
119+
}
120+
super.onDestroy();
121+
}
122+
116123
@Override
117124
public void onCallReady(String callId) {
118125
if (callerId != null) {
@@ -147,7 +154,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
147154

148155
public void startCam() {
149156
// Camera settings
150-
client.setCamera("640", "480");
157+
client.setCamera();
151158
client.start("android_test");
152159
}
153160

@@ -165,8 +172,8 @@ public void run() {
165172
public void onLocalStream(MediaStream localStream) {
166173
localStream.videoTracks.get(0).addRenderer(new VideoRenderer(localRender));
167174
VideoRendererGui.update(localRender,
168-
LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
169-
LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
175+
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
176+
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING,
170177
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
171178
}
172179

@@ -176,10 +183,18 @@ public void onAddRemoteStream(MediaStream remoteStream, int endPoint) {
176183
VideoRendererGui.update(remoteRender,
177184
REMOTE_X, REMOTE_Y,
178185
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType);
186+
VideoRendererGui.update(localRender,
187+
LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
188+
LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
189+
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
179190
}
180191

181192
@Override
182193
public void onRemoveRemoteStream(MediaStream remoteStream, int endPoint) {
183194
VideoRendererGui.remove(remoteRender);
195+
VideoRendererGui.update(localRender,
196+
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
197+
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING,
198+
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
184199
}
185200
}

src/fr/pchab/AndroidRTC/WebRtcClient.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.net.URISyntaxException;
44
import java.util.HashMap;
5+
import java.util.Iterator;
56
import java.util.LinkedList;
67

78
import com.github.nkzawa.socketio.client.IO;
@@ -19,6 +20,7 @@ class WebRtcClient {
1920
private PeerConnectionFactory factory;
2021
private HashMap<String, Peer> peers = new HashMap<>();
2122
private LinkedList<PeerConnection.IceServer> iceServers = new LinkedList<>();
23+
private PeerConnectionParameters pcParams;
2224
private MediaConstraints pcConstraints = new MediaConstraints();
2325
private MediaStream localMS;
2426
private VideoSource videoSource;
@@ -211,7 +213,6 @@ public void onAddStream(MediaStream mediaStream) {
211213
@Override
212214
public void onRemoveStream(MediaStream mediaStream) {
213215
mListener.onRemoveRemoteStream(mediaStream, endPoint);
214-
215216
removePeer(id);
216217
}
217218

@@ -235,8 +236,9 @@ public Peer(String id, int endPoint) {
235236
}
236237
}
237238

238-
public WebRtcClient(RTCListener listener, String host) {
239+
public WebRtcClient(RTCListener listener, String host, PeerConnectionParameters params) {
239240
mListener = listener;
241+
pcParams = params;
240242
factory = new PeerConnectionFactory();
241243
MessageHandler messageHandler = new MessageHandler();
242244

@@ -257,36 +259,47 @@ public WebRtcClient(RTCListener listener, String host) {
257259
pcConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
258260
}
259261

260-
public void setCamera(String height, String width){
261-
MediaConstraints videoConstraints = new MediaConstraints();
262-
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", height));
263-
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", width));
262+
public void setCamera(){
263+
localMS = factory.createLocalMediaStream("ARDAMS");
264+
if(pcParams.videoCallEnabled){
265+
MediaConstraints videoConstraints = new MediaConstraints();
266+
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", Integer.toString(pcParams.videoHeight)));
267+
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", Integer.toString(pcParams.videoWidth)));
268+
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxFrameRate", Integer.toString(pcParams.videoFps)));
269+
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minFrameRate", Integer.toString(pcParams.videoFps)));
270+
271+
videoSource = factory.createVideoSource(getVideoCapturer(), videoConstraints);
272+
localMS.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));
273+
}
264274

265-
videoSource = factory.createVideoSource(getVideoCapturer(), videoConstraints);
266275
AudioSource audioSource = factory.createAudioSource(new MediaConstraints());
267-
localMS = factory.createLocalMediaStream("ARDAMS");
268-
localMS.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));
269276
localMS.addTrack(factory.createAudioTrack("ARDAMSa0", audioSource));
270277

271278
mListener.onLocalStream(localMS);
272279
}
273280

274281
public void stopVideoSource() {
275-
if(videoSource != null) {
276-
videoSource.stop();
277-
}
282+
if(videoSource != null) videoSource.stop();
278283
}
279284

280285
public void restartVideoSource() {
281-
if(videoSource != null) {
282-
videoSource.restart();
286+
if(videoSource != null) videoSource.restart();
287+
}
288+
289+
public void disconnect() {
290+
Iterator it = peers.values().iterator();
291+
while(it.hasNext()){
292+
Peer peer = (Peer) it.next();
293+
peer.pc.dispose();
283294
}
295+
videoSource.dispose();
296+
factory.dispose();
297+
client.disconnect();
298+
client.close();
284299
}
285300

286301
private int findEndPoint() {
287-
for(int i = 0; i < MAX_PEER; i++) {
288-
if(!endPoints[i]) return i;
289-
}
302+
for(int i = 0; i < MAX_PEER; i++) if (!endPoints[i]) return i;
290303
return MAX_PEER;
291304
}
292305

0 commit comments

Comments
 (0)