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 Removed

Work with code of the example

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

1. Initialization of the API

Flashphoner.init() code

For initialization, object Сontext is passed to the init() method.

Code Block
languagejava
themeRDark
Flashphoner.init(this);

2. Session creation.

Flashphoner.createSession() code

Object SessionOptions with the following parameters is passed to createSession() method:

  • URL of WCS server
  • SurfaceViewRenderer, which will be used to display video from the camera
Code Block
languagejava
themeRDark
SessionOptions sessionOptions = new SessionOptions(url);
sessionOptions.setLocalRenderer(localRender);

/**
  * Session for connection to WCS server is created with method createSession().
  */
session = Flashphoner.createSession(sessionOptions);

3. Connection to the server.

Session.connect(). code

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

4. Receiving the event confirming successful connection

session.onConnected() code

Code Block
languagejs
themeRDark
@Override
public void onConnected(final Connection connection) {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            mStartButton.setText(R.string.action_stop);
            mStartButton.setTag(R.string.action_stop);
            mStartButton.setEnabled(true);
            mStatusView.setText(connection.getStatus());
            ...
        }
    });
}

5. Video stream creation

Session.createStream(), ActivityCompat.requestPermissions() code
Object StreamOptions (line 135) with is passed to the sreateStream() method

  • name of the stream
  • true for parameter 'record' - to enable stream recording
Code Block
languagejava
themeRDark
StreamOptions streamOptions = new StreamOptions(streamName);
streamOptions.setRecord(true);

/**
  * Stream is created with method Session.createStream().
  */
publishStream = session.createStream(streamOptions);

/**
  * Callback function for stream status change is added to display the status.
  */
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)) {
                   mStatusView.setText("RECORDING");

                   /**
                     * Filename of the recording is determined.
                     */
                   recordFilename = stream.getRecordName();
                   return;
               } else if (StreamStatus.FAILED.equals(streamStatus)) {
                   Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                   recordFilename = null;
               }
               mStatusView.setText(streamStatus.toString());
           }
       });
    }
});

6. Video stream publishing when permissions is granted

Stream.publish() code

Code Block
languagejava
themeRDark
case PUBLISH_REQUEST_CODE: {
    if (grantResults.length == 0 ||
           grantResults[0] != PackageManager.PERMISSION_GRANTED ||
           grantResults[1] != PackageManager.PERMISSION_GRANTED) {
        mStartButton.setEnabled(false);
        session.disconnect();
        Log.i(TAG, "Permission has been denied by user");
    } else {
        /**
          * Method Stream.publish() is called to publish stream.
          */
        publishStream.publish();
        Log.i(TAG, "Permission has been granted by user");
    }
}

7. Receiving the event confirming successful stream publishing

StreamStatusEvent PUBLISHING code

On this event, stream record filemane is defined with Stream.getRecordName() method.

Code Block
languagejava
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)) {
                   mStatusView.setText("RECORDING");

                   /**
                     * Filename of the recording is determined.
                     */
                   recordFilename = stream.getRecordName();
                   return;
               } else if (StreamStatus.FAILED.equals(streamStatus)) {
                   Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                   recordFilename = null;
               }
               mStatusView.setText(streamStatus.toString());
           }
       });
    }
});

8. Session disconnection.

Session.disconnect() code

Code Block
languagejava
themeRDark
mStartButton.setEnabled(false);

/**
  * Connection to WCS server is closed with method Session.disconnect().
  */
session.disconnect();

9. Receiving the event confirming successful disconnection

session.onDisconnection() code

On this event, the record file download link is formed, and local mediaplayer is launched to play the file

...

languagejava
themeRDark

...

Include Page
ANDROIDSDK1EN:Android Stream Recording
ANDROIDSDK1EN:Android Stream Recording