Skip to content

Commit

Permalink
Rework checking for null enums in JNI
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Apr 5, 2023
1 parent 8bce14e commit c8d6caa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
16 changes: 9 additions & 7 deletions roc_jni/src/main/cpp/receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ int receiver_config_unmarshal(JNIEnv* env, roc_receiver_config* config, jobject
receiverConfigClass = env->FindClass(RECEIVER_CONFIG_CLASS);
assert(receiverConfigClass != NULL);

// set all fields to zeros
memset(config, 0, sizeof(*config));

// frame_sample_rate
config->frame_sample_rate
= get_uint_field_value(env, receiverConfigClass, jconfig, "frameSampleRate", &err);
Expand All @@ -29,29 +32,28 @@ int receiver_config_unmarshal(JNIEnv* env, roc_receiver_config* config, jobject
// frame_channels
jobj = get_object_field(
env, receiverConfigClass, jconfig, "frameChannels", "L" CHANNEL_SET_CLASS ";");
if (jobj == NULL) return -1;
config->frame_channels = (roc_channel_set) get_channel_set(env, jobj);
if (jobj != NULL) config->frame_channels = (roc_channel_set) get_channel_set(env, jobj);

// frame_encoding
jobj = get_object_field(
env, receiverConfigClass, jconfig, "frameEncoding", "L" FRAME_ENCODING_CLASS ";");
if (jobj == NULL) return -1;
config->frame_encoding = (roc_frame_encoding) get_frame_encoding(env, jobj);
if (jobj != NULL) config->frame_encoding = (roc_frame_encoding) get_frame_encoding(env, jobj);

// clock_source
jobj = get_object_field(
env, receiverConfigClass, jconfig, "clockSource", "L" CLOCK_SOURCE_CLASS ";");
config->clock_source = get_clock_source(env, jobj);
if (jobj != NULL) config->clock_source = get_clock_source(env, jobj);

// resampler_backend
jobj = get_object_field(
env, receiverConfigClass, jconfig, "resamplerBackend", "L" RESAMPLER_BACKEND_CLASS ";");
config->resampler_backend = get_resampler_backend(env, jobj);
if (jobj != NULL) config->resampler_backend = get_resampler_backend(env, jobj);

// resampler_profile
jobj = get_object_field(
env, receiverConfigClass, jconfig, "resamplerProfile", "L" RESAMPLER_PROFILE_CLASS ";");
config->resampler_profile = (roc_resampler_profile) get_resampler_profile(env, jobj);
if (jobj != NULL)
config->resampler_profile = (roc_resampler_profile) get_resampler_profile(env, jobj);

// target_latency
config->target_latency
Expand Down
23 changes: 13 additions & 10 deletions roc_jni/src/main/cpp/sender.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ int sender_config_unmarshal(JNIEnv* env, roc_sender_config* config, jobject jcon
senderConfigClass = env->FindClass(SENDER_CONFIG_CLASS);
assert(senderConfigClass != NULL);

// set all fields to zeros
memset(config, 0, sizeof(*config));

// frame_sample_rate
config->frame_sample_rate
= get_uint_field_value(env, senderConfigClass, jconfig, "frameSampleRate", &err);
Expand All @@ -32,14 +35,12 @@ int sender_config_unmarshal(JNIEnv* env, roc_sender_config* config, jobject jcon
// frame_channels
jobj = get_object_field(
env, senderConfigClass, jconfig, "frameChannels", "L" CHANNEL_SET_CLASS ";");
if (jobj == NULL) return -1;
config->frame_channels = (roc_channel_set) get_channel_set(env, jobj);
if (jobj != NULL) config->frame_channels = (roc_channel_set) get_channel_set(env, jobj);

// frame_encoding
jobj = get_object_field(
env, senderConfigClass, jconfig, "frameEncoding", "L" FRAME_ENCODING_CLASS ";");
if (jobj == NULL) return -1;
config->frame_encoding = (roc_frame_encoding) get_frame_encoding(env, jobj);
if (jobj != NULL) config->frame_encoding = (roc_frame_encoding) get_frame_encoding(env, jobj);

// packet_sample_rate
config->packet_sample_rate
Expand All @@ -49,12 +50,13 @@ int sender_config_unmarshal(JNIEnv* env, roc_sender_config* config, jobject jcon
// packet_channels
jobj = get_object_field(
env, senderConfigClass, jconfig, "packetChannels", "L" CHANNEL_SET_CLASS ";");
config->packet_channels = (roc_channel_set) get_channel_set(env, jobj);
if (jobj != NULL) config->packet_channels = (roc_channel_set) get_channel_set(env, jobj);

// packet_encoding
jobj = get_object_field(
env, senderConfigClass, jconfig, "packetEncoding", "L" PACKET_ENCODING_CLASS ";");
config->packet_encoding = (roc_packet_encoding) get_packet_encoding(env, jobj);
if (jobj != NULL)
config->packet_encoding = (roc_packet_encoding) get_packet_encoding(env, jobj);

// packet_length
config->packet_length
Expand All @@ -69,22 +71,23 @@ int sender_config_unmarshal(JNIEnv* env, roc_sender_config* config, jobject jcon
// clock_source
jobj = get_object_field(
env, senderConfigClass, jconfig, "clockSource", "L" CLOCK_SOURCE_CLASS ";");
config->clock_source = get_clock_source(env, jobj);
if (jobj != NULL) config->clock_source = get_clock_source(env, jobj);

// resampler_backend
jobj = get_object_field(
env, senderConfigClass, jconfig, "resamplerBackend", "L" RESAMPLER_BACKEND_CLASS ";");
config->resampler_backend = get_resampler_backend(env, jobj);
if (jobj != NULL) config->resampler_backend = get_resampler_backend(env, jobj);

// resampler_profile
jobj = get_object_field(
env, senderConfigClass, jconfig, "resamplerProfile", "L" RESAMPLER_PROFILE_CLASS ";");
config->resampler_profile = (roc_resampler_profile) get_resampler_profile(env, jobj);
if (jobj != NULL)
config->resampler_profile = (roc_resampler_profile) get_resampler_profile(env, jobj);

// fec_encoding
jobj = get_object_field(
env, senderConfigClass, jconfig, "fecEncoding", "L" FEC_ENCODING_CLASS ";");
config->fec_encoding = (roc_fec_encoding) get_fec_encoding(env, jobj);
if (jobj != NULL) config->fec_encoding = (roc_fec_encoding) get_fec_encoding(env, jobj);

// fec_block_source_packets
config->fec_block_source_packets
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/rocstreaming/roctoolkit/ReceiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ public void TestInvalidReceiverCreation() {
ReceiverConfig config = new ReceiverConfig.Builder(-1, ChannelSet.STEREO, FrameEncoding.PCM_FLOAT).build();
new Receiver(context, config);
});
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(Exception.class, () -> {
ReceiverConfig config = new ReceiverConfig.Builder(SAMPLE_RATE, null, FrameEncoding.PCM_FLOAT).build();
new Receiver(context, config);
});
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(Exception.class, () -> {
ReceiverConfig config = new ReceiverConfig.Builder(SAMPLE_RATE, ChannelSet.STEREO, null).build();
new Receiver(context, config);
});
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/rocstreaming/roctoolkit/SenderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public void TestInvalidSenderCreation() {
new Sender(context, config);
});

assertThrows(IllegalArgumentException.class, () -> {
assertThrows(Exception.class, () -> {
SenderConfig config = new SenderConfig.Builder(SAMPLE_RATE, null, FrameEncoding.PCM_FLOAT).build();
new Sender(context, config);
});
assertThrows(IllegalArgumentException.class, () -> {
assertThrows(Exception.class, () -> {
SenderConfig config = new SenderConfig.Builder(SAMPLE_RATE, ChannelSet.STEREO, null).build();
new Sender(context, config);
});
Expand Down

0 comments on commit c8d6caa

Please sign in to comment.