embedding-webcam-broadcasting-html

Example with streamer and player on the same page

This example demonstrates how to publish a video stream while playing another one using the same web page.

 

 

Two-way-Streaming

 

Code of the example

The path to the source code of the example on WCS server is: /usr/local/FlashphonerWebCallServer/client2/examples/demo/streaming/two_way_streaming

two_way_streaming.css – file with styles
two_way_streaming.html – page of the client
two_way_streaming.js – script providing functionality for the example

This example can be tested using the following address: https://host:8888/client2/examples/demo/streaming/two_way_streaming/two_way_streaming.html

Here host is the address of the WCS server..

 

Work with code of the example

To analyze the code, let’s take the version of file two_way_streaming.js with hash f437576d88a6e24add9b5337e66ad03ff18ecc3c, which is available here and can be downloaded with corresponding build 0.5.0.1894.

1. Initialization of the API

API. Flashphoner.init() code

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

 

2. Connection to server

Flashphoner.createSession() code

Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
    setStatus("#connectStatus", session.status());
    onConnected(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
    setStatus("#connectStatus", SESSION_STATUS.DISCONNECTED);
    onDisconnected();
}).on(SESSION_STATUS.FAILED, function(){
    setStatus("#connectStatus", SESSION_STATUS.FAILED);
    onDisconnected();
});

 

3. Receiving the event confirming successful connection

ConnectionStatusEvent ESTABLISHED code

Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
    setStatus("#connectStatus", session.status());
    onConnected(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
    ...
}).on(SESSION_STATUS.FAILED, function(){
    ...
});

 

4. Video streaming.

session.createStream(), publish() code

When stream is created, the following parameters are passed

  • streamName – name of the stream
  • localVideo – div element, in which video from camera will be displayed
session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
    ...
}).publish();

 

5. Receiving the event confirming successful streaming

StreamStatusEvent PUBLISHING code

session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
}).on(STREAM_STATUS.PUBLISHING, function(stream){
    setStatus("#publishStatus", STREAM_STATUS.PUBLISHING);
    onPublishing(stream);
}).on(STREAM_STATUS.UNPUBLISHED, function(){
    ...
}).on(STREAM_STATUS.FAILED, function(){
    ...
}).publish();

 

6. Stream playback

session.createStream(), play() code.

When stream is created, the following parameters are passed

  • streamName – name of the stream (including the stream published on step above)
  • remoteVideo – div element, in which video playback will be displayed
session.createStream({
    name: streamName,
    display: remoteVideo
    ...
}).play();

 

7. Receiving the event confirming successful stream playback

StreamStatusEvent PLAYING code

session.createStream({
    name: streamName,
    display: remoteVideo
}).on(STREAM_STATUS.PLAYING, function(stream) {
    document.getElementById(stream.id()).addEventListener('resize', function(event){
        resizeVideo(event.target);
    });
    setStatus("#playStatus", stream.status());
    onPlaying(stream);
}).on(STREAM_STATUS.STOPPED, function() {
    ...
}).on(STREAM_STATUS.FAILED, function() {
    ...
}).play();

 

8. Stream playback stop

stream.stop() code

function onPlaying(stream) {
    $("#playBtn").text("Stop").off('click').click(function(){
        $(this).prop('disabled', true);
        stream.stop();
    }).prop('disabled', false);
}

 

9. Receiving the event confirming successful playback stop

StreamStatusEvent STOPPED code

session.createStream({
    name: streamName,
    display: remoteVideo
}).on(STREAM_STATUS.PLAYING, function(stream) {
    ...
}).on(STREAM_STATUS.STOPPED, function() {
    setStatus("#playStatus", STREAM_STATUS.STOPPED);
    onStopped();
}).on(STREAM_STATUS.FAILED, function() {
    ...
}).play();

 

10. Streaming stop

stream.stop(). code

function onPublishing(stream) {
    $("#publishBtn").text("Stop").off('click').click(function(){
        $(this).prop('disabled', true);
        stream.stop();
    }).prop('disabled', false);
}

 

11. Receiving the event confirming successful streaming stop

StreamStatusEvent UNPUBLISHED code

session.createStream({
    name: streamName,
    display: localVideo,
    cacheLocalResources: true
}).on(STREAM_STATUS.PUBLISHING, function(stream){
    ...
}).on(STREAM_STATUS.UNPUBLISHED, function(){
    setStatus("#publishStatus", STREAM_STATUS.UNPUBLISHED);
    onUnpublished();
}).on(STREAM_STATUS.FAILED, function(){
    ...
}).publish();

 

You can find full documentation in the Developer Guide

Streamer for Android SDK  |  Streamer for iOS SDK

Player for Android SDK  |  Player for iOS SDK

 

Related articles

iOS Safari 11 now supports WebRTC

Oh, latency, thou art a heartless bitch

Broadcast video stream from web page to Facebook and YouTube simultaneously

Deployment multiplayer webrtc technology broadcast with a web camera via server in 3 minutes

 

Related features

Broadcasting a WebRTC video stream with re-publishing as RTMP