Versions Compared

Key

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

...

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

Image RemovedImage Added

Work with code of the example

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

1. Initialization of the API. line 72

...

...

Flashphoner.init(

...

)

...

 code

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
languagejsjava
themeRDark
session = Flashphoner.createSessioninit(sessionOptionsthis);

Session is created with method createSession(), to which object SessionOptions (line 103) with 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

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
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
languagejsjava
themeRDark
session.onconnect(new SessionEvent() {
    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 onDisconnectionrun(final Connection connection) {
            mStartButton.setText(R.string.action_stop);
            mStartButton.setTag(R.string.action_stop);
    }
});

Method Session.connect() is called to establish connection with WCS server. line 224

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

3. Video streaming.

...

        mStartButton.setEnabled(true);
            mStatusView.setText(connection.getStatus());
            ...
        }
    });
}

5. Video stream creation

Session.createStream(). line 141, 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
languagejsjava
themeRDark
StreamOptions streamOptions = new StreamOptions(streamName);
streamOptions.setRecord(true);

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

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

Code Block
languagejs
themeRDark


/**
  * 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() is called to publish the stream. line 173 code

Code Block
languagejsjava
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();

4. Filename of the recording. line 158

...


        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() is used to get the filename of the stream recordingmethod.

Code Block
languagejsjava
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;

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

               }
               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

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

            /**
              * After disconnection, download link for the recording of the published stream is displayed, and the recording can be played in the media player of the application.
              */
            if (recordFilename != null) {
                /**
                  * Download link is formed.
                  * Stream recordings are saved to directory WCS_HOME/client/records on the server.
                  */
                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(;
                mRecordedLink.setText(url);
                Linkify.addLinks(mRecordedLink, Linkify.WEB_URLS);

                MediaController mediaController = new MediaController(StreamRecordingActivity.this);
                mediaController.setAnchorView(mRecordedVideoView);
                mRecordedVideoView.setMediaController(mediaController);
                mRecordedVideoView.setVideoURI(Uri.parse(url));

                /**
                  * Playback of the recording in the media player is started.
                  */
                mRecordedVideoView.start();
            }
        }
    });