Red5搭建直播平台

冷不防 2023-06-07 10:54 85阅读 0赞

下载地址 http://www.red5.org/

1, 首先启动red5

2,访问http://localhost:5080/

3,在该页面点击installer,进入安装页面。或输入http://localhost:5080/installer/

4,安装oflaDemo

5,可能会报错,下面来解决这些基本问题。

5.1,重新编译Application.java

  1. package org.red5.demos.oflaDemo;
  2. import org.red5.logging.Red5LoggerFactory;
  3. import org.red5.server.adapter.ApplicationAdapter;
  4. import org.red5.server.api.IConnection;
  5. import org.red5.server.api.IScope;
  6. import org.red5.server.api.stream.IServerStream;
  7. import org.red5.server.api.stream.IStreamCapableConnection;
  8. import org.slf4j.Logger;
  9. public class Application extends ApplicationAdapter
  10. {
  11. private static Logger log = Red5LoggerFactory.getLogger(Application.class, "oflaDemo");
  12. private IScope appScope;
  13. private IServerStream serverStream;
  14. public Application()
  15. {
  16. log.info("oflaDemo created");
  17. System.out.println("oflaDemo created");
  18. }
  19. public boolean appStart(IScope app)
  20. {
  21. log.info("oflaDemo appStart");
  22. System.out.println("oflaDemo appStart");
  23. this.appScope = app;
  24. return true;
  25. }
  26. public boolean appConnect(IConnection conn, Object[] params)
  27. {
  28. log.info("oflaDemo appConnect");
  29. measureBandwidth(conn);
  30. if ((conn instanceof IStreamCapableConnection)) {
  31. IStreamCapableConnection streamConn = (IStreamCapableConnection)conn;
  32. /**
  33. SimpleConnectionBWConfig bwConfig = new SimpleConnectionBWConfig();
  34. bwConfig.getChannelBandwidth()[3] = 1048576L;
  35. bwConfig.getChannelInitialBurst()[3] = 131072L;
  36. streamConn.setBandwidthConfigure(bwConfig);
  37. */
  38. }
  39. return super.appConnect(conn, params);
  40. }
  41. public void appDisconnect(IConnection conn)
  42. {
  43. log.info("oflaDemo appDisconnect");
  44. if ((this.appScope == conn.getScope()) && (this.serverStream != null)) {
  45. this.serverStream.close();
  46. }
  47. super.appDisconnect(conn);
  48. }
  49. }

5.2,增加所需要的jar文件spring-aop-3.0.5.RELEASE.jaraopalliance-1.0.jar

6,重启Red5,访问路径http://localhost:5080/demos/ofla_demo.html,点击Connect,右侧变绿,出现播放列表。选择播放文件。

7,使用jwplayer,进行测试,书写代码如下

  1. <script type="text/javascript" src="${pageContext.request.contextPath}/player/jwplayer.js"></script>
  2. <div id="myElement" >Loading the player...</div>
  3. jwplayer("myElement").setup({
  4. file: "rtmp://localhost/oflaDemo/9.flv",
  5. height: 360,
  6. image: "${pageContext.request.contextPath}/images/button.gif",
  7. width: 640
  8. });

正常播放则测试成功。

8,进行二次开发

8.1,先写Java类

  1. package first;
  2. import org.red5.server.adapter.ApplicationAdapter;
  3. import org.red5.server.api.IConnection;
  4. import org.red5.server.api.IScope;
  5. import org.red5.server.api.stream.IServerStream;
  6. import org.red5.server.api.stream.IStreamCapableConnection;
  7. public class Application extends ApplicationAdapter
  8. {
  9. private IScope appScope;
  10. private IServerStream serverStream;
  11. public Application()
  12. {
  13. }
  14. public boolean appStart(IScope app)
  15. {
  16. this.appScope = app;
  17. return true;
  18. }
  19. public boolean appConnect(IConnection conn, Object[] params)
  20. {
  21. measureBandwidth(conn);
  22. if ((conn instanceof IStreamCapableConnection)) {
  23. IStreamCapableConnection streamConn = (IStreamCapableConnection)conn;
  24. }
  25. return super.appConnect(conn, params);
  26. }
  27. public void appDisconnect(IConnection conn)
  28. {
  29. if ((this.appScope == conn.getScope()) && (this.serverStream != null)) {
  30. this.serverStream.close();
  31. }
  32. super.appDisconnect(conn);
  33. }
  34. }

