Cleaning up Android AppRTCDemo.
- Move signaling code from Activity to a separate class and add interface for AppRTC signaling. For now only pure GAE signaling implements this interface. - Move peer connection, video source and peer connection and SDP observer code from Activity to a separate class. - Main Activity class will do only high level calls and event handling for peer connection and signaling classes. - Also add video renderer position update and use full screen for local preview until the connection is established. BUG= R=braveyao@webrtc.org, pthatcher@webrtc.org Review URL: https://webrtc-codereview.appspot.com/24019004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@7469 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
@@ -0,0 +1,445 @@
|
||||
/*
|
||||
* libjingle
|
||||
* Copyright 2014, Google Inc.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
||||
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
|
||||
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
||||
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
||||
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
||||
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
||||
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.appspot.apprtc;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.util.Log;
|
||||
|
||||
import org.appspot.apprtc.AppRTCClient.AppRTCSignalingParameters;
|
||||
import org.webrtc.DataChannel;
|
||||
import org.webrtc.IceCandidate;
|
||||
import org.webrtc.MediaConstraints;
|
||||
import org.webrtc.MediaStream;
|
||||
import org.webrtc.MediaStreamTrack;
|
||||
import org.webrtc.PeerConnection;
|
||||
import org.webrtc.PeerConnection.IceConnectionState;
|
||||
import org.webrtc.PeerConnectionFactory;
|
||||
import org.webrtc.SdpObserver;
|
||||
import org.webrtc.SessionDescription;
|
||||
import org.webrtc.StatsObserver;
|
||||
import org.webrtc.VideoCapturer;
|
||||
import org.webrtc.VideoRenderer;
|
||||
import org.webrtc.VideoSource;
|
||||
import org.webrtc.VideoTrack;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class PeerConnectionClient {
|
||||
private static final String TAG = "RTCClient";
|
||||
private final Activity activity;
|
||||
private PeerConnectionFactory factory;
|
||||
private PeerConnection pc;
|
||||
private VideoSource videoSource;
|
||||
private boolean videoSourceStopped;
|
||||
private final PCObserver pcObserver = new PCObserver();
|
||||
private final SDPObserver sdpObserver = new SDPObserver();
|
||||
private final VideoRenderer.Callbacks remoteRender;
|
||||
private LinkedList<IceCandidate> queuedRemoteCandidates =
|
||||
new LinkedList<IceCandidate>();
|
||||
private MediaConstraints sdpMediaConstraints;
|
||||
private PeerConnectionEvents events;
|
||||
private boolean isInitiator;
|
||||
private SessionDescription localSdp = null; // either offer or answer SDP
|
||||
|
||||
public PeerConnectionClient(
|
||||
Activity activity,
|
||||
VideoRenderer.Callbacks localRender,
|
||||
VideoRenderer.Callbacks remoteRender,
|
||||
AppRTCSignalingParameters appRtcParameters,
|
||||
PeerConnectionEvents events) {
|
||||
this.activity = activity;
|
||||
this.remoteRender = remoteRender;
|
||||
this.events = events;
|
||||
isInitiator = appRtcParameters.initiator;
|
||||
|
||||
sdpMediaConstraints = new MediaConstraints();
|
||||
sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair(
|
||||
"OfferToReceiveAudio", "true"));
|
||||
sdpMediaConstraints.mandatory.add(new MediaConstraints.KeyValuePair(
|
||||
"OfferToReceiveVideo", "true"));
|
||||
|
||||
factory = new PeerConnectionFactory();
|
||||
|
||||
MediaConstraints pcConstraints = appRtcParameters.pcConstraints;
|
||||
pcConstraints.optional.add(
|
||||
new MediaConstraints.KeyValuePair("RtpDataChannels", "true"));
|
||||
pc = factory.createPeerConnection(appRtcParameters.iceServers,
|
||||
pcConstraints, pcObserver);
|
||||
isInitiator = false;
|
||||
|
||||
// Uncomment to get ALL WebRTC tracing and SENSITIVE libjingle logging.
|
||||
// NOTE: this _must_ happen while |factory| is alive!
|
||||
// Logging.enableTracing(
|
||||
// "logcat:",
|
||||
// EnumSet.of(Logging.TraceLevel.TRACE_ALL),
|
||||
// Logging.Severity.LS_SENSITIVE);
|
||||
|
||||
Log.d(TAG, "Creating local video source");
|
||||
MediaStream lMS = factory.createLocalMediaStream("ARDAMS");
|
||||
if (appRtcParameters.videoConstraints != null) {
|
||||
VideoCapturer capturer = getVideoCapturer();
|
||||
videoSource = factory.createVideoSource(
|
||||
capturer, appRtcParameters.videoConstraints);
|
||||
VideoTrack videoTrack =
|
||||
factory.createVideoTrack("ARDAMSv0", videoSource);
|
||||
videoTrack.addRenderer(new VideoRenderer(localRender));
|
||||
lMS.addTrack(videoTrack);
|
||||
}
|
||||
if (appRtcParameters.audioConstraints != null) {
|
||||
lMS.addTrack(factory.createAudioTrack(
|
||||
"ARDAMSa0",
|
||||
factory.createAudioSource(appRtcParameters.audioConstraints)));
|
||||
}
|
||||
pc.addStream(lMS, new MediaConstraints());
|
||||
}
|
||||
|
||||
public boolean getStats(StatsObserver observer, MediaStreamTrack track) {
|
||||
return pc.getStats(observer, track);
|
||||
}
|
||||
|
||||
public void createOffer() {
|
||||
isInitiator = true;
|
||||
pc.createOffer(sdpObserver, sdpMediaConstraints);
|
||||
}
|
||||
|
||||
public void createAnswer() {
|
||||
isInitiator = false;
|
||||
pc.createAnswer(sdpObserver, sdpMediaConstraints);
|
||||
}
|
||||
|
||||
|
||||
public void addRemoteIceCandidate(IceCandidate candidate) {
|
||||
if (queuedRemoteCandidates != null) {
|
||||
queuedRemoteCandidates.add(candidate);
|
||||
} else {
|
||||
pc.addIceCandidate(candidate);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRemoteDescription(SessionDescription sdp) {
|
||||
SessionDescription sdpISAC = new SessionDescription(
|
||||
sdp.type, preferISAC(sdp.description));
|
||||
Log.d(TAG, "Set remote SDP");
|
||||
pc.setRemoteDescription(sdpObserver, sdpISAC);
|
||||
}
|
||||
|
||||
public void stopVideoSource() {
|
||||
if (videoSource != null) {
|
||||
videoSource.stop();
|
||||
videoSourceStopped = true;
|
||||
}
|
||||
}
|
||||
|
||||
public void startVideoSource() {
|
||||
if (videoSource != null && videoSourceStopped) {
|
||||
videoSource.restart();
|
||||
videoSourceStopped = false;
|
||||
}
|
||||
}
|
||||
|
||||
public void close() {
|
||||
if (pc != null) {
|
||||
pc.dispose();
|
||||
pc = null;
|
||||
}
|
||||
if (videoSource != null) {
|
||||
videoSource.dispose();
|
||||
videoSource = null;
|
||||
}
|
||||
if (factory != null) {
|
||||
factory.dispose();
|
||||
factory = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* SDP/ICE ready callbacks.
|
||||
*/
|
||||
public static interface PeerConnectionEvents {
|
||||
/**
|
||||
* Callback fired once offer is created and local SDP is set.
|
||||
*/
|
||||
public void onLocalDescription(final SessionDescription sdp);
|
||||
|
||||
/**
|
||||
* Callback fired once local Ice candidate is generated.
|
||||
*/
|
||||
public void onIceCandidate(final IceCandidate candidate);
|
||||
|
||||
/**
|
||||
* Callback fired once connection is established (IceConnectionState is
|
||||
* CONNECTED).
|
||||
*/
|
||||
public void onIceConnected();
|
||||
}
|
||||
|
||||
// Cycle through likely device names for the camera and return the first
|
||||
// capturer that works, or crash if none do.
|
||||
private VideoCapturer getVideoCapturer() {
|
||||
String[] cameraFacing = { "front", "back" };
|
||||
int[] cameraIndex = { 0, 1 };
|
||||
int[] cameraOrientation = { 0, 90, 180, 270 };
|
||||
for (String facing : cameraFacing) {
|
||||
for (int index : cameraIndex) {
|
||||
for (int orientation : cameraOrientation) {
|
||||
String name = "Camera " + index + ", Facing " + facing +
|
||||
", Orientation " + orientation;
|
||||
VideoCapturer capturer = VideoCapturer.create(name);
|
||||
if (capturer != null) {
|
||||
Log.d(TAG, "Using camera: " + name);
|
||||
return capturer;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new RuntimeException("Failed to open capturer");
|
||||
}
|
||||
|
||||
// Poor-man's assert(): die with |msg| unless |condition| is true.
|
||||
private static void abortUnless(boolean condition, String msg) {
|
||||
if (!condition) {
|
||||
throw new RuntimeException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
// Mangle SDP to prefer ISAC/16000 over any other audio codec.
|
||||
private static String preferISAC(String sdpDescription) {
|
||||
String[] lines = sdpDescription.split("\r\n");
|
||||
int mLineIndex = -1;
|
||||
String isac16kRtpMap = null;
|
||||
Pattern isac16kPattern =
|
||||
Pattern.compile("^a=rtpmap:(\\d+) ISAC/16000[\r]?$");
|
||||
for (int i = 0;
|
||||
(i < lines.length) && (mLineIndex == -1 || isac16kRtpMap == null);
|
||||
++i) {
|
||||
if (lines[i].startsWith("m=audio ")) {
|
||||
mLineIndex = i;
|
||||
continue;
|
||||
}
|
||||
Matcher isac16kMatcher = isac16kPattern.matcher(lines[i]);
|
||||
if (isac16kMatcher.matches()) {
|
||||
isac16kRtpMap = isac16kMatcher.group(1);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (mLineIndex == -1) {
|
||||
Log.d(TAG, "No m=audio line, so can't prefer iSAC");
|
||||
return sdpDescription;
|
||||
}
|
||||
if (isac16kRtpMap == null) {
|
||||
Log.d(TAG, "No ISAC/16000 line, so can't prefer iSAC");
|
||||
return sdpDescription;
|
||||
}
|
||||
String[] origMLineParts = lines[mLineIndex].split(" ");
|
||||
StringBuilder newMLine = new StringBuilder();
|
||||
int origPartIndex = 0;
|
||||
// Format is: m=<media> <port> <proto> <fmt> ...
|
||||
newMLine.append(origMLineParts[origPartIndex++]).append(" ");
|
||||
newMLine.append(origMLineParts[origPartIndex++]).append(" ");
|
||||
newMLine.append(origMLineParts[origPartIndex++]).append(" ");
|
||||
newMLine.append(isac16kRtpMap);
|
||||
for (; origPartIndex < origMLineParts.length; ++origPartIndex) {
|
||||
if (!origMLineParts[origPartIndex].equals(isac16kRtpMap)) {
|
||||
newMLine.append(" ").append(origMLineParts[origPartIndex]);
|
||||
}
|
||||
}
|
||||
lines[mLineIndex] = newMLine.toString();
|
||||
StringBuilder newSdpDescription = new StringBuilder();
|
||||
for (String line : lines) {
|
||||
newSdpDescription.append(line).append("\r\n");
|
||||
}
|
||||
return newSdpDescription.toString();
|
||||
}
|
||||
|
||||
private void drainRemoteCandidates() {
|
||||
for (IceCandidate candidate : queuedRemoteCandidates) {
|
||||
pc.addIceCandidate(candidate);
|
||||
}
|
||||
queuedRemoteCandidates = null;
|
||||
}
|
||||
|
||||
// Implementation detail: observe ICE & stream changes and react accordingly.
|
||||
private class PCObserver implements PeerConnection.Observer {
|
||||
@Override
|
||||
public void onIceCandidate(final IceCandidate candidate){
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
events.onIceCandidate(candidate);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(){
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException("PeerConnection error!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSignalingChange(
|
||||
PeerConnection.SignalingState newState) {
|
||||
Log.d(TAG, "SignalingState: " + newState);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIceConnectionChange(
|
||||
PeerConnection.IceConnectionState newState) {
|
||||
Log.d(TAG, "IceConnectionState: " + newState);
|
||||
if (newState == IceConnectionState.CONNECTED) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
events.onIceConnected();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIceGatheringChange(
|
||||
PeerConnection.IceGatheringState newState) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAddStream(final MediaStream stream){
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
abortUnless(stream.audioTracks.size() <= 1 &&
|
||||
stream.videoTracks.size() <= 1,
|
||||
"Weird-looking stream: " + stream);
|
||||
if (stream.videoTracks.size() == 1) {
|
||||
stream.videoTracks.get(0).addRenderer(
|
||||
new VideoRenderer(remoteRender));
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemoveStream(final MediaStream stream){
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
stream.videoTracks.get(0).dispose();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDataChannel(final DataChannel dc) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException(
|
||||
"AppRTC doesn't use data channels, but got: " + dc.label() +
|
||||
" anyway!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRenegotiationNeeded() {
|
||||
// No need to do anything; AppRTC follows a pre-agreed-upon
|
||||
// signaling/negotiation protocol.
|
||||
}
|
||||
}
|
||||
|
||||
// Implementation detail: handle offer creation/signaling and answer setting,
|
||||
// as well as adding remote ICE candidates once the answer SDP is set.
|
||||
private class SDPObserver implements SdpObserver {
|
||||
@Override
|
||||
public void onCreateSuccess(final SessionDescription origSdp) {
|
||||
abortUnless(localSdp == null, "multiple SDP create?!?");
|
||||
final SessionDescription sdp = new SessionDescription(
|
||||
origSdp.type, preferISAC(origSdp.description));
|
||||
localSdp = sdp;
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
Log.d(TAG, "Set local SDP from " + sdp.type);
|
||||
pc.setLocalDescription(sdpObserver, sdp);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSetSuccess() {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
if (isInitiator) {
|
||||
// For offering peer connection we first create offer and set
|
||||
// local SDP, then after receiving answer set remote SDP.
|
||||
if (pc.getRemoteDescription() == null) {
|
||||
// We've just set our local SDP so time to send it.
|
||||
Log.d(TAG, "Local SDP set succesfully");
|
||||
events.onLocalDescription(localSdp);
|
||||
} else {
|
||||
// We've just set remote description,
|
||||
// so drain remote ICE candidates.
|
||||
Log.d(TAG, "Remote SDP set succesfully");
|
||||
drainRemoteCandidates();
|
||||
}
|
||||
} else {
|
||||
// For answering peer connection we set remote SDP and then
|
||||
// create answer and set local SDP.
|
||||
if (pc.getLocalDescription() != null) {
|
||||
// We've just set our local SDP so time to send it and drain
|
||||
// remote ICE candidates.
|
||||
Log.d(TAG, "Local SDP set succesfully");
|
||||
events.onLocalDescription(localSdp);
|
||||
drainRemoteCandidates();
|
||||
} else {
|
||||
// We've just set remote SDP - do nothing for now -
|
||||
// answer will be created soon.
|
||||
Log.d(TAG, "Remote SDP set succesfully");
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCreateFailure(final String error) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException("createSDP error: " + error);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSetFailure(final String error) {
|
||||
activity.runOnUiThread(new Runnable() {
|
||||
public void run() {
|
||||
throw new RuntimeException("setSDP error: " + error);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user