From 66da39a1a14cc60b66529355b64603478abff339 Mon Sep 17 00:00:00 2001 From: Mario Danic Date: Mon, 8 Oct 2018 21:13:08 +0200 Subject: [PATCH] Add initial Scarlet impl --- app/build.gradle | 5 ++ .../nextcloud/talk/api/ExternalSignaling.java | 37 ++++++++++ .../nextcloud/talk/webrtc/ScarletHelper.java | 69 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 app/src/main/java/com/nextcloud/talk/api/ExternalSignaling.java create mode 100644 app/src/main/java/com/nextcloud/talk/webrtc/ScarletHelper.java diff --git a/app/build.gradle b/app/build.gradle index db44df625..caa90aa4e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -190,6 +190,11 @@ dependencies { implementation 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1' implementation 'com.kevalpatel2106:emoticongifkeyboard:1.1' + implementation 'com.github.tinder.scarlet:scarlet:0.1.5' + implementation 'com.github.tinder.scarlet:scarlet-stream-adapter-rxjava2:0.1.5' + implementation 'com.github.tinder.scarlet:scarlet-message-adapter-moshi:0.1.5' + implementation 'com.github.tinder.scarlet:scarlet-websocket-okhttp:0.1.5' + testImplementation 'junit:junit:4.12' androidTestImplementation ('androidx.test.espresso:espresso-core:3.1.0-alpha4', { exclude group: 'com.android.support', module: 'support-annotations' diff --git a/app/src/main/java/com/nextcloud/talk/api/ExternalSignaling.java b/app/src/main/java/com/nextcloud/talk/api/ExternalSignaling.java new file mode 100644 index 000000000..aecfb3595 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/api/ExternalSignaling.java @@ -0,0 +1,37 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017-2018 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.api; + +import com.tinder.scarlet.WebSocket; +import com.tinder.scarlet.ws.Receive; + +import io.reactivex.Flowable; + +public interface ExternalSignaling { + @Receive + Flowable observeOnConnectionOpenedEvent(); + + @Receive + Flowable observeOnConnectionFailedEvent(); + + @Receive + Flowable observeOnConnectionClosedEvent(); +} diff --git a/app/src/main/java/com/nextcloud/talk/webrtc/ScarletHelper.java b/app/src/main/java/com/nextcloud/talk/webrtc/ScarletHelper.java new file mode 100644 index 000000000..87bfa91ff --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/webrtc/ScarletHelper.java @@ -0,0 +1,69 @@ +/* + * Nextcloud Talk application + * + * @author Mario Danic + * Copyright (C) 2017-2018 Mario Danic + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.nextcloud.talk.webrtc; + +import com.nextcloud.talk.api.ExternalSignaling; +import com.nextcloud.talk.application.NextcloudTalkApplication; +import com.tinder.scarlet.Scarlet; +import com.tinder.scarlet.messageadapter.moshi.MoshiMessageAdapter; +import com.tinder.scarlet.retry.ExponentialBackoffStrategy; +import com.tinder.scarlet.retry.LinearBackoffStrategy; +import com.tinder.scarlet.streamadapter.rxjava2.RxJava2StreamAdapterFactory; +import com.tinder.scarlet.websocket.okhttp.OkHttpClientUtils; +import com.tinder.scarlet.websocket.okhttp.OkHttpWebSocket; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.inject.Inject; + +import autodagger.AutoInjector; +import okhttp3.OkHttpClient; + +@AutoInjector(NextcloudTalkApplication.class) +public class ScarletHelper { + private Map externalSignalingMap = new HashMap<>(); + + @Inject + OkHttpClient okHttpClient; + + public ScarletHelper() { + NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this); + } + + public ExternalSignaling getExternalSignalingInstanceForServer(String url) { + if (externalSignalingMap.containsKey(url)) { + return externalSignalingMap.get(url); + } else { + Scarlet scarlet = new Scarlet.Builder() + .backoffStrategy(new LinearBackoffStrategy(500)) + .webSocketFactory(OkHttpClientUtils.newWebSocketFactory(okHttpClient, url)) + .addMessageAdapterFactory(new MoshiMessageAdapter.Factory()) + .addStreamAdapterFactory(new RxJava2StreamAdapterFactory()) + .build(); + ExternalSignaling externalSignaling = scarlet.create(ExternalSignaling.class); + externalSignalingMap.put(url, externalSignaling); + return externalSignaling; + } + } +}