Switch streams with the Wowza Streaming Engine Java API

You can use the Stream class of the Wowza Streaming Engine™ Java API to switch between different stream sources. Instead of switching between items in a playlist, this technique uses multiple Playlist objects each with only one item. One stream is created, the switching is from playlist to playlist. Switching is very fast using this method.

In this example, three sources can be switched:

  • IP Camera: axis.stream
  • Live encoder: myStream
  • File: sample.mp4
Note: The axis.stream file is a text file in the content folder that contains the RTSP URL to the camera. It should be started in StreamManager with MediaCaster type rtp or with /conf/StartUpStreams.xml to use it in this switch set.
package com.wowza.wms.example.module;

import com.wowza.wms.amf.*;
import com.wowza.wms.application.*;
import com.wowza.wms.client.*;
import com.wowza.wms.module.*;
import com.wowza.wms.request.*;
import com.wowza.wms.stream.publish.Playlist;
import com.wowza.wms.stream.publish.Stream;

public class ModuleStreamSwitch extends ModuleBase {

	public void switchPlaylist(IClient client, RequestFunction function,
			AMFDataList params) {

		String streamName = getParamString(params, PARAM1);
		String playlistName = getParamString(params, PARAM2);

		Stream stream = (Stream)client.getAppInstance().getProperties().getProperty(streamName);
		Playlist playlist = (Playlist)client.getAppInstance().getProperties().getProperty(playlistName);

		playlist.open(stream);
	}

	public void onAppStart(IApplicationInstance appInstance) {

		appInstance.startMediaCasterStream("axis.stream", "rtp");

		String streamName = "Stream1";
		Playlist playlist1 = new Playlist("pl1");	
		Playlist playlist2 = new Playlist("pl2");
		Playlist playlist3 = new Playlist("pl3");

		playlist1.setRepeat(true);
		playlist2.setRepeat(true);
		playlist3.setRepeat(true);

		playlist1.addItem("myStream", -2, -1);
		playlist2.addItem("axis.stream", -2, -1);
		playlist3.addItem("mp4:sample.mp4", 0, -1);

		Stream stream = Stream.createInstance(appInstance, streamName);

		appInstance.getProperties().setProperty(streamName, stream);
		appInstance.getProperties().setProperty(playlist1.getName(), playlist1);
		appInstance.getProperties().setProperty(playlist2.getName(), playlist2);
		appInstance.getProperties().setProperty(playlist3.getName(), playlist3);
	}

	public void onAppStop(IApplicationInstance appInstance) {
	}
}

Build this module in the Wowza IDE, then create a Wowza module named live with StreamType live and add this Module last in the Application.xml Modules list:

<Module>
	<Name>ModuleStreamSwitch</Name>
	<Description>ModuleStreamSwitch</Description>
	<Class>com.wowza.wms.example.module.ModuleStreamSwitch</Class>
</Module>