Tutorials:Streaming from custom directories
From Red5Tutorials
An update of Joachim Bauch's HOWTO stream content to/from custom directories to include Niels Joubert's development to support completely custom directories.
Contents |
Preface
This document describes how applications can stream ondemand videos from or record to custom directories other than the default streams folder inside the webapp. It focuses on the specifics in the 0.6.3 release of Red5, which support absolute paths.
Streaming from custom directories using Relative vs Absolute paths
This class indicates whether it resolves to an absolute or relative path. A relative path is resolved to the webapp's base directory, while an absolute path can be resolved to any place on the filesystem. The value returned by resolvesToAbsolutePath() toggles Red5's behaviour. A false return value will cause Red5 to resolve the path returned by generateFilename as a relative path, and append the current webapp's absolute path to the beginning of it. A true return value will cause Red5 to interpret the given path as a full path to the file.
This example streams from absolute paths. To stream from relative paths, simply set the return value of resolvesToAbsolutePath() to false, and return a relative path, such as "streams/stream.flv" versus "C:/streams/stream.flv". Joachim's HOWTO handles relative path streaming.
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 that generates filenames for VOD streams that should be played or recorded. Custom generator
To generate filename in different folders, a new filename generator must be implemented. This tutorial creates a filename generator that streams from a directory that is not inside a webapps folder, but can be located on, for example, a distributed file system mounted as a specific drive. We will also write this in such a way that the root folder of streams can be set via an XML configuration file.
import org.red5.server.api.IScope;
import org.red5.server.api.ScopeUtils;
import org.red5.server.api.stream.IStreamFilenameGenerator;
public class CustomFilenameGenerator implements IStreamFilenameGenerator {
/** Path that will store recorded videos. */
public static String recordPath;
/** Path that contains VOD streams. */
public static String playbackPath;
private String getStreamDirectory(IScope scope) {
final StringBuilder result = new StringBuilder();
final IScope app = ScopeUtils.findApplication(scope);
while (scope != null && scope != app) {
result.insert(0, "/" + scope.getName());
scope = scope.getParent();
}
return playbackPath + result.toString();
}
public String generateFilename(IScope scope, String name, GenerationType type) {
return generateFilename(scope, name, null, type);
}
public String generateFilename(IScope scope, String name, String extension, GenerationType type) {
String filename;
filename = getStreamDirectory(scope) + name;
if (extension != null)
// Add extension
filename += extension;
return filename;
}
public boolean resolvesToAbsolutePath() {
return true;
}
public void setPlaybackPath(String path) {
playbackPath = path;
}
public void setRecordPath(String path) {
recordPath = path;
}
}
The above class will generate filenames for recorded streams like recordedStreams/red5RecordDemo1234.flv and use the directory videoStreams as source for all VOD streams. It depends on the playbackPath variable being set through bean injection to create streams from a root directory, and builds the part of the requested URL following the webapp folder into a path on disk. Thus, a request such as rtmp://server/webapp/f1/f2/f3/stream.flv will translate into playbackPath/f1/f2/f3/stream.flv
Activate custom generator & Configure paths
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="org.red5.server.webapp.nielsApp.NielsFilenameGenerator">
<property name="playbackPath">
<value>F:/my/streams/directory</value>
</property>
<property name="recordPath">
<value>F:/my/recorded/directory</value>
</property>
</bean>
This will use the class defined above to generate stream filenames. By modifying the playbackPath property, you can set any location on disk that should be used by this application to stream from.

