Dynamically switch between RTP and MPEG-TS when streaming RTSP with the Wowza Streaming Engine Java API

This example module dynamically switches between native RTP and MPEG-TS delivery when streaming out of Wowza Streaming Engine™ using RTSP. Adding the query parameter ?mpegts to the RTSP URL will force the session to MPEG-TS. An example scenario for this module is if you want to use one application to stream to mobile devices over RTP and to have set-top boxes request MPEG-TS.

package com.wowza.wms.plugin.test.module;

import com.wowza.wms.module.*;
import com.wowza.wms.application.*;
import com.wowza.wms.rtp.model.*;
import com.wowza.wms.rtsp.*;

public class ModuleRTSPTransportSwitcher extends ModuleBase
{
	IApplicationInstance appInstance = null;

	class TransportSwitcher implements IRTSPActionNotify
	{
		public void onDescribe(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
			while(true)
			{
				RTPStream rtpStream = rtspSession.getRTSPStream();
				if (rtpStream == null)
					break;

				boolean isForceMPEGTS = false; 

				String queryStr = rtspSession.getQueryStr();
				if (queryStr != null)
					isForceMPEGTS = queryStr.toLowerCase().contains("mpegts");

				getLogger().info("ModuleRTSPTransportSwitcher#TransportSwitcher.onDescribe["+appInstance.getContextStr()+"]: mpegts:"+isForceMPEGTS+" context:"+req.getHeader("context"));
				rtpStream.setForceMPEGTSOut(isForceMPEGTS);
				break;
			}
		}

		public void onAnnounce(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onSetParameter(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onGetParameter(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onOptions(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onPause(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onPlay(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onRecord(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onRedirect(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onSetup(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}

		public void onTeardown(RTPSession rtspSession, RTSPRequestMessage req, RTSPResponseMessages resp)
		{
		}
	}

	public void onAppStart(IApplicationInstance appInstance)
	{
		getLogger().info("ModuleRTSPTransportSwitcher.onAppStart["+appInstance.getContextStr()+"]");
		this.appInstance = appInstance;
	}

	public void onRTPSessionCreate(RTPSession rtpSession)
	{
		rtpSession.addActionListener(new TransportSwitcher());
	}
}

To add this module to Application.xml, edit [install-dir]/conf/[application]/Application.xml and add the following <Module> entry as the last entry in the <Modules> list:

<Module>
	<Name>ModuleRTSPTransportSwitcher</Name>
	<Description>ModuleRTSPTransportSwitcher</Description>
	<Class>com.wowza.wms.plugin.test.module.ModuleRTSPTransportSwitcher</Class>
</Module>