How to control which template is used for transcoding with the Wowza API

The Wowza transcoder does allow you to configure stream specific or generic templates when using the application configuration so can be quite powerful. There are times however that do require more control than provided by configuration and this module is a demonstration on how to use the Wowza API to make the template selection.

Outlined in this article is the use of the ILiveStreamTranscoderNotify and ILiveStreamTranscoderActionNotify interfaces. The application module adds a listener using addLiveStreamTranscoderListener to the application instance and from there adds an additional listener addActionListener.

Each class is shown below

TemplateControl Class

package guru.thewowza.example.transcoder;

import com.wowza.wms.application.*;
import com.wowza.wms.module.*;

public class TemplateControl extends ModuleBase {

	private TranscoderNotifier TransNotify = null;
	private Logger Log = null;
	private Boolean debug = false;	

	public void onAppStart(IApplicationInstance appInstance) {
		String fullname = appInstance.getApplication().getName() + "/"
				+ appInstance.getName();
		this.debug = appInstance.getProperties().getPropertyBoolean("transcoderTemplateInfo",false);
		this.Log = new Logger(getLogger());
 		this.TransNotify = new TranscoderNotifier(this.debug, this.Log);
		appInstance.addLiveStreamTranscoderListener(this.TransNotify);
		this.Log.LogMessage("onAppStart: " + fullname);
	}

	public void onAppStop(IApplicationInstance appInstance) {
		String fullname = appInstance.getApplication().getName() + "/"
				+ appInstance.getName();
		this.Log.LogMessage("onAppStop: " + fullname);
	}
}

TranscoderNotifier Class

package guru.thewowza.example.transcoder;

import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.livetranscoder.ILiveStreamTranscoder;
import com.wowza.wms.stream.livetranscoder.ILiveStreamTranscoderNotify;
import com.wowza.wms.transcoder.model.LiveStreamTranscoder;

public class TranscoderNotifier implements ILiveStreamTranscoderNotify
	 {
	  private boolean Debug = false;
	  private Logger Log = null;
 	  public TranscoderNotifier (
					boolean debug,
					Logger log
					)
				{
				this.Debug = debug;
				this.Log = log;
				}
	  public void onLiveStreamTranscoderCreate(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream stream) {
	   ((LiveStreamTranscoder)liveStreamTranscoder).addActionListener(
			   new TranscoderActionNotifier(stream,this.Debug,this.Log));
	  }
	  public void onLiveStreamTranscoderDestroy(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream stream) { }
	  public void onLiveStreamTranscoderInit(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream stream) { } 
}

ILiveStreamTranscoderActionNotify Class

package guru.thewowza.example.transcoder;

import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.media.model.MediaCodecInfoAudio;
import com.wowza.wms.media.model.MediaCodecInfoVideo;
import com.wowza.wms.stream.IMediaStream;
import com.wowza.wms.stream.livetranscoder.LiveStreamTranscoderItem;
import com.wowza.wms.transcoder.model.ILiveStreamTranscoderActionNotify;
import com.wowza.wms.transcoder.model.LiveStreamTranscoder;
import com.wowza.wms.transcoder.model.TranscoderSessionAudioEncode;
import com.wowza.wms.transcoder.model.TranscoderSessionDataEncode;
import com.wowza.wms.transcoder.model.TranscoderSessionDestination;
import com.wowza.wms.transcoder.model.TranscoderSessionVideoEncode;
import com.wowza.wms.transcoder.model.TranscoderStreamNameGroup;

class TranscoderActionNotifier implements ILiveStreamTranscoderActionNotify
{
	  private IMediaStream TransSourceStream = null;
	  private Logger Log = null;
	  private boolean debug = false;

	  public TranscoderActionNotifier (
					IMediaStream transsourcestream,
					boolean debug,
					Logger log
					)
				{
				this.TransSourceStream = transsourcestream;
				this.debug = debug;
				this.Log = log;
				}

