Java Code Examples for org.red5.server.net.rtmp.event.IRTMPEvent#getTimestamp()

The following examples show how to use org.red5.server.net.rtmp.event.IRTMPEvent#getTimestamp() . 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: 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 2
Source File: Channel.java    From red5-server-common with Apache License 2.0 6 votes vote down vote up
/**
 * Writes packet from event data to RTMP connection and stream id.
 *
 * @param event
 *            Event data
 * @param streamId
 *            Stream id
 */
private void write(IRTMPEvent event, Number streamId) {
    log.trace("write to stream id: {} channel: {}", streamId, id);
    final Header header = new Header();
    final Packet packet = new Packet(header, event);
    // set the channel id
    header.setChannelId(id);
    int ts = event.getTimestamp();
    if (ts != 0) {
        header.setTimer(event.getTimestamp());
    }
    header.setStreamId(streamId);
    header.setDataType(event.getDataType());
    // should use RTMPConnection specific method.. 
    //log.trace("Connection type for write: {}", connection.getClass().getName());
    connection.write(packet);
}
 
Example 3
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);
    }
}