if (message instanceof ResetMessage) {
sendReset();
return;
}
if (message instanceof RTMPMessage) {
RTMPMessage rtmpMessage = (RTMPMessage) message;
IRTMPEvent body = rtmpMessage.getBody();
if (!(body instanceof IStreamData)) {
throw new RuntimeException("expected IStreamData but got "
+ body.getClass() + " (type " + body.getDataType() + ")");
}
int size = ((IStreamData) body).getData().limit();
if (body instanceof VideoData) {
IVideoStreamCodec videoCodec = null;
if (msgIn instanceof IBroadcastScope) {
IClientBroadcastStream stream = (IClientBroadcastStream) ((IBroadcastScope) msgIn)
.getAttribute(IBroadcastScope.STREAM_ATTRIBUTE);
if (stream != null && stream.getCodecInfo() != null) {
videoCodec = stream.getCodecInfo().getVideoCodec();
}
}
if (videoCodec == null || videoCodec.canDropFrames()) {
if (state == State.PAUSED) {
// The subscriber paused the video
videoFrameDropper.dropPacket(rtmpMessage);
return;
}
// Only check for frame dropping if the codec supports it
long pendingVideos = pendingVideoMessages();
if (!videoFrameDropper.canSendPacket(rtmpMessage,
pendingVideos)) {
// Drop frame as it depends on other frames that were dropped before.
return;
}
boolean drop = !videoBucket.acquireToken(size, 0);
if (!receiveVideo || drop) {
// The client disabled video or the app doesn't have enough bandwidth
// allowed for this stream.
videoFrameDropper.dropPacket(rtmpMessage);
return;
}
Long[] writeDelta = getWriteDelta();
if (pendingVideos > 1 /*|| writeDelta[0] > writeDelta[1]*/) {
// We drop because the client has insufficient bandwidth.
long now = System.currentTimeMillis();
if (bufferCheckInterval > 0 && now >= nextCheckBufferUnderrun) {
// Notify client about frame dropping (keyframe)
sendInsufficientBandwidthStatus(currentItem);
nextCheckBufferUnderrun = now + bufferCheckInterval;
}
videoFrameDropper.dropPacket(rtmpMessage);
return;
}
videoFrameDropper.sendPacket(rtmpMessage);
}
} else if (body instanceof AudioData) {
if (!receiveAudio && sendBlankAudio) {
// Send blank audio packet to reset player
sendBlankAudio = false;
body = new AudioData();
if (lastMessage != null) {
body.setTimestamp(lastMessage.getTimestamp());
} else {
body.setTimestamp(0);
}
rtmpMessage.setBody(body);
} else if (state == State.PAUSED || !receiveAudio || !audioBucket.acquireToken(size, 0)) {
return;
}
}
if (body instanceof IStreamData && ((IStreamData) body).getData() != null) {