org.red5.server.stream.message.RTMPMessage Java Examples

The following examples show how to use org.red5.server.stream.message.RTMPMessage. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Application.java    From red5-rtsp-restreamer with Apache License 2.0 6 votes vote down vote up
@Override
public void packetReceived(IBroadcastStream stream, IStreamPacket packet) {

	RTMPMessage m = RTMPMessage.build((IRTMPEvent) packet, packet.getTimestamp());

	try {

		limiter--;
		if (limiter > 1) {
			streamer.pushMessage(null, m);
		} else {
			if (streamer != null) {
				stream.removeStreamListener(this);
				streamer.stop();
				streamer = null;
			}
		}

	} catch (IOException e) {

		e.printStackTrace();
	}

}
 
Example #2
Source File: PlayEngine.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Send message to output stream and handle exceptions.
 * 
 * @param message
 *            The message to send.
 */
private void doPushMessage(AbstractMessage message) {
    if (log.isTraceEnabled()) {
        String msgType = message.getMessageType();
        log.trace("doPushMessage: {}", msgType);
    }
    IMessageOutput out = msgOutReference.get();
    if (out != null) {
        try {
            out.pushMessage(message);
            if (message instanceof RTMPMessage) {
                IRTMPEvent body = ((RTMPMessage) message).getBody();
                // update the last message sent's timestamp
                lastMessageTs = body.getTimestamp();
                IoBuffer streamData = null;
                if (body instanceof IStreamData && (streamData = ((IStreamData<?>) body).getData()) != null) {
                    bytesSent.addAndGet(streamData.limit());
                }
            }
        } catch (IOException err) {
            log.warn("Error while pushing message", err);
        }
    } else {
        log.warn("Push message failed due to null output pipe");
    }
}
 
Example #3
Source File: PlayEngine.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Send reset message
 */
private void sendReset() {
    if (pullMode) {
        Ping recorded = new Ping();
        recorded.setEventType(Ping.RECORDED_STREAM);
        recorded.setValue2(streamId);
        // recorded 
        RTMPMessage recordedMsg = RTMPMessage.build(recorded);
        doPushMessage(recordedMsg);
    }
    Ping begin = new Ping();
    begin.setEventType(Ping.STREAM_BEGIN);
    begin.setValue2(streamId);
    // begin 
    RTMPMessage beginMsg = RTMPMessage.build(begin);
    doPushMessage(beginMsg);
    // reset
    ResetMessage reset = new ResetMessage();
    doPushMessage(reset);
}
 
Example #4
Source File: PlayEngine.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Sends an onPlayStatus message.
 * 
 * http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/NetDataEvent.html
 * 
 * @param code
 * @param duration
 * @param bytes
 */
private void sendOnPlayStatus(String code, int duration, long bytes) {
    if (log.isDebugEnabled()) {
        log.debug("Sending onPlayStatus - code: {} duration: {} bytes: {}", code, duration, bytes);
    }
    // create the buffer
    IoBuffer buf = IoBuffer.allocate(102);
    buf.setAutoExpand(true);
    Output out = new Output(buf);
    out.writeString("onPlayStatus");
    ObjectMap<Object, Object> args = new ObjectMap<>();
    args.put("code", code);
    args.put("level", Status.STATUS);
    args.put("duration", duration);
    args.put("bytes", bytes);
    String name = currentItem.get().getName();
    if (StatusCodes.NS_PLAY_TRANSITION_COMPLETE.equals(code)) {
        args.put("clientId", streamId);
        args.put("details", name);
        args.put("description", String.format("Transitioned to %s", name));
        args.put("isFastPlay", false);
    }
    out.writeObject(args);
    buf.flip();
    Notify event = new Notify(buf, "onPlayStatus");
    if (lastMessageTs > 0) {
        event.setTimestamp(lastMessageTs);
    } else {
        event.setTimestamp(0);
    }
    RTMPMessage msg = RTMPMessage.build(event);
    doPushMessage(msg);
}
 
