[vlma-devel] commit: Add a watcher to control the availability of VoD streams. ( Adrien Maglo )

git version control git at videolan.org
Thu Oct 9 23:09:33 CEST 2008


vlma | branch: master | Adrien Maglo <magsoft at videolan.org> | Thu Oct  9 22:43:40 2008 +0200| [2bafa3e638d9ecda19f915033bda81ea5e049e61] | committer: Adrien Maglo 

Add a watcher to control the availability of VoD streams.

> http://git.videolan.org/gitweb.cgi/vlma.git/?a=commit;h=2bafa3e638d9ecda19f915033bda81ea5e049e61
---

 .../videolan/vlma/watcher/VodStreamWatcher.java    |  177 ++++++++++++++++++++
 1 files changed, 177 insertions(+), 0 deletions(-)

diff --git a/vlma-core/src/main/java/org/videolan/vlma/watcher/VodStreamWatcher.java b/vlma-core/src/main/java/org/videolan/vlma/watcher/VodStreamWatcher.java
new file mode 100644
index 0000000..3cfccb9
--- /dev/null
+++ b/vlma-core/src/main/java/org/videolan/vlma/watcher/VodStreamWatcher.java
@@ -0,0 +1,177 @@
+/*
+ * Copyright (C) 2006-2008 the VideoLAN team
+*
+* This file is part of VLMa.
+*
+* VLMa is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 2 of the License, or
+* (at your option) any later version.
+*
+* VLMa is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with VLMa. If not, see <http://www.gnu.org/licenses/>.
+*
+*/
+
+package org.videolan.vlma.watcher;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.net.Socket;
+import java.net.UnknownHostException;
+
+import org.apache.commons.configuration.Configuration;
+import org.apache.log4j.Logger;
+import org.videolan.vlma.model.Media;
+import org.videolan.vlma.model.Program;
+
+
+/**
+ * This class monitors VoD streams. It should never be used directly. Use
+ * {@link StreamWatcherDispatcher} instead.
+ *
+ * @author Adrien Maglo <magsoft at videolan.org>
+ */
+public class VodStreamWatcher  implements StreamWatcher {
+	
+    private Configuration configuration;
+    
+    private static final Logger logger = Logger.getLogger(DirectMulticastStreamWatcher.class);
+	
+    /**
+     * The thread used to wait.
+     */
+    private Thread waitThread;
+
+    /**
+     * The time leaved to the socket to get RTSP data.
+     */
+    private static int WAIT_SPAN = 500;
+
+    /**
+     * The socket used to connect to the RTSP server
+     */
+    private Socket mySocket = null;
+
+    /**
+     * Check if the media is played or not by asking the RTSP server
+     */
+	public boolean isPlayed(Media media) {
+    	Program program = media.getProgram();
+    	Integer port = configuration.getInt("vlma.streaming.rtsp.port");
+    	
+    	if(program.getPlayer() == null)
+    		return false;
+
+		boolean isPlayed = false;
+		String url = new String("rtsp://" + program.getPlayer().getHostAddress()
+								+ ":" + port + "/flux-"
+								+ program.getAdapterName() + media.getId());
+		
+		PrintWriter out = null;
+		BufferedReader in = null;
+
+		try {
+			mySocket = new Socket(program.getPlayer().getHostAddress(), port);
+			out = new PrintWriter(mySocket.getOutputStream(), true);
+		    in = new BufferedReader(new InputStreamReader(
+		    		mySocket.getInputStream()));
+		}
+		catch (UnknownHostException e) {
+	        logger.debug("Couldn't contact the VoD server.");
+			return false;
+		}
+		catch (IOException e) {
+	        logger.debug("Couldn't contact the VoD server.");
+			return false;
+		}
+		
+		out.println("DESCRIBE " + url + " RTSP/1.0");
+		out.println(""); /* Empty line (RTSP protocol) */
+
+        /* Check server answer */
+        startWaitingForTheSocket();
+		while( true )
+		{
+			/* The waiter thread allow to exit this loop by closing the socket */
+			String line;
+			try {
+				line = in.readLine();
+			} catch (IOException e) {
+				break;
+			}
+			
+			if( line == null )
+				break;
+			/* If the RTSP server talks about the media URL, then it must be able
+			to broadcast it.
+			Maybe we could do something better ... */
+			else if( line.contains(url) )
+			{
+				isPlayed = true;
+				break;
+			}
+		}
+
+		/* Close the socket */
+		try {
+			mySocket.close();
+		} catch (IOException e) {
+			/* Do nothing */
+		}
+		
+		/* Some log output */
+		if( isPlayed )
+	        logger.debug("The VoD media " + media.getName() + "is available.");
+		else
+	        logger.debug("The VoD media " + media.getName() + "is NOT available.");
+		
+		/* Stop the thread if it hasn't already be done */
+		waitThread.interrupt();
+
+		return isPlayed;
+	}
+
+    /**
+     * This wait WAIT_SPAN milliseconds for the socket to receive
+     * RTSP data
+     */
+    private Runnable waiter = new Runnable() {
+        public void run() {
+            /* We start waiting */
+            try {
+                Thread.sleep(WAIT_SPAN);
+                /* Now the socket must stop receiving data */
+                try {
+					mySocket.close();
+				} catch (IOException e) {
+					/* Do nothing */
+				}
+            } catch (InterruptedException e) { }
+        }
+    };
+
+    /**
+     * Start the waiting thread
+     */
+    private synchronized void startWaitingForTheSocket() {
+        waitThread = new Thread(waiter);
+        waitThread.setName("waitThread");
+        waitThread.start();
+    }
+    
+    /**
+     * @param configuration the configuration to set
+     */
+    public void setConfiguration(Configuration configuration) {
+        this.configuration = configuration;
+    }
+}
+



More information about the vlma-devel mailing list