|
|
@ -40,8 +40,16 @@ public class PeerConnectionWrapper {
|
|
|
|
@NonNull private final AudioSource audioSource;
|
|
|
|
@NonNull private final AudioSource audioSource;
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable private final VideoCapturer videoCapturer;
|
|
|
|
@Nullable private final VideoCapturer videoCapturer;
|
|
|
|
|
|
|
|
@Nullable private final VideoCapturer videoCapturerRear;
|
|
|
|
@Nullable private final VideoSource videoSource;
|
|
|
|
@Nullable private final VideoSource videoSource;
|
|
|
|
|
|
|
|
@Nullable private final VideoSource videoSourceRear;
|
|
|
|
@Nullable private final VideoTrack videoTrack;
|
|
|
|
@Nullable private final VideoTrack videoTrack;
|
|
|
|
|
|
|
|
@Nullable private final VideoTrack videoTrackRear;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable private VideoCapturer videoCapturerActive;
|
|
|
|
|
|
|
|
@Nullable private VideoTrack videoTrackActive;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Nullable private final MediaStream mediaStream;
|
|
|
|
|
|
|
|
|
|
|
|
public PeerConnectionWrapper(@NonNull Context context,
|
|
|
|
public PeerConnectionWrapper(@NonNull Context context,
|
|
|
|
@NonNull PeerConnectionFactory factory,
|
|
|
|
@NonNull PeerConnectionFactory factory,
|
|
|
@ -72,44 +80,71 @@ public class PeerConnectionWrapper {
|
|
|
|
this.peerConnection.setAudioPlayout(false);
|
|
|
|
this.peerConnection.setAudioPlayout(false);
|
|
|
|
this.peerConnection.setAudioRecording(false);
|
|
|
|
this.peerConnection.setAudioRecording(false);
|
|
|
|
|
|
|
|
|
|
|
|
this.videoCapturer = createVideoCapturer(context);
|
|
|
|
this.videoCapturer = createVideoCapturer(context, false);
|
|
|
|
|
|
|
|
this.videoCapturerRear = createVideoCapturer(context, true);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.videoCapturerActive = videoCapturer;
|
|
|
|
|
|
|
|
|
|
|
|
MediaStream mediaStream = factory.createLocalMediaStream("ARDAMS");
|
|
|
|
this.mediaStream = factory.createLocalMediaStream("ARDAMS");
|
|
|
|
this.audioSource = factory.createAudioSource(audioConstraints);
|
|
|
|
this.audioSource = factory.createAudioSource(audioConstraints);
|
|
|
|
this.audioTrack = factory.createAudioTrack("ARDAMSa0", audioSource);
|
|
|
|
this.audioTrack = factory.createAudioTrack("ARDAMSa0", audioSource);
|
|
|
|
this.audioTrack.setEnabled(false);
|
|
|
|
this.audioTrack.setEnabled(false);
|
|
|
|
mediaStream.addTrack(audioTrack);
|
|
|
|
this.mediaStream.addTrack(audioTrack);
|
|
|
|
|
|
|
|
|
|
|
|
if (videoCapturer != null) {
|
|
|
|
if (videoCapturer != null) {
|
|
|
|
this.videoSource = factory.createVideoSource(videoCapturer);
|
|
|
|
this.videoSource = factory.createVideoSource(videoCapturer);
|
|
|
|
this.videoTrack = factory.createVideoTrack("ARDAMSv0", videoSource);
|
|
|
|
this.videoTrack = factory.createVideoTrack("ARDAMSv0", videoSource);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.videoTrackActive = videoTrack;
|
|
|
|
|
|
|
|
|
|
|
|
this.videoTrack.addRenderer(new VideoRenderer(localRenderer));
|
|
|
|
this.videoTrack.addRenderer(new VideoRenderer(localRenderer));
|
|
|
|
this.videoTrack.setEnabled(false);
|
|
|
|
this.videoTrack.setEnabled(false);
|
|
|
|
mediaStream.addTrack(videoTrack);
|
|
|
|
this.mediaStream.addTrack(videoTrack);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
this.videoSource = null;
|
|
|
|
this.videoSource = null;
|
|
|
|
this.videoTrack = null;
|
|
|
|
this.videoTrack = null;
|
|
|
|
|
|
|
|
this.videoTrackActive = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (videoCapturerRear != null) {
|
|
|
|
|
|
|
|
this.videoSourceRear = factory.createVideoSource(videoCapturerRear);
|
|
|
|
|
|
|
|
this.videoTrackRear = factory.createVideoTrack("ARDAMSv0", videoSourceRear);
|
|
|
|
|
|
|
|
this.videoTrackRear.addRenderer(new VideoRenderer(localRenderer));
|
|
|
|
|
|
|
|
this.videoTrackRear.setEnabled(false);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.videoSourceRear = null;
|
|
|
|
|
|
|
|
this.videoTrackRear = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.peerConnection.addStream(mediaStream);
|
|
|
|
this.peerConnection.addStream(mediaStream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setVideoEnabled(boolean enabled) {
|
|
|
|
public void setVideoEnabled(boolean enabled) {
|
|
|
|
if (this.videoTrack != null) {
|
|
|
|
if (this.videoTrackActive != null) {
|
|
|
|
this.videoTrack.setEnabled(enabled);
|
|
|
|
this.videoTrackActive.setEnabled(enabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.videoCapturer != null) {
|
|
|
|
if (this.videoCapturerActive != null) {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
if (enabled) this.videoCapturer.startCapture(1280, 720, 30);
|
|
|
|
if (enabled) this.videoCapturerActive.startCapture(1280, 720, 30);
|
|
|
|
else this.videoCapturer.stopCapture();
|
|
|
|
else this.videoCapturerActive.stopCapture();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void flipCameras(boolean isRear) {
|
|
|
|
|
|
|
|
if (videoCapturerRear != null) {
|
|
|
|
|
|
|
|
setVideoEnabled(false);
|
|
|
|
|
|
|
|
mediaStream.removeTrack(videoTrackActive);
|
|
|
|
|
|
|
|
this.videoTrackActive = isRear ? videoTrackRear : videoTrack;
|
|
|
|
|
|
|
|
this.videoCapturerActive = isRear ? videoCapturerRear : videoCapturer;
|
|
|
|
|
|
|
|
mediaStream.addTrack(videoTrackActive);
|
|
|
|
|
|
|
|
setVideoEnabled(true);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void setCommunicationMode() {
|
|
|
|
public void setCommunicationMode() {
|
|
|
|
this.peerConnection.setAudioPlayout(true);
|
|
|
|
this.peerConnection.setAudioPlayout(true);
|
|
|
|
this.peerConnection.setAudioRecording(true);
|
|
|
|
this.peerConnection.setAudioRecording(true);
|
|
|
@ -268,10 +303,23 @@ public class PeerConnectionWrapper {
|
|
|
|
this.videoCapturer.dispose();
|
|
|
|
this.videoCapturer.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.videoCapturerRear != null) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
this.videoCapturerRear.stopCapture();
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
Log.w(TAG, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
this.videoCapturerRear.dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (this.videoSource != null) {
|
|
|
|
if (this.videoSource != null) {
|
|
|
|
this.videoSource.dispose();
|
|
|
|
this.videoSource.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (this.videoSourceRear != null) {
|
|
|
|
|
|
|
|
this.videoSourceRear.dispose();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
this.audioSource.dispose();
|
|
|
|
this.audioSource.dispose();
|
|
|
|
this.peerConnection.close();
|
|
|
|
this.peerConnection.close();
|
|
|
|
this.peerConnection.dispose();
|
|
|
|
this.peerConnection.dispose();
|
|
|
@ -281,7 +329,7 @@ public class PeerConnectionWrapper {
|
|
|
|
return this.peerConnection.addIceCandidate(candidate);
|
|
|
|
return this.peerConnection.addIceCandidate(candidate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private @Nullable CameraVideoCapturer createVideoCapturer(@NonNull Context context) {
|
|
|
|
private @Nullable CameraVideoCapturer createVideoCapturer(@NonNull Context context, boolean rear) {
|
|
|
|
boolean camera2EnumeratorIsSupported = false;
|
|
|
|
boolean camera2EnumeratorIsSupported = false;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
camera2EnumeratorIsSupported = Camera2Enumerator.isSupported(context);
|
|
|
|
camera2EnumeratorIsSupported = Camera2Enumerator.isSupported(context);
|
|
|
@ -298,12 +346,16 @@ public class PeerConnectionWrapper {
|
|
|
|
String[] deviceNames = enumerator.getDeviceNames();
|
|
|
|
String[] deviceNames = enumerator.getDeviceNames();
|
|
|
|
|
|
|
|
|
|
|
|
for (String deviceName : deviceNames) {
|
|
|
|
for (String deviceName : deviceNames) {
|
|
|
|
if (enumerator.isFrontFacing(deviceName)) {
|
|
|
|
boolean isDesiredDirection =
|
|
|
|
Log.w(TAG, "Creating front facing camera capturer.");
|
|
|
|
rear ? enumerator.isBackFacing(deviceName)
|
|
|
|
|
|
|
|
: enumerator.isFrontFacing(deviceName);
|
|
|
|
|
|
|
|
if (isDesiredDirection) {
|
|
|
|
|
|
|
|
String direction = rear ? "rear" : "front";
|
|
|
|
|
|
|
|
Log.w(TAG, "Creating " + direction + " facing camera capturer.");
|
|
|
|
final CameraVideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
|
|
|
|
final CameraVideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
|
|
|
|
|
|
|
|
|
|
|
|
if (videoCapturer != null) {
|
|
|
|
if (videoCapturer != null) {
|
|
|
|
Log.w(TAG, "Found front facing capturer: " + deviceName);
|
|
|
|
Log.w(TAG, "Found " + direction + " facing capturer: " + deviceName);
|
|
|
|
|
|
|
|
|
|
|
|
return videoCapturer;
|
|
|
|
return videoCapturer;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -311,7 +363,10 @@ public class PeerConnectionWrapper {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
for (String deviceName : deviceNames) {
|
|
|
|
for (String deviceName : deviceNames) {
|
|
|
|
if (!enumerator.isFrontFacing(deviceName)) {
|
|
|
|
boolean isDesiredDirection =
|
|
|
|
|
|
|
|
rear ? enumerator.isBackFacing(deviceName)
|
|
|
|
|
|
|
|
: enumerator.isFrontFacing(deviceName);
|
|
|
|
|
|
|
|
if (!isDesiredDirection) {
|
|
|
|
Log.w(TAG, "Creating other camera capturer.");
|
|
|
|
Log.w(TAG, "Creating other camera capturer.");
|
|
|
|
final CameraVideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
|
|
|
|
final CameraVideoCapturer videoCapturer = enumerator.createCapturer(deviceName, null);
|
|
|
|
|
|
|
|
|
|
|
|