### Install Project Dependencies Source: https://github.com/twilio/twilio-video.js/blob/master/test/framework/twilio-video-angular/README.md Run this command to install the necessary packages for the project. ```bash npm install ``` -------------------------------- ### Start Application Server Source: https://github.com/twilio/twilio-video.js/blob/master/test/framework/twilio-video-angular/README.md Execute this command to launch the application server. The application requires an Access Token provided via a `token` query parameter. ```bash npm start ``` -------------------------------- ### Install twilio-video.js via NPM Source: https://github.com/twilio/twilio-video.js/blob/master/README.md Use this command to install the twilio-video.js library using NPM. This is the recommended method for integrating the SDK into your project. ```bash npm install twilio-video --save ``` -------------------------------- ### Setup Pre-commit Hook Source: https://github.com/twilio/twilio-video.js/blob/master/README.md Link the provided pre-commit script to enable pre-commit hooks for identifying common mistakes before committing. ```bash ln -s ../../pre-commit.sh .git/hooks/pre-commit ``` -------------------------------- ### Connect to a Twilio Video Room Source: https://github.com/twilio/twilio-video.js/blob/master/README.md Example of connecting to a Twilio Video room using a token and handling participant events. Requires the 'twilio-video' module. ```javascript const Video = require('twilio-video'); Video.connect('$TOKEN', { name: 'room-name' }).then(room => { console.log('Connected to Room "%s"', room.name); room.participants.forEach(participantConnected); room.on('participantConnected', participantConnected); room.on('participantDisconnected', participantDisconnected); room.once('disconnected', error => room.participants.forEach(participantDisconnected)); }); function participantConnected(participant) { console.log('Participant "%s" connected', participant.identity); const div = document.createElement('div'); div.id = participant.sid; div.innerText = participant.identity; participant.on('trackSubscribed', track => trackSubscribed(div, track)); participant.on('trackUnsubscribed', trackUnsubscribed); participant.tracks.forEach(publication => { if (publication.isSubscribed) { trackSubscribed(div, publication.track); } }); document.body.appendChild(div); } function participantDisconnected(participant) { console.log('Participant "%s" disconnected', participant.identity); document.getElementById(participant.sid).remove(); } function trackSubscribed(div, track) { div.appendChild(track.attach()); } function trackUnsubscribed(track) { track.detach().forEach(element => element.remove()); } ``` -------------------------------- ### Create and Use Local Data Track Source: https://context7.com/twilio/twilio-video.js/llms.txt Creates a LocalDataTrack for sending arbitrary data. Supports configurable reliability and ordering. Includes an example of sending mouse position updates and receiving data on the remote side. ```javascript const Video = require('twilio-video'); // Reliable, ordered data track (default) const mouseTrack = new Video.LocalDataTrack({ name: 'mouse-position' }); // Unreliable, unordered for low-latency gaming-style updates const gameStateTrack = new Video.LocalDataTrack({ maxRetransmits: 0, ordered: false }); console.log('Reliable:', mouseTrack.reliable); // true console.log('Ordered:', gameStateTrack.ordered); // false // Connect with the data track Video.connect('YOUR_ACCESS_TOKEN', { name: 'my-room', tracks: [mouseTrack] }).then(room => { // Send mouse position updates to all remote participants window.addEventListener('mousemove', event => { mouseTrack.send(JSON.stringify({ x: event.clientX, y: event.clientY })); }); // Send binary data mouseTrack.send(new Uint8Array([1, 2, 3, 4]).buffer); // Remote side: receive the data room.on('trackSubscribed', track => { if (track.kind === 'data') { track.on('message', data => { const position = JSON.parse(data); console.log('Remote cursor:', position.x, position.y); }); } }); }); ``` -------------------------------- ### Video.runPreflight(token, options) Source: https://context7.com/twilio/twilio-video.js/llms.txt Initiates a network preflight test to assess network connectivity, measure RTT, jitter, packet loss, and MOS score, and verify TURN server reachability. It returns a PreflightTest instance with events for progress, completion, and failure. ```APIDOC ## `Video.runPreflight(token, options)` — Run a Network Preflight Test Runs a pre-call diagnostic test that checks network connectivity, measures RTT, jitter, packet loss, and MOS score, and verifies TURN server reachability. Returns a `PreflightTest` instance that emits `progress`, `completed`, and `failed` events. ```js const { runPreflight } = require('twilio-video'); const preflightTest = runPreflight('YOUR_ACCESS_TOKEN', { duration: 15000, // run for 15 seconds (default: 10000) region: 'us1' // target specific signaling region }); preflightTest.on('progress', progressName => { // Possible values: mediaAcquired, connected, mediaSubscribed, // mediaStarted, dtlsConnected, peerConnectionConnected, iceConnected console.log('Preflight progress:', progressName); }); preflightTest.on('completed', report => { console.log('Test duration (ms):', report.testTiming.duration); console.log('ICE connect time (ms):', report.networkTiming.ice?.duration); console.log('DTLS connect time (ms):', report.networkTiming.dtls?.duration); console.log('Media start time (ms):', report.networkTiming.media?.duration); const { jitter, rtt, packetLoss } = report.stats; console.log(`RTT avg/min/max: ${rtt.average}/${rtt.min}/${rtt.max} ms`); console.log(`Jitter avg: ${jitter.average} s`); console.log(`Packet loss avg: ${packetLoss.average}%`); const { localCandidate, remoteCandidate } = report.selectedIceCandidatePairStats ?? {}; console.log('ICE type:', localCandidate?.candidateType, '->', remoteCandidate?.candidateType); // Show result to user if (packetLoss.average < 1 && rtt.average < 150) { showUI('Network looks great!'); } else { showUI('Network may cause quality issues'); } }); preflightTest.on('failed', (error, partialReport) => { console.error('Preflight failed:', error.message); console.log('Progress reached:', partialReport?.progressEvents?.map(e => e.name)); }); // Optionally stop early // preflightTest.stop(); ``` ``` -------------------------------- ### Build Twilio Video.js Source: https://github.com/twilio/twilio-video.js/blob/master/README.md Run the build script to compile the project. Builds will be placed in the dist/ directory. ```bash npm run build ``` -------------------------------- ### Run Network Preflight Test with Video.runPreflight() Source: https://context7.com/twilio/twilio-video.js/llms.txt Execute Video.runPreflight(token, options) to perform a network diagnostic test, checking connectivity, RTT, jitter, packet loss, and TURN server reachability. The function returns a PreflightTest instance that emits 'progress', 'completed', and 'failed' events. ```javascript const { runPreflight } = require('twilio-video'); const preflightTest = runPreflight('YOUR_ACCESS_TOKEN', { duration: 15000, // run for 15 seconds (default: 10000) region: 'us1' // target specific signaling region }); preflightTest.on('progress', progressName => { // Possible values: mediaAcquired, connected, mediaSubscribed, // mediaStarted, dtlsConnected, peerConnectionConnected, iceConnected console.log('Preflight progress:', progressName); }); preflightTest.on('completed', report => { console.log('Test duration (ms):', report.testTiming.duration); console.log('ICE connect time (ms):', report.networkTiming.ice?.duration); console.log('DTLS connect time (ms):', report.networkTiming.dtls?.duration); console.log('Media start time (ms):', report.networkTiming.media?.duration); const { jitter, rtt, packetLoss } = report.stats; console.log(`RTT avg/min/max: ${rtt.average}/${rtt.min}/${rtt.max} ms`); console.log(`Jitter avg: ${jitter.average} s`); console.log(`Packet loss avg: ${packetLoss.average}%`); const { localCandidate, remoteCandidate } = report.selectedIceCandidatePairStats ?? {}; console.log('ICE type:', localCandidate?.candidateType, '->', remoteCandidate?.candidateType); // Show result to user if (packetLoss.average < 1 && rtt.average < 150) { showUI('Network looks great!'); } else { showUI('Network may cause quality issues'); } }); preflightTest.on('failed', (error, partialReport) => { console.error('Preflight failed:', error.message); console.log('Progress reached:', partialReport?.progressEvents?.map(e => e.name)); }); // Optionally stop early // preflightTest.stop(); ``` -------------------------------- ### Get WebRTC Media Statistics Source: https://context7.com/twilio/twilio-video.js/llms.txt Retrieve detailed per-track WebRTC statistics for the current Room session, including bitrate, packet loss, and jitter. This method returns a Promise that resolves with an array of `StatsReport` objects. Note: Not supported on Safari 12.0 and below. ```javascript Video.connect('YOUR_ACCESS_TOKEN', { name: 'my-room' }).then(room => { // Poll stats every 5 seconds const statsInterval = setInterval(async () => { try { const statsReports = await room.getStats(); statsReports.forEach(report => { report.localAudioTrackStats.forEach(stat => { console.log(`Local audio bitrate: ${stat.bytesSent * 8 / 1000} kbps`); console.log(`Packets lost: ${stat.packetsLost}`); }); report.localVideoTrackStats.forEach(stat => { console.log(`Local video bitrate: ${stat.bytesSent * 8 / 1000} kbps`); console.log(`Frame rate: ${stat.frameRate}`); console.log(`Dimensions: ${stat.dimensions?.width}x${stat.dimensions?.height}`); }); report.remoteAudioTrackStats.forEach(stat => { console.log(`Remote audio jitter: ${stat.jitter} s`); }); }); } catch (err) { console.error('Stats error:', err); } }, 5000); room.once('disconnected', () => clearInterval(statsInterval)); }); ``` -------------------------------- ### Run Unit Tests Source: https://github.com/twilio/twilio-video.js/blob/master/README.md Execute the unit tests for the Twilio Video.js project. ```bash npm run test:unit ``` -------------------------------- ### Video.createLocalAudioTrack(options) Source: https://context7.com/twilio/twilio-video.js/llms.txt Acquires a single LocalAudioTrack from the microphone. Supports optional Krisp noise cancellation and automatic default device tracking. ```APIDOC ## `Video.createLocalAudioTrack(options)` — Acquire a Single Audio Track Acquires a single `LocalAudioTrack` from the microphone. Supports optional Krisp noise cancellation integration and automatic default device tracking (restarts the track when the system default audio input changes). ### Parameters * **options** (object) - Optional configuration for the audio track. * **name** (string) - The name of the audio track. * **noiseCancellationOptions** (object) - Options for Krisp noise cancellation. * **vendor** (string) - The vendor for noise cancellation (e.g., 'krisp'). * **sdkAssetsPath** (string) - The path to the Krisp SDK assets. * **defaultDeviceCaptureMode** (string) - Specifies how to handle changes in the default audio input device ('auto' to restart the track). ### Returns A `Promise` that resolves with a `LocalAudioTrack` object. ### Example ```javascript const Video = require('twilio-video'); Video.createLocalAudioTrack({ name: 'microphone', noiseCancellationOptions: { vendor: 'krisp', sdkAssetsPath: '/assets/twilio-krisp-audio-plugin/1.0.0/dist' }, defaultDeviceCaptureMode: 'auto' }).then(localAudioTrack => { console.log('Audio track name:', localAudioTrack.name); // Publish into an already-connected room // return room.localParticipant.publishTrack(localAudioTrack); }).catch(err => console.error('Audio track error:', err)); ``` ``` -------------------------------- ### Create SDKDriver Instance Source: https://github.com/twilio/twilio-video.js/blob/master/test/lib/sdkdriver/README.md Instantiate an SDKDriver by providing a transport and a test driver implementation. ```javascript const driver = await SDKDriver.create(transport, testDriver); ``` -------------------------------- ### Video.createLocalTracks(options) Source: https://context7.com/twilio/twilio-video.js/llms.txt Acquires LocalAudioTrack and LocalVideoTrack instances from the device's camera and microphone before connecting to a Room. This is useful for showing a camera preview or reusing tracks across multiple connections. ```APIDOC ## Video.createLocalTracks(options) — Acquire Local Audio and Video Acquires `LocalAudioTrack` and `LocalVideoTrack` instances from the device's camera and microphone before connecting to a Room. Useful for showing a camera preview to the user prior to joining, or for reusing the same tracks across multiple Room connections. ### Method POST ### Endpoint /createLocalTracks ### Parameters #### Path Parameters None #### Query Parameters - **options** (object) - Optional - Configuration options for acquiring tracks. - **audio** (boolean | object) - Whether to acquire an audio track. If an object, specifies track options. - **name** (string) - The name of the audio track. - **echoCancellation** (boolean) - Enables echo cancellation. - **noiseSuppression** (boolean) - Enables noise suppression. - **video** (boolean | object) - Whether to acquire a video track. If an object, specifies track options. - **name** (string) - The name of the video track. - **width** (number) - The desired width of the video track. - **height** (number) - The desired height of the video track. - **frameRate** (number) - The desired frame rate of the video track. ### Request Example ```javascript const Video = require('twilio-video'); Video.createLocalTracks({ audio: { name: 'microphone', echoCancellation: true, noiseSuppression: true }, video: { name: 'camera', width: 1280, height: 720, frameRate: 30 } }).then(localTracks => { const videoTrack = localTracks.find(t => t.kind === 'video'); const audioTrack = localTracks.find(t => t.kind === 'audio'); // Show local preview document.getElementById('local-preview').appendChild(videoTrack.attach()); // Mute/unmute microphone audioTrack.disable(); // mute audioTrack.enable(); // unmute // Later, join the Room with pre-acquired tracks return Video.connect('YOUR_ACCESS_TOKEN', { tracks: localTracks }); }).then(room => { console.log('Joined room with pre-acquired tracks'); }).catch(error => { if (error.name === 'NotAllowedError') { console.error('Camera/microphone permission denied'); } else { console.error('Failed to get tracks:', error); } }); ``` ### Response #### Success Response (200) - **localTracks** (array) - An array of acquired local tracks (`LocalAudioTrack` and `LocalVideoTrack`). #### Response Example ```json [ { "kind": "audio", "name": "microphone", "enabled": true }, { "kind": "video", "name": "camera", "enabled": true } ] ``` ``` -------------------------------- ### Acquire Local Audio and Video Tracks Source: https://context7.com/twilio/twilio-video.js/llms.txt Acquires local audio and video tracks before connecting to a Room. Useful for camera previews or reusing tracks across multiple connections. Handles permissions and provides options for track configuration. ```javascript const Video = require('twilio-video'); // Pre-acquire tracks for a camera preview Video.createLocalTracks({ audio: { name: 'microphone', echoCancellation: true, noiseSuppression: true }, video: { name: 'camera', width: 1280, height: 720, frameRate: 30 } }).then(localTracks => { const videoTrack = localTracks.find(t => t.kind === 'video'); const audioTrack = localTracks.find(t => t.kind === 'audio'); // Show local preview const previewContainer = document.getElementById('local-preview'); previewContainer.appendChild(videoTrack.attach()); // Mute/unmute microphone audioTrack.disable(); // mute audioTrack.enable(); // unmute // Later, join the Room with pre-acquired tracks (avoids double getUserMedia call) return Video.connect('YOUR_ACCESS_TOKEN', { name: 'my-room', tracks: localTracks }); }).then(room => { console.log('Joined room with pre-acquired tracks'); }).catch(error => { if (error.name === 'NotAllowedError') { console.error('Camera/microphone permission denied'); } else { console.error('Failed to get tracks:', error); } }); ``` -------------------------------- ### new Video.LocalDataTrack(options) Source: https://context7.com/twilio/twilio-video.js/llms.txt Creates a LocalDataTrack for sending arbitrary data to subscribed remote participants over WebRTC data channels. ```APIDOC ## `new Video.LocalDataTrack(options)` — Create a Data Channel Track Creates a `LocalDataTrack` for sending arbitrary data (strings, Blobs, ArrayBuffers) to all subscribed remote participants over WebRTC data channels. Configurable reliability (ordered delivery, retransmit limits, packet lifetime). ### Parameters * **options** (object) - Optional configuration for the data track. * **name** (string) - The name of the data track. * **maxRetransmits** (number) - The maximum number of retransmissions for unreliable data. * **ordered** (boolean) - Whether the data channel should deliver messages in order. ### Returns A `LocalDataTrack` object. ### Example ```javascript const Video = require('twilio-video'); // Reliable, ordered data track (default) const mouseTrack = new Video.LocalDataTrack({ name: 'mouse-position' }); // Unreliable, unordered for low-latency gaming-style updates const gameStateTrack = new Video.LocalDataTrack({ maxRetransmits: 0, ordered: false }); // Connect with the data track Video.connect('YOUR_ACCESS_TOKEN', { name: 'my-room', tracks: [mouseTrack] }).then(room => { // Send mouse position updates to all remote participants window.addEventListener('mousemove', event => { mouseTrack.send(JSON.stringify({ x: event.clientX, y: event.clientY })); }); // Send binary data mouseTrack.send(new Uint8Array([1, 2, 3, 4]).buffer); // Remote side: receive the data room.on('trackSubscribed', track => { if (track.kind === 'data') { track.on('message', data => { const position = JSON.parse(data); console.log('Remote cursor:', position.x, position.y); }); } }); }); ``` ``` -------------------------------- ### SDKDriver Messaging Protocol Usage Source: https://github.com/twilio/twilio-video.js/blob/master/test/lib/sdkdriver/README.md Demonstrates how to use the SDKDriver to send requests to the test process and handle events. It shows how to register an event listener and send a request, then log the response. ```javascript const driver = await SDKDriver.create(transport, testDriver); driver.on('event', data => { console.log('New event from the test process:', data); }); const response = await driver.sendRequest({ call: 'some-api' }); console.log('Response from the test process:', response); ``` -------------------------------- ### Create Local Audio Track with Krisp Source: https://context7.com/twilio/twilio-video.js/llms.txt Acquires a local audio track from the microphone, enabling Krisp noise cancellation and automatic default device tracking. Listen for mute events and publish the track to a room. ```javascript const Video = require('twilio-video'); // Create an audio track with Krisp noise cancellation Video.createLocalAudioTrack({ name: 'microphone', noiseCancellationOptions: { vendor: 'krisp', sdkAssetsPath: '/assets/twilio-krisp-audio-plugin/1.0.0/dist' }, defaultDeviceCaptureMode: 'auto' // auto-restart on default device change }).then(localAudioTrack => { console.log('Audio track name:', localAudioTrack.name); console.log('Is muted:', localAudioTrack.isMuted); // Toggle noise cancellation at runtime if (localAudioTrack.noiseCancellation) { localAudioTrack.noiseCancellation.enable(); localAudioTrack.noiseCancellation.disable(); } // Listen for mute events (e.g., phone call interruption on mobile) localAudioTrack.on('muted', () => console.warn('Microphone was muted by OS')); localAudioTrack.on('unmuted', () => console.log('Microphone restored')); // Publish into an already-connected room return room.localParticipant.publishTrack(localAudioTrack); }).catch(err => console.error('Audio track error:', err)); ``` -------------------------------- ### Set Dynamic Bitrate Limits for Local Participant Source: https://context7.com/twilio/twilio-video.js/llms.txt Adjust outgoing audio and video bitrate limits for the LocalParticipant at runtime without needing to reconnect. Pass `null` to remove all limits, or an object with specific limits to set them. ```javascript Video.connect('YOUR_ACCESS_TOKEN', { name: 'my-room' }).then(room => { const { localParticipant } = room; // Set aggressive bitrate limits (e.g., user reports poor network) localParticipant.setParameters({ maxAudioBitrate: 16000, // 16 kbps maxVideoBitrate: 800000 // 800 kbps }); // Remove bitrate limits localParticipant.setParameters(null); // Restore only video limit localParticipant.setParameters({ maxVideoBitrate: 2400000 }); }); ``` -------------------------------- ### Video.createLocalVideoTrack(options) Source: https://context7.com/twilio/twilio-video.js/llms.txt Acquires a single LocalVideoTrack from the camera, with full support for MediaTrackConstraints. ```APIDOC ## `Video.createLocalVideoTrack(options)` — Acquire a Single Video Track Acquires a single `LocalVideoTrack` from the camera, with full support for `MediaTrackConstraints` (resolution, frame rate, facing mode, device ID). On mobile browsers, only one `LocalVideoTrack` can hold the camera at a time. ### Parameters * **options** (object) - Optional configuration for the video track. * **name** (string) - The name of the video track. * **facingMode** (string) - Specifies which camera to use ('user' for front, 'environment' for back). * **width** (object) - Constraints for the video width. * **ideal** (number) - The ideal width. * **max** (number) - The maximum allowed width. * **height** (object) - Constraints for the video height. * **ideal** (number) - The ideal height. * **max** (number) - The maximum allowed height. * **frameRate** (object) - Constraints for the video frame rate. * **ideal** (number) - The ideal frame rate. * **max** (number) - The maximum allowed frame rate. ### Returns A `Promise` that resolves with a `LocalVideoTrack` object. ### Example ```javascript const Video = require('twilio-video'); Video.createLocalVideoTrack({ name: 'camera', facingMode: 'environment', // 'user' for front camera width: { ideal: 1280, max: 1920 }, height: { ideal: 720, max: 1080 }, frameRate: { ideal: 30, max: 60 } }).then(localVideoTrack => { console.log('Video track name:', localVideoTrack.name); // Attach to a