Accessing medias
Twyng has a built-in function createMediaStream to access media such as audio, video, and screen. This method is an asynchronous operation. The result of this function should be used for publishing. createMediaStream requires a config parameter. The config parameter and is properties are given below
| Field | type | Description |
|---|---|---|
| audio (Optional) | camera or screen" or IAudioTrackConstraints | Parameters for audio |
| video (Optional) | mic or IVideoTrackConstraints | Parameters for video |
IAudioTrackConstraints
| Field | type | Description |
|---|---|---|
| source | mic | This value will be used in server side |
| deviceId (Optional) | string | Specifying a device ID, can be found by calling enumerateDevices |
| echoCancellation (Optional) | boolean | MediaTrackConstraints |
| noiseSuppression (Optional) | boolean | MediaTrackConstraints |
IVideoTrackConstraints
| Field | type | Description |
|---|---|---|
| source | camera or screen | This value will be used in server side |
| deviceId (Optional) | string | pecifying a device ID, can be found by calling enumerateDevices |
| resolution (Optional) | IResolution | Specifiying the resoultion of the stream |
| frameRate (Optional) |
IResolution
| Field | type | Description |
|---|---|---|
| width | number | width of the stream, in pixels |
| height | number | height of the stream, in pixels |
You can obtain a media stream from the browser by using different combinations of the configuration object. Please refer to the provided samples.
How to get video stream
To access the camera on your system, use the following code:
const localstream = await twyng.createMediastream({
video: {
source: "camera",
deviceId: "default",
resolution: {
width: 640,
height: 640,
},
frameRate: 16,
}
});
To access the camera with mic on your system, use the following code:
const localstream = await twyng.createMediastream({
video: 'camera',
audio: 'mic'
});
To access the screen on your system, use the following code:
const localstream = await twyng.createMediastream({
video: 'screen'
});
Sample Response of the createMediaStream api
{
mediaStream: MediaStream,
source: { //MediaStreamSourceInfo
audioSourceInfo: {
name: "mic"
},
videoSourceInfo: {
name: "camera" || "screen",
framerate: "number",
resolution: {
width: "number",
height: "number"
},
deviceId: "string"
},
videocodec: "vp8" || "vp9" || "h264",
audiocodec: "opus",
bitrate: "number"
}
}
Playback the stream
// Accessing camera by using createMediastream
const localstream = await twyng.createMediastream({
video: 'camera'
});
// Create a new video element
const video = document.createElement('video')
video.srcObject = localstream.mediaStream
document.body.appendChild(video)
video.play()