From d3e603a5ceb70888f429ce8a22aef424f097c4f1 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Wed, 8 Nov 2017 20:06:41 +0100 Subject: [PATCH] Work on webrtc Signed-off-by: Mario Danic --- app/build.gradle | 2 + app/src/main/AndroidManifest.xml | 1 + .../talk/activities/CallActivity.java | 38 +++++++- .../talk/controllers/CallController.java | 69 --------------- .../nextcloud/talk/utils/DisplayHelper.java | 37 ++++++++ .../webrtc/MagicPeerConnectionObserver.java | 87 +++++++++++++++++++ .../talk/webrtc/MagicSdpObserver.java | 48 ++++++++++ app/src/main/res/layout/activity_call.xml | 25 ++++-- app/src/main/res/layout/controller_call.xml | 39 --------- 9 files changed, 226 insertions(+), 120 deletions(-) delete mode 100644 app/src/main/java/com/nextcloud/talk/controllers/CallController.java create mode 100644 app/src/main/java/com/nextcloud/talk/utils/DisplayHelper.java create mode 100644 app/src/main/java/com/nextcloud/talk/webrtc/MagicPeerConnectionObserver.java create mode 100644 app/src/main/java/com/nextcloud/talk/webrtc/MagicSdpObserver.java delete mode 100644 app/src/main/res/layout/controller_call.xml diff --git a/app/build.gradle b/app/build.gradle index 81bb84981..8a8d3f327 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -138,6 +138,8 @@ dependencies { implementation 'com.yarolegovich:lovelyinput:1.0.2' implementation 'com.yarolegovich:mp:1.0.8' + compile 'gun0912.ted:tedpermission:2.0.3' + testImplementation 'junit:junit:4.12' androidTestImplementation ('com.android.support.test.espresso:espresso-core:3.0.1', { exclude group: 'com.android.support', module: 'support-annotations' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8fbf79ee1..e0b4898c7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -4,6 +4,7 @@ + diff --git a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java index 6f7b3aed8..67fe5ad34 100644 --- a/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java +++ b/app/src/main/java/com/nextcloud/talk/activities/CallActivity.java @@ -16,13 +16,19 @@ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . + * + * Inspired by: + * - Google samples + * - https://github.com/vivek1794/webrtc-android-codelab (MIT licence) */ package com.nextcloud.talk.activities; import android.Manifest; +import android.content.res.Resources; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; +import android.util.TypedValue; import android.view.View; import android.view.Window; import android.view.WindowManager; @@ -38,6 +44,7 @@ import org.webrtc.AudioSource; import org.webrtc.AudioTrack; import org.webrtc.Camera1Enumerator; import org.webrtc.CameraEnumerator; +import org.webrtc.EglBase; import org.webrtc.IceCandidate; import org.webrtc.Logging; import org.webrtc.MediaConstraints; @@ -79,6 +86,7 @@ public class CallActivity extends AppCompatActivity { VideoTrack localVideoTrack; AudioSource audioSource; AudioTrack localAudioTrack; + VideoCapturer videoCapturer; VideoRenderer localRenderer; VideoRenderer remoteRenderer; @@ -116,6 +124,8 @@ public class CallActivity extends AppCompatActivity { } }; + initViews(); + TedPermission.with(this) .setPermissionListener(permissionlistener) .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]") @@ -123,13 +133,13 @@ public class CallActivity extends AppCompatActivity { Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.ACCESS_NETWORK_STATE, Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.INTERNET) .check(); + } private VideoCapturer createVideoCapturer() { - VideoCapturer videoCapturer; videoCapturer = createCameraCapturer(new Camera1Enumerator(false)); return videoCapturer; } @@ -166,6 +176,16 @@ public class CallActivity extends AppCompatActivity { return null; } + public void initViews() { + pipVideoView.setMirror(true); + fullScreenVideoView.setMirror(false); + EglBase rootEglBase = EglBase.create(); + pipVideoView.init(rootEglBase.getEglBaseContext(), null); + pipVideoView.setZOrderMediaOverlay(true); + fullScreenVideoView.init(rootEglBase.getEglBaseContext(), null); + fullScreenVideoView.setZOrderMediaOverlay(true); + } + public void start() { //Initialize PeerConnectionFactory globals. //Params are context, initAudio,initVideo and videoCodecHwAcceleration @@ -191,12 +211,17 @@ public class CallActivity extends AppCompatActivity { audioSource = peerConnectionFactory.createAudioSource(audioConstraints); localAudioTrack = peerConnectionFactory.createAudioTrack("101", audioSource); + Resources r = getResources(); + int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 120, r.getDisplayMetrics()); + videoCapturerAndroid.startCapture(px, px, 30); + //create a videoRenderer based on SurfaceViewRenderer instance localRenderer = new VideoRenderer(pipVideoView); // And finally, with our VideoRenderer ready, we // can add our renderer to the VideoTrack. localVideoTrack.addRenderer(localRenderer); + } @@ -268,9 +293,9 @@ public class CallActivity extends AppCompatActivity { localPeer.setRemoteDescription(new MagicSdpObserver(), sessionDescription); } - },new MediaConstraints()); + }, new MediaConstraints()); } - },sdpConstraints); + }, sdpConstraints); } @@ -284,6 +309,13 @@ public class CallActivity extends AppCompatActivity { remotePeer.close(); remotePeer = null; } + + if (videoCapturer != null) { + videoCapturer.dispose(); + } + + pipVideoView.release(); + fullScreenVideoView.release(); } private void gotRemoteStream(MediaStream stream) { diff --git a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java b/app/src/main/java/com/nextcloud/talk/controllers/CallController.java deleted file mode 100644 index cc081cb39..000000000 --- a/app/src/main/java/com/nextcloud/talk/controllers/CallController.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Nextcloud Talk application - * - * @author Mario Danic - * Copyright (C) 2017 Mario Danic - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package com.nextcloud.talk.controllers; - -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -import com.nextcloud.talk.R; -import com.nextcloud.talk.application.NextcloudTalkApplication; -import com.nextcloud.talk.controllers.base.RefWatchingController; - -import org.webrtc.SurfaceViewRenderer; - -import autodagger.AutoInjector; -import butterknife.BindView; - -@AutoInjector(NextcloudTalkApplication.class) -public class CallController extends RefWatchingController { - private static final String TAG = "CallController"; - - @BindView(R.id.fullscreen_video_view) - SurfaceViewRenderer fullScreenVideo; - - @BindView(R.id.pip_video_view) - SurfaceViewRenderer pipVideoView; - - private String roomToken; - private String userDisplayName; - - public CallController(Bundle args) { - super(args); - this.roomToken = args.getString("roomToken", ""); - this.userDisplayName = args.getString("userDisplayName"); - - } - - @Override - protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) { - return inflater.inflate(R.layout.controller_call, container, false); - } - - @Override - protected void onViewBound(@NonNull View view) { - super.onViewBound(view); - NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); - } - -} diff --git a/app/src/main/java/com/nextcloud/talk/utils/DisplayHelper.java b/app/src/main/java/com/nextcloud/talk/utils/DisplayHelper.java new file mode 100644 index 000000000..c885c77c9 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/DisplayHelper.java @@ -0,0 +1,37 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.utils; + +import android.content.Context; +import android.content.res.Resources; +import android.util.DisplayMetrics; + +public class DisplayHelper { + + private static final String TAG = "DIsplayHelper"; + + public static float convertDpToPixel(float dp, Context context){ + Resources resources = context.getResources(); + DisplayMetrics metrics = resources.getDisplayMetrics(); + float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT); + return px; + } +} diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/MagicPeerConnectionObserver.java b/app/src/main/java/com/nextcloud/talk/webrtc/MagicPeerConnectionObserver.java new file mode 100644 index 000000000..8ca7b7073 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/webrtc/MagicPeerConnectionObserver.java @@ -0,0 +1,87 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.webrtc; + +import org.webrtc.DataChannel; +import org.webrtc.IceCandidate; +import org.webrtc.MediaStream; +import org.webrtc.PeerConnection; +import org.webrtc.RtpReceiver; + + +public class MagicPeerConnectionObserver implements PeerConnection.Observer { + private static final String TAG = "MagicPeerConnectionObserver"; + + @Override + public void onSignalingChange(PeerConnection.SignalingState signalingState) { + + } + + @Override + public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) { + + } + + @Override + public void onIceConnectionReceivingChange(boolean b) { + + } + + @Override + public void onIceGatheringChange(PeerConnection.IceGatheringState iceGatheringState) { + + } + + @Override + public void onIceCandidate(IceCandidate iceCandidate) { + + } + + @Override + public void onIceCandidatesRemoved(IceCandidate[] iceCandidates) { + + } + + @Override + public void onAddStream(MediaStream mediaStream) { + + } + + @Override + public void onRemoveStream(MediaStream mediaStream) { + + } + + @Override + public void onDataChannel(DataChannel dataChannel) { + + } + + @Override + public void onRenegotiationNeeded() { + + } + + @Override + public void onAddTrack(RtpReceiver rtpReceiver, MediaStream[] mediaStreams) { + + } +} diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/MagicSdpObserver.java b/app/src/main/java/com/nextcloud/talk/webrtc/MagicSdpObserver.java new file mode 100644 index 000000000..c6cf44b79 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/webrtc/MagicSdpObserver.java @@ -0,0 +1,48 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.webrtc; + +import org.webrtc.SdpObserver; +import org.webrtc.SessionDescription; + +public class MagicSdpObserver implements SdpObserver { + private static final String TAG = "MagicSdpObserver"; + + @Override + public void onCreateSuccess(SessionDescription sessionDescription) { + + } + + @Override + public void onSetSuccess() { + + } + + @Override + public void onCreateFailure(String s) { + + } + + @Override + public void onSetFailure(String s) { + + } +} diff --git a/app/src/main/res/layout/activity_call.xml b/app/src/main/res/layout/activity_call.xml index cf1e604f8..33c31309c 100644 --- a/app/src/main/res/layout/activity_call.xml +++ b/app/src/main/res/layout/activity_call.xml @@ -20,16 +20,23 @@ --> + xmlns:tools="http://schemas.android.com/tools" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:fitsSystemWindows="true" + tools:context=".activities.CallActivity"> - + + + android:layout_height="match_parent"/> \ No newline at end of file diff --git a/app/src/main/res/layout/controller_call.xml b/app/src/main/res/layout/controller_call.xml deleted file mode 100644 index 2db967632..000000000 --- a/app/src/main/res/layout/controller_call.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - - - - - - - - \ No newline at end of file