Code:Server filenamegenerator

From Red5Tutorials

Jump to: navigation, search

FileNameGenerator.java

import java.util.HashMap;

import org.slf4j.Logger; import org.slf4j.LoggerFactory;

import org.red5.server.api.IScope; import org.red5.server.api.stream.IStreamFilenameGenerator; import org.red5.server.api.stream.IStreamFilenameGenerator.GenerationType;

public class FileNameGenerator implements IStreamFilenameGenerator {

    /** Path that will store recorded videos. */
   public String recordPath = "streams/";
   /** Path that contains VOD streams. */
   public String playbackPath = "streams/";
   private boolean resolvesAbsolutePath = false;
  
   
   public String virtualDirectories;
   private String[] directories;
   private HashMap<String,String> vdirectories;
   private static final Logger log = (Logger)LoggerFactory.getLogger(FileNameGenerator.class.getName());
   
    public void setRecordPath(String path) {
         recordPath = path;
    }
    
    public void setPlaybackPath(String path) {
         playbackPath = path;
    }
   
    public void setAbsolutePath(Boolean absolute) {
         resolvesAbsolutePath = absolute;
    }
   // sets the virtual directories up on server startup
   
   public void setVirtualDirectories(String virtualDirectories) {
       vdirectories = new HashMap<String,String>();
       directories = virtualDirectories.split(",");
       for (int i = 0; i < directories.length; i++) {
            directories[i] = directories[i].trim();
            String[] paths = directories[i].split(";");
            
             if (!paths[0].equals("") && !paths[1].equals(""))
             {
                 vdirectories.put(paths[0], paths[1]);
             }
       }
   }
   
   public String generateFilename(IScope scope, String name,
           GenerationType type) {
       // Generate filename without an extension.
       return generateFilename(scope, name, null, type);
   }
   
   public String generateFilename(IScope scope, String name,
           String extension, GenerationType type) {
       String filename;
       
       filename = playbackPath + name;
       
       String[] paths = name.split("/");
       

       if ((vdirectories.size() > 0) && vdirectories.containsKey(paths[0]))
       {
       	filename =  vdirectories.get(paths[0]) + paths[1];
       }
       
       log.info("Generated FilePath: " + filename);
      return filename;
     }
     public boolean resolvesToAbsolutePath()
     {
   	return resolvesAbsolutePath;
     }
 }


red5-web.properties

webapp.contextPath=/demoapp 
webapp.virtualHosts=*, localhost, localhost:8088, 127.0.0.1:8088
recordPath=/path/to/streams/archive/
playbackPath=/path/to/streams/
virtualDirectories=vpath1;C:/path/to/videos/on/windows/, vpath2;///machinename/network/shared/drive/, vpath3;/path/to/video/on/unix/

absolutePath=true

red5-web.xml

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 <beans>
       <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
              <property name="location" value="/WEB-INF/red5-web.properties" />
       </bean>
       
       <bean id="web.context" class="org.red5.server.Context" autowire="byType" />
       
       <bean id="web.scope" class="org.red5.server.WebScope"
                init-method="register">
               <property name="server" ref="red5.server" />
               <property name="parent" ref="global.scope" />
               <property name="context" ref="web.context" />
               <property name="handler" ref="web.handler" />
               <property name="contextPath" value="${webapp.contextPath}" />
               <property name="virtualHosts" value="${webapp.virtualHosts}" />
        </bean>
    
    <bean id="web.handler" class="ApplicationAdaptor" singleton="true" />
 		
     <bean id="streamFilenameGenerator" class="FileNameGenerator">
           <property name="recordPath" value="${recordPath}" />
           <property name="playbackPath" value="${playbackPath}" />
           <property name="virtualDirectories" value="${virtualDirectories}" />
           <property name="absolutePath" value="${absolutePath}" />
      </bean>
 </beans>


Usage:

thefile.flv -> /path/to/streams/thefile.flv
/vpath1/thefile.flv -> C:\path\to\videos\on\windows\thefile.flv
/vpath2/thefile.flv -> \\machinename\network\shared\drive\thefile.flv
/vpath3/thefile.flv -> /path/to/video/on/unix/thefile.flv

Personal tools