OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
12 | 12 |
13 import android.content.Context; | 13 import android.content.Context; |
14 import android.os.Environment; | 14 import android.os.Environment; |
15 import android.os.ParcelFileDescriptor; | 15 import android.os.ParcelFileDescriptor; |
16 import android.util.Log; | 16 import android.util.Log; |
17 import java.io.File; | 17 import java.io.File; |
18 import java.io.IOException; | 18 import java.io.IOException; |
19 import java.nio.ByteBuffer; | |
19 import java.util.Collections; | 20 import java.util.Collections; |
20 import java.util.EnumSet; | 21 import java.util.EnumSet; |
21 import java.util.LinkedList; | 22 import java.util.LinkedList; |
22 import java.util.List; | 23 import java.util.List; |
23 import java.util.Timer; | 24 import java.util.Timer; |
24 import java.util.TimerTask; | 25 import java.util.TimerTask; |
25 import java.util.concurrent.Executors; | 26 import java.util.concurrent.Executors; |
26 import java.util.concurrent.ScheduledExecutorService; | 27 import java.util.concurrent.ScheduledExecutorService; |
27 import java.util.regex.Matcher; | 28 import java.util.regex.Matcher; |
28 import java.util.regex.Pattern; | 29 import java.util.regex.Pattern; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 private MediaStream mediaStream; | 120 private MediaStream mediaStream; |
120 private VideoCapturer videoCapturer; | 121 private VideoCapturer videoCapturer; |
121 // enableVideo is set to true if video should be rendered and sent. | 122 // enableVideo is set to true if video should be rendered and sent. |
122 private boolean renderVideo; | 123 private boolean renderVideo; |
123 private VideoTrack localVideoTrack; | 124 private VideoTrack localVideoTrack; |
124 private VideoTrack remoteVideoTrack; | 125 private VideoTrack remoteVideoTrack; |
125 private RtpSender localVideoSender; | 126 private RtpSender localVideoSender; |
126 // enableAudio is set to true if audio should be sent. | 127 // enableAudio is set to true if audio should be sent. |
127 private boolean enableAudio; | 128 private boolean enableAudio; |
128 private AudioTrack localAudioTrack; | 129 private AudioTrack localAudioTrack; |
130 private DataChannel dataChannel; | |
131 private boolean dataChannelEnabled; | |
129 | 132 |
130 /** | 133 /** |
131 * Peer connection parameters. | 134 * Peer connection parameters. |
135 */ | |
136 public static class DataChannelParameters { | |
137 public final boolean ordered; | |
138 public final int maxRetransmitTimeMs; | |
139 public final int maxRetransmits; | |
140 public final String protocol; | |
141 public final boolean negotiated; | |
142 public final int id; | |
143 | |
144 public DataChannelParameters(boolean ordered, int maxRetransmitTimeMs, int m axRetransmits, | |
145 String protocol, boolean negotiated, int id) { | |
146 this.ordered = ordered; | |
147 this.maxRetransmitTimeMs = maxRetransmitTimeMs; | |
148 this.maxRetransmits = maxRetransmits; | |
149 this.protocol = protocol; | |
150 this.negotiated = negotiated; | |
151 this.id = id; | |
152 } | |
153 } | |
154 | |
155 /** | |
156 * Peer connection parameters. | |
132 */ | 157 */ |
133 public static class PeerConnectionParameters { | 158 public static class PeerConnectionParameters { |
134 public final boolean videoCallEnabled; | 159 public final boolean videoCallEnabled; |
135 public final boolean loopback; | 160 public final boolean loopback; |
136 public final boolean tracing; | 161 public final boolean tracing; |
137 public final int videoWidth; | 162 public final int videoWidth; |
138 public final int videoHeight; | 163 public final int videoHeight; |
139 public final int videoFps; | 164 public final int videoFps; |
140 public final int videoMaxBitrate; | 165 public final int videoMaxBitrate; |
141 public final String videoCodec; | 166 public final String videoCodec; |
142 public final boolean videoCodecHwAcceleration; | 167 public final boolean videoCodecHwAcceleration; |
143 public final int audioStartBitrate; | 168 public final int audioStartBitrate; |
144 public final String audioCodec; | 169 public final String audioCodec; |
145 public final boolean noAudioProcessing; | 170 public final boolean noAudioProcessing; |
146 public final boolean aecDump; | 171 public final boolean aecDump; |
147 public final boolean useOpenSLES; | 172 public final boolean useOpenSLES; |
148 public final boolean disableBuiltInAEC; | 173 public final boolean disableBuiltInAEC; |
149 public final boolean disableBuiltInAGC; | 174 public final boolean disableBuiltInAGC; |
150 public final boolean disableBuiltInNS; | 175 public final boolean disableBuiltInNS; |
151 public final boolean enableLevelControl; | 176 public final boolean enableLevelControl; |
177 private final DataChannelParameters dataChannelParameters; | |
152 | 178 |
153 public PeerConnectionParameters(boolean videoCallEnabled, boolean loopback, boolean tracing, | 179 public PeerConnectionParameters(boolean videoCallEnabled, boolean loopback, boolean tracing, |
154 int videoWidth, int videoHeight, int videoFps, int videoMaxBitrate, Stri ng videoCodec, | 180 int videoWidth, int videoHeight, int videoFps, int videoMaxBitrate, Stri ng videoCodec, |
155 boolean videoCodecHwAcceleration, int audioStartBitrate, String audioCod ec, | 181 boolean videoCodecHwAcceleration, int audioStartBitrate, String audioCod ec, |
156 boolean noAudioProcessing, boolean aecDump, boolean useOpenSLES, boolean disableBuiltInAEC, | 182 boolean noAudioProcessing, boolean aecDump, boolean useOpenSLES, boolean disableBuiltInAEC, |
157 boolean disableBuiltInAGC, boolean disableBuiltInNS, boolean enableLevel Control) { | 183 boolean disableBuiltInAGC, boolean disableBuiltInNS, boolean enableLevel Control) { |
184 this(videoCallEnabled, loopback, tracing, videoWidth, videoHeight, videoFp s, videoMaxBitrate, | |
185 videoCodec, videoCodecHwAcceleration, audioStartBitrate, audioCodec, n oAudioProcessing, | |
186 aecDump, useOpenSLES, disableBuiltInAEC, disableBuiltInAGC, disableBui ltInNS, | |
187 enableLevelControl, null); | |
188 } | |
189 | |
190 public PeerConnectionParameters(boolean videoCallEnabled, boolean loopback, boolean tracing, | |
191 int videoWidth, int videoHeight, int videoFps, int videoMaxBitrate, Stri ng videoCodec, | |
192 boolean videoCodecHwAcceleration, int audioStartBitrate, String audioCod ec, | |
193 boolean noAudioProcessing, boolean aecDump, boolean useOpenSLES, boolean disableBuiltInAEC, | |
194 boolean disableBuiltInAGC, boolean disableBuiltInNS, boolean enableLevel Control, | |
195 DataChannelParameters dataChannelParameters) { | |
158 this.videoCallEnabled = videoCallEnabled; | 196 this.videoCallEnabled = videoCallEnabled; |
159 this.loopback = loopback; | 197 this.loopback = loopback; |
160 this.tracing = tracing; | 198 this.tracing = tracing; |
161 this.videoWidth = videoWidth; | 199 this.videoWidth = videoWidth; |
162 this.videoHeight = videoHeight; | 200 this.videoHeight = videoHeight; |
163 this.videoFps = videoFps; | 201 this.videoFps = videoFps; |
164 this.videoMaxBitrate = videoMaxBitrate; | 202 this.videoMaxBitrate = videoMaxBitrate; |
165 this.videoCodec = videoCodec; | 203 this.videoCodec = videoCodec; |
166 this.videoCodecHwAcceleration = videoCodecHwAcceleration; | 204 this.videoCodecHwAcceleration = videoCodecHwAcceleration; |
167 this.audioStartBitrate = audioStartBitrate; | 205 this.audioStartBitrate = audioStartBitrate; |
168 this.audioCodec = audioCodec; | 206 this.audioCodec = audioCodec; |
169 this.noAudioProcessing = noAudioProcessing; | 207 this.noAudioProcessing = noAudioProcessing; |
170 this.aecDump = aecDump; | 208 this.aecDump = aecDump; |
171 this.useOpenSLES = useOpenSLES; | 209 this.useOpenSLES = useOpenSLES; |
172 this.disableBuiltInAEC = disableBuiltInAEC; | 210 this.disableBuiltInAEC = disableBuiltInAEC; |
173 this.disableBuiltInAGC = disableBuiltInAGC; | 211 this.disableBuiltInAGC = disableBuiltInAGC; |
174 this.disableBuiltInNS = disableBuiltInNS; | 212 this.disableBuiltInNS = disableBuiltInNS; |
175 this.enableLevelControl = enableLevelControl; | 213 this.enableLevelControl = enableLevelControl; |
214 this.dataChannelParameters = dataChannelParameters; | |
176 } | 215 } |
177 } | 216 } |
178 | 217 |
179 /** | 218 /** |
180 * Peer connection events. | 219 * Peer connection events. |
181 */ | 220 */ |
182 public interface PeerConnectionEvents { | 221 public interface PeerConnectionEvents { |
183 /** | 222 /** |
184 * Callback fired once local SDP is created and set. | 223 * Callback fired once local SDP is created and set. |
185 */ | 224 */ |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 | 275 |
237 public void setPeerConnectionFactoryOptions(PeerConnectionFactory.Options opti ons) { | 276 public void setPeerConnectionFactoryOptions(PeerConnectionFactory.Options opti ons) { |
238 this.options = options; | 277 this.options = options; |
239 } | 278 } |
240 | 279 |
241 public void createPeerConnectionFactory(final Context context, | 280 public void createPeerConnectionFactory(final Context context, |
242 final PeerConnectionParameters peerConnectionParameters, final PeerConnect ionEvents events) { | 281 final PeerConnectionParameters peerConnectionParameters, final PeerConnect ionEvents events) { |
243 this.peerConnectionParameters = peerConnectionParameters; | 282 this.peerConnectionParameters = peerConnectionParameters; |
244 this.events = events; | 283 this.events = events; |
245 videoCallEnabled = peerConnectionParameters.videoCallEnabled; | 284 videoCallEnabled = peerConnectionParameters.videoCallEnabled; |
285 dataChannelEnabled = peerConnectionParameters.dataChannelParameters != null; | |
246 // Reset variables to initial states. | 286 // Reset variables to initial states. |
247 this.context = null; | 287 this.context = null; |
248 factory = null; | 288 factory = null; |
249 peerConnection = null; | 289 peerConnection = null; |
250 preferIsac = false; | 290 preferIsac = false; |
251 videoCapturerStopped = false; | 291 videoCapturerStopped = false; |
252 isError = false; | 292 isError = false; |
253 queuedRemoteCandidates = null; | 293 queuedRemoteCandidates = null; |
254 localSdp = null; // either offer or answer SDP | 294 localSdp = null; // either offer or answer SDP |
255 mediaStream = null; | 295 mediaStream = null; |
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
477 // TCP candidates are only useful when connecting to a server that supports | 517 // TCP candidates are only useful when connecting to a server that supports |
478 // ICE-TCP. | 518 // ICE-TCP. |
479 rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; | 519 rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.DISABLED; |
480 rtcConfig.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE; | 520 rtcConfig.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE; |
481 rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE; | 521 rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE; |
482 rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy .GATHER_CONTINUALLY; | 522 rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy .GATHER_CONTINUALLY; |
483 // Use ECDSA encryption. | 523 // Use ECDSA encryption. |
484 rtcConfig.keyType = PeerConnection.KeyType.ECDSA; | 524 rtcConfig.keyType = PeerConnection.KeyType.ECDSA; |
485 | 525 |
486 peerConnection = factory.createPeerConnection(rtcConfig, pcConstraints, pcOb server); | 526 peerConnection = factory.createPeerConnection(rtcConfig, pcConstraints, pcOb server); |
527 | |
528 if (dataChannelEnabled) { | |
529 DataChannel.Init init = new DataChannel.Init(); | |
530 init.ordered = peerConnectionParameters.dataChannelParameters.ordered; | |
531 init.negotiated = peerConnectionParameters.dataChannelParameters.negotiate d; | |
532 init.maxRetransmits = peerConnectionParameters.dataChannelParameters.maxRe transmits; | |
533 init.maxRetransmitTimeMs = peerConnectionParameters.dataChannelParameters. maxRetransmitTimeMs; | |
534 init.id = peerConnectionParameters.dataChannelParameters.id; | |
535 init.protocol = peerConnectionParameters.dataChannelParameters.protocol; | |
536 dataChannel = peerConnection.createDataChannel("ApprtcDemo data", init); | |
537 } | |
487 isInitiator = false; | 538 isInitiator = false; |
488 | 539 |
489 // Set default WebRTC tracing and INFO libjingle logging. | 540 // Set default WebRTC tracing and INFO libjingle logging. |
490 // NOTE: this _must_ happen while |factory| is alive! | 541 // NOTE: this _must_ happen while |factory| is alive! |
491 Logging.enableTracing("logcat:", EnumSet.of(Logging.TraceLevel.TRACE_DEFAULT )); | 542 Logging.enableTracing("logcat:", EnumSet.of(Logging.TraceLevel.TRACE_DEFAULT )); |
492 Logging.enableLogToDebugOutput(Logging.Severity.LS_INFO); | 543 Logging.enableLogToDebugOutput(Logging.Severity.LS_INFO); |
493 | 544 |
494 mediaStream = factory.createLocalMediaStream("ARDAMS"); | 545 mediaStream = factory.createLocalMediaStream("ARDAMS"); |
495 if (videoCallEnabled) { | 546 if (videoCallEnabled) { |
496 mediaStream.addTrack(createVideoTrack(videoCapturer)); | 547 mediaStream.addTrack(createVideoTrack(videoCapturer)); |
(...skipping 20 matching lines...) Expand all Loading... | |
517 | 568 |
518 Log.d(TAG, "Peer connection created."); | 569 Log.d(TAG, "Peer connection created."); |
519 } | 570 } |
520 | 571 |
521 private void closeInternal() { | 572 private void closeInternal() { |
522 if (factory != null && peerConnectionParameters.aecDump) { | 573 if (factory != null && peerConnectionParameters.aecDump) { |
523 factory.stopAecDump(); | 574 factory.stopAecDump(); |
524 } | 575 } |
525 Log.d(TAG, "Closing peer connection."); | 576 Log.d(TAG, "Closing peer connection."); |
526 statsTimer.cancel(); | 577 statsTimer.cancel(); |
578 if (dataChannel != null) { | |
579 dataChannel.dispose(); | |
580 dataChannel = null; | |
581 } | |
527 if (peerConnection != null) { | 582 if (peerConnection != null) { |
528 peerConnection.dispose(); | 583 peerConnection.dispose(); |
529 peerConnection = null; | 584 peerConnection = null; |
530 } | 585 } |
531 Log.d(TAG, "Closing audio source."); | 586 Log.d(TAG, "Closing audio source."); |
532 if (audioSource != null) { | 587 if (audioSource != null) { |
533 audioSource.dispose(); | 588 audioSource.dispose(); |
534 audioSource = null; | 589 audioSource = null; |
535 } | 590 } |
536 Log.d(TAG, "Stopping capture."); | 591 Log.d(TAG, "Stopping capture."); |
537 if (videoCapturer != null) { | 592 if (videoCapturer != null) { |
538 try { | 593 try { |
539 videoCapturer.stopCapture(); | 594 videoCapturer.stopCapture(); |
540 } catch (InterruptedException e) { | 595 } catch (InterruptedException e) { |
541 throw new RuntimeException(e); | 596 throw new RuntimeException(e); |
542 } | 597 } |
543 videoCapturerStopped = true; | |
magjed_webrtc
2016/11/17 10:51:05
Don't remove this.
| |
544 videoCapturer.dispose(); | 598 videoCapturer.dispose(); |
545 videoCapturer = null; | 599 videoCapturer = null; |
546 } | 600 } |
547 Log.d(TAG, "Closing video source."); | 601 Log.d(TAG, "Closing video source."); |
548 if (videoSource != null) { | 602 if (videoSource != null) { |
549 videoSource.dispose(); | 603 videoSource.dispose(); |
550 videoSource = null; | 604 videoSource = null; |
551 } | 605 } |
552 Log.d(TAG, "Closing peer connection factory."); | 606 Log.d(TAG, "Closing peer connection factory."); |
553 if (factory != null) { | 607 if (factory != null) { |
(...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1069 executor.execute(new Runnable() { | 1123 executor.execute(new Runnable() { |
1070 @Override | 1124 @Override |
1071 public void run() { | 1125 public void run() { |
1072 remoteVideoTrack = null; | 1126 remoteVideoTrack = null; |
1073 } | 1127 } |
1074 }); | 1128 }); |
1075 } | 1129 } |
1076 | 1130 |
1077 @Override | 1131 @Override |
1078 public void onDataChannel(final DataChannel dc) { | 1132 public void onDataChannel(final DataChannel dc) { |
1079 reportError("AppRTC doesn't use data channels, but got: " + dc.label() + " anyway!"); | 1133 Log.d(TAG, "New Data channel " + dc.label()); |
1134 | |
1135 if (!dataChannelEnabled) | |
1136 return; | |
1137 | |
1138 dc.registerObserver(new DataChannel.Observer() { | |
1139 public void onBufferedAmountChange(long previousAmount) { | |
1140 Log.d(TAG, "Data channel buffered amount changed: " + dc.label() + ": " + dc.state()); | |
1141 } | |
1142 | |
1143 @Override | |
1144 public void onStateChange() { | |
1145 Log.d(TAG, "Data channel state changed: " + dc.label() + ": " + dc.sta te()); | |
1146 } | |
1147 | |
1148 @Override | |
1149 public void onMessage(final DataChannel.Buffer buffer) { | |
1150 if (buffer.binary) { | |
1151 Log.d(TAG, "Received binary msg over " + dc); | |
1152 return; | |
1153 } | |
1154 ByteBuffer data = buffer.data; | |
1155 final byte[] bytes = new byte[data.capacity()]; | |
1156 data.get(bytes); | |
1157 String strData = new String(bytes); | |
1158 Log.d(TAG, "Got msg: " + strData + " over " + dc); | |
1159 } | |
1160 }); | |
1080 } | 1161 } |
1081 | 1162 |
1082 @Override | 1163 @Override |
1083 public void onRenegotiationNeeded() { | 1164 public void onRenegotiationNeeded() { |
1084 // No need to do anything; AppRTC follows a pre-agreed-upon | 1165 // No need to do anything; AppRTC follows a pre-agreed-upon |
1085 // signaling/negotiation protocol. | 1166 // signaling/negotiation protocol. |
1086 } | 1167 } |
1087 } | 1168 } |
1088 | 1169 |
1089 // Implementation detail: handle offer creation/signaling and answer setting, | 1170 // Implementation detail: handle offer creation/signaling and answer setting, |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1159 public void onCreateFailure(final String error) { | 1240 public void onCreateFailure(final String error) { |
1160 reportError("createSDP error: " + error); | 1241 reportError("createSDP error: " + error); |
1161 } | 1242 } |
1162 | 1243 |
1163 @Override | 1244 @Override |
1164 public void onSetFailure(final String error) { | 1245 public void onSetFailure(final String error) { |
1165 reportError("setSDP error: " + error); | 1246 reportError("setSDP error: " + error); |
1166 } | 1247 } |
1167 } | 1248 } |
1168 } | 1249 } |
OLD | NEW |