Example #5
Source File: StreamRelay.java    From red5-client with Apache License 2.0 5 votes vote down vote up
@Override
public void dispatchEvent(IEvent event) {
    System.out.println("ClientStream.dispachEvent()" + event.toString());
    try {
        proxy.pushMessage(null, RTMPMessage.build((IRTMPEvent) event));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
 
Example #6
Source File: StreamingProxy.java    From red5-client with Apache License 2.0 5 votes vote down vote up
@Override
public void pushMessage(IPipe pipe, IMessage message) throws IOException {
    if (isPublished() && message instanceof RTMPMessage) {
        RTMPMessage rtmpMsg = (RTMPMessage) message;
        rtmpClient.publishStreamData(streamId, rtmpMsg);
    } else {
        log.trace("Adding message to buffer. Current size: {}", frameBuffer.size());
        frameBuffer.add(message);
    }
}
 
Example #7
Source File: TestConnectionConsumer.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testPositiveTimestampsAreUnaffected() {
    log.debug("\n testPositiveTimestampsAreUnaffected");
    VideoData videoData1 = new VideoData();
    videoData1.setTimestamp(0);
    underTest.pushMessage(null, RTMPMessage.build(videoData1));

    assertEquals(0, videoData1.getTimestamp());

    VideoData videoData2 = new VideoData();
    videoData2.setTimestamp(Integer.MAX_VALUE);
    underTest.pushMessage(null, RTMPMessage.build(videoData2));

    assertEquals(Integer.MAX_VALUE, videoData2.getTimestamp());
}
 
Example #8
Source File: TestConnectionConsumer.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
@Test
public void testNegativeTimestampsAreRolledOver() {
    log.debug("\n testNegativeTimestampsAreRolledOver");
    VideoData videoData1 = new VideoData();
    videoData1.setTimestamp(-1);
    underTest.pushMessage(null, RTMPMessage.build(videoData1));

    assertEquals(Integer.MAX_VALUE, videoData1.getTimestamp());

    VideoData videoData2 = new VideoData();
    videoData2.setTimestamp(Integer.MIN_VALUE);
    underTest.pushMessage(null, RTMPMessage.build(videoData2));

    assertEquals(0, videoData2.getTimestamp());
}
 
Example #9
Source File: PlayEngine.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Send clear ping. Lets client know that stream has no more data to send.
 */
private void sendClearPing() {
    Ping eof = new Ping();
    eof.setEventType(Ping.STREAM_PLAYBUFFER_CLEAR);
    eof.setValue2(streamId);
    // eos 
    RTMPMessage eofMsg = RTMPMessage.build(eof);
    doPushMessage(eofMsg);
}
 
Example #10
Source File: PlayEngine.java    From red5-server-common with Apache License 2.0 5 votes vote down vote up
/**
 * Performs the processes needed for VOD / pre-recorded streams.
 * 
 * @param withReset
 *            whether or not to perform reset on the stream
 * @param itemLength
 *            length of the item to be played
 * @return message for the consumer
 * @throws IOException
 */
private final IMessage playVOD(boolean withReset, long itemLength) throws IOException {
    IMessage msg = null;
    // change state
    subscriberStream.setState(StreamState.PLAYING);
    if (withReset) {
        releasePendingMessage();
    }
    sendVODInitCM(currentItem.get());
    // Don't use pullAndPush to detect IOExceptions prior to sending NetStream.Play.Start
    int start = (int) currentItem.get().getStart();
    if (start > 0) {
        streamOffset = sendVODSeekCM(start);
        // We seeked to the nearest keyframe so use real timestamp now
        if (streamOffset == -1) {
            streamOffset = start;
        }
    }
    IMessageInput in = msgInReference.get();
    msg = in.pullMessage();
    if (msg instanceof RTMPMessage) {
        // Only send first video frame
        IRTMPEvent body = ((RTMPMessage) msg).getBody();
        if (itemLength == 0) {
            while (body != null && !(body instanceof VideoData)) {
                msg = in.pullMessage();
                if (msg != null && msg instanceof RTMPMessage) {
                    body = ((RTMPMessage) msg).getBody();
                } else {
                    break;
                }
            }
        }
        if (body != null) {
            // Adjust timestamp when playing lists
            body.setTimestamp(body.getTimestamp() + timestampOffset);
        }
    }
    return msg;
}
 
Example #11
Source File: SlicedFileConsumer.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/**
 * Push message through pipe
 * 
 * @param pipe
 *            Pipe
 * @param message
 *            Message to push
 * @throws IOException
 *             if message could not be written
 */
@SuppressWarnings("rawtypes")
public void pushMessage(IPipe pipe, IMessage message) throws IOException {
    if (message instanceof RTMPMessage) {
        final IRTMPEvent msg = ((RTMPMessage) message).getBody();
        // get the type
        byte dataType = msg.getDataType();
        // get the timestamp
        int timestamp = msg.getTimestamp();
        log.trace("Data type: {} timestamp: {}", dataType, timestamp);
        // if writes are delayed, queue the data and sort it by time
        if (queue == null) {
            // if we plan to use a queue, create one
            queue = new PriorityQueue<QueuedMediaData>(queueThreshold <= 0 ? 11 : queueThreshold);
        }
        QueuedMediaData queued = null;
        if (msg instanceof IStreamData) {
            if (log.isTraceEnabled()) {
                log.trace("Stream data, body saved. Data type: {} class type: {}", dataType, msg.getClass().getName());
            }
            // ensure that our first video frame written is a key frame
            if (msg instanceof VideoData) {
                log.debug("pushMessage video - waitForVideoKeyframe: {} gotVideoKeyframe: {}", waitForVideoKeyframe, gotVideoKeyframe);
                if (!gotVideoKeyframe) {
                    VideoData video = (VideoData) msg;
                    if (video.getFrameType() == FrameType.KEYFRAME) {
                        log.debug("Got our first keyframe");
                        gotVideoKeyframe = true;
                    }
                    if (waitForVideoKeyframe && !gotVideoKeyframe) {
                        // skip this frame bail out
                        log.debug("Skipping video data since keyframe has not been written yet");
                        return;
                    }
                }
            }
            queued = new QueuedMediaData(timestamp, dataType, (IStreamData) msg);
        } else {
            // XXX what type of message are we saving that has no body data??
            if (log.isTraceEnabled()) {
                log.trace("Non-stream data, body not saved. Data type: {} class type: {}", dataType, msg.getClass().getName());
            }
            queued = new QueuedMediaData(timestamp, dataType);
        }
        if (queued != null) {
            writeLock.lock();
            try {
                // add to the queue
                queue.add(queued);
            } finally {
                writeLock.unlock();
            }
        }
        int queueSize = 0;
        readLock.lock();
        try {
            queueSize = queue.size();
        } finally {
            readLock.unlock();
        }
        // initialize a writer
        if (writer == null) {
            init();
            if (msg instanceof VideoData) {
                writeQueuedDataSlice(createTimestampLimitedSlice(msg.getTimestamp()));
            } else if (queueThreshold >= 0 && queueSize >= queueThreshold) {
                writeQueuedDataSlice(createFixedLengthSlice(queueThreshold / (100 / percentage)));
            }
        }
    } else if (message instanceof ResetMessage) {
        startTimestamp = -1;
    } else if (log.isDebugEnabled()) {
        log.debug("Ignoring pushed message: {}", message);
    }
}
 
Example #12
Source File: VideoFrameDropper.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public void dropPacket(RTMPMessage message) {
    IRTMPEvent packet = message.getBody();
    // Only check video packets.
    if (packet instanceof VideoData) {
        VideoData video = (VideoData) packet;
        FrameType type = video.getFrameType();
        switch (state) {
            case SEND_ALL:
                if (type == FrameType.DISPOSABLE_INTERFRAME) {
                    // Remain in state, packet is safe to drop.
                    return;
                } else if (type == FrameType.INTERFRAME) {
                    // Drop all frames until the next keyframe.
                    state = SEND_KEYFRAMES;
                    return;
                } else if (type == FrameType.KEYFRAME) {
                    // Drop all frames until the next keyframe.
                    state = SEND_KEYFRAMES;
                    return;
                }
                break;
            case SEND_INTERFRAMES:
                if (type == FrameType.INTERFRAME) {
                    // Drop all frames until the next keyframe.
                    state = SEND_KEYFRAMES_CHECK;
                    return;
                } else if (type == FrameType.KEYFRAME) {
                    // Drop all frames until the next keyframe.
                    state = SEND_KEYFRAMES;
                    return;
                }
                break;
            case SEND_KEYFRAMES:
                // Remain in state.
                break;
            case SEND_KEYFRAMES_CHECK:
                if (type == FrameType.KEYFRAME) {
                    // Switch back to sending keyframes, but don't move to
                    // SEND_INTERFRAMES afterwards.
                    state = SEND_KEYFRAMES;
                    return;
                }
                break;
            default:
        }
    }
}
 
Example #13
Source File: VideoFrameDropper.java    From red5-server-common with Apache License 2.0 4 votes vote down vote up
/** {@inheritDoc} */
public boolean canSendPacket(RTMPMessage message, long pending) {
    IRTMPEvent packet = message.getBody();
    boolean result = true;
    // We currently only drop video packets.
    if (packet instanceof VideoData) {
        VideoData video = (VideoData) packet;
        FrameType type = video.getFrameType();
        switch (state) {
            case SEND_ALL:
                // All packets will be sent
                break;
            case SEND_INTERFRAMES:
                // Only keyframes and interframes will be sent.
                if (type == FrameType.KEYFRAME) {
                    if (pending == 0) {
                        // Send all frames from now on.
                        state = SEND_ALL;
                    }
                } else if (type == FrameType.INTERFRAME) {
                }
                break;
            case SEND_KEYFRAMES:
                // Only keyframes will be sent.
                result = (type == FrameType.KEYFRAME);
                if (result && pending == 0) {
                    // Maybe switch back to SEND_INTERFRAMES after the next keyframe
                    state = SEND_KEYFRAMES_CHECK;
                }
                break;
            case SEND_KEYFRAMES_CHECK:
                // Only keyframes will be sent.
                result = (type == FrameType.KEYFRAME);
                if (result && pending == 0) {
                    // Continue with sending interframes as well
                    state = SEND_INTERFRAMES;
                }
                break;
            default:
        }
    }
    return result;
}
 
Example #14
Source File: IFrameDropper.java    From red5-server-common with Apache License 2.0 2 votes vote down vote up
/**
 * Checks if a message may be sent to the subscriber.
 * 
 * @param message
 *            the message to check
 * @param pending
 *            the number of pending messages
 * @return <pre>
 * true
 * </pre>
 * 
 *         if the packet may be sent, otherwise
 * 
 *         <pre>
 * false
 * </pre>
 */
boolean canSendPacket(RTMPMessage message, long pending);
 
Example #15
Source File: IFrameDropper.java    From red5-server-common with Apache License 2.0 2 votes vote down vote up
/**
 * Notify that a packet has been dropped.
 * 
 * @param message
 *            the message that was dropped
 */
void dropPacket(RTMPMessage message);
 
Example #16
Source File: IFrameDropper.java    From red5-server-common with Apache License 2.0 2 votes vote down vote up
/**
 * Notify that a message has been sent.
 * 
 * @param message
 *            the message that was sent
 */
void sendPacket(RTMPMessage message);
 
Example #17
Source File: VideoFrameDropper.java    From red5-server-common with Apache License 2.0 2 votes vote down vote up
/** {@inheritDoc} */
public void sendPacket(RTMPMessage message) {

}