Skip to content

Commit 78e2618

Browse files
committed
Successfull build of Instance implementations + Use newer WebRTC version in libtgvoip + Pass supported tgcalls versions in CallProtocol + Do not build two versions of WebRTC
1 parent 501f92e commit 78e2618

File tree

10 files changed

+443
-375
lines changed

10 files changed

+443
-375
lines changed

‎app/jni/BuildTelegramVoIP.cmake

+309-291
Large diffs are not rendered by default.

‎app/jni/BuildTgCalls.cmake

+2-1
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ add_library(tgcalls STATIC
7474
)
7575

7676
target_include_directories(tgcalls PRIVATE
77-
"${TGCALLS_DIR}/tgcalls"
7877
"${CMAKE_HOME_DIRECTORY}"
7978
)
8079
target_include_directories(tgcalls PUBLIC
8180
"${TGCALLS_DIR}"
81+
"${TGCALLS_DIR}/tgcalls"
8282
)
8383

8484
target_compile_options(tgcalls PUBLIC
@@ -87,6 +87,7 @@ target_compile_options(tgcalls PUBLIC
8787

8888
target_link_libraries(tgcalls PRIVATE
8989
rnnoise
90+
z
9091
)
9192

9293
target_link_libraries(tgcalls PUBLIC

‎app/jni/BuildWebRTC.cmake

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ set(WEBRTC_DIR "${THIRDPARTY_DIR}/webrtc")
140140
#"${WEBRTC_DIR}/rtc_base/thread.h"
141141
#"${WEBRTC_DIR}/api/rtp_packet_infos.h"
142142
#"${WEBRTC_DIR}/api/task_queue/task_queue_base.h"
143+
#"${WEBRTC_DIR}/api/task_queue/default_task_queue_factory_gcd.cc"
143144
#"${WEBRTC_DIR}/rtc_base/thread_annotations.h"
144145
#"${WEBRTC_DIR}/common_audio/signal_processing/dot_product_with_scale.h"
145146
#"${WEBRTC_DIR}/api/audio/audio_mixer.h"
@@ -1599,7 +1600,6 @@ add_library(webrtc STATIC
15991600
"${WEBRTC_DIR}/api/rtp_transceiver_interface.cc"
16001601
"${WEBRTC_DIR}/api/sctp_transport_interface.cc"
16011602
"${WEBRTC_DIR}/api/stats_types.cc"
1602-
"${WEBRTC_DIR}/api/task_queue/default_task_queue_factory_gcd.cc"
16031603
"${WEBRTC_DIR}/api/task_queue/task_queue_base.cc"
16041604
"${WEBRTC_DIR}/api/transport/bitrate_settings.cc"
16051605
"${WEBRTC_DIR}/api/transport/field_trial_based_config.cc"

‎app/jni/CMakeLists.txt

+4-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,6 @@ add_subdirectory(
163163
"${UTILS_DIR}"
164164
)
165165

166-
# libtgvoip
167-
include("${CMAKE_HOME_DIRECTORY}/BuildTelegramVoIP.cmake")
168-
169166
# usrsctp
170167
include("${CMAKE_HOME_DIRECTORY}/BuildUsrSCTP.cmake")
171168

@@ -181,6 +178,9 @@ include("${CMAKE_HOME_DIRECTORY}/BuildAbsl.cmake")
181178
# webrtc
182179
include("${CMAKE_HOME_DIRECTORY}/BuildWebRTC.cmake")
183180

181+
# libtgvoip
182+
include("${CMAKE_HOME_DIRECTORY}/BuildTelegramVoIP.cmake")
183+
184184
# tgcalls
185185
include("${CMAKE_HOME_DIRECTORY}/BuildTgCalls.cmake")
186186

@@ -278,6 +278,7 @@ target_compile_definitions(${NATIVE_LIB} PUBLIC
278278
)
279279
target_compile_options(${NATIVE_LIB} PUBLIC
280280
-Wall -Werror -fno-math-errno -fno-strict-aliasing -ffast-math -funroll-loops
281+
-error-limit=0
281282
)
282283

283284
# == Linking dependencies ==

‎app/jni/tgvoip.cpp

+109-17
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,22 @@
1313
* File created on 16/06/2017
1414
*/
1515

16+
#include <jni_utils.h>
17+
#include "bridge.h"
18+
1619
#include <libtgvoip/VoIPController.h>
1720
#include <libtgvoip/client/android/tg_voip_jni.h>
1821

22+
#ifndef DISABLE_TGCALLS
23+
#include <tgcalls/legacy/InstanceImplLegacy.h>
24+
#include <tgcalls/InstanceImpl.h>
25+
//#include <tgcalls/VideoCaptureInterface.h>
26+
#include <tgcalls/v2/InstanceV2Impl.h>
27+
#include <tgcalls/v2_4_0_0/InstanceV2_4_0_0Impl.h>
28+
#include <tgcalls/v2/InstanceV2ReferenceImpl.h>
29+
30+
#else
31+
1932
#ifdef HAVE_TDLIB_CRYPTO
2033
#include <tdjni/telegram_crypto.h>
2134
#else
@@ -29,9 +42,6 @@ extern "C" {
2942
#include <openssl/rand.h>
3043
}
3144

32-
#include <jni_utils.h>
33-
#include "bridge.h"
34-
3545
void telegram_aes_ige_encrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t* key, uint8_t* iv){
3646
AES_KEY akey;
3747
AES_set_encrypt_key(key, 32*8, &akey);
@@ -77,10 +87,105 @@ void telegram_aes_cbc_decrypt(uint8_t* in, uint8_t* out, size_t length, uint8_t*
7787
AES_set_decrypt_key(key, 256, &akey);
7888
AES_cbc_encrypt(in, out, length, &akey, iv, AES_DECRYPT);
7989
}
90+
#endif
91+
#endif
92+
93+
namespace tgcalls {
94+
bool isInitialized = false;
95+
96+
jclass javaTgCallsController = nullptr;
97+
jclass javaNetworkStats = nullptr;
98+
jclass javaCallConfiguration = nullptr;
99+
jclass javaCallOptions = nullptr;
100+
101+
bool initialize (JNIEnv *env) {
102+
if (isInitialized) {
103+
return true;
104+
}
105+
106+
// find Java classes
107+
#define INIT(var_name, path) var_name = jni_class::get(env, path);\
108+
if ((var_name) == nullptr) {\
109+
jni::throw_new(env, #var_name" not found by path: " path, jni_class::IllegalStateException(env)); \
110+
return false;\
111+
}
112+
INIT(javaTgCallsController, "org/thunderdog/challegram/voip/TgCallsController")
113+
INIT(javaNetworkStats, "org/thunderdog/challegram/voip/NetworkStats")
114+
INIT(javaCallConfiguration, "org/thunderdog/challegram/voip/CallConfiguration")
115+
INIT(javaCallOptions, "org/thunderdog/challegram/voip/CallOptions")
116+
#undef INIT
117+
118+
#ifndef DISABLE_TGCALLS
119+
// register tgcalls implementations
120+
#define REGISTER(impl) if (!Register<impl>()) { \
121+
jni::throw_new(env, #impl" could not be registered", jni_class::IllegalStateException(env)); \
122+
}
123+
// "2.4.4"
124+
REGISTER(InstanceImplLegacy)
125+
// "2.7.7", "5.0.0"
126+
REGISTER(InstanceImpl)
127+
// "6.0.0"
128+
REGISTER(InstanceV2_4_0_0Impl)
129+
// "7.0.0", "8.0.0", "9.0.0"
130+
REGISTER(InstanceV2Impl)
131+
// "10.0.0", "11.0.0"
132+
REGISTER(InstanceV2ReferenceImpl)
133+
#undef REGISTER
80134
#endif
81135

136+
isInitialized = true;
137+
return true;
138+
}
139+
}
140+
141+
JNI_FUNC(jlong, newTgCallsInstance,
142+
jstring jVersion,
143+
jobject jConfiguration,
144+
jobject jOptions) {
145+
146+
if (!tgcalls::initialize(env)) {
147+
return 0;
148+
}
149+
150+
std::string version = jni::from_jstring(env, jVersion);
151+
jni::Object configuration (env, jConfiguration, tgcalls::javaCallConfiguration);
152+
jni::Object options (env, jOptions, tgcalls::javaCallOptions);
153+
154+
// TODO create tgcalls instance
155+
156+
return 0;
157+
}
158+
159+
JNI_FUNC(jobjectArray, getTgCallsVersions) {
160+
#ifndef DISABLE_TGCALLS
161+
tgcalls::initialize(env);
162+
std::vector<std::string> versions (tgcalls::Meta::Versions());
163+
164+
jobjectArray jArray = env->NewObjectArray(
165+
(jsize) versions.size(),
166+
jni_class::String(env),
167+
nullptr
168+
);
169+
170+
jsize index = 0;
171+
for (const auto &version : versions) {
172+
jstring jVersion = jni::to_jstring(env, version);
173+
env->SetObjectArrayElement(jArray, index, jVersion);
174+
env->DeleteLocalRef(jVersion);
175+
index++;
176+
}
177+
178+
return jArray;
179+
#else
180+
return env->NewObjectArray(0, jni_class::String(env), nullptr);
181+
#endif
182+
}
183+
184+
// JNI initialization
185+
82186
extern "C" {
83187
int voipOnJNILoad(JavaVM *vm, JNIEnv *env) {
188+
#ifdef DISABLE_TGCALLS
84189
tgvoip::VoIPController::crypto.sha1 = &telegram_sha1;
85190
tgvoip::VoIPController::crypto.sha256 = &telegram_sha256;
86191
tgvoip::VoIPController::crypto.rand_bytes = &telegram_rand_bytes;
@@ -90,22 +195,9 @@ int voipOnJNILoad(JavaVM *vm, JNIEnv *env) {
90195
#ifndef HAVE_TDLIB_CRYPTO
91196
tgvoip::VoIPController::crypto.aes_cbc_decrypt = &telegram_aes_cbc_decrypt;
92197
tgvoip::VoIPController::crypto.aes_cbc_encrypt = &telegram_aes_cbc_encrypt;
198+
#endif
93199
#endif
94200
tgvoipRegisterNatives(env);
95201
return 0;
96202
}
97-
}
98-
99-
JNI_FUNC(jlong, newTgCallsInstance,
100-
jstring jVersion,
101-
jobject jConfiguration,
102-
jobject jOptions) {
103-
104-
std::string version = jni::from_jstring(env, jVersion);
105-
jni::Object configuration (env, jConfiguration);
106-
jni::Object options (env, jOptions);
107-
108-
// TODO create tgcalls instance
109-
110-
return 0;
111203
}

‎app/jni/third_party/libtgvoip

‎app/src/main/java/org/thunderdog/challegram/N.java

+1
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@ public static native long newTgCallsInstance (
130130
@NonNull CallConfiguration configuration,
131131
@NonNull CallOptions options
132132
);
133+
public static native String[] getTgCallsVersions ();
133134
}

‎app/src/main/java/org/thunderdog/challegram/voip/TgCallsController.java

+2-54
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,17 @@
11
package org.thunderdog.challegram.voip;
22

3-
import androidx.annotation.IntDef;
43
import androidx.annotation.NonNull;
54

5+
import org.thunderdog.challegram.N;
66
import org.thunderdog.challegram.voip.annotation.CallNetworkType;
77

8-
import java.lang.annotation.Retention;
9-
import java.lang.annotation.RetentionPolicy;
10-
118
public class TgCallsController extends VoIPInstance {
12-
@Retention(RetentionPolicy.SOURCE)
13-
@IntDef({
14-
Version.LEGACY,
15-
Version.V1,
16-
Version.V2_4_0_0,
17-
Version.V2,
18-
Version.V2_REFERENCE
19-
})
20-
public @interface Version {
21-
int
22-
LEGACY = 0,
23-
V1 = 1,
24-
V2_4_0_0 = 2,
25-
V2 = 3,
26-
V2_REFERENCE = 4;
27-
}
28-
29-
public static class UnknownVersionException extends IllegalArgumentException {
30-
public UnknownVersionException (String s) {
31-
super(s);
32-
}
33-
}
34-
35-
private static @Version int parseVersion (@NonNull String version) throws UnknownVersionException {
36-
switch (version) {
37-
case "2.4.4":
38-
// TODO? InstanceImplLegacy.cpp
39-
return Version.LEGACY;
40-
case "2.7.7":
41-
case "5.0.0":
42-
// TODO InstanceImpl.cpp
43-
return Version.V1;
44-
case "6.0.0":
45-
// TODO InstanceV2_4_0_0Impl.cpp
46-
return Version.V2_4_0_0;
47-
case "7.0.0":
48-
case "8.0.0":
49-
case "9.0.0":
50-
// TODO InstanceV2Impl.cpp
51-
return Version.V2;
52-
case "10.0.0":
53-
case "11.0.0":
54-
// TODO InstanceV2ReferenceImpl.cpp
55-
return Version.V2_REFERENCE;
56-
}
57-
throw new IllegalArgumentException(version);
58-
}
59-
609
private final String version;
61-
private final @Version int internalVersion;
6210
private long nativePtr;
6311
public TgCallsController (@NonNull CallConfiguration configuration, @NonNull CallOptions options, @NonNull ConnectionStateListener stateListener, String version) {
6412
super(configuration, options, stateListener);
6513
this.version = version;
66-
this.internalVersion = parseVersion(version);
14+
this.nativePtr = N.newTgCallsInstance(version, configuration, options);
6715
}
6816

6917
@Override

‎app/src/main/java/org/thunderdog/challegram/voip/VoIP.java

+13-6
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,31 @@
1010

1111
import org.drinkless.td.libcore.telegram.TdApi;
1212
import org.thunderdog.challegram.Log;
13+
import org.thunderdog.challegram.N;
1314
import org.thunderdog.challegram.config.Config;
1415
import org.thunderdog.challegram.telegram.Tdlib;
1516
import org.thunderdog.challegram.voip.annotation.CallNetworkType;
1617
import org.webrtc.ContextUtils;
1718

1819
import java.io.File;
20+
import java.util.Collections;
21+
import java.util.LinkedHashSet;
22+
import java.util.Set;
1923

24+
import me.vkryl.core.ArrayUtils;
2025
import me.vkryl.core.StringUtils;
2126

2227
public class VoIP {
2328
public static TdApi.CallProtocol getProtocol () {
29+
Set<String> versions = new LinkedHashSet<>();
30+
versions.add(VoIPController.getVersion());
31+
Collections.addAll(versions, N.getTgCallsVersions());
2432
return new TdApi.CallProtocol(
2533
true,
2634
true,
2735
Config.VOIP_CONNECTION_MIN_LAYER,
2836
VoIPController.getConnectionMaxLayer(),
29-
new String[] {
30-
VoIPController.getVersion()
31-
}
37+
versions.toArray(new String[0])
3238
);
3339
}
3440

@@ -62,6 +68,7 @@ public static VoIPInstance instantiateAndConnect (
6268
boolean isMicDisabled
6369
) throws IllegalArgumentException {
6470
final String libtgvoipVersion = VoIPController.getVersion();
71+
final String[] tgCallsVersions = N.getTgCallsVersions();
6572

6673
final File persistentStateFile = VoIPPersistentConfig.getVoipConfigFile();
6774
final File callLogFile = VoIPLogs.getNewFile(true);
@@ -103,16 +110,16 @@ public static VoIPInstance instantiateAndConnect (
103110
options,
104111
connectionStateListener
105112
);
106-
} else {
113+
} else if (ArrayUtils.contains(tgCallsVersions, version)) {
107114
try {
108115
tgcalls = new TgCallsController(
109116
configuration,
110117
options,
111118
connectionStateListener,
112119
version
113120
);
114-
} catch (TgCallsController.UnknownVersionException e) {
115-
Log.i("Unknown tgcalls version: %s", version);
121+
} catch (Throwable t) {
122+
Log.i("Unknown tgcalls %s", t, version);
116123
}
117124
}
118125
if (tgcalls != null) {

0 commit comments

Comments
 (0)