본문 바로가기

Wowza

Detect if the live encoder has stopped.

Sample Code
package com.wowza.wms.example;

import com.wowza.wms.application.*;
import com.wowza.wms.amf.*;
import com.wowza.wms.client.*;
import com.wowza.wms.module.*;
import com.wowza.wms.request.*;
import com.wowza.wms.stream.*;
import java.util.List;
import java.util.concurrent.locks.*;
import com.wowza.wms.example.SimpleSender;

public class StreamWatcher extends ModuleBase {

	int streamTimeout = 10000;
	long streamMissingTime;
	long streamLastSeq;
	String watchStream;
	String smtpServer;
	String smtpPassword;
	String smtpLogin;
	String alertEmailAddress;
	Lock lock;
	Boolean runWatchdog = true;
	Boolean session = false;
	int callbackClientId;
	
	public void sendAlert(String cause)
	{
		SimpleSender send = new SimpleSender();
		send.login = smtpLogin;
		send.pwd = smtpPassword;
		send.send(smtpServer, alertEmailAddress,alertEmailAddress,"Stream Watcher Alert",cause);
	}
	
	public void onAppStart(IApplicationInstance appInstance) {
		String fullname = appInstance.getApplication().getName() + "/"
				+ appInstance.getName();
		getLogger().info("onAppStart: " + fullname);
		WMSProperties props = appInstance.getProperties();
		smtpServer = props.getPropertyStr("smtpServer");
		smtpPassword = props.getPropertyStr("smtpPassword");
		smtpLogin = props.getPropertyStr("smtpLogin");
		watchStream = props.getPropertyStr("watchStream");
		alertEmailAddress = props.getPropertyStr("alertEmailAddress");
		// doWatchdog(appInstance);
		
	}
	
	public void onConnect(IClient client, RequestFunction function,
			AMFDataList params) {
		getLogger().info("onConnect: " + client.getClientId());
		
	}
	public void stopWatchdog(IClient client, RequestFunction function,
			AMFDataList params) {
		getLogger().info("Stop Watchdog");
		runWatchdog = false;
		client.getAppInstance().broadcastMsg("watchdogMessage", "stopped");
		// sendResult(client, params, "Watchdog stopped");
	}
	
	public void startWatchdog(IClient client, RequestFunction function,
			AMFDataList params) {
		//AMFDataObj param1 = getParamObj(params, PARAM1);
		
		getLogger().info("startWatchdog");
		
		runWatchdog = true;
		client.getAppInstance().broadcastMsg("watchdogMessage", "started");		
		IApplicationInstance appInstance = client.getAppInstance();
		doWatchdog(client,appInstance);
		
	}
    public void doWatchdog(IClient client, IApplicationInstance appInstance)
    {
    	getLogger().info("doWatchdog: "+this.streamTimeout);
          boolean isTimeout = false;
          boolean isMissing = false;
          while (runWatchdog)
          {
                if (streamTimeout <= 0)
                      break;
               
                synchronized(this)
                {
                	try
                	{
                	Thread.sleep(3000);	
                	}
                	catch(Exception e)
                	{
                		
                	}
                	if (session == null)
                            break;
                	
                	MediaStreamMap mediamap = appInstance.getStreams();
                    IMediaStream stream = mediamap.getStream(watchStream);
                      
                      if (stream == null)
                      {
                    	  getLogger().info("doWatchdog: stream is null");
                    	  runWatchdog = false;
                    	  client.getAppInstance().broadcastMsg("watchdogMessage", "Stream is null");
                    	  client.getAppInstance().broadcastMsg("watchdogMessage", "stopped");
                    	  sendAlert("Stream " + watchStream + " does not exist");
                    	  break;
                      }

                      List packets = stream.getPlayPackets();

                      if (packets.size() == 0)
                      {
                            isMissing = true;
                            client.getAppInstance().broadcastMsg("watchdogMessage","packets 0");
                      		getLogger().info("doWatchdog: packets size is 0");
                      }
                      else
                      {
                    	  AMFPacket packet = (AMFPacket)packets.get(packets.size()-1);
                            long currSeq = packet.getSeq();
                            if (currSeq != this.streamLastSeq)
                            {
                                  this.streamLastSeq = currSeq;
                                  client.getAppInstance().broadcastMsg("watchdogMessage", "currSeq: " + currSeq);
                                  getLogger().info("doWatchdog currSeq: " + currSeq);
                            }
                            else
                            {
                                  isMissing = true;
                                  client.getAppInstance().broadcastMsg("watchdogMessage","Stream Appears Stalled");
                                  getLogger().info("doWatchdog: Stream Appears Stalled");
                            }
                      }
                      

                      if (isMissing)
                      {
                            long currtime = System.currentTimeMillis();
                            if (this.streamMissingTime == -1)
                            {
                                  this.streamMissingTime = currtime;
                                  getLogger().info("doWatchdog currtime: " + currtime);
                            }
                            else
                            { 
                            	if ((currtime-this.streamMissingTime) > this.streamTimeout)
                            	{
                                    isTimeout = true;
                               }
                            }
                      }
                      else
                      {
                    	  this.streamMissingTime = -1;
                      }
                      
                      if (isTimeout)
                      {
                    	  getLogger().info("doWatchdog: Timeout. Stream is stalled. Send Alert");
                    	  client.getAppInstance().broadcastMsg("watchdogMessage","Stream seems stalled. Sending Alert");
                    	  client.getAppInstance().broadcastMsg("watchdogMessage", "stopped");
                          sendAlert("Stream " + watchStream + " timed-out");
                          isTimeout = false;
                          this.streamMissingTime = -1;
                          runWatchdog = false;
                          break;
                      }
                }
          }
          getLogger().info("doWatchdog: done");
          client.getAppInstance().broadcastMsg("watchdogMessage","Done");
    }
}


