Tom Krcha's FlashRealtime

Hey amigo!
I am Tom Krcha, Gaming Evangelist at Adobe. These are my notes


Multicast Explained in Flash 10.1 P2P

July 1st, 2010

Multicast is one of the features of Flash Player 10.1 and it enables you to distribute NetStreams across the peer-to-peer mesh. It can be audio, video or even pure data stream (AMF3) – the data stream can be very handy for games, it’s much better for such purpose (like sending realtime positions, directions) than using Posting as Posting is more optimized for large number of senders to send something – like chat, status change and so on.

Difference between Unicast and Multicast

Unicast
Unicast simply delivers streams from a server to n clients. For this purpose you can use Flash Media Server and TCP protocols such as RTMP/T/S/E or HTTP protocol. Unicast also costs you a lot of resources -> 1 MBps stream delivered to 1000 clients means 1 GB upstream from server – which is CPU demanding and network transit is also huge.
unicast2


Application Level Multicast
Sometimes called P2P Multicast is an optimized stream distribution among peers. It’s very cost effective as the stream is distributed among peers. You can either distribute stream from a client or FMS (with the future release).
Multicast in Flash

Native IP Multicast
This one is distributed over the network not using P2P connection, but using network routing capabilities. It mostly works in internal, enterprise, VPN or LAN networks – basically in closed networks, which supports that. You can combine Native IP Multicast with Application Level Multicast to deliver combined solution called Multicast Fusion. This feature need Flash Media Server (next generation version). We will cover this feature once the new FMS is available.
Multicast Fusion with Flash

Developing Multicast in Flash

We are going to build two applications, one is going to be a receiver and one broadcaster.

Once you get connected to Cirrus or FMS. You need to setup the NetStream – same way like NetGroup (check Simple Chat using NetGroup tutorial).

My group is called “myGroup/multicastOne” and I am streaming stream “multicast”. You can define whatever names you want.

Broadcaster app (see whole source-code Broadcaster.mxml):

private function setupStream():void{
	var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/multicastOne");
	groupspec.serverChannelEnabled = true;
	groupspec.multicastEnabled = true;
 
	stream = new NetStream(nc, groupspec.groupspecWithAuthorizations());
	stream.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
 
	var cam:Camera = Camera.getCamera();
	video.attachCamera(cam);
 
	stream.attachCamera(cam);
	stream.publish("multicast");				
}

Receiver app (see whole source-code Receiver.mxml):

private function setupStream():void{
	var groupspec:GroupSpecifier = new GroupSpecifier("myGroup/multicastOne");
	groupspec.serverChannelEnabled = true;
	groupspec.multicastEnabled = true;
 
	stream = new NetStream(nc, groupspec.groupspecWithAuthorizations());
	stream.addEventListener(NetStatusEvent.NET_STATUS,netStatus);
 
	stream.play("multicast");
 
	video.attachNetStream(stream);
}

Watch: Developing Multicast Streaming in Flash Player 10.1 (Videotutorial)

Okay, but how can I stream from Flash Media Live Encoder. For this purpose you will need to get next generation of Flash Media Server. Once it’s available – I will write a tutorial how you can use it.

Packet distribution in Multicast

Multicast in Flash is optimized to be efficient. It works the way that you don’t upstream whole stream you received – you could do that – but you rather upstream only couple chunks that fits your network possibilities. Most of the normal DSL lines have around 8mbps download and 1mbps upload or more – so are able to receive 2mbps, but distribute only 1mbps (in optimal situation), so you would need more peers for further distribution, which would combine packets together.

Multicast in Flash is a Pull/Push system. Pull ensures to deliver the data to the peers. Push optimize the mesh, reduce latency and create multiple spanning trees. If there were only Pull, the latency of the live stream on the furthest peer would be too big. Push solves that.

Pull

There are these steps:

  1. Once you start distributing stream you send a map of blocks to one (or all) neighbor(s)
  2. Neighbor waits a short time to see who can source each block
  3. Multicast in Flash - Pull - Map of Blocks

  4. Neighbor picks you to send some of the blocks and sends request
  5. Multicast Flash - Pull 2

  6. Send requested blocks to neighbor
  7. All transmissions are partially-reliable
  8. Neighbor will ask someone else if requestgoes unfulfilled
  9. multicast flash pull 3

Pull then works this way:
multicastpullanimation1

