8000 Removed base64 type on android. · linuxerwang/flutter-webrtc@66bbef7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 66bbef7

Browse fi 8000 les
committed
Removed base64 type on android.
1 parent cdac00f commit 66bbef7

File tree

6 files changed

+31
-11
lines changed

6 files changed

+31
-11
lines changed

android/src/main/java/com/cloudwebrtc/webrtc/DataChannelObserver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void onMessage(DataChannel.Buffer buffer) {
7979

8080
if (buffer.binary) {
8181
params.putString("type", "binary");
82-
params.putString("data", Base64.encodeToString(bytes, Base64.NO_WRAP));
82+
params.putByte("data", bytes);
8383
} else {
8484
params.putString("type", "text");
8585
params.putString("data", new String(bytes, Charset.forName("UTF-8")));

android/src/main/java/com/cloudwebrtc/webrtc/FlutterWebRTCPlugin.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.cloudwebrtc.webrtc.utils.EglUtils;
1515
import com.cloudwebrtc.webrtc.utils.ObjectType;
1616

17+
import java.io.UnsupportedEncodingException;
1718
import java.io.File;
1819
import java.nio.ByteBuffer;
1920
import java.util.*;
@@ -209,8 +210,21 @@ public void onMethodCall(MethodCall call, Result result) {
209210
String peerConnectionId = call.argument("peerConnectionId");
210211
int dataChannelId = call.argument("dataChannelId");
211212
String type = call.argument("type");
212-
ByteBuffer byteBuffer = ByteBuffer.wrap(call.argument("data"));
213-
dataChannelSend(peerConnectionId, dataChannelId, byteBuffer, type.equals("binary"));
213+
Boolean isBinary = type.equals("binary");
214+
ByteBuffer byteBuffer;
215+
if(isBinary){
216+
byteBuffer = ByteBuffer.wrap(call.argument("data"));
217+
}else{
218+
try {
219+
String data = call.argument("data");
220+
byteBuffer = ByteBuffer.wrap(data.getBytes("UTF-8"));
221+
} catch (UnsupportedEncodingException e) {
222+
Log.d(TAG, "Could not encode text string as UTF-8.");
223+
result.error("dataChannelSendFailed", "Could not encode text string as UTF-8.",null);
224+
return;
225+
}
226+
}
227+
dataChannelSend(peerConnectionId, dataChannelId, byteBuffer, isBinary);
214228
result.success(null);
215229
} else if (call.method.equals("dataChannelClose")) {
216230
String peerConnectionId = call.argument("peerConnectionId");

android/src/main/java/com/cloudwebrtc/webrtc/utils/ConstraintsArray.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public String getString(int index){
3939
return (String) mArray.get(index);
4040
}
4141

42+
public Byte[] getByte(int index){
43+
return (Byte[]) mArray.get(index);
44+
}
45+
4246
public ConstraintsArray getArray(int index){
4347
return new ConstraintsArray((ArrayList<Object>)mArray.get(index));
4448
}
@@ -64,6 +68,8 @@ public ObjectType getType(int index) {
6468
return ObjectType.Array;
6569
} else if (object instanceof Map) {
6670
return ObjectType.Map;
71+
} else if (object instanceof Byte) {
72+
return ObjectType.Byte;
6773
}
6874
return ObjectType.Null;
6975
}

android/src/main/java/com/cloudwebrtc/webrtc/utils/ConstraintsMap.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public ObjectType getType(String name) {
6565
return ObjectType.Map;
6666
} else if (value instanceof ArrayList) {
6767
return ObjectType.Array;
68+
} else if (value instanceof Byte) {
69+
return ObjectType.Byte;
6870
} else {
6971
throw new IllegalArgumentException("Invalid value " + value.toString() + " for key " + name +
7072
"contained in ConstraintsMap");
@@ -87,6 +89,10 @@ public void putString(String key, String value) {
8789
mMap.put(key, value);
8890
}
8991

92+
public void putByte(String key, byte[] value) {
93+
mMap.put(key, value);
94+
}
95+
9096
public void putNull(String key) {
9197
mMap.put(key, null);
9298
}

android/src/main/java/com/cloudwebrtc/webrtc/utils/ObjectType.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ public enum ObjectType {
77
String,
88
Map,
99
Array,
10+
Byte
1011
}

lib/rtc_data_channel.dart

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -178,19 +178,12 @@ class RTCDataChannel {
178178
/// To send a binary message, pass a binary [RTCDataChannelMessage]
179179
/// constructed with [RTCDataChannelMessage.fromBinary]
180180
Future<void> send(RTCDataChannelMessage message) async {
181-
dynamic data;
182-
if (message.isBinary) {
183-
data = message.binary;
184-
}
185-
else {
186-
data = message.text;
187-
}
188181
await _channel.invokeMethod('dataChannelSend',
189182
<String, dynamic>{
190183
'peerConnectionId': _peerConnectionId,
1911 3270 84
'dataChannelId': _dataChannelId,
192185
'type': message.isBinary ? "binary" : "text",
193-
'data': data
186+
'data': message.isBinary? message.binary : message.text,
194187
}
195188
);
196189
}

0 commit comments

Comments
 (0)
0