Skip to content

Commit

Permalink
fixed activity cycle issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pchab committed Mar 28, 2015
1 parent f91e3ef commit 40d6c97
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 33 deletions.
7 changes: 4 additions & 3 deletions AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="18" />
android:targetSdkVersion="21" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
Expand All @@ -17,10 +17,11 @@
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application android:label="@string/app_name" android:icon="@drawable/icon">
<application android:label="@string/app_name" android:icon="@drawable/icon" android:allowBackup="true">
<activity android:name="RTCActivity"
android:label="@string/app_name"
android:screenOrientation="landscape">
android:screenOrientation="fullUser"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand Down
41 changes: 28 additions & 13 deletions src/fr/pchab/AndroidRTC/RTCActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Point;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Toast;
import org.json.JSONException;
import org.webrtc.MediaStream;
Expand Down Expand Up @@ -49,6 +47,12 @@ public class RTCActivity extends Activity implements WebRtcClient.RTCListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(
LayoutParams.FLAG_FULLSCREEN
| LayoutParams.FLAG_KEEP_SCREEN_ON
| LayoutParams.FLAG_DISMISS_KEYGUARD
| LayoutParams.FLAG_SHOW_WHEN_LOCKED
| LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.main);
mSocketAddress = "http://" + getResources().getString(R.string.host);
mSocketAddress += (":" + getResources().getString(R.string.port) + "/");
Expand All @@ -63,7 +67,7 @@ public void run() {
}
});

// Camera display view
// local and remote render
remoteRender = VideoRendererGui.create(
REMOTE_X, REMOTE_Y,
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false);
Expand All @@ -87,12 +91,7 @@ private void init() {
true, false, displaySize.x, displaySize.y, 30, 1, VIDEO_CODEC_VP9, true, 1, AUDIO_CODEC_OPUS, true);
PeerConnectionFactory.initializeAndroidGlobals(this, true, true,
params.videoCodecHwAcceleration, VideoRendererGui.getEGLContext());
client = new WebRtcClient(this, mSocketAddress);
}

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
client = new WebRtcClient(this, mSocketAddress, params);
}

@Override
Expand All @@ -113,6 +112,14 @@ public void onResume() {
}
}

@Override
public void onDestroy() {
if(client != null) {
client.disconnect();
}
super.onDestroy();
}

@Override
public void onCallReady(String callId) {
if (callerId != null) {
Expand Down Expand Up @@ -147,7 +154,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {

public void startCam() {
// Camera settings
client.setCamera("640", "480");
client.setCamera();
client.start("android_test");
}

Expand All @@ -165,8 +172,8 @@ public void run() {
public void onLocalStream(MediaStream localStream) {
localStream.videoTracks.get(0).addRenderer(new VideoRenderer(localRender));
VideoRendererGui.update(localRender,
LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING,
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
}

Expand All @@ -176,10 +183,18 @@ public void onAddRemoteStream(MediaStream remoteStream, int endPoint) {
VideoRendererGui.update(remoteRender,
REMOTE_X, REMOTE_Y,
REMOTE_WIDTH, REMOTE_HEIGHT, scalingType);
VideoRendererGui.update(localRender,
LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
}

@Override
public void onRemoveRemoteStream(MediaStream remoteStream, int endPoint) {
VideoRendererGui.remove(remoteRender);
VideoRendererGui.update(localRender,
LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING,
VideoRendererGui.ScalingType.SCALE_ASPECT_FIT);
}
}
47 changes: 30 additions & 17 deletions src/fr/pchab/AndroidRTC/WebRtcClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;

import com.github.nkzawa.socketio.client.IO;
Expand All @@ -19,6 +20,7 @@ class WebRtcClient {
private PeerConnectionFactory factory;
private HashMap<String, Peer> peers = new HashMap<>();
private LinkedList<PeerConnection.IceServer> iceServers = new LinkedList<>();
private PeerConnectionParameters pcParams;
private MediaConstraints pcConstraints = new MediaConstraints();
private MediaStream localMS;
private VideoSource videoSource;
Expand Down Expand Up @@ -211,7 +213,6 @@ public void onAddStream(MediaStream mediaStream) {
@Override
public void onRemoveStream(MediaStream mediaStream) {
mListener.onRemoveRemoteStream(mediaStream, endPoint);

removePeer(id);
}

Expand All @@ -235,8 +236,9 @@ public Peer(String id, int endPoint) {
}
}

public WebRtcClient(RTCListener listener, String host) {
public WebRtcClient(RTCListener listener, String host, PeerConnectionParameters params) {
mListener = listener;
pcParams = params;
factory = new PeerConnectionFactory();
MessageHandler messageHandler = new MessageHandler();

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

public void setCamera(String height, String width){
MediaConstraints videoConstraints = new MediaConstraints();
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", height));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", width));
public void setCamera(){
localMS = factory.createLocalMediaStream("ARDAMS");
if(pcParams.videoCallEnabled){
MediaConstraints videoConstraints = new MediaConstraints();
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxHeight", Integer.toString(pcParams.videoHeight)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxWidth", Integer.toString(pcParams.videoWidth)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("maxFrameRate", Integer.toString(pcParams.videoFps)));
videoConstraints.mandatory.add(new MediaConstraints.KeyValuePair("minFrameRate", Integer.toString(pcParams.videoFps)));

videoSource = factory.createVideoSource(getVideoCapturer(), videoConstraints);
localMS.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));
}

videoSource = factory.createVideoSource(getVideoCapturer(), videoConstraints);
AudioSource audioSource = factory.createAudioSource(new MediaConstraints());
localMS = factory.createLocalMediaStream("ARDAMS");
localMS.addTrack(factory.createVideoTrack("ARDAMSv0", videoSource));
localMS.addTrack(factory.createAudioTrack("ARDAMSa0", audioSource));

mListener.onLocalStream(localMS);
}

public void stopVideoSource() {
if(videoSource != null) {
videoSource.stop();
}
if(videoSource != null) videoSource.stop();
}

public void restartVideoSource() {
if(videoSource != null) {
videoSource.restart();
if(videoSource != null) videoSource.restart();
}

public void disconnect() {
Iterator it = peers.values().iterator();
while(it.hasNext()){
Peer peer = (Peer) it.next();
peer.pc.dispose();
}
videoSource.dispose();
factory.dispose();
client.disconnect();
client.close();
}

private int findEndPoint() {
for(int i = 0; i < MAX_PEER; i++) {
if(!endPoints[i]) return i;
}
for(int i = 0; i < MAX_PEER; i++) if (!endPoints[i]) return i;
return MAX_PEER;
}

Expand Down

0 comments on commit 40d6c97

Please sign in to comment.