Versions Compared

Key

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

...

  • 192.168.2.104 in the URL is the address of the WCS server
  • stream name is entered in to the 'Play Stream' field (RTSP URL in this case)

Work with code of the example

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

1. Initialization of the API. line 61

...

languagejs
themeRDark

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 89

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

Session is created with method 2. Session creation.

Flashphoner.createSession(), to which object SessionOptions (line 83)  code

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

  • URL of WCS server
  • SurfaceViewRenderer, which will be used to play video stream

Callback functions for session events are added (line 94)

...

Code Block
languagejava
themeRDark
SessionOptions sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
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
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 176

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

...

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

5. Playback of video stream.

After establishing connection to the server, new video stream is created with method Session.createStream(). line 119, Stream.play() code
Object StreamOptions (line 114) with name of the stream is passed to the createStream() method.

Code Block
languagejsjava
themeRDark
StreamOptions streamOptions = new StreamOptions(mPlayStreamView.getText().toString());
            
/**
  * Stream is created with method Session.createStream().
  */
playStream = session.createStream(streamOptions);

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

Code Block
languagejs
themeRDark

            
/**
  * Callback function for stream status change is added to display the status.
  */
playStream.on(new StreamStatusEvent() {
    @Override
    public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {
        runOnUiThread(new Runnable() { 
            @Override 
            public void run() { 
                if (!StreamStatus.PLAYING.equals(streamStatus)) {
                    Log.e(TAG, "Can not play stream " + stream.getName() + " " + streamStatus);
                } else if (StreamStatus.NOT_ENOUGH_BANDWIDTH.equals(streamStatus)) {
                    Log.w(TAG, "Not enough bandwidth stream " + stream.getName() + ", consider using lower video resolution or bitrate. " +
                          "Bandwidth " + (Math.round(stream.getNetworkBandwidth() / 1000)) + " " +
                          "bitrate " + (Math.round(stream.getRemoteBitrate() / 1000)));
                } else {
                    mStatusView.setText(streamStatus.toString());
                }
            }
        });

...


    }
});
            
/**
  * Method Stream.play() is called

...

 to start playback of the stream.
  */
playStream.play();

6. Sessino disconnection.

Session.disconnect() code

Code Block
languagejsjava
themeRDark
playStreamsession.playdisconnect();

5. Disconnection. line 188

Method Session.disconnect() is called to close connection to the server.7. Receiving the event confirming successful disconnection

session.onDisconnection() code

Code Block
languagejs
themeRDark
session.disconnect();@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());
        }
    });
}