package com.wowza.forum;
import com.wowza.wms.application.*;
import com.wowza.wms.server.*;
import com.wowza.wms.vhost.*;
import com.wowza.wms.stream.publish.*;
import com.wowza.wms.logging.*;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class StreamPublisher3 implements IServerNotify {
WMSLogger log = WMSLoggerFactory.getLogger(null);
public void onServerCreate(IServer server)
{
}
public void onServerInit(IServer server)
{
IVHost vhost = VHostSingleton.getInstance(server.getProperties().getPropertyStr("PublishToVHost", VHost.VHOST_DEFAULT));
IApplication app = vhost.getApplication(server.getProperties().getPropertyStr("PublishToApplication", "live"));
String storageDir = app.getAppInstance("_definst_").getStreamStorageDir();
try
{
String smilLoc = storageDir + "/playlist.smil";
File playlistxml = new File(smilLoc);
if (playlistxml.exists() == false){
log.info("Could not find playlist file: " + smilLoc);
return;
}
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(smilLoc);
document.getDocumentElement().normalize();
NodeList streamList = document.getElementsByTagName("stream");
if (streamList.getLength() == 0){
log.info("No streams defined in smil file");
return;
}
for (int i = 0; i < streamList.getLength(); i++)
{
Node streamItem = streamList.item(i);
if (streamItem.getNodeType() == Node.ELEMENT_NODE)
{
Element e = (Element) streamItem;
NodeList videos = e.getElementsByTagName("video");
if (videos.getLength() == 0){
log.info("No videos defined in stream");
return;
}
String streamName = e.getAttribute("name");
if (streamName.length()==0)
continue;
Stream stream = Stream.createInstance(vhost, app.getName(), streamName);
stream.setRepeat((e.getAttribute("repeat")=="false")?false:true);
Playlist playlist = new Playlist(streamName);
for (int j = 0; j < videos.getLength(); j++)
{
Node video = videos.item(j);
if (video.getNodeType() == Node.ELEMENT_NODE)
{
Element e2 = (Element) video;
String src = e2.getAttribute("src");
Integer start = Integer.parseInt(e2.getAttribute("start"));
Integer length = Integer.parseInt(e2.getAttribute("length"));
playlist.addItem(src, start, length);
}
}
String scheduled = e.getAttribute("scheduled");
if (scheduled=="")
{
playlist.open(stream);
log.info("Live now: " + stream.getName());
}
else
{
SimpleDateFormat parser = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date startTime = parser.parse(scheduled);
ScheduledItem item = new ScheduledItem(startTime, playlist, stream);
item.start();
log.info("Scheduled: " + stream.getName() + " for: " + scheduled);
}
}
}
}
catch(Exception ex)
{
}
}
private class ScheduledItem {
public Timer mTimer;
public TimerTask mTask;
public Date mStart;
public Playlist mPL;
public Stream mStream;
public ScheduledItem(Date d, Playlist pl, Stream s){
mStart = d;
mPL = pl;
mStream = s;
mTask = new TimerTask(){
public void run() {
mPL.open(mStream);
log.info("Scheduled stream is now live: " + mStream.getName());
}
};
mTimer = new Timer();
}
public void start(){
if (mTimer==null)
mTimer = new Timer();
mTimer.schedule(mTask, mStart);
log.info("scheduled playlist: "+mPL.getName()+
" on stream: "+mStream.getName()+
" for:"+mStart.toString());
}
public void stop(){
if (mTimer != null){
mTimer.cancel();
mTimer=null;
log.info("cancelled playlist: "+mPL.getName()+
" on stream: "+mStream.getName()+
" for:"+mStart.toString());
}
}
}
public void onServerShutdownComplete(IServer server)
{
}
public void onServerShutdownStart(IServer server)
{
}
}
Create the smil file [install-dir]/content/playlist.smil
Set scheduled value in the future, or leave blank or remove to start playing immediately.
Set repeat=true or false for each stream.
<smil>
<head>
</head>
<body>
<stream name="Stream1" repeat="true">
<video src="mp4:Extremists.m4v" start="5" length="5"/>
<video src="mp4:Extremists.m4v" start="50" length="5"/>
<video src="mp4:Extremists.m4v" start="150" length="5"/>
</stream>
<stream name="Stream2" scheduled="2009-11-30 19:03:00" repeat="false" >
<video src="mp4:Extremists.m4v" start="0" length="-1"/>
</stream>
</body>
</smil>
Create a Wowza app named “live”, change the Application.xml /StreamType to “live”
To play open [install-dir]/examples/LiveVideoStreaming/client/live.html
Server: rtmp://[wowza-address]/live
Stream: Stream1 (as set in smil file).