The construction and application of streaming media server based on Red5

Source: Internet
Author: User
Tags live chat

http://www.ibm.com/developerworks/cn/opensource/os-cn-Red5/

Red5 is an open source free Flash streaming media server with Java development. Red5 is based on Java and some powerful open source frameworks that lay the standard for enterprise-class applications. It uses the Rtmp,rtmpt,rtmps and Rtmpe streaming protocols to support: Convert audio (MP3) and video (FLV,MP4,F4V,3GP) to stream, record client stream, share object, live stream release, Remote call. RED5 provides a simple and easy solution for instant messaging, distance education, streaming media development, multiplayer online games, and more. Through the introduction of RED5, this article lets the reader understand how to use Red5 to quickly build a streaming media server and how to develop and deploy corresponding streaming media applications on the server.

0 Reviews:

Tang Copo, software engineer, IBM

Rangau, software engineer, IBM

March 26, 2015

    • Content

Develop and deploy your next application on the IBM Bluemix cloud platform.

Get started with your trial

Introduction

Streaming media files are one of the most popular network media formats, which allow users to play while downloading, greatly reducing the time users wait to play. In addition, when streaming media files are played over the network, the files themselves are not stored on the local disk, which saves a lot of disk space overhead. It is these advantages that make streaming media files widely used in network playback.

The streaming media server publishes streaming content and manages user connections by establishing a publishing point. Streaming media servers can publish live streams from devices such as video capture cards or camcorders, or they can publish pre-stored streaming media files and publish a combination of live streaming and streaming media files. A media stream can consist of a single media file, or it can be composed of multiple media files, or it can consist of a single media file directory.

Flash Media Server, or FMS, is a member of the Flash family, known as the most convenient and fast solution for developing WEB audio and video applications such as video chat rooms, video conferencing systems, podcast systems, audio and video messages, etc. Also in Web application development has more and more extensive application. Of course, this excellent function of FMS is not free to the vast number of users, $ thousands of per point price so many developers deterred. As a result, we began to try to find a more appropriate solution, open-source Red5 has become a new favorite of people.

Red5 is a Java-developed open source Flash streaming media server, similar in function to FMS. It supports: Convert audio (MP3) and video (FLV) to play stream, record client stream (FLV only), share object, live stream release, Remote call. Red5 uses RSTP as a streaming media transfer protocol, including online recording, Flash streaming, live chat, video conferencing and other basic functions. Red5-based streaming media applications at home and abroad are also being promoted, such as online video conferencing openmeeting, the king of the large Flash online games Kung Fu 2.

Back to top of page

Introduction to Streaming media

With the development of the Internet, simple text and graphics are far from enough to meet the needs, but gradually to the audio and video and other multimedia direction transition. At present, the transmission of audio/video and other multimedia files on the network, there are generally two options for downloading and streaming. Because audio/video contains large amounts of data, they occupy a large amount of storage space, which can take several minutes or even hours to download in a bandwidth-constrained network environment, so the latency of this approach is significant. If the exchange of streaming, audio, video, animation and other multimedia files will be dedicated to the user stream Media server in a continuous, real-time transmission, so that users do not have to wait until the entire file download complete, and only after a few seconds of the startup delay, when these multimedia data on the client computer playback, The remainder of the file continues to be downloaded from the streaming media server.

Stream (streaming) is a new concept appearing on the internet in recent years, its definition is very broad, mainly refers to the technology of transmitting multimedia data through the network. Streaming media contains both generalized and narrow meanings: The generalized streaming media refers to a series of technologies, methods and protocols that make the audio and video form stable and continuous stream and return streams, i.e. streaming media technology; In the narrow sense, the streaming media is relative to the traditional download-playback mode, which refers to a kind of Internet The new method of acquiring multimedia data such as audio and video, which can support the real-time transmission and real-time playback of multimedia data stream. By using streaming media technology, the server can send stable and continuous multimedia data stream to the client, and the client will replay it at a stable rate while receiving the data, without waiting for the data to be downloaded and then replayed.

Due to network bandwidth, computer processing power and protocol specifications, to download a large number of audio and video data from the Internet, no matter from the time of downloading and storage space is not very realistic, and the advent of streaming media technology is a good solution to this problem. There are two main ways to realize streaming media transmission: Sequential Stream (progressive streaming) transmission and real-time streaming (realtime streaming), which are suitable for different application situations.

Sequential Stream Transfer

Sequential stream transmission is carried out in sequential download, while the user can play back the multimedia data at the same time, but at a given moment only the part that has been downloaded cannot be skipped to the part that has not yet been downloaded, and the download speed can not be adjusted during transmission according to the network condition. Because the standard HTTP server can send this form of streaming media without the support of other special protocols, it is also often referred to as an HTTP streaming. Sequential streaming is more suitable for high-quality multimedia fragments, such as titles, trailers, or advertisements.

Live Stream Transfer

Real-time streaming ensures that the media signal bandwidth is matched to the current network conditions, so that streaming media data is always transmitted in real time, making it particularly suitable for field events. Real-time streaming supports random access, which means that users can view front or back content through fast forward or backward operations. In theory, real-time streaming will not pause once it is played, but it is still possible to have periodic pauses, especially when the network is deteriorating. Unlike sequential streaming, real-time streaming requires the use of specific streaming media servers, and also requires support for specific network protocols.

