Here the complete code (sorry, I needed to remove some parts of our business logic - it’s a project at our university)
S2.java:
package de.mpgz.camm;
import com.wowza.wms.*;
public class S2 extends ModuleBase {
private IApplicationInstance appInstance = null;
public void onAppStart(IApplicationInstance appInstance) {
String fullname = appInstance.getApplication().getName() + "/"
+ appInstance.getName();
getLogger().info("onAppStart: " + fullname);
// RESTRICT TO DEFAULT INSTANCE
if(!appInstance.getName().equalsIgnoreCase(ApplicationInstance.DEFAULT_APPINSTANCE_NAME))
{
appInstance.shutdown(false, false);
} else {
// Save Instance
this.appInstance = appInstance;
}
}
public void startMediaCasterStream(String group, String streamName, String mediaCasterType, String name, String size, int bitrate)
{
try
{
while(true)
{
getLogger().info("ModuleMediaCasterStreamManager.startMediaCasterStream["+mediaCasterType+"]: "+streamName);
MediaCasterStreamMap mediaCasterMap = this.appInstance.getMediaCasterStreams();
IVHost vhost = this.appInstance.getVHost();
MediaCasterStreamManager mediaCasterStreamManager = mediaCasterMap.getStreamManager();
MediaCasterList mediaCasterList = vhost.getMediaCasterList();
MediaCasterItem mediaCasterDef = mediaCasterList.getMediaCasterDef(mediaCasterType);
if (mediaCasterDef == null)
{
getLogger().warn("ModuleMediaCasterStreamManager.startMediaCasterStream: MediaCaster type not found: "+mediaCasterType);
break;
}
MediaCasterStreamItem mediaCasterStream = mediaCasterMap.getMediaCaster(streamName);
if (mediaCasterStream != null)
{ getLogger().warn("ModuleMediaCasterStreamManager.startMediaCasterStream: MediaCaster already exists[mediacaster]: "+streamName);
break;
}
boolean success = mediaCasterStreamManager.startStream(streamName, mediaCasterType);
if (success)
{ getLogger().info("ModuleMediaCasterStreamManager.startMediaCasterStream: Stream started: "+streamName);
}
else
{
getLogger().info("ModuleMediaCasterStreamManager.startMediaCasterStream: Stream failed: "+streamName);
}
break;
}
}
catch (Exception e)
{
getLogger().error("ModuleMediaCasterStreamManager.startMediaCasterStream: "+e.toString());
}
}
// NEEDED INFO: streamName (glf.sdp)
public void stopMediaCasterStream(String streamName)
{
try
{
while(true)
{
getLogger().info("ModuleMediaCasterStreamManager.stopMediaCasterStream: "+streamName);
MediaCasterStreamMap mediaCasterMap = this.appInstance.getMediaCasterStreams();
MediaCasterStreamManager mediaCasterStreamManager = mediaCasterMap.getStreamManager();
boolean success = mediaCasterStreamManager.stopStream(streamName);
if (success)
{
getLogger().info("ModuleStreamStarter.stopMediaCasterStream: Stream stopped: "+streamName);
}
else
{
getLogger().warn("ModuleMediaCasterStreamManager.stopMediaCasterStream: Stream not found: "+streamName);
}
break;
}
}
catch (Exception e)
{
getLogger().error("ModuleMediaCasterStreamManager.stopMediaCasterStream: "+e.toString());
}
}
}
CaMMServerListener.java:
package de.mpgz.camm;
import com.wowza.wms.*;
import edu.emory.mathcs.backport.java.util.concurrent.locks.WMSReadWriteLock;
public class CaMMServerListener implements IServerNotify {
private CaMM_S2 Main;
public void onServerCreate(IServer server) {
// TODO Auto-generated method stub
this.Main = new CaMM_S2(server);
}
private void loadAndLockAppInstance(IVHost vhost, String appName, String appInstanceName)
{
IApplication localApplication = null;
IApplicationInstance localAppInstance = null;
WMSReadWriteLock appLock = vhost.getApplicationLock();
appLock.writeLock().lock();
try
{
localApplication = vhost.getApplication(appName);
if (localApplication != null)
{
localAppInstance = localApplication.getAppInstance(appInstanceName);
localAppInstance.setApplicationTimeout(0);
if(appName.equalsIgnoreCase("live"))
{
this.Main.setLiveApplication((S2)localAppInstance.getModuleInstance("S2"));
}
} else {
WMSLoggerFactory.getLogger(null).warn("Application folder ([install-location]/applications/"+appName+") is missing");
}
}
catch (Exception e)
{
WMSLoggerFactory.getLogger(null).error("CaMMServerListener.loadAndLockAppInstance: "+ appName + " - " + e.toString());
}
finally
{
appLock.writeLock().unlock();
}
}
public void onServerInit(IServer server) {
IVHost vhost = VHostSingleton.getInstance(VHost.VHOST_DEFAULT);
loadAndLockAppInstance(vhost, "live", ApplicationInstance.DEFAULT_APPINSTANCE_NAME);
this.Main.startup();
}
public void onServerShutdownStart(IServer server) {
this.Main.shutdown();
}
public void onServerShutdownComplete(IServer server) {
// TODO Auto-generated method stub
}
}
conf/live/Application.xml:
<Module>
<Name>S2</Name>
<Description>CaMM S2 Plugin</Description>
<Class>de.mpgz.camm.S2</Class>
</Module>
conf/Server.xml
<ServerListener>
<BaseClass>de.mpgz.camm.CaMMServerListener</BaseClass>
</ServerListener>
(You can use auto-generated imports to keep them small.)
And instead of “this.Main = new CaMM_S2(server);” in the CaMMServerListener you can use your own class to check if the .sdp file is existing or wait for network connections (like we do).