SteamStream!WEB-INF!src!steamStream!SsPlaybackSecurity!!java

From Red5Wiki

Jump to: navigation, search
/*
 * SteamStream - Red5 stream transcoding application
 * Copyright (c) 2009 by Jeremy Morton - All rights reserved.
 * 
 * This application is free software; you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any later version.
 * Text of this license version 2.1 can be found at the following web address:
 * http://www.gnu.org/licenses/lgpl-2.1.txt
 */
package steamStream;
 
import java.io.File;
import java.io.IOException;
 
import org.red5.logging.Red5LoggerFactory;
import org.red5.server.api.IConnection;
import org.red5.server.api.IScope;
import org.red5.server.api.Red5;
import org.red5.server.api.stream.IStreamPlaybackSecurity;
import org.slf4j.Logger;
 
public class SsPlaybackSecurity implements IStreamPlaybackSecurity {
	// SteamStream playback security checker class
	private static Logger log = Red5LoggerFactory.getLogger(Application.class, "steamStream");
 
	@Override
	public boolean isPlaybackAllowed(IScope scope, String name, int start, int length, boolean flushPlaylist) {
		String errorStr = "";
 
		log.info("SsPlaybackSecurity isPlaybackAllowed() called.  name is: " + name);
 
		IConnection conn = Red5.getConnectionLocal();
 
		// Check whether we think the stream name is valid; does it consist of valid characters?
		if (!java.util.regex.Pattern.matches("^[a-zA-Z0-9\\-]+$", name)) {
			errorStr = "Denying access to stream because stream name (" + name + ") contains invalid character(s).  Must contain only a-z, A-Z, 0-9, and dash (-).";
			log.warn(errorStr + "  Scope: " + scope.toString());
			conn.setAttribute("lastPlaybackAllowedError", errorStr);
			return false;
		}
 
		// Can we find an SDP file associated with the specified input stream name?
		File sdpFile = null;
 
		try {
			sdpFile = scope.getContext().getResource("sdpdescriptors/" + name + ".sdp").getFile();
		} catch (IOException ex) {
			errorStr = "I/O exception trying to get SDP file: " + ex.toString();
			log.error(errorStr + " (Scope: " + scope.toString() + ")");
			conn.setAttribute("lastPlaybackAllowedError", errorStr);
			return false;
		}
		if (!sdpFile.exists()) {
			// We couldn't find the associated SDP file.  Deny access to stream.
			errorStr = "Denying access to stream because we couldn't find the associated stream SDL file (" + name + ".sdp).";
			log.warn(errorStr + "  Scope: " + scope.toString());
			conn.setAttribute("lastPlaybackAllowedError", errorStr);
			return false;
		}
 
		// All is OK... ALLOW playback (you return false to DENY playback).
		conn.setAttribute("connectedToStream", name);
		return true;
	}
}
Personal tools