Skip to content

Commit

Permalink
fix(RTC): simplify options to create track
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMcAssey committed Dec 8, 2023
1 parent 83f98f9 commit fe276cb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
10 changes: 3 additions & 7 deletions JitsiMeetJS.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ describe('JitsiMeetJS', () => {
stream: canvasStream,
sourceType: 'canvas',
mediaType: MediaType.VIDEO,
track: canvasStream.getVideoTracks()[0],
videoType: VideoType.DESKTOP
};
const newTracks = JitsiMeetJS.createLocalTracksFromMediaStreams([ trackInfo ]);
Expand All @@ -22,22 +21,19 @@ describe('JitsiMeetJS', () => {
expect(newTracks.length).toBe(1);
});

it('throws an error if track is not from the same stream', () => {
it('throws an error if track is not the correct media type', () => {
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],
mediaType: MediaType.AUDIO,
videoType: VideoType.DESKTOP
};

expect(() => JitsiMeetJS.createLocalTracksFromMediaStreams([ trackInfo ]))
.toThrowError(JitsiTrackErrors.TRACK_MISMATCHED_STREAM);
.toThrowError(JitsiTrackErrors.TRACK_NO_STREAM_TRACKS_FOUND);
});
});
});
18 changes: 13 additions & 5 deletions JitsiMeetJS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ interface ICreateLocalTrackFromMediaStreamOptions {
stream: MediaStream,
sourceType: string,
mediaType: MediaType,
track: MediaStreamTrack,
videoType?: VideoType
}

Expand Down Expand Up @@ -439,16 +438,25 @@ export default {
createLocalTracksFromMediaStreams(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);
throw new JitsiTrackError(JitsiTrackErrors.TRACK_NO_STREAM_TRACKS_FOUND, null, null);
}

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

if (trackInfo.mediaType === MediaType.AUDIO) {
trackInfo.track = tracks.find(track => track.kind === 'audio');
} else if (trackInfo.mediaType === MediaType.VIDEO) {
trackInfo.track = tracks.find(track => track.kind === 'video');
} else {
throw new JitsiTrackError(JitsiTrackErrors.NOT_FOUND, null, null);
}

if (tracks.indexOf(trackInfo.track) === -1) {
throw new JitsiTrackError(JitsiTrackErrors.TRACK_MISMATCHED_STREAM);
if (!trackInfo.track) {
throw new JitsiTrackError(JitsiTrackErrors.TRACK_NO_STREAM_TRACKS_FOUND, null, null);
}

if (!trackInfo.sourceId) {
Expand Down
6 changes: 0 additions & 6 deletions JitsiTrackErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,6 @@ export enum JitsiTrackErrors {
* 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 @@ -101,4 +96,3 @@ 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 fe276cb

Please sign in to comment.