Back to top of page

Red5 Overview

Red5 is an open source Flash streaming media server with Java development. Free open source makes the software easier to expand, you can modify the source code after downloading, more economical, compared to the high cost of FMS, Red5 can save a large cost for the general application, while the server-side Java object-oriented language is more mature than the ActionScript2 language of FMS server. In view of the various advantages of RED5, the introduction is soon accepted by the majority of users.

Red 5 supports:

1. Convert audio (MP3) and video (FLV, F4V, MP4, 3GP) to play stream;

2. Record the client stream and save the incoming audio and video recording of the camera, microphone, etc. to the server;

3. Shared objects;

4. Live stream Publishing;

5. Remote invocation;

6. Protocol: RTMP, Rtmpt, Rtmps, and Rtmpe.

RED5 provides a number of interfaces for developers to develop two times.

Table 1.red5 Common Interfaces
Interface Description
Iconnection The Connection object. Each connection has an associated client and domain. The connection may be continuous, polling, or transient. The purpose of this interface is to provide a common method for different subclasses, such as Rtmpconnection,remotingconnection,httpconnection. The Getclient () method provided by this interface provides a convenient way to obtain the client side object.
Iscope Each RED5 application has at least one domain that is used to build a connection between the processor, the ring view, and the server. Domains can form a tree structure, and all clients can act as objects within their nodes ' shared domains (such as streams and data). All customer service (client) connections are connected to the domain via a connection (connection). For a single domain, each connection corresponds to a client, each client corresponding to an ID, a simple application, the operation for an ID and a connection.
Iservicecapableconnection Gets a valid connection. The code obtains the connection instance first, then determines whether it is a valid connection and enforces the type conversion, and then fetches the client's corresponding function.
IClient The client object represents a single client. A client can establish a connection with a different domain under the same host. Client objects are similar to HTTPSession. You can use the Iclientregistry.newclient (object[]) method to create IClient objects.
Applicationadapter Applicationadapter is an application-level iscope. To handle a flow process, you implement the appropriate processing method in the Istreamawarescopehandler interface. Applicationadapter also provides an effective event handling mechanism for intercepting streams, confirming users, and so on. At the same time, the methods introduced in its subclasses can be netconnection by the client. In the Aodbe FMS, client objects must be maintained on the server side, and the RED5 provides a more convenient and efficient way for your remote requests.

Back to top of page

RED5 Installation Preparation work:

One, JAVA environment configuration: Download the JDK installation package, this article selected is Jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe.

Second, confirm your installation environment and select the RED5 installation package: If it is a Windows operating system, you can download the corresponding installation file Red5 0.9 RC2 (this version used in writing this article), if you need to download the latest version or learn about Red5 other relevant information, you can refer to this page https:// Github.com/red5/red5-server.
Preparations are complete and everything is ready.

Installation work:

Step One: First install the JDK installation package downloaded above (Note: If you first install RED5, you will be prompted not to find the JDK, you must complete the steps to continue the following actions). Install the JDK first, you must remember the installation path, keep the default, the default is C:\Program files\java\jdk1.6.0_10, and then install the JRE, leave the default, the default is C:\Program files\java\jre6. After the installation is complete, you must configure the environment variables, my Computer –> Properties –> Advanced tab –> Environment variables (n) –> Locate the system variable (S) under new (W), and then enter the variable name (n): java_home, Variable Value (V): above JDK installation path \ c \ Program files\java\jdk1.6.0_10, OK, JDK ready.

Step Two: Install the Red5, install the RED5, will be required to enter the network can access IP, such as 121.52.209.62, and then enter the port to access, typically 5080, the final installation is complete.

Back to top of page

Red5 Simple example of application

After the above wonderful introduction, I believe that you will be interested in Red5, here I give you a simple application example, only the login process as an example, to help you better understand some of the concepts and models in RED5. In this application example, the Server side is written in Java based on the Red5 0.9 RC2, while the client side uses Action Script.

Figure 1. Project structure

Because Red5 is running in Tomcat, the RED5 project is similar to the normal Java EE project structure. The difference is that the Red5 configuration file needs to be prefixed with the "red5-" prefix, when Tomcat starts, all "red5-" is defaulted to the Red5 Server's profile without being loaded by Tomcat, and the corresponding configuration file is loaded when the Red5 is started. Let's take a look at the parts of Red5-web.xml.

Listing 1. Red5-web.xml in the context section
<bean id= "Web.context" class= "Org.red5.server.Context" autowire= "Bytype" ><property name= "Scoperesolver" ref= "Red5.scoperesolver"/><property name= "Serviceinvoker" ref= "Global.serviceinvoker"/><property name = "Mappingstrategy" ref= "Global.mappingstrategy"/><property name= "Clientregistry" ref= "IloveClientRegistry"/ ></bean>

The first three items in Listing 1 property are the default properties of the Red5 project. The fourth item, "Clientregistry", is a custom Context for the project, which is primarily used to implement user registration functions.

