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
Work with code of the example
1. Initialization of the API. line 76
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.
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 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
Method Session.connect() is called to establish connection with WCS server. line 253
4. Video streaming.
After establishing connection to the server, new video stream is created with method Session.createStream(). line 167
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
Callback function for processing stream statuses is added. (line 172)
Method Stream.publish() is called to publish the stream. line 224
5. Playback of video stream.
Callback function for processing stream statuses is added. (line 193)
Method Stream.play() is called to play the stream. line 211
6. Disconnection. line 265
Method Session.disconnect() is called to close connection to the server.