Zeno's Paradox
July 30, 2003 - Useful Java Media Code

The following Java code is an example of some of the functionality we use within Oyez to determine whether a specific case has audio, rtspResourceExists (). I've also thrown in the code we use to calculate SHA hashes of the MP3 files as part of our collaboration with Creative Commons where we use their license metadata embedding methods.

First of all, these two functions "ping" RTSP servers to see if a given stream is present. One function does the pinging and the other serves as a public wrapper that caches the results of the ping so the RTSP server isn't hit more than necessary.

import java.io.*;
import java.net.*;
import java.security.*;
import java.util.*;

public class OyezAudioManager 
{
	public static int HAS_AUDIO = 0;
	public static int NO_AUDIO = 1;
	public static int DUNNO = 2;

	private static Hashtable h;
	private static Hashtable shaHash;

	private static boolean pingAudio (String host, String path) throws Exception
	{
		int port = 554;
		
		String request = "DESCRIBE rtsp://" + host + ../../" + path + " RTSP/1.0\n\n";
		
		Socket s = new Socket (host, port);
		
		OutputStream out = s.getOutputStream ();
		BufferedReader in = new BufferedReader (new InputStreamReader (s.getInputStream ()));
		out.write (request.getBytes ());

		String line = in.readLine ();
		
		while (line != null)
		{
			if (line.startsWith ("RTSP/1.0"))
			{
				if (line.indexOf ("200") != -1)
				{
					s.close();
					return true;
				}
				else
				{
					s.close ();
					return false;
				}
			}
			
			line = in.readLine ();
		}

		s.close ();		
		return false;
	}
	
	public static boolean rtspResourceExists (String host, String path) throws Exception
	{
		if (h == null)
		{
			h = new Hashtable ();
		}
		
		String audioStatus = (String) h.get (path);
		
		if (audioStatus == null)
		{
			boolean ping = pingAudio (host, path);
			
			if (ping)
			{
				h.put (path, "Yes");
				return true;
			}
			else
			{
				h.put (path, "No");
				return false;
			}
		}
		else if (audioStatus.equals ("Yes"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

It may not be the prettiest code, but it gets the job done. The next code is used to compute and cache the SHA fingerprints of Oyez MP3 files.

	public static String getShaUrn (String filename)
	{
		if (shaHash == null)
		{
			shaHash = new Hashtable ();
		}

		String hash = (String) shaHash.get (filename);
		
		if (hash == null)
		{
			hash = getSha (filename) ;
		
			shaHash.put (filename, hash);
		}
		
		return hash;
	}
	
	private static String getSha (String filename)
	{
		try
		{
	        MessageDigest sha1 = MessageDigest.getInstance("SHA");
	        FileInputStream fis = new FileInputStream(filename);
	        int read;
	        byte[] in = new byte[1024];
	        while ((read = fis.read(in)) > -1)
	        {
	            sha1.update(in, 0, read);
	        }
	        fis.close();
	        byte[] sha1digest = sha1.digest();
	        return "urn:sha1:" + Base32.encode(sha1digest);
	    }		
	    catch (Exception e)
	    {
	        return "urn:sha1:NOIDEAERROR";
	    }
	}
}
Posted by br284 at 03:11 PM

Trackbacks

Comments

Post a comment
Name:


Email Address:


URL:


Comments:




Note that by posting a comment on this site using this form or any other software tool, you have agreed to the advertising terms of this site and you agree to submit payment for any advertising in the comment above as outlined in the advertising terms. This applies to ads posted in the URL or Comment fields above.