Versions Compared

Key

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

Example of streamer for Android

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

...

  • 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 class StreamerActivity.java of the streamer example version with hash 4ed4c6d77, which can be downloaded with corresponding build build 1.0.1.338.

1. Initialization of the API. line 67

...

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 Start button is clicked. line 106

Code Block
languagejsjava
themeRDark
session = Flashphoner.createSessioninit(sessionOptionsthis);

Session is created with method createSession(), to which object SessionOptions (line 99) with 2. Session creation.

Flashphoner.createSession() code

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

  • 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
languagejava
themeRDark
SessionOptions sessionOptions = new SessionOptions(url);
sessionOptions.setLocalRenderer(localRender);
sessionOptions.setRemoteRenderer(remoteRender);

/**
  * 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
languagejsjava
themeRDark
session.on@Override
public void onConnected(final Connection connection) {
   runOnUiThread(new SessionEventRunnable() {
       @Override
       public void onConnected(final Connection connection) {
run() {
           mStartButton.setText(R.string.action_stop);
           mStartButton.setTag(R.string.action_stop);
           mStartButton.setEnabled(true);
           mStatusView.setText(connection.getStatus());

           /**
             * The options for the stream to publish are set.
             * The stream name is passed when StreamOptions object is created.
             }
*/
          public void onDisconnection(final Connection connection) {
 StreamOptions streamOptions = new StreamOptions(streamName);

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

...


}

5. Video stream creation

Session.createStream(), ActivityCompat.requestPermissions() code
Object StreamOptions with name of the stream is passed to the createStream() method.

Code Block
languagejsjava
themeRDark
session.connect(StreamOptions streamOptions = new ConnectionStreamOptions());

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
languagejs
themeRDark
publishStream = session.createStream(streamOptions);

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

js
Code Block
language
streamName);

/**
  * Stream is created with method Session.createStream().
  */
publishStream = session.createStream(streamOptions);
...
ActivityCompat.requestPermissions(StreamerActivity.this,
       new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA},
       PUBLISH_REQUEST_CODE);

6. Video stream publishing

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 receiving this event preview stream is created with Session.createStream() and Stream.play() is invoked to play it.

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)) {
                    
                    /**
                      * The options for the stream to play are set.
                      * The stream name is passed when StreamOptions object is created.
                      */
                    StreamOptions streamOptions = new StreamOptions(streamName);
                    
                    /**
                      * Stream is created with method Session.createStream().
                      */
                    playStream = session.createStream(streamOptions);
                   }
 ...                    
                    }
/**
                      * }
});Method Stream.

...

play() is called

...

Code Block
languagejs
themeRDark
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
languagejs
themeRDark
playStream = session.createStream(streamOptions);

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

Code Block
languagejs
themeRDark
playStream.on(new StreamStatusEvent() {
    .....
});

Method Stream.play() is called to play the stream. line 181

Code Block
languagejs
themeRDark
playStream = session.createStream(streamOptions);

5. Disconnection. line 234

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

Code Block
languagejs
themeRDark
session.disconnect(); to start playback of the stream.
                      */
                    playStream.play();
                } else {
                    Log.e(TAG, "Can not publish stream " + stream.getName() + " " + streamStatus);
                }
                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

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());
        }
    });
}