8.2 配置red5-web.properties,内容如下

  1. webapp.contextPath=/red58
  2. webapp.virtualHosts=*

8.3 配置red5-web.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:lang="http://www.springframework.org/schema/lang"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6. http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
  7. <bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  8. <property name="location" value="/WEB-INF/red5-web.properties" />
  9. </bean>
  10. <bean id="web.context" class="org.red5.server.Context" autowire="byType" />
  11. <bean id="web.scope" class="org.red5.server.WebScope" init-method="register">
  12. <property name="server" ref="red5.server" />
  13. <property name="parent" ref="global.scope" />
  14. <property name="context" ref="web.context" />
  15. <property name="handler" ref="web.handler" />
  16. <property name="contextPath" value="${webapp.contextPath}" />
  17. <property name="virtualHosts" value="${webapp.virtualHosts}" />
  18. </bean>
  19. <bean id="web.handler" class="first.Application" />
  20. </beans>

8.4 配置web.xml

  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2. <web-app
  3. xmlns="http://java.sun.com/xml/ns/j2ee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  6. version="2.4">
  7. <display-name>red58</display-name>
  8. <context-param>
  9. <param-name>webAppRootKey</param-name>
  10. <param-value>/red58</param-value>
  11. </context-param>
  12. <servlet>
  13. <servlet-name>rtmpt</servlet-name> <servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
  14. <load-on-startup>1</load-on-startup>
  15. </servlet>
  16. <servlet-mapping>
  17. <servlet-name>rtmpt</servlet-name>
  18. <url-pattern>/fcs/*</url-pattern>
  19. </servlet-mapping>
  20. <servlet-mapping>
  21. <servlet-name>rtmpt</servlet-name>
  22. <url-pattern>/open/*</url-pattern>
  23. </servlet-mapping>
  24. <servlet-mapping>
  25. <servlet-name>rtmpt</servlet-name>
  26. <url-pattern>/close/*</url-pattern>
  27. </servlet-mapping>
  28. <servlet-mapping>
  29. <servlet-name>rtmpt</servlet-name>
  30. <url-pattern>/send/*</url-pattern>
  31. </servlet-mapping>
  32. <servlet-mapping>
  33. <servlet-name>rtmpt</servlet-name>
  34. <url-pattern>/idle/*</url-pattern>
  35. </servlet-mapping>
  36. <security-constraint>
  37. <web-resource-collection>
  38. <web-resource-name>Forbidden</web-resource-name>
  39. <url-pattern>/streams/*</url-pattern>
  40. </web-resource-collection>
  41. <auth-constraint/>
  42. </security-constraint>
  43. </web-app>

8.5 在WebContent\streams里放置flv等类型文件

8.6 发布文件,名称为red58

8.7,发布时,删除lib文件夹中的文件

8.8 进行测试。

  1. <script type="text/javascript" src="${pageContext.request.contextPath}/player/jwplayer.js"></script>
  2. <div id="myElement" >Loading the player...</div>
  3. jwplayer("myElement").setup({
  4. file: "rtmp://localhost/red58/9.flv",
  5. height: 360,
  6. image: "${pageContext.request.contextPath}/images/button.gif",
  7. width: 640
  8. });
  9. Js代码 收藏代码
  10. jwplayer().onBeforePlay( function(event){
  11. //alert("before Play");
  12. });
  13. jwplayer().onPlay( function(event){
  14. //alert(" Play");
  15. });
  16. jwplayer().onSeek( function(event){
  17. //alert("before Play");
  18. alert("seek--position:"+event.position +"---offset :"+event.offset );
  19. });
  20. jwplayer().onTime( function(event){
  21. //this event is fired as the playback position gets updated
  22. //alert("onTime--position:"+event.position +"---duration :"+event.duration );
  23. });

发表评论

表情:
评论列表 (有 0 条评论,85人围观)

还没有评论,来说两句吧...

相关阅读

    相关 直播平台

    前言:       相信很多小伙伴在日常开发中,都有遇到开发直播的需求,是不是感觉无从下手,如果你刚好看到这篇博客,那么你真的来对地方,本篇文章将详细的讲解,如果手把手的