Versions Compared

Key

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

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

  • 192.168.2.104 is the address of the WCS server
  • testStream is the stream name

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

  • left - video from the camera
  • right - the published video stream as received from the server

Image Added

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

Code Block
languagejs
themeRDark
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

Code Block
languagejs
themeRDark
Flashphoner.getMediaDevices().getAudioList()


line 91

Code Block
languagejs
themeRDark
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

Code Block
languagejs
themeRDark
session = Flashphoner.createSession(sessionOptions);


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

  • 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 134)

  • onConnected() - will be called when connection is successfully established
  • onDisconnection() - will be called when connection is closed
Code Block
languagejs
themeRDark
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

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


4. Video streaming.

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

Code Block
languagejs
themeRDark
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

  • selected camera - set with method VideoConstraints.setCameraId()
  • FPS - set with method VideoConstraints.setVideoFps()
  • resolution (width, height) - set with method VideoConstraints.setResolution()

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

Code Block
languagejs
themeRDark
streamOptions.setConstraints(new Constraints(new AudioConstraints(), videoConstraints));


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

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


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

Code Block
languagejs
themeRDark
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.

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


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

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


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

Code Block
languagejs
themeRDark
playStream.play();


6. Disconnection. line 265

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

Code Block
languagejs
themeRDark
session.disconnect();