Push

  1. Send map of what blocks you have to one (or all) neighbor(s)
  2. Neighbor notices that data is flowing
  3. Neighbor picks candidate sources for each sequence number slice
  4. Neighbor sends mask describing which sequence numbers to push
  5. Modulo size of mask (in this example every 3rd, 5th, and 6th)
  6. Periodically tests other neighbors to check for lower latency
  7. For each sequence number slice, keeps just quickest source
  8. Data is pushed immediately as it arrives for requested slices
  9. For each slice, there is a limit to the number of push clients served

Push then works this way
Multicast Push Flash

Push/Pull
By combining two above, you get effective/optimized multicast distribution, which can be illustrated like this:

multicast pull/push animation

Conclusion

For Multicast in Flash we use NetStream class together with GroupSpecifier and NetConnection. Multicast is the most complicated thing in Flash P2P implementation and it’s very intelligent in optimizing the delivery, chunk distribution and mesh reforming. We have three types of multicast: P2P, IP and Fusion (P2P+IP). Multicast is available with Adobe Cirrus and more will be available with new FMS.

Adobe TV resources:
- Watch the session about P2P from MAX 2009 explaining multicast by Matthew Kaufman (Seek to 36:28)
- Videotutorial: Developing Multicast Streaming in Flash Player 10.1

In the next tutorial I will explain how QoS and monitoring with multicast works.

Stay tuned for more!

Where to go from here

Check other tutorials regarding P2P in Flash:

- File Sharing over P2P in Flash with Object Replication
- P2P GroupSpecifier Class Explained In Details Part 1
- Multicast Explained in Flash 10.1 P2P
- Directed Routing Explained in Flash 10.1 P2P
- Simple chat with P2P NetGroup in FP 10.1

Video tutorials:
- P2P Chat with NetGroup in Flash Player 10.1
- Multicast Streaming in Flash Player 10.1 Tutorial

Facebook comments:

