Class MultiThreadedApplicationAdapter

All Implemented Interfaces:
IEventHandler, IAttributeStore, ISchedulingService, IScopeAware, IScopeHandler, IScopeService, IBroadcastStreamService, IOnDemandStreamService, IStreamSecurityService, ISubscriberStreamService, IStreamAwareScopeHandler, ApplicationMXBean, AttributeStoreMXBean
Direct Known Subclasses:
AdminApplication, AntMediaApplicationAdapter, ApplicationAdapter

ApplicationAdapter class serves as a base class for your Red5 applications. It provides methods to work with SharedObjects and streams, as well as connections and scheduling services.
ApplicationAdapter is an application level IScope. To handle streaming processes in your application you should implement IStreamAwareScopeHandler interface and implement handling methods.
Application adapter provides you with useful event handlers that can be used to intercept streams, authorize users, etc. Also, all methods added in subclasses can be called from client side with NetConnection.call method. Unlike to Flash Media server which requires you to keep methods on Client object at server side, Red5 offers much more convenient way to add methods
EXAMPLE:
public List<String> getLiveStreams() { // Implementation goes here, say, use Red5 object to obtain scope and all it's streams }
This method added to ApplicationAdapter subclass can be called from client side with the following code:
var nc:NetConnection = new NetConnection(); nc.connect(...); nc.call("getLiveStreams", resultHandlerObj);
If you want to build a server-side framework this is a place to start and wrap it around ApplicationAdapter subclass.
Author:
The Red5 Project, Joachim Bauch (jojo@struktur.de), Paul Gregoire (mondain@gmail.com), Michael Klishin
  • Field Details

    • log

      protected org.slf4j.Logger log
      Logger object
    • logger

      protected static org.slf4j.Logger logger
    • listeners

      private CopyOnWriteArraySet<IApplication> listeners
      List of application listeners.
    • schedulingService

      protected ISchedulingService schedulingService
      Scheduling service. Uses Quartz. Adds and removes scheduled jobs.
    • publishSecurity

      private Set<IStreamPublishSecurity> publishSecurity
      List of handlers that protect stream publishing.
    • playbackSecurity

      private Set<IStreamPlaybackSecurity> playbackSecurity
      List of handlers that protect stream playback.
  • Constructor Details

    • MultiThreadedApplicationAdapter

      public MultiThreadedApplicationAdapter()
  • Method Details

    • addListener

      public void addListener(IApplication listener)
      Register a listener that will get notified about application events.
      Parameters:
      listener - object to register
    • removeListener

      public void removeListener(IApplication listener)
      Unregister handler that will not get notified about application events any longer.
      Parameters:
      listener - object to unregister
    • getListeners

      public Set<IApplication> getListeners()
      Return handlers that get notified about application events.
      Returns:
      list of handlers
    • registerStreamPublishSecurity

      public void registerStreamPublishSecurity(IStreamPublishSecurity handler)
      Add handler that protects stream publishing.
      Specified by:
      registerStreamPublishSecurity in interface IStreamSecurityService
      Parameters:
      handler - Handler to add.
    • unregisterStreamPublishSecurity

      public void unregisterStreamPublishSecurity(IStreamPublishSecurity handler)
      Remove handler that protects stream publishing.
      Specified by:
      unregisterStreamPublishSecurity in interface IStreamSecurityService
      Parameters:
      handler - Handler to remove.
    • getStreamPublishSecurity

      public Set<IStreamPublishSecurity> getStreamPublishSecurity()
      Get handlers that protect stream publishing.
      Specified by:
      getStreamPublishSecurity in interface IStreamSecurityService
      Returns:
      list of handlers
    • registerStreamPlaybackSecurity

      public void registerStreamPlaybackSecurity(IStreamPlaybackSecurity handler)
      Add handler that protects stream playback.
      Specified by:
      registerStreamPlaybackSecurity in interface IStreamSecurityService
      Parameters:
      handler - Handler to add.
    • unregisterStreamPlaybackSecurity

      public void unregisterStreamPlaybackSecurity(IStreamPlaybackSecurity handler)
      Remove handler that protects stream playback.
      Specified by:
      unregisterStreamPlaybackSecurity in interface IStreamSecurityService
      Parameters:
      handler - Handler to remove.
    • getStreamPlaybackSecurity

      public Set<IStreamPlaybackSecurity> getStreamPlaybackSecurity()
      Get handlers that protect stream plaback.
      Specified by:
      getStreamPlaybackSecurity in interface IStreamSecurityService
      Returns:
      list of handlers
    • rejectClient

      protected boolean rejectClient() throws ClientRejectedException
      Reject the currently connecting client without a special error message. This method throws ClientRejectedException exception.
      Returns:
      never returns
      Throws:
      ClientRejectedException - Thrown when client connection must be rejected by application logic
    • rejectClient

      protected boolean rejectClient(Object reason) throws ClientRejectedException
      Reject the currently connecting client with an error message. The passed object will be available as "application" property of the information object that is returned to the caller.
      Parameters:
      reason - Additional error message to return to client-side Flex/Flash application
      Returns:
      never returns
      Throws:
      ClientRejectedException - Thrown when client connection must be rejected by application logic
    • connect

      public boolean connect(IConnection conn)
      Returns connection result for given scope and parameters. Whether the scope is room or app level scope, this method distinguishes it and acts accordingly. You override appConnect(IConnection, Object[]) or roomConnect(IConnection, Object[]) in your application to make it act the way you want.
      Parameters:
      conn - Connection object
      Returns:
      true if connect is successful, false otherwise
    • connect

      public boolean connect(IConnection conn, IScope scope, Object[] params)
      Returns connection result for given scope and parameters. Whether the scope is room or app level scope, this method distinguishes it and acts accordingly. You override appConnect(IConnection, Object[]) or roomConnect(IConnection, Object[]) in your application to make it act the way you want.
      Specified by:
      connect in interface IScopeHandler
      Overrides:
      connect in class AbstractScopeAdapter
      Parameters:
      conn - Connection object
      scope - Scope
      params - List of params passed to connection handler
      Returns:
      true if connect is successful, false otherwise
    • start

      public boolean start(IScope scope)
      Starts scope. Scope can be both application or room level.
      Specified by:
      start in interface IScopeHandler
      Overrides:
      start in class AbstractScopeAdapter
      Parameters:
      scope - Scope object
      Returns:
      true if scope can be started, false otherwise. See AbstractScopeAdapter.start(IScope) for details.
    • disconnect

      public void disconnect(IConnection conn, IScope scope)
      Returns disconnection result for given scope and parameters. Whether the scope is room or app level scope, this method distinguishes it and acts accordingly.
      Specified by:
      disconnect in interface IScopeHandler
      Overrides:
      disconnect in class AbstractScopeAdapter
      Parameters:
      conn - Connection object
      scope - Scope
    • stop

      public void stop(IScope scope)
      Stops scope handling (that is, stops application if given scope is app level scope and stops room handling if given scope has lower scope level). This method calls appStop(IScope) or roomStop(IScope) handlers respectively.
      Specified by:
      stop in interface IScopeHandler
      Overrides:
      stop in class AbstractScopeAdapter
      Parameters:
      scope - Scope to stop
    • join

      public boolean join(IClient client, IScope scope)
      Adds client to scope. Scope can be both application or room. Can be applied to both application scope and scopes of lower level. This method calls appJoin(IClient, IScope) or roomJoin(IClient, IScope) handlers respectively.
      Specified by:
      join in interface IScopeHandler
      Overrides:
      join in class AbstractScopeAdapter
      Parameters:
      client - Client object
      scope - Scope object
      Returns:
       true
       
      to allow,
       false
       
      to deny connection
    • leave

      public void leave(IClient client, IScope scope)
      Disconnects client from scope. Can be applied to both application scope and scopes of lower level. This method calls appLeave(IClient, IScope) or roomLeave(IClient, IScope) handlers respectively.
      Specified by:
      leave in interface IScopeHandler
      Overrides:
      leave in class AbstractScopeAdapter
      Parameters:
      client - Client object
      scope - Scope object
    • appStart

      public boolean appStart(IScope app)
      Called once on scope (that is, application or application room) start. You override appStart(IScope) or roomStart(IScope) in your application to make it act the way you want.
      Specified by:
      appStart in interface ApplicationMXBean
      Parameters:
      app - Application scope object
      Returns:
      true if scope can be started, false otherwise
    • appStop

      public void appStop(IScope app)
      Handler method. Called when application is stopped.
      Specified by:
      appStop in interface ApplicationMXBean
      Parameters:
      app - Scope object
    • roomStart

      public boolean roomStart(IScope room)
      Handler method. Called when room scope is started.
      Specified by:
      roomStart in interface ApplicationMXBean
      Parameters:
      room - Room scope
      Returns:
      Boolean value
    • roomStop

      public void roomStop(IScope room)
      Handler method. Called when room scope is stopped.
      Specified by:
      roomStop in interface ApplicationMXBean
      Parameters:
      room - Room scope.
    • appConnect

      public boolean appConnect(IConnection conn, Object[] params)
      Handler method. Called every time new client connects (that is, new IConnection object is created after call from a SWF movie) to the application.
      You override this method to pass additional data from client to server application using NetConnection.connect method.
      EXAMPLE:
      In this simple example we pass user's skin of choice identifier from client to the server.
      Client-side:
      NetConnection.connect("rtmp://localhost/killerred5app", "silver");
      Server-side:
      if (params.length > 0) log.debug("Theme selected: {}", params[0]);
      Specified by:
      appConnect in interface ApplicationMXBean
      Parameters:
      conn - Connection object
      params - List of parameters after connection URL passed to NetConnection.connect method.
      Returns:
      Boolean value
    • roomConnect

      public boolean roomConnect(IConnection conn, Object[] params)
      Handler method. Called every time new client connects (that is, new IConnection object is created after call from a SWF movie) to the application. You override this method to pass additional data from client to server application using NetConnection.connect method. See appConnect(IConnection, Object[]) for code example.
      Specified by:
      roomConnect in interface ApplicationMXBean
      Parameters:
      conn - Connection object
      params - List of params passed to room scope
      Returns:
      Boolean value
    • appDisconnect

      public void appDisconnect(IConnection conn)
      Handler method. Called every time client disconnects from the application.
      Specified by:
      appDisconnect in interface ApplicationMXBean
      Parameters:
      conn - Disconnected connection object
    • roomDisconnect

      public void roomDisconnect(IConnection conn)
      Handler method. Called every time client disconnects from the room.
      Specified by:
      roomDisconnect in interface ApplicationMXBean
      Parameters:
      conn - Disconnected connection object
    • appJoin

      public boolean appJoin(IClient client, IScope app)
      Specified by:
      appJoin in interface ApplicationMXBean
    • appLeave

      public void appLeave(IClient client, IScope app)
      Handler method. Called every time client leaves application scope.
      Specified by:
      appLeave in interface ApplicationMXBean
      Parameters:
      client - Client object that left
      app - Application scope
    • roomJoin

      public boolean roomJoin(IClient client, IScope room)
      Specified by:
      roomJoin in interface ApplicationMXBean
    • roomLeave

      public void roomLeave(IClient client, IScope room)
      Handler method. Called every time client leaves room scope.
      Specified by:
      roomLeave in interface ApplicationMXBean
      Parameters:
      client - Disconnected client object
      room - Room scope
    • measureBandwidth

      public void measureBandwidth()
      Try to measure bandwidth of current connection. This is required for some FLV player to work because they require the "onBWDone" method to be called on the connection.
    • measureBandwidth

      public void measureBandwidth(IConnection conn)
      Try to measure bandwidth of given connection. This is required for some FLV player to work because they require the "onBWDone" method to be called on the connection.
      Parameters:
      conn - the connection to measure the bandwidth for
    • hasBroadcastStream

      public boolean hasBroadcastStream(IScope scope, String name)
      Does the scope have a broadcast stream registered with a given name
      Specified by:
      hasBroadcastStream in interface IBroadcastStreamService
      Parameters:
      scope - the scope to check for the stream
      name - name of the broadcast
      Returns:
      true is a stream exists, otherwise false
    • getBroadcastStream

      public IBroadcastStream getBroadcastStream(IScope scope, String name)
      Get a broadcast stream by name
      Specified by:
      getBroadcastStream in interface IBroadcastStreamService
      Parameters:
      scope - the scope to return the stream from
      name - the name of the broadcast
      Returns:
      broadcast stream object
    • getBroadcastStreamNames

      public Set<String> getBroadcastStreamNames(IScope scope)
      Returns list of stream names broadcasted in scope. Broadcast stream name is somewhat different from server stream name. Server stream name is just an ID assigned by Red5 to every created stream. Broadcast stream name is the name that is being used to subscribe to the stream at client side, that is, in NetStream.play call.
      Specified by:
      getBroadcastStreamNames in interface IBroadcastStreamService
      Parameters:
      scope - Scope to retrieve broadcasted stream names
      Returns:
      List of broadcasted stream names.
    • hasOnDemandStream

      public boolean hasOnDemandStream(IScope scope, String name)
      Check whether scope has VOD stream with given name or not
      Specified by:
      hasOnDemandStream in interface IOnDemandStreamService
      Parameters:
      scope - Scope
      name - VOD stream name
      Returns:
      true if scope has VOD stream with given name, false otherwise.
    • getOnDemandStream

      public IOnDemandStream getOnDemandStream(IScope scope, String name)
      Returns VOD stream with given name from specified scope.
      Specified by:
      getOnDemandStream in interface IOnDemandStreamService
      Parameters:
      scope - Scope object
      name - VOD stream name
      Returns:
      IOnDemandStream object that represents stream that can be played on demand, seekable and so forth. See IOnDemandStream for details.
    • getSubscriberStream

      public ISubscriberStream getSubscriberStream(IScope scope, String name)
      Returns subscriber stream with given name from specified scope. Subscriber stream is a stream that clients can subscribe to.
      Specified by:
      getSubscriberStream in interface ISubscriberStreamService
      Parameters:
      scope - Scope
      name - Stream name
      Returns:
      ISubscriberStream object
    • addScheduledJob

      public String addScheduledJob(int interval, IScheduledJob job)
      Wrapper around ISchedulingService, adds a scheduled job to be run periodically. We store this service in the scope as it can be shared across all rooms of the applications.
      Specified by:
      addScheduledJob in interface ISchedulingService
      Parameters:
      interval - Time interval to run the scheduled job
      job - Scheduled job object
      Returns:
      Name of the scheduled job
    • addScheduledOnceJob

      public String addScheduledOnceJob(long timeDelta, IScheduledJob job)
      Adds a scheduled job that's gonna be executed once. Please note that the jobs are not saved if Red5 is restarted in the meantime.
      Specified by:
      addScheduledOnceJob in interface ISchedulingService
      Parameters:
      timeDelta - Time offset in milliseconds from the current date when given job should be run
      job - Scheduled job object
      Returns:
      Name of the scheduled job
    • addScheduledOnceJob

      public String addScheduledOnceJob(Date date, IScheduledJob job)
      Adds a scheduled job that's gonna be executed once on given date. Please note that the jobs are not saved if Red5 is restarted in the meantime.
      Specified by:
      addScheduledOnceJob in interface ISchedulingService
      Parameters:
      date - When to run scheduled job
      job - Scheduled job object
      Returns:
      Name of the scheduled job
    • addScheduledJobAfterDelay

      public String addScheduledJobAfterDelay(int interval, IScheduledJob job, int delay)
      Adds a scheduled job which starts after the specified delay period and fires periodically.
      Specified by:
      addScheduledJobAfterDelay in interface ISchedulingService
      Parameters:
      interval - time in milliseconds between two notifications of the job
      job - the job to trigger periodically
      delay - time in milliseconds to pass before first execution.
      Returns:
      the name of the scheduled job
    • pauseScheduledJob

      public void pauseScheduledJob(String name)
      Pauses a scheduled job
      Specified by:
      pauseScheduledJob in interface ISchedulingService
      Parameters:
      name - Scheduled job name
    • resumeScheduledJob

      public void resumeScheduledJob(String name)
      Resumes a scheduled job
      Specified by:
      resumeScheduledJob in interface ISchedulingService
      Parameters:
      name - Scheduled job name
    • removeScheduledJob

      public void removeScheduledJob(String name)
      Removes scheduled job from scheduling service list
      Specified by:
      removeScheduledJob in interface ISchedulingService
      Parameters:
      name - Scheduled job name
    • getScheduledJobNames

      public List<String> getScheduledJobNames()
      Returns list of scheduled job names
      Specified by:
      getScheduledJobNames in interface ISchedulingService
      Returns:
      List of scheduled job names as list of Strings.
    • getStreamLength

      public double getStreamLength(String name)
      Returns stream length. This is a hook so it may be removed.
      Parameters:
      name - Stream name
      Returns:
      Stream length in seconds (?)
    • getClientTTL

      @Deprecated public long getClientTTL()
      Deprecated.
      Client time to live is max allowed connection ping return time in seconds
      Returns:
      TTL value used in seconds
    • setClientTTL

      @Deprecated public void setClientTTL(int clientTTL)
      Deprecated.
      Client time to live is max allowed connection ping return time in seconds
      Parameters:
      clientTTL - New TTL value in seconds
    • getGhostConnsCleanupPeriod

      @Deprecated public int getGhostConnsCleanupPeriod()
      Deprecated.
      Return period of ghost connections cleanup task call
      Returns:
      Ghost connections cleanup period
    • setGhostConnsCleanupPeriod

      @Deprecated public void setGhostConnsCleanupPeriod(int ghostConnsCleanupPeriod)
      Deprecated.
      Set new ghost connections cleanup period
      Parameters:
      ghostConnsCleanupPeriod - New ghost connections cleanup period
    • startTransmit

      public void startTransmit(Boolean bool, int num)
      Start transmission notification from Flash Player 11.1+. This command asks the server to transmit more data because the buffer is running low. http://help.adobe.com/en_US/flashmediaserver/devguide/WSd391de4d9c7bd609-569139412a3743e78e-8000.html
      Parameters:
      bool - boolean
      num - number
    • stopTransmit

      public void stopTransmit()
      Stop transmission notification from Flash Player 11.1+. This command asks the server to suspend transmission until the client sends a startTransmit event because there is enough data in the buffer.
    • stopTransmit

      public void stopTransmit(Boolean bool, int num)
      Stop transmission notification from Flash Player 11.1+. This command asks the server to suspend transmission until the client sends a startTransmit event because there is enough data in the buffer.
      Parameters:
      bool - boolean
      num - number
    • FCPublish

      public void FCPublish(String streamName)
      Notification method that is sent by FME just before publishing starts.
      Parameters:
      streamName - Name of stream that is about to be published.
    • FCUnpublish

      public void FCUnpublish()
      Notification method that is sent by FME when publishing of a stream ends.
    • FCUnpublish

      public void FCUnpublish(String streamName)
      Notification method that is sent by FME when publishing of a stream ends.
      Parameters:
      streamName - Name of stream that is about to be un-published.
    • FCSubscribe

      public void FCSubscribe(String streamName)
      Notification method that is sent by some clients just before playback starts.
      Parameters:
      streamName - Name of stream that is about to be played.
    • streamBroadcastClose

      public void streamBroadcastClose(IBroadcastStream stream)
      Notification that a broadcasting stream is closing.
      Specified by:
      streamBroadcastClose in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamBroadcastStart

      public void streamBroadcastStart(IBroadcastStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a broadcaster starts.
      Specified by:
      streamBroadcastStart in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamPlayItemPlay

      public void streamPlayItemPlay(ISubscriberStream stream, IPlayItem item, boolean isLive)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a play item plays.
      Specified by:
      streamPlayItemPlay in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
      item - item
      isLive - true if live
    • streamPlayItemStop

      public void streamPlayItemStop(ISubscriberStream stream, IPlayItem item)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a play item stops.
      Specified by:
      streamPlayItemStop in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
      item - item
    • streamPlayItemPause

      public void streamPlayItemPause(ISubscriberStream stream, IPlayItem item, int position)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a play item pauses.
      Specified by:
      streamPlayItemPause in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
      item - item
      position - position
    • streamPlayItemResume

      public void streamPlayItemResume(ISubscriberStream stream, IPlayItem item, int position)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a play item resumes.
      Specified by:
      streamPlayItemResume in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
      item - item
      position - position
    • streamPlayItemSeek

      public void streamPlayItemSeek(ISubscriberStream stream, IPlayItem item, int position)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a play item seeks.
      Specified by:
      streamPlayItemSeek in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
      item - item
      position - position
    • streamPublishStart

      public void streamPublishStart(IBroadcastStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      A broadcast stream starts being published. This will be called when the first video packet has been received.
      Specified by:
      streamPublishStart in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamRecordStart

      public void streamRecordStart(IBroadcastStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      A broadcast stream starts being recorded. This will be called when the first video packet has been received.
      Specified by:
      streamRecordStart in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamRecordStop

      public void streamRecordStop(IBroadcastStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      A broadcast stream stops being recorded. This will be called when the record-stop notification is sent to the Flash client.
      Specified by:
      streamRecordStop in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamSubscriberClose

      public void streamSubscriberClose(ISubscriberStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a subscriber closes.
      Specified by:
      streamSubscriberClose in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • streamSubscriberStart

      public void streamSubscriberStart(ISubscriberStream stream)
      Description copied from interface: IStreamAwareScopeHandler
      Notified when a subscriber starts.
      Specified by:
      streamSubscriberStart in interface IStreamAwareScopeHandler
      Parameters:
      stream - stream
    • handleEvent

      public boolean handleEvent(IEvent event)
      Description copied from class: AbstractScopeAdapter
      Handle an event.
      Specified by:
      handleEvent in interface IEventHandler
      Overrides:
      handleEvent in class AbstractScopeAdapter
      Parameters:
      event - to handle
      Returns:
      true if event was handled, false if it should bubble