@@ -41,6 +41,7 @@ class PeerConnectionObserver implements PeerConnection.Observer, EventChannel.St
4141 private PeerConnection .RTCConfiguration configuration ;
4242 final Map <String , MediaStream > remoteStreams = new HashMap <>();
4343 final Map <String , MediaStreamTrack > remoteTracks = new HashMap <>();
44+ final Map <String , RtpTransceiver > transceivers = new HashMap <>();
4445 private final StateProvider stateProvider ;
4546 private final EventChannel eventChannel ;
4647 private EventChannel .EventSink eventSink ;
@@ -151,13 +152,16 @@ void dataChannelSend(int dataChannelId, ByteBuffer byteBuffer, Boolean isBinary)
151152 }
152153
153154 RtpTransceiver getRtpTransceiverById (String id ) {
154- List <RtpTransceiver > transceivers = peerConnection .getTransceivers ();
155- for (RtpTransceiver transceiver : transceivers ) {
156- if (id .equals (transceiver .getMid ())){
157- return transceiver ;
158- }
159- }
160- return null ;
155+ RtpTransceiver transceiver = transceivers .get (id );
156+ if (null == transceiver ) {
157+ List <RtpTransceiver > transceivers = peerConnection .getTransceivers ();
158+ for (RtpTransceiver t : transceivers ) {
159+ if (id .equals (t .getMid ())){
160+ transceiver = t ;
161+ }
162+ }
163+ }
164+ return transceiver ;
161165 }
162166
163167 RtpSender getRtpSenderById (String id ) {
@@ -432,7 +436,11 @@ public void onAddTrack(RtpReceiver receiver, MediaStream[] mediaStreams) {
432436 List <RtpTransceiver > transceivers = peerConnection .getTransceivers ();
433437 for ( RtpTransceiver transceiver : transceivers ) {
434438 if (transceiver .getReceiver () != null && receiver .id ().equals (transceiver .getReceiver ().id ())) {
435- params .putMap ("transceiver" , transceiverToMap (transceiver ));
439+ String transceiverId = transceiver .getMid ();
440+ if (null == transceiverId ) {
441+ transceiverId = stateProvider .getNextStreamUUID ();
442+ }
443+ params .putMap ("transceiver" , transceiverToMap (transceiverId , transceiver ));
436444 }
437445 }
438446 }
@@ -846,10 +854,14 @@ private Map<String, Object> rtpReceiverToMap(RtpReceiver receiver){
846854 return info .toMap ();
847855 }
848856
849- Map <String , Object > transceiverToMap (RtpTransceiver transceiver ){
857+ Map <String , Object > transceiverToMap (String transceiverId , RtpTransceiver transceiver ){
850858 ConstraintsMap info = new ConstraintsMap ();
851- info .putString ("transceiverId" , transceiver .getMid ());
852- info .putString ("mid" , transceiver .getMid ());
859+ info .putString ("transceiverId" , transceiverId );
860+ if (transceiver .getMid () == null ) {
861+ info .putString ("mid" , "" );
862+ } else {
863+ info .putString ("mid" , transceiver .getMid ());
864+ }
853865 info .putString ("direction" , transceiverDirectionString (transceiver .getDirection ()));
854866 info .putMap ("sender" , rtpSenderToMap (transceiver .getSender ()));
855867 info .putMap ("receiver" , rtpReceiverToMap (transceiver .getReceiver ()));
@@ -888,7 +900,12 @@ public void addTransceiver(MediaStreamTrack track, Map<String, Object> transceiv
888900 } else {
889901 transceiver = peerConnection .addTransceiver (track );
890902 }
891- result .success (transceiverToMap (transceiver ));
903+ String transceiverId = transceiver .getMid ();
904+ if (null == transceiverId ) {
905+ transceiverId = stateProvider .getNextStreamUUID ();
906+ }
907+ transceivers .put (transceiverId , transceiver );
908+ result .success (transceiverToMap (transceiverId , transceiver ));
892909 }
893910
894911 public void addTransceiverOfType (String mediaType , Map <String , Object > transceiverInit , Result result ) {
@@ -898,7 +915,12 @@ public void addTransceiverOfType(String mediaType, Map<String, Object> transceiv
898915 } else {
899916 transceiver = peerConnection .addTransceiver (stringToMediaType (mediaType ));
900917 }
901- result .success (transceiverToMap (transceiver ));
918+ String transceiverId = transceiver .getMid ();
919+ if (null == transceiverId ) {
920+ transceiverId = stateProvider .getNextStreamUUID ();
921+ }
922+ transceivers .put (transceiverId , transceiver );
923+ result .success (transceiverToMap (transceiverId , transceiver ));
902924 }
903925
904926 public void rtpTransceiverSetDirection (String direction , String transceiverId , Result result ) {
@@ -990,8 +1012,12 @@ public void getReceivers(Result result) {
9901012 public void getTransceivers (Result result ) {
9911013 List <RtpTransceiver > transceivers = peerConnection .getTransceivers ();
9921014 ConstraintsArray transceiversParams = new ConstraintsArray ();
993- for (RtpTransceiver receiver : transceivers ){
994- transceiversParams .pushMap (new ConstraintsMap (transceiverToMap (receiver )));
1015+ for (RtpTransceiver transceiver : transceivers ){
1016+ String transceiverId = transceiver .getMid ();
1017+ if (null == transceiverId ) {
1018+ transceiverId = stateProvider .getNextStreamUUID ();
1019+ }
1020+ transceiversParams .pushMap (new ConstraintsMap (transceiverToMap (transceiverId , transceiver )));
9951021 }
9961022 ConstraintsMap params = new ConstraintsMap ();
9971023 params .putArray ("transceivers" , transceiversParams .toArrayList ());
0 commit comments