Example of streamer for Android
This streamer can be used to publish WebRTC video stream on Web Call Server.
On the screenshot below the example is displayed when a stream is being published.
In the URL specified in the input field
- 192.168.2.104 is the address of the WCS server
- test is the stream name
Two videos are played
- left - video from the camera
- right - the published video stream as received from the server
Work with code of the example
To analyze the code, let's take class StreamerActivity.java of the streamer example version with hash 4ed4c6d77, which can be downloaded with corresponding build 1.0.1.3.
1. Initialization of the API. line 67
Code Block | ||||
---|---|---|---|---|
| ||||
Flashphoner.init(this); |
For initialization, object Сontext is passed to the init() method.
2. Connection to server.
Session for connection to server is created when Start button is clicked. line 106
Code Block | ||||
---|---|---|---|---|
| ||||
session = Flashphoner.createSession(sessionOptions); |
Session is created with method createSession(), to which object SessionOptions (line 99) with the following parameters is passed
- URL of WCS server
- SurfaceViewRenderer, which will be used to display video from the camera
- SurfaceViewRenderer, which will be used to play the published video stream
Callback functions for session events are added (line 111)
- onConnected() - will be called when connection is successfully established
- onDisconnection() - will be called when connection is closed
Code Block | ||||
---|---|---|---|---|
| ||||
session.on(new SessionEvent() {
public void onConnected(final Connection connection) {
.....
}
public void onDisconnection(final Connection connection) {
.....
}
}); |
Method Session.connect() is called to establish connection with WCS server. line 222
Code Block | ||||
---|---|---|---|---|
| ||||
session.connect(new Connection()); |
3. Video streaming.
After establishing connection to the server, new video stream is created with method Session.createStream(). line 136
Object StreamOptions (line 131) with name of the stream is passed to the method.
Code Block | ||||
---|---|---|---|---|
| ||||
publishStream = session.createStream(streamOptions); |
Callback function for processing stream statuses is added. (line 141)
Code Block | ||||
---|---|---|---|---|
| ||||
publishStream.on(new StreamStatusEvent() {
@Override
public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (StreamStatus.PUBLISHING.equals(streamStatus)) {
.....
}
}
}
}); |
Method Stream.publish() is called to publish the stream. line 194
Code Block | ||||
---|---|---|---|---|
| ||||
publishStream.publish(); |
4. Playback of video stream.
When the stream is published, new stream is created to play the published stream. line 158
Object StreamOptions (line 153) with name of the published stream is passed when the stream is created.
Code Block | ||||
---|---|---|---|---|
| ||||
playStream = session.createStream(streamOptions); |
Callback function for processing stream statuses is added. (line 163)
Code Block | ||||
---|---|---|---|---|
| ||||
playStream.on(new StreamStatusEvent() {
.....
}); |
Method Stream.play() is called to play the stream. line 181
Code Block | ||||
---|---|---|---|---|
| ||||
playStream = session.createStream(streamOptions); |
5. Disconnection. line 234
Method Session.disconnect() is called to close connection to the server.
Code Block | ||||
---|---|---|---|---|
| ||||
session.disconnect(); |