Skip to main content
February 18, 2007
Answered

NetStream Fails to Initialize

  • February 18, 2007
  • 1 reply
  • 444 views
Hey Everyone -

I have a FMS application I'm working on that, to debug, I've gone all the way back to the bare basics and have created a simple test FLA to see what's going on. I have one video object on the stage named "VLP" (in both the library and the instance name).

I'm connecting to a working installation of Flash Media Server 2 located on a computer here in the office on the local area network. Using the Flash Media Server server administration console, I can see that the SWF, when I test it (cmd+enter) connects just fine, but the NetStream instance DOES NOT "go".

Here's sample output when testing:
NetConnection.onStatus called: (338 ms)
description: Connection succeeded.
code: NetConnection.Connect.Success
level: status


Now I have an almost identical trace setup for NetStream.onStatus, but I get nothing. Furthermore, using the Flash Debugger, it shows that the NetStream instance I have HAS NOTHING INSIDE IT. In other words, it's totally blank and empty, as if it were never declared.

So here's my CLIENT side code:
------------------------------------------------
var VLP:Video;
var nc:NetConnection = new NetConnection();
var ns:NetStream = new NetStream(nc);

ns.onStatus = function(infoObject2:Object) {
trace("NetStream.onStatus called: ("+getTimer()+" ms)");
for (var prop in infoObject2) {
trace("\t"+prop+":\t"+infoObject2[prop]);
}
trace("");
}



nc.onStatus = function(infoObject:Object) {
trace("NetConnection.onStatus called: ("+getTimer()+" ms)");
for (var prop in infoObject) {
trace("\t"+prop+":\t"+infoObject[prop]);
}
trace("");
}


//Assign Video.
VLP.attachVideo(ns);


//Connect
nc.connect("rtmp://apollo/VLP/About");



//Play.
ns.play("About");

---------------------------------------------------------


And my SERVER side code:
--------------------------------------------------------------
load("components.asc");

/* Copyright 2004 Macromedia, Inc. All rights reserved.
The following is Sample Code and is subject to all restrictions
on such code as contained in the Macromedia Flash Communication
Server MX 1.5 End User License Agreement .
*/

application.onConnect = function(p_client, p_autoSenseBW)
{
//Add security here

this.acceptConnection(p_client);

if (p_autoSenseBW)
this.calculateClientBw(p_client);
else
p_client.call("onBWDone");
}

Client.prototype.getStreamLength = function(p_streamName) {
return Stream.length(p_streamName);
}

Client.prototype.checkBandwidth = function() {
application.calculateClientBw(this);
}

application.calculateClientBw = function(p_client)
{
p_client.payload = new Array();
for (var i=0; i<1200; i++){
p_client.payload = Math.random(); //16K approx
}

var res = new Object();
res.latency = 0;
res.cumLatency = 1;
res.bwTime = 0;
res.count = 0;
res.sent = 0;
res.client = p_client;
var stats = p_client.getStats();
var now = (new Date()).getTime()/1;
res.pakSent = new Array();
res.pakRecv = new Array();
res.beginningValues = {b_down:stats.bytes_out, b_up:stats.bytes_in, time:now};
res.onResult = function(p_val) {
var now = (new Date()).getTime()/1;
this.pakRecv[this.count] = now;
//trace( "Packet interval = " + (this.pakRecv[this.count] - this.pakSent[this.count])*1 );
this.count++;
var timePassed = (now - this.beginningValues.time);

if (this.count == 1) {
this.latency = Math.min(timePassed, 800);
this.latency = Math.max(this.latency, 10);
}


//trace("count = " + this.count + ", sent = " + this.sent + ", timePassed = " + timePassed);

// If we have a hi-speed network with low latency send more to determine
// better bandwidth numbers, send no more than 6 packets
if ( this.count == 2 && (timePassed<2000))
{
this.pakSent[res.sent++] = now;
this.cumLatency++;
this.client.call("onBWCheck", res, this.client.payload);
}
else if ( this.sent == this.count )
{
// See if we need to normalize latency
if ( this.latency >= 100 )
{ // make sure we detect sattelite and modem correctly
if ( this.pakRecv[1] - this.pakRecv[0] > 1000 )
{
this.latency = 100;
}
}

delete this.client.payload;
// Got back responses for all the packets compute the bandwidth.
var stats = this.client.getStats();
var deltaDown = (stats.bytes_out - this.beginningValues.b_down)*8/1000;
var deltaTime = ((now - this.beginningValues.time) - (this.latency * this.cumLatency) )/1000;
if ( deltaTime <= 0 )
deltaTime = (now - this.beginningValues.time)/1000;

var kbitDown = Math.round(deltaDown/deltaTime);

trace("onBWDone: kbitDown = " + kbitDown + ", deltaDown= " + deltaDown + ", deltaTime = " + deltaTime + ", latency = " + this.latency + "KBytes " + (stats.bytes_out -
this.beginningValues.b_down)/1024) ;

this.client.call("onBWDone", null, kbitDown, deltaDown, deltaTime, this.latency );
}
}
res.pakSent[res.sent++] = now;
p_client.call("onBWCheck", res, "");
res.pakSent[res.sent++] = now;
p_client.call("onBWCheck", res, p_client.payload);
}

-----------------------------------------------------------------

Now, specifically, my "symptom" is that the video itself won't play, even though I've demonstrated that this application will connect just fine. Attempts to trace and debug the NetStream object have shown that it seems like NetStream is getting instantiated, but it doesn't have any status or anything inside it.

My application is setup like this:
/opt/macromedia/fms/applications/VLP/
-> main.asc
-> streams/
-> -> About/
-> -> -> About.flv

And just for clarity, "apollo" is a server in the office here with a static IP address that I've programmed into the hosts file on all computers. So there's no problem "finding" it.

Does anyone have any theories as to what I could be doing wrong here?

I appreciate any pointers or help anyone can provide.
    This topic has been closed for replies.
    Correct answer
    Try waiting until after your netconnection.onStatus event is fired before you try playing the stream. If you try to play a stream before the connection is established, nothing happens.

    nc.onStatus= function(info){
    if (info.code == "NetStream.Connect.Success"){
    initStream();
    }
    }

    function initStream(){
    ns = new NetStream(nc);
    ns.onStatus = function(info){
    trace(info.code);
    }
    ns.play("About");
    }
    }

    1 reply

    Correct answer
    February 18, 2007
    Try waiting until after your netconnection.onStatus event is fired before you try playing the stream. If you try to play a stream before the connection is established, nothing happens.

    nc.onStatus= function(info){
    if (info.code == "NetStream.Connect.Success"){
    initStream();
    }
    }

    function initStream(){
    ns = new NetStream(nc);
    ns.onStatus = function(info){
    trace(info.code);
    }
    ns.play("About");
    }
    }

    February 18, 2007
    Thanks a bunch Jay - that did it! There's one typo in your code that I noticed and wanted to point out for anyone googling for a similar solution:

    Change "NetStream.Connect.Success" to "NetConnection.Connect.Success" - NetStream doesn't exist yet at that if statement.

    I'm also running into an issue where the audio starts well before the video, and only after about 5 seconds will the video kick in. I think I can solve this with some buffering though.

    Thanks again Jay!