Skip to content

Fix callbacks not getting fired #121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import android.widget.GridLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
Expand Down Expand Up @@ -64,6 +63,7 @@
import org.webrtc.audio.JavaAudioDeviceModule;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -86,6 +86,7 @@
import io.antmedia.webrtcandroidframework.apprtc.AppRTCAudioManager;
import io.antmedia.webrtcandroidframework.websocket.AntMediaSignallingEvents;
import io.antmedia.webrtcandroidframework.websocket.Broadcast;
import io.antmedia.webrtcandroidframework.websocket.WebSocketConstants;
import io.antmedia.webrtcandroidframework.websocket.WebSocketHandler;

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


@Override
public void onRoomInformation(String[] streams) {
config.webRTCListener.onRoomInformation(streams);
Expand Down Expand Up @@ -1747,7 +1749,7 @@ public void forceStreamQuality(String mainTrackStreamId, String subTrackStreamId
wsHandler.forceStreamQuality(mainTrackStreamId, subTrackStreamId, height);
}

class DataChannelInternalObserver implements DataChannel.Observer {
public class DataChannelInternalObserver implements DataChannel.Observer {

private final DataChannel dataChannel;

Expand Down Expand Up @@ -1787,6 +1789,11 @@ public void onStateChange() {
}
});
}
protected String toTextMessage(DataChannel.Buffer buffer) {
ByteBuffer data = buffer.data;
String messageText = new String(data.array(), StandardCharsets.UTF_8);
return messageText;
}

@Override
public void onMessage(final DataChannel.Buffer buffer) {
Expand All @@ -1799,6 +1806,7 @@ public void onMessage(final DataChannel.Buffer buffer) {
handler.post(() -> {
if (config.dataChannelObserver == null || dataChannel == null) return;
try{
handleNotification(toTextMessage(bufferCopy));
config.dataChannelObserver.onMessage(bufferCopy, dataChannel.label());
}catch (IllegalStateException e) {
Log.e(TAG, "Data channel related error:" + e.getMessage());
Expand All @@ -1807,6 +1815,37 @@ public void onMessage(final DataChannel.Buffer buffer) {
}
}

public void handleNotification(String message) {
if (message == null || !message.contains("eventType"))
return;

JSONObject jsonMessage;
String streamId;
String eventTyp;

try {
jsonMessage = new JSONObject(message);
streamId = jsonMessage.getString(WebSocketConstants.STREAM_ID);
eventTyp = jsonMessage.getString("eventType");
} catch (Exception e) {
return;
}

switch (eventTyp) {
case WebSocketConstants.CAM_TURNED_OFF:
config.webRTCListener.onCameraTurnOffFor(streamId);
break;
case WebSocketConstants.CAM_TURNED_ON:
config.webRTCListener.onCameraTurnOnFor(streamId);
break;
case WebSocketConstants.MIC_MUTED:
config.webRTCListener.onMutedFor(streamId);
break;
case WebSocketConstants.MIC_UNMUTED:
config.webRTCListener.onUnmutedFor(streamId);
}
}

public void sendMessageViaDataChannel(String streamId, DataChannel.Buffer buffer) {
if (isDataChannelEnabled()) {
executor.execute(() -> {
Expand Down Expand Up @@ -2097,6 +2136,9 @@ public PCObserver getPCObserver(String streamId) {
return new PCObserver(streamId);
}

public DataChannelInternalObserver getInternalDataChannelObserver(DataChannel dataChannel){
return new DataChannelInternalObserver(dataChannel);
}
public void initDataChannel(String streamId) {
if (config.dataChannelEnabled) {
DataChannel.Init init = new DataChannel.Init();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -451,4 +451,11 @@ private WebSocketConstants() {

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

public static final String CAM_TURNED_OFF = "CAM_TURNED_OFF";

public static final String CAM_TURNED_ON = "CAM_TURNED_ON";

public static final String MIC_MUTED = "MIC_MUTED";

public static final String MIC_UNMUTED = "MIC_UNMUTED";
}
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ else if (commandText.equals(WebSocketConstants.STREAM_INFORMATION_NOTIFICATION))
signallingListener.onStreamInfoList(streamId, streamInfos);
}
else if (commandText.equals(NOTIFICATION_COMMAND)) {

String definition = json.getString(DEFINITION);

Log.d(TAG, "notification: " + definition);
Expand Down Expand Up @@ -280,7 +279,6 @@ else if (definition.equals(WebSocketConstants.BROADCAST_OBJECT_NOTIFICATION)) {
}else if(definition.equals(WebSocketConstants.RESOLUTION_CHANGE_INFO_COMMAND)){
int resolution = json.getInt(WebSocketConstants.STREAM_HEIGHT);
signallingListener.onResolutionChange(streamId, resolution);

}
}
else if (commandText.equals(WebSocketConstants.TRACK_LIST)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@

import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
Expand Down Expand Up @@ -1399,5 +1400,45 @@ public void testLeaveP2P() {

Mockito.verify(webRTCClient, times(1)).release(true);
}
@Test
public void handleNotification(){
IWebRTCListener listener1 = webRTCClient.getConfig().webRTCListener;
webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"CAM_TURNED_OFF\"}");
verify(listener1).onCameraTurnOffFor("test");

webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"CAM_TURNED_ON\"}");
verify(listener1).onCameraTurnOnFor("test");

webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"MIC_UNMUTED\"}");
verify(listener1).onUnmutedFor("test");

webRTCClient.handleNotification("{\"streamId\":\"test\",\"eventType\":\"MIC_MUTED\"}");
verify(listener1).onMutedFor("test");

}

@Test
public void testDataChannelOnMessage() throws InterruptedException {
IWebRTCListener listener1 = webRTCClient.getConfig().webRTCListener;
WebRTCClient.DataChannelInternalObserver dataChannelObserver = webRTCClient.getInternalDataChannelObserver(mock(DataChannel.class));
ByteBuffer buf = ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8));
DataChannel.Buffer buffer = new DataChannel.Buffer(buf, false);
dataChannelObserver.onMessage(buffer);
Thread.sleep(2000);
verify(webRTCClient).handleNotification("hello");

buf = ByteBuffer.wrap("{\"streamId\":\"test\",\"noeventtyp\":\"MIC_MUTED\"}".getBytes(StandardCharsets.UTF_8));
buffer = new DataChannel.Buffer(buf, false);
dataChannelObserver.onMessage(buffer);

Thread.sleep(2000);
verify(listener1,times(0)).onMutedFor("test");

buf = ByteBuffer.wrap("{\"streamId\":\"test\",\"eventType\":\"MIC_MUTED\"}".getBytes(StandardCharsets.UTF_8));
buffer = new DataChannel.Buffer(buf, false);
dataChannelObserver.onMessage(buffer);

Thread.sleep(2000);
verify(listener1).onMutedFor("test");
}
}
Loading