Session is undefined - Two_way_streaming.js

Hi,

What can be the problem, that i get "session is undefined" in firefox, and some times in chrome. Some times visa/versa.

I get this error: "Uncaught TypeError: session is undefined"

The following line fails:
var session = Flashphoner.getSessions()[0];
constraints = {
audio: true,
video: true
};
if (constraints.video) {
constraints.video = {
width: 640,
height: 480
};
}
if (Browser.isSafariWebRTC()) {
Flashphoner.playFirstVideo(localVideo, true);
}

session.createStream({
name: streamName,
display: localVideo,
constraints: constraints,
cacheLocalResources: true,
receiveVideo: false,
receiveAudio: false

here is attachment from code:
1612092706442.png

It does not know what is session after i get it as var session = Flashphoner.getSessions()[0]; It says it a TypeError - session is undefined

what can be the problem ?

Eyal Klein
 

Max

Administrator
Staff member
Good day.
I get this error: "Uncaught TypeError: session is undefined"
This means there is no websocket session established. In this case, Flashphoner.getSessions() returns a zero lenght arry, so its first item will be undefined.
You should check Flashphoner.getSessions() array length, for example:
Code:
var sessions = Flashphoner.getSessions();
if(sessions.length > 0) {
     var session = sessions[0];
     ...
}
Please also note that playFirstVideo returns a promise, and stream publishing/plkaying should be invoked it context of this promise resolution, for example
Code:
Flashphoner.playFirstVideo(localVideo, true, PRELOADER_URL).then(function() {
    session.createStream({
        name: streamName,
        display: localVideo,
        constraints: constraints,
        cacheLocalResources: true,
        receiveVideo: false,
        receiveAudio: false
        ...
    }).publish();
});
 
Hi Max, thank you for your answer.

About this code:
var sessions = Flashphoner.getSessions();
if(sessions.length > 0) {
var session = sessions[0];
...
}

I understand, but i don't unser stand about this:

Flashphoner.playFirstVideo(localVideo, true, PRELOADER_URL).then(function() {
session.createStream({
name: streamName,
display: localVideo,
constraints: constraints,
cacheLocalResources: true,
receiveVideo: false,
receiveAudio: false
...
}).publish();
});

in your examples this code line "Flashphoner.playFirstVideo" is being revoked when:
"if (Browser.isSafariWebRTC() || Flashphoner.getMediaProviders()[0] === "MSE")" - in playStream
or
"if (Browser.isSafariWebRTC()) { " - in publishStream

Please explain where your code example needs to be placed.

also what should he set this "PRELOADER_URL"

attached is my two_way_streaming.js.

P.S.
In Opera / Firefox i get different error:
WebSocket connection to 'wss://[WCS]:8443/' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID

Thank you,
Eyal
 

Attachments

Last edited:

Max

Administrator
Staff member
Please explain where your code example needs to be placed.
You should use playFirstVideo in Safari browser only. This function returns a promise. You should start punlishing or playback only when the promise returned by this funsction is resolved. Please see Two Way Streaming example code on GitHub:
Code:
function publishBtnClick() {
    if (validateForm("streamerForm")) {
        $('#publishStream').prop('disabled', true);
        $(this).prop('disabled', true);
        if (Browser.isSafariWebRTC()) {
            Flashphoner.playFirstVideo(localVideo, true, PRELOADER_URL).then(function() {
                // This funsction will be invoked only when promise is re
                publishStream();
            });
            return;
        }
        publishStream();
    }
}
...
function publishStream() {
    var session = Flashphoner.getSessions()[0];
    var streamName = $('#publishStream').val();

    session.createStream({
        name: streamName,
        display: localVideo,
        cacheLocalResources: true,
        receiveVideo: false,
        receiveAudio: false
        ...
    }).publish();
}
also what should he set this "PRELOADER_URL"
This is video file URL to play in video element. The promise mentioned above will be resolved only if this file is played succesfully. The file may be blank screen of 1 second length. The example file is shipped with Web SDK /usr/local/FlashphonerWebCallServer/client2/examples/demo/dependencies/media/preloader.mp4
Code:
var PRELOADER_URL = "../../dependencies/media/preloader.mp4";
In Opera / Firefox i get different error:
WebSocket connection to 'wss://[WCS]:8443/' failed: Error in connection establishment: net::ERR_CERT_AUTHORITY_INVALID
You should import a valid SSL certificate to server to establish Websocket connection and publish WebRTC.
 
Hey Max,

Thank you for your explanation. All works now.

The only think that i need to know, how do i play with resolution. What i see in my camera - is not what is shown on other side.

Is there a "quality" parameter i'm missing ? buffer rate ? etc ?

Thank you,
Eyal
 

Max

Administrator
Staff member
The only think that i need to know, how do i play with resolution. What i see in my camera - is not what is shown on other side.
The publishing browser can change stream resolution and bitrate on the fly if it does not fit to the channel. So if you're publishing 1280x720, but on players side the resolution is 960x540, this means you channel is not enough to publish 720p.
So you have to lower publishing resolution and/or bitrate, or change the network (from 3G to LTE, from LTE to Wi-Fi, from Wi-Fi to cable), or use the server instance in datacenter closer to the publisher.
 

Max

Administrator
Staff member
How is this done ?
Publishing resolution and bitrate are set with constraints:
Code:
    session.createStream({
        name: streamName,
        display: localVideo,
        cacheLocalResources: true,
        receiveVideo: false,
        receiveAudio: false,
        constraints: {
               audio: true,
               video: {
                      width: 640,
                      height: 360,
                      maxBitrate: 500
               }
        }
        ...
    }).publish();
Please see details here
 
Top