The problem
I have to replace the mx:VideoDisplay component in an existing Flex project with a custom made component.
To achieve this, I have to conform to its current Interface, so my component receives the video urls (via source parameter) in the form of either:
- /data/myflvfile.flv (if the file is played locally)
- rtmp://streamcloud.myserver.com/cfx/st/somedirectory/myflvfile.flv (if the file is streamed from Cloudfront)
My new component is based on NetConnection and NetStream. With any of the entries above I have to split the input in two strings: one for the NetConnection.connect(NetConnectionStr) method and the other for the NetStream.play(NetStreamStr) method. For instance:
- Given /data/myflvfile.flv then:
- NetConnectionStr = null
- NetStreamStr = "/data/myflvfile"
- Given rtmp://streamcloud.myserver.com/cfx/st/somedirectory/myflvfile.flv then:
- NetConnectionStr = "rtmp://streamcloud.myserver.com/cfx/st"
- NetStreamStr = "somedirectory/myflvfile"
- Given rtmp://streamcloud.myserver.com/cfx/st/somedirectory/subdirectory/myflvfile.flv then:
- NetConnectionStr = "rtmp://streamcloud.myserver.com/cfx/st"
- NetStreamStr = "somedirectory/subdirectory/myflvfile"
Building the two strings is very obvious for the "local files" case, but it gets tricky in the others. The problem is that I have no smart way to guess what part of the input is the server URL and what part is the stream name + directory structure.
In some examples found on the Internet, people are simply guessing that the last part of the source (what is after the last "/" found) is the NetStream name. In my case, this is not always true because the streams may be in subdirectories on the server. It is even worse because server names may contain "/" characters!
Strategies to solve it
Connecting to server, retrieving its real URL, finding stream name
As NetConnection seems to be "smart" enough, my first attempt was to invoke connect method with the full source url. For instance, given rtmp://streamcloud.myserver.com/cfx/st/somedirectory/subdirectory/myflvfile.flv
NetConnection is a success with:
- connection.connect(rtmp://streamcloud.myserver.com/cfx/st/)
- connection.connect(rtmp://streamcloud.myserver.com/cfx/st/somedirectory/)
- ...
- connection.connect(rtmp://streamcloud.myserver.com/cfx/st/somedirectory/subdirectory/myflvfile.flv)
Then I was hoping to retrieve somehow the "real" url of the server (rtmp://streamcloud.myserver.com/cfx/st) so I would be able to guess the stream part (somedirectory/subdirectory/myflvfile).
Unfortunately, I found no way to get the real server adress out of a NetConnection object (connection.uri returns back the exact input). So this seems to be a dead end.
Connecting to server, iteratively retrieving streams
Another strategy could be to connect to the server and then try iteratively (starting from the very end) to play streams until it works:
Given rtmp://streamcloud.myserver.com/cfx/st/somedirectory/subdirectory/myflvfile.flv :
- Try 1: stream.play(myflvfile) FAIL
- Try 2: stream.play(subdirectory/myflvfile) FAIL
- Try 3: stream.play(somedirectory/subdirectory/myflvfile) SUCCESS
But this is a very ugly way to proceed, and I am looking for a better solution.
A better solution?
Is there any good method to solve this issue? Does anyone knows how they are doing it in the original VideoDisplay component (if it is based on NetConnection/NetStream objects?)?
Thanks in advance for any help and/or comments on this issue :)