47 Comments »

  1. Excellent work and great animations. Now I want to read the next tutorial !!!

    Comment by Michael Chaize — July 1, 2010 @ 10:29 am

  2. Hi Tom,
    this looks good. I have a question, are all the push/pull actions really handled by the flash player or is there something to be programed in AS? Would:
    groupspec.serverChannelEnabled = true;
    groupspec.multicastEnabled = true;
    + NetStream publish() / send() / play()
    work out the way how to distribute blocks? or do I have to explicitly define some inteligent distribution?

    Comment by Jozef Chutka — July 1, 2010 @ 10:30 am

  3. This whole intelligence is done by Flash Player – you can only limit peers count you are sending data to and bandwidth. If you want to control all – then you need to use Object Replication feature in FP 10.1 P2P. I will be writing article about this soon.

    Comment by tom — July 1, 2010 @ 10:35 am

  4. cool, looks like making ustram clone with 10 lines of code :)

    Comment by Jozef Chutka — July 1, 2010 @ 1:28 pm

  5. that’s it! :) looking forward to your apps – leave some links here, when done ,)

    Comment by tom — July 1, 2010 @ 2:38 pm

  6. [...] Direct Link [...]

    Pingback by Multicast Explained in Flash 10.1 P2P | Lively Flash Tuts — July 1, 2010 @ 10:13 pm

  7. Very well thought-out and well written article, Tom. Good to have such information all at one place, and the code examples are very helpful.

    One remark, though: sending from one server to ‘n’ clients describes Broadcast rather than Unicast; Unicast means one server to one client.

    I know that there are plenty of other examples referring to Unicast instead of Broadcast out there (even Microsoft does it). That probably is because common Broadcast scenarios usually consist of ‘n’ instances of Unicast (1:1) connections, thus leading to a 1:n distribution.

    Thus, referring to “Broadcast via ‘n’ times Unicast” or something like that might be a little bit more precise.

    Best regards
    Constantin

    Comment by Constantin Ehrenstein — July 2, 2010 @ 12:28 pm

  8. Thanks for a tip Constantin. That’s true. Just thought it’s obvious, but rather updated the image to be more precise.

    Comment by tom — July 4, 2010 @ 8:36 pm

  9. It seems to me that Adobe is a bit confused in Evangelizing its new technologies (and upcoming Products) and even more vague about market availability of its products (Stratus?).

    Q: What are the limitations of RTMFP in Flash Player 10?
    A: Flash player 10 will not enable swarming, multi-cast or broadcast quality live video. It will only enable communication from the voice and video devices native to your computer (e.g. microphone and webcam) and enables application developers to send ActionScript data messages directly between Flash Players – useful in multi-player game development.
    http://download.macromedia.com/pub/labs/flashplayer10/flashplayer10_rtmfp_faq_111208.pdf

    Comment by Erik — July 6, 2010 @ 1:55 pm

  10. Erik, that was before Flash Player 10.1. Flash Player 10 enabled only point-to-point P2P. Now you can do swarming, multicast and so on.

    Comment by tom — July 7, 2010 @ 2:24 pm

  11. [...] Tip, READ: Multicast Explained in Flash 10.1 P2P [...]

    Pingback by P2P Multicast in new Flash Player 10.1 — FlashRealtime.com — July 7, 2010 @ 2:26 pm

  12. [...] Replication is the most low-level P2P access available in Flash Player 10.1 (next to Multicast, Posting and Directed Routing) It basically enables you to send chunks of between peers. Object [...]

    Pingback by File Sharing over P2P in Flash with Object Replication — FlashRealtime.com — July 14, 2010 @ 9:29 pm

  13. “Okay, but how can I stream from Flash Media Live Encoder. For this purpose you will need to get next generation of Flash Media Server. Once it’s available – I will write a tutorial how you can use it.”

    Can we get some roughest idea on when the next generation FMS will come around? Thanks!

    Comment by Yi — July 15, 2010 @ 6:30 pm

  14. Hi Yi, stay tuned :) That’s all I can say you…

    Comment by tom — July 15, 2010 @ 6:50 pm

  15. Excelent post, very descriptive.
    The only thing I want to find out if there is any possibility to broadcast external video source (not webcam) using this tech?

    Comment by Akaki — July 25, 2010 @ 12:47 pm

  16. Hi Akaki, there will be such a possibility with new Flash Media Server.

    Comment by tom — July 26, 2010 @ 11:09 am

  17. Thanks for the reply. And one more question, have you got any idea when the new FMS will be released? at least the beta testing version.

    Comment by Akaki — July 26, 2010 @ 6:34 pm

  18. Can you write me an email to tomkr [at] adobe.com, please?

    Comment by tom — July 27, 2010 @ 4:22 pm

  19. hi Tom. your Video is very nice!!! but I still got almost same problem. which was on the Adobe TV

    I got 10.1 player and SDK4.1 and DEV key and server key supposed to be right..

    Error: Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.
    at flash.net::NetStream/invoke()
    at flash.net::NetStream/attachCamera()
    at Broadcaster/setupStream()[D:\flex4_workspaces\Stratus_Demo\src\Broadcaster.mxml:67]
    at Broadcaster/netStatus()[D:\flex4_workspaces\Stratus_Demo\src\Broadcaster.mxml:50]

    Comment by minoru — July 28, 2010 @ 11:52 am

  20. [...] Flash Player 10.1 with Object Replication – P2P GroupSpecifier Class Explained In Details Part 1 – Multicast Explained in Flash 10.1 P2P – Directed Routing Explained in Flash 10.1 P2P – Simple chat with P2P NetGroup in FP [...]

    Pingback by Video-on-Demand over P2P in Flash Player 10.1 with Object Replication — FlashRealtime.com — July 28, 2010 @ 8:00 pm

  21. [...] Multicast Explained in Flash Player 10.1 [...]

    Pingback by 50 resources to get up to speed with the Flash Platform : Mihai Corlan — July 29, 2010 @ 3:57 pm

  22. [...] Multicast Explained in Flash 10.1 P2P [...]

    Pingback by The next big thing: Adobe Flash P2P « Flash Video Technology and Optimizations — July 30, 2010 @ 8:01 pm

  23. [...] Some of you guys were probably wondering how to establish P2P connections in the local network (LAN) without Stratus. Yes. It’s possible. Using native IP-only multicast. Let’s build a simple chat with Posting. Adding Multicast broadcast is just a simple next step which I am sure you can accomplish by yourself with few hints from my article about Multicast [...]

    Pingback by Local Flash Peer-to-Peer Communication over LAN (without Stratus) — FlashRealtime.com — August 27, 2010 @ 2:20 pm

  24. [...] need separate connections to each peer or you’d need to stick to a client-server architecture. Multicasting with Flash and Flex is possible with Flash 10.1 and higher. The advantage of Java over Flash/Flex is that more [...]

    Pingback by What language should I use for a chat app — September 19, 2010 @ 12:16 am

  25. [...] interactuación. Envío tradicional o streaming, Unicast y Multicast. Gracias a este artículo de ‘flashrealtime’ (artículo incluido literal y traducido) vamos a ver las diferencias que [...]

    Pingback by Flex y Air permiten realizar comunicaciones directas entre usuarios (P2P) | Kumin Blog — September 19, 2010 @ 12:15 pm

  26. Hi,
    I would like to know if i can have a consultation on a subject we have done before and we don’t know who to ask about it.
    we are an education company, in 2006 we create a with a developer a online classroom using action script 2 and FMS 2, we also create a chat and white board, but we never get public cause we where not happy with the file sheering and we didn’t know how much student we can put one server, in another word we didn’t trust the development.
    now we want to create a small module of that with only video audio and chat, allowing meeting throw the internet for the same purpose.
    so now i see the new way to use stratus as a per to per and lower costs on the server but didn’t understand if i can use it on my site for meeting.
    so my question is:
    a. can i use struts on the site to allude streaming video between the site members and for what limit?
    b. can i use your code with my FMS2 to stream the video?
    c. can i combine the two, using struts and FMS2?

    I don’t know if you answer this kind of a questions, but it will make me very happy to get a professional answer.

    Thanks
    Ilan

    Comment by Ilan mor livne — October 10, 2010 @ 6:16 am

  27. Hi Tom,

    i find a issue with your setupStream function. As i have observed, you cannot a multicast stream until you get the “NetStream.Connect.Success” info from connection handler.

    Also there is a issue with stopping publish and starting over again. NetStream still thing publish is in session even if you null the NetStream object. I have already posted this on Adobe forum.

    Comment by Rajdeep Rath — November 5, 2010 @ 12:10 pm

  28. Hi, could you explain more definitely what “Modulo size of mask (in this example every 3rd, 5th, and 6th)” means? What example and what mask do you mean? Where I can see “every 3rd, 5th, and 6th” WHAT? Thnx…

    Comment by Sergio — November 7, 2010 @ 9:20 pm

  29. And does here “slice” mean the same that “chunk”, a part of data transmitted?

    Comment by Sergio — November 7, 2010 @ 10:21 pm

  30. I’m curious about the send() function in NetStream. If the publishing peeer sends some data via send(), is the data relayed through P2P, or just unicast by the publishing peer to each playing peer?

    Comment by Yi — November 12, 2010 @ 11:44 pm

  31. Hi Yi, depends if you have multicast or DIRECT_CONNECTIONS. With multicast NetStream it’s relayed, if you make dir.connection it’s unicast like, directly.

    Comment by tom — November 13, 2010 @ 10:46 am

  32. Well, does your ignoring of my question mean that you do not know the answer by yourself? Then, where is the original article to look and try understand from it?

    Comment by Sergio — November 13, 2010 @ 7:27 pm

  33. just what I need, thanks Tom!

    Comment by Yi — November 14, 2010 @ 7:59 am

  34. Great info.!

    I`ve been using Flash/Flex for a while, but I`m fairly new to working with live video streaming. I just want to set up a simple P2P video stream (i.e. Skype), between two users, where they are both broadcasting and receiving simultaneously. Is this possible using the Multicast NetStream method shown above, or is there another, more efficient/recommended way to achieve this?

    Thanks in advance!

    Comment by Matt — November 22, 2010 @ 2:24 pm

  35. Hi Matt, the better method for VoIP like service will be using DIRECT_CONNECTIONS http://www.flashrealtime.com/basics-of-p2p-in-flash/

    Comment by tom — November 23, 2010 @ 5:33 pm

  36. Tom, thank you very much!
    That is exactly what I was looking for!

    Comment by Matt — November 25, 2010 @ 5:24 pm

  37. Tom,

    Just a quick question. Sorry for my lack of knowledge in this area, but I`m not too sure how to set up a web service. I am using the Adobe sample p2p video streaming app. The help says “You should override the AbstractIdManager class to implement your own peer ID exchange mechanism—using, for example, XMPP, Google Apps, or the Facebook framework.” Do I have to write the web service app myself? Is there any public code available that works with this particular example (i.e. the Adobe sample app.)?

    Comment by Matt — December 2, 2010 @ 5:05 pm

  38. I had a question you are using GroupSpecifier class which is part of flash.net package. This class is in the Beta version of Action Script 3.0 hence my flash builder 4.0 version is unable to recognize it. also is flash player 10.1 able to recognize it?

    Comment by Gamer — December 23, 2010 @ 11:41 am

  39. I need to know exactly what will do about this?!?

    -Sincere Regards

    cigar

    Comment by Bernie — December 26, 2010 @ 1:09 am

  40. I could use some help.
    When I compile and run the broadcaster xmxl, it says “NetConnection.Connect.Success”, but then nothing else happens. Same with the receiver. I put in my dev key, and tried the existing stratus server, and the server address given with my dev key.

    When I run it with Flash debugger, I get the following message:
    Error: Error #2154: The NetStream Object is invalid. This may be due to a failed NetConnection.
    at flash.net::NetStream/invoke()
    at flash.net::NetStream/attachCamera()
    at Main/setupStream()[C:\Users\Professor Frink\Documents\FlDev\VidChat\src\Main.mxml:66]
    at Main/netStatus()[C:\Users\Professor Frink\Documents\FlDev\VidChat\src\Main.mxml:48]

    Any help is appreciated.

    Comment by Pete — January 12, 2011 @ 1:06 am

  41. I’ve been browsing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made good content as you did, the net will be much more useful than ever before.

    Comment by outcall Ukraine escorts — January 19, 2011 @ 10:50 pm

  42. What a diagram, i never see better

    Comment by sport — March 25, 2011 @ 2:13 pm

  43. Keep posting as I am gonna come to read it everyday. Thanks for sharing.

    Comment by gravity forms discount code — May 5, 2011 @ 6:56 pm

  44. Hello, I am a university student. Our lab is attempting to work on a p2p live streaming using flash p2p features. The media source is not from a webcam but a file from a certain server, which is not directly supported by any of the 4 methods flash player offers(posting, direct routing , object replication,and multicast.) As some of the forum threads mentioned, our method is to use NetStream.publish() a stream and use send(“callbackname”, data) to all subscribers that have joined in a NetGroup. So we can use p2p transmission. Now here are our questions:

    1. We know from MAX 2009 that flash p2p camera video multicasting implements pull-push mechanism inside which makes full use of p2p features like buffer map exchange. If we use NetStream.send() API to send non-webcam source data, will it also make use of this pull-push mechanism to spread data among all peers(subscribers) with proper data exchange?

    or more detailedly,

    I saw the P2P Gaming Libs at flashrealtime.com. It uses DIRECT_CONNECTIONS when creating publish-used NetStream in order to send data with lowest latency. My question is if I do not use DIRECT_CONNECTIONS, than when the NetGroup grow large (1000), will those peers that are not direct neighbors to the publisher in the same group relay data to each other using pull-push via buffer map?

    2. I have written a sample app and use send() to deliver data only (NetStream.bufferTime = 0) without camera video and audio. When the publisher sends the data in a “for()” stantence for 20 times, the subscribers can only receive about 8 of them. But when I set a timer and periodically send them(e.g., 500ms), the subscribers can receive them correctly. My questions are: Is the packet loss caused by UDP unreliability? Can this be solved by setting the NetStream.dataReliable = ture? Can this totally be solved by setting a timer? How to set the delaytime property in timer? Are all data sent are orderedly received?

    I’m very appreciated if you can answer my questions? thanks.

    Comment by mogao — May 27, 2011 @ 11:48 am

  45. Hi Tom, Just posted a question on stackoverflow, that I’m hoping you might be able to answer… http://stackoverflow.com/questions/6351665/adobe-cirrus-examples-needed

    Comment by Tom — June 15, 2011 @ 2:20 am

  46. Hi everyone!
    Could somebody expalin me, what means Error #2032
    which I get when trying to check both, broadcaster and reciever in Adobe Flash Builder 4.5?
    It undrerline by red rtmpf as mistace, despite address is given me by Adobe and I did everyrthing exactly as shown in video

    Comment by Viach — August 22, 2011 @ 8:52 pm

  47. HI,TOM,thank you very much for your article,but i still have a question just like some one(Sergio) above the comment,i’m a chinese ,my english is not so good,but very interest in your blog,it’s very useful,but i have cost full day to Understand what’s mean,the question is:

    [what “Modulo size of mask (in this example every 3rd, 5th, and 6th)” means? What example and what mask do you mean? Where I can see “every 3rd, 5th, and 6th” WHAT? ]

    would you give us some explain? i’ll wait for you,and
    Persist to notice your blog,thank you ,thank you very much.

    Comment by thomas — September 17, 2011 @ 5:15 pm

RSS feed for comments on this post. / TrackBack URL

Leave a comment

Comment moderation is enabled. Your comment may take some time to appear.