Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enables a configurable SocketFactory for the RTSP client #9606

Merged
merged 21 commits into from
Dec 6, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
* RTSP:
* Support RFC4566 SDP attribute field grammar
([#9430](https://github.com/google/ExoPlayer/issues/9430)).
* Provide a client API to override the `SocketFactory` used for any server
connection ([#9606](https://github.com/google/ExoPlayer/pull/9606)).
* DASH:
* Populate `Format.sampleMimeType`, `width` and `height` for image
`AdaptationSet` elements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ public interface PlaybackEventListener {
private final PlaybackEventListener playbackEventListener;
private final String userAgent;
private final boolean debugLoggingEnabled;
@Nullable private final SocketFactory socketFactory;
ened marked this conversation as resolved.
Show resolved Hide resolved
private final ArrayDeque<RtpLoadInfo> pendingSetupRtpLoadInfos;
// TODO(b/172331505) Add a timeout monitor for pending requests.
private final SparseArray<RtspRequest> pendingRequests;
Expand Down Expand Up @@ -161,11 +162,13 @@ public RtspClient(
PlaybackEventListener playbackEventListener,
String userAgent,
Uri uri,
boolean debugLoggingEnabled) {
boolean debugLoggingEnabled,
ened marked this conversation as resolved.
Show resolved Hide resolved
@Nullable SocketFactory socketFactory) {
this.sessionInfoListener = sessionInfoListener;
this.playbackEventListener = playbackEventListener;
this.userAgent = userAgent;
this.debugLoggingEnabled = debugLoggingEnabled;
this.socketFactory = socketFactory;
ened marked this conversation as resolved.
Show resolved Hide resolved
this.pendingSetupRtpLoadInfos = new ArrayDeque<>();
this.pendingRequests = new SparseArray<>();
this.messageSender = new MessageSender();
Expand Down Expand Up @@ -286,10 +289,14 @@ private void maybeLogMessage(List<String> message) {
}

/** Returns a {@link Socket} that is connected to the {@code uri}. */
private static Socket getSocket(Uri uri) throws IOException {
private Socket getSocket(Uri uri) throws IOException {
checkArgument(uri.getHost() != null);
int rtspPort = uri.getPort() > 0 ? uri.getPort() : DEFAULT_RTSP_PORT;
return SocketFactory.getDefault().createSocket(checkNotNull(uri.getHost()), rtspPort);

SocketFactory socketFactory =
this.socketFactory != null ? this.socketFactory : SocketFactory.getDefault();

return socketFactory.createSocket(checkNotNull(uri.getHost()), rtspPort);
}

private void dispatchRtspError(Throwable error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import java.net.BindException;
import java.util.ArrayList;
import java.util.List;
import javax.net.SocketFactory;
import org.checkerframework.checker.nullness.compatqual.NullableType;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;

Expand Down Expand Up @@ -96,20 +97,22 @@ interface Listener {

/**
* Creates an RTSP media period.
*
* @param allocator An {@link Allocator} from which to obtain media buffer allocations.
* @param rtpDataChannelFactory A {@link RtpDataChannel.Factory} for {@link RtpDataChannel}.
* @param uri The RTSP playback {@link Uri}.
* @param listener A {@link Listener} to receive session information updates.
* @param userAgent The user agent.
* @param debugLoggingEnabled Whether to log RTSP messages.
* @param socketFactory A socket factory.
ened marked this conversation as resolved.
Show resolved Hide resolved
*/
public RtspMediaPeriod(
Allocator allocator,
RtpDataChannel.Factory rtpDataChannelFactory,
Uri uri,
Listener listener,
String userAgent,
boolean debugLoggingEnabled) {
boolean debugLoggingEnabled,
@Nullable SocketFactory socketFactory) {
this.allocator = allocator;
this.rtpDataChannelFactory = rtpDataChannelFactory;
this.listener = listener;
Expand All @@ -122,7 +125,8 @@ public RtspMediaPeriod(
/* playbackEventListener= */ internalListener,
/* userAgent= */ userAgent,
/* uri= */ uri,
debugLoggingEnabled);
debugLoggingEnabled,
socketFactory);
rtspLoaderWrappers = new ArrayList<>();
selectedLoadInfos = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import com.google.android.exoplayer2.upstream.LoadErrorHandlingPolicy;
import com.google.android.exoplayer2.upstream.TransferListener;
import java.io.IOException;
import javax.net.SocketFactory;

/** An Rtsp {@link MediaSource} */
public final class RtspMediaSource extends BaseMediaSource {
Expand Down Expand Up @@ -70,6 +71,7 @@ public static final class Factory implements MediaSourceFactory {
private String userAgent;
private boolean forceUseRtpTcp;
private boolean debugLoggingEnabled;
private SocketFactory socketFactory;
ened marked this conversation as resolved.
Show resolved Hide resolved

public Factory() {
timeoutMs = DEFAULT_TIMEOUT_MS;
Expand Down Expand Up @@ -117,6 +119,19 @@ public Factory setDebugLoggingEnabled(boolean debugLoggingEnabled) {
return this;
}

/**
* Configures a socket factory to be used for client connections.
ened marked this conversation as resolved.
Show resolved Hide resolved
*
* When unspecified, {@link SocketFactory#getDefault()} is used.
*
* @param socketFactory A socket factory.
* @return This Factory, for convenience.
*/
public Factory setSocketFactory(SocketFactory socketFactory) {
this.socketFactory = socketFactory;
return this;
}

/**
* Sets the timeout in milliseconds, the default value is {@link #DEFAULT_TIMEOUT_MS}.
*
Expand Down Expand Up @@ -202,7 +217,8 @@ public RtspMediaSource createMediaSource(MediaItem mediaItem) {
? new TransferRtpDataChannelFactory(timeoutMs)
: new UdpDataSourceRtpDataChannelFactory(timeoutMs),
userAgent,
debugLoggingEnabled);
debugLoggingEnabled,
socketFactory);
ened marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -227,6 +243,9 @@ public RtspPlaybackException(String message, Throwable e) {
private final Uri uri;
private final boolean debugLoggingEnabled;

@Nullable
private final SocketFactory socketFactory;

private long timelineDurationUs;
private boolean timelineIsSeekable;
private boolean timelineIsLive;
Expand All @@ -237,12 +256,14 @@ public RtspPlaybackException(String message, Throwable e) {
MediaItem mediaItem,
RtpDataChannel.Factory rtpDataChannelFactory,
String userAgent,
boolean debugLoggingEnabled) {
boolean debugLoggingEnabled,
@Nullable SocketFactory socketFactory) {
this.mediaItem = mediaItem;
this.rtpDataChannelFactory = rtpDataChannelFactory;
this.userAgent = userAgent;
this.uri = checkNotNull(this.mediaItem.localConfiguration).uri;
this.debugLoggingEnabled = debugLoggingEnabled;
this.socketFactory = socketFactory;
this.timelineDurationUs = C.TIME_UNSET;
this.timelineIsPlaceholder = true;
}
Expand Down Expand Up @@ -281,7 +302,8 @@ public MediaPeriod createPeriod(MediaPeriodId id, Allocator allocator, long star
notifySourceInfoRefreshed();
},
userAgent,
debugLoggingEnabled);
debugLoggingEnabled,
socketFactory);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@
import com.google.android.exoplayer2.source.rtsp.RtspMediaSource.RtspPlaybackException;
import com.google.android.exoplayer2.util.Util;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import javax.net.SocketFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand Down Expand Up @@ -77,6 +81,79 @@ public void tearDown() {
Util.closeQuietly(rtspClient);
}

@Test
public void connectServerAndClient_usesCustomSocketFactory() throws Exception {
class ResponseProvider implements RtspServer.ResponseProvider {
@Override
public RtspResponse getOptionsResponse() {
return new RtspResponse(
/* status= */ 200,
new RtspHeaders.Builder().add(RtspHeaders.PUBLIC, "OPTIONS, DESCRIBE").build());
}

@Override
public RtspResponse getDescribeResponse(Uri requestedUri) {
return RtspTestUtils.newDescribeResponseWithSdpMessage(
SESSION_DESCRIPTION, rtpPacketStreamDumps, requestedUri);
}
}
rtspServer = new RtspServer(new ResponseProvider());

final AtomicReference<Boolean> didCallCreateSocket = new AtomicReference<>();

final SocketFactory socketFactory =
new SocketFactory() {

@Override
public Socket createSocket(String host, int port) throws IOException {
didCallCreateSocket.set(true);

return SocketFactory.getDefault().createSocket(host, port);
}

@Override
public Socket createSocket(String s, int i, InetAddress inetAddress, int i1)
throws IOException {
return SocketFactory.getDefault().createSocket(s, i, inetAddress, i1);
}

@Override
public Socket createSocket(InetAddress inetAddress, int i) throws IOException {
return SocketFactory.getDefault().createSocket(inetAddress, i);
}

@Override
public Socket createSocket(
InetAddress inetAddress, int i, InetAddress inetAddress1, int i1) throws IOException {
return SocketFactory.getDefault().createSocket(inetAddress, i, inetAddress1, i1);
}
};

AtomicReference<ImmutableList<RtspMediaTrack>> tracksInSession = new AtomicReference<>();
rtspClient =
new RtspClient(
new SessionInfoListener() {
@Override
public void onSessionTimelineUpdated(
RtspSessionTiming timing, ImmutableList<RtspMediaTrack> tracks) {
tracksInSession.set(tracks);
}

@Override
public void onSessionTimelineRequestFailed(
String message, @Nullable Throwable cause) {}
},
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false,
socketFactory);
rtspClient.start();
RobolectricUtil.runMainLooperUntil(() -> tracksInSession.get() != null);

assertThat(didCallCreateSocket.get()).isTrue();
}

@Test
public void connectServerAndClient_serverSupportsDescribe_updatesSessionTimeline()
throws Exception {
Expand Down Expand Up @@ -113,7 +190,8 @@ public void onSessionTimelineRequestFailed(
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);
ened marked this conversation as resolved.
Show resolved Hide resolved
rtspClient.start();
RobolectricUtil.runMainLooperUntil(() -> tracksInSession.get() != null);

Expand Down Expand Up @@ -164,7 +242,8 @@ public void onSessionTimelineRequestFailed(
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);
rtspClient.start();
RobolectricUtil.runMainLooperUntil(() -> tracksInSession.get() != null);

Expand Down Expand Up @@ -207,7 +286,8 @@ public void onSessionTimelineRequestFailed(
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);
rtspClient.start();
RobolectricUtil.runMainLooperUntil(() -> tracksInSession.get() != null);

Expand Down Expand Up @@ -253,7 +333,8 @@ public void onSessionTimelineRequestFailed(
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);
rtspClient.start();
RobolectricUtil.runMainLooperUntil(() -> failureMessage.get() != null);

Expand Down Expand Up @@ -299,7 +380,8 @@ public void onSessionTimelineRequestFailed(
EMPTY_PLAYBACK_LISTENER,
/* userAgent= */ "ExoPlayer:RtspClientTest",
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);
rtspClient.start();

RobolectricUtil.runMainLooperUntil(() -> failureCause.get() != null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public RtspResponse getDescribeResponse(Uri requestedUri) {
RtspTestUtils.getTestUri(rtspServer.startAndGetPortNumber()),
/* listener= */ timing -> refreshedSourceDurationMs.set(timing.getDurationMs()),
/* userAgent= */ "ExoPlayer:RtspPeriodTest",
/* debugLoggingEnabled= */ false);
/* debugLoggingEnabled= */ false,
/* socketFactory */ null);

mediaPeriod.prepare(
new MediaPeriod.Callback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ private ExoPlayer createExoPlayer(
MediaItem.fromUri(RtspTestUtils.getTestUri(serverRtspPortNumber)),
rtpDataChannelFactory,
"ExoPlayer:PlaybackTest",
/* debugLoggingEnabled= */ false),
/* debugLoggingEnabled= */ false,
/* socketFactory */ null),
false);
return player;
}
Expand Down