new Twilio.Video.LocalVideoTrack(mediaStreamTrack [, options])
Construct a LocalVideoTrack from a MediaStreamTrack.
Parameters:
| Name | Type | Argument | Description |
|---|---|---|---|
mediaStreamTrack |
MediaStreamTrack | The underlying MediaStreamTrack |
|
options |
LocalTrackOptions |
<optional> |
LocalTrack options |
Properties:
| Name | Type | Description |
|---|---|---|
id |
Track.ID | The LocalVideoTrack's ID |
isMuted |
boolean | Whether or not the video source has stopped sending frames to the
LocalVideoTrack; This can happen when the camera is taken over by another application,
mainly on mobile devices; When this property toggles, then |
isStopped |
boolean | Whether or not the LocalVideoTrack is stopped |
Fires:
Extends
Methods
-
addProcessor(processor [, options])
-
Add a VideoProcessor to allow for custom processing of video frames belonging to a VideoTrack.
Parameters:
Name Type Argument Description processorVideoProcessor The VideoProcessor to use.
optionsAddProcessorOptions <optional>
AddProcessorOptions to provide.
- Overrides:
Returns:
- Type
- this
Example
class GrayScaleProcessor { constructor(percentage) { this.percentage = percentage; } processFrame(inputFrameBuffer, outputFrameBuffer) { const context = outputFrameBuffer.getContext('2d'); context.filter = `grayscale(${this.percentage}%)`; context.drawImage(inputFrameBuffer, 0, 0, inputFrameBuffer.width, inputFrameBuffer.height); } } const localVideoTrack = Array.from(room.localParticipant.videoTracks.values())[0].track; localVideoTrack.addProcessor(new GrayScaleProcessor(100)); -
attach()
-
Create an HTMLVideoElement and attach the VideoTrack to it.
The HTMLVideoElement's
srcObjectwill be set to a new MediaStream containing the VideoTrack's MediaStreamTrack.- Inherited From:
- Overrides:
Returns:
videoElement
- Type
- HTMLVideoElement
Example
const Video = require('twilio-video'); Video.createLocalVideoTrack().then(function(videoTrack) { const videoElement = videoTrack.attach(); document.body.appendChild(videoElement); }); -
detach()
-
Detach the VideoTrack from all previously attached HTMLMediaElements.
- Inherited From:
- Overrides:
Returns:
mediaElements
- Type
- Array.<HTMLMediaElement>
Example
const mediaElements = videoTrack.detach(); mediaElements.forEach(mediaElement => mediaElement.remove());
-
disable()
-
Disable the LocalVideoTrack. This is equivalent to pausing a video source. If a VideoProcessor is added, then
processedTrackis also disabled.Fires:
Returns:
- Type
- this
-
enable()
-
Enable the LocalVideoTrack. This is equivalent to unpausing the video source. If a VideoProcessor is added, then
processedTrackis also enabled.Fires:
Returns:
- Type
- this
-
enable( [enabled])
-
Enable or disable the LocalVideoTrack. This is equivalent to unpausing or pausing the video source respectively. If a VideoProcessor is added, then
processedTrackis also enabled or disabled.Parameters:
Name Type Argument Default Description enabledboolean <optional>
true Specify false to disable the LocalVideoTrack
Fires:
Returns:
- Type
- this
-
removeProcessor(processor)
-
Remove the previously added VideoProcessor using
addProcessorAPI.Parameters:
Name Type Description processorVideoProcessor The VideoProcessor to remove.
- Overrides:
Returns:
- Type
- this
Example
class GrayScaleProcessor { constructor(percentage) { this.percentage = percentage; } processFrame(inputFrameBuffer, outputFrameBuffer) { const context = outputFrameBuffer.getContext('2d'); context.filter = `grayscale(${this.percentage}%)`; context.drawImage(inputFrameBuffer, 0, 0, inputFrameBuffer.width, inputFrameBuffer.height); } } const localVideoTrack = Array.from(room.localParticipant.videoTracks.values())[0].track; const grayScaleProcessor = new GrayScaleProcessor(100); localVideoTrack.addProcessor(grayScaleProcessor); document.getElementById('remove-button').onclick = () => localVideoTrack.removeProcessor(grayScaleProcessor); -
restart( [constraints])
-
Restart the LocalVideoTrack. This stops the existing MediaStreamTrack and creates a new MediaStreamTrack. If the LocalVideoTrack is being published to a Room, then all the RemoteParticipants will start receiving media from the newly created MediaStreamTrack. You can access the new MediaStreamTrack via the
mediaStreamTrackproperty. If you want to listen to events on the MediaStreamTrack directly, please do so in the "started" event handler. Also, the LocalVideoTrack's ID is no longer guaranteed to be the same as the underlying MediaStreamTrack's ID.Parameters:
Name Type Argument Description constraintsMediaTrackConstraints <optional>
The optional MediaTrackConstraints for restarting the LocalVideoTrack; If not specified, then the current MediaTrackConstraints will be used; If
{}(empty object) is specified, then the default MediaTrackConstraints will be usedFires:
Returns:
Rejects with a TypeError if the LocalVideoTrack was not created using an one of
createLocalVideoTrack,createLocalTracksorconnect; Also rejects with the DOMException raised bygetUserMediawhen it fails- Type
- Promise.<void>
Example
const { connect, createLocalVideoTrack } = require('twilio-video'); // Create a LocalVideoTrack that captures video from the front-facing camera. createLocalVideoTrack({ facingMode: 'user' }).then(function(localVideoTrack) { return connect('token', { name: 'my-cool-room', tracks: [localVideoTrack] }); }).then(function(room) { // Restart the LocalVideoTrack to capture video from the back-facing camera. const localVideoTrack = Array.from(room.localParticipant.videoTracks.values())[0].track; return localVideoTrack.restart({ facingMode: 'environment' }); }); -
stop()
-
Calls stop on the underlying MediaStreamTrack. If you choose to stop a LocalVideoTrack, you should unpublish it after stopping.
Fires:
Returns:
- Type
- this
Events
-
dimensionsChanged
-
The VideoTrack's dimensions changed.
Parameters:
Name Type Description trackVideoTrack The VideoTrack whose dimensions changed
- Inherited From:
- Overrides:
-
disabled
-
The LocalVideoTrack was disabled, i.e. the video source was paused by the user.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that was disabled
- Overrides:
-
enabled
-
The LocalVideoTrack was enabled, i.e. the video source was unpaused by the user.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that was enabled
- Overrides:
-
muted
-
The LocalVideoTrack was muted because the video source stopped sending frames, most likely due to another application taking said video source, especially on mobile devices.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that was muted
-
started
-
The LocalVideoTrack started. This means there is enough video data to begin playback.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that started
- Overrides:
-
stopped
-
The LocalVideoTrack stopped, either because LocalVideoTrack#stop or LocalVideoTrack#restart was called or because the underlying MediaStreamTrack ended.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that stopped
-
unmuted
-
The LocalVideoTrack was unmuted because the video source resumed sending frames, most likely due to the application that took over the said video source has released it back to the application, especially on mobile devices. This event is also fired when LocalVideoTrack#restart is called on a muted LocalVideoTrack with a new video source.
Parameters:
Name Type Description trackLocalVideoTrack The LocalVideoTrack that was unmuted