Disconnecting Clients - Module Tutorial

Many people are looking for code examples or Wowza module tutorials because they want to disconnect stream clients gracefully. In this small article I would like to show you how this is achieved in Wowza Wrench behind the scenes:

Here is a code snippet that is run periodically (see this Wowza module tutorial on threading for periodical tasks)

     //IApplicationInstance appInstance;
     // Connection is a custom wrapper class for all the different connections
     // that makes it possible to handle them the same way
     List<Connection> connections = new ArrayList<Connection>();

     for(IClient c : nullSafe(appInstance.getClients())) {
      connections.add(new FlashConnection(c));
     }
     for(IHTTPStreamerSession c : nullSafe(appInstance.getHTTPStreamerSessions())) {
      connections.add(new HTTPStreamerBasedConnection(c));
     }
     for(RTPSession c : nullSafe(appInstance.getRTPSessions())) {
      connections.add(new RTPConnection(c));
     }

The trick here is that different streaming clients can be retrieved from the application instance by different getter methods. This is very inconvenient, as you most probably want to handle them the same way. I worked this around by creating a custom Connection interface, which is implemented by the classes RTPConnection, HTTPConnection and FlashConnection. These classes wrap the Wowza specific connection classes and allow uniform handling.

   // now we iterate over all the connections and drop some of them selectively
   for(Connection client : connections) {
     if(myArbitraryConditionsAreTrue(client)) {
       client.reject();
       client.shutdown();
     }
   }

The reject() and shutdown() methods are defined on my custom Connection interface and are implemented by each custom connection wrapper class.

Disconnecting Flash clients:

public class FlashConnection implements Connection {

 private final IClient client;

 @Override
 public void reject() {
  client.rejectConnection();
 }

 @Override
 public void shutdown() {
  client.shutdownClient();
 }

}

Disconnecting RTP clients:

public class RTPConnection implements Connection {

 private RTPSession rtpSession;

 @Override
 public void reject() {
  rtpSession.rejectSession();
 }

 @Override
 public void shutdown() {
  rtpSession.shutdown();
 }

}

Disconnecting HTTP clients:

public class HTTPStreamerBasedConnection implements Connection {

 private IHTTPStreamerSession httpSession;

 @Override
 public void reject() {
  httpSession.rejectSession();
 }

 @Override
 public void shutdown() {
  httpSession.shutdown();
 }

}

Write new comment