Video-on-Demand over P2P in Flash Player 10.1 with Object Replication
July 28th, 2010
In the previous tutorial File Sharing over P2P in Flash Player 10.1 with Object Replication we went through the Object Replication basics. And you can see that the Receiver is requesting packets one by one. That’s not suitable for the real world app, but it’s good for testing on a LAN to see the progress. In the real world app, you can immediately request all packets using NetGroup.addWantObjects(beginIndex, endIndex);.
Transferring VoD video over P2P
Let’s get something real with Object Replication. The use-case I like most is a Realtime P2P Distributed System for Video-on-Demand.

This is the vision. It could be nicely integrated by video/news portals, especially on the currently most demanded videos (latest hits, hot news, etc.). When users are currently acquiring one video or a few hot videos from a server, why not try to lighten the load on the server and improve the viewers experience.
I am not afraid to say that this can save you millions or at least most of your video portal costs.
*Note: This is only a study how it could work. It still needs further work and testing.
See how it works
This technique shows how you can distribute VoD video with Flash Player 10.1 Object Replication
Getting the video
To download a video file, you can use URLStream class. Register ProgressEvent to control every byte segment you get, while putting these segments together you can already start creating deliverable chunks (~64KB) for object replication. If you are on a slow network, you will probably get more segments to chunk, than on a fast network. This is illustrated below. It doesn’t matter, just keep in mind, that you need to wait until you can separate these segments into chunks.
Sharing the video
While downloading, you can already start sharing the video in the P2P network using Object Replication. Once others receive packets, they start sharing them as well, while receiving the rest. This makes the network more stable and less dependent on a relatively low number of original providers.
Viewing the video
Flash Player 10.1 added a new NetStream function called appendBytes, which enables you to pass a ByteArray containing FLV bytes to a NetStream and then display it. This was originally added for HTTP streaming, but we can leverage it nicely as well. You can read more about it here.
Solution #1 (Basic)
Sender | Receiver | Tester (both in frames) | FLV video for testing
In this demo, you browse for a FLV video on a local storage device; once you choose it, it starts providing packets for other peers.

Basically, you cut video into chunks and start providing it using NetGroup.addHaveObjects(0, 84); – this video has 84 chunks. On the receiver side, you ask for a zero chunk, which is a file descriptor, using NetGroup.addWantObjects(0,0); In this descriptor you receive the total chunks count and you can add more info like file name, size, etc. Once you receive the zero chunk, you ask for the rest of the objects using NetGroup.addWantObjects(1,84); and Flash Player will start receiving the rest of the packets after a few seconds.
Try opening the receiver in other 5 or more windows and you will find an error. There is no guarantee that packets are received one by one, they can be received in completely different order and for this, you will need a special buffer. More in Advanced solution below.
Solution #2 (Advanced)

The Challenges
There were couple challenges, which need to be addressed to make it work properly.
Chunks
You need to split received bytes from URLStream to deliverable chunks. You have to wait until you receive at least one chunk (64KB) and than add it to an array. If you receive less, you wait, until you have 64 KB or more and then you add it. (So – First buffer). If you receive more than 64 KB in one progress event, you need to cut it in a cycle. So for instance if you receive 100 KB, you create one chunk (64 KB) and the rest (36 KB) you leave in the buffer to be filled (to at least 64 KB). Â If you receive 200 KB (you create 3 chunks and keep 8 KB for the next batch). If you receive last bits of the data, which is in Event.COMPLETE, than you create just final chunk (which is always going to be less than 64 KB or same). All these things are done in URLByteLoader.as. Check especially createChunks() function.

Download buffer for viewing
While downloading, I put chunks into a buffer, which is appended directly to the NetStream and then played in a Video object. This enables me to display video during the download process.
Postponed buffer for P2P (put chunks together)
The biggest problem with receiving Object Replication packets is that there is no guarantee that you will receive packets in correct linear order one by one. Typically you receive packets a completely mixed order – e.g. 1,2,3,4,20,21,22,5,6,7 and so on. So you need a Timer that puts these packets together and waits for the next one in the line and then continues with appending bytes to the NetStream. This basically enables you to display video during the Object Replication process and you don’t have to wait until you receive all the packets before you start playing video.
Deciding who is provider
In the real world, you will need to decide who is the provider and who is the receiver. Providers should be clients on the best uplink/downlink network. You can make a network check before playing video and then say that this client is going to be a provider. You can save this information in cookies with a corresponding expire time.
The Source
Download here (src.zip, 10 KB)
Where to go from here
Check out these other tutorials on P2P in Flash:
- File Sharing over P2P in 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 10.1
Video tutorials:
- P2P Chat with NetGroup in Flash Player 10.1
- Multicast Streaming in Flash Player 10.1 Tutorial
Facebook comments:
57 Comments »
RSS feed for comments on this post. / TrackBack URL





