Skip to content

Commit 1e73f6c

Browse files
committed
fix callbacks not fired
1 parent cc80c08 commit 1e73f6c

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/core/WebRTCClient.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import android.util.DisplayMetrics;
1717
import android.util.Log;
1818
import android.view.WindowManager;
19-
import android.widget.GridLayout;
2019
import android.widget.Toast;
2120

2221
import androidx.annotation.NonNull;
@@ -64,6 +63,7 @@
6463
import org.webrtc.audio.JavaAudioDeviceModule;
6564

6665
import java.nio.ByteBuffer;
66+
import java.nio.charset.StandardCharsets;
6767
import java.util.ArrayList;
6868
import java.util.Arrays;
6969
import java.util.Collections;
@@ -86,6 +86,7 @@
8686
import io.antmedia.webrtcandroidframework.apprtc.AppRTCAudioManager;
8787
import io.antmedia.webrtcandroidframework.websocket.AntMediaSignallingEvents;
8888
import io.antmedia.webrtcandroidframework.websocket.Broadcast;
89+
import io.antmedia.webrtcandroidframework.websocket.WebSocketConstants;
8990
import io.antmedia.webrtcandroidframework.websocket.WebSocketHandler;
9091

9192
public class WebRTCClient implements IWebRTCClient, AntMediaSignallingEvents {
@@ -1576,6 +1577,7 @@ public void onJoinedTheRoom(String streamId, String[] streams) {
15761577
config.webRTCListener.onJoinedTheRoom(streamId, streams);
15771578
}
15781579

1580+
15791581
@Override
15801582
public void onRoomInformation(String[] streams) {
15811583
config.webRTCListener.onRoomInformation(streams);
@@ -1787,6 +1789,11 @@ public void onStateChange() {
17871789
}
17881790
});
17891791
}
1792+
protected String toTextMessage(DataChannel.Buffer buffer) {
1793+
ByteBuffer data = buffer.data;
1794+
String messageText = new String(data.array(), StandardCharsets.UTF_8);
1795+
return messageText;
1796+
}
17901797

17911798
@Override
17921799
public void onMessage(final DataChannel.Buffer buffer) {
@@ -1799,6 +1806,7 @@ public void onMessage(final DataChannel.Buffer buffer) {
17991806
handler.post(() -> {
18001807
if (config.dataChannelObserver == null || dataChannel == null) return;
18011808
try{
1809+
handleNotification(toTextMessage(bufferCopy));
18021810
config.dataChannelObserver.onMessage(bufferCopy, dataChannel.label());
18031811
}catch (IllegalStateException e) {
18041812
Log.e(TAG, "Data channel related error:" + e.getMessage());
@@ -1807,6 +1815,37 @@ public void onMessage(final DataChannel.Buffer buffer) {
18071815
}
18081816
}
18091817

1818+
public void handleNotification(String message) {
1819+
if (message == null || !message.contains("eventType"))
1820+
return;
1821+
1822+
JSONObject jsonMessage;
1823+
String streamId;
1824+
String eventTyp;
1825+
1826+
try {
1827+
jsonMessage = new JSONObject(message);
1828+
streamId = jsonMessage.getString(WebSocketConstants.STREAM_ID);
1829+
eventTyp = jsonMessage.getString("eventType");
1830+
} catch (Exception e) {
1831+
return;
1832+
}
1833+
1834+
switch (eventTyp) {
1835+
case WebSocketConstants.CAM_TURNED_OFF:
1836+
config.webRTCListener.onCameraTurnOffFor(streamId);
1837+
break;
1838+
case WebSocketConstants.CAM_TURNED_ON:
1839+
config.webRTCListener.onCameraTurnOnFor(streamId);
1840+
break;
1841+
case WebSocketConstants.MIC_MUTED:
1842+
config.webRTCListener.onMutedFor(streamId);
1843+
break;
1844+
case WebSocketConstants.MIC_UNMUTED:
1845+
config.webRTCListener.onUnmutedFor(streamId);
1846+
}
1847+
}
1848+
18101849
public void sendMessageViaDataChannel(String streamId, DataChannel.Buffer buffer) {
18111850
if (isDataChannelEnabled()) {
18121851
executor.execute(() -> {

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketConstants.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,11 @@ private WebSocketConstants() {
451451

452452
public static final int WEBSOCKET_CONNECTION_TIMEOUT = 10000; //10 sec
453453

454+
public static final String CAM_TURNED_OFF = "CAM_TURNED_OFF";
455+
456+
public static final String CAM_TURNED_ON = "CAM_TURNED_ON";
457+
458+
public static final String MIC_MUTED = "MIC_MUTED";
459+
460+
public static final String MIC_UNMUTED = "MIC_UNMUTED";
454461
}

webrtc-android-framework/src/main/java/io/antmedia/webrtcandroidframework/websocket/WebSocketHandler.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,6 @@ else if (commandText.equals(WebSocketConstants.STREAM_INFORMATION_NOTIFICATION))
212212
signallingListener.onStreamInfoList(streamId, streamInfos);
213213
}
214214
else if (commandText.equals(NOTIFICATION_COMMAND)) {
215-
216215
String definition = json.getString(DEFINITION);
217216

218217
Log.d(TAG, "notification: " + definition);
@@ -280,7 +279,6 @@ else if (definition.equals(WebSocketConstants.BROADCAST_OBJECT_NOTIFICATION)) {
280279
}else if(definition.equals(WebSocketConstants.RESOLUTION_CHANGE_INFO_COMMAND)){
281280
int resolution = json.getInt(WebSocketConstants.STREAM_HEIGHT);
282281
signallingListener.onResolutionChange(streamId, resolution);
283-
284282
}
285283
}
286284
else if (commandText.equals(WebSocketConstants.TRACK_LIST)) {

webrtc-android-framework/src/test/java/io/antmedia/webrtcandroidframework/WebRTCClientTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,5 +1399,21 @@ public void testLeaveP2P() {
13991399

14001400
Mockito.verify(webRTCClient, times(1)).release(true);
14011401
}
1402+
@Test
1403+
public void handleNotification(){
1404+
IWebRTCListener listener1 = webRTCClient.getConfig().webRTCListener;
1405+
webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"CAM_TURNED_OFF\"}");
1406+
verify(listener1).onCameraTurnOffFor("test");
1407+
1408+
webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"CAM_TURNED_ON\"}");
1409+
verify(listener1).onCameraTurnOnFor("test");
1410+
1411+
webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"MIC_UNMUTED\"}");
1412+
verify(listener1).onUnmutedFor("test");
1413+
1414+
webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"MIC_MUTED\"}");
1415+
verify(listener1).onMutedFor("test");
1416+
1417+
}
14021418

14031419
}

0 commit comments

Comments
 (0)