Results 1 to 3 of 3

Thread: wowza switch streams

  1. #1

    Default wowza switch streams

    Hellu guys. Using this tutorial http://www.wowza.com/forums/content....de-live-stream i would like to add some modifications.

    Using this code:

    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");
    	}
    }
    wowza switches streams every 10 seconds. I would like to be able to read from a txt file, and of the word is "webcam" then wowza would only switch to the webcam stream and it the word is "local" them it would play sample.mp4. Can you please help me with this one? Thanks!

  2. #2

    Default

    Hi,

    You would have to add some code to read your file initially before the while(true) loop in the run method. You would also need to check it's modified time and re-read it inside the while loop.

    The following is a modified example that reads a text file to get the type. It checks the live stream is published if `webcam` is returned from the file. If not then `local` is returned. The getType() could be changed to get the type from a different location but the method should return immediately. If there is going to be any delay in the method returning, database lookup etc, then it should be handled in a separate thread and a local field updated instead.

    package com.wowza.wms.plugin.test.integration;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import com.wowza.wms.stream.IMediaStream;
    import com.wowza.wms.stream.publish.*;
    import com.wowza.wms.vhost.*;
    import com.wowza.wms.logging.*;
    
    public class ServerPublisherWorkerFileReader 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 long fileLastMod = -1;
    
    	public synchronized void quit()
    	{
    		synchronized(lock)
    		{
    			running = false;
    		}
    	}
    
    	public void run()
    	{
    		WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).info("ServerPublisherWorkerFileReader.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);
    			String type = getType(publisher, "local");
    
    			IPublishingProvider provider = null;
    			if(type.equalsIgnoreCase("webcam")) {
    				provider = new PublishingProviderLive(publisher, playStartTime, liveStreamName);
    			} else {
    				provider = new PublishingProviderMediaReader(publisher, playStartTime, vodStreamName);
    			}
    			provider.setRealTimeStartTime(startTime);
    
    			WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).info("ServerPublisherWorkerFileReader.run: Start with vod stream: "+vodStreamName);
    
    			while(true)
    			{
    				boolean moreInFile = provider!=null?provider.play(publisher):false;
    				String currentType = type;
    				type = getType(publisher, currentType);
    
    				long currentTime = System.currentTimeMillis();
    				if (!moreInFile || !currentType.equalsIgnoreCase(type))
    				{
    					if (provider != null)
    						provider.close();
    					provider = null;
    
    					if (type.equalsIgnoreCase("webcam"))
    					{
    						provider = new PublishingProviderLive(publisher, publisher.getMaxTimecode(), liveStreamName);
    						provider.setRealTimeStartTime(currentTime);
    
    						WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).info("ServerPublisherWorkerFileReader.run: Switch to live stream: "+liveStreamName);
    					}
    					else
    					{
    						provider = new PublishingProviderMediaReader(publisher, publisher.getMaxTimecode(), vodStreamName);
    						provider.setRealTimeStartTime(currentTime);
    
    						WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).info("ServerPublisherWorkerFileReader.run: Switch to vod stream: "+vodStreamName);
    					}
    
    				}
    				else
    					sleep(sleepTime);
    
    				synchronized(lock)
    				{
    					if (!running)
    						break;
    				}
    			}
    
    			provider.close();
    
    			publisher.publish(null);
    
    			synchronized(lock)
    			{
    				running = false;
    			}
    		}
    		catch (Exception e)
    		{
    			WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).error("ServerPublisherWorkerFileReader.run: "+e.toString());
    			e.printStackTrace();
    		}
    
    		WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).info("ServerPublisherWorkerFileReader.run: STOP");
    	}
    	
    	private String getType(Publisher publisher, String type) {
    		File file = new File("typeFile.txt");
    		if(file.exists() && file.lastModified() > this.fileLastMod) {
    			String line = null;
    			BufferedReader reader = null;
    			try {
    				reader = new BufferedReader(new FileReader(file));
    				while(( line = reader.readLine()) != null) {
    					if(line.equalsIgnoreCase("local") || line.equalsIgnoreCase("webcam")) {
    						type = line.toLowerCase();
    						break;
    					}
    				}
    			} catch (Exception e) {
    				WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).error("ServerPublisherWorkerFileReader.getType()" + e.toString());
    			} finally {
    				try {
    					if(reader != null)
    						reader.close();
    				} catch (IOException e) {
    				}
    				reader = null;
    			}
    			this.fileLastMod = file.lastModified();
    		}
    		
    		// Check if current type is webcam and if it is not published, set to local.
    		if(type.equalsIgnoreCase("webcam")) {
    			IMediaStream stream = publisher.getAppInstance().getStreams().getStream(liveStreamName);
    			if(stream == null) {
    				WMSLoggerFactory.getLogger(ServerPublisherWorkerFileReader.class).warn("ServerPublisherWorkerFileReader.getType(): webcam requested but "+liveStreamName+" is not published. returning local");
    				return "local";
    			}
    		}
    		
    		return type;
    	}
    }
    Roger.

  3. #3

    Default

    Hey Roger, thanks a lot, works like a charm. I was knocking my head against the wall yesterday, because i couldn't make it work

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •