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.
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 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. 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 | ||||
---|---|---|---|---|
| ||||
Flashphoner.init({flashMediaProviderSwfLocation: '../../../../media-provider.swf'}); |
2. Connection to server. line 33Connection to server is established when Connect button is clicked.
Flashphoner.createSession() code
Code Block | ||||
---|---|---|---|---|
| ||||
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(); }); |
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 118
...
3. Receiving the event confirming successful connection
ConnectionStatusEvent ESTABLISHED code
Code Block | ||||
---|---|---|---|---|
| ||||
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(), and function publish() is called to publish the stream. code
When stream is created, the following parameters are passed
...
Code Block | ||||
---|---|---|---|---|
| ||||
session.createStream({ name: streamName, display: localVideo, cacheLocalResources: true ... }).on(STREAM_STATUS.PUBLISHING, function(stream)publish(); |
5. Receiving the event confirming successful streaming
StreamStatusEvent PUBLISHING code
Code Block | ||||
---|---|---|---|---|
| ||||
session.createStream({ setStatus("#publishStatus", STREAM_STATUS.PUBLISHING);name: streamName, display: localVideo, onPublishing(stream);cacheLocalResources: true }).on(STREAM_STATUS.UNPUBLISHEDPUBLISHING, function(stream){ setStatus("#publishStatus", STREAM_STATUS.UNPUBLISHEDPUBLISHING); onUnpublishedonPublishing(stream); }).on(STREAM_STATUS.FAILEDUNPUBLISHED, function(){ setStatus("#publishStatus", STREAM_STATUS.FAILED); onUnpublished();... }).publish(); |
...
on(STREAM_STATUS. |
...
FAILED |
...
In the example, when STREAM_STATUS.PUBLISHING status is received, function onPublishing() of the example is called; and when STREAM_STATUS.UNPUBLISHED and STREAM_STATUS.FAILED statuses are received - function onUnpublished().
Those functions make appropriate changes in controls of the interface.
4. Stop of streaming. line 70
The following method is called to stop video streaming
Code Block | ||||
---|---|---|---|---|
| ||||
stream.stop(); |
After calling the method, status STREAM_STATUS.UNPUBLISHED should arrive to confirm stop of streaming from the server side.
5. Playback of video stream. line 137
...
, function(){
...
}).publish(); |
6. Stream playback
session.createStream(), and function play() is called to play the stream 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 the video playback will be displayed
Code Block | ||||
---|---|---|---|---|
| ||||
session.createStream({
name: streamName,
display: remoteVideo
...
}).play(); |
7. Receiving the event confirming successful stream playback
StreamStatusEvent PLAYING code
Code Block | ||||
---|---|---|---|---|
| ||||
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() { setStatus("#playStatus", ... }).on(STREAM_STATUS.STOPPED);FAILED, function() { onStopped();... }).on(STREAM_STATUS.FAILED, function(play(); |
8. Stream playback stop
stream.stop() code
Code Block | ||||
---|---|---|---|---|
| ||||
function onPlaying(stream) { setStatus$("#playBtn").text("#playStatus", STREAM_STATUS.FAILEDStop").off('click').click(function(){ $(this).prop('disabled', true); onStopped stream.stop(); }).play(prop('disabled', false); |
...
} |
9. Receiving the event confirming successful playback stop
StreamStatusEvent STOPPED code
Code Block | ||||
---|---|---|---|---|
| ||||
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 |
...
6. Stop of playback. line 94
...
, function() {
...
}).play(); |
10. Streaming stop
stream.stop(). code
Code Block | ||||
---|---|---|---|---|
| ||||
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
Code Block | ||||
---|---|---|---|---|
| ||||
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();
|