Send Mail Code : 그냥 참조만...
package com.wowza.wms.example;

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import javax.mail.Transport;

public class SimpleSender {

 //   private static final String SMTP_HOST_NAME = "smtp.myserver.com";
    public String login;
    public String pwd;
	
	public void send(
			String smtpServer, 
			String to, 
			String from, 
			String subject, 
			String body)
		{
			    try
			    {
			    	java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			      Properties props = System.getProperties();
			      // -- Attaching to default Session, or we could start a new one --
			      props.put("mail.smtp.starttls.enable","true");
			      props.put("mail.smtp.host", smtpServer);
			      props.put("mail.smtp.auth", "true");
			      props.put("mail.transport.protocol", "smtp");
			     // props.put("mail.smtps.port", "465");
			     // props.put("mail.transport.protocol", "smtp");
			      //props.put("mail.smtp.port","587");
			      Authenticator auth = new SMTPAuthenticator();
			      Session session = Session.getDefaultInstance(props, auth);
			      // -- Create a new message --
			      Message msg = new MimeMessage(session);
			      // -- Set the FROM and TO fields --
			      msg.setFrom(new InternetAddress(from));
			 
			      msg.setRecipients(Message.RecipientType.TO,
			        InternetAddress.parse(to, false));
			      // -- We could include CC recipients too --
			      // if (cc != null)
			      // msg.setRecipients(Message.RecipientType.CC
			      // ,InternetAddress.parse(cc, false));
			      // -- Set the subject and body text --
			      msg.setSubject(subject);
			      msg.setText(body);
			      // -- Set some other header information --
			      msg.setHeader("X-Mailer", "Lakesidetechnical");
			      msg.setSentDate(new Date());
			      // -- Send the message --
			      
			      //Transport.send(msg);
			      
			      Transport t = session.getTransport();
			      try {
			   
			  	t.connect(smtpServer, to, pwd);
			  	t.sendMessage(msg, msg.getAllRecipients());
			      } finally {
			  	t.close();
			      }
			      
			      //System.out.println("Message sent OK.");
			    }
			    catch (Exception ex)
			    {
			      ex.printStackTrace();
			    }
			 
			  }
    private class SMTPAuthenticator extends javax.mail.Authenticator {
        public PasswordAuthentication getPasswordAuthentication() {
           String username = login;
           String password = pwd;
           return new PasswordAuthentication(username, password);
        }
    }
}

출처 : http://www.wowza.com/forums/showthread.php?6165-Detect-if-the-live-encoder-has-stopped.