Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Streamer example

This streamer can be used to publish the following types of streams on Web Call Server

...

Two videos are played on the page
- 'Local' - video from the camera
- 'Preview' - the video as received from the server

Code of the example

The path to the source code of the example on WCS server is:

...

Here host is the address of the WCS server.

Work with code of the streamer

To analyze the code, let's take the version of file streamer.js with hash cf0daabc6b86e21d5a2f9e4605366c8b7f0d27eb, which is available here and can be downloaded with corresponding build 0.3.18.1894.

1. Initialization of the API. line 13API is initialized after loading the page. For Flash support, the path to SWF file is passed to the

Flashphoner.init() method. code

Code Block
languagejs
themeRDark
Flashphoner.init({flashMediaProviderSwfLocation: '../../../../media-provider.swf'});


2. Connection to server. line 51Connection to server is established when Start button is clicked.

Flashphoner.createSession() code

Code Block
languagejs
themeRDark
Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
    //session connected, start streaming
    startStreaming(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
    setStatus(SESSION_STATUS.DISCONNECTED);
    $('#url').prop('disabled', false);
    onStopped();
}).on(SESSION_STATUS.FAILED, function(){
    setStatus(SESSION_STATUS.FAILED);
    $('#url').prop('disabled', false);
    onStopped();
});

Session is created with method createSession(). Callback function, which will be called in case of successfully established connection (status SESSION_STATUS.ESTABLISHED), is added.

3. Video streaming. line 68

...

3. Receiving the event confirming successful connection.

ConnectionStatusEvent ESTABLISHED code

Code Block
languagejs
themeRDark
Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
    //session connected, start streaming
    startStreaming(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
    ...
}).on(SESSION_STATUS.FAILED, function(){
    ...
});

4. Video streaming. 

session.createStream(), and function publish() is called to publish the stream.
When stream is created, the following parameters are passed
-  code

Parameters passed to createStream():

streamName - name of the stream;

localVideo - localVideo div- <div> element , in which video from camera will be displayedto display video from camera.

Code Block
languagejs
themeRDark
session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
    ...
}).publish();


5. Receiving the event confirming successful streaming

StreamStatusEvent PUBLISHING code


On receiving the event, preview stream is created with createStream(), and play() is called to play it.


Code Block
languagejs
themeRDark
session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
}).on(STREAM_STATUS.PUBLISHING, function(publishStream){
    setStatus(STREAM_STATUS.PUBLISHING);
    //play preview
    session.createStream({
        name: streamName,
        display: remoteVideo
        ...
    }).play();
    ...
}).on(STREAM_STATUS.PLAYING, function(previewStream){
        //enable stop button
        onStarted(publishStream, previewStream);publish();


6. Stop of preview playback.

previewStream.stop() code

Code Block
languagejs
themeRDark
function onStarted(publishStream, previewStream) {
    $("#publishBtn").text("Stop").off('click').click(function(){
        $(this).prop('disabled', true);
        previewStream.stop();
    }).prop('disabled', false);
}

7. Receiving the event confirming playback stop

StreamStatusEvent STOPPED code

Code Block
languagejs
themeRDark
    session.createStream({
        name: streamName,
        display: remoteVideo
    }).on(STREAM_STATUS.PLAYING, function(previewStream){
        ...
    }).on(STREAM_STATUS.STOPPED, function(){
        publishStream.stop();
    }).on(STREAM_STATUS.FAILED, function(){
          //preview failed, stop publishStream
...
    }).play();

8. Stop of streaming after stop of preview playback.

publishStream.stop() code

Code Block
languagejs
themeRDark
 session.createStream({
        name: streamName,
        display: remoteVideo
   if (publishStream}).statuson() == STREAM_STATUS.PUBLISHING) PLAYING, function(previewStream){
        ...
    setStatus}).on(STREAM_STATUS.FAILED);STOPPED, function(){
        publishStream.stop();
    publishStream.stop}).on(STREAM_STATUS.FAILED, function();{
        }...
    }).play();

9. Receiving the event confirming successful streaming stop

StreamStatusEvent UNPUBLISHED code

Code Block
languagejs
themeRDark
session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
}).on(STREAM_STATUS.UNPUBLISHEDPUBLISHING, function(publishStream){
    setStatus(STREAM_STATUS.UNPUBLISHED);
    //enable start button
    onStopped();
...
}).on(STREAM_STATUS.FAILEDUNPUBLISHED, function(){
    setStatus(STREAM_STATUS.FAILEDUNPUBLISHED);
    //enable start button
    onStopped();
}).publish();

When stream is created, callback functions for events STREAM_STATUS.PUBLISHING, STREAM_STATUS.UNPUBLISHED, STREAM_STATUS.FAILED can be added.

...

on(STREAM_STATUS.FAILED

...

STREAM_STATUS.UNPUBLISHED and STREAM_STATUS.FAILED - when one of these statuses is received, function onStopped() of the example is called to make appropriate changes in controls of the interface.

4. Stop of playback. line 31

The following method is called to stop playback of preview video stream

Code Block
languagejs
themeRDark
previewStream.stop();

After calling the method, status STREAM_STATUS.STOPPED should arrive to confirm stop of playback from the server side.

5. Stop of streaming after stop of preview playback. line 82

The following method is called to stop video streaming

Code Block
languagejs
themeRDark
publishStream.stop();

...

, function(){
    ...
}).publish();