Player example
This player can be used to play any type of stream on Web Call Server
...
On the screenshot below an RTSP stream is being playing in the web player.
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 player
To analyze the code, let's take the version of file player.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 8API 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 58Connection to server is established when Start button is clicked.
Flashphoner.createSession() code
Code Block | ||||
---|---|---|---|---|
| ||||
Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
setStatus(session.status());
//session connected, start playback
playStream(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
setStatus(SESSION_STATUS.DISCONNECTED);
onStopped();
}).on(SESSION_STATUS.FAILED, function(){
setStatus(SESSION_STATUS.FAILED);
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. Receiving the event confirming successful connection
ConnectionStatusEvent ESTABLISHED code
Code Block | ||||
---|---|---|---|---|
| ||||
Flashphoner.createSession({urlServer: url}).on(SESSION_STATUS.ESTABLISHED, function(session){
setStatus(session.status());
//session connected, start playback
playStream(session);
}).on(SESSION_STATUS.DISCONNECTED, function(){
...
}).on(SESSION_STATUS.FAILED, function(){
...
}); |
4. Playback of video stream. line 74
After establishing connection to the server, new video stream is created with method 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 (rtsp:// address in this case)
- remoteVideo - <div> element, in which the video will be displayed
Code Block | ||||
---|---|---|---|---|
| ||||
session.createStream({
name: streamName,
display: remoteVideo
...
}).play(); |
5. 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(stream.status()); onStarted(stream); }).on(STREAM_STATUS.STOPPED, function() { ... setStatus}).on(STREAM_STATUS.STOPPED); FAILED, function() { ... onStopped}).play(); |
6. Stop of playback.
stream.stop() code
Code Block | ||||
---|---|---|---|---|
| ||||
function onStarted(stream) { }$("#playBtn").text("Stop").on(STREAM_STATUS.FAILED, off('click').click(function() { setStatus(STREAM_STATUS.FAILED$(this).prop('disabled', true); onStoppedstream.stop(); }).play(prop('disabled', false); |
...
} |
7. Receiving the event confirming successful stream 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(STREAM_STATUS.STOPPED |
...
); onStopped(); }).on(STREAM_STATUS.FAILED |
...
4. Stop of playback. line 24
The following method is called to stop playback of video stream
Code Block | ||||
---|---|---|---|---|
| ||||
stream.stop(); |
...
, function() {
...
}).play(); |