Skip to content

Commit

Permalink
fix(RTC): add additional checks for creating tracks via mediastream
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMcAssey committed Dec 8, 2023
1 parent 18de315 commit 83f98f9
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
43 changes: 43 additions & 0 deletions JitsiMeetJS.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import JitsiMeetJS from './JitsiMeetJS';
import { VideoType } from './service/RTC/VideoType';
import { MediaType } from './service/RTC/MediaType';
import { JitsiTrackErrors } from './JitsiTrackErrors';

describe('JitsiMeetJS', () => {
describe('createLocalTracksFromMediaStreams', () => {
it('creates a local track from a media stream', () => {
const canvas = document.createElement('canvas');

const canvasStream = canvas.captureStream(5);
const trackInfo = {
stream: canvasStream,
sourceType: 'canvas',
mediaType: MediaType.VIDEO,
track: canvasStream.getVideoTracks()[0],
videoType: VideoType.DESKTOP
};
const newTracks = JitsiMeetJS.createLocalTracksFromMediaStreams([ trackInfo ]);

expect(newTracks).toBeDefined();
expect(newTracks.length).toBe(1);
});

it('throws an error if track is not from the same stream', () => {
const canvas = document.createElement('canvas');
const otherCanvas = document.createElement('canvas');

const canvasStream = canvas.captureStream(5);
const otherCanvasStream = otherCanvas.captureStream(5);
const trackInfo = {
stream: canvasStream,
sourceType: 'canvas',
mediaType: MediaType.VIDEO,
track: otherCanvasStream.getVideoTracks()[0],
videoType: VideoType.DESKTOP
};

expect(() => JitsiMeetJS.createLocalTracksFromMediaStreams([ trackInfo ]))
.toThrowError(JitsiTrackErrors.TRACK_MISMATCHED_STREAM);
});
});
});
32 changes: 30 additions & 2 deletions JitsiMeetJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import * as ConnectionQualityEvents
import * as E2ePingEvents from './service/e2eping/E2ePingEvents';
import { createGetUserMediaEvent } from './service/statistics/AnalyticsEvents';
import * as RTCStatsEvents from './modules/RTCStats/RTCStatsEvents';
import { VideoType } from './service/RTC/VideoType';

const logger = Logger.getLogger(__filename);

Expand Down Expand Up @@ -90,6 +91,14 @@ interface IJitsiMeetJSOptions {
}
}

interface ICreateLocalTrackFromMediaStreamOptions {
stream: MediaStream,
sourceType: string,
mediaType: MediaType,
track: MediaStreamTrack,
videoType?: VideoType
}

/**
* The public API of the Jitsi Meet library (a.k.a. {@code JitsiMeetJS}).
*/
Expand Down Expand Up @@ -424,11 +433,30 @@ export default {
/**
* Manually create JitsiLocalTrack's from the provided track info, by exposing the RTC method
*
* @param {Array<Object>} tracksInfo - array of track information
* @param {Array<ICreateLocalTrackFromMediaStreamOptions>} tracksInfo - array of track information
* @returns {Array<JitsiLocalTrack>} - created local tracks
*/
createLocalTracksFromMediaStreams(tracksInfo) {
return RTC.createLocalTracks(tracksInfo);
return RTC.createLocalTracks(tracksInfo.map((trackInfo) => {
const tracks = trackInfo.stream.getTracks();
if (!tracks || tracks.length === 0) {
throw new JitsiTrackError(JitsiTrackErrors.TRACK_NO_STREAM_TRACKS_FOUND);

Check failure on line 443 in JitsiMeetJS.ts

View workflow job for this annotation

GitHub Actions / Build

Expected 3 arguments, but got 1.
}

if (tracks.length > 1) {
throw new JitsiTrackError(JitsiTrackErrors.TRACK_TOO_MANY_TRACKS_IN_STREAM);

Check failure on line 447 in JitsiMeetJS.ts

View workflow job for this annotation

GitHub Actions / Build

Expected 3 arguments, but got 1.
}

if (tracks.indexOf(trackInfo.track) === -1) {
throw new JitsiTrackError(JitsiTrackErrors.TRACK_MISMATCHED_STREAM);

Check failure on line 451 in JitsiMeetJS.ts

View workflow job for this annotation

GitHub Actions / Build

Expected 3 arguments, but got 1.
}

if (!trackInfo.sourceId) {
trackInfo.sourceId = 'GENERATEDVALUEHERE';
}

return trackInfo;
}));
},

/**
Expand Down
20 changes: 19 additions & 1 deletion JitsiTrackErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,22 @@ export enum JitsiTrackErrors {
* An error which indicates that requested video resolution is not supported
* by a webcam.
*/
UNSUPPORTED_RESOLUTION = 'gum.unsupported_resolution'
UNSUPPORTED_RESOLUTION = 'gum.unsupported_resolution',

/**
* An error which indicates that there are too many tracks in the provided media stream
*/
TRACK_TOO_MANY_TRACKS_IN_STREAM = 'track.too_many_tracks_in_stream',

/**
* An error which indicates that no tracks were found in the media stream
*/
TRACK_NO_STREAM_TRACKS_FOUND = 'track.no_stream_tracks_found',

/**
* An error which indicates that the track does not belong to the provided media stream
*/
TRACK_MISMATCHED_STREAM = 'track.mismatched_stream'
}

// exported for backward compatibility
Expand All @@ -84,3 +99,6 @@ export const TIMEOUT = JitsiTrackErrors.TIMEOUT;
export const TRACK_IS_DISPOSED = JitsiTrackErrors.TRACK_IS_DISPOSED;
export const TRACK_NO_STREAM_FOUND = JitsiTrackErrors.TRACK_NO_STREAM_FOUND;
export const UNSUPPORTED_RESOLUTION = JitsiTrackErrors.UNSUPPORTED_RESOLUTION;
export const TRACK_TOO_MANY_TRACKS_IN_STREAM = JitsiTrackErrors.TRACK_TOO_MANY_TRACKS_IN_STREAM;
export const TRACK_NO_STREAM_TRACKS_FOUND = JitsiTrackErrors.TRACK_NO_STREAM_TRACKS_FOUND;
export const TRACK_MISMATCHED_STREAM = JitsiTrackErrors.TRACK_MISMATCHED_STREAM;

0 comments on commit 83f98f9

Please sign in to comment.