The simplest example of flash-based streaming media: rtmp push and receive (ActionScript)

Source: Internet
Author: User
Tags addchild wowza server flv file

This article records some examples of flash-based streaming media processing. The most common streaming media protocol for the Flash platform is rtmp. Some of the previously documented RTMP players/boosters are based on C + +, but no rtmp player/ejector based on ActionScript in Flash was recorded. In fact, the RTMP player/ejector based on Flash is a "regular" in rtmp technology. RTMP itself is designed to communicate between the Flash platform, and the biggest advantage of rtmp-"no plug-in live", but also benefit from the widely installed on the client Flash Player. Therefore, this paper records an ActionScript-based rtmp player and an ActionScript-based rtmp streaming device, respectively.

Examples of rtmp streaming media processing based on C/+ + can be found in the following sections.

Release

Simplest LIBRTMP-based example: release of H. (H. rtmp Post)

Simplest LIBRTMP-based example: Publish (FLV is released via RTMP)

The simplest ffmpeg-based push-to-stream (for example, to push rtmp)

Receive

Simplest LIBRTMP-based example: Receive (rtmp saved as FLV)

The simplest ffmpeg+sdl-based video player Ver2 (with SDL2.0)


Brief introduction

The use of ActionScript to process rtmp is very simple compared to using a/C + + processing rtmp. Rtmp's method of establishing a connection is already encapsulated, just call the ready-made interface function. But the disadvantage of using ActionScript to handle rtmp is also obvious-there are few places for yourself to develop. Since flash itself is not open source, we cannot get its underlying code, and therefore cannot adjust the underlying parameters of the codec. In summary, ActionScript handles rtmp in a few words: "simple but inflexible."

ActionScript plays rtmp

The flow of rtmp streaming media as shown in ActionScript.


As you can see, the process can be divided into two parts: playback and display.

Play

Playback is divided into 3 steps:

(1) Establishing netconnection
(2) Establishing NetStream
(3) Call NetStream's Play () method

The first 2 steps set up two logical structures in the RTMP specification: Netconnection and NetStream. Netconnection represents the underlying connectivity between the server-side application and the client. NetStream represents a channel for sending multimedia data. Only one netconnection can be established between the server and the client, but many NetStream are created based on the connection. The structure of these two structures is as shown.


Show

The Display section displays the video that is played on the stage. This section is implemented by creating a video object.


ActionScript Push rtmp

The process for ActionScript to push rtmp streaming is as shown in.


As you can see, the process and playback of the push rtmp are somewhat similar, the main difference being that the push is the last call to the NetStream publish () method, and the last call to play is the NetStream Play () method. The push stream is divided into 4 steps:
(1) Establishing netconnection
(2) Establishing NetStream
(3) Bind the camera and microphone
(4) Call NetStream's Play () method
Once the push-flow program has started running, you can view the streaming media through the FFPLAY,VLC or Flash application to access the appropriate rtmp URL.

Code

The following 2 ActionScript projects are included in the annex to this article:

simplest AS3 rtmp player, the simplest rtmp player, which contains 3 separate sub-projects:
Simplest_as3_rtmp_player: The simplest rtmp player.
Simplest_as3_local_player: The simplest local file player.
Simplest_as3_rtmp_player_multiscreen: The simplest rtmp multi-screen player.
Simplest_as3_rtmp_streamer, the simplest rtmp push-flow device
Here's a look at the source code for several of these projects.


Simplest_as3_rtmp_player

Simplest_as3_rtmp_player is the simplest rtmp player, as shown in the code below.


