# [转载]Multicast Explained in Flash 10.1 P2P

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.

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).

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.

### 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.

 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"); } 

 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. Neighbor picks you to send some of the blocks and sends request
4. Send requested blocks to neighbor
5. All transmissions are partially-reliable
6. Neighbor will ask someone else if requestgoes unfulfilled
7.

Pull then works this way:

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

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

### 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.