Skip to content

Commit

Permalink
Fixed Content-Base not being processed
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeyvasilyev committed Oct 13, 2024
1 parent 9e1d86c commit ea4b552
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
40 changes: 34 additions & 6 deletions library-client-rtsp/src/main/java/com/alexvas/rtsp/RtspClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ public static class SdpInfo {
public abstract static class Track {
public String request;
public int payloadType;

@NonNull
@Override
public String toString() {
return "Track{request='" + request + "', payloadType=" + payloadType + '}';
}
}

public static final int VIDEO_CODEC_H264 = 0;
Expand Down Expand Up @@ -235,7 +241,7 @@ private final static class NoResponseHeadersException extends IOException {
}

private final @NonNull Socket rtspSocket;
private final @NonNull String uriRtsp;
private @NonNull String uriRtsp;
private final @NonNull AtomicBoolean exitFlag;
private final @NonNull RtspClientListener listener;

Expand Down Expand Up @@ -369,6 +375,14 @@ public void execute() {
if (DEBUG)
Log.i(TAG, "DESCRIBE status: " + status);
checkStatusCode(status);

String contentBaseUri = getHeaderContentBase(headers);
if (contentBaseUri != null) {
if (debug)
Log.i(TAG_DEBUG, "RTSP URI changed to '" + uriRtsp + "'");
uriRtsp = contentBaseUri;
}

int contentLength = getHeaderContentLength(headers);
if (contentLength > 0) {
String content = readContentAsText(inputStream, contentLength);
Expand Down Expand Up @@ -413,7 +427,7 @@ public void execute() {
case 0 -> track = requestVideo ? sdpInfo.videoTrack : null;
case 1 -> track = requestAudio ? sdpInfo.audioTrack : null;
default -> track = requestApplication ? sdpInfo.applicationTrack : null;
};
}
if (track != null) {
String uriRtspSetup = getUriForSetup(uriRtsp, track);
if (uriRtspSetup == null) {
Expand Down Expand Up @@ -566,6 +580,7 @@ public void execute() {

@Nullable
private static String getUriForSetup(@NonNull String uriRtsp, @Nullable Track track) {
if (DEBUG) Log.v(TAG, "getUriForSetup(uriRtsp='" + uriRtsp + "', track=" + track + ")");
if (track == null)
return null;
if (track.request == null) {
Expand All @@ -574,12 +589,12 @@ private static String getUriForSetup(@NonNull String uriRtsp, @Nullable Track tr
track.request = uriRtsp;
}
String uriRtspSetup = uriRtsp;
// Absolute URL
if (track.request.startsWith("rtsp://") || track.request.startsWith("rtsps://")) {
// Absolute URL
uriRtspSetup = track.request;
// Relative URL
} else {
// Relative URL
if (!track.request.startsWith("/")) {
if (!track.request.startsWith("/") && !uriRtspSetup.endsWith("/")) {
track.request = "/" + track.request;
}
uriRtspSetup += track.request;
Expand Down Expand Up @@ -911,7 +926,7 @@ private ArrayList<Pair<String, String>> readResponseHeaders(@NonNull InputStream
if (CRLF.equals(line)) {
return headers;
} else {
String[] pairs = TextUtils.split(line, ":");
String[] pairs = line.split(":", 2);
if (pairs.length == 2) {
headers.add(Pair.create(pairs[0].trim(), pairs[1].trim()));
}
Expand Down Expand Up @@ -1232,6 +1247,19 @@ private static void updateAudioTrackFromDescribeParam(@NonNull AudioTrack audioT
}
}

/**
* Search for header "Content-Base: rtsp://example.com/stream/"
* and return "rtsp://example.com/stream/"
*/
@Nullable
private static String getHeaderContentBase(@NonNull ArrayList<Pair<String, String>> headers) {
String contentBase = getHeader(headers, "content-base");
if (!TextUtils.isEmpty(contentBase)) {
return contentBase;
}
return null;
}

private static int getHeaderContentLength(@NonNull ArrayList<Pair<String, String>> headers) {
String length = getHeader(headers, "content-length");
if (!TextUtils.isEmpty(length)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.view.SurfaceView
import androidx.annotation.OptIn
import androidx.media3.common.util.UnstableApi
import androidx.media3.container.NalUnitUtil
import com.alexvas.rtsp.codec.VideoDecodeThread
import com.alexvas.rtsp.codec.VideoDecodeThread.DecoderType
import com.alexvas.rtsp.codec.VideoDecoderSurfaceThread
import com.alexvas.rtsp.widget.RtspProcessor.Statistics
Expand Down Expand Up @@ -54,12 +53,6 @@ open class RtspSurfaceView: SurfaceView {
get() = rtspProcessor.debug
set(value) { rtspProcessor.debug = value }

var onApplicationDataReceived: ((
data: ByteArray,
offset: Int,
length: Int
) -> VideoDecodeThread)? = null

private val surfaceCallback = object: SurfaceHolder.Callback {
override fun surfaceCreated(holder: SurfaceHolder) {
if (DEBUG) Log.v(TAG, "surfaceCreated()")
Expand Down

0 comments on commit ea4b552

Please sign in to comment.