1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780 |
- /*
- * Nextcloud Talk application
- *
- * @author Mario Danic
- * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
- *
- * 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 <http://www.gnu.org/licenses/>.
- */
- package com.nextcloud.talk.controllers;
- import android.Manifest;
- import android.animation.Animator;
- import android.animation.AnimatorListenerAdapter;
- import android.annotation.SuppressLint;
- import android.content.res.Configuration;
- import android.graphics.Color;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import androidx.annotation.NonNull;
- import androidx.annotation.Nullable;
- import androidx.appcompat.app.AppCompatActivity;
- import android.text.TextUtils;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.ImageView;
- import android.widget.LinearLayout;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import com.bluelinelabs.logansquare.LoganSquare;
- import com.bumptech.glide.load.engine.DiskCacheStrategy;
- import com.bumptech.glide.load.resource.bitmap.CircleCrop;
- import com.bumptech.glide.request.RequestOptions;
- import com.nextcloud.talk.R;
- import com.nextcloud.talk.api.NcApi;
- import com.nextcloud.talk.application.NextcloudTalkApplication;
- import com.nextcloud.talk.controllers.base.BaseController;
- import com.nextcloud.talk.events.ConfigurationChangeEvent;
- import com.nextcloud.talk.events.MediaStreamEvent;
- import com.nextcloud.talk.events.PeerConnectionEvent;
- import com.nextcloud.talk.events.SessionDescriptionSendEvent;
- import com.nextcloud.talk.models.database.UserEntity;
- import com.nextcloud.talk.models.json.call.CallOverall;
- import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall;
- import com.nextcloud.talk.models.json.generic.GenericOverall;
- import com.nextcloud.talk.models.json.participants.Participant;
- import com.nextcloud.talk.models.json.participants.ParticipantsOverall;
- import com.nextcloud.talk.models.json.rooms.Conversation;
- import com.nextcloud.talk.models.json.rooms.RoomsOverall;
- import com.nextcloud.talk.models.json.signaling.DataChannelMessage;
- import com.nextcloud.talk.models.json.signaling.NCIceCandidate;
- import com.nextcloud.talk.models.json.signaling.NCMessagePayload;
- import com.nextcloud.talk.models.json.signaling.NCMessageWrapper;
- import com.nextcloud.talk.models.json.signaling.NCSignalingMessage;
- import com.nextcloud.talk.models.json.signaling.Signaling;
- import com.nextcloud.talk.models.json.signaling.SignalingOverall;
- import com.nextcloud.talk.models.json.signaling.settings.IceServer;
- import com.nextcloud.talk.models.json.signaling.settings.SignalingSettingsOverall;
- import com.nextcloud.talk.utils.ApiUtils;
- import com.nextcloud.talk.utils.MagicFlipView;
- import com.nextcloud.talk.utils.NotificationUtils;
- import com.nextcloud.talk.utils.animations.PulseAnimation;
- import com.nextcloud.talk.utils.bundle.BundleKeys;
- import com.nextcloud.talk.utils.database.user.UserUtils;
- import com.nextcloud.talk.utils.glide.GlideApp;
- import com.nextcloud.talk.utils.preferences.AppPreferences;
- import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
- import com.nextcloud.talk.webrtc.MagicAudioManager;
- import com.nextcloud.talk.webrtc.MagicPeerConnectionWrapper;
- import com.nextcloud.talk.webrtc.MagicWebRTCUtils;
- import com.wooplr.spotlight.SpotlightView;
- import org.apache.commons.lang3.StringEscapeUtils;
- import org.greenrobot.eventbus.EventBus;
- import org.greenrobot.eventbus.Subscribe;
- import org.greenrobot.eventbus.ThreadMode;
- import org.parceler.Parcels;
- import org.webrtc.AudioSource;
- import org.webrtc.AudioTrack;
- import org.webrtc.Camera1Enumerator;
- import org.webrtc.Camera2Enumerator;
- import org.webrtc.CameraEnumerator;
- import org.webrtc.CameraVideoCapturer;
- import org.webrtc.EglBase;
- import org.webrtc.IceCandidate;
- import org.webrtc.Logging;
- import org.webrtc.MediaConstraints;
- import org.webrtc.MediaStream;
- import org.webrtc.PeerConnection;
- import org.webrtc.PeerConnectionFactory;
- import org.webrtc.RendererCommon;
- import org.webrtc.SessionDescription;
- import org.webrtc.SurfaceViewRenderer;
- import org.webrtc.VideoCapturer;
- import org.webrtc.VideoSource;
- import org.webrtc.VideoTrack;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.concurrent.TimeUnit;
- import javax.inject.Inject;
- import autodagger.AutoInjector;
- import butterknife.BindView;
- import butterknife.OnClick;
- import butterknife.OnLongClick;
- import eu.davidea.flipview.FlipView;
- import io.reactivex.Observer;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.disposables.Disposable;
- import io.reactivex.schedulers.Schedulers;
- import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied;
- import me.zhanghai.android.effortlesspermissions.EffortlessPermissions;
- import me.zhanghai.android.effortlesspermissions.OpenAppDetailsDialogFragment;
- import okhttp3.Cache;
- import pub.devrel.easypermissions.AfterPermissionGranted;
- @AutoInjector(NextcloudTalkApplication.class)
- public class CallController extends BaseController {
- private static final String TAG = "CallController";
- private static final String[] PERMISSIONS_CALL = {
- android.Manifest.permission.CAMERA,
- android.Manifest.permission.RECORD_AUDIO,
- };
- private static final String[] PERMISSIONS_CAMERA = {
- Manifest.permission.CAMERA
- };
- private static final String[] PERMISSIONS_MICROPHONE = {
- Manifest.permission.RECORD_AUDIO
- };
- @BindView(R.id.callControlEnableSpeaker)
- MagicFlipView callControlEnableSpeaker;
- @BindView(R.id.pip_video_view)
- SurfaceViewRenderer pipVideoView;
- @BindView(R.id.relative_layout)
- RelativeLayout relativeLayout;
- @BindView(R.id.remote_renderers_layout)
- LinearLayout remoteRenderersLayout;
- @BindView(R.id.callControlsLinearLayoutView)
- LinearLayout callControls;
- @BindView(R.id.call_control_microphone)
- FlipView microphoneControlButton;
- @BindView(R.id.call_control_camera)
- FlipView cameraControlButton;
- @BindView(R.id.call_control_switch_camera)
- FlipView cameraSwitchButton;
- @BindView(R.id.connectingTextView)
- TextView connectingTextView;
- @BindView(R.id.connectingRelativeLayoutView)
- RelativeLayout connectingView;
- @BindView(R.id.conversationRelativeLayoutView)
- RelativeLayout conversationView;
- @Inject
- NcApi ncApi;
- @Inject
- EventBus eventBus;
- @Inject
- UserUtils userUtils;
- @Inject
- AppPreferences appPreferences;
- @Inject
- Cache cache;
- private PeerConnectionFactory peerConnectionFactory;
- private MediaConstraints audioConstraints;
- private MediaConstraints videoConstraints;
- private MediaConstraints sdpConstraints;
- private MagicAudioManager audioManager;
- private VideoSource videoSource;
- private VideoTrack localVideoTrack;
- private AudioSource audioSource;
- private AudioTrack localAudioTrack;
- private VideoCapturer videoCapturer;
- private EglBase rootEglBase;
- private boolean leavingCall = false;
- private boolean inCall = false;
- private Disposable signalingDisposable;
- private Disposable pingDisposable;
- private List<PeerConnection.IceServer> iceServers;
- private CameraEnumerator cameraEnumerator;
- private String roomToken;
- private UserEntity conversationUser;
- private String callSession;
- private MediaStream localMediaStream;
- private String credentials;
- private List<MagicPeerConnectionWrapper> magicPeerConnectionWrapperList = new ArrayList<>();
- private Map<String, Participant> participantMap = new HashMap<>();
- private boolean videoOn = false;
- private boolean audioOn = false;
- private boolean isMultiSession = false;
- private boolean needsPing = true;
- private boolean isVoiceOnlyCall;
- private boolean isFromNotification;
- private Handler callControlHandler = new Handler();
- private Handler cameraSwitchHandler = new Handler();
- private boolean isPTTActive = false;
- private PulseAnimation pulseAnimation;
- private View.OnClickListener videoOnClickListener;
- private String baseUrl;
- private String roomId;
- private SpotlightView spotlightView;
- public CallController(Bundle args) {
- super(args);
- NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
- roomId = args.getString(BundleKeys.KEY_ROOM_ID, "");
- roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "");
- conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
- if (conversationUser == null) {
- conversationUser = userUtils.getCurrentUser();
- }
- callSession = args.getString(BundleKeys.KEY_CALL_SESSION, "0");
- credentials = ApiUtils.getCredentials(conversationUser.getUsername(), conversationUser.getToken());
- isVoiceOnlyCall = args.getBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, false);
- if (conversationUser.getUserId().equals("?")) {
- credentials = null;
- }
- baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
- if (TextUtils.isEmpty(baseUrl)) {
- baseUrl = conversationUser.getBaseUrl();
- }
- isFromNotification = TextUtils.isEmpty(roomToken);
- }
- @Override
- protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
- return inflater.inflate(R.layout.controller_call, container, false);
- }
- private void createCameraEnumerator() {
- if (getActivity() != null) {
- boolean camera2EnumeratorIsSupported = false;
- try {
- camera2EnumeratorIsSupported = Camera2Enumerator.isSupported(getActivity());
- } catch (final Throwable throwable) {
- Log.w(TAG, "Camera2Enumator threw an error");
- }
- if (camera2EnumeratorIsSupported) {
- cameraEnumerator = new Camera2Enumerator(getActivity());
- } else {
- cameraEnumerator = new Camera1Enumerator(MagicWebRTCUtils.shouldEnableVideoHardwareAcceleration());
- }
- }
- }
- @Override
- protected void onViewBound(@NonNull View view) {
- super.onViewBound(view);
- microphoneControlButton.setOnTouchListener(new MicrophoneButtonTouchListener());
- videoOnClickListener = new VideoClickListener();
- pulseAnimation = PulseAnimation.create().with(microphoneControlButton.getFrontImageView())
- .setDuration(310)
- .setRepeatCount(PulseAnimation.INFINITE)
- .setRepeatMode(PulseAnimation.REVERSE);
- try {
- cache.evictAll();
- } catch (IOException e) {
- Log.e(TAG, "Failed to evict cache");
- }
- if (isVoiceOnlyCall) {
- callControlEnableSpeaker.setVisibility(View.VISIBLE);
- }
- callControls.setZ(100.0f);
- basicInitialization();
- if (isFromNotification) {
- handleFromNotification();
- } else {
- initViews();
- checkPermissions();
- }
- }
- private void basicInitialization() {
- rootEglBase = EglBase.create();
- createCameraEnumerator();
- //Create a new PeerConnectionFactory instance.
- PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
- peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory();
- peerConnectionFactory.setVideoHwAccelerationOptions(rootEglBase.getEglBaseContext(),
- rootEglBase.getEglBaseContext());
- //Create MediaConstraints - Will be useful for specifying video and audio constraints.
- audioConstraints = new MediaConstraints();
- videoConstraints = new MediaConstraints();
- localMediaStream = peerConnectionFactory.createLocalMediaStream("NCMS");
- // Create and audio manager that will take care of audio routing,
- // audio modes, audio device enumeration etc.
- audioManager = MagicAudioManager.create(getApplicationContext(), !isVoiceOnlyCall);
- // Store existing audio settings and change audio mode to
- // MODE_IN_COMMUNICATION for best possible VoIP performance.
- Log.d(TAG, "Starting the audio manager...");
- audioManager.start(this::onAudioManagerDevicesChanged);
- iceServers = new ArrayList<>();
- //create sdpConstraints
- sdpConstraints = new MediaConstraints();
- sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
- String offerToReceiveVideoString = "true";
- if (isVoiceOnlyCall) {
- offerToReceiveVideoString = "false";
- }
- sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo",
- offerToReceiveVideoString));
- sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("internalSctpDataChannels", "true"));
- sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
- if (!isVoiceOnlyCall) {
- cameraInitialization();
- }
- microphoneInitialization();
- }
- private void handleFromNotification() {
- ncApi.getRooms(credentials, ApiUtils.getUrlForGetRooms(baseUrl))
- .retry(3)
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<RoomsOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(RoomsOverall roomsOverall) {
- for (Conversation conversation : roomsOverall.getOcs().getData()) {
- if (roomId.equals(conversation.getRoomId())) {
- roomToken = conversation.getToken();
- break;
- }
- }
- initViews();
- checkPermissions();
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void initViews() {
- if (isVoiceOnlyCall) {
- cameraSwitchButton.setVisibility(View.GONE);
- cameraControlButton.setVisibility(View.GONE);
- pipVideoView.setVisibility(View.GONE);
- } else {
- if (cameraEnumerator.getDeviceNames().length < 2) {
- cameraSwitchButton.setVisibility(View.GONE);
- }
- // setting this to true because it's not shown by default
- pipVideoView.setMirror(false);
- pipVideoView.init(rootEglBase.getEglBaseContext(), null);
- pipVideoView.setZOrderMediaOverlay(true);
- // disabled because it causes some devices to crash
- pipVideoView.setEnableHardwareScaler(false);
- pipVideoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
- }
- }
- private void checkPermissions() {
- if (isVoiceOnlyCall) {
- onMicrophoneClick();
- } else if (getActivity() != null) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(PERMISSIONS_CALL, 100);
- } else {
- onRequestPermissionsResult(100, PERMISSIONS_CALL, new int[]{1, 1});
- }
- }
- }
- @AfterPermissionGranted(100)
- private void onPermissionsGranted() {
- if (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CALL)) {
- if (!videoOn && !isVoiceOnlyCall) {
- onCameraClick();
- }
- if (!audioOn) {
- onMicrophoneClick();
- }
- if (!isVoiceOnlyCall) {
- if (cameraEnumerator.getDeviceNames().length == 0) {
- cameraControlButton.setVisibility(View.GONE);
- }
- if (cameraEnumerator.getDeviceNames().length > 1) {
- cameraSwitchButton.setVisibility(View.VISIBLE);
- }
- }
- if (!inCall) {
- startCall();
- }
- } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
- PERMISSIONS_CALL)) {
- checkIfSomeAreApproved();
- }
- }
- private void checkIfSomeAreApproved() {
- if (!isVoiceOnlyCall) {
- if (cameraEnumerator.getDeviceNames().length == 0) {
- cameraControlButton.setVisibility(View.GONE);
- }
- if (cameraEnumerator.getDeviceNames().length > 1) {
- cameraSwitchButton.setVisibility(View.VISIBLE);
- }
- if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA)) {
- if (!videoOn) {
- onCameraClick();
- }
- } else {
- cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_off_white_24px);
- cameraControlButton.setAlpha(0.7f);
- cameraSwitchButton.setVisibility(View.GONE);
- }
- }
- if (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE)) {
- if (!audioOn) {
- onMicrophoneClick();
- }
- } else {
- microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
- }
- if (!inCall) {
- startCall();
- }
- }
- @AfterPermissionDenied(100)
- private void onPermissionsDenied() {
- if (!isVoiceOnlyCall) {
- if (cameraEnumerator.getDeviceNames().length == 0) {
- cameraControlButton.setVisibility(View.GONE);
- } else if (cameraEnumerator.getDeviceNames().length == 1) {
- cameraSwitchButton.setVisibility(View.GONE);
- }
- }
- if (getActivity() != null && (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA) ||
- EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE))) {
- checkIfSomeAreApproved();
- } else if (!inCall) {
- startCall();
- }
- }
- private void onAudioManagerDevicesChanged(
- final MagicAudioManager.AudioDevice device, final Set<MagicAudioManager.AudioDevice> availableDevices) {
- Log.d(TAG, "onAudioManagerDevicesChanged: " + availableDevices + ", "
- + "selected: " + device);
- }
- private void cameraInitialization() {
- videoCapturer = createCameraCapturer(cameraEnumerator);
- //Create a VideoSource instance
- if (videoCapturer != null) {
- videoSource = peerConnectionFactory.createVideoSource(videoCapturer);
- localVideoTrack = peerConnectionFactory.createVideoTrack("NCv0", videoSource);
- localMediaStream.addTrack(localVideoTrack);
- localVideoTrack.setEnabled(false);
- localVideoTrack.addSink(pipVideoView);
- }
- }
- private void microphoneInitialization() {
- //create an AudioSource instance
- audioSource = peerConnectionFactory.createAudioSource(audioConstraints);
- localAudioTrack = peerConnectionFactory.createAudioTrack("NCa0", audioSource);
- localAudioTrack.setEnabled(false);
- localMediaStream.addTrack(localAudioTrack);
- }
- private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
- final String[] deviceNames = enumerator.getDeviceNames();
- // First, try to find front facing camera
- Logging.d(TAG, "Looking for front facing cameras.");
- for (String deviceName : deviceNames) {
- if (enumerator.isFrontFacing(deviceName)) {
- Logging.d(TAG, "Creating front facing camera capturer.");
- VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
- if (videoCapturer != null) {
- return videoCapturer;
- }
- }
- }
- // Front facing camera not found, try something else
- Logging.d(TAG, "Looking for other cameras.");
- for (String deviceName : deviceNames) {
- if (!enumerator.isFrontFacing(deviceName)) {
- Logging.d(TAG, "Creating other camera capturer.");
- VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
- if (videoCapturer != null) {
- pipVideoView.setMirror(false);
- return videoCapturer;
- }
- }
- }
- return null;
- }
- @OnLongClick(R.id.call_control_microphone)
- public boolean onMicrophoneLongClick() {
- if (!audioOn) {
- callControlHandler.removeCallbacksAndMessages(null);
- cameraSwitchHandler.removeCallbacksAndMessages(null);
- isPTTActive = true;
- callControls.setVisibility(View.VISIBLE);
- cameraSwitchButton.setVisibility(View.VISIBLE);
- }
- onMicrophoneClick();
- return true;
- }
- @OnClick(R.id.callControlEnableSpeaker)
- public void onEnableSpeakerphoneClick() {
- if (audioManager != null) {
- audioManager.toggleUseSpeakerphone();
- callControlEnableSpeaker.flipSilently(!callControlEnableSpeaker.isFlipped());
- }
- }
- @OnClick(R.id.call_control_microphone)
- public void onMicrophoneClick() {
- if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE)) {
- if (getActivity() != null && !appPreferences.getPushToTalkIntroShown()) {
- spotlightView = new SpotlightView.Builder(getActivity())
- .introAnimationDuration(300)
- .enableRevealAnimation(true)
- .performClick(false)
- .fadeinTextDuration(400)
- .headingTvColor(getResources().getColor(R.color.colorPrimary))
- .headingTvSize(20)
- .headingTvText(getResources().getString(R.string.nc_push_to_talk))
- .subHeadingTvColor(getResources().getColor(R.color.white))
- .subHeadingTvSize(16)
- .subHeadingTvText(getResources().getString(R.string.nc_push_to_talk_desc))
- .maskColor(Color.parseColor("#dc000000"))
- .target(microphoneControlButton)
- .lineAnimDuration(400)
- .lineAndArcColor(getResources().getColor(R.color.colorPrimary))
- .enableDismissAfterShown(true)
- .dismissOnBackPress(true)
- .usageId("pushToTalk")
- .show();
- appPreferences.setPushToTalkIntroShown(true);
- }
- if (!isPTTActive) {
- audioOn = !audioOn;
- if (audioOn) {
- microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_white_24px);
- } else {
- microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
- }
- toggleMedia(audioOn, false);
- } else {
- microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_white_24px);
- pulseAnimation.start();
- toggleMedia(true, false);
- }
- if (isVoiceOnlyCall && !inCall) {
- startCall();
- }
- } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
- PERMISSIONS_MICROPHONE)) {
- // Microphone permission is permanently denied so we cannot request it normally.
- OpenAppDetailsDialogFragment.show(
- R.string.nc_microphone_permission_permanently_denied,
- R.string.nc_permissions_settings, (AppCompatActivity) getActivity());
- } else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(PERMISSIONS_MICROPHONE, 100);
- } else {
- onRequestPermissionsResult(100, PERMISSIONS_MICROPHONE, new int[]{1});
- }
- }
- }
- @OnClick(R.id.callControlHangupView)
- public void onHangupClick() {
- if (inCall) {
- hangup(false);
- } else {
- hangup(true);
- }
- }
- @OnClick(R.id.call_control_camera)
- public void onCameraClick() {
- if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA)) {
- videoOn = !videoOn;
- if (videoOn) {
- cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_white_24px);
- if (cameraEnumerator.getDeviceNames().length > 1) {
- cameraSwitchButton.setVisibility(View.VISIBLE);
- }
- } else {
- cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_off_white_24px);
- cameraSwitchButton.setVisibility(View.GONE);
- }
- toggleMedia(videoOn, true);
- } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
- PERMISSIONS_CAMERA)) {
- // Camera permission is permanently denied so we cannot request it normally.
- OpenAppDetailsDialogFragment.show(
- R.string.nc_camera_permission_permanently_denied,
- R.string.nc_permissions_settings, (AppCompatActivity) getActivity());
- } else {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(PERMISSIONS_CAMERA, 100);
- } else {
- onRequestPermissionsResult(100, PERMISSIONS_CAMERA, new int[]{1});
- }
- }
- }
- @OnClick(R.id.call_control_switch_camera)
- public void switchCamera() {
- CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
- if (cameraVideoCapturer != null) {
- cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
- @Override
- public void onCameraSwitchDone(boolean b) {
- pipVideoView.setMirror(false);
- }
- @Override
- public void onCameraSwitchError(String s) {
- }
- });
- }
- }
- private void toggleMedia(boolean enable, boolean video) {
- String message;
- if (video) {
- message = "videoOff";
- if (enable) {
- cameraControlButton.setAlpha(1.0f);
- message = "videoOn";
- startVideoCapture();
- } else {
- cameraControlButton.setAlpha(0.7f);
- if (videoCapturer != null) {
- try {
- videoCapturer.stopCapture();
- } catch (InterruptedException e) {
- Log.d(TAG, "Failed to stop capturing video while sensor is near the ear");
- }
- }
- }
- if (localMediaStream != null && localMediaStream.videoTracks.size() > 0) {
- localMediaStream.videoTracks.get(0).setEnabled(enable);
- }
- if (enable) {
- pipVideoView.setVisibility(View.VISIBLE);
- } else {
- pipVideoView.setVisibility(View.INVISIBLE);
- }
- } else {
- message = "audioOff";
- if (enable) {
- message = "audioOn";
- microphoneControlButton.setAlpha(1.0f);
- } else {
- microphoneControlButton.setAlpha(0.7f);
- }
- if (localMediaStream != null && localMediaStream.audioTracks.size() > 0) {
- localMediaStream.audioTracks.get(0).setEnabled(enable);
- }
- }
- if (inCall) {
- for (int i = 0; i < magicPeerConnectionWrapperList.size(); i++) {
- magicPeerConnectionWrapperList.get(i).sendChannelData(new DataChannelMessage(message));
- }
- }
- }
- private void startCall() {
- startPullingSignalingMessages();
- }
- private void animateCallControls(boolean show, long startDelay) {
- if (isVoiceOnlyCall) {
- if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
- spotlightView.setVisibility(View.GONE);
- }
- } else if (!isPTTActive) {
- float alpha;
- long duration;
- if (show) {
- callControlHandler.removeCallbacksAndMessages(null);
- cameraSwitchHandler.removeCallbacksAndMessages(null);
- alpha = 1.0f;
- duration = 1000;
- if (callControls.getVisibility() != View.VISIBLE) {
- callControls.setAlpha(0.0f);
- callControls.setVisibility(View.VISIBLE);
- cameraSwitchButton.setAlpha(0.0f);
- cameraSwitchButton.setVisibility(View.VISIBLE);
- } else {
- callControlHandler.postDelayed(() -> animateCallControls(false, 0), 5000);
- return;
- }
- } else {
- alpha = 0.0f;
- duration = 1000;
- }
- if (callControls != null) {
- callControls.setEnabled(false);
- callControls.animate()
- .translationY(0)
- .alpha(alpha)
- .setDuration(duration)
- .setStartDelay(startDelay)
- .setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
- if (callControls != null) {
- if (!show) {
- callControls.setVisibility(View.GONE);
- if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
- spotlightView.setVisibility(View.GONE);
- }
- } else {
- callControlHandler.postDelayed(new Runnable() {
- @Override
- public void run() {
- if (!isPTTActive) {
- animateCallControls(false, 0);
- }
- }
- }, 7500);
- }
- callControls.setEnabled(true);
- }
- }
- });
- }
- if (cameraSwitchButton != null) {
- cameraSwitchButton.setEnabled(false);
- cameraSwitchButton.animate()
- .translationY(0)
- .alpha(alpha)
- .setDuration(duration)
- .setStartDelay(startDelay)
- .setListener(new AnimatorListenerAdapter() {
- @Override
- public void onAnimationEnd(Animator animation) {
- super.onAnimationEnd(animation);
- if (cameraSwitchButton != null) {
- if (!show) {
- cameraSwitchButton.setVisibility(View.GONE);
- }
- cameraSwitchButton.setEnabled(true);
- }
- }
- });
- }
- }
- }
- @Override
- public void onDestroy() {
- onHangupClick();
- super.onDestroy();
- }
- private void startPullingSignalingMessages() {
- leavingCall = false;
- ncApi.getSignalingSettings(credentials, ApiUtils.getUrlForSignalingSettings(baseUrl))
- .subscribeOn(Schedulers.newThread())
- .retry(3)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<SignalingSettingsOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(SignalingSettingsOverall signalingSettingsOverall) {
- IceServer iceServer;
- if (signalingSettingsOverall != null && signalingSettingsOverall.getOcs() != null &&
- signalingSettingsOverall.getOcs().getSettings() != null) {
- if (signalingSettingsOverall.getOcs().getSettings().getStunServers() != null) {
- for (int i = 0; i < signalingSettingsOverall.getOcs().getSettings().getStunServers().size();
- i++) {
- iceServer = signalingSettingsOverall.getOcs().getSettings().getStunServers().get(i);
- if (TextUtils.isEmpty(iceServer.getUsername()) || TextUtils.isEmpty(iceServer
- .getCredential())) {
- iceServers.add(new PeerConnection.IceServer(iceServer.getUrl()));
- } else {
- iceServers.add(new PeerConnection.IceServer(iceServer.getUrl(),
- iceServer.getUsername(), iceServer.getCredential()));
- }
- }
- }
- if (signalingSettingsOverall.getOcs().getSettings().getTurnServers() != null) {
- for (int i = 0; i < signalingSettingsOverall.getOcs().getSettings().getTurnServers().size();
- i++) {
- iceServer = signalingSettingsOverall.getOcs().getSettings().getTurnServers().get(i);
- for (int j = 0; j < iceServer.getUrls().size(); j++) {
- if (TextUtils.isEmpty(iceServer.getUsername()) || TextUtils.isEmpty(iceServer
- .getCredential())) {
- iceServers.add(new PeerConnection.IceServer(iceServer.getUrls().get(j)));
- } else {
- iceServers.add(new PeerConnection.IceServer(iceServer.getUrls().get(j),
- iceServer.getUsername(), iceServer.getCredential()));
- }
- }
- }
- }
- }
- checkCapabilities();
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void checkCapabilities() {
- ncApi.getCapabilities(credentials, ApiUtils.getUrlForCapabilities(baseUrl))
- .retry(3)
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<CapabilitiesOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(CapabilitiesOverall capabilitiesOverall) {
- isMultiSession = capabilitiesOverall.getOcs().getData()
- .getCapabilities() != null && capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability() != null &&
- capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability()
- .getFeatures() != null && capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability()
- .getFeatures().contains("multi-room-users");
- needsPing = !(capabilitiesOverall.getOcs().getData()
- .getCapabilities() != null && capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability() != null &&
- capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability()
- .getFeatures() != null && capabilitiesOverall.getOcs().getData()
- .getCapabilities().getSpreedCapability()
- .getFeatures().contains("no-ping"));
- joinRoomAndCall();
- }
- @Override
- public void onError(Throwable e) {
- isMultiSession = false;
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void joinRoomAndCall() {
- if ("0".equals(callSession)) {
- ncApi.joinRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken), null)
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .retry(3)
- .subscribe(new Observer<CallOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(CallOverall callOverall) {
- callSession = callOverall.getOcs().getData().getSessionId();
- performCall();
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- } else {
- performCall();
- }
- }
- private void performCall() {
- ncApi.joinCall(credentials,
- ApiUtils.getUrlForCall(baseUrl, roomToken))
- .subscribeOn(Schedulers.newThread())
- .retry(3)
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<GenericOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(GenericOverall genericOverall) {
- inCall = true;
- if (connectingView != null) {
- connectingView.setVisibility(View.GONE);
- }
- if (conversationView != null) {
- conversationView.setVisibility(View.VISIBLE);
- }
- if (!isPTTActive) {
- animateCallControls(false, 5000);
- }
- ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
- ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomToken);
- ApplicationWideCurrentRoomHolder.getInstance().setInCall(true);
- ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(conversationUser);
- if (needsPing) {
- ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
- .takeWhile(observable -> inCall)
- .retry(3, observable -> inCall)
- .subscribe(new Observer<GenericOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- pingDisposable = d;
- }
- @Override
- public void onNext(GenericOverall genericOverall) {
- }
- @Override
- public void onError(Throwable e) {
- dispose(pingDisposable);
- }
- @Override
- public void onComplete() {
- dispose(pingDisposable);
- }
- });
- }
- // Start pulling signaling messages
- String urlToken = null;
- if (isMultiSession) {
- urlToken = roomToken;
- }
- NotificationUtils.cancelExistingNotifications(getApplicationContext(), conversationUser);
- ncApi.pullSignalingMessages(credentials, ApiUtils.getUrlForSignaling(baseUrl, urlToken))
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .repeatWhen(observable -> observable)
- .takeWhile(observable -> inCall)
- .retry(3, observable -> inCall)
- .subscribe(new Observer<SignalingOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- signalingDisposable = d;
- }
- @Override
- public void onNext(SignalingOverall signalingOverall) {
- if (signalingOverall.getOcs().getSignalings() != null) {
- for (int i = 0; i < signalingOverall.getOcs().getSignalings().size(); i++) {
- try {
- receivedSignalingMessage(signalingOverall.getOcs().getSignalings().get(i));
- } catch (IOException e) {
- Log.e(TAG, "Failed to process received signaling" +
- " message");
- }
- }
- }
- }
- @Override
- public void onError(Throwable e) {
- dispose(signalingDisposable);
- }
- @Override
- public void onComplete() {
- dispose(signalingDisposable);
- }
- });
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- @OnClick({R.id.pip_video_view, R.id.remote_renderers_layout})
- public void showCallControls() {
- animateCallControls(true, 0);
- }
- private void dispose(@Nullable Disposable disposable) {
- if (disposable != null && !disposable.isDisposed()) {
- disposable.dispose();
- } else if (disposable == null) {
- if (pingDisposable != null && !pingDisposable.isDisposed()) {
- pingDisposable.dispose();
- pingDisposable = null;
- }
- if (signalingDisposable != null && !signalingDisposable.isDisposed()) {
- signalingDisposable.dispose();
- signalingDisposable = null;
- }
- }
- }
- private void receivedSignalingMessage(Signaling signaling) throws IOException {
- String messageType = signaling.getType();
- if (leavingCall) {
- return;
- }
- if ("usersInRoom".equals(messageType)) {
- processUsersInRoom((List<HashMap<String, Object>>) signaling.getMessageWrapper());
- } else if ("message".equals(messageType)) {
- NCSignalingMessage ncSignalingMessage = LoganSquare.parse(signaling.getMessageWrapper().toString(),
- NCSignalingMessage.class);
- if (ncSignalingMessage.getRoomType().equals("video")) {
- MagicPeerConnectionWrapper magicPeerConnectionWrapper = alwaysGetPeerConnectionWrapperForSessionId
- (ncSignalingMessage.getFrom());
- String type = null;
- if (ncSignalingMessage.getPayload() != null && ncSignalingMessage.getPayload().getType() !=
- null) {
- type = ncSignalingMessage.getPayload().getType();
- } else if (ncSignalingMessage.getType() != null) {
- type = ncSignalingMessage.getType();
- }
- if (type != null) {
- switch (type) {
- case "offer":
- case "answer":
- magicPeerConnectionWrapper.setNick(ncSignalingMessage.getPayload().getNick());
- String sessionDescriptionStringWithPreferredCodec = MagicWebRTCUtils.preferCodec
- (ncSignalingMessage.getPayload().getSdp(),
- "VP8", false);
- SessionDescription sessionDescriptionWithPreferredCodec = new SessionDescription(
- SessionDescription.Type.fromCanonicalForm(type),
- sessionDescriptionStringWithPreferredCodec);
- if (magicPeerConnectionWrapper.getPeerConnection() != null) {
- magicPeerConnectionWrapper.getPeerConnection().setRemoteDescription(magicPeerConnectionWrapper
- .getMagicSdpObserver(), sessionDescriptionWithPreferredCodec);
- }
- break;
- case "candidate":
- NCIceCandidate ncIceCandidate = ncSignalingMessage.getPayload().getIceCandidate();
- IceCandidate iceCandidate = new IceCandidate(ncIceCandidate.getSdpMid(),
- ncIceCandidate.getSdpMLineIndex(), ncIceCandidate.getCandidate());
- magicPeerConnectionWrapper.addCandidate(iceCandidate);
- break;
- case "endOfCandidates":
- magicPeerConnectionWrapper.drainIceCandidates();
- break;
- default:
- break;
- }
- }
- } else {
- Log.d(TAG, "Something went very very wrong");
- }
- } else {
- Log.d(TAG, "Something went very very wrong");
- }
- }
- private void hangup(boolean dueToNetworkChange) {
- leavingCall = true;
- inCall = false;
- if (videoCapturer != null) {
- try {
- videoCapturer.stopCapture();
- } catch (InterruptedException e) {
- Log.e(TAG, "Failed to stop capturing while hanging up");
- }
- videoCapturer.dispose();
- videoCapturer = null;
- }
- for (int i = 0; i < magicPeerConnectionWrapperList.size(); i++) {
- endPeerConnection(magicPeerConnectionWrapperList.get(i).getSessionId());
- }
- if (pipVideoView != null) {
- pipVideoView.release();
- }
- if (audioSource != null) {
- audioSource.dispose();
- audioSource = null;
- }
- if (audioManager != null) {
- audioManager.stop();
- audioManager = null;
- }
- if (videoSource != null) {
- videoSource = null;
- }
- if (peerConnectionFactory != null) {
- peerConnectionFactory = null;
- }
- localMediaStream = null;
- localAudioTrack = null;
- localVideoTrack = null;
- if (!dueToNetworkChange) {
- hangupNetworkCalls();
- } else {
- if (getActivity() != null) {
- getActivity().finish();
- }
- }
- }
- private void hangupNetworkCalls() {
- ncApi.leaveCall(credentials, ApiUtils.getUrlForCall(baseUrl, roomToken))
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<GenericOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(GenericOverall genericOverall) {
- if (isMultiSession) {
- if (getActivity() != null) {
- getActivity().finish();
- }
- } else {
- leaveRoom();
- }
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void leaveRoom() {
- ncApi.leaveRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken))
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<GenericOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(GenericOverall genericOverall) {
- if (getActivity() != null) {
- getActivity().finish();
- }
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void startVideoCapture() {
- if (videoCapturer != null) {
- videoCapturer.startCapture(1280, 720, 30);
- }
- }
- private void processUsersInRoom(List<HashMap<String, Object>> users) {
- List<String> newSessions = new ArrayList<>();
- Set<String> oldSesssions = new HashSet<>();
- for (HashMap<String, Object> participant : users) {
- if (!participant.get("sessionId").equals(callSession)) {
- Object inCallObject = participant.get("inCall");
- boolean isNewSession;
- if (inCallObject instanceof Boolean) {
- isNewSession = (boolean) inCallObject;
- } else {
- isNewSession = ((long) inCallObject) != 0;
- }
- if (isNewSession) {
- newSessions.add(participant.get("sessionId").toString());
- } else {
- oldSesssions.add(participant.get("sessionId").toString());
- }
- }
- }
- for (MagicPeerConnectionWrapper magicPeerConnectionWrapper : magicPeerConnectionWrapperList) {
- oldSesssions.add(magicPeerConnectionWrapper.getSessionId());
- }
- // Calculate sessions that left the call
- oldSesssions.removeAll(newSessions);
- // Calculate sessions that join the call
- newSessions.removeAll(oldSesssions);
- if (leavingCall) {
- return;
- }
- if (newSessions.size() > 0) {
- getPeersForCall();
- }
- for (String sessionId : newSessions) {
- alwaysGetPeerConnectionWrapperForSessionId(sessionId);
- }
- for (String sessionId : oldSesssions) {
- endPeerConnection(sessionId);
- }
- }
- private void getPeersForCall() {
- ncApi.getPeersForCall(credentials, ApiUtils.getUrlForCall(baseUrl, roomToken))
- .subscribeOn(Schedulers.newThread())
- .subscribe(new Observer<ParticipantsOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(ParticipantsOverall participantsOverall) {
- participantMap = new HashMap<>();
- for (Participant participant : participantsOverall.getOcs().getData()) {
- participantMap.put(participant.getSessionId(), participant);
- if (getActivity() != null) {
- getActivity().runOnUiThread(() -> setupAvatarForSession(participant.getSessionId()));
- }
- }
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- private void deleteMagicPeerConnection(MagicPeerConnectionWrapper magicPeerConnectionWrapper) {
- magicPeerConnectionWrapper.removePeerConnection();
- magicPeerConnectionWrapperList.remove(magicPeerConnectionWrapper);
- }
- private MagicPeerConnectionWrapper alwaysGetPeerConnectionWrapperForSessionId(String sessionId) {
- MagicPeerConnectionWrapper magicPeerConnectionWrapper;
- if ((magicPeerConnectionWrapper = getPeerConnectionWrapperForSessionId(sessionId)) != null) {
- return magicPeerConnectionWrapper;
- } else {
- magicPeerConnectionWrapper = new MagicPeerConnectionWrapper(peerConnectionFactory,
- iceServers, sdpConstraints, sessionId, callSession, localMediaStream);
- magicPeerConnectionWrapperList.add(magicPeerConnectionWrapper);
- return magicPeerConnectionWrapper;
- }
- }
- private MagicPeerConnectionWrapper getPeerConnectionWrapperForSessionId(String sessionId) {
- for (MagicPeerConnectionWrapper magicPeerConnectionWrapper : magicPeerConnectionWrapperList) {
- if (magicPeerConnectionWrapper.getSessionId().equals(sessionId)) {
- return magicPeerConnectionWrapper;
- }
- }
- return null;
- }
- private void endPeerConnection(String sessionId) {
- MagicPeerConnectionWrapper magicPeerConnectionWrapper;
- if ((magicPeerConnectionWrapper = getPeerConnectionWrapperForSessionId(sessionId)) != null && getActivity()
- != null) {
- getActivity().runOnUiThread(() -> removeMediaStream(sessionId));
- deleteMagicPeerConnection(magicPeerConnectionWrapper);
- }
- }
- private void removeMediaStream(String sessionId) {
- if (remoteRenderersLayout != null && remoteRenderersLayout.getChildCount() > 0) {
- RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
- if (relativeLayout != null) {
- SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
- surfaceViewRenderer.release();
- remoteRenderersLayout.removeView(relativeLayout);
- remoteRenderersLayout.invalidate();
- }
- }
- if (callControls != null) {
- callControls.setZ(100.0f);
- }
- }
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onMessageEvent(ConfigurationChangeEvent configurationChangeEvent) {
- if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- remoteRenderersLayout.setOrientation(LinearLayout.HORIZONTAL);
- } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- remoteRenderersLayout.setOrientation(LinearLayout.VERTICAL);
- }
- }
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onMessageEvent(PeerConnectionEvent peerConnectionEvent) {
- if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent.PeerConnectionEventType
- .PEER_CLOSED)) {
- endPeerConnection(peerConnectionEvent.getSessionId());
- } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.SENSOR_FAR) ||
- peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.SENSOR_NEAR)) {
-
- if (!isVoiceOnlyCall) {
- boolean enableVideo = peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.SENSOR_FAR) && videoOn;
- if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA) &&
- inCall && videoOn
- && enableVideo != localVideoTrack.enabled()) {
- toggleMedia(enableVideo, true);
- }
- }
- } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.NICK_CHANGE)) {
- gotNick(peerConnectionEvent.getSessionId(), peerConnectionEvent.getNick());
- } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.VIDEO_CHANGE) && !isVoiceOnlyCall) {
- gotAudioOrVideoChange(true, peerConnectionEvent.getSessionId(),
- peerConnectionEvent.getChangeValue());
- } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
- .PeerConnectionEventType.AUDIO_CHANGE)) {
- gotAudioOrVideoChange(false, peerConnectionEvent.getSessionId(),
- peerConnectionEvent.getChangeValue());
- }
- }
- @Subscribe(threadMode = ThreadMode.MAIN)
- public void onMessageEvent(MediaStreamEvent mediaStreamEvent) {
- if (mediaStreamEvent.getMediaStream() != null) {
- setupVideoStreamForLayout(mediaStreamEvent.getMediaStream(), mediaStreamEvent.getSession(),
- mediaStreamEvent.getMediaStream().videoTracks != null
- && mediaStreamEvent.getMediaStream().videoTracks.size() > 0);
- } else {
- setupVideoStreamForLayout(null, mediaStreamEvent.getSession(), false);
- }
- }
- @Subscribe(threadMode = ThreadMode.BACKGROUND)
- public void onMessageEvent(SessionDescriptionSendEvent sessionDescriptionSend) throws IOException {
- NCMessageWrapper ncMessageWrapper = new NCMessageWrapper();
- ncMessageWrapper.setEv("message");
- ncMessageWrapper.setSessionId(callSession);
- NCSignalingMessage ncSignalingMessage = new NCSignalingMessage();
- ncSignalingMessage.setTo(sessionDescriptionSend.getPeerId());
- ncSignalingMessage.setRoomType("video");
- ncSignalingMessage.setType(sessionDescriptionSend.getType());
- NCMessagePayload ncMessagePayload = new NCMessagePayload();
- ncMessagePayload.setType(sessionDescriptionSend.getType());
- if (!"candidate".equals(sessionDescriptionSend.getType())) {
- ncMessagePayload.setSdp(sessionDescriptionSend.getSessionDescription().description);
- ncMessagePayload.setNick(conversationUser.getDisplayName());
- } else {
- ncMessagePayload.setIceCandidate(sessionDescriptionSend.getNcIceCandidate());
- }
- // Set all we need
- ncSignalingMessage.setPayload(ncMessagePayload);
- ncMessageWrapper.setSignalingMessage(ncSignalingMessage);
- StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append("{")
- .append("\"fn\":\"")
- .append(StringEscapeUtils.escapeJson(LoganSquare.serialize(ncMessageWrapper.getSignalingMessage()))).append("\"")
- .append(",")
- .append("\"sessionId\":")
- .append("\"").append(StringEscapeUtils.escapeJson(callSession)).append("\"")
- .append(",")
- .append("\"ev\":\"message\"")
- .append("}");
- List<String> strings = new ArrayList<>();
- String stringToSend = stringBuilder.toString();
- strings.add(stringToSend);
- String urlToken = null;
- if (isMultiSession) {
- urlToken = roomToken;
- }
- ncApi.sendSignalingMessages(credentials, ApiUtils.getUrlForSignaling(baseUrl, urlToken),
- strings.toString())
- .retry(3)
- .subscribeOn(Schedulers.newThread())
- .subscribe(new Observer<SignalingOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(SignalingOverall signalingOverall) {
- if (signalingOverall.getOcs().getSignalings() != null) {
- for (int i = 0; i < signalingOverall.getOcs().getSignalings().size(); i++) {
- try {
- receivedSignalingMessage(signalingOverall.getOcs().getSignalings().get(i));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
- @NonNull int[] grantResults) {
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- EffortlessPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults,
- this);
- }
- private void setupAvatarForSession(String session) {
- if (remoteRenderersLayout != null) {
- RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(session);
- if (relativeLayout != null) {
- ImageView avatarImageView = relativeLayout.findViewById(R.id.avatarImageView);
- if (participantMap.containsKey(session) && avatarImageView.getDrawable() == null) {
- int size = Math.round(getResources().getDimension(R.dimen.avatar_size_big));
- if (getActivity() != null) {
- GlideApp.with(getActivity())
- .asBitmap()
- .diskCacheStrategy(DiskCacheStrategy.NONE)
- .load(ApiUtils.getUrlForAvatarWithName(baseUrl, participantMap.get(session).getUserId(), R.dimen.avatar_size_big))
- .centerInside()
- .override(size, size)
- .apply(RequestOptions.bitmapTransform(new CircleCrop()))
- .into(avatarImageView);
- }
- }
- }
- }
- }
- private void setupVideoStreamForLayout(@Nullable MediaStream mediaStream, String session, boolean enable) {
- boolean isInitialLayoutSetupForPeer = false;
- if (remoteRenderersLayout.findViewWithTag(session) == null) {
- setupNewPeerLayout(session);
- isInitialLayoutSetupForPeer = true;
- }
- RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(session);
- SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
- ImageView imageView = relativeLayout.findViewById(R.id.avatarImageView);
- if (mediaStream != null && mediaStream.videoTracks != null && mediaStream.videoTracks.size() > 0 && enable) {
- VideoTrack videoTrack = mediaStream.videoTracks.get(0);
- videoTrack.addSink(surfaceViewRenderer);
- imageView.setVisibility(View.INVISIBLE);
- surfaceViewRenderer.setVisibility(View.VISIBLE);
- } else {
- imageView.setVisibility(View.VISIBLE);
- surfaceViewRenderer.setVisibility(View.INVISIBLE);
- if (isInitialLayoutSetupForPeer && isVoiceOnlyCall) {
- gotAudioOrVideoChange(true, session, false);
- }
- }
- callControls.setZ(100.0f);
- }
- private void gotAudioOrVideoChange(boolean video, String sessionId, boolean change) {
- RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
- if (relativeLayout != null) {
- ImageView imageView;
- ImageView avatarImageView = relativeLayout.findViewById(R.id.avatarImageView);
- SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
- if (video) {
- imageView = relativeLayout.findViewById(R.id.remote_video_off);
- if (change) {
- avatarImageView.setVisibility(View.INVISIBLE);
- surfaceViewRenderer.setVisibility(View.VISIBLE);
- } else {
- avatarImageView.setVisibility(View.VISIBLE);
- surfaceViewRenderer.setVisibility(View.INVISIBLE);
- }
- } else {
- imageView = relativeLayout.findViewById(R.id.remote_audio_off);
- }
- if (change && imageView.getVisibility() != View.INVISIBLE) {
- imageView.setVisibility(View.INVISIBLE);
- } else if (!change && imageView.getVisibility() != View.VISIBLE) {
- imageView.setVisibility(View.VISIBLE);
- }
- }
- }
- private void setupNewPeerLayout(String session) {
- if (remoteRenderersLayout.findViewWithTag(session) == null && getActivity() != null) {
- getActivity().runOnUiThread(() -> {
- RelativeLayout relativeLayout = (RelativeLayout)
- getActivity().getLayoutInflater().inflate(R.layout.call_item, remoteRenderersLayout,
- false);
- relativeLayout.setTag(session);
- SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id
- .surface_view);
- surfaceViewRenderer.setMirror(false);
- surfaceViewRenderer.init(rootEglBase.getEglBaseContext(), null);
- surfaceViewRenderer.setZOrderMediaOverlay(false);
- // disabled because it causes some devices to crash
- surfaceViewRenderer.setEnableHardwareScaler(false);
- surfaceViewRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
- surfaceViewRenderer.setOnClickListener(videoOnClickListener);
- remoteRenderersLayout.addView(relativeLayout);
- gotNick(session, getPeerConnectionWrapperForSessionId(session).getNick());
- setupAvatarForSession(session);
- callControls.setZ(100.0f);
- });
- }
- }
- private void gotNick(String sessionId, String nick) {
- RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
- if (relativeLayout != null) {
- TextView textView = relativeLayout.findViewById(R.id.peer_nick_text_view);
- textView.setText(nick);
- }
- }
- @Override
- protected void onAttach(@NonNull View view) {
- super.onAttach(view);
- eventBus.register(this);
- }
- @Override
- protected void onDetach(@NonNull View view) {
- super.onDetach(view);
- eventBus.unregister(this);
- }
- private class MicrophoneButtonTouchListener implements View.OnTouchListener {
- @SuppressLint("ClickableViewAccessibility")
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- v.onTouchEvent(event);
- if (event.getAction() == MotionEvent.ACTION_UP && isPTTActive) {
- isPTTActive = false;
- microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
- pulseAnimation.stop();
- toggleMedia(false, false);
- animateCallControls(false, 5000);
- }
- return true;
- }
- }
- private class VideoClickListener implements View.OnClickListener {
- @Override
- public void onClick(View v) {
- showCallControls();
- }
- }
- }
|