Versions Compared

Key

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

Example of video conference client for Android

This example can be used to participate in video conference for three participants on Web Call Server and allows to publish WebRTC stream.

On the screenshot below the participant is connected, publishing a stream and playing streams from the other two participants.

Input fields required for connecting to WCS server and joining conference

  • 'WCS URL', where 192.168.2.104 is the address of the WCS server
  • 'Login', where Alice is the username
  • 'Room', where room1 is the name of conference room

Three videos are played

  • video from the camera of this participant - the lower one
  • videos from the other participants (Bob and Cindy)

Volume control is located between the video elements.
Below are located controls for muting/unmuting audio and video for the published stream, input field for a text message and messages log.

Image Added

Work with code of the example

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

Unlike direct connection to server with method createSession(), , RoomManager object is used for managing connection to server and conference. Connection to server is established when RoomManager object is created, and method RoomManager.join() is called for joining a conference.
When joining, Room object is created for work with the "room". Participant objects are used for work with conference participants.
All events occurring in the "room" (a user joined/left, or sent a message), are sent to all users connected to the "room".

For example, in the following code, a user joins to a "room" and gets the list of already connected users.

Code Block
languagejs
themeRDark
room = roomManager.join(roomOptions);
room.on(new RoomEvent() {
    public void onState(final Room room) {
        for (final Participant participant : room.getParticipants()) {
    ...


ParticipantView (SurfaceViewRenderer + TextView - line 613) is assigned for each of the other participants, to display the name and video stream of that participant (Bob and Cindy on the screenshot above).

1. Initialization of the API. line 98

Code Block
languagejs
themeRDark
Flashphoner.init(this);


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

2. Connection to server.

RoomManager object and session for connection to server are created when Connect button is clicked. line 145

Code Block
languagejs
themeRDark
roomManager = Flashphoner.createRoomManager(roomManagerOptions);


RoomManager object is created with method createRoomManager(), to which RoomManagerOptions object (line 139) with the following parameters is passed

  • URL of WCS server
  • username

Callback functions, which make appropriate changes in controls of the interface, are added for session events (line 150)

  • onConnected() - will be called when connection is successfully established
  • onDisconnection() - will be called when connection is closed
Code Block
languagejs
themeRDark
roomManager.on(new RoomManagerEvent() {
    public void onConnected(final Connection connection) {
        .....
    }
    public void onDisconnection(final Connection connection) {
        .....
    }
});


3. Joining a conference. line 145

After establishing connection to the server, method connection.join() is used to join the conference.
To join, name of the conference room is passed to the method. (The name can be specified as parameter in the URL of the client page; otherwise, random name will be generated.)

The user joins a conference when Join button is clicked.
To join, method RoomManager.join() is called. RoomOptions object (line 240) with the name of the conference room is passed to the method.

Code Block
languagejs
themeRDark
room = roomManager.join(roomOptions);


Callback functions for conference room events are added (line 254)

  • onState() - will be called when successfully joined the conference
  • onJoined() - will be called when a new participants joins the conference: the interface is changed to display the new participant and playback of video stream published by that participant is started
  • onLeft() - will be called when one of the participants leaves the conference: appropriate changes in the interface are done
  • onPublished() - will be called when one of the participants starts publishing: playback of video stream published by that participant is started
  • onFailed() - will be called in case of failure: the user leaves the conference
  • onMessage() - will be called when one of the participants sends text message: the message is added to the messages log

In callback function onState(),

  • the size of the collection of Participant objects returned by method Room.getParticipants() is determined to get the number of already connected participants
  • if the maximum allowed number of participants had already been reached, the user leaves the "room" (line 264)
  • otherwise, appropriate changes in the interface are done and playback of video stream published by the other participants is started
Code Block
languagejs
themeRDark
room.on(new RoomEvent() {
    public void onState(final Room room) {
        .....
    }
    public void onJoined(final Participant participant) {
        .....
    }
    public void onLeft(final Participant participant) {
        .....
    }
    public void onPublished(final Participant participant) {
        .....
    }
    public void onFailed(Room room, final String info) {
        .....
    }
    public void onMessage(final Message message) {
        .....
    }
});


4. Video streaming. line 479

Video stream publication is started when Publish button is clicked.

Code Block
languagejs
themeRDark
stream = room.publish(localRenderer);


Method Room.publish() is called to publish a stream. SurfaceViewRenderer to be used to display video from the camera is passed to the method.

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

Code Block
languagejs
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)) {
                    .....
                }
        }
    }
});


5. Playback of video stream. line 376

When one of the conference participants starts publishing, method Participant.play() is used to start playback of that stream.
SurfaceViewRenderer, in which the video will be displayed, is passed to the method.

Code Block
languagejs
themeRDark
participant.play(participantView.surfaceViewRenderer);


6. Stop of streaming. line 518

The following method is called to stop video streaming when Unpublish button is clicked.

Code Block
languagejs
themeRDark
room.unpublish();


7. Leaving conference room. line 416

Method room.leave() is called for leaving conference room when Leave button is clicked.
Handler for processing response from server-side REST application is passed to the method.

Code Block
languagejs
themeRDark
room.leave(new RestAppCommunicator.Handler() {
    public void onAccepted(Data data) {
        ......
    }
    public void onRejected(Data data) {
        ......
    }
});


8. Disconnection. line 213

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

Code Block
languagejs
themeRDark
roomManager.disconnect();


9. Mute/unmute audio and video of the published stream. line 533, line 548

The following methods are used to mute/unmute audio and video

  • Stream.unmuteAudio();
  • Stream.muteAudio();
  • Stream.unmuteVideo();
  • Stream.muteVideo();

10. Sending text message. line 599

Method Participant.sendMessage() is used for sending text message to other participants. Message text is passed to the method.

When Send button is clicked,

  • method Room.getParticipants() is used to get the collection of connected participants
  • the message is sent to each participant
Code Block
languagejs
themeRDark
for (Participant participant : room.getParticipants()) {
    participant.sendMessage(text);
}


11. Adjusting the volume line 100

The following methods are used to adjust the volume

  • Flashphoner.getMaxVolume() - returns the maximum available volume level
  • Flashphoner.getVolume() - returns the current volume level
  • Flashphoner.setVolume() - sets the volume level; int value is passed to the method