## Chapter 12. Customize Stream Paths

This document describes how applications can stream ondemand videos (VOD) from or record to custom directories other than the default streams folder inside the webapp.

## 12.1. Filename generator service

Red5 uses a concept called scope services for functionality that is provided for a certain scope. One of these scope services is IStreamFilenameGenerator http://dl.fancycode.com/red5/api/org/red5/server/api/stream/IStreamFilenameGenerator.html that generates filenames for VOD streams that should be played or recorded.

## 12.2. Custom generator

To generate filename in different folders, a new filename generator must be implemented:

    import org.red5.server.api.IScope;
    import org.red5.server.api.stream.IStreamFilenameGenerator;
    public class CustomFilenameGenerator implements IStreamFilenameGenerator {
     /** Path that will store recorded videos. */
     public String recordPath = "recordedStreams/";
     /** Path that contains VOD streams. */
     public String playbackPath = "videoStreams/";
     /** Set if the path is absolute or relative */
     public boolean resolvesAbsolutePath = false;
     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;
      if (type == GenerationType.RECORD)
       filename = recordPath + name;
      else
       filename = playbackPath + name;

      if (extension != null)
       // Add extension
       filename += extension;

      return filename;
     }

     public boolean resolvesToAbsolutePath()
        {
         return resolvesAbsolutePath;
        }
    }

The above class will generate filenames for recorded streams like recordedStreams/ red5RecordDemo1234.flv and use the directory videoStreams as source for all VOD streams.

## 12.3. Activate custom generator

In the next step, the custom generator must be activate in the configuration files for the desired application.

Add the following definition to yourApp/WEB-INF/red5-web.xml:

    <bean id="streamFilenameGenerator"
        class="path.to.your.CustomFilenameGenerator" />

This will use the class defined above to generate stream filenames.

## 12.4. Change paths through configuration

While the class described here works as expected, it's a bit unhandy to change the paths inside the code as every change requires recompilation of the class.

Therefore you can pass parameters to the bean defined in the previous step to specify the paths to use inside the configuration file.

Add three methods to your class that will be executed while the configuration file is parsed:

    public void setRecordPath(String path) {
     recordPath = path;
    }
    public void setPlaybackPath(String path) {
     playbackPath = path;
    }
    public void setAbsolutePath(Boolean absolute) {
     resolvesAbsolutePath = absolute;
    }

Now you can set the paths inside the bean definition:

    <bean id="streamFilenameGenerator"
       class="path.to.your.CustomFilenameGenerator">
       <property name="recordPath" value="recordedStreams/" />
       <property name="playbackPath" value="videoStreams/" />
       <property name="absolutePath" value="false" />
    </bean>
    <bean id="streamFilenameGenerator"
       class="path.to.your.CustomFilenameGenerator">
       <property name="recordPath" value="/path/to/recordedStreams/" />
       <property name="playbackPath" value="/path/to/videoStreams/" />
       <property name="absolutePath" value="true" />
    </bean>

You can also move the paths to the yourApp/WEB-INF/red5-web.properties file and use parameters to access them:

    <bean id="streamFilenameGenerator"
       class="path.to.your.CustomFilenameGenerator">
       <property name="recordPath" value="${recordPath}" />
       <property name="playbackPath" value="${playbackPath}" />
       <property name="absolutePath" value="${absolutePath}" />
    </bean>

In that case you will have to add the following lines to your properties file:

red5-web.properties -

recordPath=recordedStreams/
playbackPath=videoStreams/
absolutePath=false
recordPath=/path/to/recordedStreams/
playbackPath=/path/to/videoStreams/
absolutePath=true

Prev | Home | Next
Chapter 11. Deploying Red5 To Tomcat | Red5 Open Source Flash Server | Chapter 13. Security