CallController.java 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771
  1. /*
  2. * Nextcloud Talk application
  3. *
  4. * @author Mario Danic
  5. * Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. package com.nextcloud.talk.controllers;
  21. import android.Manifest;
  22. import android.animation.Animator;
  23. import android.animation.AnimatorListenerAdapter;
  24. import android.annotation.SuppressLint;
  25. import android.content.res.Configuration;
  26. import android.graphics.Color;
  27. import android.os.Build;
  28. import android.os.Bundle;
  29. import android.os.Handler;
  30. import android.support.annotation.NonNull;
  31. import android.support.annotation.Nullable;
  32. import android.support.v7.app.AppCompatActivity;
  33. import android.text.TextUtils;
  34. import android.util.Log;
  35. import android.view.LayoutInflater;
  36. import android.view.MotionEvent;
  37. import android.view.View;
  38. import android.view.ViewGroup;
  39. import android.widget.ImageView;
  40. import android.widget.LinearLayout;
  41. import android.widget.RelativeLayout;
  42. import android.widget.TextView;
  43. import com.bluelinelabs.logansquare.LoganSquare;
  44. import com.bumptech.glide.load.engine.DiskCacheStrategy;
  45. import com.bumptech.glide.load.resource.bitmap.CircleCrop;
  46. import com.bumptech.glide.request.RequestOptions;
  47. import com.nextcloud.talk.R;
  48. import com.nextcloud.talk.api.NcApi;
  49. import com.nextcloud.talk.application.NextcloudTalkApplication;
  50. import com.nextcloud.talk.controllers.base.BaseController;
  51. import com.nextcloud.talk.events.ConfigurationChangeEvent;
  52. import com.nextcloud.talk.events.MediaStreamEvent;
  53. import com.nextcloud.talk.events.PeerConnectionEvent;
  54. import com.nextcloud.talk.events.SessionDescriptionSendEvent;
  55. import com.nextcloud.talk.models.database.UserEntity;
  56. import com.nextcloud.talk.models.json.call.CallOverall;
  57. import com.nextcloud.talk.models.json.capabilities.CapabilitiesOverall;
  58. import com.nextcloud.talk.models.json.generic.GenericOverall;
  59. import com.nextcloud.talk.models.json.participants.Participant;
  60. import com.nextcloud.talk.models.json.participants.ParticipantsOverall;
  61. import com.nextcloud.talk.models.json.rooms.Conversation;
  62. import com.nextcloud.talk.models.json.rooms.RoomsOverall;
  63. import com.nextcloud.talk.models.json.signaling.DataChannelMessage;
  64. import com.nextcloud.talk.models.json.signaling.NCIceCandidate;
  65. import com.nextcloud.talk.models.json.signaling.NCMessagePayload;
  66. import com.nextcloud.talk.models.json.signaling.NCMessageWrapper;
  67. import com.nextcloud.talk.models.json.signaling.NCSignalingMessage;
  68. import com.nextcloud.talk.models.json.signaling.Signaling;
  69. import com.nextcloud.talk.models.json.signaling.SignalingOverall;
  70. import com.nextcloud.talk.models.json.signaling.settings.IceServer;
  71. import com.nextcloud.talk.models.json.signaling.settings.SignalingSettingsOverall;
  72. import com.nextcloud.talk.utils.ApiUtils;
  73. import com.nextcloud.talk.utils.MagicFlipView;
  74. import com.nextcloud.talk.utils.animations.PulseAnimation;
  75. import com.nextcloud.talk.utils.bundle.BundleKeys;
  76. import com.nextcloud.talk.utils.database.user.UserUtils;
  77. import com.nextcloud.talk.utils.glide.GlideApp;
  78. import com.nextcloud.talk.utils.preferences.AppPreferences;
  79. import com.nextcloud.talk.utils.singletons.ApplicationWideCurrentRoomHolder;
  80. import com.nextcloud.talk.webrtc.MagicAudioManager;
  81. import com.nextcloud.talk.webrtc.MagicPeerConnectionWrapper;
  82. import com.nextcloud.talk.webrtc.MagicWebRTCUtils;
  83. import com.wooplr.spotlight.SpotlightView;
  84. import org.apache.commons.lang3.StringEscapeUtils;
  85. import org.greenrobot.eventbus.EventBus;
  86. import org.greenrobot.eventbus.Subscribe;
  87. import org.greenrobot.eventbus.ThreadMode;
  88. import org.parceler.Parcels;
  89. import org.webrtc.AudioSource;
  90. import org.webrtc.AudioTrack;
  91. import org.webrtc.Camera1Enumerator;
  92. import org.webrtc.Camera2Enumerator;
  93. import org.webrtc.CameraEnumerator;
  94. import org.webrtc.CameraVideoCapturer;
  95. import org.webrtc.EglBase;
  96. import org.webrtc.IceCandidate;
  97. import org.webrtc.Logging;
  98. import org.webrtc.MediaConstraints;
  99. import org.webrtc.MediaStream;
  100. import org.webrtc.PeerConnection;
  101. import org.webrtc.PeerConnectionFactory;
  102. import org.webrtc.RendererCommon;
  103. import org.webrtc.SessionDescription;
  104. import org.webrtc.SurfaceViewRenderer;
  105. import org.webrtc.VideoCapturer;
  106. import org.webrtc.VideoSource;
  107. import org.webrtc.VideoTrack;
  108. import java.io.IOException;
  109. import java.util.ArrayList;
  110. import java.util.HashMap;
  111. import java.util.HashSet;
  112. import java.util.List;
  113. import java.util.Map;
  114. import java.util.Set;
  115. import java.util.concurrent.TimeUnit;
  116. import javax.inject.Inject;
  117. import autodagger.AutoInjector;
  118. import butterknife.BindView;
  119. import butterknife.OnClick;
  120. import butterknife.OnLongClick;
  121. import eu.davidea.flipview.FlipView;
  122. import io.reactivex.Observer;
  123. import io.reactivex.android.schedulers.AndroidSchedulers;
  124. import io.reactivex.disposables.Disposable;
  125. import io.reactivex.schedulers.Schedulers;
  126. import me.zhanghai.android.effortlesspermissions.AfterPermissionDenied;
  127. import me.zhanghai.android.effortlesspermissions.EffortlessPermissions;
  128. import me.zhanghai.android.effortlesspermissions.OpenAppDetailsDialogFragment;
  129. import okhttp3.Cache;
  130. import pub.devrel.easypermissions.AfterPermissionGranted;
  131. @AutoInjector(NextcloudTalkApplication.class)
  132. public class CallController extends BaseController {
  133. private static final String TAG = "CallController";
  134. private static final String[] PERMISSIONS_CALL = {
  135. android.Manifest.permission.CAMERA,
  136. android.Manifest.permission.RECORD_AUDIO,
  137. };
  138. private static final String[] PERMISSIONS_CAMERA = {
  139. Manifest.permission.CAMERA
  140. };
  141. private static final String[] PERMISSIONS_MICROPHONE = {
  142. Manifest.permission.RECORD_AUDIO
  143. };
  144. @BindView(R.id.callControlEnableSpeaker)
  145. MagicFlipView callControlEnableSpeaker;
  146. @BindView(R.id.pip_video_view)
  147. SurfaceViewRenderer pipVideoView;
  148. @BindView(R.id.relative_layout)
  149. RelativeLayout relativeLayout;
  150. @BindView(R.id.remote_renderers_layout)
  151. LinearLayout remoteRenderersLayout;
  152. @BindView(R.id.callControlsLinearLayoutView)
  153. LinearLayout callControls;
  154. @BindView(R.id.call_control_microphone)
  155. FlipView microphoneControlButton;
  156. @BindView(R.id.call_control_camera)
  157. FlipView cameraControlButton;
  158. @BindView(R.id.call_control_switch_camera)
  159. FlipView cameraSwitchButton;
  160. @BindView(R.id.connectingTextView)
  161. TextView connectingTextView;
  162. @BindView(R.id.connectingRelativeLayoutView)
  163. RelativeLayout connectingView;
  164. @BindView(R.id.conversationRelativeLayoutView)
  165. RelativeLayout conversationView;
  166. @Inject
  167. NcApi ncApi;
  168. @Inject
  169. EventBus eventBus;
  170. @Inject
  171. UserUtils userUtils;
  172. @Inject
  173. AppPreferences appPreferences;
  174. @Inject
  175. Cache cache;
  176. private PeerConnectionFactory peerConnectionFactory;
  177. private MediaConstraints audioConstraints;
  178. private MediaConstraints videoConstraints;
  179. private MediaConstraints sdpConstraints;
  180. private MagicAudioManager audioManager;
  181. private VideoSource videoSource;
  182. private VideoTrack localVideoTrack;
  183. private AudioSource audioSource;
  184. private AudioTrack localAudioTrack;
  185. private VideoCapturer videoCapturer;
  186. private EglBase rootEglBase;
  187. private boolean leavingCall = false;
  188. private boolean inCall = false;
  189. private Disposable signalingDisposable;
  190. private Disposable pingDisposable;
  191. private List<PeerConnection.IceServer> iceServers;
  192. private CameraEnumerator cameraEnumerator;
  193. private String roomToken;
  194. private UserEntity userEntity;
  195. private String callSession;
  196. private MediaStream localMediaStream;
  197. private String credentials;
  198. private List<MagicPeerConnectionWrapper> magicPeerConnectionWrapperList = new ArrayList<>();
  199. private Map<String, Participant> participantMap = new HashMap<>();
  200. private boolean videoOn = false;
  201. private boolean audioOn = false;
  202. private boolean isMultiSession = false;
  203. private boolean needsPing = true;
  204. private boolean isVoiceOnlyCall;
  205. private boolean isFromNotification;
  206. private Handler callControlHandler = new Handler();
  207. private Handler cameraSwitchHandler = new Handler();
  208. private boolean isPTTActive = false;
  209. private PulseAnimation pulseAnimation;
  210. private View.OnClickListener videoOnClickListener;
  211. private String baseUrl;
  212. private String roomId;
  213. private SpotlightView spotlightView;
  214. public CallController(Bundle args) {
  215. super(args);
  216. NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
  217. roomId = args.getString(BundleKeys.KEY_ROOM_ID, "");
  218. roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN, "");
  219. userEntity = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
  220. if (userEntity == null) {
  221. userEntity = userUtils.getCurrentUser();
  222. }
  223. callSession = args.getString(BundleKeys.KEY_CALL_SESSION, "0");
  224. credentials = ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken());
  225. isVoiceOnlyCall = args.getBoolean(BundleKeys.KEY_CALL_VOICE_ONLY, false);
  226. if (userEntity.getUserId().equals("?")) {
  227. credentials = null;
  228. }
  229. baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
  230. if (TextUtils.isEmpty(baseUrl)) {
  231. baseUrl = userEntity.getBaseUrl();
  232. }
  233. isFromNotification = TextUtils.isEmpty(roomToken);
  234. }
  235. @Override
  236. protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
  237. return inflater.inflate(R.layout.controller_call, container, false);
  238. }
  239. private void createCameraEnumerator() {
  240. if (getActivity() != null) {
  241. boolean camera2EnumeratorIsSupported = false;
  242. try {
  243. camera2EnumeratorIsSupported = Camera2Enumerator.isSupported(getActivity());
  244. } catch (final Throwable throwable) {
  245. Log.w(TAG, "Camera2Enumator threw an error");
  246. }
  247. if (camera2EnumeratorIsSupported) {
  248. cameraEnumerator = new Camera2Enumerator(getActivity());
  249. } else {
  250. cameraEnumerator = new Camera1Enumerator(MagicWebRTCUtils.shouldEnableVideoHardwareAcceleration());
  251. }
  252. }
  253. }
  254. @Override
  255. protected void onViewBound(@NonNull View view) {
  256. super.onViewBound(view);
  257. microphoneControlButton.setOnTouchListener(new MicrophoneButtonTouchListener());
  258. videoOnClickListener = new VideoClickListener();
  259. pulseAnimation = PulseAnimation.create().with(microphoneControlButton.getFrontImageView())
  260. .setDuration(310)
  261. .setRepeatCount(PulseAnimation.INFINITE)
  262. .setRepeatMode(PulseAnimation.REVERSE);
  263. try {
  264. cache.evictAll();
  265. } catch (IOException e) {
  266. Log.e(TAG, "Failed to evict cache");
  267. }
  268. if (isVoiceOnlyCall) {
  269. callControlEnableSpeaker.setVisibility(View.VISIBLE);
  270. }
  271. callControls.setZ(100.0f);
  272. basicInitialization();
  273. if (isFromNotification) {
  274. handleFromNotification();
  275. } else {
  276. initViews();
  277. checkPermissions();
  278. }
  279. }
  280. private void basicInitialization() {
  281. rootEglBase = EglBase.create();
  282. createCameraEnumerator();
  283. //Create a new PeerConnectionFactory instance.
  284. PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
  285. peerConnectionFactory = PeerConnectionFactory.builder().createPeerConnectionFactory();
  286. peerConnectionFactory.setVideoHwAccelerationOptions(rootEglBase.getEglBaseContext(),
  287. rootEglBase.getEglBaseContext());
  288. //Create MediaConstraints - Will be useful for specifying video and audio constraints.
  289. audioConstraints = new MediaConstraints();
  290. videoConstraints = new MediaConstraints();
  291. localMediaStream = peerConnectionFactory.createLocalMediaStream("NCMS");
  292. // Create and audio manager that will take care of audio routing,
  293. // audio modes, audio device enumeration etc.
  294. audioManager = MagicAudioManager.create(getApplicationContext(), !isVoiceOnlyCall);
  295. // Store existing audio settings and change audio mode to
  296. // MODE_IN_COMMUNICATION for best possible VoIP performance.
  297. Log.d(TAG, "Starting the audio manager...");
  298. audioManager.start(this::onAudioManagerDevicesChanged);
  299. iceServers = new ArrayList<>();
  300. //create sdpConstraints
  301. sdpConstraints = new MediaConstraints();
  302. sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveAudio", "true"));
  303. String offerToReceiveVideoString = "true";
  304. if (isVoiceOnlyCall) {
  305. offerToReceiveVideoString = "false";
  306. }
  307. sdpConstraints.mandatory.add(new MediaConstraints.KeyValuePair("OfferToReceiveVideo",
  308. offerToReceiveVideoString));
  309. sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("internalSctpDataChannels", "true"));
  310. sdpConstraints.optional.add(new MediaConstraints.KeyValuePair("DtlsSrtpKeyAgreement", "true"));
  311. if (!isVoiceOnlyCall) {
  312. cameraInitialization();
  313. }
  314. microphoneInitialization();
  315. }
  316. private void handleFromNotification() {
  317. ncApi.getRooms(credentials, ApiUtils.getUrlForGetRooms(baseUrl))
  318. .retry(3)
  319. .subscribeOn(Schedulers.newThread())
  320. .observeOn(AndroidSchedulers.mainThread())
  321. .subscribe(new Observer<RoomsOverall>() {
  322. @Override
  323. public void onSubscribe(Disposable d) {
  324. }
  325. @Override
  326. public void onNext(RoomsOverall roomsOverall) {
  327. for (Conversation conversation : roomsOverall.getOcs().getData()) {
  328. if (roomId.equals(conversation.getRoomId())) {
  329. roomToken = conversation.getToken();
  330. break;
  331. }
  332. }
  333. initViews();
  334. checkPermissions();
  335. }
  336. @Override
  337. public void onError(Throwable e) {
  338. }
  339. @Override
  340. public void onComplete() {
  341. }
  342. });
  343. }
  344. private void initViews() {
  345. if (isVoiceOnlyCall) {
  346. cameraSwitchButton.setVisibility(View.GONE);
  347. cameraControlButton.setVisibility(View.GONE);
  348. pipVideoView.setVisibility(View.GONE);
  349. } else {
  350. if (cameraEnumerator.getDeviceNames().length < 2) {
  351. cameraSwitchButton.setVisibility(View.GONE);
  352. }
  353. // setting this to true because it's not shown by default
  354. pipVideoView.setMirror(false);
  355. pipVideoView.init(rootEglBase.getEglBaseContext(), null);
  356. pipVideoView.setZOrderMediaOverlay(true);
  357. // disabled because it causes some devices to crash
  358. pipVideoView.setEnableHardwareScaler(false);
  359. pipVideoView.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
  360. }
  361. }
  362. private void checkPermissions() {
  363. if (isVoiceOnlyCall) {
  364. onMicrophoneClick();
  365. } else if (getActivity() != null) {
  366. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  367. requestPermissions(PERMISSIONS_CALL, 100);
  368. } else {
  369. onRequestPermissionsResult(100, PERMISSIONS_CALL, new int[]{1, 1});
  370. }
  371. }
  372. }
  373. @AfterPermissionGranted(100)
  374. private void onPermissionsGranted() {
  375. if (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CALL)) {
  376. if (!videoOn && !isVoiceOnlyCall) {
  377. onCameraClick();
  378. }
  379. if (!audioOn) {
  380. onMicrophoneClick();
  381. }
  382. if (!isVoiceOnlyCall) {
  383. if (cameraEnumerator.getDeviceNames().length == 0) {
  384. cameraControlButton.setVisibility(View.GONE);
  385. }
  386. if (cameraEnumerator.getDeviceNames().length > 1) {
  387. cameraSwitchButton.setVisibility(View.VISIBLE);
  388. }
  389. }
  390. if (!inCall) {
  391. startCall();
  392. }
  393. } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
  394. PERMISSIONS_CALL)) {
  395. checkIfSomeAreApproved();
  396. }
  397. }
  398. private void checkIfSomeAreApproved() {
  399. if (!isVoiceOnlyCall) {
  400. if (cameraEnumerator.getDeviceNames().length == 0) {
  401. cameraControlButton.setVisibility(View.GONE);
  402. }
  403. if (cameraEnumerator.getDeviceNames().length > 1) {
  404. cameraSwitchButton.setVisibility(View.VISIBLE);
  405. }
  406. if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA)) {
  407. if (!videoOn) {
  408. onCameraClick();
  409. }
  410. } else {
  411. cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_off_white_24px);
  412. cameraControlButton.setAlpha(0.7f);
  413. cameraSwitchButton.setVisibility(View.GONE);
  414. }
  415. }
  416. if (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE)) {
  417. if (!audioOn) {
  418. onMicrophoneClick();
  419. }
  420. } else {
  421. microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
  422. }
  423. if (!inCall) {
  424. startCall();
  425. }
  426. }
  427. @AfterPermissionDenied(100)
  428. private void onPermissionsDenied() {
  429. if (!isVoiceOnlyCall) {
  430. if (cameraEnumerator.getDeviceNames().length == 0) {
  431. cameraControlButton.setVisibility(View.GONE);
  432. } else if (cameraEnumerator.getDeviceNames().length == 1) {
  433. cameraSwitchButton.setVisibility(View.GONE);
  434. }
  435. }
  436. if (getActivity() != null && (EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA) ||
  437. EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE))) {
  438. checkIfSomeAreApproved();
  439. } else if (!inCall) {
  440. startCall();
  441. }
  442. }
  443. private void onAudioManagerDevicesChanged(
  444. final MagicAudioManager.AudioDevice device, final Set<MagicAudioManager.AudioDevice> availableDevices) {
  445. Log.d(TAG, "onAudioManagerDevicesChanged: " + availableDevices + ", "
  446. + "selected: " + device);
  447. }
  448. private void cameraInitialization() {
  449. videoCapturer = createCameraCapturer(cameraEnumerator);
  450. //Create a VideoSource instance
  451. if (videoCapturer != null) {
  452. videoSource = peerConnectionFactory.createVideoSource(videoCapturer);
  453. localVideoTrack = peerConnectionFactory.createVideoTrack("NCv0", videoSource);
  454. localMediaStream.addTrack(localVideoTrack);
  455. localVideoTrack.setEnabled(false);
  456. localVideoTrack.addSink(pipVideoView);
  457. }
  458. }
  459. private void microphoneInitialization() {
  460. //create an AudioSource instance
  461. audioSource = peerConnectionFactory.createAudioSource(audioConstraints);
  462. localAudioTrack = peerConnectionFactory.createAudioTrack("NCa0", audioSource);
  463. localAudioTrack.setEnabled(false);
  464. localMediaStream.addTrack(localAudioTrack);
  465. }
  466. private VideoCapturer createCameraCapturer(CameraEnumerator enumerator) {
  467. final String[] deviceNames = enumerator.getDeviceNames();
  468. // First, try to find front facing camera
  469. Logging.d(TAG, "Looking for front facing cameras.");
  470. for (String deviceName : deviceNames) {
  471. if (enumerator.isFrontFacing(deviceName)) {
  472. Logging.d(TAG, "Creating front facing camera capturer.");
  473. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  474. if (videoCapturer != null) {
  475. return videoCapturer;
  476. }
  477. }
  478. }
  479. // Front facing camera not found, try something else
  480. Logging.d(TAG, "Looking for other cameras.");
  481. for (String deviceName : deviceNames) {
  482. if (!enumerator.isFrontFacing(deviceName)) {
  483. Logging.d(TAG, "Creating other camera capturer.");
  484. VideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
  485. if (videoCapturer != null) {
  486. pipVideoView.setMirror(false);
  487. return videoCapturer;
  488. }
  489. }
  490. }
  491. return null;
  492. }
  493. @OnLongClick(R.id.call_control_microphone)
  494. public boolean onMicrophoneLongClick() {
  495. if (!audioOn) {
  496. callControlHandler.removeCallbacksAndMessages(null);
  497. cameraSwitchHandler.removeCallbacksAndMessages(null);
  498. isPTTActive = true;
  499. callControls.setVisibility(View.VISIBLE);
  500. cameraSwitchButton.setVisibility(View.VISIBLE);
  501. }
  502. onMicrophoneClick();
  503. return true;
  504. }
  505. @OnClick(R.id.callControlEnableSpeaker)
  506. public void onEnableSpeakerphoneClick() {
  507. if (audioManager != null) {
  508. audioManager.toggleUseSpeakerphone();
  509. callControlEnableSpeaker.flipSilently(!callControlEnableSpeaker.isFlipped());
  510. }
  511. }
  512. @OnClick(R.id.call_control_microphone)
  513. public void onMicrophoneClick() {
  514. if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_MICROPHONE)) {
  515. if (getActivity() != null && !appPreferences.getPushToTalkIntroShown()) {
  516. spotlightView = new SpotlightView.Builder(getActivity())
  517. .introAnimationDuration(300)
  518. .enableRevealAnimation(true)
  519. .performClick(false)
  520. .fadeinTextDuration(400)
  521. .headingTvColor(getResources().getColor(R.color.colorPrimary))
  522. .headingTvSize(20)
  523. .headingTvText(getResources().getString(R.string.nc_push_to_talk))
  524. .subHeadingTvColor(getResources().getColor(R.color.white))
  525. .subHeadingTvSize(16)
  526. .subHeadingTvText(getResources().getString(R.string.nc_push_to_talk_desc))
  527. .maskColor(Color.parseColor("#dc000000"))
  528. .target(microphoneControlButton)
  529. .lineAnimDuration(400)
  530. .lineAndArcColor(getResources().getColor(R.color.colorPrimary))
  531. .enableDismissAfterShown(true)
  532. .dismissOnBackPress(true)
  533. .usageId("pushToTalk")
  534. .show();
  535. appPreferences.setPushToTalkIntroShown(true);
  536. }
  537. if (!isPTTActive) {
  538. audioOn = !audioOn;
  539. if (audioOn) {
  540. microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_white_24px);
  541. } else {
  542. microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
  543. }
  544. toggleMedia(audioOn, false);
  545. } else {
  546. microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_white_24px);
  547. pulseAnimation.start();
  548. toggleMedia(true, false);
  549. }
  550. if (isVoiceOnlyCall && !inCall) {
  551. startCall();
  552. }
  553. } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
  554. PERMISSIONS_MICROPHONE)) {
  555. // Microphone permission is permanently denied so we cannot request it normally.
  556. OpenAppDetailsDialogFragment.show(
  557. R.string.nc_microphone_permission_permanently_denied,
  558. R.string.nc_permissions_settings, (AppCompatActivity) getActivity());
  559. } else {
  560. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  561. requestPermissions(PERMISSIONS_MICROPHONE, 100);
  562. } else {
  563. onRequestPermissionsResult(100, PERMISSIONS_MICROPHONE, new int[]{1});
  564. }
  565. }
  566. }
  567. @OnClick(R.id.callControlHangupView)
  568. public void onHangupClick() {
  569. if (inCall) {
  570. hangup(false);
  571. } else {
  572. hangup(true);
  573. }
  574. }
  575. @OnClick(R.id.call_control_camera)
  576. public void onCameraClick() {
  577. if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA)) {
  578. videoOn = !videoOn;
  579. if (videoOn) {
  580. cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_white_24px);
  581. if (cameraEnumerator.getDeviceNames().length > 1) {
  582. cameraSwitchButton.setVisibility(View.VISIBLE);
  583. }
  584. } else {
  585. cameraControlButton.getFrontImageView().setImageResource(R.drawable.ic_videocam_off_white_24px);
  586. cameraSwitchButton.setVisibility(View.GONE);
  587. }
  588. toggleMedia(videoOn, true);
  589. } else if (getActivity() != null && EffortlessPermissions.somePermissionPermanentlyDenied(getActivity(),
  590. PERMISSIONS_CAMERA)) {
  591. // Camera permission is permanently denied so we cannot request it normally.
  592. OpenAppDetailsDialogFragment.show(
  593. R.string.nc_camera_permission_permanently_denied,
  594. R.string.nc_permissions_settings, (AppCompatActivity) getActivity());
  595. } else {
  596. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  597. requestPermissions(PERMISSIONS_CAMERA, 100);
  598. } else {
  599. onRequestPermissionsResult(100, PERMISSIONS_CAMERA, new int[]{1});
  600. }
  601. }
  602. }
  603. @OnClick(R.id.call_control_switch_camera)
  604. public void switchCamera() {
  605. CameraVideoCapturer cameraVideoCapturer = (CameraVideoCapturer) videoCapturer;
  606. if (cameraVideoCapturer != null) {
  607. cameraVideoCapturer.switchCamera(new CameraVideoCapturer.CameraSwitchHandler() {
  608. @Override
  609. public void onCameraSwitchDone(boolean b) {
  610. pipVideoView.setMirror(false);
  611. }
  612. @Override
  613. public void onCameraSwitchError(String s) {
  614. }
  615. });
  616. }
  617. }
  618. private void toggleMedia(boolean enable, boolean video) {
  619. String message;
  620. if (video) {
  621. message = "videoOff";
  622. if (enable) {
  623. cameraControlButton.setAlpha(1.0f);
  624. message = "videoOn";
  625. startVideoCapture();
  626. } else {
  627. cameraControlButton.setAlpha(0.7f);
  628. if (videoCapturer != null) {
  629. try {
  630. videoCapturer.stopCapture();
  631. } catch (InterruptedException e) {
  632. Log.d(TAG, "Failed to stop capturing video while sensor is near the ear");
  633. }
  634. }
  635. }
  636. if (localMediaStream != null && localMediaStream.videoTracks.size() > 0) {
  637. localMediaStream.videoTracks.get(0).setEnabled(enable);
  638. }
  639. if (enable) {
  640. pipVideoView.setVisibility(View.VISIBLE);
  641. } else {
  642. pipVideoView.setVisibility(View.INVISIBLE);
  643. }
  644. } else {
  645. message = "audioOff";
  646. if (enable) {
  647. message = "audioOn";
  648. microphoneControlButton.setAlpha(1.0f);
  649. } else {
  650. microphoneControlButton.setAlpha(0.7f);
  651. }
  652. if (localMediaStream != null && localMediaStream.audioTracks.size() > 0) {
  653. localMediaStream.audioTracks.get(0).setEnabled(enable);
  654. }
  655. }
  656. if (inCall) {
  657. for (int i = 0; i < magicPeerConnectionWrapperList.size(); i++) {
  658. magicPeerConnectionWrapperList.get(i).sendChannelData(new DataChannelMessage(message));
  659. }
  660. }
  661. }
  662. private void startCall() {
  663. startPullingSignalingMessages();
  664. }
  665. private void animateCallControls(boolean show, long startDelay) {
  666. if (isVoiceOnlyCall) {
  667. if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
  668. spotlightView.setVisibility(View.GONE);
  669. }
  670. } else if (!isPTTActive) {
  671. float alpha;
  672. long duration;
  673. if (show) {
  674. callControlHandler.removeCallbacksAndMessages(null);
  675. cameraSwitchHandler.removeCallbacksAndMessages(null);
  676. alpha = 1.0f;
  677. duration = 1000;
  678. if (callControls.getVisibility() != View.VISIBLE) {
  679. callControls.setAlpha(0.0f);
  680. callControls.setVisibility(View.VISIBLE);
  681. cameraSwitchButton.setAlpha(0.0f);
  682. cameraSwitchButton.setVisibility(View.VISIBLE);
  683. } else {
  684. callControlHandler.postDelayed(() -> animateCallControls(false, 0), 5000);
  685. return;
  686. }
  687. } else {
  688. alpha = 0.0f;
  689. duration = 1000;
  690. }
  691. if (callControls != null) {
  692. callControls.setEnabled(false);
  693. callControls.animate()
  694. .translationY(0)
  695. .alpha(alpha)
  696. .setDuration(duration)
  697. .setStartDelay(startDelay)
  698. .setListener(new AnimatorListenerAdapter() {
  699. @Override
  700. public void onAnimationEnd(Animator animation) {
  701. super.onAnimationEnd(animation);
  702. if (callControls != null) {
  703. if (!show) {
  704. callControls.setVisibility(View.GONE);
  705. if (spotlightView != null && spotlightView.getVisibility() != View.GONE) {
  706. spotlightView.setVisibility(View.GONE);
  707. }
  708. } else {
  709. callControlHandler.postDelayed(new Runnable() {
  710. @Override
  711. public void run() {
  712. if (!isPTTActive) {
  713. animateCallControls(false, 0);
  714. }
  715. }
  716. }, 7500);
  717. }
  718. callControls.setEnabled(true);
  719. }
  720. }
  721. });
  722. }
  723. if (cameraSwitchButton != null) {
  724. cameraSwitchButton.setEnabled(false);
  725. cameraSwitchButton.animate()
  726. .translationY(0)
  727. .alpha(alpha)
  728. .setDuration(duration)
  729. .setStartDelay(startDelay)
  730. .setListener(new AnimatorListenerAdapter() {
  731. @Override
  732. public void onAnimationEnd(Animator animation) {
  733. super.onAnimationEnd(animation);
  734. if (cameraSwitchButton != null) {
  735. if (!show) {
  736. cameraSwitchButton.setVisibility(View.GONE);
  737. }
  738. cameraSwitchButton.setEnabled(true);
  739. }
  740. }
  741. });
  742. }
  743. }
  744. }
  745. @Override
  746. public void onDestroy() {
  747. onHangupClick();
  748. super.onDestroy();
  749. }
  750. private void startPullingSignalingMessages() {
  751. leavingCall = false;
  752. ncApi.getSignalingSettings(credentials, ApiUtils.getUrlForSignalingSettings(baseUrl))
  753. .subscribeOn(Schedulers.newThread())
  754. .retry(3)
  755. .observeOn(AndroidSchedulers.mainThread())
  756. .subscribe(new Observer<SignalingSettingsOverall>() {
  757. @Override
  758. public void onSubscribe(Disposable d) {
  759. }
  760. @Override
  761. public void onNext(SignalingSettingsOverall signalingSettingsOverall) {
  762. IceServer iceServer;
  763. if (signalingSettingsOverall != null && signalingSettingsOverall.getOcs() != null &&
  764. signalingSettingsOverall.getOcs().getSettings() != null) {
  765. if (signalingSettingsOverall.getOcs().getSettings().getStunServers() != null) {
  766. for (int i = 0; i < signalingSettingsOverall.getOcs().getSettings().getStunServers().size();
  767. i++) {
  768. iceServer = signalingSettingsOverall.getOcs().getSettings().getStunServers().get(i);
  769. if (TextUtils.isEmpty(iceServer.getUsername()) || TextUtils.isEmpty(iceServer
  770. .getCredential())) {
  771. iceServers.add(new PeerConnection.IceServer(iceServer.getUrl()));
  772. } else {
  773. iceServers.add(new PeerConnection.IceServer(iceServer.getUrl(),
  774. iceServer.getUsername(), iceServer.getCredential()));
  775. }
  776. }
  777. }
  778. if (signalingSettingsOverall.getOcs().getSettings().getTurnServers() != null) {
  779. for (int i = 0; i < signalingSettingsOverall.getOcs().getSettings().getTurnServers().size();
  780. i++) {
  781. iceServer = signalingSettingsOverall.getOcs().getSettings().getTurnServers().get(i);
  782. for (int j = 0; j < iceServer.getUrls().size(); j++) {
  783. if (TextUtils.isEmpty(iceServer.getUsername()) || TextUtils.isEmpty(iceServer
  784. .getCredential())) {
  785. iceServers.add(new PeerConnection.IceServer(iceServer.getUrls().get(j)));
  786. } else {
  787. iceServers.add(new PeerConnection.IceServer(iceServer.getUrls().get(j),
  788. iceServer.getUsername(), iceServer.getCredential()));
  789. }
  790. }
  791. }
  792. }
  793. }
  794. checkCapabilities();
  795. }
  796. @Override
  797. public void onError(Throwable e) {
  798. }
  799. @Override
  800. public void onComplete() {
  801. }
  802. });
  803. }
  804. private void checkCapabilities() {
  805. ncApi.getCapabilities(credentials, ApiUtils.getUrlForCapabilities(baseUrl))
  806. .retry(3)
  807. .subscribeOn(Schedulers.newThread())
  808. .observeOn(AndroidSchedulers.mainThread())
  809. .subscribe(new Observer<CapabilitiesOverall>() {
  810. @Override
  811. public void onSubscribe(Disposable d) {
  812. }
  813. @Override
  814. public void onNext(CapabilitiesOverall capabilitiesOverall) {
  815. isMultiSession = capabilitiesOverall.getOcs().getData()
  816. .getCapabilities() != null && capabilitiesOverall.getOcs().getData()
  817. .getCapabilities().getSpreedCapability() != null &&
  818. capabilitiesOverall.getOcs().getData()
  819. .getCapabilities().getSpreedCapability()
  820. .getFeatures() != null && capabilitiesOverall.getOcs().getData()
  821. .getCapabilities().getSpreedCapability()
  822. .getFeatures().contains("multi-room-users");
  823. needsPing = !(capabilitiesOverall.getOcs().getData()
  824. .getCapabilities() != null && capabilitiesOverall.getOcs().getData()
  825. .getCapabilities().getSpreedCapability() != null &&
  826. capabilitiesOverall.getOcs().getData()
  827. .getCapabilities().getSpreedCapability()
  828. .getFeatures() != null && capabilitiesOverall.getOcs().getData()
  829. .getCapabilities().getSpreedCapability()
  830. .getFeatures().contains("no-ping"));
  831. joinRoomAndCall();
  832. }
  833. @Override
  834. public void onError(Throwable e) {
  835. isMultiSession = false;
  836. }
  837. @Override
  838. public void onComplete() {
  839. }
  840. });
  841. }
  842. private void joinRoomAndCall() {
  843. if ("0".equals(callSession)) {
  844. ncApi.joinRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken), null)
  845. .subscribeOn(Schedulers.newThread())
  846. .observeOn(AndroidSchedulers.mainThread())
  847. .retry(3)
  848. .subscribe(new Observer<CallOverall>() {
  849. @Override
  850. public void onSubscribe(Disposable d) {
  851. }
  852. @Override
  853. public void onNext(CallOverall callOverall) {
  854. callSession = callOverall.getOcs().getData().getSessionId();
  855. performCall();
  856. }
  857. @Override
  858. public void onError(Throwable e) {
  859. }
  860. @Override
  861. public void onComplete() {
  862. }
  863. });
  864. } else {
  865. performCall();
  866. }
  867. }
  868. private void performCall() {
  869. ncApi.joinCall(credentials,
  870. ApiUtils.getUrlForCall(baseUrl, roomToken))
  871. .subscribeOn(Schedulers.newThread())
  872. .retry(3)
  873. .observeOn(AndroidSchedulers.mainThread())
  874. .subscribe(new Observer<GenericOverall>() {
  875. @Override
  876. public void onSubscribe(Disposable d) {
  877. }
  878. @Override
  879. public void onNext(GenericOverall genericOverall) {
  880. inCall = true;
  881. if (connectingView != null) {
  882. connectingView.setVisibility(View.GONE);
  883. }
  884. if (conversationView != null) {
  885. conversationView.setVisibility(View.VISIBLE);
  886. }
  887. if (!isPTTActive) {
  888. animateCallControls(false, 5000);
  889. }
  890. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomId(roomId);
  891. ApplicationWideCurrentRoomHolder.getInstance().setCurrentRoomToken(roomToken);
  892. ApplicationWideCurrentRoomHolder.getInstance().setInCall(true);
  893. ApplicationWideCurrentRoomHolder.getInstance().setUserInRoom(userEntity);
  894. if (needsPing) {
  895. ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
  896. .subscribeOn(Schedulers.newThread())
  897. .observeOn(AndroidSchedulers.mainThread())
  898. .repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
  899. .takeWhile(observable -> inCall)
  900. .retry(3, observable -> inCall)
  901. .subscribe(new Observer<GenericOverall>() {
  902. @Override
  903. public void onSubscribe(Disposable d) {
  904. pingDisposable = d;
  905. }
  906. @Override
  907. public void onNext(GenericOverall genericOverall) {
  908. }
  909. @Override
  910. public void onError(Throwable e) {
  911. dispose(pingDisposable);
  912. }
  913. @Override
  914. public void onComplete() {
  915. dispose(pingDisposable);
  916. }
  917. });
  918. }
  919. // Start pulling signaling messages
  920. String urlToken = null;
  921. if (isMultiSession) {
  922. urlToken = roomToken;
  923. }
  924. ncApi.pullSignalingMessages(credentials, ApiUtils.getUrlForSignaling(baseUrl, urlToken))
  925. .subscribeOn(Schedulers.newThread())
  926. .observeOn(AndroidSchedulers.mainThread())
  927. .repeatWhen(observable -> observable)
  928. .takeWhile(observable -> inCall)
  929. .retry(3, observable -> inCall)
  930. .subscribe(new Observer<SignalingOverall>() {
  931. @Override
  932. public void onSubscribe(Disposable d) {
  933. signalingDisposable = d;
  934. }
  935. @Override
  936. public void onNext(SignalingOverall signalingOverall) {
  937. if (signalingOverall.getOcs().getSignalings() != null) {
  938. for (int i = 0; i < signalingOverall.getOcs().getSignalings().size(); i++) {
  939. try {
  940. receivedSignalingMessage(signalingOverall.getOcs().getSignalings().get(i));
  941. } catch (IOException e) {
  942. Log.e(TAG, "Failed to process received signaling" +
  943. " message");
  944. }
  945. }
  946. }
  947. }
  948. @Override
  949. public void onError(Throwable e) {
  950. dispose(signalingDisposable);
  951. }
  952. @Override
  953. public void onComplete() {
  954. dispose(signalingDisposable);
  955. }
  956. });
  957. }
  958. @Override
  959. public void onError(Throwable e) {
  960. }
  961. @Override
  962. public void onComplete() {
  963. }
  964. });
  965. }
  966. @OnClick({R.id.pip_video_view, R.id.remote_renderers_layout})
  967. public void showCallControls() {
  968. animateCallControls(true, 0);
  969. }
  970. private void dispose(@Nullable Disposable disposable) {
  971. if (disposable != null && !disposable.isDisposed()) {
  972. disposable.dispose();
  973. } else if (disposable == null) {
  974. if (pingDisposable != null && !pingDisposable.isDisposed()) {
  975. pingDisposable.dispose();
  976. pingDisposable = null;
  977. }
  978. if (signalingDisposable != null && !signalingDisposable.isDisposed()) {
  979. signalingDisposable.dispose();
  980. signalingDisposable = null;
  981. }
  982. }
  983. }
  984. private void receivedSignalingMessage(Signaling signaling) throws IOException {
  985. String messageType = signaling.getType();
  986. if (leavingCall) {
  987. return;
  988. }
  989. if ("usersInRoom".equals(messageType)) {
  990. processUsersInRoom((List<HashMap<String, String>>) signaling.getMessageWrapper());
  991. } else if ("message".equals(messageType)) {
  992. NCSignalingMessage ncSignalingMessage = LoganSquare.parse(signaling.getMessageWrapper().toString(),
  993. NCSignalingMessage.class);
  994. if (ncSignalingMessage.getRoomType().equals("video")) {
  995. MagicPeerConnectionWrapper magicPeerConnectionWrapper = alwaysGetPeerConnectionWrapperForSessionId
  996. (ncSignalingMessage.getFrom());
  997. String type = null;
  998. if (ncSignalingMessage.getPayload() != null && ncSignalingMessage.getPayload().getType() !=
  999. null) {
  1000. type = ncSignalingMessage.getPayload().getType();
  1001. } else if (ncSignalingMessage.getType() != null) {
  1002. type = ncSignalingMessage.getType();
  1003. }
  1004. if (type != null) {
  1005. switch (type) {
  1006. case "offer":
  1007. case "answer":
  1008. magicPeerConnectionWrapper.setNick(ncSignalingMessage.getPayload().getNick());
  1009. String sessionDescriptionStringWithPreferredCodec = MagicWebRTCUtils.preferCodec
  1010. (ncSignalingMessage.getPayload().getSdp(),
  1011. "VP8", false);
  1012. SessionDescription sessionDescriptionWithPreferredCodec = new SessionDescription(
  1013. SessionDescription.Type.fromCanonicalForm(type),
  1014. sessionDescriptionStringWithPreferredCodec);
  1015. if (magicPeerConnectionWrapper.getPeerConnection() != null) {
  1016. magicPeerConnectionWrapper.getPeerConnection().setRemoteDescription(magicPeerConnectionWrapper
  1017. .getMagicSdpObserver(), sessionDescriptionWithPreferredCodec);
  1018. }
  1019. break;
  1020. case "candidate":
  1021. NCIceCandidate ncIceCandidate = ncSignalingMessage.getPayload().getIceCandidate();
  1022. IceCandidate iceCandidate = new IceCandidate(ncIceCandidate.getSdpMid(),
  1023. ncIceCandidate.getSdpMLineIndex(), ncIceCandidate.getCandidate());
  1024. magicPeerConnectionWrapper.addCandidate(iceCandidate);
  1025. break;
  1026. case "endOfCandidates":
  1027. magicPeerConnectionWrapper.drainIceCandidates();
  1028. break;
  1029. default:
  1030. break;
  1031. }
  1032. }
  1033. } else {
  1034. Log.d(TAG, "Something went very very wrong");
  1035. }
  1036. } else {
  1037. Log.d(TAG, "Something went very very wrong");
  1038. }
  1039. }
  1040. private void hangup(boolean dueToNetworkChange) {
  1041. leavingCall = true;
  1042. inCall = false;
  1043. if (videoCapturer != null) {
  1044. try {
  1045. videoCapturer.stopCapture();
  1046. } catch (InterruptedException e) {
  1047. Log.e(TAG, "Failed to stop capturing while hanging up");
  1048. }
  1049. videoCapturer.dispose();
  1050. videoCapturer = null;
  1051. }
  1052. for (int i = 0; i < magicPeerConnectionWrapperList.size(); i++) {
  1053. endPeerConnection(magicPeerConnectionWrapperList.get(i).getSessionId());
  1054. }
  1055. if (pipVideoView != null) {
  1056. pipVideoView.release();
  1057. }
  1058. if (audioSource != null) {
  1059. audioSource.dispose();
  1060. audioSource = null;
  1061. }
  1062. if (audioManager != null) {
  1063. audioManager.stop();
  1064. audioManager = null;
  1065. }
  1066. if (videoSource != null) {
  1067. videoSource = null;
  1068. }
  1069. if (peerConnectionFactory != null) {
  1070. peerConnectionFactory = null;
  1071. }
  1072. localMediaStream = null;
  1073. localAudioTrack = null;
  1074. localVideoTrack = null;
  1075. if (!dueToNetworkChange) {
  1076. hangupNetworkCalls();
  1077. } else {
  1078. if (getActivity() != null) {
  1079. getActivity().finish();
  1080. }
  1081. }
  1082. }
  1083. private void hangupNetworkCalls() {
  1084. ncApi.leaveCall(credentials, ApiUtils.getUrlForCall(baseUrl, roomToken))
  1085. .subscribeOn(Schedulers.newThread())
  1086. .observeOn(AndroidSchedulers.mainThread())
  1087. .subscribe(new Observer<GenericOverall>() {
  1088. @Override
  1089. public void onSubscribe(Disposable d) {
  1090. }
  1091. @Override
  1092. public void onNext(GenericOverall genericOverall) {
  1093. if (isMultiSession) {
  1094. if (getActivity() != null) {
  1095. getActivity().finish();
  1096. }
  1097. } else {
  1098. leaveRoom();
  1099. }
  1100. }
  1101. @Override
  1102. public void onError(Throwable e) {
  1103. }
  1104. @Override
  1105. public void onComplete() {
  1106. }
  1107. });
  1108. }
  1109. private void leaveRoom() {
  1110. ncApi.leaveRoom(credentials, ApiUtils.getUrlForSettingMyselfAsActiveParticipant(baseUrl, roomToken))
  1111. .subscribeOn(Schedulers.newThread())
  1112. .observeOn(AndroidSchedulers.mainThread())
  1113. .subscribe(new Observer<GenericOverall>() {
  1114. @Override
  1115. public void onSubscribe(Disposable d) {
  1116. }
  1117. @Override
  1118. public void onNext(GenericOverall genericOverall) {
  1119. if (getActivity() != null) {
  1120. getActivity().finish();
  1121. }
  1122. }
  1123. @Override
  1124. public void onError(Throwable e) {
  1125. }
  1126. @Override
  1127. public void onComplete() {
  1128. }
  1129. });
  1130. }
  1131. private void startVideoCapture() {
  1132. if (videoCapturer != null) {
  1133. videoCapturer.startCapture(1280, 720, 30);
  1134. }
  1135. }
  1136. private void processUsersInRoom(List<HashMap<String, String>> users) {
  1137. List<String> newSessions = new ArrayList<>();
  1138. Set<String> oldSesssions = new HashSet<>();
  1139. for (HashMap<String, String> participant : users) {
  1140. if (!participant.get("sessionId").equals(callSession)) {
  1141. Object inCallObject = participant.get("inCall");
  1142. if ((boolean) inCallObject) {
  1143. newSessions.add(participant.get("sessionId"));
  1144. } else {
  1145. oldSesssions.add(participant.get("sessionId"));
  1146. }
  1147. }
  1148. }
  1149. for (MagicPeerConnectionWrapper magicPeerConnectionWrapper : magicPeerConnectionWrapperList) {
  1150. oldSesssions.add(magicPeerConnectionWrapper.getSessionId());
  1151. }
  1152. // Calculate sessions that left the call
  1153. oldSesssions.removeAll(newSessions);
  1154. // Calculate sessions that join the call
  1155. newSessions.removeAll(oldSesssions);
  1156. if (leavingCall) {
  1157. return;
  1158. }
  1159. if (newSessions.size() > 0) {
  1160. getPeersForCall();
  1161. }
  1162. for (String sessionId : newSessions) {
  1163. alwaysGetPeerConnectionWrapperForSessionId(sessionId);
  1164. }
  1165. for (String sessionId : oldSesssions) {
  1166. endPeerConnection(sessionId);
  1167. }
  1168. }
  1169. private void getPeersForCall() {
  1170. ncApi.getPeersForCall(credentials, ApiUtils.getUrlForCall(baseUrl, roomToken))
  1171. .subscribeOn(Schedulers.newThread())
  1172. .subscribe(new Observer<ParticipantsOverall>() {
  1173. @Override
  1174. public void onSubscribe(Disposable d) {
  1175. }
  1176. @Override
  1177. public void onNext(ParticipantsOverall participantsOverall) {
  1178. participantMap = new HashMap<>();
  1179. for (Participant participant : participantsOverall.getOcs().getData()) {
  1180. participantMap.put(participant.getSessionId(), participant);
  1181. if (getActivity() != null) {
  1182. getActivity().runOnUiThread(() -> setupAvatarForSession(participant.getSessionId()));
  1183. }
  1184. }
  1185. }
  1186. @Override
  1187. public void onError(Throwable e) {
  1188. }
  1189. @Override
  1190. public void onComplete() {
  1191. }
  1192. });
  1193. }
  1194. private void deleteMagicPeerConnection(MagicPeerConnectionWrapper magicPeerConnectionWrapper) {
  1195. magicPeerConnectionWrapper.removePeerConnection();
  1196. magicPeerConnectionWrapperList.remove(magicPeerConnectionWrapper);
  1197. }
  1198. private MagicPeerConnectionWrapper alwaysGetPeerConnectionWrapperForSessionId(String sessionId) {
  1199. MagicPeerConnectionWrapper magicPeerConnectionWrapper;
  1200. if ((magicPeerConnectionWrapper = getPeerConnectionWrapperForSessionId(sessionId)) != null) {
  1201. return magicPeerConnectionWrapper;
  1202. } else {
  1203. magicPeerConnectionWrapper = new MagicPeerConnectionWrapper(peerConnectionFactory,
  1204. iceServers, sdpConstraints, sessionId, callSession, localMediaStream);
  1205. magicPeerConnectionWrapperList.add(magicPeerConnectionWrapper);
  1206. return magicPeerConnectionWrapper;
  1207. }
  1208. }
  1209. private MagicPeerConnectionWrapper getPeerConnectionWrapperForSessionId(String sessionId) {
  1210. for (MagicPeerConnectionWrapper magicPeerConnectionWrapper : magicPeerConnectionWrapperList) {
  1211. if (magicPeerConnectionWrapper.getSessionId().equals(sessionId)) {
  1212. return magicPeerConnectionWrapper;
  1213. }
  1214. }
  1215. return null;
  1216. }
  1217. private void endPeerConnection(String sessionId) {
  1218. MagicPeerConnectionWrapper magicPeerConnectionWrapper;
  1219. if ((magicPeerConnectionWrapper = getPeerConnectionWrapperForSessionId(sessionId)) != null && getActivity()
  1220. != null) {
  1221. getActivity().runOnUiThread(() -> removeMediaStream(sessionId));
  1222. deleteMagicPeerConnection(magicPeerConnectionWrapper);
  1223. }
  1224. }
  1225. private void removeMediaStream(String sessionId) {
  1226. if (remoteRenderersLayout != null && remoteRenderersLayout.getChildCount() > 0) {
  1227. RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
  1228. if (relativeLayout != null) {
  1229. SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
  1230. surfaceViewRenderer.release();
  1231. remoteRenderersLayout.removeView(relativeLayout);
  1232. remoteRenderersLayout.invalidate();
  1233. }
  1234. }
  1235. if (callControls != null) {
  1236. callControls.setZ(100.0f);
  1237. }
  1238. }
  1239. @Subscribe(threadMode = ThreadMode.MAIN)
  1240. public void onMessageEvent(ConfigurationChangeEvent configurationChangeEvent) {
  1241. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  1242. remoteRenderersLayout.setOrientation(LinearLayout.HORIZONTAL);
  1243. } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  1244. remoteRenderersLayout.setOrientation(LinearLayout.VERTICAL);
  1245. }
  1246. }
  1247. @Subscribe(threadMode = ThreadMode.MAIN)
  1248. public void onMessageEvent(PeerConnectionEvent peerConnectionEvent) {
  1249. if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent.PeerConnectionEventType
  1250. .PEER_CLOSED)) {
  1251. endPeerConnection(peerConnectionEvent.getSessionId());
  1252. } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1253. .PeerConnectionEventType.SENSOR_FAR) ||
  1254. peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1255. .PeerConnectionEventType.SENSOR_NEAR)) {
  1256. if (!isVoiceOnlyCall) {
  1257. boolean enableVideo = peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1258. .PeerConnectionEventType.SENSOR_FAR) && videoOn;
  1259. if (getActivity() != null && EffortlessPermissions.hasPermissions(getActivity(), PERMISSIONS_CAMERA) &&
  1260. inCall && videoOn
  1261. && enableVideo != localVideoTrack.enabled()) {
  1262. toggleMedia(enableVideo, true);
  1263. }
  1264. }
  1265. } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1266. .PeerConnectionEventType.NICK_CHANGE)) {
  1267. gotNick(peerConnectionEvent.getSessionId(), peerConnectionEvent.getNick());
  1268. } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1269. .PeerConnectionEventType.VIDEO_CHANGE) && !isVoiceOnlyCall) {
  1270. gotAudioOrVideoChange(true, peerConnectionEvent.getSessionId(),
  1271. peerConnectionEvent.getChangeValue());
  1272. } else if (peerConnectionEvent.getPeerConnectionEventType().equals(PeerConnectionEvent
  1273. .PeerConnectionEventType.AUDIO_CHANGE)) {
  1274. gotAudioOrVideoChange(false, peerConnectionEvent.getSessionId(),
  1275. peerConnectionEvent.getChangeValue());
  1276. }
  1277. }
  1278. @Subscribe(threadMode = ThreadMode.MAIN)
  1279. public void onMessageEvent(MediaStreamEvent mediaStreamEvent) {
  1280. if (mediaStreamEvent.getMediaStream() != null) {
  1281. setupVideoStreamForLayout(mediaStreamEvent.getMediaStream(), mediaStreamEvent.getSession(),
  1282. mediaStreamEvent.getMediaStream().videoTracks != null
  1283. && mediaStreamEvent.getMediaStream().videoTracks.size() > 0);
  1284. } else {
  1285. setupVideoStreamForLayout(null, mediaStreamEvent.getSession(), false);
  1286. }
  1287. }
  1288. @Subscribe(threadMode = ThreadMode.BACKGROUND)
  1289. public void onMessageEvent(SessionDescriptionSendEvent sessionDescriptionSend) throws IOException {
  1290. NCMessageWrapper ncMessageWrapper = new NCMessageWrapper();
  1291. ncMessageWrapper.setEv("message");
  1292. ncMessageWrapper.setSessionId(callSession);
  1293. NCSignalingMessage ncSignalingMessage = new NCSignalingMessage();
  1294. ncSignalingMessage.setTo(sessionDescriptionSend.getPeerId());
  1295. ncSignalingMessage.setRoomType("video");
  1296. ncSignalingMessage.setType(sessionDescriptionSend.getType());
  1297. NCMessagePayload ncMessagePayload = new NCMessagePayload();
  1298. ncMessagePayload.setType(sessionDescriptionSend.getType());
  1299. if (!"candidate".equals(sessionDescriptionSend.getType())) {
  1300. ncMessagePayload.setSdp(sessionDescriptionSend.getSessionDescription().description);
  1301. ncMessagePayload.setNick(userEntity.getDisplayName());
  1302. } else {
  1303. ncMessagePayload.setIceCandidate(sessionDescriptionSend.getNcIceCandidate());
  1304. }
  1305. // Set all we need
  1306. ncSignalingMessage.setPayload(ncMessagePayload);
  1307. ncMessageWrapper.setSignalingMessage(ncSignalingMessage);
  1308. StringBuilder stringBuilder = new StringBuilder();
  1309. stringBuilder.append("{");
  1310. stringBuilder.append("\"fn\":\"");
  1311. stringBuilder.append(StringEscapeUtils.escapeJson(LoganSquare.serialize(ncMessageWrapper
  1312. .getSignalingMessage()))).append("\"");
  1313. stringBuilder.append(",");
  1314. stringBuilder.append("\"sessionId\":");
  1315. stringBuilder.append("\"").append(StringEscapeUtils.escapeJson(callSession)).append("\"");
  1316. stringBuilder.append(",");
  1317. stringBuilder.append("\"ev\":\"message\"");
  1318. stringBuilder.append("}");
  1319. List<String> strings = new ArrayList<>();
  1320. String stringToSend = stringBuilder.toString();
  1321. strings.add(stringToSend);
  1322. String urlToken = null;
  1323. if (isMultiSession) {
  1324. urlToken = roomToken;
  1325. }
  1326. ncApi.sendSignalingMessages(credentials, ApiUtils.getUrlForSignaling(baseUrl, urlToken),
  1327. strings.toString())
  1328. .retry(3)
  1329. .subscribeOn(Schedulers.newThread())
  1330. .subscribe(new Observer<SignalingOverall>() {
  1331. @Override
  1332. public void onSubscribe(Disposable d) {
  1333. }
  1334. @Override
  1335. public void onNext(SignalingOverall signalingOverall) {
  1336. if (signalingOverall.getOcs().getSignalings() != null) {
  1337. for (int i = 0; i < signalingOverall.getOcs().getSignalings().size(); i++) {
  1338. try {
  1339. receivedSignalingMessage(signalingOverall.getOcs().getSignalings().get(i));
  1340. } catch (IOException e) {
  1341. e.printStackTrace();
  1342. }
  1343. }
  1344. }
  1345. }
  1346. @Override
  1347. public void onError(Throwable e) {
  1348. }
  1349. @Override
  1350. public void onComplete() {
  1351. }
  1352. });
  1353. }
  1354. @Override
  1355. public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
  1356. @NonNull int[] grantResults) {
  1357. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  1358. EffortlessPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults,
  1359. this);
  1360. }
  1361. private void setupAvatarForSession(String session) {
  1362. if (remoteRenderersLayout != null) {
  1363. RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(session);
  1364. if (relativeLayout != null) {
  1365. ImageView avatarImageView = relativeLayout.findViewById(R.id.avatarImageView);
  1366. if (participantMap.containsKey(session) && avatarImageView.getDrawable() == null) {
  1367. int size = Math.round(getResources().getDimension(R.dimen.avatar_size_big));
  1368. if (getActivity() != null) {
  1369. GlideApp.with(getActivity())
  1370. .asBitmap()
  1371. .diskCacheStrategy(DiskCacheStrategy.NONE)
  1372. .load(ApiUtils.getUrlForAvatarWithName(baseUrl, participantMap.get(session).getUserId(), R.dimen.avatar_size_big))
  1373. .centerInside()
  1374. .override(size, size)
  1375. .apply(RequestOptions.bitmapTransform(new CircleCrop()))
  1376. .into(avatarImageView);
  1377. }
  1378. }
  1379. }
  1380. }
  1381. }
  1382. private void setupVideoStreamForLayout(@Nullable MediaStream mediaStream, String session, boolean enable) {
  1383. boolean isInitialLayoutSetupForPeer = false;
  1384. if (remoteRenderersLayout.findViewWithTag(session) == null) {
  1385. setupNewPeerLayout(session);
  1386. isInitialLayoutSetupForPeer = true;
  1387. }
  1388. RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(session);
  1389. SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
  1390. ImageView imageView = relativeLayout.findViewById(R.id.avatarImageView);
  1391. if (mediaStream != null && mediaStream.videoTracks != null && mediaStream.videoTracks.size() > 0 && enable) {
  1392. VideoTrack videoTrack = mediaStream.videoTracks.get(0);
  1393. videoTrack.addSink(surfaceViewRenderer);
  1394. imageView.setVisibility(View.INVISIBLE);
  1395. surfaceViewRenderer.setVisibility(View.VISIBLE);
  1396. } else {
  1397. imageView.setVisibility(View.VISIBLE);
  1398. surfaceViewRenderer.setVisibility(View.INVISIBLE);
  1399. if (isInitialLayoutSetupForPeer && isVoiceOnlyCall) {
  1400. gotAudioOrVideoChange(true, session, false);
  1401. }
  1402. }
  1403. callControls.setZ(100.0f);
  1404. }
  1405. private void gotAudioOrVideoChange(boolean video, String sessionId, boolean change) {
  1406. RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
  1407. if (relativeLayout != null) {
  1408. ImageView imageView;
  1409. ImageView avatarImageView = relativeLayout.findViewById(R.id.avatarImageView);
  1410. SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id.surface_view);
  1411. if (video) {
  1412. imageView = relativeLayout.findViewById(R.id.remote_video_off);
  1413. if (change) {
  1414. avatarImageView.setVisibility(View.INVISIBLE);
  1415. surfaceViewRenderer.setVisibility(View.VISIBLE);
  1416. } else {
  1417. avatarImageView.setVisibility(View.VISIBLE);
  1418. surfaceViewRenderer.setVisibility(View.INVISIBLE);
  1419. }
  1420. } else {
  1421. imageView = relativeLayout.findViewById(R.id.remote_audio_off);
  1422. }
  1423. if (change && imageView.getVisibility() != View.INVISIBLE) {
  1424. imageView.setVisibility(View.INVISIBLE);
  1425. } else if (!change && imageView.getVisibility() != View.VISIBLE) {
  1426. imageView.setVisibility(View.VISIBLE);
  1427. }
  1428. }
  1429. }
  1430. private void setupNewPeerLayout(String session) {
  1431. if (remoteRenderersLayout.findViewWithTag(session) == null && getActivity() != null) {
  1432. getActivity().runOnUiThread(() -> {
  1433. RelativeLayout relativeLayout = (RelativeLayout)
  1434. getActivity().getLayoutInflater().inflate(R.layout.call_item, remoteRenderersLayout,
  1435. false);
  1436. relativeLayout.setTag(session);
  1437. SurfaceViewRenderer surfaceViewRenderer = relativeLayout.findViewById(R.id
  1438. .surface_view);
  1439. surfaceViewRenderer.setMirror(false);
  1440. surfaceViewRenderer.init(rootEglBase.getEglBaseContext(), null);
  1441. surfaceViewRenderer.setZOrderMediaOverlay(false);
  1442. // disabled because it causes some devices to crash
  1443. surfaceViewRenderer.setEnableHardwareScaler(false);
  1444. surfaceViewRenderer.setScalingType(RendererCommon.ScalingType.SCALE_ASPECT_FIT);
  1445. surfaceViewRenderer.setOnClickListener(videoOnClickListener);
  1446. remoteRenderersLayout.addView(relativeLayout);
  1447. gotNick(session, getPeerConnectionWrapperForSessionId(session).getNick());
  1448. setupAvatarForSession(session);
  1449. callControls.setZ(100.0f);
  1450. });
  1451. }
  1452. }
  1453. private void gotNick(String sessionId, String nick) {
  1454. RelativeLayout relativeLayout = remoteRenderersLayout.findViewWithTag(sessionId);
  1455. if (relativeLayout != null) {
  1456. TextView textView = relativeLayout.findViewById(R.id.peer_nick_text_view);
  1457. textView.setText(nick);
  1458. }
  1459. }
  1460. @Override
  1461. protected void onAttach(@NonNull View view) {
  1462. super.onAttach(view);
  1463. eventBus.register(this);
  1464. }
  1465. @Override
  1466. protected void onDetach(@NonNull View view) {
  1467. super.onDetach(view);
  1468. eventBus.unregister(this);
  1469. }
  1470. private class MicrophoneButtonTouchListener implements View.OnTouchListener {
  1471. @SuppressLint("ClickableViewAccessibility")
  1472. @Override
  1473. public boolean onTouch(View v, MotionEvent event) {
  1474. v.onTouchEvent(event);
  1475. if (event.getAction() == MotionEvent.ACTION_UP && isPTTActive) {
  1476. isPTTActive = false;
  1477. microphoneControlButton.getFrontImageView().setImageResource(R.drawable.ic_mic_off_white_24px);
  1478. pulseAnimation.stop();
  1479. toggleMedia(false, false);
  1480. animateCallControls(false, 5000);
  1481. }
  1482. return true;
  1483. }
  1484. }
  1485. private class VideoClickListener implements View.OnClickListener {
  1486. @Override
  1487. public void onClick(View v) {
  1488. showCallControls();
  1489. }
  1490. }
  1491. }