This is pretty interesting stuff, the real acid test would be to see it in production.
Comment by Ahmed Nuaman — July 29, 2010 @ 12:17 am
[...] Go here to read the rest [...]
Pingback by Video-on-Demand over P2P in Flash Player 10.1 with Object Replication | Flash Stock Files — July 29, 2010 @ 2:59 am
Great article, congradulations =)
Comment by Leonardo França — July 29, 2010 @ 6:04 am
oh! very cool
Comment by cooerson — July 29, 2010 @ 6:11 am
[...] Direct Link [...]
Pingback by Video-on-Demand over P2P in Flash Player 10.1 with Object Replication | Lively Flash Tuts — July 29, 2010 @ 8:33 am
Hi tom,
I want to use P2P between java and flash client. Is it possible by any means ?
regards,
ATIF
Comment by Atif — July 29, 2010 @ 12:39 pm
Hi Atif, I think currently not – RTMFP is currently only in Flash Player and it’s not yet open protocol due to security reasons. Maybe in the future.
Comment by tom — July 29, 2010 @ 12:55 pm
thanks tom nice article btw.
Comment by Atif — July 29, 2010 @ 1:08 pm
Can i publish local video to FMS as well or its only limited to P2P ?
Comment by Atif — July 29, 2010 @ 1:13 pm
Well – most probably yes. Using RTMP NetStream and passing byte array data.
Comment by tom — July 29, 2010 @ 1:21 pm
hi Tom,
Can you please give any example in how to pass data.
I am using appendBytes but it gives following error.
TypeError: Error #2004: One of the parameters is invalid.
at flash.net::NetStream/appendBytesAction()
Comment by Atif — July 29, 2010 @ 2:36 pm
Atif – read about it here: http://www.bytearray.org/?p=1689
Comment by tom — July 29, 2010 @ 3:06 pm
yes i have already read this article.But i think publishing stream to FMS is not yet possible. As i read the documentation of appendBytes (Passes a ByteArray into a NetStream for playout.)not publishing.
Comment by Atif — July 29, 2010 @ 3:26 pm
sure, but – you can load a local file into bytearray chunks and then pass these chunks via NetStream.send() to FMS, which distributes those packets to receivers. It shouldn’t be that complicated. I can prepare an example.
Comment by tom — July 29, 2010 @ 4:08 pm
Great article! The FP10.1 capabilities are amazing. Thanks!
Comment by Snickers[PL] — July 29, 2010 @ 4:44 pm
Using Arduino with Adobe Flash for Model Railroading: Part 2…
I really found your post interesting so I added a trackback to it on my Track Hacker blog
…
Trackback by Track Hacker — July 30, 2010 @ 7:37 pm
[...] Video-on-Demand over P2P in Flash Player 10.1 with Object Replication [...]
Pingback by The next big thing: Adobe Flash P2P « Flash Video Technology and Optimizations — July 30, 2010 @ 8:05 pm
Hi Tom,
What should be appendBytes length.I am trying to use appendBytes with virtual file.But no success.Video player only Shows First Frame and the nothing after that.Any suggestions.
regards,
ATIF
Comment by Atif — July 31, 2010 @ 11:20 am
Hi Atif, I am not sure about this. But more bytes = better. I would provide bytes for at least couple seconds – which is by my opinion sufficient buffer. The minimum is defined by so many bytes, which can be played until you come to their end – and till that, you need to provide another more bytes to follow.
Comment by tom — August 2, 2010 @ 11:22 am
i just loaded a local file and on load complete i made different chunks of it and sending to the other client using NETSTREAM.SEND function
FMS_ns.send(“RemoteApendStream”,bytarray);
getting this error
Error #2044: Unhandled AsyncErrorEvent:. text=Error #2095: flash.net.NetStream was unable to invoke callback RemoteApendStream. error=TypeError: Error #1034: Type Coercion failed: cannot convert Object@11820ee1 to flash.utils.ByteArray.
please help me out.
Comment by waqas — August 6, 2010 @ 1:03 pm
Very nicely detailed. And to think, we were thinking of this possibility on a flight a couple of weeks back. But really, we really need rtmfp on FMS rather than just Stratus.
Comment by Arindam Biswas — August 7, 2010 @ 10:54 pm
To waqas:
you can use ffmpeg to realize that.
Comment by FMSer.CN — August 9, 2010 @ 9:44 am
[...] Read also: Video-on-Demand over P2P in Flash Player 10.1 with Object Replication [...]
Pingback by File Sharing over P2P in Flash Player 10.1 with Object Replication — FlashRealtime.com — August 18, 2010 @ 1:00 am
Hello Tom.
Thanks for the tutorial that is great.
I have question. Is it possible to apply this concept to a live stream, like a webcam or something similar. I need to make this kind of file distribution but the source file is a webcam stream. Is it possible? Thanks for the help.
Comment by JoĂŁo batalha — August 20, 2010 @ 12:02 pm
Hi Joao, absolutely. But for live stream it’s better to use Multicast – read here: http://www.flashrealtime.com/multicast-explained-flash-101-p2p/
Comment by tom — August 25, 2010 @ 6:06 pm
Hello Tom.
Thanks for the reply. I was already testing the example you gave me.
The porpose of the project i’m working is this. I have a central server that is streaming a webcam source and there is a flash application there that is publishing it as a multicas stream. Then i need to have the clients (swf application served in a http server) to connect to this streaming server. I want to use this type of technology because we need to decreasing to the minimal level the amount of data transmitted between the server and a local network where several clients are connected. So the perfect solution would be:
- for each local network the first client that connects to the streaming server will be the main “server” for the next one and so on..
We have manage to build this type of structure and we are now beginning to test it, but all the clients belong to the same group so we can’t be sure that a client from a local network will serve and will be served by the clients of that corresponding network. understand?
Is this possible using this type of technology and get to the objective we want?
thanks.(you can use my email if you want)
Comment by JoĂŁo batalha — August 26, 2010 @ 11:11 am
This is pretty interesting stuff..!
Comment by Ravi — September 22, 2010 @ 9:54 pm
Nice interesting stuff..!
Comment by Ravi — September 22, 2010 @ 9:55 pm
as3
Comment by Ravi — September 22, 2010 @ 9:57 pm
Comment by Ravi — September 22, 2010 @ 9:57 pm
AS 3.0
Comment by Ravi — September 22, 2010 @ 9:58 pm
Is it possible to resize the movie at some point? either on the providers end or the receivers end is fine.
Comment by Jesse — September 24, 2010 @ 1:29 am
After thinking about it, is it possible to resize the movie on the providers end, so that the video transfer bandwidth isn’t as large.
I do not want to resize the video on the receivers end.
Comment by Jesse — September 24, 2010 @ 9:54 pm
Hi Tom, thanks for the great article.
On the VideoDisplay we have constructed based on your sample, the NetStream.Seek() function doesn’t work as supposed to, the video will just stop playing whether all the packets have been downloaded or not. can you please advice?
Comment by Fei — September 25, 2010 @ 5:27 am
How can I mix an local mp3 and my microphone and send it to the listeners?
I´d read all the as3 documentation, and could not find it, is that possible?
Thanks.
Comment by jovane — September 27, 2010 @ 6:07 pm
Is it possible to use OSMF player to play this vod netstream?
Thanks
Comment by jason — October 3, 2010 @ 8:08 am
This is pretty interesting stuff, the real acid test would be to see it in production.
Comment by The Netmate — October 21, 2010 @ 4:22 pm
Hi! Thanks for docs.
i have a ? – How can i set download speed in
private function startDownload():void{
fileLoader.load(URLbox.text.toString()+”?”+Math.round(Math.random()*1000));
//fileLoader.load(“ironman.flv”)
}
or when i can set download speed of flv file?
Comment by Garik — November 3, 2010 @ 8:06 pm
http://www.net.informatik.uni-goettingen.de/publications/1695/MobileP2P-ICCCN10.pdf
Comment by Garik — November 3, 2010 @ 8:10 pm
when i try 270 mb flv file – the apss download it from local folder too fast and flashplayer can
Comment by Garik — November 3, 2010 @ 8:18 pm
can`t make array from this big file. I wanna set doenlaod speed 1 mb (for test) and apps get more time for make array of local file
Comment by Garik — November 3, 2010 @ 8:19 pm
WOWWWW i see LocalFileLoader great!!!
i wanna load files from local and share to other with buffer from advanstion sample.
please help
Comment by Garik — November 3, 2010 @ 8:56 pm
anywhere file 250 mb cant seed.
Comment by Garik — November 3, 2010 @ 9:24 pm
if i can load file from local – like url and set speed of download 1-2 mb like i get it from http/
Comment by Garik — November 3, 2010 @ 9:24 pm
[...] takeaway -Â I need to lean about appendByte in OSMF. And [...]
Pingback by AdobeMAX, one week later – my list* | @radley — November 5, 2010 @ 3:34 am
Hello, Tom. I try to run Sender.swf and Reciever.swf, that I compile from your source code. But video from Sender.swf don’t appear in Reciever.swf. And don’t appear confirm about using flash peering network. And don’t appear setupGroup in Sender and Reciever.
I’m using Flash Player 10.1 and mxmlc version 4.1.0 build 16076.
What can be the problem?
Thanks for any help.
Comment by brenta — January 11, 2011 @ 10:10 am
Hi Tom,
Looking at your implementation of a Main.mxml VOD P2P solution it seems to bog down and crash the browser when loading a larger flv file from the server. Is there a memory leak in your URLByteLoader.as or somewhere else??
Comment by Denis — February 15, 2011 @ 11:52 am
Hello Tom ~
thx for your sharing!
Is it possible to use mp4 format , cas I try but it doesn’t work ?
Comment by Ray — February 17, 2011 @ 4:29 am
hello tom
you wrote
Hi Joao, absolutely. But for live stream it’s better to use Multicast – read here: http://www.flashrealtime.com/multicast-explained-flash-101-p2p/
this means i only have to add
groupspec.multicastEnabled = true; ?
do your have an example for live streaming multicast
source from an url?
best regards
arnold
Comment by arnold — April 8, 2011 @ 12:34 pm
Hi Tom, I have a task to deliver live video stream from media encoder (or webcam) to 100 000 users. I’m wondering is it possible to use p2p to reduce bandwith from servers in my case.
What is the approx % of traffic that could be delivered using p2p?
Do I need Stratus or FMS Enterprice vesion for this?
Do you have some sample player application that could get stream from media server and share this stream with others via p2p.
–
Thanks,
Volodymyr
Comment by Voa — May 14, 2011 @ 1:50 pm
Hi Volodymyr, it’s definitely possible and it will reduce the streaming costs. You need FMS Enterprise or get Amazon Web Services FMES (http://www.adobe.com/products/flashmediaserver/amazonwebservices/) or Influxis.com P2P FMES hosting.
Comment by tom — May 16, 2011 @ 1:15 pm
Hi Tom,
Great example! I wondered if I want to build a player to player app, for instance an air player running as server and other mobile players as clients. Then I would need to share several videos simultaneously (for different clients requesting different videos). So to “stream” to each client with the video they requested then I would need to create a netgroup for each different streaming isn’t it?
thanks in advanced
Comment by Polaco — June 23, 2011 @ 4:51 pm
How Can i distribute live stream among peers…plz help
Comment by Usama — July 21, 2011 @ 9:29 pm
Hi
I would like to know what would happen if we deliberately remove some chunks of a video file at the receiving end(after sending all chunks). in this case, will the appendBytes function still stream the data simply ignoring the missing chunks and appending the next available chunks?
Or, do we need to manually recreate an FLV out of the available chunks?. Please help. Thank you
Comment by aditya — August 3, 2011 @ 12:11 am
Hey Tom I converted the main(mxml) file to work in Flash and tweaked it to work over a lan – All is well except when receiving in the fileShareComplete function I get an error of an invalid parameter error for the appendBytes action (last chunk traced out as null)
I can trap the error ok but the the video playing seems to be a chunk short of complete
Any suggestions
Comment by Carl — August 13, 2011 @ 9:41 pm
Piggy back on the above –the problem appears to be in the
URLByteLoader class as the happens on the down load buttons as well
Comment by Carl — August 14, 2011 @ 12:36 am
Hi Tom,
I have a task to establish the following multi-to-one
scenario for HD VoD rtmp/http streaming:
- Each media stream within a presentation can reside on different server.
- The client automatically establishes several concurrent control session with the different media server.
- Media synchronization is performed on client side.
Do you have any sample player application for the above given scenario?
If you need more information, please contact me.
Sincerely,
Evgeny
Comment by Evgeny — January 4, 2012 @ 9:55 pm