Wowza Community

Generating smil files programatically

Hi,

I’m using multiple origins and egdes, all of them use the same shared disc for the streams storage path.

For each new stream in the origin server, I want to generate a smil file in the shared streams storage directory.

I want the smill to match the ngrp:myStream_all.

That’s the code I wrote:

class LiveStreamTranscoderListener implements ILiveStreamTranscoderNotify {

@Override

public void onLiveStreamTranscoderCreate(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream mediaStream) {

}

@Override

public void onLiveStreamTranscoderDestroy(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream mediaStream) {

}

@Override

public void onLiveStreamTranscoderInit(ILiveStreamTranscoder liveStreamTranscoder, IMediaStream mediaStream) {

IApplicationInstance appInstance = liveStreamTranscoder.getAppInstance();

String streamName = mediaStream.getName();

String appName = appInstance.getContextStr();

getLogger().debug(“LiveStreamEntry#LiveStreamTranscoderListener.onLiveStreamTranscoderInit [” + appName + “/” + mediaStream.getName() + “]”);

// Itried all the three options below:

MediaList mediaList = MediaListUtils.parseMediaList(appInstance, streamName, “smil”, null);

// MediaList mediaList = MediaListUtils.parseMediaList(appInstance, streamName + “_all”, “smil”, null);

// MediaList mediaList = MediaListUtils.parseMediaList(appInstance, “ngrp:” + streamName + “_all”, “smil”, null);

if (mediaList == null) {

getLogger().error("LiveStreamEntry#LiveStreamTranscoderListener.onLiveStreamTranscoderInit: MediaList not found: " + appName + “/” + streamName);

return;

}

String smil = mediaList.toSMILString();

String filePath = appInstance.getStreamStoragePath() + File.separator + streamName + “_all.smil”;

try {

PrintWriter out = new PrintWriter(filePath);

out.print(smil);

out.close();

} catch (FileNotFoundException e) {

getLogger().error(“LiveStreamEntry#LiveStreamTranscoderListener.onLiveStreamTranscoderInit: Failed writing to file [” + filePath + "]: " + e.getMessage());

return;

}

getLogger().info(“LiveStreamEntry#LiveStreamTranscoderListener.onLiveStreamTranscoderInit: Created smil file [” + filePath + "] for stream " + appName + “/” + streamName + “:\n” + smil + “\n\n”);

}

}

public void onAppStart(IApplicationInstance appInstance) {

appInstance.addLiveStreamTranscoderListener(new LiveStreamTranscoderListener());

}

When I access the medialist tool:

http://wowza-01.dev:8086/medialist?streamname=ngrp:0_xmk3txay_all&application=kLive/p&format=smil

I get a valid smil:

But when I generate the smil file from the code, I get empty smil:

Is there a way to do it?

Thanks,

T.

Hi,

You are trying to access the StreamNameGroup before it is created.

You need to implement the ILiveStreamTranscoderActionNotify interface which has the methods, onRegisterStreamNameGroup and onUnregisterStreamNameGroup. From these methods, you should be able to call MedialistUtils.parseMedialist.

There is a convenience class that already implements all of the ILiveStreamTranscoderActionNotify methods so you can just extend that in your own class and implement the required methods.

. . .
public void onLiveStreamTranscoderCreate (ILiveStreamTranscoder liveStreamTranscoder, IMediaStream stream)
{
       ((LiveStreamTranscoder)liveStreamTranscoder).addActionListener(new MyTranscoderActionNotifier());
}
. . . 
class MyTranscoderActionNotifier extends LiveStreamTranscoderActionNotifyBase
{
	void onRegisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, TranscoderStreamNameGroup streamNameGroup)
	{
		// Create smil here.
	}
	void onUnRegisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, TranscoderStreamNameGroup streamNameGroup)
	{
		// remove smil here if needed.
	}
}

Thank you Roger,

But I still get an empty smil file, although the media list tool over http returns the expected smil.

I tried few options, all of them returned empty smil:

MediaList mediaList = MediaListUtils.parseMediaList(appInstance, streamName, “smil”, null);

MediaList mediaList = MediaListUtils.parseMediaList(appInstance, streamName + “_all”, “smil”, null);

MediaList mediaList = MediaListUtils.parseMediaList(appInstance, “ngrp:” + streamName + “_all”, “smil”, null);

Could you please tell me what is the right one or what I do wrong?

Thanks,

T.

solved it:

class LiveStreamTranscoderActionListener extends LiveStreamTranscoderActionNotifyBase {

@Override

public void onRegisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, TranscoderStreamNameGroup streamNameGroup){

IApplicationInstance appInstance = liveStreamTranscoder.getAppInstance();

String groupName = streamNameGroup.getStreamName();

String appName = appInstance.getContextStr();

getLogger().debug(“LiveStreamEntry#LiveStreamTranscoderActionListener.onRegisterStreamNameGroup [” + appName + “/” + groupName + “]”);

MediaList mediaList = MediaListUtils.parseMediaList(appInstance, groupName, “ngrp”, null);

if (mediaList == null) {

getLogger().error("LiveStreamEntry#LiveStreamTranscoderActionListener.onRegisterStreamNameGroup: MediaList not found: " + appName + “/” + groupName);

return;

}

String smil = mediaList.toSMILString();

String filePath = appInstance.getStreamStoragePath() + File.separator + groupName + “.smil”;

try {

PrintWriter out = new PrintWriter(filePath);

out.print(smil);

out.close();

} catch (FileNotFoundException e) {

getLogger().error(“LiveStreamEntry#LiveStreamTranscoderActionListener.onRegisterStreamNameGroup: Failed writing to file [” + filePath + "]: " + e.getMessage());

return;

}

getLogger().info(“LiveStreamEntry#LiveStreamTranscoderActionListener.onRegisterStreamNameGroup: Created smil file [” + filePath + "] for stream " + appName + “/” + groupName + “:\n” + smil + “\n\n”);

}

@Override

public void onUnregisterStreamNameGroup(LiveStreamTranscoder liveStreamTranscoder, TranscoderStreamNameGroup streamNameGroup){

IApplicationInstance appInstance = liveStreamTranscoder.getAppInstance();

String groupName = streamNameGroup.getStreamName();

String appName = appInstance.getContextStr();

getLogger().debug(“LiveStreamEntry#LiveStreamTranscoderActionListener.onUnregisterStreamNameGroup [” + appName + “/” + groupName + “]”);

String filePath = appInstance.getStreamStoragePath() + File.separator + groupName + “.smil”;

File file = new File(filePath);

if(file.exists())

file.delete();

getLogger().info(“LiveStreamEntry#LiveStreamTranscoderActionListener.onUnregisterStreamNameGroup: Deleted smil file [” + filePath + "] for stream " + appName + “/” + groupName);

}

}