Versions Compared

Key

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

Example of Android application for stream recording

This streamer can be used to publish and record WebRTC video stream on Web Call Server.

On the screenshot below the example is displayed when connection to server is closed and stream publication is stopped.
In the URL specified in the input field

  • 192.168.2.104 is the address of the WCS server
  • testStream is the stream name

Above the input field video from the camera is displayed.
Under the input field are located

  • download link for the recording of published stream
  • media player, which can be used to play the recording

Image Added

Work with code of the example

To analyze the code, let's take class StreamRecordingActivity.java of the stream-recording example version with hash 4ed4c6d77, which can be downloaded with corresponding build 1.0.1.3.

1. Initialization of the API. line 72

Code Block
languagejs
themeRDark
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 Record button is clicked. line 109

Code Block
languagejs
themeRDark
session = Flashphoner.createSession(sessionOptions);


Session is created with method createSession(), to which object SessionOptions (line 103) with the following parameters is passed

  • URL of WCS server
  • SurfaceViewRenderer, which will be used to display video from the camera

Callback functions for session events are added (line 114)

  • onConnected() - will be called when connection is successfully established
  • onDisconnection() - will be called when connection is closed
Code Block
languagejs
themeRDark
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 224

Code Block
languagejs
themeRDark
session.connect(new Connection());


3. Video streaming.

After establishing connection to the server, new video stream is created with method Session.createStream(). line 141
Object StreamOptions (line 135) with is passed to the method

  • name of the stream
  • true for parameter 'record' - to enable stream recording
Code Block
languagejs
themeRDark
publishStream = session.createStream(streamOptions);


Callback function for processing stream statuses is added. (line 146)

Code Block
languagejs
themeRDark
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 173

Code Block
languagejs
themeRDark
publishStream.publish();


4. Filename of the recording. line 158

When the stream is published, method Stream.getRecordName() is used to get the filename of the stream recording.

Code Block
languagejs
themeRDark
recordFilename = stream.getRecordName();


5. Download link. line 201

Stream recordings are saved to directory WCS_HOME/client/records.
When the session is closed, download link for the recording is formed.

Code Block
languagejs
themeRDark
String url = "http://" + uri.getHost() +":9091/client/records/" + recordFilename;


Here

  • uri.getHost() - address of the WCS server
  • recordFilename - filename of the recording

URL of the file is used to play the recording in the media player of the example.

6. Disconnection. line 236

Method Session.disconnect() is called to close connection to the server.

Code Block
languagejs
themeRDark
session.disconnect();