下面是一个例子,它利用这个API创建到了一个名字为publishstream的流,并将其发布到live应用。 这个类在后台启动了一个线程,每隔10秒钟在VOD文件sample.mp4的片段和直播流myStream之间切换直播流的信号源。
package com.wowza.wms.plugin.test.integration; import com.wowza.wms.stream.publish.*; import com.wowza.wms.vhost.*; import com.wowza.wms.logging.*; public class ServerPublisherWorker extends Thread { private long sleepTime = 75; private boolean running = true; private Object lock = new Object(); private String applicationName = "live"; private String vodStreamName = "mp4:sample.mp4"; private String liveStreamName = "myStream"; private String publishStreamName = "publishstream"; private int cycleTime = 10000; public synchronized void quit() { synchronized(lock) { running = false; } } public void run() { WMSLoggerFactory.getLogger(ServerPublisherWorker.class).info("ServerPublisherWorker.run: START"); long startTime = System.currentTimeMillis(); long playStartTime = startTime; try { IVHost vhost = VHostSingleton.getInstance(VHost.VHOST_DEFAULT); Publisher publisher = Publisher.createInstance(vhost, applicationName); publisher.publish(publishStreamName); long nextSwitch = playStartTime + cycleTime; long nextType = 0; IPublishingProvider provider = new PublishingProviderMediaReader(publisher, playStartTime, vodStreamName); //provider.seek(20000); provider.setRealTimeStartTime(startTime); WMSLoggerFactory.getLogger(ServerPublisherWorker.class).info("ServerPublisherWorker.run: Start with vod stream: "+vodStreamName); while(true) { boolean moreInFile = provider!=null?provider.play(publisher):false; long currentTime = System.currentTimeMillis(); if (!moreInFile || currentTime > nextSwitch) { if (provider != null) provider.close(); provider = null; if ((nextType % 2) == 0) { provider = new PublishingProviderLive(publisher, publisher.getMaxTimecode(), liveStreamName); //((PublishingProviderLive)provider).setStartOnPreviousKeyFrame(false); provider.setRealTimeStartTime(currentTime); WMSLoggerFactory.getLogger(ServerPublisherWorker.class).info("ServerPublisherWorker.run: Switch to live stream: "+liveStreamName); } else { provider = new PublishingProviderMediaReader(publisher, publisher.getMaxTimecode(), vodStreamName); //provider.seek(20000); provider.setRealTimeStartTime(currentTime); WMSLoggerFactory.getLogger(ServerPublisherWorker.class).info("ServerPublisherWorker.run: Switch to vod stream: "+vodStreamName); } nextSwitch = currentTime + cycleTime; nextType++; if (nextType == 100) break; } else sleep(sleepTime); synchronized(lock) { if (!running) break; } } provider.close(); publisher.publish(null); synchronized(lock) { running = false; } } catch (Exception e) { WMSLoggerFactory.getLogger(ServerPublisherWorker.class).error("ServerPublisherWorker.run: "+e.toString()); e.printStackTrace(); } WMSLoggerFactory.getLogger(ServerPublisherWorker.class).info("ServerPublisherWorker.run: STOP"); } }然后你需要创建一个Server listener,在Server启动时启动上面的这个线程,在Server关闭时停止这个线程,下面是个例子:
package com.wowza.wms.plugin.test.integration; import com.wowza.wms.server.*; public class ServerPublisherServerListener implements IServerNotify { ServerPublisherWorker worker = null; public void onServerCreate(IServer server) { } public void onServerInit(IServer server) { worker = new ServerPublisherWorker(); worker.start(); } public void onServerShutdownComplete(IServer server) { } public void onServerShutdownStart(IServer server) { if (worker != null) worker.quit(); worker = null; } }
之后,将这个server listener 添加到[install-dir]/conf/Server.xml文件的<ServerListeners>列表中:
<ServerListener> <BaseClass>com.wowza.wms.plugin.test.integration.ServerPublisherServerListener</BaseClass> </ServerListener>
在启动后,发布一个名为myStream的直播流到live应用中。 开始测试后,你可以播放名为publishstream的直播流,你将会看到每隔10秒钟直播流都会发生一次变化,它会在点播文件和直播信号之间做切换。