Skip to content

Commit

Permalink
configurable rtp packet size limit
Browse files Browse the repository at this point in the history
  • Loading branch information
vzhn committed Nov 20, 2019
1 parent 9c08b6c commit a5681c2
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/deploy/conf/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ streaming:
file:
class: Filesystem
conf:
max_rtp_size: 65536
# path to video folder (/opt/video_samples for example)
basedir: video
file: jellyfish-5-mbps-hd-h264.mkv
picture:
class: Generated
conf:
max_rtp_size: 65536
picture:
width: 640
height: 480
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/me/vzhilin/bstreamer/server/RtpEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@
import me.vzhilin.bstreamer.server.streaming.file.MediaPacket;

public class RtpEncoder {
private static final int MTU = 65536;
private final int maxRtpSize;
private long seqNo = 0;

public RtpEncoder(int maxRtpSize) {
this.maxRtpSize = maxRtpSize;
if (this.maxRtpSize > 65536 || this.maxRtpSize <= 18) {
throw new RuntimeException("incorrect maxRtpSize");
}
}

public void encode(ByteBuf buffer, MediaPacket pkt, long rtpTimestamp) {
int sz = pkt.getPayload().readableBytes();
if (sz + 16 > MTU) {
if (sz + 16 > maxRtpSize) {
writeFuA(buffer, pkt, rtpTimestamp);
} else {
writeNalu(buffer, pkt, rtpTimestamp);
Expand Down Expand Up @@ -54,7 +61,7 @@ private void writeFuA(ByteBuf buffer, MediaPacket pkt, long rtpTimestamp) {
// 12 (RTP header)
// 1 (FU header)
// 1 (FU indicator)
int numberOfPackets = (sz - 2) / (MTU - 18) + 1;
int numberOfPackets = (sz - 2) / (maxRtpSize - 18) + 1;

byte firstByte = payload.readByte();
int fuIndicator = firstByte & 0b11100000 | 28;
Expand All @@ -74,7 +81,7 @@ private void writeFuA(ByteBuf buffer, MediaPacket pkt, long rtpTimestamp) {

fuHeader |= (r & 1) << 5;

int dataLen = Math.min(MTU - 18, sz - offset);
int dataLen = Math.min(maxRtpSize - 18, sz - offset);
writeInterleavedHeader(buffer, dataLen + 12 + 2);
writeRtpHeader(buffer, pkt.isKey(), rtpTimestamp);

Expand All @@ -87,8 +94,8 @@ private void writeFuA(ByteBuf buffer, MediaPacket pkt, long rtpTimestamp) {
}

public int estimateSize(int payloadSize) {
if (payloadSize + 16 > MTU) {
int numberOfPackets = (payloadSize - 2) / (MTU - 18) + 1;
if (payloadSize + 16 > maxRtpSize) {
int numberOfPackets = (payloadSize - 2) / (maxRtpSize - 18) + 1;
return numberOfPackets * (12 + 2 + 4) + payloadSize;
} else {
return payloadSize + 16;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private FullHttpResponse description(RtspUriParser uri, SourceDescription descri
String profileLevelId = String.format("%02x%02x%02x", sps[0], sps[1], sps[2]);
String sdpMessage = "v=0\r\n" +
"o=RTSP 50539017935697 1 IN IP4 0.0.0.0\r\n" +
"s=1234\r\n" +
"s=bserver\r\n" +
"a=control:*\r\n" +
"m=video 0 RTP/AVP 98\r\n" +
"a=fmtp:98 sprop-parameter-sets=" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ public final class PushSource {
public PushSource(Supplier<PullSource> pullSourceSupplier,
PropertyMap props,
ScheduledExecutorService pullExecutor,
BufferingLimits limits) {
BufferingLimits bufferingLimits) {
this.pullExecutor = pullExecutor;
this.props = props;
task = new PushTask(pullSourceSupplier, limits, pullExecutor);
int maxRtpSize = props.getInt("max_rtp_size", 65536);
task = new PushTask(pullSourceSupplier, bufferingLimits, maxRtpSize, pullExecutor);
}

public SourceDescription describe() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ final class PushTask implements Runnable {
private boolean finished;
private long startTimeMillis;
private long startDtsMillis;
private final RtpEncoder interleavedEncoder = new RtpEncoder();
private final RtpEncoder interleavedEncoder;
private ScheduledFuture<?> advanceFuture = null;

private long lastDts;
Expand All @@ -36,7 +36,9 @@ final class PushTask implements Runnable {

PushTask(Supplier<PullSource> pullSource,
BufferingLimits limits,
int maxRtpSize,
ScheduledExecutorService executor) {
this.interleavedEncoder = new RtpEncoder(maxRtpSize);
this.limits = limits;
this.sourceSupplier = pullSource;
this.executor = executor;
Expand Down

0 comments on commit a5681c2

Please sign in to comment.