Listing 2. Red5-web.xml in the Scope section
<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>

The default values for Scope in the RED5 project are defined in Listing 2. If necessary, you can add your own defined Scope and assign it a value.

Listing 3. Handler part of Red5-web.xml
<bean id= "Web.handler" class= "org.red5.server.webapps.Ilove.Application" ><property name= "Userinfoservice "Ref=" Userinfoservice "/></bean>

Listing 3 overloads the Web.handler, so org.red5.server.webapps.Ilove.Application will be used to process client requests when there is a client connection request with the Server.

Next we look at how to implement the method of invoking the Red5 application from the Flash client and how to use Scope to determine the user's login status.

Figure 2. Client Login Figure 3. Login interface

Figure 2 shows how ActionScript handles different Connection states in the Flash client. The login request initiated by the Flash client needs to first determine the current Connection state with the server, and the method "can be called to the server side only if the status is" Netconnection.connect,sucess " Logicprocess ". Figure 3 shows a hint from the Flash client when different Connection states are present.

Listing 4. Logininterface
Public interface Logininterface extends Istreamawarescopehandler {...//user logon when the server side calls the current connection method public String Loginprocess (string[] args) throws Filenotfoundexception,parserconfigurationexception, Saxexception, IOException;// When the user logs on, the server side error calls the current connection method public void Systemerror ();/** the client calls the current connection method while the user is logged out *  * @param userName user name to be removed */public void Logoutprocess (String userName); ......}

As shown in Listing 4, Org.red5.server.webapps.Ilove.Application implements the Logininterface interface, which defines some of the client-visible method names. Because org.red5.server.webapps.Ilove.Application is defined as the Handler of the project, all public methods in the class can be called by the client.

Listing 5. User Login
Public String loginprocess (string[] args) throws Filenotfoundexception,parserconfigurationexception, Saxexception, IOException {final Iconnection conn = red5.getconnectionlocal (); if (conn.getclient () = = null) return Systemcontent.no_ Entry;final Iscope scope = Conn.getscope (); final IClient client = Conn.getclient (); final int code = (Integer) client.getat Tribute ("codename");//Check for current login conflict if (code = = Systemcontent.status_code_duplicateuser) {//This user name is logged in, notify client does not allow duplicate login if ( Model.statusMap.get (Args[0])) return systemcontent.no_entry;//If no user is logged in, the session value in the update database is the current user else { This.userInfoService.updateSession (Args[0], (String) Client.getattribute ("CurrentID"));}} If verified, the client should be ilovestreamclient type if (client instanceof Ilovestreamclient) {Boolean authenticated = ((Ilovestream Client). IsAuthenticated (); Current validation status if (authenticated) {synchronized (Model.statusmap) {model.statusMap.put (args[0], true); Refreshuserlist (conn , Args[0], "refresh"), Refreshuserlist (conn, args[0], "add"); InvokeclienTMethod (conn, "DECLAREPUBLICSO", new object[] {"CHATINGSO"}, NULL); Messageadapter.sendpublicmessage (So, "Welcome to login! "+ args[0]," system Robot "); final String schedulename = This.addscheduledjob (5000,new iloveuserlistchecker (conn, args[0])); Scope.setattribute (String) client.getattribute ("CurrentID"), schedulename); return args[0] + "found";}} return args[0] + "not Found";}

As shown in Listing 5, the program first obtains the current user's credential through the Red5 Globalscope, because each Connection exists independently in the Scope. The logon process will determine if the current user is repeatedly logged on, or if the logon information is correct. The server side calls the Flash client's method only when the user is in the authenticated state, and generates a shared Object to help the Flash client initialize the interface.

回页首

Red5 应用的部署

Red5 0.9 RC2 中已经内嵌了 Tomcat 服务器。因此 Red5 应用的部署和普通 JavaEE 应用在 Tomcat 中的部署方式是一样的。这里需要注意的是,上文中提到的 XML 配置文件必须是正确的,以防止运行报错。图 4 中是一个基于 Red5 应用的视频聊天系统,当用户登录成功后,可以在当前的聊天室中与其他已登录用户进行文字交谈或者视频聊天。具体功能的实现就不在本文中阐述了。

图 4. 视频聊天系统界面

Back to top of page

Conclusion

Red5 is an open source Flash streaming media server with Java development. Red5 also has the advantages of flex, Java, and a stronger advantage than flex. RED5 provides a simple and easy solution for instant messaging, distance education, streaming media development, online games, and more. By introducing Red5, this article gives readers an idea of how to use Red5 to quickly build a streaming media server and how to quickly develop and deploy appropriate streaming media applications on the server.

Reference Learning
    • Red5 source code download, from GitHub to get Red5.
    • ActionScript, refer to Adobe Web pages for an in-depth look at ActionScript3.0 features.
    • DeveloperWorks Open Source Technology topic: Find rich operational information, tools, and project updates to help you master open source technologies and use them with IBM products.
Discuss
    • Join the DeveloperWorks Chinese community to see developer-driven blogs, forums, groups, and wikis, and communicate with other DeveloperWorks users.

The construction and application of streaming media server based on Red5

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.