The simplest example of Flash-based streaming media: RTMP push and receipt (ActionScript) and rtmpactionscript

Source: Internet
Author: User
Tags wowza server flv file

The simplest example of Flash-based streaming media: RTMP push and receipt (ActionScript) and rtmpactionscript
This article records some Flash-based streaming media processing examples. The most common Streaming Media Protocol for Flash platforms is RTMP. Some RTMP players/streamer Based on C/C ++ recorded previously, but no RTMP player/streamer Based on the Flash-based ActionScript. In fact, Flash-based RTMP player/streamer can be regarded as the "Regular Army" in RTMP technology ". RTMP is designed for communication between Flash platforms. The biggest advantage of RTMP is "no plug-in live broadcast", which also benefits from the Flash Player widely installed on the client. Therefore, this article records an RTMP Player Based on ActionScript and an RTMP streamer Based on ActionScript respectively.

For examples of C/C ++-based RTMP Streaming Media Processing, refer to the following.

Release

The simplest librtmp-based example: Release H.264 (release through RTMP in H.264)

The simplest librtmp-based example: release (FLV is released through RTMP)

The simplest FFmpeg-based streamer (using RTMP push as an example)

Receive

The simplest librtmp-based example: receive (RTMP is saved as FLV)

The simplest Video Player ver2 Based on FFMPEG + SDL (using SDL2.0)


Introduction

Compared to using C/C ++ to process RTMP, it is very easy to process RTMP using ActionScript. The methods for establishing the RTMP connection have been encapsulated. You only need to call the ready-made interface function. However, the disadvantage of using ActionScript to process RTMP is also obvious-there are few places for your own development. Because Flash itself is not open-source, we cannot get its underlying code, and therefore cannot adjust the underlying parameters of codec. All in all, it can be summarized as follows: "simple but not flexible ".

Playing RTMP using ActionScript

Shows the process of playing RTMP streaming media using the ActionScript method.


The process can be divided into two parts: playback and display.

Play

Playback is divided into three steps:

(1) Establish NetConnection
(2) create a NetStream
(3) call the play () method of NetStream

Two logical structures in the RTMP specification are established in the first two steps: NetConnection and NetStream. NetConnection represents the basic connection between server applications and clients. NetStream represents the channel for sending multimedia data. Only one NetConnection can be established between the server and the client, but many netstreams can be created based on the connection. Shows the structure of these two structures.


Display

The display part displays the video on the stage. This part is implemented by creating a Video object.


Push RTMP via ActionScript

Shows the process of pushing RTMP streaming media by using ActionScript.


It can be seen that the process of pushing RTMP is similar to playing. The major difference is that the final call of the push is the publish () method of NetStream, and the final call of playing is the play () method of NetStream () method. Streaming is divided into four steps:
(1) Establish NetConnection
(2) create a NetStream
(3) bind the camera and microphone
(4) call the play () method of NetStream
After the streaming program starts running, you can access the rtmp url through ffplay, VLC, or Flash application to view streaming media.

Code

The attachment to this article contains the following two ActionScript projects:

Simplest as3 rtmp player, the simplest RTMP player, contains three independent 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 streamer
Let's take a look at the source code of the above several projects.


Simplest_as3_rtmp_player

Simplest_as3_rtmp_player is the simplest RTMP player. The Code is as follows.


/*** The Simplest RTMP Player Based on ActionScript * Simplest AS3 RTMP Player ** leixiao Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/ digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program is completed in the ActionScript3 language, playing streaming media on the RTMP server * is the simplest ActionScript3-based player. ** This software is written in Actionscript3, it plays stream * on RTMP server * It's the simplest RTMP player based on ActionScript3. **/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 20 seconds on ns. play ("bikes", 20,-1, false); // End on frame 5ns. play ("bikes", 5, 0, false); addChild (video );}*/}}


Simplest_as3_local_player

Simplest_as3_local_player is used to play a local FLV file. The process for playing a local video (*. flv) in ActionScript is the same as that for playing RTMP: Create NetConnection and then create NetStream. The biggest difference between them is that when a NetConnection is established for playing a local file, the address is not transmitted. For example, the Code for playing RTMP is as follows:

nc.connect("rtmp://localhost/live");
The code for playing a local file is as follows:
nc.connect(null);
When you call play (), RTMP transmits the path on the server as follows.
ns.play("myCamera");
The local file is directly transferred 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. 4 videos are played in a 2x2 mesh. There are no more records.


Simplest_as3_rtmp_streamer

Simplest_as3_rtmp_player is the simplest RTMP streamer. The Code is as follows.


/*** The Simplest RTMP Streamer Based on ActionScript * Simplest AS3 RTMP Streamer ** leixiao Li Lei Xiaohua * leixiaohua1020@126.com * China Media University/Digital TV technology * Communication University of China/ digital TV Technology * http://blog.csdn.net/leixiaohua1020 ** this program is completed in the ActionScript3 language, push the data from the local camera to the RTMP Streaming Media Server. * It is the simplest ActionScript3-based streamer. ** This software is 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. hsf-profile; // import flash. media. hsf-videostreamsettings; public class simplest_as3_rtmp_streamer extends MovieClip {var nc: NetConnection; var ns: NetStream; var nsPlayer: NetStream; 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 (NetStatusEvent. 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 (); displayPublishingVideo (); displayPlaybackVideo () ;}} private function publishCamera () {// Camcam = Camera. getCamera ();/*** public function setMode (width: int, height: 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 15, which means that 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 (25);/*** public function setQuality (bandwidth: int, quality: int): void * bandwidth: int-Specifies the maximum amount of bandwidth that the current outgoing video feed can use, in bytes per second (bps ). * To specify that the 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 compression * being applied to each video frame. acceptable values range from 1 (lowest quality, maximum compression) to 100 * (highest quality, no compression ). to specify that picture 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 profile and level for video encoding. * Possible values for profile are hsf-profile. BASELINE and hsf-profile. MAIN. default value is hsf-profile. BASELINE. * Other values are ignored and results in an error. * Supported levels are 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 may be increased if required by resolution and frame rate. * // var hsf-setting: hsf-videostreamsettings = new hsf-videostreamsettings (); // hsf-setting. setProfileLevel (hsf-profile. MAIN, 4); // Micmic = Microphone. getMicrophone ();/** The encoded speech quality when using the Speex codec. possible values are 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 that are 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 is capturing sound, in kHz. acceptable values are 5, 8, 11, 22, 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 that * your sound capture device supports, usually 11 kHz. **/mic. rate = 44; ns = new NetStream (nc); // H. 264 Setting // ns. videoStreamSettings = hsf-setting; 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. attachCamera (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 );}}}



Result


Simplest as3 rtmp player automatically connects to rtmp url: rtmp: // localhost/live/myCamera after running.

Shows the result of running the program.


Simplest_as3_local_player will play the sintel. flv file.

Shows the running result.


Simplest_as3_rtmp_player_multiscreen after running, four RTMP URLs are connected.

Shows the running result.


After simplest_as3_rtmp_streamer runs the result, the video and microphone audio of the local camera are pushed to the specified rtmp url (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 streaming (there is usually a certain delay ).

Shows the running result.



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 example 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 streamer (based on ActionScript)
Rtmp_sample_player_adobe: Test player extracted from Adobe Flash Media Sever
Rtmp_sample_player_wowza: The test player extracted from the Wowza server.
Rtmp_sample_player_flowplayer: RTMP/HTTP Player Based on FlowPlayer (add RTMP plugin)
Rtmp_sample_player_videojs: RTMP/HTTP Player Based on VideoJS
Rtmp_sample_player_audio PLAYER: RTMP/HTTP Player Based on audio player
Hls_sample_player_flowplayer: FlowPlayer-based HLS player (add HLS plugin)
Hls_video_player_html5: HTML5-based HLS/HTTP player
Activex_vlc_player: VLC-based ActiveX Control player


Note: Some players cannot open html pages directly. You need to put the player on the Web server.
(For example, Apache or Nginx)

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.