/** * Simplest ActionScript-based rtmp player * Simplest AS3 rtmp player * * Lei hua Lei Xiaohua * [email protected] * Communication University/Digital TV Technology * Communication University of China/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program uses ACTIONSCRIPT3 language Complete, play streaming media on the rtmp server * is the simplest ACTIONSCRIPT3 based player. * * This software was written in Actionscript3, it plays stream * on RTMP server * It's the simplest RTMP player based on A CtionScript3.    * */package {import flash.display.Sprite;    Import flash.net.NetConnection;    Import flash.events.NetStatusEvent;    Import flash.events.AsyncErrorEvent;    Import Flash.net.NetStream;    Import Flash.media.Video;        public class Simplest_as3_rtmp_player extends Sprite {var nc:netconnection;        var ns:netstream;var video:video;            Public Function Simplest_as3_rtmp_player () {NC = new netconnection ();        Nc.addeventlistener (Netstatusevent.net_status, Netstatushandler); Nc.connect ("rtmp://localhost/live");     }   Private Function Netstatushandler (event:netstatusevent): void {trace ("Event.info.level:" + event.info.level + "\            N "," Event.info.code: "+ event.info.code);                Switch (event.info.code) {case "NetConnection.Connect.Success": Dovideo (NC);                Break;case "NetConnection.Connect.Failed": break;            Case "NetConnection.Connect.Rejected": break;            Case "NetStream.Play.Stop": break;        Case "NetStream.Play.StreamNotFound": break;            }}//play a recorded stream on the server private function Dovideo (nc:netconnection): void {            NS = new NetStream (NC);            Ns.addeventlistener (Netstatusevent.net_status, Netstatushandler);            Video = new video (640,480);            Video.attachnetstream (NS);            Ns.play ("Mycamera");        AddChild (video); }//Create a playlist on the server/* private function Doplaylist (nc:netconnection): Void {ns = new NetStream (NC);            Ns.addeventlistener (Netstatusevent.net_status, Netstatushandler);            Video = new video ();            Video.attachnetstream (NS); Play the first 3 seconds of the video Ns.play ("Bikes", 0, 3, true);//play from + seconds on n            S.play ("Bikes", 5,-1, false);//End on frame 5ns.play ("bikes", 0, false);        AddChild (video); }*/    }}


Simplest_as3_local_player

The Simplest_as3_local_player is used to play local FLV files. Playing local video (*.flv) in ActionScript is the same as playing the rtmp process: Create netconnection First, and then create NetStream. Their biggest difference is that when playing local files to establish netconnection, it is not the address. For example, when playing rtmp, the code is as follows:

Nc.connect ("rtmp://localhost/live");
The code for playing local files is as follows:
Nc.connect (NULL);
When you call play (), the rtmp passes the path on the server as follows.
Ns.play ("Mycamera");
Local files are passed directly to the local path, as shown below.
Ns.play ("sintel.flv");

Simplest_as3_rtmp_player_multiscreen

Simplest_as3_rtmp_player_multiscreen is a simple example of multi-screen playback. The 2x2 grid is implemented to play 4-channel video. Do not record too much.


Simplest_as3_rtmp_streamer

The Simplest_as3_rtmp_player is the simplest rtmp ejector, as shown in the code below.


/** * Simplest ActionScript-based rtmp pusher * simplest AS3 rtmp streamer * * Lei hua Lei Xiaohua * [email protected] * Communication University/Digital TV Technology * Communication University of china/digital TV technology * http://blog.csdn.net/leixiaohua1020 * * This program uses ACTIONSCRIPT3 Language completion, push local camera data to rtmp streaming media server, * is the simplest ACTIONSCRIPT3 based on the streaming device. * * This software are written in ACTIONSCRIPT3, it streams camera ' s video to * RTMP server. * It ' s The simplest RTMP streamer based on ACTIONSCRIPT3.    * */package {import flash.display.MovieClip;    Import flash.net.NetConnection;    Import flash.events.NetStatusEvent;    Import Flash.net.NetStream;    Import Flash.media.video;import Flash.media.Camera;      Import Flash.media.microphone;//import flash.media.H264Profile;     Import flash.media.H264VideoStreamSettings; public class Simplest_as3_rtmp_streamer extends MovieClip {var nc:netconnection;var ns:netstream;var Nsplayer:netstrea M;var vid:video;var vidplayer:video;var cam:camera;var mic:microphone;var Screen_w:int=320;var SCREen_h:int=240;public function Simplest_as3_rtmp_streamer () {NC = new netconnection (); Nc.addeventlistener (NetStatusE        Vent.net_status, Onnetstatus); Nc.connect ("rtmp://localhost/live"); }private function Onnetstatus (event:netstatusevent): Void{trace (Event.info.code); if (Event.info.code = = " NetConnection.Connect.Success ") {Publishcamera ();d isplaypublishingvideo ();d isplayplaybackvideo ();}} Private Function Publishcamera () {//camcam = Camera.getcamera ();/** * Public Function SetMode (Width:int, Heigh T:int, fps:number, Favorarea:boolean = true): void * Width:int-the requested capture width, in pixels.               The default value is 160. * Height:int-the requested capture height, in pixels.               The default value is 120. * Fps:number-the requested capture frame rate, in frames per second.               The default value is 15.  */cam.setmode (640, 480, 15); /** * Public Function Setkeyframeinterval (keyframeinterval:int): void * The number of Video frames transmitted in full (called keyframes) instead of being interpolated by the video compression algorithm. * The default value is which means, every 15th frame is a keyframe.  A value of 1 means that every frame is a keyframe.  * The allowed values are 1 through 300.               */Cam.setkeyframeinterval (+);/** * Public Function setquality (Bandwidth:int, quality:int): void * Bandwidth:int-specifies The maximum amount of bandwidth, the current outgoing video feed can use, in bytes per sec                Ond (bps).                 * To specify this video can use as much bandwidth as needed to maintain the value of quality, pass 0 for bandwidth.               * The default value is 16384. * Quality:int-an Integer that specifies the required level of picture quality, as determined by the amount of Compressio n * being applied to each video frame.  Acceptable values range from 1 (lowest quality, maximum compression) to 100              * (Highest quality, no compression).               To specify the quality can vary as needed to avoid exceeding bandwidth, * Pass 0 for quality. */cam.setquality (200000, 90);  /** * Public Function Setprofilelevel (profile:string, level:string): void * Set profiles and level for video encoding. * Possible values for profiles are H264profile.baseline and H264profile.main. Default value is h264profile.baseline. * Other values is ignored and results in an error. * Supported levels is 1, 1b, 1.1, 1.2, 1.3, 2, 2.1, 2.2, 3, 3.1, 3.2, 4, 4.1, 4.2, 5, and 5.1. * level is increased if required by resolution and frame rate.              *///var h264setting:h264videostreamsettings = new H264videostreamsettings ();               H264setting.setprofilelevel (H264profile.main, 4); Micmic = Microphone.getmicrophone ();/* * The encoded speech quality when using the Speex codec. Possible values is from 0 to 10.  The default value is 6. * Higher numbers REpresent higher quality but require more bandwidth, as shown in the following table.              * The bit rate values of that is listed represent net bit rates and do not include packetization overhead. *------------------------------------------* Quality value |       Required bit Rate (kbps) *-------------------------------------------* 0 |       3.95 * 1 |       5.75 * 2 |       7.75 * 3 |       9.80 * 4 |       12.8 * 5 |       16.8 * 6 |       20.6 * 7 |       23.8 * 8 |       27.8 * 9 |       34.2 * 10 |              42.2 *-------------------------------------------*/mic.encodequality = 9; /* the rate at which the microphone are capturing sound, in KHz. Acceptable values are 5, 8, one, a, and 44. The default value is 8 KHz * If your sound capture device supports this value. Otherwise, the default value is the next AvailabLe capture level above 8 khz/your sound capture device supports, usually one khz.  * */mic.rate = 44; NS = new NetStream (NC);//h.264 setting//ns.videostreamsettings = h264setting; Ns.attachcamera (CAM); Ns.attachaudio (MIC); Ns.publish ("Mycamera", "Live");} Private Function Displaypublishingvideo (): void {vid = new Video (Screen_w, screen_h); vid.x = 10;vid.y = 10;vid.attachcamer A (CAM); AddChild (vid);} Private Function Displayplaybackvideo (): Void{nsplayer = new NetStream (NC); Nsplayer.play ("Mycamera"); vidplayer = new Video (Screen_w, screen_h); vidplayer.x = screen_w + 20;vidplayer.y = 10;vidplayer.attachnetstream (NsPlayer); AddChild ( Vidplayer);}}}



Results


Simplest AS3 rtmp player will automatically connect to rtmp Url:rtmp://localhost/live/mycamera after running.

The results of the program run are as shown.


The Simplest_as3_local_player run will play the sintel.flv file.

The result of the operation is as shown.


4 rtmp URLs are connected after the Simplest_as3_rtmp_player_multiscreen is run.

The result of the operation is as shown.


Simplest_as3_rtmp_streamer running results will push the camera's video and microphone audio to the specified rtmp URL (here is Rtmp://localhost/live/mycamera). The video on the left is the video read from the camera, and the video on the right is the video read from the rtmp URL after the stream is pushed (usually with a certain delay).

The result of the operation is as shown.



Download

simplest Flashmedia Example


sourceforge:https://sourceforge.net/projects/simplestflashmediaexample/

Github:https://github.com/leixiaohua1020/simplest_flashmedia_example

Open source China: http://git.oschina.net/leixiaohua1020/simplest_flashmedia_example


CSDN Download: http://download.csdn.net/detail/leixiaohua1020/8456441


This project contains the following examples of streaming media based on flash technology:
Simplest_as3_rtmp_player: The simplest rtmp player (based on ActionScript)
Simplest_as3_rtmp_streamer: The simplest rtmp ejector (based on ActionScript)
Rtmp_sample_player_adobe: Test player extracted from Adobe Flash Media sever
Rtmp_sample_player_wowza: Test player extracted from the Wowza server
Rtmp_sample_player_flowplayer: FlowPlayer-based rtmp/http player (add rtmp plugin)
RTMP_SAMPLE_PLAYER_VIDEOJS: Videojs-based rtmp/http player
Rtmp_sample_player_jwplayer: Jwplayer-based rtmp/http player
Hls_sample_player_flowplayer: FlowPlayer-based HLS player (add HLS plugin)
HLS_VIDEO_PLAYER_HTML5: HTML5-based hls/http player
Activex_vlc_player: The player for the VLC-based ActiveX control


Note: Some players that open HTML pages directly do not work and need to place the player on the Web server.
(e.g. Apache or nginx)

The simplest example of flash-based streaming media: rtmp push and receive (ActionScript)

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.