《Red5 用户参考手册》之十:Red5 核心技术第三章 自定义流路径

来源:互联网 发布:影吧点播软件 编辑:程序博客网 时间:2024/05/01 01:31

转自:http://blog.csdn.net/defonds/article/details/7776311

官方最新《Red5 用户参考手册》全套下载地址
        本文介绍了如何使应用流化或录制按需视频(VOD)到指定目录,而不是默认的 webapp 下的 streams 目录。
        文件名生成器服务
        Red5 使用一个叫做域服务的概念为一个特定的域提供功能服务。这些域服务之一是 IStreamFilenameGeneratorhttp://dl.fancycode.com/red5/api/org/red5/server/api/stream/IStreamFilenameGenerator.html 生成可以播放和录制的 VOD 流的文件名。
        自定义生成器

        要在不同的文件里生成文件名,必须实现一个新的文件名生成器:

[java] view plaincopyprint?
  1. import org.red5.server.api.IScope;   
  2. import org.red5.server.api.stream.IStreamFilenameGenerator;   
  3. public class CustomFilenameGenerator implements IStreamFilenameGenerator {   
  4.  /** Path that will store recorded videos. */   
  5.  public String recordPath = "recordedStreams/";   
  6.  /** Path that contains VOD streams. */   
  7.  public String playbackPath = "videoStreams/";   
  8.  /** Set if the path is absolute or relative */   
  9.  public boolean resolvesAbsolutePath = false;   
  10.  public String generateFilename(IScope scope, String name, GenerationType type) {   
  11.   // Generate filename without an extension.   
  12.   return generateFilename(scope, name, null, type);   
  13.  }   
  14.  public String generateFilename(IScope scope, String name, String extension, GenerationType type) {   
  15.   String filename;   
  16.   if (type == GenerationType.RECORD)   
  17.    filename = recordPath + name;   
  18.   else   
  19.    filename = playbackPath + name;   
  20.     
  21.   if (extension != null)   
  22.    // Add extension   
  23.    filename += extension;   
  24.     
  25.   return filename;   
  26.  }   
  27.    
  28.  public boolean resolvesToAbsolutePath()   
  29.     {   
  30.      return resolvesAbsolutePath;   
  31.     }   
  32. }   

        以上类将会为录制的流生成类似 recordedStreams/ red5RecordDemo1234.flv 的文件名,并使用 videoStreams 目录作为所有 VOD 流的源。
        激活自定义生成器
        下一步,自定义生成器必须得在配置文件中为所需的应用程序激活。

        将以下定义添加到 yourApp/WEB-INF/red5-web.xml:

[html] view plaincopyprint?
  1. <bean id="streamFilenameGenerator"   
  2.     class="path.to.your.CustomFilenameGenerator" />   

        这将使用以上定义的类来生成流的文件名。
        通过配置修改存放路径
        当以上所述的类按预期工作时,如若修改代码里定义的路径不是太方便,因为每一次改变都需要对它进行重新编译。
        所以你可以在上一步配置文件里里定义的 bean 里传入参数来定义用到的路径。

        添加三个方法到你的类,这些方法会在配置文件被解析时执行:

[java] view plaincopyprint?
  1. public void setRecordPath(String path) {   
  2.  recordPath = path;   
  3. }   
  4. public void setPlaybackPath(String path) {   
  5.  playbackPath = path;   
  6. }   
  7. public void setAbsolutePath(Boolean absolute) {   
  8.  resolvesAbsolutePath = absolute;   
  9. }   

        现在你可以在 bean 定义里设置路径了:

[html] view plaincopyprint?
  1. <bean id="streamFilenameGenerator"   
  2.    class="path.to.your.CustomFilenameGenerator">   
  3.    <property name="recordPath" value="recordedStreams/" />   
  4.    <property name="playbackPath" value="videoStreams/" />   
  5.    <property name="absolutePath" value="false" />   
  6. </bean>   
  7. <bean id="streamFilenameGenerator"   
  8.    class="path.to.your.CustomFilenameGenerator">   
  9.    <property name="recordPath" value="/path/to/recordedStreams/" />   
  10.    <property name="playbackPath" value="/path/to/videoStreams/" />   
  11.    <property name="absolutePath" value="true" />   
  12. </bean>   

        你也可以把路径放到 yourApp/WEB-INF/red5-web.properties 文件里并使用参数对它们进行访问:

[html] view plaincopyprint?
  1. <bean id="streamFilenameGenerator"   
  2.    class="path.to.your.CustomFilenameGenerator">   
  3.    <property name="recordPath" value="${recordPath}" />   
  4.    <property name="playbackPath" value="${playbackPath}" />   
  5.    <property name="absolutePath" value="${absolutePath}" />   
  6. </bean>   

        这时你需要把以下行添加到你的 properties 文件(red5-web.properties):

[plain] view plaincopyprint?
  1. recordPath=recordedStreams/   
  2. playbackPath=videoStreams/   
  3. absolutePath=false   
  4. recordPath=/path/to/recordedStreams/   
  5. playbackPath=/path/to/videoStreams/   
  6. absolutePath=true   

原文链接:http://trac.red5.org/wiki/Documentation/UsersReferenceManual/Red5CoreTechnologies/03-Customize-Stream-Paths
原创粉丝点击