Clean some code

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-12-22 15:23:14 +01:00
parent de02ad278c
commit 7571634872

View File

@ -63,6 +63,8 @@ public class MagicPeerConnectionWrapper {
private boolean hasInitiated; private boolean hasInitiated;
private MediaStream localMediaStream;
public MagicPeerConnectionWrapper(PeerConnectionFactory peerConnectionFactory, public MagicPeerConnectionWrapper(PeerConnectionFactory peerConnectionFactory,
List<PeerConnection.IceServer> iceServerList, List<PeerConnection.IceServer> iceServerList,
MediaConstraints mediaConstraints, MediaConstraints mediaConstraints,
@ -70,6 +72,7 @@ public class MagicPeerConnectionWrapper {
this.iceServers = iceServerList; this.iceServers = iceServerList;
this.localSession = localSession; this.localSession = localSession;
this.localMediaStream = mediaStream;
peerConnection = peerConnectionFactory.createPeerConnection(iceServerList, mediaConstraints, peerConnection = peerConnectionFactory.createPeerConnection(iceServerList, mediaConstraints,
new MagicPeerConnectionObserver()); new MagicPeerConnectionObserver());
@ -113,11 +116,13 @@ public class MagicPeerConnectionWrapper {
public void sendChannelData(DataChannelMessage dataChannelMessage) { public void sendChannelData(DataChannelMessage dataChannelMessage) {
ByteBuffer buffer = null; ByteBuffer buffer = null;
try { if (magicDataChannel != null) {
buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes()); try {
magicDataChannel.send(new DataChannel.Buffer(buffer, false)); buffer = ByteBuffer.wrap(LoganSquare.serialize(dataChannelMessage).getBytes());
} catch (IOException e) { magicDataChannel.send(new DataChannel.Buffer(buffer, false));
Log.d(TAG, "Failed to send channel data"); } catch (IOException e) {
Log.d(TAG, "Failed to send channel data");
}
} }
} }
@ -145,6 +150,20 @@ public class MagicPeerConnectionWrapper {
this.nick = nick; this.nick = nick;
} }
private void sendInitialMediaStatus() {
if (localMediaStream.videoTracks.size() == 1 && localMediaStream.videoTracks.get(0).enabled()) {
sendChannelData(new DataChannelMessage("videoOn"));
} else {
sendChannelData(new DataChannelMessage("videoOff"));
}
if (localMediaStream.audioTracks.size() == 1 && localMediaStream.audioTracks.get(0).enabled()) {
sendChannelData(new DataChannelMessage("audioOn"));
} else {
sendChannelData(new DataChannelMessage("audioOff"));
}
}
private class MagicDataChannelObserver implements DataChannel.Observer { private class MagicDataChannelObserver implements DataChannel.Observer {
@Override @Override
@ -156,8 +175,7 @@ public class MagicPeerConnectionWrapper {
public void onStateChange() { public void onStateChange() {
if (magicDataChannel.state().equals(DataChannel.State.OPEN) && if (magicDataChannel.state().equals(DataChannel.State.OPEN) &&
magicDataChannel.label().equals("status")) { magicDataChannel.label().equals("status")) {
sendChannelData(new DataChannelMessage("videoOn")); sendInitialMediaStatus();
sendChannelData(new DataChannelMessage("audioOn"));
} }
} }
@ -218,8 +236,7 @@ public class MagicPeerConnectionWrapper {
@Override @Override
public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) { public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
if (iceConnectionState.equals(PeerConnection.IceConnectionState.CONNECTED) && hasInitiated) { if (iceConnectionState.equals(PeerConnection.IceConnectionState.CONNECTED) && hasInitiated) {
sendChannelData(new DataChannelMessage("videoOn")); sendInitialMediaStatus();
sendChannelData(new DataChannelMessage("audioOn"));
} else if (iceConnectionState.equals(PeerConnection.IceConnectionState.FAILED)) { } else if (iceConnectionState.equals(PeerConnection.IceConnectionState.FAILED)) {
EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType EventBus.getDefault().post(new PeerConnectionEvent(PeerConnectionEvent.PeerConnectionEventType
.CLOSE_PEER, sessionId, null, null)); .CLOSE_PEER, sessionId, null, null));
@ -321,5 +338,4 @@ public class MagicPeerConnectionWrapper {
} }
} }
} }
} }