Example of Android application managing media devices

This example can be used as streamer allowing to select source camera and microphone and specify parameters for the published video: FPS (Frames Per Second) and resolution (width, height).

On the screenshot below the example is displayed when a stream is being published.
In the URL specified in the 'WCS URL' input field

Below that input field are located drop-down lists of available microphones and cameras and input fields for FPS, width and height.
Two videos are played

Work with code of the example

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

1. Initialization of the API. line 76

Flashphoner.init(this);


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

2. List available media devices.

After initialization of the API, method getMediaDevices(), which returns MediaDeviceList object, is used to request list of all available media devices.
Then methods MediaDeviceList.getAudioList() and MediaDeviceList.getVideoList() are used to list available microphones and cameras.

line 88

Flashphoner.getMediaDevices().getAudioList()


line 91

Flashphoner.getMediaDevices().getVideoList()


The received lists of media devices are used to fill the corresponding drop-down lists.

3. Connection to server.

Session for connection to server is created when Start button is clicked. line 129

session = Flashphoner.createSession(sessionOptions);


Session is created with method createSession(), to which object SessionOptions (line 122) with the following parameters is passed

Callback functions for session events are added (line 134)

session.on(new SessionEvent() {
    public void onConnected(final Connection connection) {
        .....
    }
    public void onDisconnection(final Connection connection) {
        .....
    }
});


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

session.connect(new Connection());


4. Video streaming.

After establishing connection to the server, new video stream is created with method Session.createStream(). line 167

publishStream = session.createStream(streamOptions);


Object StreamOptions (line 156) with name of the stream and VideoConstraints object is passed to the method.

Parameters of VideoConstraints object

To add —Āonstraints, method StreamOptions.setConstraints() is used. line 162

streamOptions.setConstraints(new Constraints(new AudioConstraints(), videoConstraints));


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

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


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

publishStream.publish();


5. Playback of video stream.

When the stream is published, new stream is created to play the published stream. line 188
Object StreamOptions (line 183) with name of the published stream is passed when the stream is created.

playStream = session.createStream(streamOptions);


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

playStream.on(new StreamStatusEvent() {
    .....
});


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

playStream.play();


6. Disconnection. line 265

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

session.disconnect();