Package org.red5.server.stream
Class PlayEngine
java.lang.Object
org.red5.server.stream.PlayEngine
- All Implemented Interfaces:
IConsumer
,IFilter
,IMessageComponent
,IPipeConnectionListener
,IProvider
,IPushableConsumer
public final class PlayEngine
extends Object
implements IFilter, IPushableConsumer, IPipeConnectionListener
A play engine for playing a IPlayItem.
- Author:
- The Red5 Project, Steven Gong, Paul Gregoire (mondain@gmail.com), Dan Rossi, Tiago Daniel Jacobs (tiago@imdt.com.br), Vladimir Hmelyoff (vlhm@splitmedialabs.com)
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Builder patternprivate class
private final class
Periodically triggered by executor to send messages to the client.private final class
Runnable worker to handle seek operations. -
Field Summary
Modifier and TypeFieldDescriptionprivate int
Interval in ms to check for buffer underruns in VOD streams.private int
Index of the buffered interframe to send instead of current frameprivate AtomicLong
Number of bytes sent.private IConsumerService
private IPlayItem
private String
Flag denoting whether or not the job that closes stream after buffer runs out is scheduled.private long
private long
private long
private int
Timestamp of the last message sent to the client.private static final org.slf4j.Logger
private int
threshold for number of pending video framesprivate int
if we have more than 1 pending video frames, but less than maxPendingVideoFrames, continue sending until there are this many sequential frames with more than 1 pendingprivate final AtomicReference<IMessageInput>
private final AtomicReference<IMessageOutput>
private long
Timestamp when buffer should be checked for underruns next.private int
the number of sequential video frames with > 0 pending framesprivate RTMPMessage
private ConcurrentLinkedQueue<Runnable>
List of pending operationsprivate long
Start time of stream playback.private int
Decision: 0 for Live, 1 for File, 2 for Wait, 3 for N/Aprivate IProviderService
private String
Flag denoting whether or not the push and pull job is scheduled.private boolean
private final AtomicBoolean
Monitor guarding completion of a given push/pull run.private boolean
Receive audio?private boolean
Receive video?private ISchedulingService
private boolean
Send blank audio packet next?private Number
private int
Offset in milliseconds where the stream started.private AtomicInteger
timestamp of first sent packetprivate final ISubscriberStream
private int
private int
Number of pending messages at which aprivate IFrameDropper
State machine for video frame dropping in live streamsprivate String
Fields inherited from interface org.red5.server.messaging.IPushableConsumer
KEY
-
Constructor Summary
ModifierConstructorDescriptionprivate
PlayEngine
(PlayEngine.Builder builder) Constructs a new PlayEngine. -
Method Summary
Modifier and TypeMethodDescriptionprivate void
protected boolean
checkSendMessageEnabled
(RTMPMessage message) Check if sending the given message was enabled by the client.private void
Clear all scheduled waiting jobsvoid
close()
Close streamprivate final void
connectToProvider
(String itemName) Connects to the data provider.private void
doPushMessage
(AbstractMessage message) Send message to output stream and handle exceptions.private void
doPushMessage
(Status status) Sends a status message.private void
Make sure the pull and push processing is running.int
Returns the timestamp of the last message sent.long
private boolean
private boolean
isClientBufferFull
(long now) Estimate client buffer fill.boolean
isPaused()
boolean
private boolean
okayToSendMessage
(IRTMPEvent message) Check if it's okay to send the client more data.void
onOOBControlMessage
(IMessageComponent source, IPipe pipe, OOBControlMessage oobCtrlMsg) void
Pipe connection event handlervoid
pause
(int position) Pause at positionprivate long
Get number of pending messages to be sentprivate long
Get number of pending video messagesvoid
Play streamvoid
Play stream See: https://www.adobe.com/devnet/adobe-media-server/articles/dynstream_actionscript.htmlprivate final void
playLive()
Performs the processes needed for live streams.private final IMessage
playVOD
(boolean withReset, long itemLength) Performs the processes needed for VOD / pre-recorded streams.void
pushMessage
(IPipe pipe, IMessage message) Pushes message through pipeboolean
Returns true if the engine currently receives audio.boolean
receiveAudio
(boolean receive) Returns true if the engine currently receives audio and sets the new value.boolean
Returns true if the engine currently receives video.boolean
receiveVideo
(boolean receive) Returns true if the engine currently receives video and sets the new value.private void
Releases pending message body, nullifies pending message objectvoid
resume
(int position) Resume playbackprivate void
Schedule a stop to be run from a separate thread to allow the background thread to stop cleanly.void
seek
(int position) Seek to a given positionvoid
sendBlankAudio
(boolean sendBlankAudio) private boolean
Send VOD check video control messageprivate void
Send clear ping.private void
Send playlist complete status notificationprivate void
Insufficient bandwidth notificationprivate void
sendMessage
(RTMPMessage messageIn) Send RTMP messageprivate void
sendOnPlayStatus
(String code, int duration, long bytes) Sends an onPlayStatus message.private void
sendPauseStatus
(IPlayItem item) Send pause status notificationprivate void
sendPublishedStatus
(IPlayItem item) Send published status notificationprivate void
Send reset messageprivate void
sendResetStatus
(IPlayItem item) Send reset status for itemprivate void
sendResumeStatus
(IPlayItem item) Send resume status notificationprivate void
sendSeekStatus
(IPlayItem item, int position) Send seek status notificationprivate void
sendStartStatus
(IPlayItem item) Send playback start status notificationprivate void
sendStopStatus
(IPlayItem item) Send playback stoppage status notificationprivate void
Stream not found status notificationprivate void
Send playlist switch status notificationprivate void
Send transition status notificationprivate void
Send unpublished status notificationprivate void
sendVODInitCM
(IPlayItem item) Send VOD init control messageprivate int
sendVODSeekCM
(int position) Send VOD seek control messagevoid
setBufferCheckInterval
(int bufferCheckInterval) void
setMaxPendingVideoFrames
(int maxPendingVideoFrames) void
setMaxSequentialPendingVideoFrames
(int maxSequentialPendingVideoFrames) (package private) void
setMessageOut
(IMessageOutput msgOut) void
setUnderrunTrigger
(int underrunTrigger) private boolean
void
start()
Start streamvoid
stop()
Stop playback
-
Field Details
-
log
private static final org.slf4j.Logger log -
msgInReference
-
msgOutReference
-
subscriberStream
-
schedulingService
-
consumerService
-
providerService
-
streamId
-
receiveVideo
private boolean receiveVideoReceive video? -
receiveAudio
private boolean receiveAudioReceive audio? -
pullMode
private boolean pullMode -
waitLiveJob
-
streamStartTS
timestamp of first sent packet -
currentItem
-
pendingMessage
-
bufferCheckInterval
private int bufferCheckIntervalInterval in ms to check for buffer underruns in VOD streams. -
underrunTrigger
private int underrunTriggerNumber of pending messages at which aNetStream.Play.InsufficientBW
message is generated for VOD streams. -
maxPendingVideoFramesThreshold
private int maxPendingVideoFramesThresholdthreshold for number of pending video frames -
maxSequentialPendingVideoFrames
private int maxSequentialPendingVideoFramesif we have more than 1 pending video frames, but less than maxPendingVideoFrames, continue sending until there are this many sequential frames with more than 1 pending -
numSequentialPendingVideoFrames
private int numSequentialPendingVideoFramesthe number of sequential video frames with > 0 pending frames -
videoFrameDropper
State machine for video frame dropping in live streams -
timestampOffset
private int timestampOffset -
lastMessageTs
private int lastMessageTsTimestamp of the last message sent to the client. -
bytesSent
Number of bytes sent. -
playbackStart
private volatile long playbackStartStart time of stream playback. It's not a time when the stream is being played but the time when the stream should be played if it's played from the very beginning. Eg. A stream is played at timestamp 5s on 1:00:05. The playbackStart is 1:00:00. -
pullAndPush
Flag denoting whether or not the push and pull job is scheduled. The job makes sure messages are sent to the client. -
deferredStop
Flag denoting whether or not the job that closes stream after buffer runs out is scheduled. -
pushPullRunning
Monitor guarding completion of a given push/pull run. Used to wait for job cancellation to finish. -
streamOffset
private int streamOffsetOffset in milliseconds where the stream started. -
nextCheckBufferUnderrun
private long nextCheckBufferUnderrunTimestamp when buffer should be checked for underruns next. -
sendBlankAudio
private boolean sendBlankAudioSend blank audio packet next? -
playDecision
private int playDecisionDecision: 0 for Live, 1 for File, 2 for Wait, 3 for N/A -
bufferedInterframeIdx
private int bufferedInterframeIdxIndex of the buffered interframe to send instead of current frame -
pendingOperations
List of pending operations -
droppedPacketsCount
private long droppedPacketsCount -
droppedPacketsCountLastLogTimestamp
private long droppedPacketsCountLastLogTimestamp -
droppedPacketsCountLogInterval
private long droppedPacketsCountLogInterval
-
-
Constructor Details
-
PlayEngine
Constructs a new PlayEngine.
-
-
Method Details
-
setBufferCheckInterval
public void setBufferCheckInterval(int bufferCheckInterval) -
setUnderrunTrigger
public void setUnderrunTrigger(int underrunTrigger) -
setMessageOut
-
start
public void start()Start stream -
play
Play stream- Parameters:
item
- Playlist item- Throws:
StreamNotFoundException
- Stream not foundIllegalStateException
- Stream is in stopped stateIOException
- Stream had io exception
-
play
public void play(IPlayItem item, boolean withReset) throws StreamNotFoundException, IllegalStateException, IOException Play stream See: https://www.adobe.com/devnet/adobe-media-server/articles/dynstream_actionscript.html- Parameters:
item
- Playlist itemwithReset
- Send reset status before playing.- Throws:
StreamNotFoundException
- Stream not foundIllegalStateException
- Stream is in stopped stateIOException
- Stream had IO exception
-
playLive
Performs the processes needed for live streams. The following items are sent if they exist:- Metadata
- Decoder configurations (ie. AVC codec)
- Most recent keyframe
- Throws:
IOException
-
playVOD
Performs the processes needed for VOD / pre-recorded streams.- Parameters:
withReset
- whether or not to perform reset on the streamitemLength
- length of the item to be played- Returns:
- message for the consumer
- Throws:
IOException
-
connectToProvider
Connects to the data provider.- Parameters:
itemName
- name of the item to play
-
pause
Pause at position- Parameters:
position
- Position in file- Throws:
IllegalStateException
- If stream is stopped
-
resume
Resume playback- Parameters:
position
- Resumes playback- Throws:
IllegalStateException
- If stream is stopped
-
seek
Seek to a given position- Parameters:
position
- Position- Throws:
IllegalStateException
- If stream is in stopped stateOperationNotSupportedException
- If this object doesn't support the operation.
-
stop
Stop playback- Throws:
IllegalStateException
- If stream is in stopped state
-
close
public void close()Close stream -
okayToSendMessage
Check if it's okay to send the client more data. This takes the configured bandwidth as well as the requested client buffer into account.- Parameters:
message
-- Returns:
- true if it is ok to send more, false otherwise
-
isClientBufferFull
private boolean isClientBufferFull(long now) Estimate client buffer fill.- Parameters:
now
- The current timestamp being used.- Returns:
- True if it appears that the client buffer is full, otherwise false.
-
isClientBufferEmpty
private boolean isClientBufferEmpty() -
ensurePullAndPushRunning
private void ensurePullAndPushRunning()Make sure the pull and push processing is running. -
clearWaitJobs
private void clearWaitJobs()Clear all scheduled waiting jobs -
doPushMessage
Sends a status message.- Parameters:
status
-
-
doPushMessage
Send message to output stream and handle exceptions.- Parameters:
message
- The message to send.
-
sendMessage
Send RTMP message- Parameters:
messageIn
- RTMP message
-
sendClearPing
private void sendClearPing()Send clear ping. Lets client know that stream has no more data to send. -
sendReset
private void sendReset()Send reset message -
sendResetStatus
Send reset status for item- Parameters:
item
- Playlist item
-
sendStartStatus
Send playback start status notification- Parameters:
item
- Playlist item
-
sendStopStatus
Send playback stoppage status notification- Parameters:
item
- Playlist item
-
sendOnPlayStatus
Sends an onPlayStatus message. http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/events/NetDataEvent.html- Parameters:
code
-duration
-bytes
-
-
sendSwitchStatus
private void sendSwitchStatus()Send playlist switch status notification -
sendTransitionStatus
private void sendTransitionStatus()Send transition status notification -
sendCompleteStatus
private void sendCompleteStatus()Send playlist complete status notification -
sendSeekStatus
Send seek status notification- Parameters:
item
- Playlist itemposition
- Seek position
-
sendPauseStatus
Send pause status notification- Parameters:
item
- Playlist item
-
sendResumeStatus
Send resume status notification- Parameters:
item
- Playlist item
-
sendPublishedStatus
Send published status notification- Parameters:
item
- Playlist item
-
sendUnpublishedStatus
Send unpublished status notification- Parameters:
item
- Playlist item
-
sendStreamNotFoundStatus
Stream not found status notification- Parameters:
item
- Playlist item
-
sendInsufficientBandwidthStatus
Insufficient bandwidth notification- Parameters:
item
- Playlist item
-
sendVODInitCM
Send VOD init control message- Parameters:
item
- Playlist item
-
sendVODSeekCM
private int sendVODSeekCM(int position) Send VOD seek control message- Parameters:
msgIn
- Message inputposition
- Playlist item- Returns:
- Out-of-band control message call result or -1 on failure
-
sendCheckVideoCM
private boolean sendCheckVideoCM()Send VOD check video control message- Returns:
- result of oob control message
-
onOOBControlMessage
- Specified by:
onOOBControlMessage
in interfaceIMessageComponent
- Parameters:
source
- Message component sourcepipe
- Connection pipeoobCtrlMsg
- Out-of-band control message
-
onPipeConnectionEvent
Pipe connection event handler- Specified by:
onPipeConnectionEvent
in interfaceIPipeConnectionListener
- Parameters:
event
- Pipe connection event
-
shouldLogPacketDrop
private boolean shouldLogPacketDrop() -
pushMessage
Pushes message through pipe- Specified by:
pushMessage
in interfaceIPushableConsumer
- Parameters:
pipe
- Pipemessage
- Message- Throws:
IOException
- if message could not be written
-
pendingVideoMessages
private long pendingVideoMessages()Get number of pending video messages- Returns:
- Number of pending video messages
-
pendingMessages
private long pendingMessages()Get number of pending messages to be sent- Returns:
- Number of pending messages
-
isPullMode
public boolean isPullMode() -
isPaused
public boolean isPaused() -
getLastMessageTimestamp
public int getLastMessageTimestamp()Returns the timestamp of the last message sent.- Returns:
- last message timestamp
-
getPlaybackStart
public long getPlaybackStart() -
sendBlankAudio
public void sendBlankAudio(boolean sendBlankAudio) -
receiveAudio
public boolean receiveAudio()Returns true if the engine currently receives audio.- Returns:
- receive audio
-
receiveAudio
public boolean receiveAudio(boolean receive) Returns true if the engine currently receives audio and sets the new value.- Parameters:
receive
- new value- Returns:
- old value
-
receiveVideo
public boolean receiveVideo()Returns true if the engine currently receives video.- Returns:
- receive video
-
receiveVideo
public boolean receiveVideo(boolean receive) Returns true if the engine currently receives video and sets the new value.- Parameters:
receive
- new value- Returns:
- old value
-
releasePendingMessage
private void releasePendingMessage()Releases pending message body, nullifies pending message object -
checkSendMessageEnabled
Check if sending the given message was enabled by the client.- Parameters:
message
- the message to check- Returns:
- true if the message should be sent, false otherwise (and the message is discarded)
-
runDeferredStop
private void runDeferredStop()Schedule a stop to be run from a separate thread to allow the background thread to stop cleanly. -
cancelDeferredStop
private void cancelDeferredStop() -
setMaxPendingVideoFrames
public void setMaxPendingVideoFrames(int maxPendingVideoFrames) - Parameters:
maxPendingVideoFrames
- the maxPendingVideoFrames to set
-
setMaxSequentialPendingVideoFrames
public void setMaxSequentialPendingVideoFrames(int maxSequentialPendingVideoFrames) - Parameters:
maxSequentialPendingVideoFrames
- the maxSequentialPendingVideoFrames to set
-