Skip to content

Commit 3a19c36

Browse files
PertsevRomanneilcsmith-net
authored andcommitted
Bugfix/issue 152 2 (#154)
* get enum from int * add webrtc bin test
1 parent 9c10b06 commit 3a19c36

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@
1313

1414
# NetBeans Files #
1515
/nbproject/
16-
nb-configuration.xml
16+
nb-configuration.xml
17+
18+
.idea

src/org/freedesktop/gstreamer/glib/NativeEnum.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
/*
1+
/*
22
* Copyright (c) 2019 Neil C Smith
3-
*
3+
*
44
* This file is part of gstreamer-java.
55
*
66
* This code is free software: you can redistribute it and/or modify it under
@@ -23,7 +23,18 @@
2323
* @param <T> Java enum type
2424
*/
2525
public interface NativeEnum<T extends Enum<T>> {
26-
26+
2727
public int intValue();
2828

29+
public static <T extends Enum<T> & NativeEnum<T>> T fromInt(Class<T> type, int intValue) {
30+
for (T value : type.getEnumConstants()) {
31+
if (value.intValue() == intValue) {
32+
return value;
33+
}
34+
}
35+
36+
throw new IllegalArgumentException("Value " + intValue + " is unacceptable for " +
37+
type.getSimpleName() + " enum");
38+
}
39+
2940
}

src/org/freedesktop/gstreamer/webrtc/WebRTCBin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.freedesktop.gstreamer.Promise;
2525
import org.freedesktop.gstreamer.Structure;
2626

27+
import org.freedesktop.gstreamer.glib.NativeEnum;
2728
import org.freedesktop.gstreamer.lowlevel.GstAPI.GstCallback;
2829

2930
/**
@@ -262,7 +263,7 @@ public String getTurnServer() {
262263
* state
263264
*/
264265
public WebRTCPeerConnectionState getConnectionState() {
265-
return (WebRTCPeerConnectionState) get("connection-state");
266+
return NativeEnum.fromInt(WebRTCPeerConnectionState.class, (Integer) get("connection-state"));
266267
}
267268

268269
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.freedesktop.gstreamer;
2+
3+
import org.freedesktop.gstreamer.glib.NativeEnum;
4+
import org.freedesktop.gstreamer.webrtc.WebRTCPeerConnectionState;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class WebRTCBinTest {
10+
@Test
11+
public void connectionStateTest() {
12+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 0), WebRTCPeerConnectionState.NEW);
13+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 1), WebRTCPeerConnectionState.CONNECTING);
14+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 2), WebRTCPeerConnectionState.CONNECTED);
15+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 3), WebRTCPeerConnectionState.DISCONNECTED);
16+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 4), WebRTCPeerConnectionState.FAILED);
17+
assertEquals(NativeEnum.fromInt(WebRTCPeerConnectionState.class, 5), WebRTCPeerConnectionState.CLOSED);
18+
}
19+
}

0 commit comments

Comments
 (0)