Skip to main content

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

FieldtypeDescription
audio (Optional)camera or screen" or IAudioTrackConstraintsParameters for audio
video (Optional)mic or IVideoTrackConstraintsParameters for video

IAudioTrackConstraints

FieldtypeDescription
sourcemicThis value will be used in server side
deviceId (Optional)stringSpecifying a device ID, can be found by calling enumerateDevices
echoCancellation (Optional)booleanMediaTrackConstraints
noiseSuppression (Optional)booleanMediaTrackConstraints

IVideoTrackConstraints

FieldtypeDescription
sourcecamera or screenThis value will be used in server side
deviceId (Optional)stringpecifying a device ID, can be found by calling enumerateDevices
resolution (Optional)IResolutionSpecifiying the resoultion of the stream
frameRate (Optional)

IResolution

FieldtypeDescription
widthnumberwidth of the stream, in pixels
heightnumberheight 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()