1212import org .json .JSONException ;
1313import org .json .JSONObject ;
1414
15- import org .webrtc .*;
15+ import org .webrtc .IceCandidate ;
16+ import org .webrtc .MediaConstraints ;
17+ import org .webrtc .MediaStream ;
18+ import org .webrtc .PeerConnection ;
19+ import org .webrtc .PeerConnectionFactory ;
20+ import org .webrtc .SdpObserver ;
21+ import org .webrtc .SessionDescription ;
22+ import org .webrtc .VideoCapturer ;
23+ import org .webrtc .VideoSource ;
1624
1725import java .net .URI ;
1826import java .util .LinkedList ;
1927import java .util .HashMap ;
20- import java .util .Map ;
2128
2229public class RTCActivity extends Activity {
23- private String host = "http://54.214.218.3:3000/" ;
30+ private static final String HOST = "http://54.214.218.3:3000/" ;
2431 private LinkedList <PeerConnection .IceServer > iceServers = new LinkedList <PeerConnection .IceServer >();
2532 private PeerConnectionFactory factory ;
26- private Map <String , Peer > peers = new HashMap <String , Peer >();
33+ private HashMap <String , Peer > peers = new HashMap <String , Peer >();
2734 private MediaConstraints pcConstraints ;
2835 private MediaStream lMS ;
2936 private EditText name ;
3037 private TextView link ;
31- private SocketIOClient client = new SocketIOClient (URI .create (host ), new SocketIOClient .Handler () {
38+ private final SocketIOClient client = new SocketIOClient (URI .create (HOST ), new SocketIOClient .Handler () {
3239 @ Override
3340 public void onConnect () {
3441 }
3542
3643 @ Override
37- public void on (String event , JSONArray arguments ) {
38-
39- try {
40- if (event .equals ("id" )) updateLink (arguments .getString (0 ));
41- JSONObject json = arguments .getJSONObject (0 );
42- messageHandler .handle (json );
43- } catch (JSONException e ) {
44- e .printStackTrace ();
45- }
44+ public void on (final String event , final JSONArray arguments ) {
45+
46+ runOnUiThread (new Runnable () {
47+ @ Override
48+ public void run () {
49+ try {
50+ if (event .equals ("id" )) link .setText (HOST + arguments .getString (0 ));
51+ JSONObject json = arguments .getJSONObject (0 );
52+ messageHandler .handle (json );
53+ } catch (JSONException e ) {
54+ e .printStackTrace ();
55+ }
56+ }
57+ });
4658 }
4759
4860 @ Override
@@ -65,15 +77,15 @@ public void onError(Exception error) {
6577 public void onConnectToEndpoint (String endpoint ) {
6678 }
6779 });
68- private MessageHandler messageHandler = new MessageHandler ();
80+ private final MessageHandler messageHandler = new MessageHandler ();
6981
70- // Command pattern
7182 public interface Command {
72- void execute (Peer peer , JSONObject payload ) throws JSONException ;
83+ void execute (String peerId , JSONObject payload ) throws JSONException ;
7384 }
7485
7586 public class SetRemoteSDPCommand implements Command {
76- public void execute (Peer peer , JSONObject payload ) throws JSONException {
87+ public void execute (String peerId , JSONObject payload ) throws JSONException {
88+ Peer peer = peers .get (peerId );
7789 SessionDescription sdp = new SessionDescription (
7890 SessionDescription .Type .fromCanonicalForm (payload .getString ("type" )),
7991 payload .getString ("sdp" )
@@ -83,26 +95,27 @@ public void execute(Peer peer, JSONObject payload) throws JSONException {
8395 }
8496
8597 public class StopCommand implements Command {
86- public void execute (Peer peer , JSONObject payload ) throws JSONException {
87- sendMessage (peer . id , "closed" , payload );
98+ public void execute (String peerId , JSONObject payload ) throws JSONException {
99+ sendMessage (peerId , "closed" , payload );
88100 }
89101 }
90102
91103 public class CloseCommand implements Command {
92- public void execute (Peer peer , JSONObject payload ) {
93- removePeer (peer );
104+ public void execute (String peerId , JSONObject payload ) {
105+ removePeer (peerId );
94106 }
95107 }
96108
97109 public class AddIceCandidateCommand implements Command {
98- public void execute (Peer peer , JSONObject payload ) throws JSONException {
99- if (peer .pc .getRemoteDescription () != null ) {
110+ public void execute (String peerId , JSONObject payload ) throws JSONException {
111+ PeerConnection pc = peers .get (peerId ).pc ;
112+ if (pc .getRemoteDescription () != null ) {
100113 IceCandidate candidate = new IceCandidate (
101114 payload .getString ("id" ),
102115 payload .getInt ("label" ),
103116 payload .getString ("candidate" )
104117 );
105- peer . pc .addIceCandidate (candidate );
118+ pc .addIceCandidate (candidate );
106119 }
107120 }
108121 }
@@ -116,7 +129,7 @@ private void sendMessage(String to, String type, JSONObject payload) throws JSON
116129 }
117130
118131 public class MessageHandler {
119- private Map <String , Command > commandMap ;
132+ private HashMap <String , Command > commandMap ;
120133
121134 public MessageHandler () {
122135 this .commandMap = new HashMap <String , Command >();
@@ -137,7 +150,7 @@ public void handle(JSONObject json) throws JSONException {
137150 addPeer (from );
138151 }
139152
140- commandMap .get (type ).execute (peers . get ( from ) , payload );
153+ commandMap .get (type ).execute (from , payload );
141154 }
142155 }
143156
@@ -181,7 +194,7 @@ public void onSetFailure(String s) {}
181194 @ Override
182195 public void onSignalingChange (PeerConnection .SignalingState signalingState ) {
183196 if (signalingState == PeerConnection .SignalingState .CLOSED ) {
184- removePeer (this );
197+ removePeer (id );
185198 }
186199 }
187200
@@ -247,16 +260,16 @@ public void onCreate(Bundle savedInstanceState) {
247260 videoConstraints .mandatory .add (new MediaConstraints .KeyValuePair ("maxHeight" , "240" ));
248261 videoConstraints .mandatory .add (new MediaConstraints .KeyValuePair ("maxWidth" , "320" ));
249262
250- VideoCapturer capturer = getVideoCapturer ();
251- VideoSource videoSource = factory .createVideoSource (capturer , videoConstraints );
263+ VideoSource videoSource = factory .createVideoSource (getVideoCapturer (), videoConstraints );
252264 lMS = factory .createLocalMediaStream ("ARDAMS" );
253- VideoTrack videoTrack = factory .createVideoTrack ("ARDAMSv0" , videoSource );
254- lMS .addTrack (videoTrack );
265+ lMS .addTrack (factory .createVideoTrack ("ARDAMSv0" , videoSource ));
255266 lMS .addTrack (factory .createAudioTrack ("ARDAMSa0" ));
256267 }
257268
258- // Cycle through likely device names for the camera and return the first
259- // capturer that works, or crash if none do.
269+ /*
270+ Cycle through likely device names for the camera and return the first
271+ capturer that works, or crash if none do.
272+ */
260273 private VideoCapturer getVideoCapturer () {
261274 String [] cameraFacing = { "back" , "front" };
262275 int [] cameraIndex = { 0 , 1 };
@@ -282,7 +295,8 @@ public void addPeer(String id) {
282295 peers .put (id , peer );
283296 }
284297
285- public void removePeer (Peer peer ) {
298+ public void removePeer (String id ) {
299+ Peer peer = peers .get (id );
286300 peer .pc .close ();
287301 peer .pc .dispose ();
288302 peers .remove (peer .id );
@@ -298,13 +312,4 @@ public void stream(View view) {
298312 e .printStackTrace ();
299313 }
300314 }
301-
302- private void updateLink (final String id ){
303- runOnUiThread (new Runnable () {
304- @ Override
305- public void run () {
306- link .setText (host + id );
307- }
308- });
309- }
310315}
0 commit comments