Skip to content

Commit ed1caf5

Browse files
committed
Patch to m125
1 parent 83836b7 commit ed1caf5

34 files changed

+841
-452
lines changed
Binary file not shown.
Binary file not shown.
261 KB
Binary file not shown.
35.7 KB
Binary file not shown.

stream-webrtc-android/src/main/java/org/webrtc/AudioTrack.java

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010

1111
package org.webrtc;
1212

13+
import java.util.IdentityHashMap;
14+
1315
/** Java wrapper for a C++ AudioTrackInterface */
1416
public class AudioTrack extends MediaStreamTrack {
17+
private final IdentityHashMap<AudioTrackSink, Long> sinks = new IdentityHashMap<AudioTrackSink, Long>();
18+
1519
public AudioTrack(long nativeTrack) {
1620
super(nativeTrack);
1721
}
@@ -23,10 +27,54 @@ public void setVolume(double volume) {
2327
nativeSetVolume(getNativeAudioTrack(), volume);
2428
}
2529

30+
/**
31+
* Adds an AudioTrackSink to the track. This callback is only
32+
* called for remote audio tracks.
33+
*
34+
* Repeated addSink calls will not add the sink multiple times.
35+
*/
36+
public void addSink(AudioTrackSink sink) {
37+
if (sink == null) {
38+
throw new IllegalArgumentException("The AudioTrackSink is not allowed to be null");
39+
}
40+
if (!sinks.containsKey(sink)) {
41+
final long nativeSink = nativeWrapSink(sink);
42+
sinks.put(sink, nativeSink);
43+
nativeAddSink(getNativeMediaStreamTrack(), nativeSink);
44+
}
45+
}
46+
47+
/**
48+
* Removes an AudioTrackSink from the track.
49+
*
50+
* If the AudioTrackSink was not attached to the track, this is a no-op.
51+
*/
52+
public void removeSink(AudioTrackSink sink) {
53+
final Long nativeSink = sinks.remove(sink);
54+
if (nativeSink != null) {
55+
nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
56+
nativeFreeSink(nativeSink);
57+
}
58+
}
59+
60+
@Override
61+
public void dispose() {
62+
for (long nativeSink : sinks.values()) {
63+
nativeRemoveSink(getNativeMediaStreamTrack(), nativeSink);
64+
nativeFreeSink(nativeSink);
65+
}
66+
sinks.clear();
67+
super.dispose();
68+
}
69+
2670
/** Returns a pointer to webrtc::AudioTrackInterface. */
2771
long getNativeAudioTrack() {
2872
return getNativeMediaStreamTrack();
2973
}
3074

3175
private static native void nativeSetVolume(long track, double volume);
32-
}
76+
private static native void nativeAddSink(long track, long nativeSink);
77+
private static native void nativeRemoveSink(long track, long nativeSink);
78+
private static native long nativeWrapSink(AudioTrackSink sink);
79+
private static native void nativeFreeSink(long sink);
80+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2023 The WebRTC project authors. All Rights Reserved.
3+
*
4+
* Use of this source code is governed by a BSD-style license
5+
* that can be found in the LICENSE file in the root of the source
6+
* tree. An additional intellectual property rights grant can be found
7+
* in the file PATENTS. All contributing project authors may
8+
* be found in the AUTHORS file in the root of the source tree.
9+
*/
10+
11+
package org.webrtc;
12+
13+
import java.nio.ByteBuffer;
14+
15+
/**
16+
* Java version of rtc::AudioTrackSinkInterface.
17+
*/
18+
public interface AudioTrackSink {
19+
/**
20+
* Implementations should copy the audio data into a local copy if they wish
21+
* to use the data after this function returns.
22+
*/
23+
@CalledByNative
24+
void onData(ByteBuffer audioData, int bitsPerSample, int sampleRate,
25+
int numberOfChannels, int numberOfFrames,
26+
long absoluteCaptureTimestampMs);
27+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.webrtc;
18+
19+
import androidx.annotation.Nullable;
20+
21+
public class FrameCryptor {
22+
public enum FrameCryptionState {
23+
NEW,
24+
OK,
25+
ENCRYPTIONFAILED,
26+
DECRYPTIONFAILED,
27+
MISSINGKEY,
28+
KEYRATCHETED,
29+
INTERNALERROR;
30+
31+
@CalledByNative("FrameCryptionState")
32+
static FrameCryptionState fromNativeIndex(int nativeIndex) {
33+
return values()[nativeIndex];
34+
}
35+
}
36+
37+
public static interface Observer {
38+
@CalledByNative("Observer")
39+
void onFrameCryptionStateChanged(String participantId, FrameCryptionState newState);
40+
}
41+
42+
private long nativeFrameCryptor;
43+
private long observerPtr;
44+
45+
public long getNativeFrameCryptor() {
46+
return nativeFrameCryptor;
47+
}
48+
49+
@CalledByNative
50+
public FrameCryptor(long nativeFrameCryptor) {
51+
this.nativeFrameCryptor = nativeFrameCryptor;
52+
this.observerPtr = 0;
53+
}
54+
55+
public void setEnabled(boolean enabled) {
56+
checkFrameCryptorExists();
57+
nativeSetEnabled(nativeFrameCryptor, enabled);
58+
}
59+
60+
public boolean isEnabled() {
61+
checkFrameCryptorExists();
62+
return nativeIsEnabled(nativeFrameCryptor);
63+
}
64+
65+
public int getKeyIndex() {
66+
checkFrameCryptorExists();
67+
return nativeGetKeyIndex(nativeFrameCryptor);
68+
}
69+
70+
public void setKeyIndex(int index) {
71+
checkFrameCryptorExists();
72+
nativeSetKeyIndex(nativeFrameCryptor, index);
73+
}
74+
75+
public void dispose() {
76+
checkFrameCryptorExists();
77+
nativeUnSetObserver(nativeFrameCryptor);
78+
JniCommon.nativeReleaseRef(nativeFrameCryptor);
79+
nativeFrameCryptor = 0;
80+
if (observerPtr != 0) {
81+
JniCommon.nativeReleaseRef(observerPtr);
82+
observerPtr = 0;
83+
}
84+
}
85+
86+
public void setObserver(@Nullable Observer observer) {
87+
checkFrameCryptorExists();
88+
long newPtr = nativeSetObserver(nativeFrameCryptor, observer);
89+
if (observerPtr != 0) {
90+
JniCommon.nativeReleaseRef(observerPtr);
91+
observerPtr = 0;
92+
}
93+
newPtr = observerPtr;
94+
}
95+
96+
private void checkFrameCryptorExists() {
97+
if (nativeFrameCryptor == 0) {
98+
throw new IllegalStateException("FrameCryptor has been disposed.");
99+
}
100+
}
101+
102+
private static native void nativeSetEnabled(long frameCryptorPointer, boolean enabled);
103+
private static native boolean nativeIsEnabled(long frameCryptorPointer);
104+
private static native void nativeSetKeyIndex(long frameCryptorPointer, int index);
105+
private static native int nativeGetKeyIndex(long frameCryptorPointer);
106+
private static native long nativeSetObserver(long frameCryptorPointer, Observer observer);
107+
private static native void nativeUnSetObserver(long frameCryptorPointer);
108+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.webrtc;
18+
19+
public enum FrameCryptorAlgorithm {
20+
AES_GCM,
21+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.webrtc;
18+
19+
public class FrameCryptorFactory {
20+
public static FrameCryptorKeyProvider createFrameCryptorKeyProvider(
21+
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady) {
22+
return nativeCreateFrameCryptorKeyProvider(sharedKey, ratchetSalt, ratchetWindowSize, uncryptedMagicBytes, failureTolerance, keyRingSize, discardFrameWhenCryptorNotReady);
23+
}
24+
25+
public static FrameCryptor createFrameCryptorForRtpSender(PeerConnectionFactory factory, RtpSender rtpSender,
26+
String participantId, FrameCryptorAlgorithm algorithm, FrameCryptorKeyProvider keyProvider) {
27+
return nativeCreateFrameCryptorForRtpSender(factory.getNativeOwnedFactoryAndThreads(),rtpSender.getNativeRtpSender(), participantId,
28+
algorithm.ordinal(), keyProvider.getNativeKeyProvider());
29+
}
30+
31+
public static FrameCryptor createFrameCryptorForRtpReceiver(PeerConnectionFactory factory, RtpReceiver rtpReceiver,
32+
String participantId, FrameCryptorAlgorithm algorithm, FrameCryptorKeyProvider keyProvider) {
33+
return nativeCreateFrameCryptorForRtpReceiver(factory.getNativeOwnedFactoryAndThreads(), rtpReceiver.getNativeRtpReceiver(), participantId,
34+
algorithm.ordinal(), keyProvider.getNativeKeyProvider());
35+
}
36+
37+
private static native FrameCryptor nativeCreateFrameCryptorForRtpSender(long factory,
38+
long rtpSender, String participantId, int algorithm, long nativeFrameCryptorKeyProvider);
39+
private static native FrameCryptor nativeCreateFrameCryptorForRtpReceiver(long factory,
40+
long rtpReceiver, String participantId, int algorithm, long nativeFrameCryptorKeyProvider);
41+
42+
private static native FrameCryptorKeyProvider nativeCreateFrameCryptorKeyProvider(
43+
boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize, byte[] uncryptedMagicBytes, int failureTolerance, int keyRingSize, boolean discardFrameWhenCryptorNotReady);
44+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2022 LiveKit
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.webrtc;
18+
19+
import java.util.ArrayList;
20+
21+
public class FrameCryptorKeyProvider {
22+
private long nativeKeyProvider;
23+
24+
@CalledByNative
25+
public FrameCryptorKeyProvider(long nativeKeyProvider) {
26+
this.nativeKeyProvider = nativeKeyProvider;
27+
}
28+
29+
public long getNativeKeyProvider() {
30+
return nativeKeyProvider;
31+
}
32+
33+
public boolean setSharedKey(int index, byte[] key) {
34+
checkKeyProviderExists();
35+
return nativeSetSharedKey(nativeKeyProvider,index, key);
36+
}
37+
38+
public byte[] ratchetSharedKey(int index) {
39+
checkKeyProviderExists();
40+
return nativeRatchetSharedKey(nativeKeyProvider, index);
41+
}
42+
43+
public byte[] exportSharedKey(int index) {
44+
checkKeyProviderExists();
45+
return nativeExportSharedKey(nativeKeyProvider, index);
46+
}
47+
48+
public boolean setKey(String participantId, int index, byte[] key) {
49+
checkKeyProviderExists();
50+
return nativeSetKey(nativeKeyProvider, participantId, index, key);
51+
}
52+
53+
public byte[] ratchetKey(String participantId, int index) {
54+
checkKeyProviderExists();
55+
return nativeRatchetKey(nativeKeyProvider, participantId, index);
56+
}
57+
58+
public byte[] exportKey(String participantId, int index) {
59+
checkKeyProviderExists();
60+
return nativeExportKey(nativeKeyProvider, participantId, index);
61+
}
62+
63+
public void setSifTrailer(byte[] sifTrailer) {
64+
checkKeyProviderExists();
65+
nativeSetSifTrailer(nativeKeyProvider, sifTrailer);
66+
}
67+
68+
public void dispose() {
69+
checkKeyProviderExists();
70+
JniCommon.nativeReleaseRef(nativeKeyProvider);
71+
nativeKeyProvider = 0;
72+
}
73+
74+
private void checkKeyProviderExists() {
75+
if (nativeKeyProvider == 0) {
76+
throw new IllegalStateException("FrameCryptorKeyProvider has been disposed.");
77+
}
78+
}
79+
private static native boolean nativeSetSharedKey(
80+
long keyProviderPointer, int index, byte[] key);
81+
private static native byte[] nativeRatchetSharedKey(
82+
long keyProviderPointer, int index);
83+
private static native byte[] nativeExportSharedKey(
84+
long keyProviderPointer, int index);
85+
private static native boolean nativeSetKey(
86+
long keyProviderPointer, String participantId, int index, byte[] key);
87+
private static native byte[] nativeRatchetKey(
88+
long keyProviderPointer, String participantId, int index);
89+
private static native byte[] nativeExportKey(
90+
long keyProviderPointer, String participantId, int index);
91+
private static native void nativeSetSifTrailer(
92+
long keyProviderPointer, byte[] sifTrailer);
93+
}

0 commit comments

Comments
 (0)