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 class PlayEngine
extends Object
implements IFilter, IPushableConsumer, IPipeConnectionListener
A play engine for playing a IPlayItem.
- Author:
- The Red5 Project, Steven Gong, Paul Gregoire ([email protected]), Dan Rossi, Tiago Daniel Jacobs ([email protected]), Vladimir Hmelyoff ([email protected])
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classBuilder patternprivate classprivate final classPeriodically triggered by executor to send messages to the client.private final classRunnable worker to handle seek operations. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate intInterval in ms to check for buffer underruns in VOD streams.private intIndex of the buffered interframe to send instead of current frameprivate AtomicLongNumber of bytes sent.private IConsumerServiceprivate IPlayItemprivate StringFlag denoting whether or not the job that closes stream after buffer runs out is scheduled.private longprivate longprivate longprivate intTimestamp of the last message sent to the client.private static final org.slf4j.Loggerprivate intthreshold for number of pending video framesprivate intif 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 longTimestamp when buffer should be checked for underruns next.private intthe number of sequential video frames with > 0 pending framesprivate RTMPMessageprivate ConcurrentLinkedQueue<Runnable>List of pending operationsprivate longStart time of stream playback.private intDecision: 0 for Live, 1 for File, 2 for Wait, 3 for N/Aprivate IProviderServiceprivate StringFlag denoting whether or not the push and pull job is scheduled.private booleanprivate final AtomicBooleanMonitor guarding completion of a given push/pull run.private booleanReceive audio?private booleanReceive video?private ISchedulingServiceprivate booleanSend blank audio packet next?private Numberprivate intOffset in milliseconds where the stream started.private AtomicIntegertimestamp of first sent packetprivate final ISubscriberStreamprivate intprivate intNumber of pending messages at which aprivate IFrameDropperState machine for video frame dropping in live streamsprivate StringFields inherited from interface org.red5.server.messaging.IPushableConsumer
KEY -
Constructor Summary
ConstructorsModifierConstructorDescriptionprivatePlayEngine(PlayEngine.Builder builder) Constructs a new PlayEngine. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidprotected booleancheckSendMessageEnabled(RTMPMessage message) Check if sending the given message was enabled by the client.private voidClear all scheduled waiting jobsvoidclose()Close streamprivate final voidconnectToProvider(String itemName) Connects to the data provider.private voiddoPushMessage(AbstractMessage message) Send message to output stream and handle exceptions.private voiddoPushMessage(Status status) Sends a status message.private voidMake sure the pull and push processing is running.intReturns the timestamp of the last message sent.longprivate booleanprivate booleanisClientBufferFull(long now) Estimate client buffer fill.booleanisPaused()booleanprivate booleanokayToSendMessage(IRTMPEvent message) Check if it's okay to send the client more data.voidonOOBControlMessage(IMessageComponent source, IPipe pipe, OOBControlMessage oobCtrlMsg) voidPipe connection event handlervoidpause(int position) Pause at positionprivate longGet number of pending messages to be sentprivate longGet number of pending video messagesvoidPlay streamvoidPlay stream See: https://www.adobe.com/devnet/adobe-media-server/articles/dynstream_actionscript.htmlprivate final voidplayLive()Performs the processes needed for live streams.private final IMessageplayVOD(boolean withReset, long itemLength) Performs the processes needed for VOD / pre-recorded streams.voidpushMessage(IPipe pipe, IMessage message) Pushes message through pipebooleanReturns true if the engine currently receives audio.booleanreceiveAudio(boolean receive) Returns true if the engine currently receives audio and sets the new value.booleanReturns true if the engine currently receives video.booleanreceiveVideo(boolean receive) Returns true if the engine currently receives video and sets the new value.private voidReleases pending message body, nullifies pending message objectvoidresume(int position) Resume playbackprivate voidSchedule a stop to be run from a separate thread to allow the background thread to stop cleanly.voidseek(int position) Seek to a given positionvoidsendBlankAudio(boolean sendBlankAudio) private booleanSend VOD check video control messageprivate voidSend clear ping.private voidSend playlist complete status notificationprivate voidInsufficient bandwidth notificationprivate voidsendMessage(RTMPMessage messageIn) Send RTMP messageprivate voidsendOnPlayStatus(String code, int duration, long bytes) Sends an onPlayStatus message.private voidsendPauseStatus(IPlayItem item) Send pause status notificationprivate voidsendPublishedStatus(IPlayItem item) Send published status notificationprivate voidSend reset messageprivate voidsendResetStatus(IPlayItem item) Send reset status for itemprivate voidsendResumeStatus(IPlayItem item) Send resume status notificationprivate voidsendSeekStatus(IPlayItem item, int position) Send seek status notificationprivate voidsendStartStatus(IPlayItem item) Send playback start status notificationprivate voidsendStopStatus(IPlayItem item) Send playback stoppage status notificationprivate voidStream not found status notificationprivate voidSend playlist switch status notificationprivate voidSend transition status notificationprivate voidSend unpublished status notificationprivate voidsendVODInitCM(IPlayItem item) Send VOD init control messageprivate intsendVODSeekCM(int position) Send VOD seek control messagevoidsetBufferCheckInterval(int bufferCheckInterval) voidsetMaxPendingVideoFrames(int maxPendingVideoFrames) voidsetMaxSequentialPendingVideoFrames(int maxSequentialPendingVideoFrames) (package private) voidsetMessageOut(IMessageOutput msgOut) voidsetUnderrunTrigger(int underrunTrigger) private booleanvoidstart()Start streamvoidstop()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:
onOOBControlMessagein interfaceIMessageComponent- Parameters:
source- Message component sourcepipe- Connection pipeoobCtrlMsg- Out-of-band control message
-
onPipeConnectionEvent
Pipe connection event handler- Specified by:
onPipeConnectionEventin interfaceIPipeConnectionListener- Parameters:
event- Pipe connection event
-
shouldLogPacketDrop
private boolean shouldLogPacketDrop() -
pushMessage
Pushes message through pipe- Specified by:
pushMessagein 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
-
getSubscriberStream
-