Do server-side redirect of RTMP connections from one application to another with the Wowza Streaming Engine Java API

This Wowza Streaming Engine™ Java API example code shows how to redirect RTMP client connections to transparently force connections to specific applications and application instances.

Any client that connects to any application can be redirected. This is an example of a VHost listener that redirects a live encoder connection to an application named live2 instead of the application name specified in the encoder.

package com.wowza.demo.VHostExample;

import com.wowza.wms.amf.AMFData;
import com.wowza.wms.amf.AMFDataItem;
import com.wowza.wms.amf.AMFDataList;
import com.wowza.wms.amf.AMFDataObj;
import com.wowza.wms.client.IClient;
import com.wowza.wms.request.RequestFunction;
import com.wowza.wms.vhost.*;

public class VHostTest implements IVHostNotify {

public void onVHostClientConnect(IVHost vhost, IClient inClient, RequestFunction function,
AMFDataList params)
               {
                         if (params.size() >= 3)
                         {
                              AMFDataObj connectObj = (AMFDataObj)params.get(2);
                              if (connectObj.getType() == AMFData.DATA_TYPE_OBJECT)
                              {
                                        AMFDataObj connectObj2 = (AMFDataObj)params.get(2);

                                        // Show all the variables you can change.
                                        List<String> MyKeys = connectObj2.getKeys();
                                        Iterator<String> MyKeysIter = MyKeys.iterator();
                                        while ( MyKeysIter.hasNext())
                                                  {
                                                  String Name = MyKeysIter.next();
                                                  String Value = connectObj2.get(Name).toString();
                                                  System.out.println("Name is "+Name+" value is "+Value+"");
                                                  }

                                        String appStr = (String)connectObj2.get("app").toString();
                                        String tcUrlStr = connectObj2.get("tcUrl").toString();

                                        // rewrite them here
                                        // Example change the appStr from anything to "live2"

                                        tcUrlStr = tcUrlStr.replace(appStr,"live2");

                                        connectObj.put("app", new AMFDataItem("live2"));
                                        connectObj.put("tcUrl", new AMFDataItem(tcUrlStr));
                              }
                    }
          }

          public void onVHostCreate(IVHost vhost) {

          }

          public void onVHostInit(IVHost vhost) {

          }

          public void onVHostShutdownComplete(IVHost vhost) {

          }

          public void onVHostShutdownStart(IVHost vhost) {

          }

}

To use this module:

  1. Add a VHost listener to [install-dir]/conf/Server.xml.
    <VHostListener>
         <BaseClass>com.wowza.demo.VHostExample.VHostTest</BaseClass>
    </VHostListener>
  2. Verify the listener is working as expected through log statements.

    Expected log output in [install-dir]/logs/wowzastreamingengine_access.log when the VHostListener is active and where the client connects to application named 123456 and the stream being published is myStream. However, you can see in the rest of the log file that the actual application that is actually running is named live2.
    Name is app value is 123456
    Name is tcUrl value is rtmp://[wowza-ip-address]/123456
    Name is type value is nonprivate
    Name is flashVer value is FMLE/1.0 (compatible; FMSc/1.0)
    Name is swfUrl value is rtmp://[wowza-ip-address]/123456
    INFO application app-start _definst_ live2/_definst_
 
Note: The application that is specified for the redirect connection does not have to have be preconfigured. Thus, in this example you could connect with the following URL and connections would be redirected to back to the application named live2.

rtmp://[wowza-ip-address]/thisappname/stream1