	  public void onInitBeforeLoadTemplate(LiveStreamTranscoder liveStreamTranscoder) {

		// Here we output the template name, the template folder and the source 
		// stream name.
		 this.Log.LogMessage("Template name is "+liveStreamTranscoder.getTemplateName());
		 this.Log.LogMessage("Template folder is "+liveStreamTranscoder.getTemplateDir());
		 this.Log.LogMessage("Source stream name is "+this.TransSourceStream.getName());
		// Here we output we are changing the template name.
		// You could additional logic to the template selection based on stream name
		// or other criteria
		 this.Log.LogMessage("Setting template name to myTemplateName.xml");
		 liveStreamTranscoder.setTemplateName("myTemplateName.xml");
		}			

 public void onInitStart(LiveStreamTranscoder liveStreamTranscoder, String streamName, String transcoderName, 
		IApplicationInstance appInstance, LiveStreamTranscoderItem liveStreamTranscoderItem) { }
 public void onInitAfterLoadTemplate(LiveStreamTranscoder liveStreamTranscoder) { }
 public void onInitStop(LiveStreamTranscoder liveStreamTranscoder) { }
 public void onCalculateSourceVideoBitrate(LiveStreamTranscoder liveStreamTranscoder, long bitrate) { }
 public void onCalculateSourceAudioBitrate(LiveStreamTranscoder liveStreamTranscoder, long bitrate) { }
 public void onSessionDestinationCreate(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionDestination sessionDestination) { }
 public void onSessionVideoEncodeCreate(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionVideoEncode sessionVideoEncode) { }
 public void onSessionAudioEncodeCreate(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionAudioEncode sessionAudioEncode) { }
 public void onSessionDataEncodeCreate(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionDataEncode sessionDataEncode) { }
 public void onSessionVideoEncodeInit(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionVideoEncode sessionVideoEncode) { }
 public void onSessionAudioEncodeInit(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionAudioEncode sessionAudioEncode) { }
 public void onSessionDataEncodeInit(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionDataEncode sessionDataEncode) { }
 public void onSessionVideoEncodeSetup(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionVideoEncode sessionVideoEncode) { }
 public void onSessionAudioEncodeSetup(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionAudioEncode sessionAudioEncode) { }
 public void onSessionVideoEncodeCodecInfo(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionVideoEncode sessionVideoEncode, MediaCodecInfoVideo codecInfoVideo) { }
 public void onSessionAudioEncodeCodecInfo(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderSessionAudioEncode sessionAudioEncode, MediaCodecInfoAudio codecInfoAudio) { }
 public void onSessionVideoDecodeCodecInfo(LiveStreamTranscoder liveStreamTranscoder, 
		MediaCodecInfoVideo codecInfoVideo) { }
 public void onSessionAudioDecodeCodecInfo(LiveStreamTranscoder liveStreamTranscoder, 
		MediaCodecInfoAudio codecInfoAudio) { }
 public void onRegisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderStreamNameGroup streamNameGroup) { }
 public void onUnregisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, 
		TranscoderStreamNameGroup streamNameGroup) { }
 public void onShutdownStart(LiveStreamTranscoder liveStreamTranscoder) { } 
 public void onShutdownStop(LiveStreamTranscoder liveStreamTranscoder) { }
 public void onResetStream(LiveStreamTranscoder liveStreamTranscoder) { }
}

Logger Class

package guru.thewowza.example.transcoder;

import com.wowza.wms.logging.WMSLogger;

public class Logger {

	private WMSLogger Logger = null;

	public Logger( WMSLogger Log ) {		
			Logger = Log;
			}

	public void LogMessage(	String Message ) {
		Logger.info("guru.thewowza.example.transcoder.TemplateControl: '"+Message+"'");
	}

}

To use the example code you will need to add a Module into your Application as the last module in the Modules section.

<Module>
	<Name>TemplateControl</Name>
	<Description>TemplateControl</Description>
	<BaseClass>guru.thewowza.example.transcoder.TemplateControl</BaseClass>
</Module>

The source code and jar file can be found in the download here. To use copy the jar file into your lib/ folder of your Wowza install.


Comments are closed.