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)
  • Field Details

    • log

      private static final org.slf4j.Logger log
    • msgInReference

      private final AtomicReference<IMessageInput> msgInReference
    • msgOutReference

      private final AtomicReference<IMessageOutput> msgOutReference
    • subscriberStream

      private final ISubscriberStream subscriberStream
    • schedulingService

      private ISchedulingService schedulingService
    • consumerService

      private IConsumerService consumerService
    • providerService

      private IProviderService providerService
    • streamId

      private Number streamId
    • receiveVideo

      private boolean receiveVideo
      Receive video?
    • receiveAudio

      private boolean receiveAudio
      Receive audio?
    • pullMode

      private boolean pullMode
    • waitLiveJob

      private String waitLiveJob
    • streamStartTS

      private AtomicInteger streamStartTS
      timestamp of first sent packet
    • currentItem

      private IPlayItem currentItem
    • pendingMessage

      private RTMPMessage pendingMessage
    • bufferCheckInterval

      private int bufferCheckInterval
      Interval in ms to check for buffer underruns in VOD streams.
    • underrunTrigger

      private int underrunTrigger
      Number of pending messages at which a
       NetStream.Play.InsufficientBW
       
      message is generated for VOD streams.
    • maxPendingVideoFramesThreshold

      private int maxPendingVideoFramesThreshold
      threshold for number of pending video frames
    • maxSequentialPendingVideoFrames

      private int maxSequentialPendingVideoFrames
      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 pending
    • numSequentialPendingVideoFrames

      private int numSequentialPendingVideoFrames
      the number of sequential video frames with > 0 pending frames
    • videoFrameDropper

      private IFrameDropper videoFrameDropper
      State machine for video frame dropping in live streams
    • timestampOffset

      private int timestampOffset
    • lastMessageTs

      private int lastMessageTs
      Timestamp of the last message sent to the client.
    • bytesSent

      private AtomicLong bytesSent
      Number of bytes sent.
    • playbackStart

      private volatile long playbackStart
      Start 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

      private volatile String pullAndPush
      Flag denoting whether or not the push and pull job is scheduled. The job makes sure messages are sent to the client.
    • deferredStop

      private volatile String deferredStop
      Flag denoting whether or not the job that closes stream after buffer runs out is scheduled.
    • pushPullRunning

      private final AtomicBoolean pushPullRunning
      Monitor guarding completion of a given push/pull run. Used to wait for job cancellation to finish.
    • streamOffset

      private int streamOffset
      Offset in milliseconds where the stream started.
    • nextCheckBufferUnderrun

      private long nextCheckBufferUnderrun
      Timestamp when buffer should be checked for underruns next.
    • sendBlankAudio

      private boolean sendBlankAudio
      Send blank audio packet next?
    • playDecision

      private int playDecision
      Decision: 0 for Live, 1 for File, 2 for Wait, 3 for N/A
    • bufferedInterframeIdx

      private int bufferedInterframeIdx
      Index of the buffered interframe to send instead of current frame
    • pendingOperations

      private ConcurrentLinkedQueue<Runnable> pendingOperations
      List of pending operations
    • droppedPacketsCount

      private long droppedPacketsCount
    • droppedPacketsCountLastLogTimestamp

      private long droppedPacketsCountLastLogTimestamp
    • droppedPacketsCountLogInterval

      private long droppedPacketsCountLogInterval
  • Constructor Details

    • PlayEngine

      private PlayEngine(PlayEngine.Builder builder)
      Constructs a new PlayEngine.
  • Method Details

    • setBufferCheckInterval

      public void setBufferCheckInterval(int bufferCheckInterval)
    • setUnderrunTrigger

      public void setUnderrunTrigger(int underrunTrigger)
    • setMessageOut

      void setMessageOut(IMessageOutput msgOut)
    • start

      public void start()
      Start stream
    • play

      Play stream
      Parameters:
      item - Playlist item
      Throws:
      StreamNotFoundException - Stream not found
      IllegalStateException - Stream is in stopped state
      IOException - 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 item
      withReset - Send reset status before playing.
      Throws:
      StreamNotFoundException - Stream not found
      IllegalStateException - Stream is in stopped state
      IOException - Stream had IO exception
    • playLive

      private final void playLive() throws IOException
      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

      private final IMessage playVOD(boolean withReset, long itemLength) throws IOException
      Performs the processes needed for VOD / pre-recorded streams.
      Parameters:
      withReset - whether or not to perform reset on the stream
      itemLength - length of the item to be played
      Returns:
      message for the consumer
      Throws:
      IOException
    • connectToProvider

      private final void connectToProvider(String itemName)
      Connects to the data provider.
      Parameters:
      itemName - name of the item to play
    • pause

      public void pause(int position) throws IllegalStateException
      Pause at position
      Parameters:
      position - Position in file
      Throws:
      IllegalStateException - If stream is stopped
    • resume

      public void resume(int position) throws IllegalStateException
      Resume playback
      Parameters:
      position - Resumes playback
      Throws:
      IllegalStateException - If stream is stopped
    • seek

      public void seek(int position) throws IllegalStateException, OperationNotSupportedException
      Seek to a given position
      Parameters:
      position - Position
      Throws:
      IllegalStateException - If stream is in stopped state
      OperationNotSupportedException - If this object doesn't support the operation.
    • stop

      public void stop() throws IllegalStateException
      Stop playback
      Throws:
      IllegalStateException - If stream is in stopped state
    • close

      public void close()
      Close stream
    • okayToSendMessage

      private boolean okayToSendMessage(IRTMPEvent message)
      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

      private void doPushMessage(Status status)
      Sends a status message.
      Parameters:
      status -
    • doPushMessage

      private void doPushMessage(AbstractMessage message)
      Send message to output stream and handle exceptions.
      Parameters:
      message - The message to send.
    • sendMessage

      private void sendMessage(RTMPMessage messageIn)
      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

      private void sendResetStatus(IPlayItem item)
      Send reset status for item
      Parameters:
      item - Playlist item
    • sendStartStatus

      private void sendStartStatus(IPlayItem item)
      Send playback start status notification
      Parameters:
      item - Playlist item
    • sendStopStatus

      private void sendStopStatus(IPlayItem item)
      Send playback stoppage status notification
      Parameters:
      item - Playlist item
    • sendOnPlayStatus

      private void sendOnPlayStatus(String code, int duration, long bytes)
      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

      private void sendSeekStatus(IPlayItem item, int position)
      Send seek status notification
      Parameters:
      item - Playlist item
      position - Seek position
    • sendPauseStatus

      private void sendPauseStatus(IPlayItem item)
      Send pause status notification
      Parameters:
      item - Playlist item
    • sendResumeStatus

      private void sendResumeStatus(IPlayItem item)
      Send resume status notification
      Parameters:
      item - Playlist item
    • sendPublishedStatus

      private void sendPublishedStatus(IPlayItem item)
      Send published status notification
      Parameters:
      item - Playlist item
    • sendUnpublishedStatus

      private void sendUnpublishedStatus(IPlayItem item)
      Send unpublished status notification
      Parameters:
      item - Playlist item
    • sendStreamNotFoundStatus

      private void sendStreamNotFoundStatus(IPlayItem item)
      Stream not found status notification
      Parameters:
      item - Playlist item
    • sendInsufficientBandwidthStatus

      private void sendInsufficientBandwidthStatus(IPlayItem item)
      Insufficient bandwidth notification
      Parameters:
      item - Playlist item
    • sendVODInitCM

      private void sendVODInitCM(IPlayItem item)
      Send VOD init control message
      Parameters:
      item - Playlist item
    • sendVODSeekCM

      private int sendVODSeekCM(int position)
      Send VOD seek control message
      Parameters:
      msgIn - Message input
      position - 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

      public void onOOBControlMessage(IMessageComponent source, IPipe pipe, OOBControlMessage oobCtrlMsg)
      Specified by:
      onOOBControlMessage in interface IMessageComponent
      Parameters:
      source - Message component source
      pipe - Connection pipe
      oobCtrlMsg - Out-of-band control message
    • onPipeConnectionEvent

      public void onPipeConnectionEvent(PipeConnectionEvent event)
      Pipe connection event handler
      Specified by:
      onPipeConnectionEvent in interface IPipeConnectionListener
      Parameters:
      event - Pipe connection event
    • shouldLogPacketDrop

      private boolean shouldLogPacketDrop()
    • pushMessage

      public void pushMessage(IPipe pipe, IMessage message) throws IOException
      Pushes message through pipe
      Specified by:
      pushMessage in interface IPushableConsumer
      Parameters:
      pipe - Pipe
      message - 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

      protected boolean checkSendMessageEnabled(RTMPMessage message)
      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