开发者

Servering mulitple flv files from custom webserver while keeping the sockets open stops after the 6th connection

开发者 https://www.devze.com 2023-01-04 19:23 出处:网络
This is driving me nuts and because it\'s a combination of various technologies together I have a hard time tracking the problem.

This is driving me nuts and because it's a combination of various technologies together I have a hard time tracking the problem.

Scenario

I wrote a tiny HTTP server that serves .flv files; these flv files are encoded on the spot. The flv files are encoded with adobe's screencodec (I started with libflv but that was too buggy so I ended up implementing my own flv encoder). For the problem below it doesn't matter which codec I use or if I serve plain flv files instead of on-the-spot generated ones (the problem开发者_开发知识库 also occurs if I just send 12 h264 encoded files). I also have a swf that I coded in as3. The as3 opens 12 connections to my server and requests 12 different flv's and just displays them as 12 different videos on the page. I tested the setup with Firefox and chrome and am using the latest flash client (version 10). The HTTP server is pseudo streaming those files; e.g. it tells the client the file is of mime type video/x-flv, tells the filesize is 2GB and just starts streaming. For sake of completeness here's a header it generates:

HTTP/1.1 200 OK
Content-Type: video/x-flv
Accept-Ranges: bytes
Content-Length: 2147483647
Date: Mon, 28 Jun 2010 12:53:09 GMT
Host: localhost
Keep-Alive: timeout=100000, max=100
Connection: Keep-Alive
Server: Foobar

So what happens is that the swf asks for a flv, the server gets the request, starts encoding a flv and starts sending that bytestream over the open tcp connection. Both the client (Browser) and the server are running on localhost. The server will also serve crossdomain.xml stuff (both on port 843 as adobe wants it) as well as on port 80. Flash indeed asks for the crossdomain file and in the file I allow everything. In flash/CS4 I enabled all settings that allow the swf to do its networking stuff.

This all works fine except that flash stops after the 6th flv. If I close the socket after I send a couple of frames then the swf will happily display all 12. But if I keep the socket open (which is the whole point if I want to keep serving) it just quits after six Netstream connections. Ergo, it will send 6 requests to the server and then stops. On the as3 side I tried all kind of things (using one NetConnection, tweeking the buffertime) but nothing seems to help.

The as3 code looks like this:

var connection = new NetConnection();
connection.connect(null); 
var stream = new NetStream(connection);
var listener:Object = new Object();
listener.onMetaData = onMetaData;
listener.onCuePoint = onCuePoint;
// stream.checkPolicyFile=true;
stream.client = listener;
stream.bufferTime = 0; 
var video = new Video(640,480);
video.attachNetStream(stream);
// connection.maxPeerConnections = 32;
stream.play("http://localhost:80/" + n +".flv"); 

And I loop over this code 12 times to generate the requests.

So what gives; why does flash stop after the 6th connection. I google'd whole week trying to find whether flash has some maximum on either its sockets or NetConnections or NetStreams but neither Adobe nor anyone else seems to mention this anywhere.

Another odd thing that I found out is that the 6 open connections are only on a per-port basis, so I can open 6 flv's on port 80 and another 6 on port 81 ... I could probably rewrite my implementation to serve on different ports but that would be a pain (especially considering people have firewalls) and it just doesn't make sense.

Either I am doing something wrong in as3, or flash does have some limitation that I am not aware of and that's not documented, or maybe there's some problem in my HTTP headers.

Anybody got a clue what might be the problem? I am really running out of options, I've been fiddling with this for a week now and it's driving me nuts!


I believe that when Flash opens an HTTP connection, it goes through the browser's connection mechanisms which has limits to the maximum number of concurrent persistent connections to a single host, as well as total concurrent persistent connections.

You could test this theory by increasing the maximum number of connections in Firefox by going to the config URL about:config and filtering on "network.http". There should be some settings such as "network.http.max-persistent-connections-per-server" that you can tweak.

If this turns out to be the limiting factor, then you may want to research other ways to serve your videos, such as RTMP which doesn't go through the browser for connections, but rather connects directly from Flash to your server.

0

精彩评论

暂无评论...
验证码 换一张
取 消