-
Notifications
You must be signed in to change notification settings - Fork 88
/
android_simulcast.patch
436 lines (425 loc) · 15.8 KB
/
android_simulcast.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
diff --git a/sdk/android/BUILD.gn b/sdk/android/BUILD.gn
index 8858d21b60..3911b834e1 100644
--- a/sdk/android/BUILD.gn
+++ b/sdk/android/BUILD.gn
@@ -528,6 +529,8 @@ if (is_android) {
sources = [
"api/org/webrtc/SoftwareVideoDecoderFactory.java",
"api/org/webrtc/SoftwareVideoEncoderFactory.java",
+ "api/org/webrtc/SimulcastVideoEncoder.java",
+ "api/org/webrtc/SimulcastVideoEncoderFactory.java",
]
deps = [
@@ -874,6 +877,23 @@ if (current_os == "linux" || is_android) {
]
}
+ rtc_library("simulcast_jni") {
+ visibility = [ "*" ]
+ allow_poison = [ "software_video_codecs" ]
+ sources = [
+ "src/jni/simulcast_video_encoder.cc",
+ "src/jni/simulcast_video_encoder.h",
+ "src/jni/simulcast_video_encoder_factory.cc",
+ "src/jni/simulcast_video_encoder_factory.h"
+ ]
+ deps = [
+ ":base_jni",
+ ":video_jni",
+ ":native_api_codecs",
+ "../../media:rtc_simulcast_encoder_adapter"
+ ]
+ }
+
rtc_library("libaom_av1_encoder_jni") {
visibility = [ "*" ]
allow_poison = [ "software_video_codecs" ]
@@ -911,6 +931,7 @@ if (current_os == "linux" || is_android) {
":generated_swcodecs_jni",
":libvpx_vp8_jni",
":libvpx_vp9_jni",
+ ":simulcast_jni",
":native_api_jni",
":video_jni",
"../../api/environment",
diff --git a/sdk/android/api/org/webrtc/RtpParameters.java b/sdk/android/api/org/webrtc/RtpParameters.java
index 9ca8311610..d6a22662bb 100644
--- a/sdk/android/api/org/webrtc/RtpParameters.java
+++ b/sdk/android/api/org/webrtc/RtpParameters.java
@@ -76,6 +76,7 @@ public class RtpParameters {
// If non-null, scale the width and height down by this factor for video. If null,
// implementation default scaling factor will be used.
@Nullable public Double scaleResolutionDownBy;
+ @Nullable public String scalabilityMode;
// SSRC to be used by this encoding.
// Can't be changed between getParameters/setParameters.
public Long ssrc;
@@ -93,7 +94,7 @@ public class RtpParameters {
@CalledByNative("Encoding")
Encoding(String rid, boolean active, double bitratePriority, @Priority int networkPriority,
Integer maxBitrateBps, Integer minBitrateBps, Integer maxFramerate,
- Integer numTemporalLayers, Double scaleResolutionDownBy, Long ssrc,
+ Integer numTemporalLayers, Double scaleResolutionDownBy, String scalabilityMode, Long ssrc,
boolean adaptiveAudioPacketTime) {
this.rid = rid;
this.active = active;
@@ -104,10 +105,17 @@ public class RtpParameters {
this.maxFramerate = maxFramerate;
this.numTemporalLayers = numTemporalLayers;
this.scaleResolutionDownBy = scaleResolutionDownBy;
+ this.scalabilityMode = scalabilityMode;
this.ssrc = ssrc;
this.adaptiveAudioPacketTime = adaptiveAudioPacketTime;
}
+ @Nullable
+ @CalledByNative("Encoding")
+ String getScalabilityMode() {
+ return scalabilityMode;
+ }
+
@Nullable
@CalledByNative("Encoding")
String getRid() {
diff --git a/sdk/android/api/org/webrtc/SimulcastVideoEncoder.java b/sdk/android/api/org/webrtc/SimulcastVideoEncoder.java
new file mode 100644
index 0000000000..1bea910fae
--- /dev/null
+++ b/sdk/android/api/org/webrtc/SimulcastVideoEncoder.java
@@ -0,0 +1,27 @@
+package org.webrtc;
+
+public class SimulcastVideoEncoder extends WrappedNativeVideoEncoder {
+
+ static native long nativeCreateEncoder(long webrtcEnvRef, VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info);
+
+ VideoEncoderFactory primary;
+ VideoEncoderFactory fallback;
+ VideoCodecInfo info;
+
+ public SimulcastVideoEncoder(VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info) {
+ this.primary = primary;
+ this.fallback = fallback;
+ this.info = info;
+ }
+
+ @Override
+ public long createNative(long webrtcEnvRef) {
+ return nativeCreateEncoder(webrtcEnvRef, primary, fallback, info);
+ }
+
+ @Override
+ public boolean isHardwareEncoder() {
+ return false;
+ }
+
+}
diff --git a/sdk/android/api/org/webrtc/SimulcastVideoEncoderFactory.java b/sdk/android/api/org/webrtc/SimulcastVideoEncoderFactory.java
new file mode 100644
index 0000000000..21339f8349
--- /dev/null
+++ b/sdk/android/api/org/webrtc/SimulcastVideoEncoderFactory.java
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2017 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+package org.webrtc;
+
+import androidx.annotation.Nullable;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Arrays;
+
+public class SimulcastVideoEncoderFactory implements VideoEncoderFactory {
+
+ static native List<VideoCodecInfo> nativeVP9Codecs();
+ static native VideoCodecInfo nativeAV1Codec();
+
+ VideoEncoderFactory primary;
+ VideoEncoderFactory fallback;
+
+ public SimulcastVideoEncoderFactory(VideoEncoderFactory primary, VideoEncoderFactory fallback) {
+ this.primary = primary;
+ this.fallback = fallback;
+ }
+
+ @Nullable
+ @Override
+ public VideoEncoder createEncoder(VideoCodecInfo info) {
+ return new SimulcastVideoEncoder(primary, fallback, info);
+ }
+
+ @Override
+ public VideoCodecInfo[] getSupportedCodecs() {
+ List<VideoCodecInfo> codecs = new ArrayList<VideoCodecInfo>();
+ codecs.addAll(Arrays.asList(primary.getSupportedCodecs()));
+ if (fallback != null) {
+ codecs.addAll(Arrays.asList(fallback.getSupportedCodecs()));
+ }
+ codecs.addAll(nativeVP9Codecs());
+ codecs.add(nativeAV1Codec());
+ return codecs.toArray(new VideoCodecInfo[codecs.size()]);
+ }
+
+}
diff --git a/sdk/android/api/org/webrtc/VideoCodecInfo.java b/sdk/android/api/org/webrtc/VideoCodecInfo.java
index 4f97cf74cf..7e57d3fa4c 100644
--- a/sdk/android/api/org/webrtc/VideoCodecInfo.java
+++ b/sdk/android/api/org/webrtc/VideoCodecInfo.java
@@ -34,6 +34,7 @@ public class VideoCodecInfo {
public final String name;
public final Map<String, String> params;
+ public int[] scalabilityModes;
@Deprecated public final int payload;
@CalledByNative
@@ -41,6 +42,7 @@ public class VideoCodecInfo {
this.payload = 0;
this.name = name;
this.params = params;
+ this.scalabilityModes = new int[0];
}
@Deprecated
@@ -48,6 +50,7 @@ public class VideoCodecInfo {
this.payload = payload;
this.name = name;
this.params = params;
+ this.scalabilityModes = new int[0];
}
@Override
@@ -83,4 +86,14 @@ public class VideoCodecInfo {
Map getParams() {
return params;
}
+
+ @CalledByNative
+ int[] getScalabilityModes() {
+ return scalabilityModes;
+ }
+
+ @CalledByNative
+ void setScalabilityModes(int[] values) {
+ scalabilityModes = values;
+ }
}
diff --git a/sdk/android/src/jni/pc/rtp_parameters.cc b/sdk/android/src/jni/pc/rtp_parameters.cc
index 4bd9ee0e1d..dfc7f17f6d 100644
--- a/sdk/android/src/jni/pc/rtp_parameters.cc
+++ b/sdk/android/src/jni/pc/rtp_parameters.cc
@@ -53,6 +53,7 @@ ScopedJavaLocalRef<jobject> NativeToJavaRtpEncodingParameter(
NativeToJavaInteger(env, encoding.max_framerate),
NativeToJavaInteger(env, encoding.num_temporal_layers),
NativeToJavaDouble(env, encoding.scale_resolution_down_by),
+ NativeToJavaString(env, encoding.scalability_mode),
encoding.ssrc ? NativeToJavaLong(env, *encoding.ssrc) : nullptr,
encoding.adaptive_ptime);
}
@@ -116,6 +117,11 @@ RtpEncodingParameters JavaToNativeRtpEncodingParameters(
Java_Encoding_getScaleResolutionDownBy(jni, j_encoding_parameters);
encoding.scale_resolution_down_by =
JavaToNativeOptionalDouble(jni, j_scale_resolution_down_by);
+ ScopedJavaLocalRef<jstring> j_scalability_mode =
+ Java_Encoding_getScalabilityMode(jni, j_encoding_parameters);
+ if (!IsNull(jni, j_scalability_mode)) {
+ encoding.scalability_mode = JavaToNativeString(jni, j_scalability_mode);
+ }
encoding.adaptive_ptime =
Java_Encoding_getAdaptivePTime(jni, j_encoding_parameters);
ScopedJavaLocalRef<jobject> j_ssrc =
diff --git a/sdk/android/src/jni/simulcast_video_encoder.cc b/sdk/android/src/jni/simulcast_video_encoder.cc
new file mode 100644
index 0000000000..3f0ca5331c
--- /dev/null
+++ b/sdk/android/src/jni/simulcast_video_encoder.cc
@@ -0,0 +1,35 @@
+#include <jni.h>
+
+#include "api/transport/field_trial_based_config.h"
+#include "sdk/android/src/jni/jni_helpers.h"
+#include "sdk/android/src/jni/video_encoder_factory_wrapper.h"
+#include "sdk/android/src/jni/video_codec_info.h"
+#include "sdk/android/native_api/codecs/wrapper.h"
+#include "media/engine/simulcast_encoder_adapter.h"
+#include "rtc_base/logging.h"
+
+using namespace webrtc;
+using namespace webrtc::jni;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// (VideoEncoderFactory primary, VideoEncoderFactory fallback, VideoCodecInfo info)
+JNIEXPORT jlong JNICALL Java_org_webrtc_SimulcastVideoEncoder_nativeCreateEncoder(JNIEnv *env, jclass klass, jlong webrtcEnvRef, jobject primary, jobject fallback, jobject info) {
+ RTC_LOG(LS_INFO) << "Create simulcast video encoder";
+ JavaParamRef<jobject> info_ref(env, info);
+ SdpVideoFormat format = VideoCodecInfoToSdpVideoFormat(env, info_ref);
+
+ // TODO: 影響は軽微だが、リークする可能性があるので将来的に修正したい
+ // https://github.com/shiguredo-webrtc-build/webrtc-build/pull/16#pullrequestreview-600675795
+ return NativeToJavaPointer(std::make_unique<SimulcastEncoderAdapter>(
+ *reinterpret_cast<const webrtc::Environment*>(webrtcEnvRef),
+ JavaToNativeVideoEncoderFactory(env, primary).release(),
+ fallback != nullptr ? JavaToNativeVideoEncoderFactory(env, fallback).release() : nullptr,
+ format).release());
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/sdk/android/src/jni/simulcast_video_encoder.h b/sdk/android/src/jni/simulcast_video_encoder.h
new file mode 100644
index 0000000000..985fc036b7
--- /dev/null
+++ b/sdk/android/src/jni/simulcast_video_encoder.h
@@ -0,0 +1,21 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_webrtc_SimulcastVideoEncoder */
+
+#ifndef _Included_org_webrtc_SimulcastVideoEncoder
+#define _Included_org_webrtc_SimulcastVideoEncoder
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_webrtc_SimulcastVideoEncoder
+ * Method: nativeCreateEncoder
+ * Signature: (JLorg/webrtc/VideoEncoderFactory;Lorg/webrtc/VideoEncoderFactory;Lorg/webrtc/VideoCodecInfo;)J
+ */
+JNIEXPORT jlong JNICALL Java_org_webrtc_SimulcastVideoEncoder_nativeCreateEncoder
+ (JNIEnv *, jclass, jlong, jobject, jobject, jobject);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/sdk/android/src/jni/simulcast_video_encoder_factory.cc b/sdk/android/src/jni/simulcast_video_encoder_factory.cc
new file mode 100644
index 0000000000..3bf904e989
--- /dev/null
+++ b/sdk/android/src/jni/simulcast_video_encoder_factory.cc
@@ -0,0 +1,40 @@
+#include <jni.h>
+
+#include "sdk/android/src/jni/jni_helpers.h"
+#include "sdk/android/src/jni/video_encoder_factory_wrapper.h"
+#include "sdk/android/src/jni/video_codec_info.h"
+#include "sdk/android/native_api/codecs/wrapper.h"
+#include "sdk/android/native_api/jni/class_loader.h"
+#include "modules/video_coding/codecs/av1/av1_svc_config.h"
+#include "modules/video_coding/codecs/vp9/include/vp9.h"
+#include "media/base/media_constants.h"
+#include "media/engine/simulcast_encoder_adapter.h"
+#include "absl/container/inlined_vector.h"
+#include "api/video_codecs/video_codec.h"
+#include "api/video_codecs/sdp_video_format.h"
+#include "api/video_codecs/video_codec.h"
+
+using namespace webrtc;
+using namespace webrtc::jni;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+JNIEXPORT jobject JNICALL Java_org_webrtc_SimulcastVideoEncoderFactory_nativeVP9Codecs
+ (JNIEnv *env, jclass klass) {
+ std::vector<SdpVideoFormat> formats = SupportedVP9Codecs(true);
+ return NativeToJavaList(env, formats, &SdpVideoFormatToVideoCodecInfo).Release();
+}
+
+JNIEXPORT jobject JNICALL Java_org_webrtc_SimulcastVideoEncoderFactory_nativeAV1Codec
+ (JNIEnv *env, jclass klass) {
+ SdpVideoFormat format = SdpVideoFormat(
+ cricket::kAv1CodecName, webrtc::CodecParameterMap(),
+ LibaomAv1EncoderSupportedScalabilityModes());
+ return SdpVideoFormatToVideoCodecInfo(env, format).Release();
+}
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/sdk/android/src/jni/simulcast_video_encoder_factory.h b/sdk/android/src/jni/simulcast_video_encoder_factory.h
new file mode 100644
index 0000000000..d7426953a0
--- /dev/null
+++ b/sdk/android/src/jni/simulcast_video_encoder_factory.h
@@ -0,0 +1,29 @@
+/* DO NOT EDIT THIS FILE - it is machine generated */
+#include <jni.h>
+/* Header for class org_webrtc_SimulcastVideoEncoderFactory */
+
+#ifndef _Included_org_webrtc_SimulcastVideoEncoderFactory
+#define _Included_org_webrtc_SimulcastVideoEncoderFactory
+#ifdef __cplusplus
+extern "C" {
+#endif
+/*
+ * Class: org_webrtc_SimulcastVideoEncoderFactory
+ * Method: nativeVP9Codecs
+ * Signature: ()Ljava/util/List;
+ */
+JNIEXPORT jobject JNICALL Java_org_webrtc_SimulcastVideoEncoderFactory_nativeVP9Codecs
+ (JNIEnv *, jclass);
+
+/*
+ * Class: org_webrtc_SimulcastVideoEncoderFactory
+ * Method: nativeAV1Codec
+ * Signature: ()Lorg/webrtc/VideoCodecInfo;
+ */
+JNIEXPORT jobject JNICALL Java_org_webrtc_SimulcastVideoEncoderFactory_nativeAV1Codec
+ (JNIEnv *, jclass);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/sdk/android/src/jni/video_codec_info.cc b/sdk/android/src/jni/video_codec_info.cc
index a218a1d23f..22deafadd4 100644
--- a/sdk/android/src/jni/video_codec_info.cc
+++ b/sdk/android/src/jni/video_codec_info.cc
@@ -14,14 +14,22 @@
#include "sdk/android/native_api/jni/java_types.h"
#include "sdk/android/src/jni/jni_helpers.h"
+#include "absl/container/inlined_vector.h"
+
namespace webrtc {
namespace jni {
SdpVideoFormat VideoCodecInfoToSdpVideoFormat(JNIEnv* jni,
const JavaRef<jobject>& j_info) {
+ absl::InlinedVector<webrtc::ScalabilityMode, webrtc::kScalabilityModeCount> scalabilityModes;
+ std::vector<int32_t> javaScalabilityModes = JavaToNativeIntArray(jni, Java_VideoCodecInfo_getScalabilityModes(jni, j_info));
+ for (const auto& scalabilityMode : javaScalabilityModes) {
+ scalabilityModes.push_back(static_cast<webrtc::ScalabilityMode>(scalabilityMode));
+ }
return SdpVideoFormat(
JavaToNativeString(jni, Java_VideoCodecInfo_getName(jni, j_info)),
- JavaToNativeStringMap(jni, Java_VideoCodecInfo_getParams(jni, j_info)));
+ JavaToNativeStringMap(jni, Java_VideoCodecInfo_getParams(jni, j_info)),
+ scalabilityModes);
}
ScopedJavaLocalRef<jobject> SdpVideoFormatToVideoCodecInfo(
@@ -29,8 +37,17 @@ ScopedJavaLocalRef<jobject> SdpVideoFormatToVideoCodecInfo(
const SdpVideoFormat& format) {
ScopedJavaLocalRef<jobject> j_params =
NativeToJavaStringMap(jni, format.parameters);
- return Java_VideoCodecInfo_Constructor(
+ ScopedJavaLocalRef<jobject> codec = Java_VideoCodecInfo_Constructor(
jni, NativeToJavaString(jni, format.name), j_params);
+
+ size_t size = format.scalability_modes.size();
+ std::vector<int32_t> temp(size);
+ for (size_t i = 0; i < size; i++) {
+ temp[i] = static_cast<jint>(format.scalability_modes[i]);
+ }
+ Java_VideoCodecInfo_setScalabilityModes(jni, codec, NativeToJavaIntArray(jni, temp));
+
+ return codec;
}
} // namespace jni