Versions Compared

Key

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

...

To analyze the code, let's take class StreamingMinActivity.java of  of the streaming-min example version with hash 4ed4c6d77, which can be downloaded with corresponding 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 Connect button is clicked. line 95

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

Session is created with method 2. Session creation.

Flashphoner.createSession(), to which object  code

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

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

Callback functions for session events are added (line 100)

  • onConnected() - will be called when connection is successfully established
  • onDisconnection() - will be called when connection is closed

...

languagejs
themeRDark

...

Code Block
languagejava
themeRDark
sessionOptions = new SessionOptions(mWcsUrlView.getText().toString());
sessionOptions.setLocalRenderer(localRender);
sessionOptions.setRemoteRenderer(remoteRender);

/**
  * Uncomment this code to use your own RTCConfiguration. For example, you can use custom TURN server
  */
//List<PeerConnection.IceServer> iceServers = new ArrayList<>();
//iceServers.add(new PeerConnection.IceServer("turn:your.turn-server.com:443?transport=tcp","username","passw0rd"));
//PeerConnection.RTCConfiguration customConfig = new PeerConnection.RTCConfiguration(iceServers);
//sessionOptions.setMediaOptions(customConfig);

/**
  * 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
languagejava
themeRDark
@Override
public void onConnected(final Connection connection) {
    runOnUiThread(new Runnable() {
        @Override
        public void onConnected(final Connection connection) {run() {
            mConnectButton.setText(R.string.action_disconnect);
            mConnectButton.setTag(R.string.action_disconnect);
            mConnectButton.setEnabled(true);
            mConnectStatus.setText(connection.getStatus());
            mPublishButton.setEnabled(true);
            mPlayButton.setEnabled(true);
        }
    });

5. Permissions request to publish video stream on "Publish" button pressing

ActivityCompat.requestPermissions() code

Code Block
languagejava
themeRDark
mPublishButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onDisconnection(final Connection connection onClick(View view) {
        if (mPublishButton.getTag() == null || Integer.valueOf(R.string.action_publish).equals(mPublishButton.getTag())) {
            ActivityCompat.requestPermissions(StreamingMinActivity.this,
                  new String[]{Manifest.permission.RECORD_AUDIO, Manifest.permission.CAMERA},
                  PUBLISH_REQUEST_CODE);
            ...
        } else {
            ...
        }
        ...
    }
});

...

6. Video streaming when permissions are granted.

Session.connectcreateStream() is called to establish connection with WCS server. line 152, Stream.publish() code

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

3. Video streaming.

New video stream is created with method Session.createStream() when Publish is clicked. line 196
Object StreamOptions (line 191) 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 201)

Code Block
languagejs
themeRDark
publishStream.on(new StreamStatusEventcase PUBLISH_REQUEST_CODE: {
    if (grantResults.length == 0 ||
           grantResults[0] != PackageManager.PERMISSION_GRANTED ||
           grantResults[1] != PackageManager.PERMISSION_GRANTED) {
        Log.i(TAG, "Permission has been denied by user");
    } else {
        mPublishButton.setEnabled(false);
        /**
          * The options for the stream to publish are set.
          * The stream name is passed when StreamOptions object is created.
          */
        StreamOptions streamOptions = new StreamOptions(mPublishStreamView.getText().toString());

        /**
          * Uncomment this code to use case WebRTC-as-RTMP. Stream will be republished to your rtmpUrl
          */
        //streamOptions.setRtmpUrl("rtmp://192.168.1.100:1935/live2");

        /**
          * Stream is created with method Session.createStream().
          */
        publishStream = session.createStream(streamOptions);
        ...
        /**
          * Method Stream.publish() is called to publish stream.
          */
        publishStream.publish();

        Log.i(TAG, "Permission has been granted by user");
    }
}

7. Stream playback on "Play" button pressing

Session.createStream(), Stream.play() code

Code Block
languagejava
themeRDark
mPlayButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) {onClick(View view) {
        mPlayButton.setEnabled(false);
        runOnUiThread(new Runnable() { if (mPlayButton.getTag() == null || Integer.valueOf(R.string.action_play).equals(mPlayButton.getTag())) {
            /**
            @Override 
  * The options for the stream to play are set.
              * The stream name is passed when StreamOptions object is created.
              */
            StreamOptions streamOptions public= void run() { new StreamOptions(mPlayStreamView.getText().toString());

            /**
              * Stream is created with ifmethod Session.createStream(StreamStatus.PUBLISHING.equals(streamStatus)) {).
              */
            playStream = session.createStream(streamOptions);
            ...
            /**
              * Method Stream.play() is called to start playback of the stream.
              */
            playStream.play();
            ....
        } else {
            ...
        }
        }...
    }
});

...

8. Stream playback stop on "Stop" button pressing

Stream.publishstop() is called to publish the stream. line 224 code

Code Block
languagejsjava
themeRDark
publishStreamplayStream.publishstop();

4. Stop of streaming. line 234

...


playStream = null;

9. Streaming stop on "Unpublish" button pressing

Stream.stop() is called to stop video streaming when Unpublish button is clicked. code

Code Block
languagejsjava
themeRDark
publishStream.stop();
publishStream = null;

5. Playback of video stream.

New video stream is created with method Session.createStream() when Play is clicked. line 267
Object StreamOptions (line 262) with name of the stream is passed to the method.10. Session disconnection.

Session.disconnect() code

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

...

mConnectButton.setEnabled(false);

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

11. Receiving the event confirming successful disconnection.

session.onDisconnection() code

Code Block
languagejsjava
themeRDark
playStream.on@Override
public void onDisconnection(final Connection connection) {
    runOnUiThread(new StreamStatusEventRunnable() {
        @Override
        public void onStreamStatus(final Stream stream, final StreamStatus streamStatus) { run() {
            mConnectButton.setText(R.string.action_connect);
            mConnectButton.setTag(R.string.action_connect);
            mConnectButton.setEnabled(true);
            mPublishButton.setText(R.string.action_publish);
            mPublishButton.setTag(R.string.action_publish);
           }
});

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

Code Block
languagejs
themeRDark
playStream.play();

6. Stop of video stream playback. line 306

Method Stream.stop() is called to stop playback of video stream when Stop button is clicked.

Code Block
languagejs
themeRDark
playStream.stop();

7. Disconnection. line 164

Method Session.disconnect() is called to close connection to the server when Disconnect button is clicked.

Code Block
languagejs
themeRDark
session.disconnect(); mPublishButton.setEnabled(false);
            mPlayButton.setText(R.string.action_play);
            mPlayButton.setTag(R.string.action_play);
            mPlayButton.setEnabled(false);
            mConnectStatus.setText(connection.getStatus());
            mPublishStatus.setText("");
            mPlayStatus.setText("");
        }
    });
}