Make PeerConnectionClient non-singleton.

Ownership of EglBase is moved to PeerConnectionClient.

BUG=webrtc:8135

Review-Url: https://codereview.webrtc.org/3007893002
Cr-Commit-Position: refs/heads/master@{#19634}
This commit is contained in:
sakal
2017-08-31 08:03:46 -07:00
committed by Commit Bot
parent da194e79c4
commit 85d7650ab6
3 changed files with 46 additions and 102 deletions

View File

@ -45,7 +45,6 @@ import org.appspot.apprtc.PeerConnectionClient.PeerConnectionParameters;
import org.webrtc.Camera1Enumerator;
import org.webrtc.Camera2Enumerator;
import org.webrtc.CameraEnumerator;
import org.webrtc.EglBase;
import org.webrtc.FileVideoCapturer;
import org.webrtc.IceCandidate;
import org.webrtc.Logging;
@ -162,7 +161,6 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
private AppRTCClient appRtcClient;
private SignalingParameters signalingParameters;
private AppRTCAudioManager audioManager = null;
private EglBase rootEglBase;
private SurfaceViewRenderer pipRenderer;
private SurfaceViewRenderer fullscreenRenderer;
private VideoFileRenderer videoFileRenderer;
@ -234,9 +232,11 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
final Intent intent = getIntent();
// Create peer connection client.
peerConnectionClient = new PeerConnectionClient();
// Create video renderers.
rootEglBase = EglBase.create();
pipRenderer.init(rootEglBase.getEglBaseContext(), null);
pipRenderer.init(peerConnectionClient.getRenderContext(), null);
pipRenderer.setScalingType(ScalingType.SCALE_ASPECT_FIT);
String saveRemoteVideoToFile = intent.getStringExtra(EXTRA_SAVE_REMOTE_VIDEO_TO_FILE);
@ -245,15 +245,15 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
int videoOutWidth = intent.getIntExtra(EXTRA_SAVE_REMOTE_VIDEO_TO_FILE_WIDTH, 0);
int videoOutHeight = intent.getIntExtra(EXTRA_SAVE_REMOTE_VIDEO_TO_FILE_HEIGHT, 0);
try {
videoFileRenderer = new VideoFileRenderer(
saveRemoteVideoToFile, videoOutWidth, videoOutHeight, rootEglBase.getEglBaseContext());
videoFileRenderer = new VideoFileRenderer(saveRemoteVideoToFile, videoOutWidth,
videoOutHeight, peerConnectionClient.getRenderContext());
remoteRenderers.add(videoFileRenderer);
} catch (IOException e) {
throw new RuntimeException(
"Failed to open video file for output: " + saveRemoteVideoToFile, e);
}
}
fullscreenRenderer.init(rootEglBase.getEglBaseContext(), null);
fullscreenRenderer.init(peerConnectionClient.getRenderContext(), null);
fullscreenRenderer.setScalingType(ScalingType.SCALE_ASPECT_FILL);
pipRenderer.setZOrderMediaOverlay(true);
@ -368,7 +368,6 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
}, runTimeMs);
}
peerConnectionClient = PeerConnectionClient.getInstance();
if (loopback) {
PeerConnectionFactory.Options options = new PeerConnectionFactory.Options();
options.networkIgnoreMask = 0;
@ -507,7 +506,6 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
logToast.cancel();
}
activityRunning = false;
rootEglBase.release();
super.onDestroy();
}
@ -623,10 +621,6 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
appRtcClient.disconnectFromRoom();
appRtcClient = null;
}
if (peerConnectionClient != null) {
peerConnectionClient.close();
peerConnectionClient = null;
}
if (pipRenderer != null) {
pipRenderer.release();
pipRenderer = null;
@ -639,6 +633,10 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
fullscreenRenderer.release();
fullscreenRenderer = null;
}
if (peerConnectionClient != null) {
peerConnectionClient.close();
peerConnectionClient = null;
}
if (audioManager != null) {
audioManager.stop();
audioManager = null;
@ -747,8 +745,8 @@ public class CallActivity extends Activity implements AppRTCClient.SignalingEven
if (peerConnectionParameters.videoCallEnabled) {
videoCapturer = createVideoCapturer();
}
peerConnectionClient.createPeerConnection(rootEglBase.getEglBaseContext(), localProxyRenderer,
remoteRenderers, videoCapturer, signalingParameters);
peerConnectionClient.createPeerConnection(
localProxyRenderer, remoteRenderers, videoCapturer, signalingParameters);
if (signalingParameters.initiator) {
logAndToast("Creating OFFER...");

View File

@ -103,11 +103,15 @@ public class PeerConnectionClient {
private static final int HD_VIDEO_HEIGHT = 720;
private static final int BPS_IN_KBPS = 1000;
private static final PeerConnectionClient instance = new PeerConnectionClient();
// Executor thread is started once in private ctor and is used for all
// peer connection API calls to ensure new peer connection factory is
// created on the same thread as previously destroyed factory.
private static final ExecutorService executor = Executors.newSingleThreadExecutor();
private final PCObserver pcObserver = new PCObserver();
private final SDPObserver sdpObserver = new SDPObserver();
private final ExecutorService executor;
private final EglBase rootEglBase;
private PeerConnectionFactory factory;
private PeerConnection peerConnection;
PeerConnectionFactory.Options options = null;
@ -288,15 +292,8 @@ public class PeerConnectionClient {
void onPeerConnectionError(final String description);
}
private PeerConnectionClient() {
// Executor thread is started once in private ctor and is used for all
// peer connection API calls to ensure new peer connection factory is
// created on the same thread as previously destroyed factory.
executor = Executors.newSingleThreadExecutor();
}
public static PeerConnectionClient getInstance() {
return instance;
public PeerConnectionClient() {
rootEglBase = EglBase.create();
}
public void setPeerConnectionFactoryOptions(PeerConnectionFactory.Options options) {
@ -335,15 +332,15 @@ public class PeerConnectionClient {
});
}
public void createPeerConnection(final EglBase.Context renderEGLContext,
final VideoRenderer.Callbacks localRender, final VideoRenderer.Callbacks remoteRender,
final VideoCapturer videoCapturer, final SignalingParameters signalingParameters) {
createPeerConnection(renderEGLContext, localRender, Collections.singletonList(remoteRender),
videoCapturer, signalingParameters);
public void createPeerConnection(final VideoRenderer.Callbacks localRender,
final VideoRenderer.Callbacks remoteRender, final VideoCapturer videoCapturer,
final SignalingParameters signalingParameters) {
createPeerConnection(
localRender, Collections.singletonList(remoteRender), videoCapturer, signalingParameters);
}
public void createPeerConnection(final EglBase.Context renderEGLContext,
final VideoRenderer.Callbacks localRender, final List<VideoRenderer.Callbacks> remoteRenders,
final VideoCapturer videoCapturer, final SignalingParameters signalingParameters) {
public void createPeerConnection(final VideoRenderer.Callbacks localRender,
final List<VideoRenderer.Callbacks> remoteRenders, final VideoCapturer videoCapturer,
final SignalingParameters signalingParameters) {
if (peerConnectionParameters == null) {
Log.e(TAG, "Creating peer connection without initializing factory.");
return;
@ -357,7 +354,7 @@ public class PeerConnectionClient {
public void run() {
try {
createMediaConstraintsInternal();
createPeerConnectionInternal(renderEGLContext);
createPeerConnectionInternal();
} catch (Exception e) {
reportError("Failed to create peer connection: " + e.getMessage());
throw e;
@ -583,7 +580,7 @@ public class PeerConnectionClient {
}
}
private void createPeerConnectionInternal(EglBase.Context renderEGLContext) {
private void createPeerConnectionInternal() {
if (factory == null || isError) {
Log.e(TAG, "Peerconnection factory is not created");
return;
@ -594,8 +591,8 @@ public class PeerConnectionClient {
queuedRemoteCandidates = new LinkedList<IceCandidate>();
if (videoCallEnabled) {
Log.d(TAG, "EGLContext: " + renderEGLContext);
factory.setVideoHwAccelerationOptions(renderEGLContext, renderEGLContext);
factory.setVideoHwAccelerationOptions(
rootEglBase.getEglBaseContext(), rootEglBase.getEglBaseContext());
}
PeerConnection.RTCConfiguration rtcConfig =
@ -698,6 +695,7 @@ public class PeerConnectionClient {
factory = null;
}
options = null;
rootEglBase.release();
Log.d(TAG, "Closing peer connection done.");
events.onPeerConnectionClosed();
PeerConnectionFactory.stopInternalTracingCapture();
@ -713,6 +711,10 @@ public class PeerConnectionClient {
return videoWidth * videoHeight >= 1280 * 720;
}
public EglBase.Context getRenderContext() {
return rootEglBase.getEglBaseContext();
}
private void getStats() {
if (peerConnection == null || isError) {
return;