Skip to content

Commit

Permalink
chore: for android.
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudwebrtc committed Mar 29, 2023
1 parent 77ed9d6 commit b36765f
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 58 deletions.
9 changes: 5 additions & 4 deletions sdk/android/api/org/webrtc/FrameCryptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@

public class FrameCryptor {

public enum FrameCryptorErrorState {
public enum FrameCryptionState {
NEW,
OK,
ENCRYPTIONFAILED,
DECRYPTIONFAILED,
MISSINGKEY,
KEYRATCHETED,
INTERNALERROR;

@CalledByNative("FrameCryptorErrorState")
static FrameCryptorErrorState fromNativeIndex(int nativeIndex) {
@CalledByNative("FrameCryptionState")
static FrameCryptionState fromNativeIndex(int nativeIndex) {
return values()[nativeIndex];
}
}

public static interface Observer {
@CalledByNative("Observer")
void onFrameCryptorErrorState(String participantId, FrameCryptorErrorState newState);
void onFrameCryptionStateChanged(String participantId, FrameCryptionState newState);
}

private long nativeFrameCryptor;
Expand Down
6 changes: 3 additions & 3 deletions sdk/android/api/org/webrtc/FrameCryptorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.webrtc;

public class FrameCryptorFactory {
public static FrameCryptorKeyManager createFrameCryptorKeyManager() {
return nativeCreateFrameCryptorKeyManager();
public static FrameCryptorKeyManager createFrameCryptorKeyManager(boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize) {
return nativeCreateFrameCryptorKeyManager(sharedKey, ratchetSalt, ratchetWindowSize);
}

public static FrameCryptor createFrameCryptorForRtpSender(RtpSender rtpSender,
Expand All @@ -38,5 +38,5 @@ private static native FrameCryptor nativeCreateFrameCryptorForRtpSender(
private static native FrameCryptor nativeCreateFrameCryptorForRtpReceiver(
long rtpReceiver, String participantId, int algorithm, long nativeFrameCryptorKeyManager);

private static native FrameCryptorKeyManager nativeCreateFrameCryptorKeyManager();
private static native FrameCryptorKeyManager nativeCreateFrameCryptorKeyManager(boolean sharedKey, byte[] ratchetSalt, int ratchetWindowSize);
}
17 changes: 5 additions & 12 deletions sdk/android/api/org/webrtc/FrameCryptorKeyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ public boolean setKey(String participantId, int index, byte[] key) {
return nativeSetKey(nativeKeyManager, participantId, index, key);
}

public boolean setKeys(String participantId, ArrayList<byte[]> keys) {
return nativeSetKeys(nativeKeyManager, participantId, keys);
}

public ArrayList<byte[]> getKeys(String participantId) {
return nativeGetKeys(nativeKeyManager, participantId);
public byte[] ratchetKey(String participantId, int index) {
return nativeRatchetKey(nativeKeyManager, participantId, index);
}

public void dispose() {
Expand All @@ -53,12 +49,9 @@ private void checkKeyManagerExists() {
throw new IllegalStateException("FrameCryptorKeyManager has been disposed.");
}
}

private static native long createNativeKeyManager();

private static native boolean nativeSetKey(
long keyManagerPointer, String participantId, int index, byte[] key);
private static native boolean nativeSetKeys(
long keyManagerPointer, String participantId, ArrayList<byte[]> keys);
private static native ArrayList<byte[]> nativeGetKeys(
long keyManagerPointer, String participantId);
private static native byte[] nativeRatchetKey(
long keyManagerPointer, String participantId, int index);
}
20 changes: 20 additions & 0 deletions sdk/android/api/org/webrtc/OpenH264Decoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 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;

public class OpenH264Decoder extends WrappedNativeVideoDecoder {
@Override
public long createNativeVideoDecoder() {
return nativeCreateDecoder();
}

static native long nativeCreateDecoder();
}
25 changes: 25 additions & 0 deletions sdk/android/api/org/webrtc/OpenH264Encoder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (c) 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;

public class OpenH264Encoder extends WrappedNativeVideoEncoder {
@Override
public long createNativeVideoEncoder() {
return nativeCreateEncoder();
}

static native long nativeCreateEncoder();

@Override
public boolean isHardwareEncoder() {
return false;
}
}
30 changes: 30 additions & 0 deletions sdk/android/src/jni/openh264_codec.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* 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.
*/

#include <jni.h>

#include "modules/video_coding/codecs/h264/include/h264.h"
#include "sdk/android/generated_open_h264_jni/OpenH264Decoder_jni.h"
#include "sdk/android/generated_open_h264_jni/OpenH264Encoder_jni.h"
#include "sdk/android/src/jni/jni_helpers.h"

namespace webrtc {
namespace jni {

static jlong JNI_OpenH264Encoder_CreateEncoder(JNIEnv* jni) {
return jlongFromPointer(H264Encoder::Create(cricket::VideoCodec("H264")).release());
}

static jlong JNI_OpenH264Decoder_CreateDecoder(JNIEnv* jni) {
return jlongFromPointer(H264Decoder::Create().release());
}

} // namespace jni
} // namespace webrtc
18 changes: 12 additions & 6 deletions sdk/android/src/jni/pc/frame_cryptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ FrameCryptorObserverJni::FrameCryptorObserverJni(

FrameCryptorObserverJni::~FrameCryptorObserverJni() {}

void FrameCryptorObserverJni::OnFrameCryptionError(
void FrameCryptorObserverJni::OnFrameCryptionStateChanged(
const std::string participant_id,
FrameCryptionError new_state) {
FrameCryptionState new_state) {
JNIEnv* env = AttachCurrentThreadIfNeeded();
Java_Observer_onFrameCryptorErrorState(
Java_Observer_onFrameCryptionStateChanged(
env, j_observer_global_, NativeToJavaString(env, participant_id),
Java_FrameCryptorErrorState_fromNativeIndex(env, new_state));
Java_FrameCryptionState_fromNativeIndex(env, new_state));
}

ScopedJavaLocalRef<jobject> NativeToJavaFrameCryptor(
Expand Down Expand Up @@ -163,9 +163,15 @@ JNI_FrameCryptorFactory_CreateFrameCryptorForRtpSender(
}

static base::android::ScopedJavaLocalRef<jobject>
JNI_FrameCryptorFactory_CreateFrameCryptorKeyManager(JNIEnv* env) {
JNI_FrameCryptorFactory_CreateFrameCryptorKeyManager(JNIEnv* env, jboolean j_shared,
const base::android::JavaParamRef<jbyteArray>& j_ratchetSalt, jint j_ratchetWindowSize) {
auto ratchetSalt = JavaToNativeByteArray(env, j_ratchetSalt);
KeyProviderOptions options;
options.ratchet_salt = std::vector<uint8_t>(ratchetSalt.begin(), ratchetSalt.end());
options.ratchet_window_size = j_ratchetWindowSize;
options.shared_key = j_shared;
return NativeToJavaFrameCryptorKeyManager(
env, rtc::make_ref_counted<webrtc::DefaultKeyManagerImpl>());
env, rtc::make_ref_counted<webrtc::DefaultKeyManagerImpl>(options));
}

} // namespace jni
Expand Down
4 changes: 2 additions & 2 deletions sdk/android/src/jni/pc/frame_cryptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ class FrameCryptorObserverJni : public FrameCryptorTransformerObserver, public r
~FrameCryptorObserverJni() override;

protected:
void OnFrameCryptionError(const std::string participant_id,
FrameCryptionError error) override;
void OnFrameCryptionStateChanged(const std::string participant_id,
FrameCryptionState state) override;

private:
const ScopedJavaGlobalRef<jobject> j_observer_global_;
Expand Down
37 changes: 6 additions & 31 deletions sdk/android/src/jni/pc/frame_cryptor_key_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,18 @@ static jboolean JNI_FrameCryptorKeyManager_SetKey(
std::vector<uint8_t>(key.begin(), key.end()));
}

static jboolean JNI_FrameCryptorKeyManager_SetKeys(
static base::android::ScopedJavaLocalRef<jbyteArray> JNI_FrameCryptorKeyManager_RatchetKey(
JNIEnv* env,
jlong keyManagerPointer,
const base::android::JavaParamRef<jstring>& participantId,
const base::android::JavaParamRef<jobject>& keys) {
jint j_index) {
auto participant_id = JavaToStdString(env, participantId);
auto key_manager =
reinterpret_cast<webrtc::DefaultKeyManagerImpl*>(keyManagerPointer);
auto keys_size = env->GetArrayLength((jobjectArray)keys.obj());
std::vector<std::vector<uint8_t>> keys_vector;
for (int i = 0; i < keys_size; i++) {
auto key = JavaToNativeByteArray(
env, base::android::JavaParamRef<jbyteArray>(
env, (jbyteArray)env->GetObjectArrayElement(
(jobjectArray)keys.obj(), i)));
keys_vector.push_back(std::vector<uint8_t>(key.begin(), key.end()));
}
return key_manager->SetKeys(participant_id, keys_vector);
auto newKey = key_manager->RatchetKey(participant_id, j_index);
std::vector<int8_t> int8tKey =
std::vector<int8_t>(newKey.begin(), newKey.end());
return NativeToJavaByteArray(env, rtc::ArrayView<int8_t>(int8tKey));
}

static ScopedJavaLocalRef<jobject> JNI_FrameCryptorKeyManager_GetKeys(
JNIEnv* jni,
jlong j_key_manager,
const base::android::JavaParamRef<jstring>& participantId) {
auto participant_id = JavaToStdString(jni, participantId);
auto keys = reinterpret_cast<webrtc::DefaultKeyManagerImpl*>(j_key_manager)
->keys(participant_id);
JavaListBuilder j_keys(jni);
for (size_t i = 0; i < keys.size(); i++) {
auto uint8Key = keys[i];
std::vector<int8_t> int8tKey =
std::vector<int8_t>(uint8Key.begin(), uint8Key.end());
auto j_key = NativeToJavaByteArray(jni, rtc::ArrayView<int8_t>(int8tKey));
j_keys.add(j_key);
}
return j_keys.java_list();
}

} // namespace jni
} // namespace webrtc

0 comments on commit b36765f

Please sign in to comment.