AS3, NetConnection/NetStream: Is there a smart way to split source url?
Asked Answered
B

3

8

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:

  1. /data/myflvfile.flv (if the file is played locally)
  2. 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:

  1. Given /data/myflvfile.flv then:
    • NetConnectionStr = null
    • NetStreamStr = "/data/myflvfile"
  2. Given rtmp://streamcloud.myserver.com/cfx/st/somedirectory/myflvfile.flv then:
    • NetConnectionStr = "rtmp://streamcloud.myserver.com/cfx/st"
    • NetStreamStr = "somedirectory/myflvfile"
  3. 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 :)

Baa answered 22/11, 2011 at 14:33 Comment(1)
Thank you for your comment! I've tried to state it as clearly as possible...Baa
V
0
var match:Array = path.match('rtmp://(.*/.*/.*/)');
var server:String = match[0];
var file:String = removeExtension(path.replace(match[1], ''));
var channel:String = removeExtension(path.replace(match[2], ''));

ref: http://onyx-vj.googlecode.com/svn/trunk/Onyx-VJ%203.0.0/OnyxCore/onyx/core/ProtocolRTMP.as

Vitrify answered 28/11, 2011 at 0:8 Comment(1)
Thank you very much for your answer! Unfortunately, pattern matching on path separators is not a valid option. Case 2. (rtmp://streamcloud.myserver.com/cfx/st/somedirectory/myflvfile.flv) and case 3. (rtmp://streamcloud.myserver.com/cfx/st/somedirectory/subdirectory/myflvfile.flv) fail because there is no way to guess if the "/" are part of server url or stream path. The problem is mainly caused by having the possibility of "/" in both the server and stream coupled with the need to provide the exact path (from server's root) to the NetStream object.Baa
B
0

Without having tested a solution against a running rtmp service, it's hard to verify this absolutely but consider the following refinement of your second, less desirable strategy:

  • create a NetConnection as normal

  • the first time connect() is successful, iterate through possible combinations of service and asset ("rtmp://streamcloud.myserver.com/cfx/" & "/st/somedirectory/subdirectory/myflvfile.flv", etc), and create concurrent NetStreams for each one -- that way you don't have to wait for sequential discovery

  • if one of the NetStreams dispatches a success event, attach it to the display and drop all the other ones on the floor (they will probably fail out anyway)

  • optionally you could store the successful service / asset pair that worked, to speed things up if the connection is to be reused

It seems like a vexing problem!

Beriberi answered 14/12, 2011 at 4:56 Comment(0)
T
0

I'm confused by your question. Is there no part of your connection to rtmp that is consistent? Are you swapping streaming providers regularly? If that is consistent then at least half the battle is won. How I go about connecting to rtmp is what goes in the connect is always the same. The latter part is more dynamic which is passed in, in the same way; [folder space]/[video name]. I'll do all my parsing only on what is passed in but then I have a single rtmp provider.

Terence answered 25/7, 2013 at 18:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.